xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 20da44dfe8eff5b61685e394dec690a5d9dc36ce)
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"
37831df883SMaor Gottlieb #include "dm.h"
38f7c4ffdaSLeon Romanovsky #include "fs.h"
39f3da6577SLeon Romanovsky #include "srq.h"
40333fbaa0SLeon Romanovsky #include "qp.h"
41029e88fdSLeon Romanovsky #include "wr.h"
42b572ebe6SLeon Romanovsky #include "restrack.h"
4364825827SLeon Romanovsky #include "counters.h"
44c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h>
458c84660bSMatan Barak #include <rdma/uverbs_std_types.h>
462904bb37SYishai Hadas #include <rdma/uverbs_ioctl.h>
47c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h>
48c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h>
494061ff7aSErez Alfasi #include <rdma/ib_umem_odp.h>
508c84660bSMatan Barak 
518c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
528c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
53e126ba97SEli Cohen 
54e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
55daeee976SShay Drory MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) IB driver");
56e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
57e126ba97SEli Cohen 
58d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
59d69a24e0SDaniel Jurgens 	struct work_struct	work;
60df097a27SSaeed Mahameed 	union {
61df097a27SSaeed Mahameed 		struct mlx5_ib_dev	      *dev;
62df097a27SSaeed Mahameed 		struct mlx5_ib_multiport_info *mpi;
63df097a27SSaeed Mahameed 	};
64df097a27SSaeed Mahameed 	bool			is_slave;
65134e9349SSaeed Mahameed 	unsigned int		event;
66df097a27SSaeed Mahameed 	void			*param;
67d69a24e0SDaniel Jurgens };
68d69a24e0SDaniel Jurgens 
69da7525d2SEran Ben Elisha enum {
70da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
71da7525d2SEran Ben Elisha };
721b5daf11SMajd Dibbiny 
73d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
7432f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
7532f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
7632f69e4bSDaniel Jurgens /*
7732f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
7832f69e4bSDaniel Jurgens  */
7932f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
8032f69e4bSDaniel Jurgens 
8132f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
8232f69e4bSDaniel Jurgens {
8332f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
8432f69e4bSDaniel Jurgens 
8532f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
8632f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
8732f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
8832f69e4bSDaniel Jurgens 	return dev;
8932f69e4bSDaniel Jurgens }
9032f69e4bSDaniel Jurgens 
911b5daf11SMajd Dibbiny static enum rdma_link_layer
92ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
931b5daf11SMajd Dibbiny {
94ebd61f68SAchiad Shochat 	switch (port_type_cap) {
951b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
961b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
971b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
981b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
991b5daf11SMajd Dibbiny 	default:
1001b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1011b5daf11SMajd Dibbiny 	}
1021b5daf11SMajd Dibbiny }
1031b5daf11SMajd Dibbiny 
104ebd61f68SAchiad Shochat static enum rdma_link_layer
1051fb7f897SMark Bloch mlx5_ib_port_link_layer(struct ib_device *device, u32 port_num)
106ebd61f68SAchiad Shochat {
107ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
108ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
109ebd61f68SAchiad Shochat 
110ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
111ebd61f68SAchiad Shochat }
112ebd61f68SAchiad Shochat 
113fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
1141fb7f897SMark Bloch 			  u32 port_num,
115fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
116fd65f1b8SMoni Shoua {
117fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
118fd65f1b8SMoni Shoua 	int ret;
119fd65f1b8SMoni Shoua 
120fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1213023a1e9SKamal Heib 	ret = ibdev->ops.query_port(ibdev, port_num, &attr);
122fd65f1b8SMoni Shoua 	if (!ret)
123fd65f1b8SMoni Shoua 		*state = attr.state;
124fd65f1b8SMoni Shoua 	return ret;
125fd65f1b8SMoni Shoua }
126fd65f1b8SMoni Shoua 
12735b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
12835b0aa67SMark Bloch 					   struct net_device *ndev,
1291fb7f897SMark Bloch 					   u32 *port_num)
13035b0aa67SMark Bloch {
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);
141658cfcebSMark Bloch 		rep_ndev = mlx5_ib_get_rep_netdev(port->rep->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);
1591fb7f897SMark Bloch 	u32 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,
2381fb7f897SMark Bloch 					     u32 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,
2661fb7f897SMark Bloch 						   u32 ib_port_num,
2671fb7f897SMark Bloch 						   u32 *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 
3011fb7f897SMark Bloch void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u32 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 
4571fb7f897SMark Bloch static int mlx5_query_port_roce(struct ib_device *device, u32 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;
4671fb7f897SMark Bloch 	u32 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 
5541fb7f897SMark Bloch static int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num,
555095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
556095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
5573cca2606SAchiad Shochat {
558dedbc2d3SParav Pandit 	enum ib_gid_type gid_type;
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 	gid_type = attr->gid_type;
566dedbc2d3SParav Pandit 	if (gid) {
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;
578dedbc2d3SParav Pandit 		if (gid && 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,
605dedbc2d3SParav Pandit 			     attr->index, NULL, attr);
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 
117711656f59SLior Nahmanson 	if (offsetofend(typeof(resp), dci_streams_caps) <= uhw_outlen) {
117811656f59SLior Nahmanson 		resp.response_length += sizeof(resp.dci_streams_caps);
117911656f59SLior Nahmanson 
118011656f59SLior Nahmanson 		resp.dci_streams_caps.max_log_num_concurent =
118111656f59SLior Nahmanson 			MLX5_CAP_GEN(mdev, log_max_dci_stream_channels);
118211656f59SLior Nahmanson 
118311656f59SLior Nahmanson 		resp.dci_streams_caps.max_log_num_errored =
118411656f59SLior Nahmanson 			MLX5_CAP_GEN(mdev, log_max_dci_errored_streams);
118511656f59SLior Nahmanson 	}
118611656f59SLior Nahmanson 
118748357091SLeon Romanovsky 	if (uhw_outlen) {
1188402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1189402ca536SBodong Wang 
1190402ca536SBodong Wang 		if (err)
1191402ca536SBodong Wang 			return err;
1192402ca536SBodong Wang 	}
1193402ca536SBodong Wang 
11941b5daf11SMajd Dibbiny 	return 0;
11951b5daf11SMajd Dibbiny }
1196e126ba97SEli Cohen 
1197639bf441SAharon Landau static void translate_active_width(struct ib_device *ibdev, u16 active_width,
11981b5daf11SMajd Dibbiny 				   u8 *ib_width)
11991b5daf11SMajd Dibbiny {
12001b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12011b5daf11SMajd Dibbiny 
1202e27014bdSAharon Landau 	if (active_width & MLX5_PTYS_WIDTH_1X)
12031b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1204e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_2X)
1205d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1206e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_4X)
12071b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1208e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_8X)
12091b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1210e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_12X)
12111b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1212db7a691aSMichael Guralnik 	else {
1213db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
1214e27014bdSAharon Landau 			    active_width);
1215db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
12161b5daf11SMajd Dibbiny 	}
12171b5daf11SMajd Dibbiny 
1218db7a691aSMichael Guralnik 	return;
12191b5daf11SMajd Dibbiny }
12201b5daf11SMajd Dibbiny 
12211b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
12221b5daf11SMajd Dibbiny {
12231b5daf11SMajd Dibbiny 	switch (mtu) {
12241b5daf11SMajd Dibbiny 	case 256: return 1;
12251b5daf11SMajd Dibbiny 	case 512: return 2;
12261b5daf11SMajd Dibbiny 	case 1024: return 3;
12271b5daf11SMajd Dibbiny 	case 2048: return 4;
12281b5daf11SMajd Dibbiny 	case 4096: return 5;
12291b5daf11SMajd Dibbiny 	default:
12301b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
12311b5daf11SMajd Dibbiny 		return -1;
12321b5daf11SMajd Dibbiny 	}
12331b5daf11SMajd Dibbiny }
12341b5daf11SMajd Dibbiny 
12351b5daf11SMajd Dibbiny enum ib_max_vl_num {
12361b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
12371b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
12381b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
12391b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
12401b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
12411b5daf11SMajd Dibbiny };
12421b5daf11SMajd Dibbiny 
12431b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
12441b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
12451b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
12461b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
12471b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
12481b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
12491b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
12501b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
12511b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
12521b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
12531b5daf11SMajd Dibbiny };
12541b5daf11SMajd Dibbiny 
12551b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
12561b5daf11SMajd Dibbiny 				u8 *max_vl_num)
12571b5daf11SMajd Dibbiny {
12581b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
12591b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
12601b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
12611b5daf11SMajd Dibbiny 		break;
12621b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
12631b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
12641b5daf11SMajd Dibbiny 		break;
12651b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
12661b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
12671b5daf11SMajd Dibbiny 		break;
12681b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
12691b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
12701b5daf11SMajd Dibbiny 		break;
12711b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
12721b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
12731b5daf11SMajd Dibbiny 		break;
12741b5daf11SMajd Dibbiny 
12751b5daf11SMajd Dibbiny 	default:
12761b5daf11SMajd Dibbiny 		return -EINVAL;
12771b5daf11SMajd Dibbiny 	}
12781b5daf11SMajd Dibbiny 
12791b5daf11SMajd Dibbiny 	return 0;
12801b5daf11SMajd Dibbiny }
12811b5daf11SMajd Dibbiny 
12821fb7f897SMark Bloch static int mlx5_query_hca_port(struct ib_device *ibdev, u32 port,
12831b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
12841b5daf11SMajd Dibbiny {
12851b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12861b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
12871b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1288046339eaSSaeed Mahameed 	u16 max_mtu;
1289046339eaSSaeed Mahameed 	u16 oper_mtu;
12901b5daf11SMajd Dibbiny 	int err;
1291639bf441SAharon Landau 	u16 ib_link_width_oper;
12921b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
12931b5daf11SMajd Dibbiny 
12941b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
12951b5daf11SMajd Dibbiny 	if (!rep) {
12961b5daf11SMajd Dibbiny 		err = -ENOMEM;
12971b5daf11SMajd Dibbiny 		goto out;
12981b5daf11SMajd Dibbiny 	}
12991b5daf11SMajd Dibbiny 
1300c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
13011b5daf11SMajd Dibbiny 
13021b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
13031b5daf11SMajd Dibbiny 	if (err)
13041b5daf11SMajd Dibbiny 		goto out;
13051b5daf11SMajd Dibbiny 
13061b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
13071b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
13081b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
13091b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
13101b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
13111b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
13121b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
13131b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
13141b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
13151b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
13161b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
13171b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
13181b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
13191b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
13201b5daf11SMajd Dibbiny 
13214106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
13224106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
13234106a758SMichael Guralnik 
1324639bf441SAharon Landau 	err = mlx5_query_ib_port_oper(mdev, &ib_link_width_oper,
1325376ceb31SAharon Landau 				      &props->active_speed, port);
13261b5daf11SMajd Dibbiny 	if (err)
13271b5daf11SMajd Dibbiny 		goto out;
13281b5daf11SMajd Dibbiny 
1329db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1330db7a691aSMichael Guralnik 
1331facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
13321b5daf11SMajd Dibbiny 
13331b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
13341b5daf11SMajd Dibbiny 
1335facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
13361b5daf11SMajd Dibbiny 
13371b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
13381b5daf11SMajd Dibbiny 
13391b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
13401b5daf11SMajd Dibbiny 	if (err)
13411b5daf11SMajd Dibbiny 		goto out;
13421b5daf11SMajd Dibbiny 
13431b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
13441b5daf11SMajd Dibbiny 				   &props->max_vl_num);
13451b5daf11SMajd Dibbiny out:
13461b5daf11SMajd Dibbiny 	kfree(rep);
1347e126ba97SEli Cohen 	return err;
1348e126ba97SEli Cohen }
1349e126ba97SEli Cohen 
13501fb7f897SMark Bloch int mlx5_ib_query_port(struct ib_device *ibdev, u32 port,
1351e126ba97SEli Cohen 		       struct ib_port_attr *props)
1352e126ba97SEli Cohen {
1353095b0927SIlan Tayari 	unsigned int count;
1354095b0927SIlan Tayari 	int ret;
1355095b0927SIlan Tayari 
13561b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13571b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1358095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1359095b0927SIlan Tayari 		break;
1360e126ba97SEli Cohen 
13611b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1362095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1363095b0927SIlan Tayari 		break;
13641b5daf11SMajd Dibbiny 
13653f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1366095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1367095b0927SIlan Tayari 		break;
13683f89a643SAchiad Shochat 
13691b5daf11SMajd Dibbiny 	default:
1370095b0927SIlan Tayari 		ret = -EINVAL;
1371e126ba97SEli Cohen 	}
1372095b0927SIlan Tayari 
1373095b0927SIlan Tayari 	if (!ret && props) {
1374b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1375b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1376b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1377b3cbd6f0SDaniel Jurgens 
1378b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1379b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1380b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1381b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1382b3cbd6f0SDaniel Jurgens 			 */
1383b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1384b3cbd6f0SDaniel Jurgens 			port = 1;
1385b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1386b3cbd6f0SDaniel Jurgens 		}
1387b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1388b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1389b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1390095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1391095b0927SIlan Tayari 	}
1392095b0927SIlan Tayari 	return ret;
1393e126ba97SEli Cohen }
1394e126ba97SEli Cohen 
13951fb7f897SMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u32 port,
13968e6efa3aSMark Bloch 				  struct ib_port_attr *props)
13978e6efa3aSMark Bloch {
13987a58779eSParav Pandit 	return mlx5_query_port_roce(ibdev, port, props);
13998e6efa3aSMark Bloch }
14008e6efa3aSMark Bloch 
14011fb7f897SMark Bloch static int mlx5_ib_rep_query_pkey(struct ib_device *ibdev, u32 port, u16 index,
1402d6fd59e1SParav Pandit 				  u16 *pkey)
1403d6fd59e1SParav Pandit {
1404d6fd59e1SParav Pandit 	/* Default special Pkey for representor device port as per the
1405d6fd59e1SParav Pandit 	 * IB specification 1.3 section 10.9.1.2.
1406d6fd59e1SParav Pandit 	 */
1407d6fd59e1SParav Pandit 	*pkey = 0xffff;
1408d6fd59e1SParav Pandit 	return 0;
1409d6fd59e1SParav Pandit }
1410d6fd59e1SParav Pandit 
14111fb7f897SMark Bloch static int mlx5_ib_query_gid(struct ib_device *ibdev, u32 port, int index,
1412e126ba97SEli Cohen 			     union ib_gid *gid)
1413e126ba97SEli Cohen {
14141b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
14151b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1416e126ba97SEli Cohen 
14171b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14181b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14191b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1420e126ba97SEli Cohen 
14211b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14221b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1423e126ba97SEli Cohen 
14241b5daf11SMajd Dibbiny 	default:
14251b5daf11SMajd Dibbiny 		return -EINVAL;
14261b5daf11SMajd Dibbiny 	}
1427e126ba97SEli Cohen 
1428e126ba97SEli Cohen }
1429e126ba97SEli Cohen 
14301fb7f897SMark Bloch static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u32 port,
1431b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1432b3cbd6f0SDaniel Jurgens {
1433b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1434b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1435b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
14361fb7f897SMark Bloch 	u32 mdev_port_num;
1437b3cbd6f0SDaniel Jurgens 	int err;
1438b3cbd6f0SDaniel Jurgens 
1439b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1440b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1441b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1442b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1443b3cbd6f0SDaniel Jurgens 		 */
1444b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1445b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1446b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1447b3cbd6f0SDaniel Jurgens 	}
1448b3cbd6f0SDaniel Jurgens 
1449b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1450b3cbd6f0SDaniel Jurgens 					index, pkey);
1451b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1452b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1453b3cbd6f0SDaniel Jurgens 
1454b3cbd6f0SDaniel Jurgens 	return err;
1455b3cbd6f0SDaniel Jurgens }
1456b3cbd6f0SDaniel Jurgens 
14571fb7f897SMark Bloch static int mlx5_ib_query_pkey(struct ib_device *ibdev, u32 port, u16 index,
1458e126ba97SEli Cohen 			      u16 *pkey)
1459e126ba97SEli Cohen {
14601b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14611b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14621b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1463e126ba97SEli Cohen 
14641b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14651b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1466b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
14671b5daf11SMajd Dibbiny 	default:
14681b5daf11SMajd Dibbiny 		return -EINVAL;
1469e126ba97SEli Cohen 	}
14701b5daf11SMajd Dibbiny }
1471e126ba97SEli Cohen 
1472e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1473e126ba97SEli Cohen 				 struct ib_device_modify *props)
1474e126ba97SEli Cohen {
1475e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1476e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1477e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1478e126ba97SEli Cohen 	int err;
1479e126ba97SEli Cohen 
1480e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1481e126ba97SEli Cohen 		return -EOPNOTSUPP;
1482e126ba97SEli Cohen 
1483e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1484e126ba97SEli Cohen 		return 0;
1485e126ba97SEli Cohen 
1486e126ba97SEli Cohen 	/*
1487e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1488e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1489e126ba97SEli Cohen 	 */
1490bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
14919603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1492e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1493e126ba97SEli Cohen 	if (err)
1494e126ba97SEli Cohen 		return err;
1495e126ba97SEli Cohen 
1496bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1497e126ba97SEli Cohen 
1498e126ba97SEli Cohen 	return err;
1499e126ba97SEli Cohen }
1500e126ba97SEli Cohen 
15011fb7f897SMark Bloch static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u32 port_num, u32 mask,
1502cdbe33d0SEli Cohen 				u32 value)
1503cdbe33d0SEli Cohen {
1504cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1505b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
15061fb7f897SMark Bloch 	u32 mdev_port_num;
1507cdbe33d0SEli Cohen 	int err;
1508cdbe33d0SEli Cohen 
1509b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1510b3cbd6f0SDaniel Jurgens 	if (!mdev)
1511b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1512b3cbd6f0SDaniel Jurgens 
1513b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1514cdbe33d0SEli Cohen 	if (err)
1515b3cbd6f0SDaniel Jurgens 		goto out;
1516cdbe33d0SEli Cohen 
1517cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1518cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1519cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1520b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1521b3cbd6f0SDaniel Jurgens 		goto out;
1522cdbe33d0SEli Cohen 	}
1523cdbe33d0SEli Cohen 
1524cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1525cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1526b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1527b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1528b3cbd6f0SDaniel Jurgens 
1529b3cbd6f0SDaniel Jurgens out:
1530b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1531cdbe33d0SEli Cohen 
1532cdbe33d0SEli Cohen 	return err;
1533cdbe33d0SEli Cohen }
1534cdbe33d0SEli Cohen 
15351fb7f897SMark Bloch static int mlx5_ib_modify_port(struct ib_device *ibdev, u32 port, int mask,
1536e126ba97SEli Cohen 			       struct ib_port_modify *props)
1537e126ba97SEli Cohen {
1538e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1539e126ba97SEli Cohen 	struct ib_port_attr attr;
1540e126ba97SEli Cohen 	u32 tmp;
1541e126ba97SEli Cohen 	int err;
1542cdbe33d0SEli Cohen 	u32 change_mask;
1543cdbe33d0SEli Cohen 	u32 value;
1544cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1545cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1546cdbe33d0SEli Cohen 
1547ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1548ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1549ec255879SMajd Dibbiny 	 */
1550ec255879SMajd Dibbiny 	if (!is_ib)
1551ec255879SMajd Dibbiny 		return 0;
1552ec255879SMajd Dibbiny 
1553cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1554cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1555cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1556cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1557cdbe33d0SEli Cohen 	}
1558e126ba97SEli Cohen 
1559e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1560e126ba97SEli Cohen 
1561c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1562e126ba97SEli Cohen 	if (err)
1563e126ba97SEli Cohen 		goto out;
1564e126ba97SEli Cohen 
1565e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1566e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1567e126ba97SEli Cohen 
15689603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1569e126ba97SEli Cohen 
1570e126ba97SEli Cohen out:
1571e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1572e126ba97SEli Cohen 	return err;
1573e126ba97SEli Cohen }
1574e126ba97SEli Cohen 
157530aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
157630aa60b3SEli Cohen {
157730aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
157830aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
157930aa60b3SEli Cohen }
158030aa60b3SEli Cohen 
158131a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
158231a78a5aSYishai Hadas {
158331a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
158431a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
158531a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
158631a78a5aSYishai Hadas 
158731a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
158831a78a5aSYishai Hadas }
158931a78a5aSYishai Hadas 
1590b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1591b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
159231a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1593b037c29aSEli Cohen {
1594b037c29aSEli Cohen 	int uars_per_sys_page;
1595b037c29aSEli Cohen 	int bfregs_per_sys_page;
1596b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1597b037c29aSEli Cohen 
1598b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1599b037c29aSEli Cohen 		return -EINVAL;
1600b037c29aSEli Cohen 
1601b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1602b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1603b037c29aSEli Cohen 
1604b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1605b037c29aSEli Cohen 		return -ENOMEM;
1606b037c29aSEli Cohen 
1607b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1608b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
160931a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1610b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1611b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1612b037c29aSEli Cohen 		return -EINVAL;
1613b037c29aSEli Cohen 
161431a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
161531a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
161631a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
161731a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
161831a78a5aSYishai Hadas 
161931a78a5aSYishai 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",
1620b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1621b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
162231a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
162331a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1624b037c29aSEli Cohen 
1625b037c29aSEli Cohen 	return 0;
1626b037c29aSEli Cohen }
1627b037c29aSEli Cohen 
1628b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1629b037c29aSEli Cohen {
1630b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1631b037c29aSEli Cohen 	int err;
1632b037c29aSEli Cohen 	int i;
1633b037c29aSEli Cohen 
1634b037c29aSEli Cohen 	bfregi = &context->bfregi;
163531a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1636b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1637b037c29aSEli Cohen 		if (err)
1638b037c29aSEli Cohen 			goto error;
1639b037c29aSEli Cohen 
1640b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1641b037c29aSEli Cohen 	}
16424ed131d0SYishai Hadas 
16434ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
16444ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
16454ed131d0SYishai Hadas 
1646b037c29aSEli Cohen 	return 0;
1647b037c29aSEli Cohen 
1648b037c29aSEli Cohen error:
1649b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1650b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1651b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1652b037c29aSEli Cohen 
1653b037c29aSEli Cohen 	return err;
1654b037c29aSEli Cohen }
1655b037c29aSEli Cohen 
165615177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
165715177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1658b037c29aSEli Cohen {
1659b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1660b037c29aSEli Cohen 	int i;
1661b037c29aSEli Cohen 
1662b037c29aSEli Cohen 	bfregi = &context->bfregi;
166315177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
16644ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
166515177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
166615177999SLeon Romanovsky 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1667b037c29aSEli Cohen }
1668b037c29aSEli Cohen 
16690042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1670a560f1d9SMark Bloch {
1671a560f1d9SMark Bloch 	int err = 0;
1672a560f1d9SMark Bloch 
1673a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16740042f9e4SMark Bloch 	if (td)
1675a560f1d9SMark Bloch 		dev->lb.user_td++;
16760042f9e4SMark Bloch 	if (qp)
16770042f9e4SMark Bloch 		dev->lb.qps++;
1678a560f1d9SMark Bloch 
16790042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
16800042f9e4SMark Bloch 	    dev->lb.qps == 1) {
16810042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1682a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
16830042f9e4SMark Bloch 			dev->lb.enabled = true;
16840042f9e4SMark Bloch 		}
16850042f9e4SMark Bloch 	}
1686a560f1d9SMark Bloch 
1687a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1688a560f1d9SMark Bloch 
1689a560f1d9SMark Bloch 	return err;
1690a560f1d9SMark Bloch }
1691a560f1d9SMark Bloch 
16920042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1693a560f1d9SMark Bloch {
1694a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16950042f9e4SMark Bloch 	if (td)
1696a560f1d9SMark Bloch 		dev->lb.user_td--;
16970042f9e4SMark Bloch 	if (qp)
16980042f9e4SMark Bloch 		dev->lb.qps--;
1699a560f1d9SMark Bloch 
17000042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
17010042f9e4SMark Bloch 	    dev->lb.qps == 0) {
17020042f9e4SMark Bloch 		if (dev->lb.enabled) {
1703a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
17040042f9e4SMark Bloch 			dev->lb.enabled = false;
17050042f9e4SMark Bloch 		}
17060042f9e4SMark Bloch 	}
1707a560f1d9SMark Bloch 
1708a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1709a560f1d9SMark Bloch }
1710a560f1d9SMark Bloch 
1711d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1712d2d19121SYishai Hadas 					  u16 uid)
1713c85023e1SHuy Nguyen {
1714c85023e1SHuy Nguyen 	int err;
1715c85023e1SHuy Nguyen 
1716cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1717cfdeb893SLeon Romanovsky 		return 0;
1718cfdeb893SLeon Romanovsky 
1719d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1720c85023e1SHuy Nguyen 	if (err)
1721c85023e1SHuy Nguyen 		return err;
1722c85023e1SHuy Nguyen 
1723c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17248978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17258978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1726c85023e1SHuy Nguyen 		return err;
1727c85023e1SHuy Nguyen 
17280042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1729c85023e1SHuy Nguyen }
1730c85023e1SHuy Nguyen 
1731d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1732d2d19121SYishai Hadas 					     u16 uid)
1733c85023e1SHuy Nguyen {
1734cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1735cfdeb893SLeon Romanovsky 		return;
1736cfdeb893SLeon Romanovsky 
1737d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1738c85023e1SHuy Nguyen 
1739c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17408978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17418978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1742c85023e1SHuy Nguyen 		return;
1743c85023e1SHuy Nguyen 
17440042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1745c85023e1SHuy Nguyen }
1746c85023e1SHuy Nguyen 
174745ec21c9SYishai Hadas static int set_ucontext_resp(struct ib_ucontext *uctx,
174845ec21c9SYishai Hadas 			     struct mlx5_ib_alloc_ucontext_resp *resp)
174945ec21c9SYishai Hadas {
175045ec21c9SYishai Hadas 	struct ib_device *ibdev = uctx->device;
175145ec21c9SYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
175245ec21c9SYishai Hadas 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
175345ec21c9SYishai Hadas 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
175445ec21c9SYishai Hadas 	int err;
175545ec21c9SYishai Hadas 
175645ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
175745ec21c9SYishai Hadas 		err = mlx5_cmd_dump_fill_mkey(dev->mdev,
175845ec21c9SYishai Hadas 					      &resp->dump_fill_mkey);
175945ec21c9SYishai Hadas 		if (err)
176045ec21c9SYishai Hadas 			return err;
176145ec21c9SYishai Hadas 		resp->comp_mask |=
176245ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
176345ec21c9SYishai Hadas 	}
176445ec21c9SYishai Hadas 
176545ec21c9SYishai Hadas 	resp->qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
176645ec21c9SYishai Hadas 	if (dev->wc_support)
176745ec21c9SYishai Hadas 		resp->bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev,
176845ec21c9SYishai Hadas 						      log_bf_reg_size);
176945ec21c9SYishai Hadas 	resp->cache_line_size = cache_line_size();
177045ec21c9SYishai Hadas 	resp->max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
177145ec21c9SYishai Hadas 	resp->max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
177245ec21c9SYishai Hadas 	resp->max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
177345ec21c9SYishai Hadas 	resp->max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
177445ec21c9SYishai Hadas 	resp->max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
177545ec21c9SYishai Hadas 	resp->cqe_version = context->cqe_version;
177645ec21c9SYishai Hadas 	resp->log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
177745ec21c9SYishai Hadas 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
177845ec21c9SYishai Hadas 	resp->num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
177945ec21c9SYishai Hadas 					MLX5_CAP_GEN(dev->mdev,
178045ec21c9SYishai Hadas 						     num_of_uars_per_page) : 1;
178145ec21c9SYishai Hadas 
178245ec21c9SYishai Hadas 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
178345ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_DEVICE) {
178445ec21c9SYishai Hadas 		if (mlx5_get_flow_namespace(dev->mdev,
178545ec21c9SYishai Hadas 				MLX5_FLOW_NAMESPACE_EGRESS))
178645ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
178745ec21c9SYishai Hadas 		if (mlx5_accel_ipsec_device_caps(dev->mdev) &
178845ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
178945ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
179045ec21c9SYishai Hadas 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
179145ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
179245ec21c9SYishai Hadas 		if (mlx5_accel_ipsec_device_caps(dev->mdev) &
179345ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
179445ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
179545ec21c9SYishai Hadas 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
179645ec21c9SYishai Hadas 	}
179745ec21c9SYishai Hadas 
179845ec21c9SYishai Hadas 	resp->tot_bfregs = bfregi->lib_uar_dyn ? 0 :
179945ec21c9SYishai Hadas 			bfregi->total_num_bfregs - bfregi->num_dyn_bfregs;
180045ec21c9SYishai Hadas 	resp->num_ports = dev->num_ports;
180145ec21c9SYishai Hadas 	resp->cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
180245ec21c9SYishai Hadas 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
180345ec21c9SYishai Hadas 
180445ec21c9SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
180545ec21c9SYishai Hadas 		mlx5_query_min_inline(dev->mdev, &resp->eth_min_inline);
180645ec21c9SYishai Hadas 		resp->eth_min_inline++;
180745ec21c9SYishai Hadas 	}
180845ec21c9SYishai Hadas 
180945ec21c9SYishai Hadas 	if (dev->mdev->clock_info)
181045ec21c9SYishai Hadas 		resp->clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
181145ec21c9SYishai Hadas 
181245ec21c9SYishai Hadas 	/*
181345ec21c9SYishai Hadas 	 * We don't want to expose information from the PCI bar that is located
181445ec21c9SYishai Hadas 	 * after 4096 bytes, so if the arch only supports larger pages, let's
181545ec21c9SYishai Hadas 	 * pretend we don't support reading the HCA's core clock. This is also
181645ec21c9SYishai Hadas 	 * forced by mmap function.
181745ec21c9SYishai Hadas 	 */
181845ec21c9SYishai Hadas 	if (PAGE_SIZE <= 4096) {
181945ec21c9SYishai Hadas 		resp->comp_mask |=
182045ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
182145ec21c9SYishai Hadas 		resp->hca_core_clock_offset =
182245ec21c9SYishai Hadas 			offsetof(struct mlx5_init_seg,
182345ec21c9SYishai Hadas 				 internal_timer_h) % PAGE_SIZE;
182445ec21c9SYishai Hadas 	}
182545ec21c9SYishai Hadas 
182645ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, ece_support))
182745ec21c9SYishai Hadas 		resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_ECE;
182845ec21c9SYishai Hadas 
182933652951SAharon Landau 	if (rt_supported(MLX5_CAP_GEN(dev->mdev, sq_ts_format)) &&
183033652951SAharon Landau 	    rt_supported(MLX5_CAP_GEN(dev->mdev, rq_ts_format)) &&
183133652951SAharon Landau 	    rt_supported(MLX5_CAP_ROCE(dev->mdev, qp_ts_format)))
183233652951SAharon Landau 		resp->comp_mask |=
183333652951SAharon Landau 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_REAL_TIME_TS;
183433652951SAharon Landau 
183545ec21c9SYishai Hadas 	resp->num_dyn_bfregs = bfregi->num_dyn_bfregs;
1836c906b86eSSergey Gorenko 
1837c906b86eSSergey Gorenko 	if (MLX5_CAP_GEN(dev->mdev, drain_sigerr))
1838c906b86eSSergey Gorenko 		resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_SQD2RTS;
1839c906b86eSSergey Gorenko 
184045ec21c9SYishai Hadas 	return 0;
184145ec21c9SYishai Hadas }
184245ec21c9SYishai Hadas 
1843a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1844e126ba97SEli Cohen 				  struct ib_udata *udata)
1845e126ba97SEli Cohen {
1846a2a074efSLeon Romanovsky 	struct ib_device *ibdev = uctx->device;
1847e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1848b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1849b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1850a2a074efSLeon Romanovsky 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
18512f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
185278c0f98cSEli Cohen 	int ver;
1853e126ba97SEli Cohen 	int err;
1854a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1855a168a41cSMajd Dibbiny 				     max_cqe_version);
1856b037c29aSEli Cohen 	bool lib_uar_4k;
18570a2fd01cSYishai Hadas 	bool lib_uar_dyn;
1858e126ba97SEli Cohen 
1859e126ba97SEli Cohen 	if (!dev->ib_active)
1860a2a074efSLeon Romanovsky 		return -EAGAIN;
1861e126ba97SEli Cohen 
1862e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
186378c0f98cSEli Cohen 		ver = 0;
1864e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
186578c0f98cSEli Cohen 		ver = 2;
186678c0f98cSEli Cohen 	else
1867a2a074efSLeon Romanovsky 		return -EINVAL;
186878c0f98cSEli Cohen 
1869e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1870e126ba97SEli Cohen 	if (err)
1871a2a074efSLeon Romanovsky 		return err;
1872e126ba97SEli Cohen 
1873a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1874a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
187578c0f98cSEli Cohen 
1876f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1877a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
1878b368d7cbSMatan Barak 
18792f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
18802f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
18812f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1882a2a074efSLeon Romanovsky 		return -EINVAL;
1883e126ba97SEli Cohen 
188430aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
18850a2fd01cSYishai Hadas 	lib_uar_dyn = req.lib_caps & MLX5_LIB_CAP_DYN_UAR;
18862f5ff264SEli Cohen 	bfregi = &context->bfregi;
1887b037c29aSEli Cohen 
18880a2fd01cSYishai Hadas 	if (lib_uar_dyn) {
18890a2fd01cSYishai Hadas 		bfregi->lib_uar_dyn = lib_uar_dyn;
18900a2fd01cSYishai Hadas 		goto uar_done;
18910a2fd01cSYishai Hadas 	}
18920a2fd01cSYishai Hadas 
1893b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
189431a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1895b037c29aSEli Cohen 	if (err)
1896b037c29aSEli Cohen 		goto out_ctx;
1897b037c29aSEli Cohen 
18982f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1899b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
190031a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1901b037c29aSEli Cohen 				GFP_KERNEL);
1902b037c29aSEli Cohen 	if (!bfregi->count) {
1903e126ba97SEli Cohen 		err = -ENOMEM;
1904e126ba97SEli Cohen 		goto out_ctx;
1905e126ba97SEli Cohen 	}
1906e126ba97SEli Cohen 
1907b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1908b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1909e126ba97SEli Cohen 				    GFP_KERNEL);
1910b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1911e126ba97SEli Cohen 		err = -ENOMEM;
1912e126ba97SEli Cohen 		goto out_count;
1913e126ba97SEli Cohen 	}
1914e126ba97SEli Cohen 
1915b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1916b037c29aSEli Cohen 	if (err)
1917b037c29aSEli Cohen 		goto out_sys_pages;
1918b037c29aSEli Cohen 
19190a2fd01cSYishai Hadas uar_done:
1920a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1921fb98153bSYishai Hadas 		err = mlx5_ib_devx_create(dev, true);
192276dc5a84SYishai Hadas 		if (err < 0)
1923d2d19121SYishai Hadas 			goto out_uars;
192476dc5a84SYishai Hadas 		context->devx_uid = err;
1925a8b92ca1SYishai Hadas 	}
1926a8b92ca1SYishai Hadas 
1927d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1928d2d19121SYishai Hadas 					     context->devx_uid);
1929a8b92ca1SYishai Hadas 	if (err)
1930d2d19121SYishai Hadas 		goto out_devx;
1931a8b92ca1SYishai Hadas 
1932e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1933e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1934e126ba97SEli Cohen 
193545ec21c9SYishai Hadas 	context->cqe_version = min_t(__u8,
193645ec21c9SYishai Hadas 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
193745ec21c9SYishai Hadas 				 req.max_cqe_version);
1938b368d7cbSMatan Barak 
193945ec21c9SYishai Hadas 	err = set_ucontext_resp(uctx, &resp);
194045ec21c9SYishai Hadas 	if (err)
194145ec21c9SYishai Hadas 		goto out_mdev;
1942b368d7cbSMatan Barak 
194345ec21c9SYishai Hadas 	resp.response_length = min(udata->outlen, sizeof(resp));
1944b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1945e126ba97SEli Cohen 	if (err)
1946a8b92ca1SYishai Hadas 		goto out_mdev;
1947e126ba97SEli Cohen 
19482f5ff264SEli Cohen 	bfregi->ver = ver;
19492f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
195030aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
195130aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1952f72300c5SHaggai Abramovsky 
1953802dcc7fSMark Zhang 	if (mlx5_ib_lag_should_assign_affinity(dev)) {
19541fb7f897SMark Bloch 		u32 port = mlx5_core_native_port_num(dev->mdev) - 1;
1955c6a21c38SMajd Dibbiny 
1956c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1957c6a21c38SMajd Dibbiny 			   atomic_add_return(
195895579e78SMark Bloch 				   1, &dev->port[port].roce.tx_port_affinity));
1959c6a21c38SMajd Dibbiny 	}
1960c6a21c38SMajd Dibbiny 
1961a2a074efSLeon Romanovsky 	return 0;
1962e126ba97SEli Cohen 
1963a8b92ca1SYishai Hadas out_mdev:
1964d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1965d2d19121SYishai Hadas out_devx:
1966a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
196776dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1968146d2f1aSmajd@mellanox.com 
1969e126ba97SEli Cohen out_uars:
1970b037c29aSEli Cohen 	deallocate_uars(dev, context);
1971b037c29aSEli Cohen 
1972b037c29aSEli Cohen out_sys_pages:
1973b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1974b037c29aSEli Cohen 
1975e126ba97SEli Cohen out_count:
19762f5ff264SEli Cohen 	kfree(bfregi->count);
1977e126ba97SEli Cohen 
1978e126ba97SEli Cohen out_ctx:
1979a2a074efSLeon Romanovsky 	return err;
1980e126ba97SEli Cohen }
1981e126ba97SEli Cohen 
19820fb556b2SYishai Hadas static int mlx5_ib_query_ucontext(struct ib_ucontext *ibcontext,
19830fb556b2SYishai Hadas 				  struct uverbs_attr_bundle *attrs)
19840fb556b2SYishai Hadas {
19850fb556b2SYishai Hadas 	struct mlx5_ib_alloc_ucontext_resp uctx_resp = {};
19860fb556b2SYishai Hadas 	int ret;
19870fb556b2SYishai Hadas 
19880fb556b2SYishai Hadas 	ret = set_ucontext_resp(ibcontext, &uctx_resp);
19890fb556b2SYishai Hadas 	if (ret)
19900fb556b2SYishai Hadas 		return ret;
19910fb556b2SYishai Hadas 
19920fb556b2SYishai Hadas 	uctx_resp.response_length =
19930fb556b2SYishai Hadas 		min_t(size_t,
19940fb556b2SYishai Hadas 		      uverbs_attr_get_len(attrs,
19950fb556b2SYishai Hadas 				MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX),
19960fb556b2SYishai Hadas 		      sizeof(uctx_resp));
19970fb556b2SYishai Hadas 
19980fb556b2SYishai Hadas 	ret = uverbs_copy_to_struct_or_zero(attrs,
19990fb556b2SYishai Hadas 					MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
20000fb556b2SYishai Hadas 					&uctx_resp,
20010fb556b2SYishai Hadas 					sizeof(uctx_resp));
20020fb556b2SYishai Hadas 	return ret;
20030fb556b2SYishai Hadas }
20040fb556b2SYishai Hadas 
2005a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
2006e126ba97SEli Cohen {
2007e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2008e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
2009b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
2010e126ba97SEli Cohen 
2011b037c29aSEli Cohen 	bfregi = &context->bfregi;
2012d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2013d2d19121SYishai Hadas 
2014b037c29aSEli Cohen 	if (context->devx_uid)
201576dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
2016e126ba97SEli Cohen 
2017e126ba97SEli Cohen 	deallocate_uars(dev, context);
20182f5ff264SEli Cohen 	kfree(bfregi->sys_pages);
20192f5ff264SEli Cohen 	kfree(bfregi->count);
2020e126ba97SEli Cohen }
2021e126ba97SEli Cohen 
2022e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2023e126ba97SEli Cohen 				 int uar_idx)
2024e126ba97SEli Cohen {
2025b037c29aSEli Cohen 	int fw_uars_per_page;
2026b037c29aSEli Cohen 
2027b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2028b037c29aSEli Cohen 
2029aa8106f1SHuy Nguyen 	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2030e126ba97SEli Cohen }
2031e126ba97SEli Cohen 
2032342ee59dSYishai Hadas static u64 uar_index2paddress(struct mlx5_ib_dev *dev,
2033342ee59dSYishai Hadas 				 int uar_idx)
2034342ee59dSYishai Hadas {
2035342ee59dSYishai Hadas 	unsigned int fw_uars_per_page;
2036342ee59dSYishai Hadas 
2037342ee59dSYishai Hadas 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
2038342ee59dSYishai Hadas 				MLX5_UARS_IN_PAGE : 1;
2039342ee59dSYishai Hadas 
2040342ee59dSYishai Hadas 	return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE);
2041342ee59dSYishai Hadas }
2042342ee59dSYishai Hadas 
2043e126ba97SEli Cohen static int get_command(unsigned long offset)
2044e126ba97SEli Cohen {
2045e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2046e126ba97SEli Cohen }
2047e126ba97SEli Cohen 
2048e126ba97SEli Cohen static int get_arg(unsigned long offset)
2049e126ba97SEli Cohen {
2050e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2051e126ba97SEli Cohen }
2052e126ba97SEli Cohen 
2053e126ba97SEli Cohen static int get_index(unsigned long offset)
2054e126ba97SEli Cohen {
2055e126ba97SEli Cohen 	return get_arg(offset);
2056e126ba97SEli Cohen }
2057e126ba97SEli Cohen 
20584ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
20594ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20604ed131d0SYishai Hadas {
20614ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20624ed131d0SYishai Hadas }
20634ed131d0SYishai Hadas 
20647c2344c3SMaor Gottlieb 
20657c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20667c2344c3SMaor Gottlieb {
20677c2344c3SMaor Gottlieb }
20687c2344c3SMaor Gottlieb 
206937aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2070e126ba97SEli Cohen {
207137aa5c36SGuy Levi 	switch (cmd) {
207237aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
207337aa5c36SGuy Levi 		return "WC";
2074e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
207537aa5c36SGuy Levi 		return "best effort WC";
207637aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
207737aa5c36SGuy Levi 		return "NC";
207824da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
207924da0016SAriel Levkovich 		return "Device Memory";
208037aa5c36SGuy Levi 	default:
208137aa5c36SGuy Levi 		return NULL;
208237aa5c36SGuy Levi 	}
208337aa5c36SGuy Levi }
208437aa5c36SGuy Levi 
20855c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20865c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20875c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20885c99eaecSFeras Daoud {
20894eb6ab13SJason Gunthorpe 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
20904eb6ab13SJason Gunthorpe 	    !(vma->vm_flags & VM_SHARED))
20915c99eaecSFeras Daoud 		return -EINVAL;
20925c99eaecSFeras Daoud 
20935c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20945c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20955c99eaecSFeras Daoud 
20964eb6ab13SJason Gunthorpe 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
20975c99eaecSFeras Daoud 		return -EPERM;
2098c660133cSJason Gunthorpe 	vma->vm_flags &= ~VM_MAYWRITE;
20995c99eaecSFeras Daoud 
2100ddcdc368SJason Gunthorpe 	if (!dev->mdev->clock_info)
21015c99eaecSFeras Daoud 		return -EOPNOTSUPP;
21025c99eaecSFeras Daoud 
21034eb6ab13SJason Gunthorpe 	return vm_insert_page(vma, vma->vm_start,
21044eb6ab13SJason Gunthorpe 			      virt_to_page(dev->mdev->clock_info));
21055c99eaecSFeras Daoud }
21065c99eaecSFeras Daoud 
2107dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
2108dc2316ebSYishai Hadas {
2109dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
2110dc2316ebSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device);
21117be76befSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
2112dc2316ebSYishai Hadas 
2113dc2316ebSYishai Hadas 	switch (mentry->mmap_flag) {
2114dc2316ebSYishai Hadas 	case MLX5_IB_MMAP_TYPE_MEMIC:
2115cea85fa5SMaor Gottlieb 	case MLX5_IB_MMAP_TYPE_MEMIC_OP:
2116cea85fa5SMaor Gottlieb 		mlx5_ib_dm_mmap_free(dev, mentry);
2117dc2316ebSYishai Hadas 		break;
21187be76befSYishai Hadas 	case MLX5_IB_MMAP_TYPE_VAR:
21197be76befSYishai Hadas 		mutex_lock(&var_table->bitmap_lock);
21207be76befSYishai Hadas 		clear_bit(mentry->page_idx, var_table->bitmap);
21217be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
21227be76befSYishai Hadas 		kfree(mentry);
21237be76befSYishai Hadas 		break;
2124342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_WC:
2125342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_NC:
2126342ee59dSYishai Hadas 		mlx5_cmd_free_uar(dev->mdev, mentry->page_idx);
2127342ee59dSYishai Hadas 		kfree(mentry);
2128342ee59dSYishai Hadas 		break;
2129dc2316ebSYishai Hadas 	default:
2130dc2316ebSYishai Hadas 		WARN_ON(true);
2131dc2316ebSYishai Hadas 	}
2132dc2316ebSYishai Hadas }
2133dc2316ebSYishai Hadas 
213437aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
21357c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
21367c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
213737aa5c36SGuy Levi {
21382f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
213937aa5c36SGuy Levi 	int err;
214037aa5c36SGuy Levi 	unsigned long idx;
2141aa09ea6eSKamal Heib 	phys_addr_t pfn;
214237aa5c36SGuy Levi 	pgprot_t prot;
21434ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
21444ed131d0SYishai Hadas 	u32 uar_index;
21454ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
21464ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
21474ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2148b037c29aSEli Cohen 
21490a2fd01cSYishai Hadas 	if (bfregi->lib_uar_dyn)
21500a2fd01cSYishai Hadas 		return -EINVAL;
21510a2fd01cSYishai Hadas 
2152b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2153b037c29aSEli Cohen 		return -EINVAL;
2154b037c29aSEli Cohen 
21554ed131d0SYishai Hadas 	if (dyn_uar)
21564ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
21574ed131d0SYishai Hadas 	else
2158b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
21594ed131d0SYishai Hadas 
21604ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21614ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21624ed131d0SYishai Hadas 			     idx, max_valid_idx);
2163b037c29aSEli Cohen 		return -EINVAL;
2164b037c29aSEli Cohen 	}
216537aa5c36SGuy Levi 
216637aa5c36SGuy Levi 	switch (cmd) {
216737aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21684ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
216937aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
217037aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
217137aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
217237aa5c36SGuy Levi 		break;
217337aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
217437aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
217537aa5c36SGuy Levi 		break;
217637aa5c36SGuy Levi 	default:
217737aa5c36SGuy Levi 		return -EINVAL;
217837aa5c36SGuy Levi 	}
217937aa5c36SGuy Levi 
21804ed131d0SYishai Hadas 	if (dyn_uar) {
21814ed131d0SYishai Hadas 		int uars_per_page;
21824ed131d0SYishai Hadas 
21834ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21844ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21854ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21864ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21874ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21884ed131d0SYishai Hadas 			return -EINVAL;
21894ed131d0SYishai Hadas 		}
21904ed131d0SYishai Hadas 
21914ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21924ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21934ed131d0SYishai Hadas 		 * page holds its count.
21944ed131d0SYishai Hadas 		 */
21954ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
21964ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
21974ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
21984ed131d0SYishai Hadas 			return -EINVAL;
21994ed131d0SYishai Hadas 		}
22004ed131d0SYishai Hadas 
22014ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
22024ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
22034ed131d0SYishai Hadas 
22044ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
22054ed131d0SYishai Hadas 		if (err) {
22064ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
22074ed131d0SYishai Hadas 			goto free_bfreg;
22084ed131d0SYishai Hadas 		}
22094ed131d0SYishai Hadas 	} else {
22104ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
22114ed131d0SYishai Hadas 	}
22124ed131d0SYishai Hadas 
22134ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
221437aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2215e126ba97SEli Cohen 
2216e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2217c043ff2cSMichal Kalderon 				prot, NULL);
221837aa5c36SGuy Levi 	if (err) {
22198f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2220e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
22218f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
22224ed131d0SYishai Hadas 		goto err;
222337aa5c36SGuy Levi 	}
2224e126ba97SEli Cohen 
22254ed131d0SYishai Hadas 	if (dyn_uar)
22264ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
22274ed131d0SYishai Hadas 	return 0;
22284ed131d0SYishai Hadas 
22294ed131d0SYishai Hadas err:
22304ed131d0SYishai Hadas 	if (!dyn_uar)
22314ed131d0SYishai Hadas 		return err;
22324ed131d0SYishai Hadas 
22334ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
22344ed131d0SYishai Hadas 
22354ed131d0SYishai Hadas free_bfreg:
22364ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
22374ed131d0SYishai Hadas 
22384ed131d0SYishai Hadas 	return err;
223937aa5c36SGuy Levi }
224037aa5c36SGuy Levi 
2241dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma)
2242dc2316ebSYishai Hadas {
2243dc2316ebSYishai Hadas 	unsigned long idx;
2244dc2316ebSYishai Hadas 	u8 command;
2245dc2316ebSYishai Hadas 
2246dc2316ebSYishai Hadas 	command = get_command(vma->vm_pgoff);
2247dc2316ebSYishai Hadas 	idx = get_extended_index(vma->vm_pgoff);
2248dc2316ebSYishai Hadas 
2249dc2316ebSYishai Hadas 	return (command << 16 | idx);
2250dc2316ebSYishai Hadas }
2251dc2316ebSYishai Hadas 
2252dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev,
2253dc2316ebSYishai Hadas 			       struct vm_area_struct *vma,
2254dc2316ebSYishai Hadas 			       struct ib_ucontext *ucontext)
2255dc2316ebSYishai Hadas {
2256dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry;
2257dc2316ebSYishai Hadas 	struct rdma_user_mmap_entry *entry;
2258dc2316ebSYishai Hadas 	unsigned long pgoff;
2259dc2316ebSYishai Hadas 	pgprot_t prot;
2260dc2316ebSYishai Hadas 	phys_addr_t pfn;
2261dc2316ebSYishai Hadas 	int ret;
2262dc2316ebSYishai Hadas 
2263dc2316ebSYishai Hadas 	pgoff = mlx5_vma_to_pgoff(vma);
2264dc2316ebSYishai Hadas 	entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff);
2265dc2316ebSYishai Hadas 	if (!entry)
226624da0016SAriel Levkovich 		return -EINVAL;
226724da0016SAriel Levkovich 
2268dc2316ebSYishai Hadas 	mentry = to_mmmap(entry);
2269dc2316ebSYishai Hadas 	pfn = (mentry->address >> PAGE_SHIFT);
2270342ee59dSYishai Hadas 	if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR ||
2271342ee59dSYishai Hadas 	    mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC)
22723f59b6c3SYishai Hadas 		prot = pgprot_noncached(vma->vm_page_prot);
22733f59b6c3SYishai Hadas 	else
2274dc2316ebSYishai Hadas 		prot = pgprot_writecombine(vma->vm_page_prot);
2275dc2316ebSYishai Hadas 	ret = rdma_user_mmap_io(ucontext, vma, pfn,
2276dc2316ebSYishai Hadas 				entry->npages * PAGE_SIZE,
2277dc2316ebSYishai Hadas 				prot,
2278dc2316ebSYishai Hadas 				entry);
2279dc2316ebSYishai Hadas 	rdma_user_mmap_entry_put(&mentry->rdma_entry);
2280dc2316ebSYishai Hadas 	return ret;
228124da0016SAriel Levkovich }
228224da0016SAriel Levkovich 
22837be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry)
22847be76befSYishai Hadas {
22859b6d3bbcSLeon Romanovsky 	u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF;
22869b6d3bbcSLeon Romanovsky 	u64 index = entry->rdma_entry.start_pgoff & 0xFFFF;
22877be76befSYishai Hadas 
22887be76befSYishai Hadas 	return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) |
22897be76befSYishai Hadas 		(index & 0xFF)) << PAGE_SHIFT;
22907be76befSYishai Hadas }
22917be76befSYishai Hadas 
229237aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
229337aa5c36SGuy Levi {
229437aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
229537aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
229637aa5c36SGuy Levi 	unsigned long command;
229737aa5c36SGuy Levi 	phys_addr_t pfn;
229837aa5c36SGuy Levi 
229937aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
230037aa5c36SGuy Levi 	switch (command) {
230137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
23021f3db161SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
23031f3db161SYishai Hadas 		if (!dev->wc_support)
23041f3db161SYishai Hadas 			return -EPERM;
23051f3db161SYishai Hadas 		fallthrough;
230637aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
230737aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
23087c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2309e126ba97SEli Cohen 
2310e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2311e126ba97SEli Cohen 		return -ENOSYS;
2312e126ba97SEli Cohen 
2313d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2314d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2315d69e3bcfSMatan Barak 			return -EINVAL;
2316d69e3bcfSMatan Barak 
23176cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2318d69e3bcfSMatan Barak 			return -EPERM;
2319c660133cSJason Gunthorpe 		vma->vm_flags &= ~VM_MAYWRITE;
2320d69e3bcfSMatan Barak 
2321d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2322d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2323d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2324d69e3bcfSMatan Barak 
2325d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2326d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2327d69e3bcfSMatan Barak 			PAGE_SHIFT;
2328d5e560d3SJason Gunthorpe 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2329d5e560d3SJason Gunthorpe 					 PAGE_SIZE,
2330c043ff2cSMichal Kalderon 					 pgprot_noncached(vma->vm_page_prot),
2331c043ff2cSMichal Kalderon 					 NULL);
23325c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
23335c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2334d69e3bcfSMatan Barak 
2335e126ba97SEli Cohen 	default:
2336dc2316ebSYishai Hadas 		return mlx5_ib_mmap_offset(dev, vma, ibcontext);
2337e126ba97SEli Cohen 	}
2338e126ba97SEli Cohen 
2339e126ba97SEli Cohen 	return 0;
2340e126ba97SEli Cohen }
2341e126ba97SEli Cohen 
2342ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2343e126ba97SEli Cohen {
234421a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
234521a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2346e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2347e126ba97SEli Cohen 	int err;
2348a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2349a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2350a1069c1cSYishai Hadas 	u16 uid = 0;
2351ff23dfa1SShamir Rabinovitch 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2352ff23dfa1SShamir Rabinovitch 		udata, struct mlx5_ib_ucontext, ibucontext);
2353e126ba97SEli Cohen 
2354ff23dfa1SShamir Rabinovitch 	uid = context ? context->devx_uid : 0;
2355a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2356a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
235731578defSLeon Romanovsky 	err = mlx5_cmd_exec_inout(to_mdev(ibdev)->mdev, alloc_pd, in, out);
235821a428a0SLeon Romanovsky 	if (err)
235921a428a0SLeon Romanovsky 		return err;
2360e126ba97SEli Cohen 
2361a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2362a1069c1cSYishai Hadas 	pd->uid = uid;
2363ff23dfa1SShamir Rabinovitch 	if (udata) {
2364e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2365e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2366a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
236721a428a0SLeon Romanovsky 			return -EFAULT;
2368e126ba97SEli Cohen 		}
2369e126ba97SEli Cohen 	}
2370e126ba97SEli Cohen 
237121a428a0SLeon Romanovsky 	return 0;
2372e126ba97SEli Cohen }
2373e126ba97SEli Cohen 
237491a7c58fSLeon Romanovsky static int mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2375e126ba97SEli Cohen {
2376e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2377e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2378e126ba97SEli Cohen 
237991a7c58fSLeon Romanovsky 	return mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2380e126ba97SEli Cohen }
2381e126ba97SEli Cohen 
2382e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2383e126ba97SEli Cohen {
2384e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
238581e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
2386e126ba97SEli Cohen 	int err;
2387539ec982SYishai Hadas 	u16 uid;
2388539ec982SYishai Hadas 
2389539ec982SYishai Hadas 	uid = ibqp->pd ?
2390539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
2391e126ba97SEli Cohen 
23922be08c30SLeon Romanovsky 	if (mqp->flags & IB_QP_CREATE_SOURCE_QPN) {
239381e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
239481e30880SYishai Hadas 		return -EOPNOTSUPP;
239581e30880SYishai Hadas 	}
239681e30880SYishai Hadas 
2397539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2398e126ba97SEli Cohen 	if (err)
2399e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2400e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2401e126ba97SEli Cohen 
2402e126ba97SEli Cohen 	return err;
2403e126ba97SEli Cohen }
2404e126ba97SEli Cohen 
2405e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2406e126ba97SEli Cohen {
2407e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2408e126ba97SEli Cohen 	int err;
2409539ec982SYishai Hadas 	u16 uid;
2410e126ba97SEli Cohen 
2411539ec982SYishai Hadas 	uid = ibqp->pd ?
2412539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
2413539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2414e126ba97SEli Cohen 	if (err)
2415e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2416e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2417e126ba97SEli Cohen 
2418e126ba97SEli Cohen 	return err;
2419e126ba97SEli Cohen }
2420e126ba97SEli Cohen 
2421e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2422e126ba97SEli Cohen {
24231b5daf11SMajd Dibbiny 	int err;
2424e126ba97SEli Cohen 
24251b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2426e126ba97SEli Cohen 	if (err)
2427e126ba97SEli Cohen 		return err;
24281b5daf11SMajd Dibbiny 
24291b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
24301b5daf11SMajd Dibbiny 
24311b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2432e126ba97SEli Cohen }
2433e126ba97SEli Cohen 
2434508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
2435508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2436e126ba97SEli Cohen {
2437e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
243854747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2439e126ba97SEli Cohen 
24401c7fd726SJoe Perches 	return sysfs_emit(buf, "%d\n", dev->mdev->priv.fw_pages);
2441e126ba97SEli Cohen }
2442508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
2443e126ba97SEli Cohen 
2444508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
2445e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2446e126ba97SEli Cohen {
2447e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
244854747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2449e126ba97SEli Cohen 
24501c7fd726SJoe Perches 	return sysfs_emit(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2451e126ba97SEli Cohen }
2452508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
2453e126ba97SEli Cohen 
2454508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
2455508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2456e126ba97SEli Cohen {
2457e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
245854747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
245954747231SParav Pandit 
24601c7fd726SJoe Perches 	return sysfs_emit(buf, "MT%d\n", dev->mdev->pdev->device);
2461e126ba97SEli Cohen }
2462508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
2463e126ba97SEli Cohen 
2464508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
2465508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
2466e126ba97SEli Cohen {
2467e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
246854747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
246954747231SParav Pandit 
24701c7fd726SJoe Perches 	return sysfs_emit(buf, "%x\n", dev->mdev->rev_id);
2471e126ba97SEli Cohen }
2472508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
2473e126ba97SEli Cohen 
2474508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
2475508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2476e126ba97SEli Cohen {
2477e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
247854747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
247954747231SParav Pandit 
24801c7fd726SJoe Perches 	return sysfs_emit(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
24819603b61dSJack Morgenstein 			  dev->mdev->board_id);
2482e126ba97SEli Cohen }
2483508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
2484e126ba97SEli Cohen 
2485508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
2486508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
2487508a523fSParav Pandit 	&dev_attr_hca_type.attr,
2488508a523fSParav Pandit 	&dev_attr_board_id.attr,
2489508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
2490508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
2491508a523fSParav Pandit 	NULL,
2492508a523fSParav Pandit };
2493e126ba97SEli Cohen 
2494508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
2495508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
2496e126ba97SEli Cohen };
2497e126ba97SEli Cohen 
24987722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
24997722f47eSHaggai Eran {
25007722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
25017722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
25027722f47eSHaggai Eran 			     pkey_change_work);
25037722f47eSHaggai Eran 
25048c9e7f03SLeon Romanovsky 	if (!ports->gsi)
25058c9e7f03SLeon Romanovsky 		/*
25068c9e7f03SLeon Romanovsky 		 * We got this event before device was fully configured
25078c9e7f03SLeon Romanovsky 		 * and MAD registration code wasn't called/finished yet.
25088c9e7f03SLeon Romanovsky 		 */
25098c9e7f03SLeon Romanovsky 		return;
25108c9e7f03SLeon Romanovsky 
25117722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
25127722f47eSHaggai Eran }
25137722f47eSHaggai Eran 
251489ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
251589ea94a7SMaor Gottlieb {
251689ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
251789ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
251889ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
251989ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
252089ea94a7SMaor Gottlieb 	unsigned long flags_qp;
252189ea94a7SMaor Gottlieb 	unsigned long flags_cq;
252289ea94a7SMaor Gottlieb 	unsigned long flags;
252389ea94a7SMaor Gottlieb 
252489ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
252589ea94a7SMaor Gottlieb 
252689ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
252789ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
252889ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
252989ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
253089ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
253189ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
253289ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
253389ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
253489ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
253589ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
253689ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
253789ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
253889ea94a7SMaor Gottlieb 						      &cq_armed_list);
253989ea94a7SMaor Gottlieb 				}
254089ea94a7SMaor Gottlieb 			}
254189ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
254289ea94a7SMaor Gottlieb 		}
254389ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
254489ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
254589ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
254689ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
254789ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
254889ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
254989ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
255089ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
255189ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
255289ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
255389ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
255489ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
255589ea94a7SMaor Gottlieb 							      &cq_armed_list);
255689ea94a7SMaor Gottlieb 					}
255789ea94a7SMaor Gottlieb 				}
255889ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
255989ea94a7SMaor Gottlieb 						       flags_cq);
256089ea94a7SMaor Gottlieb 			}
256189ea94a7SMaor Gottlieb 		}
256289ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
256389ea94a7SMaor Gottlieb 	}
256489ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
256589ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
256689ea94a7SMaor Gottlieb 	 */
256789ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
25684e0e2ea1SYishai Hadas 		mcq->comp(mcq, NULL);
256989ea94a7SMaor Gottlieb 	}
257089ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
257189ea94a7SMaor Gottlieb }
257289ea94a7SMaor Gottlieb 
257303404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
257403404e8aSMaor Gottlieb {
257503404e8aSMaor Gottlieb 	int err;
257603404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
257703404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
257803404e8aSMaor Gottlieb 			     delay_drop_work);
257903404e8aSMaor Gottlieb 
2580fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
2581fe248c3aSMaor Gottlieb 
258203404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
2583333fbaa0SLeon Romanovsky 	err = mlx5_core_set_delay_drop(delay_drop->dev, delay_drop->timeout);
258403404e8aSMaor Gottlieb 	if (err) {
258503404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
258603404e8aSMaor Gottlieb 			     delay_drop->timeout);
258703404e8aSMaor Gottlieb 		delay_drop->activate = false;
258803404e8aSMaor Gottlieb 	}
258903404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
259003404e8aSMaor Gottlieb }
259103404e8aSMaor Gottlieb 
259209e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
259309e574faSSaeed Mahameed 				 struct ib_event *ibev)
259409e574faSSaeed Mahameed {
25951fb7f897SMark Bloch 	u32 port = (eqe->data.port.port >> 4) & 0xf;
25966cfdc7e4SAya Levin 
259709e574faSSaeed Mahameed 	switch (eqe->sub_type) {
259809e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
25996cfdc7e4SAya Levin 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
26006cfdc7e4SAya Levin 					    IB_LINK_LAYER_ETHERNET)
260109e574faSSaeed Mahameed 			schedule_work(&ibdev->delay_drop.delay_drop_work);
260209e574faSSaeed Mahameed 		break;
260309e574faSSaeed Mahameed 	default: /* do nothing */
260409e574faSSaeed Mahameed 		return;
260509e574faSSaeed Mahameed 	}
260609e574faSSaeed Mahameed }
260709e574faSSaeed Mahameed 
2608134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
2609134e9349SSaeed Mahameed 			      struct ib_event *ibev)
2610134e9349SSaeed Mahameed {
26111fb7f897SMark Bloch 	u32 port = (eqe->data.port.port >> 4) & 0xf;
2612134e9349SSaeed Mahameed 
2613134e9349SSaeed Mahameed 	ibev->element.port_num = port;
2614134e9349SSaeed Mahameed 
2615134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
2616134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
2617134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
2618134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
2619134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
2620134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
2621134e9349SSaeed Mahameed 		 */
2622134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2623134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
2624134e9349SSaeed Mahameed 			return -EINVAL;
2625134e9349SSaeed Mahameed 
2626134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
2627134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2628134e9349SSaeed Mahameed 		break;
2629134e9349SSaeed Mahameed 
2630134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
2631134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
2632134e9349SSaeed Mahameed 		break;
2633134e9349SSaeed Mahameed 
2634134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
2635134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
2636134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2637134e9349SSaeed Mahameed 		break;
2638134e9349SSaeed Mahameed 
2639134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
2640134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
2641134e9349SSaeed Mahameed 		break;
2642134e9349SSaeed Mahameed 
2643134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
2644134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
2645134e9349SSaeed Mahameed 		break;
2646134e9349SSaeed Mahameed 	default:
2647134e9349SSaeed Mahameed 		return -EINVAL;
2648134e9349SSaeed Mahameed 	}
2649134e9349SSaeed Mahameed 
2650134e9349SSaeed Mahameed 	return 0;
2651134e9349SSaeed Mahameed }
2652134e9349SSaeed Mahameed 
2653d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
2654e126ba97SEli Cohen {
2655d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
2656d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
2657d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
2658e126ba97SEli Cohen 	struct ib_event ibev;
2659dbaaff2aSEli Cohen 	bool fatal = false;
2660e126ba97SEli Cohen 
2661df097a27SSaeed Mahameed 	if (work->is_slave) {
2662df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
2663d69a24e0SDaniel Jurgens 		if (!ibdev)
2664d69a24e0SDaniel Jurgens 			goto out;
2665d69a24e0SDaniel Jurgens 	} else {
2666df097a27SSaeed Mahameed 		ibdev = work->dev;
2667d69a24e0SDaniel Jurgens 	}
2668d69a24e0SDaniel Jurgens 
2669d69a24e0SDaniel Jurgens 	switch (work->event) {
2670e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2671e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
267289ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2673134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
2674dbaaff2aSEli Cohen 		fatal = true;
2675e126ba97SEli Cohen 		break;
2676134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
2677134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
2678d69a24e0SDaniel Jurgens 			goto out;
2679e126ba97SEli Cohen 		break;
268009e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
268109e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
2682df561f66SGustavo A. R. Silva 		fallthrough;
2683bdc37924SSaeed Mahameed 	default:
268403404e8aSMaor Gottlieb 		goto out;
2685e126ba97SEli Cohen 	}
2686e126ba97SEli Cohen 
2687e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
2688e126ba97SEli Cohen 
2689134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
2690134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
269103404e8aSMaor Gottlieb 		goto out;
2692a0c84c32SEli Cohen 	}
2693a0c84c32SEli Cohen 
2694e126ba97SEli Cohen 	if (ibdev->ib_active)
2695e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2696dbaaff2aSEli Cohen 
2697dbaaff2aSEli Cohen 	if (fatal)
2698dbaaff2aSEli Cohen 		ibdev->ib_active = false;
269903404e8aSMaor Gottlieb out:
2700d69a24e0SDaniel Jurgens 	kfree(work);
2701d69a24e0SDaniel Jurgens }
2702d69a24e0SDaniel Jurgens 
2703df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
2704df097a27SSaeed Mahameed 			 unsigned long event, void *param)
2705d69a24e0SDaniel Jurgens {
2706d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
2707d69a24e0SDaniel Jurgens 
2708d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
270910bea9c8SLeon Romanovsky 	if (!work)
2710df097a27SSaeed Mahameed 		return NOTIFY_DONE;
271110bea9c8SLeon Romanovsky 
2712d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
2713df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
2714df097a27SSaeed Mahameed 	work->is_slave = false;
2715d69a24e0SDaniel Jurgens 	work->param = param;
2716d69a24e0SDaniel Jurgens 	work->event = event;
2717d69a24e0SDaniel Jurgens 
2718d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
2719df097a27SSaeed Mahameed 
2720df097a27SSaeed Mahameed 	return NOTIFY_OK;
2721df097a27SSaeed Mahameed }
2722df097a27SSaeed Mahameed 
2723df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
2724df097a27SSaeed Mahameed 				    unsigned long event, void *param)
2725df097a27SSaeed Mahameed {
2726df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
2727df097a27SSaeed Mahameed 
2728df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
2729df097a27SSaeed Mahameed 	if (!work)
2730df097a27SSaeed Mahameed 		return NOTIFY_DONE;
2731df097a27SSaeed Mahameed 
2732df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
2733df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
2734df097a27SSaeed Mahameed 	work->is_slave = true;
2735df097a27SSaeed Mahameed 	work->param = param;
2736df097a27SSaeed Mahameed 	work->event = event;
2737df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
2738df097a27SSaeed Mahameed 
2739df097a27SSaeed Mahameed 	return NOTIFY_OK;
2740d69a24e0SDaniel Jurgens }
2741d69a24e0SDaniel Jurgens 
2742c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2743c43f1112SMaor Gottlieb {
2744c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
2745c43f1112SMaor Gottlieb 	int err;
2746c43f1112SMaor Gottlieb 	int port;
2747c43f1112SMaor Gottlieb 
27483ce60f44SParav Pandit 	for (port = 1; port <= ARRAY_SIZE(dev->port_caps); port++) {
27493ce60f44SParav Pandit 		dev->port_caps[port - 1].has_smi = false;
2750c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2751c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
2752c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2753c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
2754c43f1112SMaor Gottlieb 								   port, 0,
2755c43f1112SMaor Gottlieb 								   &vport_ctx);
2756c43f1112SMaor Gottlieb 				if (err) {
2757c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2758c43f1112SMaor Gottlieb 						    port, err);
2759c43f1112SMaor Gottlieb 					return err;
2760c43f1112SMaor Gottlieb 				}
27613ce60f44SParav Pandit 				dev->port_caps[port - 1].has_smi =
2762c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
2763c43f1112SMaor Gottlieb 			} else {
27643ce60f44SParav Pandit 				dev->port_caps[port - 1].has_smi = true;
2765c43f1112SMaor Gottlieb 			}
2766c43f1112SMaor Gottlieb 		}
2767c43f1112SMaor Gottlieb 	}
2768c43f1112SMaor Gottlieb 	return 0;
2769c43f1112SMaor Gottlieb }
2770c43f1112SMaor Gottlieb 
2771e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2772e126ba97SEli Cohen {
277313179652SParav Pandit 	unsigned int port;
2774e126ba97SEli Cohen 
277513179652SParav Pandit 	rdma_for_each_port (&dev->ib_dev, port)
2776e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
2777e126ba97SEli Cohen }
2778e126ba97SEli Cohen 
27796e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
27806e8484c5SMax Gurtovoy {
27816e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
27826e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
27836e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
27846e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
27856e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
27866e8484c5SMax Gurtovoy 	default:
27876e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
27886e8484c5SMax Gurtovoy 	}
27896e8484c5SMax Gurtovoy }
27906e8484c5SMax Gurtovoy 
27911e2b5a90SLeon Romanovsky static int mlx5_ib_dev_res_init(struct mlx5_ib_dev *dev)
2792e126ba97SEli Cohen {
27931e2b5a90SLeon Romanovsky 	struct mlx5_ib_resources *devr = &dev->devr;
2794e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
279521a428a0SLeon Romanovsky 	struct ib_device *ibdev;
2796bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
27977722f47eSHaggai Eran 	int port;
2798e126ba97SEli Cohen 	int ret = 0;
2799e126ba97SEli Cohen 
280021a428a0SLeon Romanovsky 	ibdev = &dev->ib_dev;
2801e126ba97SEli Cohen 
2802f4375443SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, xrc))
2803f4375443SLeon Romanovsky 		return -EOPNOTSUPP;
2804f4375443SLeon Romanovsky 
2805*20da44dfSLeon Romanovsky 	devr->p0 = ib_alloc_pd(ibdev, 0);
2806*20da44dfSLeon Romanovsky 	if (IS_ERR(devr->p0))
2807*20da44dfSLeon Romanovsky 		return PTR_ERR(devr->p0);
280821a428a0SLeon Romanovsky 
2809*20da44dfSLeon Romanovsky 	devr->c0 = ib_create_cq(ibdev, NULL, NULL, NULL, &cq_attr);
2810*20da44dfSLeon Romanovsky 	if (IS_ERR(devr->c0)) {
2811*20da44dfSLeon Romanovsky 		ret = PTR_ERR(devr->c0);
2812e126ba97SEli Cohen 		goto error1;
2813e126ba97SEli Cohen 	}
2814e39afe3dSLeon Romanovsky 
2815f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn0, 0);
2816f4375443SLeon Romanovsky 	if (ret)
2817e126ba97SEli Cohen 		goto error2;
2818e126ba97SEli Cohen 
2819f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn1, 0);
2820f4375443SLeon Romanovsky 	if (ret)
2821e126ba97SEli Cohen 		goto error3;
2822e126ba97SEli Cohen 
2823e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
2824e126ba97SEli Cohen 	attr.attr.max_sge = 1;
2825e126ba97SEli Cohen 	attr.attr.max_wr = 1;
2826e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
28271a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
2828e126ba97SEli Cohen 
2829*20da44dfSLeon Romanovsky 	devr->s0 = ib_create_srq(devr->p0, &attr);
2830*20da44dfSLeon Romanovsky 	if (IS_ERR(devr->s0)) {
2831*20da44dfSLeon Romanovsky 		ret = PTR_ERR(devr->s0);
283268e326deSLeon Romanovsky 		goto err_create;
2833*20da44dfSLeon Romanovsky 	}
2834e126ba97SEli Cohen 
28354aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
28364aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
28374aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
28384aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
283968e326deSLeon Romanovsky 
2840*20da44dfSLeon Romanovsky 	devr->s1 = ib_create_srq(devr->p0, &attr);
2841*20da44dfSLeon Romanovsky 	if (IS_ERR(devr->s1)) {
2842*20da44dfSLeon Romanovsky 		ret = PTR_ERR(devr->s1);
284368e326deSLeon Romanovsky 		goto error6;
2844*20da44dfSLeon Romanovsky 	}
28454aa17b28SHaggai Abramonvsky 
2846f8225e34SLeon Romanovsky 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
28477722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
28487722f47eSHaggai Eran 			  pkey_change_handler);
28497722f47eSHaggai Eran 
2850e126ba97SEli Cohen 	return 0;
2851e126ba97SEli Cohen 
285268e326deSLeon Romanovsky error6:
2853*20da44dfSLeon Romanovsky 	ib_destroy_srq(devr->s0);
285468e326deSLeon Romanovsky err_create:
2855f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
2856e126ba97SEli Cohen error3:
2857f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
2858e126ba97SEli Cohen error2:
2859*20da44dfSLeon Romanovsky 	ib_destroy_cq(devr->c0);
2860e126ba97SEli Cohen error1:
2861*20da44dfSLeon Romanovsky 	ib_dealloc_pd(devr->p0);
2862e126ba97SEli Cohen 	return ret;
2863e126ba97SEli Cohen }
2864e126ba97SEli Cohen 
28651e2b5a90SLeon Romanovsky static void mlx5_ib_dev_res_cleanup(struct mlx5_ib_dev *dev)
2866e126ba97SEli Cohen {
28671e2b5a90SLeon Romanovsky 	struct mlx5_ib_resources *devr = &dev->devr;
28687722f47eSHaggai Eran 	int port;
28697722f47eSHaggai Eran 
2870b0791dbfSLeon Romanovsky 	/*
2871b0791dbfSLeon Romanovsky 	 * Make sure no change P_Key work items are still executing.
2872b0791dbfSLeon Romanovsky 	 *
2873b0791dbfSLeon Romanovsky 	 * At this stage, the mlx5_ib_event should be unregistered
2874b0791dbfSLeon Romanovsky 	 * and it ensures that no new works are added.
2875b0791dbfSLeon Romanovsky 	 */
2876b0791dbfSLeon Romanovsky 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
2877b0791dbfSLeon Romanovsky 		cancel_work_sync(&devr->ports[port].pkey_change_work);
2878b0791dbfSLeon Romanovsky 
2879*20da44dfSLeon Romanovsky 	ib_destroy_srq(devr->s1);
2880*20da44dfSLeon Romanovsky 	ib_destroy_srq(devr->s0);
2881f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
2882f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
2883*20da44dfSLeon Romanovsky 	ib_destroy_cq(devr->c0);
2884*20da44dfSLeon Romanovsky 	ib_dealloc_pd(devr->p0);
2885e126ba97SEli Cohen }
2886e126ba97SEli Cohen 
2887b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
2888b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
2889e53505a8SAchiad Shochat {
2890e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2891e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
2892e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
2893e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
289485c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
2895e53505a8SAchiad Shochat 	u32 ret = 0;
2896e53505a8SAchiad Shochat 
2897b02289b3SArtemy Kovalyov 	if (rep->grh_required)
2898b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
2899b02289b3SArtemy Kovalyov 
2900e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
2901b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
2902e53505a8SAchiad Shochat 
290385c7c014SDaniel Jurgens 	if (raw_support)
2904b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
290572cd5717SOr Gerlitz 
2906e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
290772cd5717SOr Gerlitz 		return ret;
2908e53505a8SAchiad Shochat 
2909e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
291072cd5717SOr Gerlitz 		return ret;
2911e53505a8SAchiad Shochat 
2912e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
2913e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
2914e53505a8SAchiad Shochat 
2915e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
2916e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
2917e53505a8SAchiad Shochat 
2918e53505a8SAchiad Shochat 	return ret;
2919e53505a8SAchiad Shochat }
2920e53505a8SAchiad Shochat 
29211fb7f897SMark Bloch static int mlx5_port_immutable(struct ib_device *ibdev, u32 port_num,
29227738613eSIra Weiny 			       struct ib_port_immutable *immutable)
29237738613eSIra Weiny {
29247738613eSIra Weiny 	struct ib_port_attr attr;
2925ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2926ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
2927b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
29287738613eSIra Weiny 	int err;
29297738613eSIra Weiny 
2930c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
29317738613eSIra Weiny 	if (err)
29327738613eSIra Weiny 		return err;
29337738613eSIra Weiny 
2934b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
2935b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
2936b02289b3SArtemy Kovalyov 						   &rep);
2937b02289b3SArtemy Kovalyov 		if (err)
2938b02289b3SArtemy Kovalyov 			return err;
2939b02289b3SArtemy Kovalyov 	}
2940b02289b3SArtemy Kovalyov 
29417738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
29427738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
2943b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
2944337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
29457738613eSIra Weiny 
29467738613eSIra Weiny 	return 0;
29477738613eSIra Weiny }
29487738613eSIra Weiny 
29491fb7f897SMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u32 port_num,
29508e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
29518e6efa3aSMark Bloch {
29528e6efa3aSMark Bloch 	struct ib_port_attr attr;
29538e6efa3aSMark Bloch 	int err;
29548e6efa3aSMark Bloch 
29558e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
29568e6efa3aSMark Bloch 
29578e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
29588e6efa3aSMark Bloch 	if (err)
29598e6efa3aSMark Bloch 		return err;
29608e6efa3aSMark Bloch 
29618e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
29628e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
29638e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
29648e6efa3aSMark Bloch 
29658e6efa3aSMark Bloch 	return 0;
29668e6efa3aSMark Bloch }
29678e6efa3aSMark Bloch 
29689abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
2969c7342823SIra Weiny {
2970c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
2971c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
29729abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
29739abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
29749abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
2975c7342823SIra Weiny }
2976c7342823SIra Weiny 
297745f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
29789ef9c640SAviv Heller {
29799ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
29809ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
29819ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
29829ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
29839ef9c640SAviv Heller 	int err;
29849ef9c640SAviv Heller 
29857c34ec19SAviv Heller 	if (!ns || !mlx5_lag_is_roce(mdev))
29869ef9c640SAviv Heller 		return 0;
29879ef9c640SAviv Heller 
29889ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
29899ef9c640SAviv Heller 	if (err)
29909ef9c640SAviv Heller 		return err;
29919ef9c640SAviv Heller 
29929ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
29939ef9c640SAviv Heller 	if (IS_ERR(ft)) {
29949ef9c640SAviv Heller 		err = PTR_ERR(ft);
29959ef9c640SAviv Heller 		goto err_destroy_vport_lag;
29969ef9c640SAviv Heller 	}
29979ef9c640SAviv Heller 
29989a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
29997c34ec19SAviv Heller 	dev->lag_active = true;
30009ef9c640SAviv Heller 	return 0;
30019ef9c640SAviv Heller 
30029ef9c640SAviv Heller err_destroy_vport_lag:
30039ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
30049ef9c640SAviv Heller 	return err;
30059ef9c640SAviv Heller }
30069ef9c640SAviv Heller 
300745f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
30089ef9c640SAviv Heller {
30099ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
30109ef9c640SAviv Heller 
30117c34ec19SAviv Heller 	if (dev->lag_active) {
30127c34ec19SAviv Heller 		dev->lag_active = false;
30137c34ec19SAviv Heller 
30149a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
30159a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
30169ef9c640SAviv Heller 
30179ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
30189ef9c640SAviv Heller 	}
30199ef9c640SAviv Heller }
30209ef9c640SAviv Heller 
30211fb7f897SMark Bloch static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u32 port_num)
3022fc24fc5eSAchiad Shochat {
3023e53505a8SAchiad Shochat 	int err;
3024e53505a8SAchiad Shochat 
302595579e78SMark Bloch 	dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
3026de641d74SParav Pandit 	err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
30275ec8c83eSAviv Heller 	if (err) {
302895579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
3029e53505a8SAchiad Shochat 		return err;
30305ec8c83eSAviv Heller 	}
3031e53505a8SAchiad Shochat 
3032d012f5d6SOr Gerlitz 	return 0;
3033d012f5d6SOr Gerlitz }
3034d012f5d6SOr Gerlitz 
30351fb7f897SMark Bloch static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u32 port_num)
3036e126ba97SEli Cohen {
303795579e78SMark Bloch 	if (dev->port[port_num].roce.nb.notifier_call) {
3038de641d74SParav Pandit 		unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
303995579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
3040e126ba97SEli Cohen 	}
3041e126ba97SEli Cohen }
3042e126ba97SEli Cohen 
3043e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3044e126ba97SEli Cohen {
3045e126ba97SEli Cohen 	int err;
3046e126ba97SEli Cohen 
3047e53505a8SAchiad Shochat 	err = mlx5_nic_vport_enable_roce(dev->mdev);
3048e53505a8SAchiad Shochat 	if (err)
30498e6efa3aSMark Bloch 		return err;
3050e53505a8SAchiad Shochat 
305145f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
30529ef9c640SAviv Heller 	if (err)
30539ef9c640SAviv Heller 		goto err_disable_roce;
30549ef9c640SAviv Heller 
3055e53505a8SAchiad Shochat 	return 0;
3056e53505a8SAchiad Shochat 
30579ef9c640SAviv Heller err_disable_roce:
30589ef9c640SAviv Heller 	mlx5_nic_vport_disable_roce(dev->mdev);
30599ef9c640SAviv Heller 
3060e53505a8SAchiad Shochat 	return err;
3061fc24fc5eSAchiad Shochat }
3062fc24fc5eSAchiad Shochat 
306345f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3064fc24fc5eSAchiad Shochat {
306545f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3066e53505a8SAchiad Shochat 	mlx5_nic_vport_disable_roce(dev->mdev);
3067fc24fc5eSAchiad Shochat }
3068fc24fc5eSAchiad Shochat 
30691fb7f897SMark Bloch static int mlx5_ib_rn_get_params(struct ib_device *device, u32 port_num,
3070693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
3071f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
3072693dfd5aSErez Shitrit {
3073693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
3074f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
3075693dfd5aSErez Shitrit 
3076f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
3077693dfd5aSErez Shitrit }
3078693dfd5aSErez Shitrit 
3079fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3080fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
3081fe248c3aSMaor Gottlieb {
3082fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3083fe248c3aSMaor Gottlieb 	char lbuf[20];
3084fe248c3aSMaor Gottlieb 	int len;
3085fe248c3aSMaor Gottlieb 
3086fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3087fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
3088fe248c3aSMaor Gottlieb }
3089fe248c3aSMaor Gottlieb 
3090fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3091fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
3092fe248c3aSMaor Gottlieb {
3093fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3094fe248c3aSMaor Gottlieb 	u32 timeout;
3095fe248c3aSMaor Gottlieb 	u32 var;
3096fe248c3aSMaor Gottlieb 
3097fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
3098fe248c3aSMaor Gottlieb 		return -EFAULT;
3099fe248c3aSMaor Gottlieb 
3100fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3101fe248c3aSMaor Gottlieb 			1000);
3102fe248c3aSMaor Gottlieb 	if (timeout != var)
3103fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3104fe248c3aSMaor Gottlieb 			    timeout);
3105fe248c3aSMaor Gottlieb 
3106fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
3107fe248c3aSMaor Gottlieb 
3108fe248c3aSMaor Gottlieb 	return count;
3109fe248c3aSMaor Gottlieb }
3110fe248c3aSMaor Gottlieb 
3111fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
3112fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
3113fe248c3aSMaor Gottlieb 	.open	= simple_open,
3114fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
3115fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
3116fe248c3aSMaor Gottlieb };
3117fe248c3aSMaor Gottlieb 
311832f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
311932f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
312032f69e4bSDaniel Jurgens {
31211fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
312232f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
312332f69e4bSDaniel Jurgens 	int comps;
312432f69e4bSDaniel Jurgens 	int err;
312532f69e4bSDaniel Jurgens 	int i;
312632f69e4bSDaniel Jurgens 
31279dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
31289dc4cfffSLeon Romanovsky 
3129a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
3130a9e546e7SParav Pandit 
313132f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
313232f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
313332f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
313432f69e4bSDaniel Jurgens 		return;
313532f69e4bSDaniel Jurgens 	}
3136df097a27SSaeed Mahameed 
313732f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
313832f69e4bSDaniel Jurgens 
313932f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
314023eaf3b5SLeon Romanovsky 	if (mpi->mdev_events.notifier_call)
314123eaf3b5SLeon Romanovsky 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
314223eaf3b5SLeon Romanovsky 	mpi->mdev_events.notifier_call = NULL;
314332f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
314432f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
314532f69e4bSDaniel Jurgens 
314632f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
314732f69e4bSDaniel Jurgens 	if (comps) {
314832f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
314932f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
315032f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
315132f69e4bSDaniel Jurgens 
315232f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
315332f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
315432f69e4bSDaniel Jurgens 
315532f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
315632f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
315732f69e4bSDaniel Jurgens 	}
315832f69e4bSDaniel Jurgens 
315932f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
316032f69e4bSDaniel Jurgens 
316132f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
316232f69e4bSDaniel Jurgens 
316332f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
316432f69e4bSDaniel Jurgens 
31651fb7f897SMark Bloch 	mlx5_ib_dbg(ibdev, "unaffiliated port %u\n", port_num + 1);
316632f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
316732f69e4bSDaniel Jurgens 	 * it back to the list.
316832f69e4bSDaniel Jurgens 	 */
316932f69e4bSDaniel Jurgens 	if (err)
317032f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
317132f69e4bSDaniel Jurgens 			    port_num + 1);
317232f69e4bSDaniel Jurgens 
317395579e78SMark Bloch 	ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
317432f69e4bSDaniel Jurgens }
317532f69e4bSDaniel Jurgens 
317632f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
317732f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
317832f69e4bSDaniel Jurgens {
31791fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
318032f69e4bSDaniel Jurgens 	int err;
318132f69e4bSDaniel Jurgens 
31829dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
31839dc4cfffSLeon Romanovsky 
318432f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
318532f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
31861fb7f897SMark Bloch 		mlx5_ib_dbg(ibdev, "port %u already affiliated.\n",
318732f69e4bSDaniel Jurgens 			    port_num + 1);
318832f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
318932f69e4bSDaniel Jurgens 		return false;
319032f69e4bSDaniel Jurgens 	}
319132f69e4bSDaniel Jurgens 
319232f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
319332f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
3194df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
319532f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
319632f69e4bSDaniel Jurgens 
319732f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
319832f69e4bSDaniel Jurgens 	if (err)
319932f69e4bSDaniel Jurgens 		goto unbind;
320032f69e4bSDaniel Jurgens 
320132f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
320232f69e4bSDaniel Jurgens 	if (err) {
320332f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
320432f69e4bSDaniel Jurgens 			    port_num + 1);
320532f69e4bSDaniel Jurgens 		goto unbind;
320632f69e4bSDaniel Jurgens 	}
320732f69e4bSDaniel Jurgens 
3208df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
3209df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
3210df097a27SSaeed Mahameed 
321173eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
3212a9e546e7SParav Pandit 
321332f69e4bSDaniel Jurgens 	return true;
321432f69e4bSDaniel Jurgens 
321532f69e4bSDaniel Jurgens unbind:
321632f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
321732f69e4bSDaniel Jurgens 	return false;
321832f69e4bSDaniel Jurgens }
321932f69e4bSDaniel Jurgens 
322032f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
322132f69e4bSDaniel Jurgens {
32221fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
322332f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
322432f69e4bSDaniel Jurgens 							  port_num + 1);
322532f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
322632f69e4bSDaniel Jurgens 	int err;
32271fb7f897SMark Bloch 	u32 i;
322832f69e4bSDaniel Jurgens 
322932f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
323032f69e4bSDaniel Jurgens 		return 0;
323132f69e4bSDaniel Jurgens 
323232f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
323332f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
323432f69e4bSDaniel Jurgens 	if (err)
323532f69e4bSDaniel Jurgens 		return err;
323632f69e4bSDaniel Jurgens 
323732f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
323832f69e4bSDaniel Jurgens 	if (err)
323932f69e4bSDaniel Jurgens 		return err;
324032f69e4bSDaniel Jurgens 
324132f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
324232f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
324332f69e4bSDaniel Jurgens 		bool bound = false;
324432f69e4bSDaniel Jurgens 
324532f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
324632f69e4bSDaniel Jurgens 		if (i == port_num) {
324732f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
324832f69e4bSDaniel Jurgens 			if (!mpi) {
324932f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
325032f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
325132f69e4bSDaniel Jurgens 				return -ENOMEM;
325232f69e4bSDaniel Jurgens 			}
325332f69e4bSDaniel Jurgens 
325432f69e4bSDaniel Jurgens 			mpi->is_master = true;
325532f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
325632f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
325732f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
325832f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
325932f69e4bSDaniel Jurgens 			mpi = NULL;
326032f69e4bSDaniel Jurgens 			continue;
326132f69e4bSDaniel Jurgens 		}
326232f69e4bSDaniel Jurgens 
326332f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
326432f69e4bSDaniel Jurgens 				    list) {
326532f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
326632f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
326732f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
326832f69e4bSDaniel Jurgens 			}
326932f69e4bSDaniel Jurgens 
327032f69e4bSDaniel Jurgens 			if (bound) {
3271c42260f1SVu Pham 				dev_dbg(mpi->mdev->device,
3272c42260f1SVu Pham 					"removing port from unaffiliated list.\n");
327332f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
327432f69e4bSDaniel Jurgens 				list_del(&mpi->list);
327532f69e4bSDaniel Jurgens 				break;
327632f69e4bSDaniel Jurgens 			}
327732f69e4bSDaniel Jurgens 		}
32787416790eSParav Pandit 		if (!bound)
327932f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
328032f69e4bSDaniel Jurgens 				    i + 1);
328132f69e4bSDaniel Jurgens 	}
328232f69e4bSDaniel Jurgens 
328332f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
328432f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
328532f69e4bSDaniel Jurgens 	return err;
328632f69e4bSDaniel Jurgens }
328732f69e4bSDaniel Jurgens 
328832f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
328932f69e4bSDaniel Jurgens {
32901fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
329132f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
329232f69e4bSDaniel Jurgens 							  port_num + 1);
32931fb7f897SMark Bloch 	u32 i;
329432f69e4bSDaniel Jurgens 
329532f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
329632f69e4bSDaniel Jurgens 		return;
329732f69e4bSDaniel Jurgens 
329832f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
329932f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
330032f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
330132f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
330232f69e4bSDaniel Jurgens 			if (i == port_num) {
330332f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
330432f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
330532f69e4bSDaniel Jurgens 			} else {
33061fb7f897SMark Bloch 				mlx5_ib_dbg(dev, "unbinding port_num: %u\n",
33071fb7f897SMark Bloch 					    i + 1);
33087ce6095eSLeon Romanovsky 				list_add_tail(&dev->port[i].mp.mpi->list,
33097ce6095eSLeon Romanovsky 					      &mlx5_ib_unaffiliated_port_list);
33104a754d76SLeon Romanovsky 				mlx5_ib_unbind_slave_port(dev,
33114a754d76SLeon Romanovsky 							  dev->port[i].mp.mpi);
331232f69e4bSDaniel Jurgens 			}
331332f69e4bSDaniel Jurgens 		}
331432f69e4bSDaniel Jurgens 	}
331532f69e4bSDaniel Jurgens 
331632f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
331732f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
331832f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
331932f69e4bSDaniel Jurgens 
332032f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
332132f69e4bSDaniel Jurgens }
332232f69e4bSDaniel Jurgens 
3323342ee59dSYishai Hadas static int mmap_obj_cleanup(struct ib_uobject *uobject,
33247be76befSYishai Hadas 			    enum rdma_remove_reason why,
33257be76befSYishai Hadas 			    struct uverbs_attr_bundle *attrs)
33267be76befSYishai Hadas {
33277be76befSYishai Hadas 	struct mlx5_user_mmap_entry *obj = uobject->object;
33287be76befSYishai Hadas 
33297be76befSYishai Hadas 	rdma_user_mmap_entry_remove(&obj->rdma_entry);
33307be76befSYishai Hadas 	return 0;
33317be76befSYishai Hadas }
33327be76befSYishai Hadas 
3333342ee59dSYishai Hadas static int mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext *c,
3334342ee59dSYishai Hadas 					    struct mlx5_user_mmap_entry *entry,
3335342ee59dSYishai Hadas 					    size_t length)
3336342ee59dSYishai Hadas {
3337342ee59dSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
3338342ee59dSYishai Hadas 		&c->ibucontext, &entry->rdma_entry, length,
3339342ee59dSYishai Hadas 		(MLX5_IB_MMAP_OFFSET_START << 16),
3340342ee59dSYishai Hadas 		((MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1));
3341342ee59dSYishai Hadas }
3342342ee59dSYishai Hadas 
33437be76befSYishai Hadas static struct mlx5_user_mmap_entry *
33447be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c)
33457be76befSYishai Hadas {
33467be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
33477be76befSYishai Hadas 	struct mlx5_var_table *var_table;
33487be76befSYishai Hadas 	u32 page_idx;
33497be76befSYishai Hadas 	int err;
33507be76befSYishai Hadas 
33517be76befSYishai Hadas 	var_table = &to_mdev(c->ibucontext.device)->var_table;
33527be76befSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
33537be76befSYishai Hadas 	if (!entry)
33547be76befSYishai Hadas 		return ERR_PTR(-ENOMEM);
33557be76befSYishai Hadas 
33567be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
33577be76befSYishai Hadas 	page_idx = find_first_zero_bit(var_table->bitmap,
33587be76befSYishai Hadas 				       var_table->num_var_hw_entries);
33597be76befSYishai Hadas 	if (page_idx >= var_table->num_var_hw_entries) {
33607be76befSYishai Hadas 		err = -ENOSPC;
33617be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
33627be76befSYishai Hadas 		goto end;
33637be76befSYishai Hadas 	}
33647be76befSYishai Hadas 
33657be76befSYishai Hadas 	set_bit(page_idx, var_table->bitmap);
33667be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
33677be76befSYishai Hadas 
33687be76befSYishai Hadas 	entry->address = var_table->hw_start_addr +
33697be76befSYishai Hadas 				(page_idx * var_table->stride_size);
33707be76befSYishai Hadas 	entry->page_idx = page_idx;
33717be76befSYishai Hadas 	entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR;
33727be76befSYishai Hadas 
3373342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry,
3374342ee59dSYishai Hadas 					       var_table->stride_size);
33757be76befSYishai Hadas 	if (err)
33767be76befSYishai Hadas 		goto err_insert;
33777be76befSYishai Hadas 
33787be76befSYishai Hadas 	return entry;
33797be76befSYishai Hadas 
33807be76befSYishai Hadas err_insert:
33817be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
33827be76befSYishai Hadas 	clear_bit(page_idx, var_table->bitmap);
33837be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
33847be76befSYishai Hadas end:
33857be76befSYishai Hadas 	kfree(entry);
33867be76befSYishai Hadas 	return ERR_PTR(err);
33877be76befSYishai Hadas }
33887be76befSYishai Hadas 
33897be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)(
33907be76befSYishai Hadas 	struct uverbs_attr_bundle *attrs)
33917be76befSYishai Hadas {
33927be76befSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
33937be76befSYishai Hadas 		attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
33947be76befSYishai Hadas 	struct mlx5_ib_ucontext *c;
33957be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
33967be76befSYishai Hadas 	u64 mmap_offset;
33977be76befSYishai Hadas 	u32 length;
33987be76befSYishai Hadas 	int err;
33997be76befSYishai Hadas 
34007be76befSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
34017be76befSYishai Hadas 	if (IS_ERR(c))
34027be76befSYishai Hadas 		return PTR_ERR(c);
34037be76befSYishai Hadas 
34047be76befSYishai Hadas 	entry = alloc_var_entry(c);
34057be76befSYishai Hadas 	if (IS_ERR(entry))
34067be76befSYishai Hadas 		return PTR_ERR(entry);
34077be76befSYishai Hadas 
34087be76befSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
34097be76befSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
34107be76befSYishai Hadas 	uobj->object = entry;
34110ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
34127be76befSYishai Hadas 
34137be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
34147be76befSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
34157be76befSYishai Hadas 	if (err)
34160ac8903cSJason Gunthorpe 		return err;
34177be76befSYishai Hadas 
34187be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
34197be76befSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
34207be76befSYishai Hadas 	if (err)
34210ac8903cSJason Gunthorpe 		return err;
34227be76befSYishai Hadas 
34237be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
34247be76befSYishai Hadas 			     &length, sizeof(length));
34257be76befSYishai Hadas 	return err;
34267be76befSYishai Hadas }
34277be76befSYishai Hadas 
34287be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
34297be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_ALLOC,
34307be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE,
34317be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
34327be76befSYishai Hadas 			UVERBS_ACCESS_NEW,
34337be76befSYishai Hadas 			UA_MANDATORY),
34347be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
34357be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
34367be76befSYishai Hadas 			   UA_MANDATORY),
34377be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
34387be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
34397be76befSYishai Hadas 			   UA_MANDATORY),
34407be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
34417be76befSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
34427be76befSYishai Hadas 			    UA_MANDATORY));
34437be76befSYishai Hadas 
34447be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
34457be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_DESTROY,
34467be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE,
34477be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
34487be76befSYishai Hadas 			UVERBS_ACCESS_DESTROY,
34497be76befSYishai Hadas 			UA_MANDATORY));
34507be76befSYishai Hadas 
34517be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR,
3452342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
34537be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC),
34547be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY));
34557be76befSYishai Hadas 
34567be76befSYishai Hadas static bool var_is_supported(struct ib_device *device)
34577be76befSYishai Hadas {
34587be76befSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(device);
34597be76befSYishai Hadas 
34607be76befSYishai Hadas 	return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
34617be76befSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q);
34627be76befSYishai Hadas }
34637be76befSYishai Hadas 
3464342ee59dSYishai Hadas static struct mlx5_user_mmap_entry *
3465342ee59dSYishai Hadas alloc_uar_entry(struct mlx5_ib_ucontext *c,
3466342ee59dSYishai Hadas 		enum mlx5_ib_uapi_uar_alloc_type alloc_type)
3467342ee59dSYishai Hadas {
3468342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
3469342ee59dSYishai Hadas 	struct mlx5_ib_dev *dev;
3470342ee59dSYishai Hadas 	u32 uar_index;
3471342ee59dSYishai Hadas 	int err;
3472342ee59dSYishai Hadas 
3473342ee59dSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
3474342ee59dSYishai Hadas 	if (!entry)
3475342ee59dSYishai Hadas 		return ERR_PTR(-ENOMEM);
3476342ee59dSYishai Hadas 
3477342ee59dSYishai Hadas 	dev = to_mdev(c->ibucontext.device);
3478342ee59dSYishai Hadas 	err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
3479342ee59dSYishai Hadas 	if (err)
3480342ee59dSYishai Hadas 		goto end;
3481342ee59dSYishai Hadas 
3482342ee59dSYishai Hadas 	entry->page_idx = uar_index;
3483342ee59dSYishai Hadas 	entry->address = uar_index2paddress(dev, uar_index);
3484342ee59dSYishai Hadas 	if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3485342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC;
3486342ee59dSYishai Hadas 	else
3487342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_NC;
3488342ee59dSYishai Hadas 
3489342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry, PAGE_SIZE);
3490342ee59dSYishai Hadas 	if (err)
3491342ee59dSYishai Hadas 		goto err_insert;
3492342ee59dSYishai Hadas 
3493342ee59dSYishai Hadas 	return entry;
3494342ee59dSYishai Hadas 
3495342ee59dSYishai Hadas err_insert:
3496342ee59dSYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, uar_index);
3497342ee59dSYishai Hadas end:
3498342ee59dSYishai Hadas 	kfree(entry);
3499342ee59dSYishai Hadas 	return ERR_PTR(err);
3500342ee59dSYishai Hadas }
3501342ee59dSYishai Hadas 
3502342ee59dSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)(
3503342ee59dSYishai Hadas 	struct uverbs_attr_bundle *attrs)
3504342ee59dSYishai Hadas {
3505342ee59dSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
3506342ee59dSYishai Hadas 		attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3507342ee59dSYishai Hadas 	enum mlx5_ib_uapi_uar_alloc_type alloc_type;
3508342ee59dSYishai Hadas 	struct mlx5_ib_ucontext *c;
3509342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
3510342ee59dSYishai Hadas 	u64 mmap_offset;
3511342ee59dSYishai Hadas 	u32 length;
3512342ee59dSYishai Hadas 	int err;
3513342ee59dSYishai Hadas 
3514342ee59dSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
3515342ee59dSYishai Hadas 	if (IS_ERR(c))
3516342ee59dSYishai Hadas 		return PTR_ERR(c);
3517342ee59dSYishai Hadas 
3518342ee59dSYishai Hadas 	err = uverbs_get_const(&alloc_type, attrs,
3519342ee59dSYishai Hadas 			       MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE);
3520342ee59dSYishai Hadas 	if (err)
3521342ee59dSYishai Hadas 		return err;
3522342ee59dSYishai Hadas 
3523342ee59dSYishai Hadas 	if (alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF &&
3524342ee59dSYishai Hadas 	    alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_NC)
3525342ee59dSYishai Hadas 		return -EOPNOTSUPP;
3526342ee59dSYishai Hadas 
3527342ee59dSYishai Hadas 	if (!to_mdev(c->ibucontext.device)->wc_support &&
3528342ee59dSYishai Hadas 	    alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3529342ee59dSYishai Hadas 		return -EOPNOTSUPP;
3530342ee59dSYishai Hadas 
3531342ee59dSYishai Hadas 	entry = alloc_uar_entry(c, alloc_type);
3532342ee59dSYishai Hadas 	if (IS_ERR(entry))
3533342ee59dSYishai Hadas 		return PTR_ERR(entry);
3534342ee59dSYishai Hadas 
3535342ee59dSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
3536342ee59dSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
3537342ee59dSYishai Hadas 	uobj->object = entry;
35380ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3539342ee59dSYishai Hadas 
3540342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3541342ee59dSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
3542342ee59dSYishai Hadas 	if (err)
35430ac8903cSJason Gunthorpe 		return err;
3544342ee59dSYishai Hadas 
3545342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3546342ee59dSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
3547342ee59dSYishai Hadas 	if (err)
35480ac8903cSJason Gunthorpe 		return err;
3549342ee59dSYishai Hadas 
3550342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3551342ee59dSYishai Hadas 			     &length, sizeof(length));
3552342ee59dSYishai Hadas 	return err;
3553342ee59dSYishai Hadas }
3554342ee59dSYishai Hadas 
3555342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
3556342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_ALLOC,
3557342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE,
3558342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
3559342ee59dSYishai Hadas 			UVERBS_ACCESS_NEW,
3560342ee59dSYishai Hadas 			UA_MANDATORY),
3561342ee59dSYishai Hadas 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE,
3562342ee59dSYishai Hadas 			     enum mlx5_ib_uapi_uar_alloc_type,
3563342ee59dSYishai Hadas 			     UA_MANDATORY),
3564342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3565342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
3566342ee59dSYishai Hadas 			   UA_MANDATORY),
3567342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3568342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
3569342ee59dSYishai Hadas 			   UA_MANDATORY),
3570342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3571342ee59dSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
3572342ee59dSYishai Hadas 			    UA_MANDATORY));
3573342ee59dSYishai Hadas 
3574342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
3575342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_DESTROY,
3576342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_DESTROY_HANDLE,
3577342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
3578342ee59dSYishai Hadas 			UVERBS_ACCESS_DESTROY,
3579342ee59dSYishai Hadas 			UA_MANDATORY));
3580342ee59dSYishai Hadas 
3581342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_UAR,
3582342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
3583342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_ALLOC),
3584342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_DESTROY));
3585342ee59dSYishai Hadas 
35869a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
35879a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
35889a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
3589c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
3590bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
3591bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
3592c6475a0bSAviad Yehezkel 
35930fb556b2SYishai Hadas ADD_UVERBS_ATTRIBUTES_SIMPLE(
35940fb556b2SYishai Hadas 	mlx5_ib_query_context,
35950fb556b2SYishai Hadas 	UVERBS_OBJECT_DEVICE,
35960fb556b2SYishai Hadas 	UVERBS_METHOD_QUERY_CONTEXT,
35970fb556b2SYishai Hadas 	UVERBS_ATTR_PTR_OUT(
35980fb556b2SYishai Hadas 		MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
35990fb556b2SYishai Hadas 		UVERBS_ATTR_STRUCT(struct mlx5_ib_alloc_ucontext_resp,
36000fb556b2SYishai Hadas 				   dump_fill_mkey),
36010fb556b2SYishai Hadas 		UA_MANDATORY));
36020fb556b2SYishai Hadas 
36030cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
360436e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
36050cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
360630f2fe40SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_qos_defs),
360705f71ef9SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_std_types_defs),
3608831df883SMaor Gottlieb 	UAPI_DEF_CHAIN(mlx5_ib_dm_defs),
36098c84660bSMatan Barak 
36100cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
36110cbf432dSJason Gunthorpe 				&mlx5_ib_flow_action),
36120fb556b2SYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DEVICE, &mlx5_ib_query_context),
36137be76befSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR,
36147be76befSYishai Hadas 				UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)),
3615342ee59dSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_UAR),
36160cbf432dSJason Gunthorpe 	{}
36170cbf432dSJason Gunthorpe };
36188c84660bSMatan Barak 
3619fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
3620e126ba97SEli Cohen {
362132f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
3622806b101bSJason Gunthorpe 	WARN_ON(!xa_empty(&dev->odp_mkeys));
3623ab40530aSParav Pandit 	mutex_destroy(&dev->cap_mask_mutex);
362450211ec9SJason Gunthorpe 	WARN_ON(!xa_empty(&dev->sig_mrs));
36254056b12eSAriel Levkovich 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
362616c1975fSMark Bloch }
362716c1975fSMark Bloch 
3628fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
362916c1975fSMark Bloch {
363016c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
3631e126ba97SEli Cohen 	int err;
363232f69e4bSDaniel Jurgens 	int i;
3633e126ba97SEli Cohen 
363413179652SParav Pandit 	dev->ib_dev.node_type = RDMA_NODE_IB_CA;
363513179652SParav Pandit 	dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
363613179652SParav Pandit 	dev->ib_dev.phys_port_cnt = dev->num_ports;
363713179652SParav Pandit 	dev->ib_dev.dev.parent = mdev->device;
363813179652SParav Pandit 	dev->ib_dev.lag_flags = RDMA_LAG_FLAGS_HASH_ALL_SLAVES;
363913179652SParav Pandit 
364032f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
364132f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
364295579e78SMark Bloch 		rwlock_init(&dev->port[i].roce.netdev_lock);
3643d3b5cc1cSMark Bloch 		dev->port[i].roce.dev = dev;
3644d3b5cc1cSMark Bloch 		dev->port[i].roce.native_port_num = i + 1;
3645d3b5cc1cSMark Bloch 		dev->port[i].roce.last_port_state = IB_PORT_DOWN;
364632f69e4bSDaniel Jurgens 	}
364732f69e4bSDaniel Jurgens 
364832f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
3649e126ba97SEli Cohen 	if (err)
3650da796ccbSMark Bloch 		return err;
3651e126ba97SEli Cohen 
3652a989ea01SMark Bloch 	err = set_has_smi_cap(dev);
3653a989ea01SMark Bloch 	if (err)
36542cb091f6SParav Pandit 		goto err_mp;
3655e126ba97SEli Cohen 
36562019d70eSParav Pandit 	err = mlx5_query_max_pkeys(&dev->ib_dev, &dev->pkey_table_len);
36572019d70eSParav Pandit 	if (err)
36582019d70eSParav Pandit 		goto err_mp;
36592019d70eSParav Pandit 
36601b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3661e126ba97SEli Cohen 		get_ext_port_caps(dev);
3662e126ba97SEli Cohen 
3663f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
3664e126ba97SEli Cohen 
36653cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
36663cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
36673cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
3668806b101bSJason Gunthorpe 	xa_init(&dev->odp_mkeys);
366950211ec9SJason Gunthorpe 	xa_init(&dev->sig_mrs);
3670f743ff3bSSaeed Mahameed 	atomic_set(&dev->mkey_var, 0);
36713cc297dbSMark Bloch 
36723b113a1eSAriel Levkovich 	spin_lock_init(&dev->dm.lock);
36733b113a1eSAriel Levkovich 	dev->dm.dev = mdev;
367416c1975fSMark Bloch 	return 0;
367525c13324SAriel Levkovich 
367632f69e4bSDaniel Jurgens err_mp:
367732f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
3678d286ac1dSParav Pandit 	return err;
367916c1975fSMark Bloch }
368016c1975fSMark Bloch 
3681ac47bf5eSLeon Romanovsky static int mlx5_ib_enable_driver(struct ib_device *dev)
36829a4ca38dSMark Bloch {
3683ac47bf5eSLeon Romanovsky 	struct mlx5_ib_dev *mdev = to_mdev(dev);
3684ac47bf5eSLeon Romanovsky 	int ret;
36859a4ca38dSMark Bloch 
3686ac47bf5eSLeon Romanovsky 	ret = mlx5_ib_test_wc(mdev);
3687ac47bf5eSLeon Romanovsky 	mlx5_ib_dbg(mdev, "Write-Combining %s",
3688ac47bf5eSLeon Romanovsky 		    mdev->wc_support ? "supported" : "not supported");
36899a4ca38dSMark Bloch 
3690ac47bf5eSLeon Romanovsky 	return ret;
36919a4ca38dSMark Bloch }
36929a4ca38dSMark Bloch 
369396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
36947a154142SJason Gunthorpe 	.owner = THIS_MODULE,
3695b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_MLX5,
369672c6ec18SJason Gunthorpe 	.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION,
3697b9560a41SJason Gunthorpe 
369896458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
369996458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
37006c984472SMax Gurtovoy 	.alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
370196458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
370296458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
370396458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
370496458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
370596458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
370696458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
370796458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
370896458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
3709676a80adSJason Gunthorpe 	.create_user_ah = mlx5_ib_create_ah,
371096458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
371196458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
371296458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
371396458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
371496458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
371596458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
371696458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
371796458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
371896458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
371996458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
372096458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
372196458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
3722915e4af5SJason Gunthorpe 	.device_group = &mlx5_attr_group,
372311f552e2SMichael Guralnik 	.enable_driver = mlx5_ib_enable_driver,
372496458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
372596458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
372696458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
372796458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
37286c984472SMax Gurtovoy 	.map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
372996458233SKamal Heib 	.mmap = mlx5_ib_mmap,
3730dc2316ebSYishai Hadas 	.mmap_free = mlx5_ib_mmap_free,
373196458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
373296458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
373396458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
373496458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
373596458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
373696458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
3737029e88fdSLeon Romanovsky 	.post_recv = mlx5_ib_post_recv_nodrain,
3738029e88fdSLeon Romanovsky 	.post_send = mlx5_ib_post_send_nodrain,
373996458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
374096458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
374196458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
374296458233SKamal Heib 	.query_device = mlx5_ib_query_device,
374396458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
374496458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
374596458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
374696458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
37470fb556b2SYishai Hadas 	.query_ucontext = mlx5_ib_query_ucontext,
374896458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
374990da7dc8SJianxin Xiong 	.reg_user_mr_dmabuf = mlx5_ib_reg_user_mr_dmabuf,
375096458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
375196458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
375296458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
3753d3456914SLeon Romanovsky 
3754d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
37553b023e1bSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_counters, mlx5_ib_mcounters, ibcntrs),
3756e39afe3dSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
375721a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
3758514aee66SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_qp, mlx5_ib_qp, ibqp),
375968e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
3760a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
376196458233SKamal Heib };
376296458233SKamal Heib 
376396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
376496458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
376596458233SKamal Heib };
376696458233SKamal Heib 
376796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
376896458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
37699c0015efSDanit Goldberg 	.get_vf_guid = mlx5_ib_get_vf_guid,
377096458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
377196458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
377296458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
377396458233SKamal Heib };
377496458233SKamal Heib 
377596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
377696458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
377796458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
3778d18bb3e1SLeon Romanovsky 
3779d18bb3e1SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_mw, mlx5_ib_mw, ibmw),
378096458233SKamal Heib };
378196458233SKamal Heib 
378296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
378396458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
378496458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
378528ad5f65SLeon Romanovsky 
378628ad5f65SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_xrcd, mlx5_ib_xrcd, ibxrcd),
378796458233SKamal Heib };
378896458233SKamal Heib 
3789f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev)
3790f164be8cSYishai Hadas {
3791f164be8cSYishai Hadas 	struct mlx5_core_dev *mdev = dev->mdev;
3792f164be8cSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
3793f164be8cSYishai Hadas 	u8 log_doorbell_bar_size;
3794f164be8cSYishai Hadas 	u8 log_doorbell_stride;
3795f164be8cSYishai Hadas 	u64 bar_size;
3796f164be8cSYishai Hadas 
3797f164be8cSYishai Hadas 	log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
3798f164be8cSYishai Hadas 					log_doorbell_bar_size);
3799f164be8cSYishai Hadas 	log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
3800f164be8cSYishai Hadas 					log_doorbell_stride);
3801f164be8cSYishai Hadas 	var_table->hw_start_addr = dev->mdev->bar_addr +
3802f164be8cSYishai Hadas 				MLX5_CAP64_DEV_VDPA_EMULATION(mdev,
3803f164be8cSYishai Hadas 					doorbell_bar_offset);
3804f164be8cSYishai Hadas 	bar_size = (1ULL << log_doorbell_bar_size) * 4096;
3805f164be8cSYishai Hadas 	var_table->stride_size = 1ULL << log_doorbell_stride;
380691b74bf5SAlexander Lobakin 	var_table->num_var_hw_entries = div_u64(bar_size,
380791b74bf5SAlexander Lobakin 						var_table->stride_size);
3808f164be8cSYishai Hadas 	mutex_init(&var_table->bitmap_lock);
3809f164be8cSYishai Hadas 	var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries,
3810f164be8cSYishai Hadas 					  GFP_KERNEL);
3811f164be8cSYishai Hadas 	return (var_table->bitmap) ? 0 : -ENOMEM;
3812f164be8cSYishai Hadas }
3813f164be8cSYishai Hadas 
3814f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev)
3815f164be8cSYishai Hadas {
3816f164be8cSYishai Hadas 	bitmap_free(dev->var_table.bitmap);
3817f164be8cSYishai Hadas }
3818f164be8cSYishai Hadas 
3819fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
382016c1975fSMark Bloch {
382116c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
382216c1975fSMark Bloch 	int err;
382316c1975fSMark Bloch 
3824f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
3825f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
382696458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
382796458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
38288e959601SNiranjana Vishwanathapura 
382996458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
383096458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
38317c2344c3SMaor Gottlieb 
38326e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
38336e8484c5SMax Gurtovoy 
383444ce37bcSJason Gunthorpe 	if (MLX5_CAP_GEN(mdev, imaicl))
383596458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
3836d2370e0aSMatan Barak 
383744ce37bcSJason Gunthorpe 	if (MLX5_CAP_GEN(mdev, xrc))
383896458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
3839e126ba97SEli Cohen 
384025c13324SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic) ||
384125c13324SAriel Levkovich 	    MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
384225c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
384396458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
384424da0016SAriel Levkovich 
384596458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
384681e30880SYishai Hadas 
384736e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
384836e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
384981e30880SYishai Hadas 
3850e126ba97SEli Cohen 	err = init_node_data(dev);
3851e126ba97SEli Cohen 	if (err)
385216c1975fSMark Bloch 		return err;
3853e126ba97SEli Cohen 
3854c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
3855e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
3856e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
3857a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
3858c8b89924SMark Bloch 
3859f164be8cSYishai Hadas 	if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
3860f164be8cSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) {
3861f164be8cSYishai Hadas 		err = mlx5_ib_init_var_table(dev);
3862f164be8cSYishai Hadas 		if (err)
3863f164be8cSYishai Hadas 			return err;
3864f164be8cSYishai Hadas 	}
3865f164be8cSYishai Hadas 
386696e2fd73SLeon Romanovsky 	dev->ib_dev.use_cq_dim = true;
386796e2fd73SLeon Romanovsky 
386816c1975fSMark Bloch 	return 0;
386916c1975fSMark Bloch }
387016c1975fSMark Bloch 
387196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
387296458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
387396458233SKamal Heib 	.query_port = mlx5_ib_query_port,
387496458233SKamal Heib };
387596458233SKamal Heib 
38768e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
387716c1975fSMark Bloch {
387896458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
38798e6efa3aSMark Bloch 	return 0;
38808e6efa3aSMark Bloch }
38818e6efa3aSMark Bloch 
388296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
388396458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
388496458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
3885d6fd59e1SParav Pandit 	.query_pkey = mlx5_ib_rep_query_pkey,
388696458233SKamal Heib };
388796458233SKamal Heib 
3888b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev)
38898e6efa3aSMark Bloch {
389096458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
38918e6efa3aSMark Bloch 	return 0;
38928e6efa3aSMark Bloch }
38938e6efa3aSMark Bloch 
389496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
389596458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
389696458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
389796458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
389896458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
389996458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
390096458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
3901c0a6b5ecSLeon Romanovsky 
3902c0a6b5ecSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_rwq_ind_table, mlx5_ib_rwq_ind_table,
3903c0a6b5ecSLeon Romanovsky 			   ib_rwq_ind_tbl),
390496458233SKamal Heib };
390596458233SKamal Heib 
39061e2b5a90SLeon Romanovsky static int mlx5_ib_roce_init(struct mlx5_ib_dev *dev)
39078e6efa3aSMark Bloch {
39081e2b5a90SLeon Romanovsky 	struct mlx5_core_dev *mdev = dev->mdev;
39091e2b5a90SLeon Romanovsky 	enum rdma_link_layer ll;
39101e2b5a90SLeon Romanovsky 	int port_type_cap;
39111fb7f897SMark Bloch 	u32 port_num = 0;
39121e2b5a90SLeon Romanovsky 	int err;
39137fd8aefbSDaniel Jurgens 
39141e2b5a90SLeon Romanovsky 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
39151e2b5a90SLeon Romanovsky 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
39161e2b5a90SLeon Romanovsky 
39171e2b5a90SLeon Romanovsky 	if (ll == IB_LINK_LAYER_ETHERNET) {
391896458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
39198e6efa3aSMark Bloch 
3920e3f1ed1fSLeon Romanovsky 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
3921e3f1ed1fSLeon Romanovsky 
392226628e2dSMark Bloch 		/* Register only for native ports */
39231e2b5a90SLeon Romanovsky 		err = mlx5_add_netdev_notifier(dev, port_num);
39247852546fSMaor Gottlieb 		if (err || dev->is_rep || !mlx5_is_roce_init_enabled(mdev))
39251e2b5a90SLeon Romanovsky 			/*
39261e2b5a90SLeon Romanovsky 			 * We don't enable ETH interface for
39271e2b5a90SLeon Romanovsky 			 * 1. IB representors
39281e2b5a90SLeon Romanovsky 			 * 2. User disabled ROCE through devlink interface
39291e2b5a90SLeon Romanovsky 			 */
39308e6efa3aSMark Bloch 			return err;
39318e6efa3aSMark Bloch 
3932e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
39338e6efa3aSMark Bloch 		if (err)
39348e6efa3aSMark Bloch 			goto cleanup;
39358e6efa3aSMark Bloch 	}
39368e6efa3aSMark Bloch 
393716c1975fSMark Bloch 	return 0;
39388e6efa3aSMark Bloch cleanup:
39391e2b5a90SLeon Romanovsky 	mlx5_remove_netdev_notifier(dev, port_num);
39408e6efa3aSMark Bloch 	return err;
394145bded2cSKamal Heib }
39426aec21f6SHaggai Eran 
39431e2b5a90SLeon Romanovsky static void mlx5_ib_roce_cleanup(struct mlx5_ib_dev *dev)
394416c1975fSMark Bloch {
394516c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
394616c1975fSMark Bloch 	enum rdma_link_layer ll;
394716c1975fSMark Bloch 	int port_type_cap;
39481fb7f897SMark Bloch 	u32 port_num;
39494a2da0b8SParav Pandit 
395016c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
395116c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
395216c1975fSMark Bloch 
395316c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
39541e2b5a90SLeon Romanovsky 		if (!dev->is_rep)
395516c1975fSMark Bloch 			mlx5_disable_eth(dev);
395616c1975fSMark Bloch 
39571e2b5a90SLeon Romanovsky 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
39581e2b5a90SLeon Romanovsky 		mlx5_remove_netdev_notifier(dev, port_num);
395916c1975fSMark Bloch 	}
396016c1975fSMark Bloch }
396116c1975fSMark Bloch 
396216c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
396316c1975fSMark Bloch {
396473eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
3965a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
396673eb8f03SGreg Kroah-Hartman 	return 0;
396716c1975fSMark Bloch }
396816c1975fSMark Bloch 
396916c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
397016c1975fSMark Bloch {
3971a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
3972a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
397316c1975fSMark Bloch }
397416c1975fSMark Bloch 
397516c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
397616c1975fSMark Bloch {
39775fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
3978444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
397916c1975fSMark Bloch }
398016c1975fSMark Bloch 
398116c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
398216c1975fSMark Bloch {
398316c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
398416c1975fSMark Bloch }
398516c1975fSMark Bloch 
3986fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
398716c1975fSMark Bloch {
398816c1975fSMark Bloch 	int err;
39895fe9dec0SEli Cohen 
39905fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
39915fe9dec0SEli Cohen 	if (err)
399216c1975fSMark Bloch 		return err;
39935fe9dec0SEli Cohen 
39945fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
39955fe9dec0SEli Cohen 	if (err)
39961c3aa6bdSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->bfreg);
39975fe9dec0SEli Cohen 
399816c1975fSMark Bloch 	return err;
399916c1975fSMark Bloch }
40000837e86aSMark Bloch 
4001fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
400216c1975fSMark Bloch {
400316c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
400416c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
400516c1975fSMark Bloch }
4006e126ba97SEli Cohen 
4007fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
400816c1975fSMark Bloch {
4009e349f858SJason Gunthorpe 	const char *name;
4010e349f858SJason Gunthorpe 
40117c34ec19SAviv Heller 	if (!mlx5_lag_is_roce(dev->mdev))
4012e349f858SJason Gunthorpe 		name = "mlx5_%d";
4013e349f858SJason Gunthorpe 	else
4014e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
4015e0477b34SJason Gunthorpe 	return ib_register_device(&dev->ib_dev, name, &dev->mdev->pdev->dev);
401616c1975fSMark Bloch }
401716c1975fSMark Bloch 
4018fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
401942cea83fSMark Bloch {
40201e2b5a90SLeon Romanovsky 	int err;
40211e2b5a90SLeon Romanovsky 
40221e2b5a90SLeon Romanovsky 	err = mlx5_mr_cache_cleanup(dev);
40231e2b5a90SLeon Romanovsky 	if (err)
40241e2b5a90SLeon Romanovsky 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
40251e2b5a90SLeon Romanovsky 
40261e2b5a90SLeon Romanovsky 	if (dev->umrc.qp)
40270dc0da15SLeon Romanovsky 		ib_destroy_qp(dev->umrc.qp);
40281e2b5a90SLeon Romanovsky 	if (dev->umrc.cq)
40291e2b5a90SLeon Romanovsky 		ib_free_cq(dev->umrc.cq);
40301e2b5a90SLeon Romanovsky 	if (dev->umrc.pd)
40311e2b5a90SLeon Romanovsky 		ib_dealloc_pd(dev->umrc.pd);
403242cea83fSMark Bloch }
403342cea83fSMark Bloch 
4034fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
403516c1975fSMark Bloch {
403616c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
403716c1975fSMark Bloch }
403816c1975fSMark Bloch 
40391e2b5a90SLeon Romanovsky enum {
40401e2b5a90SLeon Romanovsky 	MAX_UMR_WR = 128,
40411e2b5a90SLeon Romanovsky };
40421e2b5a90SLeon Romanovsky 
4043fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
404416c1975fSMark Bloch {
40451e2b5a90SLeon Romanovsky 	struct ib_qp_init_attr *init_attr = NULL;
40461e2b5a90SLeon Romanovsky 	struct ib_qp_attr *attr = NULL;
40471e2b5a90SLeon Romanovsky 	struct ib_pd *pd;
40481e2b5a90SLeon Romanovsky 	struct ib_cq *cq;
40491e2b5a90SLeon Romanovsky 	struct ib_qp *qp;
40501e2b5a90SLeon Romanovsky 	int ret;
40511e2b5a90SLeon Romanovsky 
40521e2b5a90SLeon Romanovsky 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
40531e2b5a90SLeon Romanovsky 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
40541e2b5a90SLeon Romanovsky 	if (!attr || !init_attr) {
40551e2b5a90SLeon Romanovsky 		ret = -ENOMEM;
40561e2b5a90SLeon Romanovsky 		goto error_0;
40571e2b5a90SLeon Romanovsky 	}
40581e2b5a90SLeon Romanovsky 
40591e2b5a90SLeon Romanovsky 	pd = ib_alloc_pd(&dev->ib_dev, 0);
40601e2b5a90SLeon Romanovsky 	if (IS_ERR(pd)) {
40611e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
40621e2b5a90SLeon Romanovsky 		ret = PTR_ERR(pd);
40631e2b5a90SLeon Romanovsky 		goto error_0;
40641e2b5a90SLeon Romanovsky 	}
40651e2b5a90SLeon Romanovsky 
40661e2b5a90SLeon Romanovsky 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
40671e2b5a90SLeon Romanovsky 	if (IS_ERR(cq)) {
40681e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
40691e2b5a90SLeon Romanovsky 		ret = PTR_ERR(cq);
40701e2b5a90SLeon Romanovsky 		goto error_2;
40711e2b5a90SLeon Romanovsky 	}
40721e2b5a90SLeon Romanovsky 
40731e2b5a90SLeon Romanovsky 	init_attr->send_cq = cq;
40741e2b5a90SLeon Romanovsky 	init_attr->recv_cq = cq;
40751e2b5a90SLeon Romanovsky 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
40761e2b5a90SLeon Romanovsky 	init_attr->cap.max_send_wr = MAX_UMR_WR;
40771e2b5a90SLeon Romanovsky 	init_attr->cap.max_send_sge = 1;
40781e2b5a90SLeon Romanovsky 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
40791e2b5a90SLeon Romanovsky 	init_attr->port_num = 1;
40800dc0da15SLeon Romanovsky 	qp = ib_create_qp(pd, init_attr);
40811e2b5a90SLeon Romanovsky 	if (IS_ERR(qp)) {
40821e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
40831e2b5a90SLeon Romanovsky 		ret = PTR_ERR(qp);
40841e2b5a90SLeon Romanovsky 		goto error_3;
40851e2b5a90SLeon Romanovsky 	}
40861e2b5a90SLeon Romanovsky 
40871e2b5a90SLeon Romanovsky 	attr->qp_state = IB_QPS_INIT;
40881e2b5a90SLeon Romanovsky 	attr->port_num = 1;
40890dc0da15SLeon Romanovsky 	ret = ib_modify_qp(qp, attr,
40900dc0da15SLeon Romanovsky 			   IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT);
40911e2b5a90SLeon Romanovsky 	if (ret) {
40921e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
40931e2b5a90SLeon Romanovsky 		goto error_4;
40941e2b5a90SLeon Romanovsky 	}
40951e2b5a90SLeon Romanovsky 
40961e2b5a90SLeon Romanovsky 	memset(attr, 0, sizeof(*attr));
40971e2b5a90SLeon Romanovsky 	attr->qp_state = IB_QPS_RTR;
40981e2b5a90SLeon Romanovsky 	attr->path_mtu = IB_MTU_256;
40991e2b5a90SLeon Romanovsky 
41000dc0da15SLeon Romanovsky 	ret = ib_modify_qp(qp, attr, IB_QP_STATE);
41011e2b5a90SLeon Romanovsky 	if (ret) {
41021e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
41031e2b5a90SLeon Romanovsky 		goto error_4;
41041e2b5a90SLeon Romanovsky 	}
41051e2b5a90SLeon Romanovsky 
41061e2b5a90SLeon Romanovsky 	memset(attr, 0, sizeof(*attr));
41071e2b5a90SLeon Romanovsky 	attr->qp_state = IB_QPS_RTS;
41080dc0da15SLeon Romanovsky 	ret = ib_modify_qp(qp, attr, IB_QP_STATE);
41091e2b5a90SLeon Romanovsky 	if (ret) {
41101e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
41111e2b5a90SLeon Romanovsky 		goto error_4;
41121e2b5a90SLeon Romanovsky 	}
41131e2b5a90SLeon Romanovsky 
41141e2b5a90SLeon Romanovsky 	dev->umrc.qp = qp;
41151e2b5a90SLeon Romanovsky 	dev->umrc.cq = cq;
41161e2b5a90SLeon Romanovsky 	dev->umrc.pd = pd;
41171e2b5a90SLeon Romanovsky 
41181e2b5a90SLeon Romanovsky 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
41191e2b5a90SLeon Romanovsky 	ret = mlx5_mr_cache_init(dev);
41201e2b5a90SLeon Romanovsky 	if (ret) {
41211e2b5a90SLeon Romanovsky 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
41221e2b5a90SLeon Romanovsky 		goto error_4;
41231e2b5a90SLeon Romanovsky 	}
41241e2b5a90SLeon Romanovsky 
41251e2b5a90SLeon Romanovsky 	kfree(attr);
41261e2b5a90SLeon Romanovsky 	kfree(init_attr);
41271e2b5a90SLeon Romanovsky 
41281e2b5a90SLeon Romanovsky 	return 0;
41291e2b5a90SLeon Romanovsky 
41301e2b5a90SLeon Romanovsky error_4:
41310dc0da15SLeon Romanovsky 	ib_destroy_qp(qp);
41321e2b5a90SLeon Romanovsky 	dev->umrc.qp = NULL;
41331e2b5a90SLeon Romanovsky 
41341e2b5a90SLeon Romanovsky error_3:
41351e2b5a90SLeon Romanovsky 	ib_free_cq(cq);
41361e2b5a90SLeon Romanovsky 	dev->umrc.cq = NULL;
41371e2b5a90SLeon Romanovsky 
41381e2b5a90SLeon Romanovsky error_2:
41391e2b5a90SLeon Romanovsky 	ib_dealloc_pd(pd);
41401e2b5a90SLeon Romanovsky 	dev->umrc.pd = NULL;
41411e2b5a90SLeon Romanovsky 
41421e2b5a90SLeon Romanovsky error_0:
41431e2b5a90SLeon Romanovsky 	kfree(attr);
41441e2b5a90SLeon Romanovsky 	kfree(init_attr);
41451e2b5a90SLeon Romanovsky 	return ret;
414616c1975fSMark Bloch }
414716c1975fSMark Bloch 
414816c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
414916c1975fSMark Bloch {
41501e2b5a90SLeon Romanovsky 	struct dentry *root;
415103404e8aSMaor Gottlieb 
41521e2b5a90SLeon Romanovsky 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
41531e2b5a90SLeon Romanovsky 		return 0;
41541e2b5a90SLeon Romanovsky 
41551e2b5a90SLeon Romanovsky 	mutex_init(&dev->delay_drop.lock);
41561e2b5a90SLeon Romanovsky 	dev->delay_drop.dev = dev;
41571e2b5a90SLeon Romanovsky 	dev->delay_drop.activate = false;
41581e2b5a90SLeon Romanovsky 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
41591e2b5a90SLeon Romanovsky 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
41601e2b5a90SLeon Romanovsky 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
41611e2b5a90SLeon Romanovsky 	atomic_set(&dev->delay_drop.events_cnt, 0);
41621e2b5a90SLeon Romanovsky 
41631e2b5a90SLeon Romanovsky 	if (!mlx5_debugfs_root)
41641e2b5a90SLeon Romanovsky 		return 0;
41651e2b5a90SLeon Romanovsky 
41661e2b5a90SLeon Romanovsky 	root = debugfs_create_dir("delay_drop", dev->mdev->priv.dbg_root);
41671e2b5a90SLeon Romanovsky 	dev->delay_drop.dir_debugfs = root;
41681e2b5a90SLeon Romanovsky 
41691e2b5a90SLeon Romanovsky 	debugfs_create_atomic_t("num_timeout_events", 0400, root,
41701e2b5a90SLeon Romanovsky 				&dev->delay_drop.events_cnt);
41711e2b5a90SLeon Romanovsky 	debugfs_create_atomic_t("num_rqs", 0400, root,
41721e2b5a90SLeon Romanovsky 				&dev->delay_drop.rqs_cnt);
41731e2b5a90SLeon Romanovsky 	debugfs_create_file("timeout", 0600, root, &dev->delay_drop,
41741e2b5a90SLeon Romanovsky 			    &fops_delay_drop_timeout);
417516c1975fSMark Bloch 	return 0;
417616c1975fSMark Bloch }
417716c1975fSMark Bloch 
417816c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
417916c1975fSMark Bloch {
41801e2b5a90SLeon Romanovsky 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
41811e2b5a90SLeon Romanovsky 		return;
41821e2b5a90SLeon Romanovsky 
41831e2b5a90SLeon Romanovsky 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
41841e2b5a90SLeon Romanovsky 	if (!dev->delay_drop.dir_debugfs)
41851e2b5a90SLeon Romanovsky 		return;
41861e2b5a90SLeon Romanovsky 
41871e2b5a90SLeon Romanovsky 	debugfs_remove_recursive(dev->delay_drop.dir_debugfs);
41881e2b5a90SLeon Romanovsky 	dev->delay_drop.dir_debugfs = NULL;
418916c1975fSMark Bloch }
419016c1975fSMark Bloch 
4191df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
4192df097a27SSaeed Mahameed {
4193df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
4194df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
4195df097a27SSaeed Mahameed 	return 0;
4196df097a27SSaeed Mahameed }
4197df097a27SSaeed Mahameed 
4198df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
4199df097a27SSaeed Mahameed {
4200df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
4201df097a27SSaeed Mahameed }
4202df097a27SSaeed Mahameed 
4203b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
420416c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
420516c1975fSMark Bloch 		      int stage)
420616c1975fSMark Bloch {
42074cca96a8SParav Pandit 	dev->ib_active = false;
42084cca96a8SParav Pandit 
420916c1975fSMark Bloch 	/* Number of stages to cleanup */
421016c1975fSMark Bloch 	while (stage) {
421116c1975fSMark Bloch 		stage--;
421216c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
421316c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
421416c1975fSMark Bloch 	}
42154a6dc855SMark Bloch 
4216da796ccbSMark Bloch 	kfree(dev->port);
42174a6dc855SMark Bloch 	ib_dealloc_device(&dev->ib_dev);
421816c1975fSMark Bloch }
421916c1975fSMark Bloch 
422093f82444SLeon Romanovsky int __mlx5_ib_add(struct mlx5_ib_dev *dev,
422116c1975fSMark Bloch 		  const struct mlx5_ib_profile *profile)
422216c1975fSMark Bloch {
422316c1975fSMark Bloch 	int err;
422416c1975fSMark Bloch 	int i;
422516c1975fSMark Bloch 
42268d93efb8SMark Bloch 	dev->profile = profile;
42278d93efb8SMark Bloch 
422816c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
422916c1975fSMark Bloch 		if (profile->stage[i].init) {
423016c1975fSMark Bloch 			err = profile->stage[i].init(dev);
423116c1975fSMark Bloch 			if (err)
423216c1975fSMark Bloch 				goto err_out;
423316c1975fSMark Bloch 		}
423416c1975fSMark Bloch 	}
423516c1975fSMark Bloch 
4236e126ba97SEli Cohen 	dev->ib_active = true;
423793f82444SLeon Romanovsky 	return 0;
4238e126ba97SEli Cohen 
423916c1975fSMark Bloch err_out:
424093f82444SLeon Romanovsky 	/* Clean up stages which were initialized */
424193f82444SLeon Romanovsky 	while (i) {
424293f82444SLeon Romanovsky 		i--;
424393f82444SLeon Romanovsky 		if (profile->stage[i].cleanup)
424493f82444SLeon Romanovsky 			profile->stage[i].cleanup(dev);
424593f82444SLeon Romanovsky 	}
424693f82444SLeon Romanovsky 	return -ENOMEM;
4247e126ba97SEli Cohen }
4248e126ba97SEli Cohen 
424916c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
425016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
425116c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
425216c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
4253f7c4ffdaSLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_FS,
4254f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_init,
4255f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_cleanup),
425616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
425716c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
4258f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
42598e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
42608e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
42618e6efa3aSMark Bloch 		     NULL),
426216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
42631e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_init,
42641e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_cleanup),
4265333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
4266333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
4267333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
4268f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4269f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
4270f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
427116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
42721e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_init,
42731e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_cleanup),
4274df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4275df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
4276df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
427716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
42781e2b5a90SLeon Romanovsky 		     mlx5_ib_odp_init_one,
42791e2b5a90SLeon Romanovsky 		     mlx5_ib_odp_cleanup_one),
428016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
428164825827SLeon Romanovsky 		     mlx5_ib_counters_init,
428264825827SLeon Romanovsky 		     mlx5_ib_counters_cleanup),
428316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
428416c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
428516c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
428616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
428716c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
428816c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
428916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
429016c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
429116c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
429242cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
429342cea83fSMark Bloch 		     NULL,
429442cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
429581773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4296d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_init,
4297d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_cleanup),
429816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
429916c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
430016c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
430142cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
430242cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
430342cea83fSMark Bloch 		     NULL),
430416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
430516c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
430616c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
4307b572ebe6SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4308b572ebe6SLeon Romanovsky 		     mlx5_ib_restrack_init,
4309b572ebe6SLeon Romanovsky 		     NULL),
431016c1975fSMark Bloch };
431116c1975fSMark Bloch 
4312b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = {
4313b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
4314b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
4315b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
4316f7c4ffdaSLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_FS,
4317f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_init,
4318f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_cleanup),
4319b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
4320b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
4321f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
4322b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
4323b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_non_default_cb,
4324b5ca15adSMark Bloch 		     NULL),
4325b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
43261e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_init,
43271e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_cleanup),
4328333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
4329333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
4330333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
4331f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4332f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
4333f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
4334b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
43351e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_init,
43361e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_cleanup),
4337df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4338df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
4339df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
4340b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
434164825827SLeon Romanovsky 		     mlx5_ib_counters_init,
434264825827SLeon Romanovsky 		     mlx5_ib_counters_cleanup),
434379db784eSParav Pandit 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
434479db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_init,
434579db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_cleanup),
4346b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
4347b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
4348b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
4349b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
4350b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
4351b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
435203fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
435303fe2debSDavid S. Miller 		     NULL,
435403fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
43557f575103SMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4356d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_init,
4357d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_cleanup),
4358b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
4359b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
4360b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
436103fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
436203fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
436303fe2debSDavid S. Miller 		     NULL),
4364b572ebe6SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4365b572ebe6SLeon Romanovsky 		     mlx5_ib_restrack_init,
4366b572ebe6SLeon Romanovsky 		     NULL),
4367b5ca15adSMark Bloch };
4368b5ca15adSMark Bloch 
436993f82444SLeon Romanovsky static int mlx5r_mp_probe(struct auxiliary_device *adev,
437093f82444SLeon Romanovsky 			  const struct auxiliary_device_id *id)
437132f69e4bSDaniel Jurgens {
437293f82444SLeon Romanovsky 	struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
437393f82444SLeon Romanovsky 	struct mlx5_core_dev *mdev = idev->mdev;
437432f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
437532f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
437632f69e4bSDaniel Jurgens 	bool bound = false;
437732f69e4bSDaniel Jurgens 	int err;
437832f69e4bSDaniel Jurgens 
437932f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
438032f69e4bSDaniel Jurgens 	if (!mpi)
438193f82444SLeon Romanovsky 		return -ENOMEM;
438232f69e4bSDaniel Jurgens 
438332f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
438432f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
438532f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
438632f69e4bSDaniel Jurgens 	if (err) {
438732f69e4bSDaniel Jurgens 		kfree(mpi);
438893f82444SLeon Romanovsky 		return err;
438932f69e4bSDaniel Jurgens 	}
439032f69e4bSDaniel Jurgens 
439132f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
439232f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
439332f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
439432f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
439532f69e4bSDaniel Jurgens 
439632f69e4bSDaniel Jurgens 		if (bound) {
439732f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
439897f30d32SMaor Gottlieb 			mpi->ibdev->ib_active = true;
439932f69e4bSDaniel Jurgens 			break;
440032f69e4bSDaniel Jurgens 		}
440132f69e4bSDaniel Jurgens 	}
440232f69e4bSDaniel Jurgens 
440332f69e4bSDaniel Jurgens 	if (!bound) {
440432f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
4405c42260f1SVu Pham 		dev_dbg(mdev->device,
4406c42260f1SVu Pham 			"no suitable IB device found to bind to, added to unaffiliated list.\n");
440732f69e4bSDaniel Jurgens 	}
440832f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
440932f69e4bSDaniel Jurgens 
441093f82444SLeon Romanovsky 	dev_set_drvdata(&adev->dev, mpi);
441193f82444SLeon Romanovsky 	return 0;
441232f69e4bSDaniel Jurgens }
441332f69e4bSDaniel Jurgens 
441493f82444SLeon Romanovsky static void mlx5r_mp_remove(struct auxiliary_device *adev)
441516c1975fSMark Bloch {
441693f82444SLeon Romanovsky 	struct mlx5_ib_multiport_info *mpi;
441793f82444SLeon Romanovsky 
441893f82444SLeon Romanovsky 	mpi = dev_get_drvdata(&adev->dev);
441993f82444SLeon Romanovsky 	mutex_lock(&mlx5_ib_multiport_mutex);
442093f82444SLeon Romanovsky 	if (mpi->ibdev)
442193f82444SLeon Romanovsky 		mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
442293f82444SLeon Romanovsky 	list_del(&mpi->list);
442393f82444SLeon Romanovsky 	mutex_unlock(&mlx5_ib_multiport_mutex);
442493f82444SLeon Romanovsky 	kfree(mpi);
442593f82444SLeon Romanovsky }
442693f82444SLeon Romanovsky 
442793f82444SLeon Romanovsky static int mlx5r_probe(struct auxiliary_device *adev,
442893f82444SLeon Romanovsky 		       const struct auxiliary_device_id *id)
442993f82444SLeon Romanovsky {
443093f82444SLeon Romanovsky 	struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
443193f82444SLeon Romanovsky 	struct mlx5_core_dev *mdev = idev->mdev;
443294de879cSMichael Guralnik 	const struct mlx5_ib_profile *profile;
443393f82444SLeon Romanovsky 	int port_type_cap, num_ports, ret;
443432f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
4435b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
4436f0666f1fSBodong Wang 
443732f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
443832f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
443932f69e4bSDaniel Jurgens 
4440da796ccbSMark Bloch 	num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
4441da796ccbSMark Bloch 			MLX5_CAP_GEN(mdev, num_vhca_ports));
4442459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
4443b5ca15adSMark Bloch 	if (!dev)
444493f82444SLeon Romanovsky 		return -ENOMEM;
4445da796ccbSMark Bloch 	dev->port = kcalloc(num_ports, sizeof(*dev->port),
4446da796ccbSMark Bloch 			     GFP_KERNEL);
4447da796ccbSMark Bloch 	if (!dev->port) {
4448a5c9c299SParav Pandit 		ib_dealloc_device(&dev->ib_dev);
444993f82444SLeon Romanovsky 		return -ENOMEM;
4450da796ccbSMark Bloch 	}
4451b5ca15adSMark Bloch 
4452b5ca15adSMark Bloch 	dev->mdev = mdev;
4453da796ccbSMark Bloch 	dev->num_ports = num_ports;
4454b5ca15adSMark Bloch 
44557852546fSMaor Gottlieb 	if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_is_roce_init_enabled(mdev))
445694de879cSMichael Guralnik 		profile = &raw_eth_profile;
445794de879cSMichael Guralnik 	else
445894de879cSMichael Guralnik 		profile = &pf_profile;
445994de879cSMichael Guralnik 
446093f82444SLeon Romanovsky 	ret = __mlx5_ib_add(dev, profile);
446193f82444SLeon Romanovsky 	if (ret) {
446293f82444SLeon Romanovsky 		kfree(dev->port);
446393f82444SLeon Romanovsky 		ib_dealloc_device(&dev->ib_dev);
446493f82444SLeon Romanovsky 		return ret;
446516c1975fSMark Bloch 	}
446616c1975fSMark Bloch 
446793f82444SLeon Romanovsky 	dev_set_drvdata(&adev->dev, dev);
446893f82444SLeon Romanovsky 	return 0;
446993f82444SLeon Romanovsky }
447093f82444SLeon Romanovsky 
447193f82444SLeon Romanovsky static void mlx5r_remove(struct auxiliary_device *adev)
4472e126ba97SEli Cohen {
447332f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
44746aec21f6SHaggai Eran 
447593f82444SLeon Romanovsky 	dev = dev_get_drvdata(&adev->dev);
447616c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
4477e126ba97SEli Cohen }
4478e126ba97SEli Cohen 
447993f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_mp_id_table[] = {
448093f82444SLeon Romanovsky 	{ .name = MLX5_ADEV_NAME ".multiport", },
448193f82444SLeon Romanovsky 	{},
448293f82444SLeon Romanovsky };
448393f82444SLeon Romanovsky 
448493f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_id_table[] = {
448593f82444SLeon Romanovsky 	{ .name = MLX5_ADEV_NAME ".rdma", },
448693f82444SLeon Romanovsky 	{},
448793f82444SLeon Romanovsky };
448893f82444SLeon Romanovsky 
448993f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_mp_id_table);
449093f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_id_table);
449193f82444SLeon Romanovsky 
449293f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_mp_driver = {
449393f82444SLeon Romanovsky 	.name = "multiport",
449493f82444SLeon Romanovsky 	.probe = mlx5r_mp_probe,
449593f82444SLeon Romanovsky 	.remove = mlx5r_mp_remove,
449693f82444SLeon Romanovsky 	.id_table = mlx5r_mp_id_table,
449793f82444SLeon Romanovsky };
449893f82444SLeon Romanovsky 
449993f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_driver = {
450093f82444SLeon Romanovsky 	.name = "rdma",
450193f82444SLeon Romanovsky 	.probe = mlx5r_probe,
450293f82444SLeon Romanovsky 	.remove = mlx5r_remove,
450393f82444SLeon Romanovsky 	.id_table = mlx5r_id_table,
4504e126ba97SEli Cohen };
4505e126ba97SEli Cohen 
4506e126ba97SEli Cohen static int __init mlx5_ib_init(void)
4507e126ba97SEli Cohen {
450893f82444SLeon Romanovsky 	int ret;
45096aec21f6SHaggai Eran 
45108010d74bSJason Gunthorpe 	xlt_emergency_page = (void *)__get_free_page(GFP_KERNEL);
4511c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
4512d69a24e0SDaniel Jurgens 		return -ENOMEM;
4513d69a24e0SDaniel Jurgens 
4514c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
4515c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
45168010d74bSJason Gunthorpe 		free_page((unsigned long)xlt_emergency_page);
4517c44ef998SIlya Lesokhin 		return -ENOMEM;
4518c44ef998SIlya Lesokhin 	}
4519c44ef998SIlya Lesokhin 
452081713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
452193f82444SLeon Romanovsky 	ret = mlx5r_rep_init();
452293f82444SLeon Romanovsky 	if (ret)
452393f82444SLeon Romanovsky 		goto rep_err;
452493f82444SLeon Romanovsky 	ret = auxiliary_driver_register(&mlx5r_mp_driver);
452593f82444SLeon Romanovsky 	if (ret)
452693f82444SLeon Romanovsky 		goto mp_err;
452793f82444SLeon Romanovsky 	ret = auxiliary_driver_register(&mlx5r_driver);
452893f82444SLeon Romanovsky 	if (ret)
452993f82444SLeon Romanovsky 		goto drv_err;
453093f82444SLeon Romanovsky 	return 0;
45319603b61dSJack Morgenstein 
453293f82444SLeon Romanovsky drv_err:
453393f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_mp_driver);
453493f82444SLeon Romanovsky mp_err:
453593f82444SLeon Romanovsky 	mlx5r_rep_cleanup();
453693f82444SLeon Romanovsky rep_err:
453793f82444SLeon Romanovsky 	destroy_workqueue(mlx5_ib_event_wq);
453893f82444SLeon Romanovsky 	free_page((unsigned long)xlt_emergency_page);
453993f82444SLeon Romanovsky 	return ret;
4540e126ba97SEli Cohen }
4541e126ba97SEli Cohen 
4542e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
4543e126ba97SEli Cohen {
454493f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_driver);
454593f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_mp_driver);
454693f82444SLeon Romanovsky 	mlx5r_rep_cleanup();
454793f82444SLeon Romanovsky 
4548d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
45498010d74bSJason Gunthorpe 	free_page((unsigned long)xlt_emergency_page);
4550e126ba97SEli Cohen }
4551e126ba97SEli Cohen 
4552e126ba97SEli Cohen module_init(mlx5_ib_init);
4553e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
4554