xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 312b8f79eb05479628ee71357749815b2eeeeea8)
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>
29ca7ef7adSDaisuke Matsuda #include <rdma/ib_umem_odp.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"
4404876c12SAharon Landau #include "umr.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>
498c84660bSMatan Barak 
508c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
518c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
52e126ba97SEli Cohen 
53e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
54daeee976SShay Drory MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) IB driver");
55e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
56e126ba97SEli Cohen 
57d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
58d69a24e0SDaniel Jurgens 	struct work_struct	work;
59df097a27SSaeed Mahameed 	union {
60df097a27SSaeed Mahameed 		struct mlx5_ib_dev	      *dev;
61df097a27SSaeed Mahameed 		struct mlx5_ib_multiport_info *mpi;
62df097a27SSaeed Mahameed 	};
63df097a27SSaeed Mahameed 	bool			is_slave;
64134e9349SSaeed Mahameed 	unsigned int		event;
65df097a27SSaeed Mahameed 	void			*param;
66d69a24e0SDaniel Jurgens };
67d69a24e0SDaniel Jurgens 
68da7525d2SEran Ben Elisha enum {
69da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
70da7525d2SEran Ben Elisha };
711b5daf11SMajd Dibbiny 
72d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
7332f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
7432f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
7532f69e4bSDaniel Jurgens /*
7632f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
7732f69e4bSDaniel Jurgens  */
7832f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
7932f69e4bSDaniel Jurgens 
8032f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
8132f69e4bSDaniel Jurgens {
8232f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
8332f69e4bSDaniel Jurgens 
8432f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
8532f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
8632f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
8732f69e4bSDaniel Jurgens 	return dev;
8832f69e4bSDaniel Jurgens }
8932f69e4bSDaniel Jurgens 
901b5daf11SMajd Dibbiny static enum rdma_link_layer
91ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
921b5daf11SMajd Dibbiny {
93ebd61f68SAchiad Shochat 	switch (port_type_cap) {
941b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
951b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
961b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
971b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
981b5daf11SMajd Dibbiny 	default:
991b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1001b5daf11SMajd Dibbiny 	}
1011b5daf11SMajd Dibbiny }
1021b5daf11SMajd Dibbiny 
103ebd61f68SAchiad Shochat static enum rdma_link_layer
1041fb7f897SMark Bloch mlx5_ib_port_link_layer(struct ib_device *device, u32 port_num)
105ebd61f68SAchiad Shochat {
106ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
107ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
108ebd61f68SAchiad Shochat 
109ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
110ebd61f68SAchiad Shochat }
111ebd61f68SAchiad Shochat 
112fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
1131fb7f897SMark Bloch 			  u32 port_num,
114fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
115fd65f1b8SMoni Shoua {
116fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
117fd65f1b8SMoni Shoua 	int ret;
118fd65f1b8SMoni Shoua 
119fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1203023a1e9SKamal Heib 	ret = ibdev->ops.query_port(ibdev, port_num, &attr);
121fd65f1b8SMoni Shoua 	if (!ret)
122fd65f1b8SMoni Shoua 		*state = attr.state;
123fd65f1b8SMoni Shoua 	return ret;
124fd65f1b8SMoni Shoua }
125fd65f1b8SMoni Shoua 
12635b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
12735b0aa67SMark Bloch 					   struct net_device *ndev,
128c446d9daSMark Bloch 					   struct net_device *upper,
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 
140c446d9daSMark Bloch 		if (upper == ndev && port->rep->vport == MLX5_VPORT_UPLINK) {
141c446d9daSMark Bloch 			*port_num = i + 1;
142c446d9daSMark Bloch 			return &port->roce;
143c446d9daSMark Bloch 		}
144c446d9daSMark Bloch 
145c446d9daSMark Bloch 		if (upper && port->rep->vport == MLX5_VPORT_UPLINK)
146c446d9daSMark Bloch 			continue;
147c446d9daSMark Bloch 
14835b0aa67SMark Bloch 		read_lock(&port->roce.netdev_lock);
149658cfcebSMark Bloch 		rep_ndev = mlx5_ib_get_rep_netdev(port->rep->esw,
15035b0aa67SMark Bloch 						  port->rep->vport);
15135b0aa67SMark Bloch 		if (rep_ndev == ndev) {
15235b0aa67SMark Bloch 			read_unlock(&port->roce.netdev_lock);
15335b0aa67SMark Bloch 			*port_num = i + 1;
15435b0aa67SMark Bloch 			return &port->roce;
15535b0aa67SMark Bloch 		}
15635b0aa67SMark Bloch 		read_unlock(&port->roce.netdev_lock);
15735b0aa67SMark Bloch 	}
15835b0aa67SMark Bloch 
15935b0aa67SMark Bloch 	return NULL;
16035b0aa67SMark Bloch }
16135b0aa67SMark Bloch 
162fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
163fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
164fc24fc5eSAchiad Shochat {
1657fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
166fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1671fb7f897SMark Bloch 	u32 port_num = roce->native_port_num;
1687fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1697fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1707fd8aefbSDaniel Jurgens 
1717fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
17232f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
17332f69e4bSDaniel Jurgens 	if (!mdev)
17432f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
175fc24fc5eSAchiad Shochat 
1765ec8c83eSAviv Heller 	switch (event) {
1775ec8c83eSAviv Heller 	case NETDEV_REGISTER:
17835b0aa67SMark Bloch 		/* Should already be registered during the load */
17935b0aa67SMark Bloch 		if (ibdev->is_rep)
18035b0aa67SMark Bloch 			break;
1817fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
182dce45af5SLinus Torvalds 		if (ndev->dev.parent == mdev->device)
183842a9c83SOr Gerlitz 			roce->netdev = ndev;
1847fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
1855ec8c83eSAviv Heller 		break;
1865ec8c83eSAviv Heller 
187842a9c83SOr Gerlitz 	case NETDEV_UNREGISTER:
18835b0aa67SMark Bloch 		/* In case of reps, ib device goes away before the netdevs */
189842a9c83SOr Gerlitz 		write_lock(&roce->netdev_lock);
190842a9c83SOr Gerlitz 		if (roce->netdev == ndev)
191842a9c83SOr Gerlitz 			roce->netdev = NULL;
192842a9c83SOr Gerlitz 		write_unlock(&roce->netdev_lock);
193842a9c83SOr Gerlitz 		break;
194842a9c83SOr Gerlitz 
195fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
1965ec8c83eSAviv Heller 	case NETDEV_UP:
19788621dfeSAviv Heller 	case NETDEV_DOWN: {
1987fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
19988621dfeSAviv Heller 		struct net_device *upper = NULL;
20088621dfeSAviv Heller 
20188621dfeSAviv Heller 		if (lag_ndev) {
20288621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
20388621dfeSAviv Heller 			dev_put(lag_ndev);
20488621dfeSAviv Heller 		}
20588621dfeSAviv Heller 
20635b0aa67SMark Bloch 		if (ibdev->is_rep)
207c446d9daSMark Bloch 			roce = mlx5_get_rep_roce(ibdev, ndev, upper, &port_num);
20835b0aa67SMark Bloch 		if (!roce)
20935b0aa67SMark Bloch 			return NOTIFY_DONE;
210c446d9daSMark Bloch 		if ((upper == ndev ||
211c446d9daSMark Bloch 		     ((!upper || ibdev->is_rep) && ndev == roce->netdev)) &&
212c446d9daSMark Bloch 		    ibdev->ib_active) {
213626bc02dSBart Van Assche 			struct ib_event ibev = { };
214fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2155ec8c83eSAviv Heller 
2167fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2177fd8aefbSDaniel Jurgens 					   &port_state))
2187fd8aefbSDaniel Jurgens 				goto done;
219fd65f1b8SMoni Shoua 
2207fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2217fd8aefbSDaniel Jurgens 				goto done;
222fd65f1b8SMoni Shoua 
2237fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2245ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
225fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
226fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
227fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
228fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
229fd65f1b8SMoni Shoua 			else
2307fd8aefbSDaniel Jurgens 				goto done;
231fd65f1b8SMoni Shoua 
2327fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2335ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2345ec8c83eSAviv Heller 		}
2355ec8c83eSAviv Heller 		break;
23688621dfeSAviv Heller 	}
2375ec8c83eSAviv Heller 
2385ec8c83eSAviv Heller 	default:
2395ec8c83eSAviv Heller 		break;
2405ec8c83eSAviv Heller 	}
2417fd8aefbSDaniel Jurgens done:
24232f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
243fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
244fc24fc5eSAchiad Shochat }
245fc24fc5eSAchiad Shochat 
246fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
2471fb7f897SMark Bloch 					     u32 port_num)
248fc24fc5eSAchiad Shochat {
249fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
250fc24fc5eSAchiad Shochat 	struct net_device *ndev;
25132f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
252fc24fc5eSAchiad Shochat 
25332f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
25432f69e4bSDaniel Jurgens 	if (!mdev)
25532f69e4bSDaniel Jurgens 		return NULL;
25632f69e4bSDaniel Jurgens 
25732f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
25888621dfeSAviv Heller 	if (ndev)
25932f69e4bSDaniel Jurgens 		goto out;
26088621dfeSAviv Heller 
261fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
262fc24fc5eSAchiad Shochat 	 */
26395579e78SMark Bloch 	read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
26495579e78SMark Bloch 	ndev = ibdev->port[port_num - 1].roce.netdev;
265fc24fc5eSAchiad Shochat 	if (ndev)
266fc24fc5eSAchiad Shochat 		dev_hold(ndev);
26795579e78SMark Bloch 	read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
268fc24fc5eSAchiad Shochat 
26932f69e4bSDaniel Jurgens out:
27032f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
271fc24fc5eSAchiad Shochat 	return ndev;
272fc24fc5eSAchiad Shochat }
273fc24fc5eSAchiad Shochat 
27432f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
2751fb7f897SMark Bloch 						   u32 ib_port_num,
2761fb7f897SMark Bloch 						   u32 *native_port_num)
27732f69e4bSDaniel Jurgens {
27832f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
27932f69e4bSDaniel Jurgens 							  ib_port_num);
28032f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
28132f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
28232f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
28332f69e4bSDaniel Jurgens 
284210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
285210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
286210b1f78SMark Bloch 		if (native_port_num)
287210b1f78SMark Bloch 			*native_port_num = ib_port_num;
288210b1f78SMark Bloch 		return ibdev->mdev;
289210b1f78SMark Bloch 	}
290210b1f78SMark Bloch 
29132f69e4bSDaniel Jurgens 	if (native_port_num)
29232f69e4bSDaniel Jurgens 		*native_port_num = 1;
29332f69e4bSDaniel Jurgens 
29432f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
29532f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
29632f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
29732f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
29832f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
29932f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
30032f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
30132f69e4bSDaniel Jurgens 		 */
30232f69e4bSDaniel Jurgens 		if (!mpi->is_master)
30332f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
30432f69e4bSDaniel Jurgens 	}
30532f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
30632f69e4bSDaniel Jurgens 
30732f69e4bSDaniel Jurgens 	return mdev;
30832f69e4bSDaniel Jurgens }
30932f69e4bSDaniel Jurgens 
3101fb7f897SMark Bloch void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u32 port_num)
31132f69e4bSDaniel Jurgens {
31232f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
31332f69e4bSDaniel Jurgens 							  port_num);
31432f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
31532f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
31632f69e4bSDaniel Jurgens 
31732f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
31832f69e4bSDaniel Jurgens 		return;
31932f69e4bSDaniel Jurgens 
32032f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
32132f69e4bSDaniel Jurgens 
32232f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
32332f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
32432f69e4bSDaniel Jurgens 	if (mpi->is_master)
32532f69e4bSDaniel Jurgens 		goto out;
32632f69e4bSDaniel Jurgens 
32732f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
32832f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
32932f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
33032f69e4bSDaniel Jurgens out:
33132f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
33232f69e4bSDaniel Jurgens }
33332f69e4bSDaniel Jurgens 
334639bf441SAharon Landau static int translate_eth_legacy_proto_oper(u32 eth_proto_oper,
335639bf441SAharon Landau 					   u16 *active_speed, u8 *active_width)
336f1b65df5SNoa Osherovich {
337f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
338f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
339f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
340f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
341f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
342f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
343f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
344f1b65df5SNoa Osherovich 		break;
345f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
346f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
347f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
348f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
349f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
350f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
351f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
352f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
353f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
354f1b65df5SNoa Osherovich 		break;
355f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
356f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
357f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
358f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
359f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
360f1b65df5SNoa Osherovich 		break;
361f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
362f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
363f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
364f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
365f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
366f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
367f1b65df5SNoa Osherovich 		break;
368f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
369f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
370f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
371f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
372f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
373f1b65df5SNoa Osherovich 		break;
374f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
375f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
376f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
377f1b65df5SNoa Osherovich 		break;
378f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
379f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
380f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
381f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
382f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
383f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
384f1b65df5SNoa Osherovich 		break;
385f1b65df5SNoa Osherovich 	default:
386f1b65df5SNoa Osherovich 		return -EINVAL;
387f1b65df5SNoa Osherovich 	}
388f1b65df5SNoa Osherovich 
389f1b65df5SNoa Osherovich 	return 0;
390f1b65df5SNoa Osherovich }
391f1b65df5SNoa Osherovich 
392639bf441SAharon Landau static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u16 *active_speed,
39308e8676fSAya Levin 					u8 *active_width)
39408e8676fSAya Levin {
39508e8676fSAya Levin 	switch (eth_proto_oper) {
39608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
39708e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
39808e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
39908e8676fSAya Levin 		*active_speed = IB_SPEED_SDR;
40008e8676fSAya Levin 		break;
40108e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
40208e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
40308e8676fSAya Levin 		*active_speed = IB_SPEED_DDR;
40408e8676fSAya Levin 		break;
40508e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
40608e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
40708e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
40808e8676fSAya Levin 		break;
40908e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
41008e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
41108e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
41208e8676fSAya Levin 		break;
41308e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
41408e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
41508e8676fSAya Levin 		*active_speed = IB_SPEED_EDR;
41608e8676fSAya Levin 		break;
41708e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
418cd272875SAya Levin 		*active_width = IB_WIDTH_2X;
419cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
420cd272875SAya Levin 		break;
42108e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
42208e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
42308e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
42408e8676fSAya Levin 		break;
425cd272875SAya Levin 	case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
426cd272875SAya Levin 		*active_width = IB_WIDTH_4X;
427cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
428cd272875SAya Levin 		break;
42908e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
43008e8676fSAya Levin 		*active_width = IB_WIDTH_2X;
43108e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
43208e8676fSAya Levin 		break;
433f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_100GAUI_1_100GBASE_CR_KR):
434f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_1X;
435f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
436f946e45fSMeir Lichtinger 		break;
43708e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
43808e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
43908e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
44008e8676fSAya Levin 		break;
441f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_200GAUI_2_200GBASE_CR2_KR2):
442f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_2X;
443f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
444f946e45fSMeir Lichtinger 		break;
445f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_400GAUI_4_400GBASE_CR4_KR4):
446f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_4X;
447f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
448f946e45fSMeir Lichtinger 		break;
44908e8676fSAya Levin 	default:
45008e8676fSAya Levin 		return -EINVAL;
45108e8676fSAya Levin 	}
45208e8676fSAya Levin 
45308e8676fSAya Levin 	return 0;
45408e8676fSAya Levin }
45508e8676fSAya Levin 
456639bf441SAharon Landau static int translate_eth_proto_oper(u32 eth_proto_oper, u16 *active_speed,
45708e8676fSAya Levin 				    u8 *active_width, bool ext)
45808e8676fSAya Levin {
45908e8676fSAya Levin 	return ext ?
46008e8676fSAya Levin 		translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
46108e8676fSAya Levin 					     active_width) :
46208e8676fSAya Levin 		translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
46308e8676fSAya Levin 						active_width);
46408e8676fSAya Levin }
46508e8676fSAya Levin 
4661fb7f897SMark Bloch static int mlx5_query_port_roce(struct ib_device *device, u32 port_num,
4673f89a643SAchiad Shochat 				struct ib_port_attr *props)
4683f89a643SAchiad Shochat {
4693f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
470bc4e12ffSAya Levin 	u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
471da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
47288621dfeSAviv Heller 	struct net_device *ndev, *upper;
4733f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
474b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
475f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
4761fb7f897SMark Bloch 	u32 mdev_port_num;
47708e8676fSAya Levin 	bool ext;
478095b0927SIlan Tayari 	int err;
4793f89a643SAchiad Shochat 
480b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
481b3cbd6f0SDaniel Jurgens 	if (!mdev) {
482b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
483b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
484b3cbd6f0SDaniel Jurgens 		 */
485b3cbd6f0SDaniel Jurgens 		put_mdev = false;
486b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
487b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
488b3cbd6f0SDaniel Jurgens 		port_num = 1;
489b3cbd6f0SDaniel Jurgens 	}
490b3cbd6f0SDaniel Jurgens 
491f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
492f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
49326628e2dSMark Bloch 	 * Use native port in case of reps
49450f22fd8SNoa Osherovich 	 */
49526628e2dSMark Bloch 	if (dev->is_rep)
49626628e2dSMark Bloch 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
49726628e2dSMark Bloch 					   1);
49826628e2dSMark Bloch 	else
499bc4e12ffSAya Levin 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
500b3cbd6f0SDaniel Jurgens 					   mdev_port_num);
501095b0927SIlan Tayari 	if (err)
502b3cbd6f0SDaniel Jurgens 		goto out;
503530c8632SAya Levin 	ext = !!MLX5_GET_ETH_PROTO(ptys_reg, out, true, eth_proto_capability);
50408e8676fSAya Levin 	eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
505f1b65df5SNoa Osherovich 
5067672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
5077672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
5087672ed33SHonggang Li 
509f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
51008e8676fSAya Levin 				 &props->active_width, ext);
5113f89a643SAchiad Shochat 
5127852546fSMaor Gottlieb 	if (!dev->is_rep && dev->mdev->roce.roce_en) {
5137a58779eSParav Pandit 		u16 qkey_viol_cntr;
5147a58779eSParav Pandit 
5153f89a643SAchiad Shochat 		props->port_cap_flags |= IB_PORT_CM_SUP;
5162f944c0fSJason Gunthorpe 		props->ip_gids = true;
5173f89a643SAchiad Shochat 		props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
5183f89a643SAchiad Shochat 						   roce_address_table_size);
5197a58779eSParav Pandit 		mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
5207a58779eSParav Pandit 		props->qkey_viol_cntr = qkey_viol_cntr;
5217a58779eSParav Pandit 	}
5223f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
5233f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
5243f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
5253f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
52672a7720fSKamal Heib 	props->phys_state       = IB_PORT_PHYS_STATE_DISABLED;
5273f89a643SAchiad Shochat 
528b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
529b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
530b3cbd6f0SDaniel Jurgens 		goto out;
531b3cbd6f0SDaniel Jurgens 
5323f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
5333f89a643SAchiad Shochat 	if (!ndev)
534b3cbd6f0SDaniel Jurgens 		goto out;
5353f89a643SAchiad Shochat 
5367c34ec19SAviv Heller 	if (dev->lag_active) {
53788621dfeSAviv Heller 		rcu_read_lock();
53888621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
53988621dfeSAviv Heller 		if (upper) {
54088621dfeSAviv Heller 			dev_put(ndev);
54188621dfeSAviv Heller 			ndev = upper;
54288621dfeSAviv Heller 			dev_hold(ndev);
54388621dfeSAviv Heller 		}
54488621dfeSAviv Heller 		rcu_read_unlock();
54588621dfeSAviv Heller 	}
54688621dfeSAviv Heller 
5473f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
5483f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
54972a7720fSKamal Heib 		props->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
5503f89a643SAchiad Shochat 	}
5513f89a643SAchiad Shochat 
5523f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
5533f89a643SAchiad Shochat 
5543f89a643SAchiad Shochat 	dev_put(ndev);
5553f89a643SAchiad Shochat 
5563f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
557b3cbd6f0SDaniel Jurgens out:
558b3cbd6f0SDaniel Jurgens 	if (put_mdev)
559b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
560b3cbd6f0SDaniel Jurgens 	return err;
5613f89a643SAchiad Shochat }
5623f89a643SAchiad Shochat 
5631fb7f897SMark Bloch static int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num,
564095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
565095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
5663cca2606SAchiad Shochat {
567dedbc2d3SParav Pandit 	enum ib_gid_type gid_type;
568a70c0739SParav Pandit 	u16 vlan_id = 0xffff;
569095b0927SIlan Tayari 	u8 roce_version = 0;
570095b0927SIlan Tayari 	u8 roce_l3_type = 0;
571095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
572a70c0739SParav Pandit 	int ret;
5733cca2606SAchiad Shochat 
574095b0927SIlan Tayari 	gid_type = attr->gid_type;
575dedbc2d3SParav Pandit 	if (gid) {
576a70c0739SParav Pandit 		ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
577a70c0739SParav Pandit 		if (ret)
578a70c0739SParav Pandit 			return ret;
5793cca2606SAchiad Shochat 	}
5803cca2606SAchiad Shochat 
581095b0927SIlan Tayari 	switch (gid_type) {
5821c15b4f2SAvihai Horon 	case IB_GID_TYPE_ROCE:
583095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
5843cca2606SAchiad Shochat 		break;
5853cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
586095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
587dedbc2d3SParav Pandit 		if (gid && ipv6_addr_v4mapped((void *)gid))
588095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
589095b0927SIlan Tayari 		else
590095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
5913cca2606SAchiad Shochat 		break;
5923cca2606SAchiad Shochat 
5933cca2606SAchiad Shochat 	default:
594095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
5953cca2606SAchiad Shochat 	}
5963cca2606SAchiad Shochat 
597095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
598cf34e1feSParav Pandit 				      roce_l3_type, gid->raw, mac,
599a70c0739SParav Pandit 				      vlan_id < VLAN_CFI_MASK, vlan_id,
600cf34e1feSParav Pandit 				      port_num);
6013cca2606SAchiad Shochat }
6023cca2606SAchiad Shochat 
603f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
6043cca2606SAchiad Shochat 			   __always_unused void **context)
6053cca2606SAchiad Shochat {
606414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
607f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
6083cca2606SAchiad Shochat }
6093cca2606SAchiad Shochat 
610414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
611414448d2SParav Pandit 			   __always_unused void **context)
6123cca2606SAchiad Shochat {
613414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
614dedbc2d3SParav Pandit 			     attr->index, NULL, attr);
6153cca2606SAchiad Shochat }
6163cca2606SAchiad Shochat 
6175ac55dfcSMark Zhang __be16 mlx5_get_roce_udp_sport_min(const struct mlx5_ib_dev *dev,
61847ec3866SParav Pandit 				   const struct ib_gid_attr *attr)
6192811ba51SAchiad Shochat {
62047ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
6212811ba51SAchiad Shochat 		return 0;
6222811ba51SAchiad Shochat 
6232811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
6242811ba51SAchiad Shochat }
6252811ba51SAchiad Shochat 
6261b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
6271b5daf11SMajd Dibbiny {
6287fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
629d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
6307fae6655SNoa Osherovich 	return 0;
6311b5daf11SMajd Dibbiny }
6321b5daf11SMajd Dibbiny 
6331b5daf11SMajd Dibbiny enum {
6341b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
6351b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
6361b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
6371b5daf11SMajd Dibbiny };
6381b5daf11SMajd Dibbiny 
6391b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
6401b5daf11SMajd Dibbiny {
6411b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
6421b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
6431b5daf11SMajd Dibbiny 
644ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
6451b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
6461b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
6471b5daf11SMajd Dibbiny 
6481b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
6491b5daf11SMajd Dibbiny }
6501b5daf11SMajd Dibbiny 
651da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
652776a3906SMoni Shoua 			    u8 atomic_size_qp,
653da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
654da7525d2SEran Ben Elisha {
655da7525d2SEran Ben Elisha 	u8 tmp;
656da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
657da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
658bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
659da7525d2SEran Ben Elisha 
660da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
661da7525d2SEran Ben Elisha 	 * of host endianness respond
662da7525d2SEran Ben Elisha 	 */
663da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
664da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
665da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
666da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
667da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
668da7525d2SEran Ben Elisha 	} else {
669da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
670da7525d2SEran Ben Elisha 	}
671da7525d2SEran Ben Elisha }
672da7525d2SEran Ben Elisha 
673776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
674776a3906SMoni Shoua 			       struct ib_device_attr *props)
675776a3906SMoni Shoua {
676776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
677776a3906SMoni Shoua 
678776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
679776a3906SMoni Shoua }
680776a3906SMoni Shoua 
6811b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
6821b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
6831b5daf11SMajd Dibbiny {
6841b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6851b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6861b5daf11SMajd Dibbiny 	u64 tmp;
6871b5daf11SMajd Dibbiny 	int err;
6881b5daf11SMajd Dibbiny 
6891b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6901b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6911b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
6921b5daf11SMajd Dibbiny 							    sys_image_guid);
6931b5daf11SMajd Dibbiny 
6941b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6951b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
6963f89a643SAchiad Shochat 		break;
6973f89a643SAchiad Shochat 
6983f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6993f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
7003f89a643SAchiad Shochat 		break;
7011b5daf11SMajd Dibbiny 
7021b5daf11SMajd Dibbiny 	default:
7031b5daf11SMajd Dibbiny 		return -EINVAL;
7041b5daf11SMajd Dibbiny 	}
7053f89a643SAchiad Shochat 
7063f89a643SAchiad Shochat 	if (!err)
7073f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
7083f89a643SAchiad Shochat 
7093f89a643SAchiad Shochat 	return err;
7103f89a643SAchiad Shochat 
7111b5daf11SMajd Dibbiny }
7121b5daf11SMajd Dibbiny 
7131b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
7141b5daf11SMajd Dibbiny 				u16 *max_pkeys)
7151b5daf11SMajd Dibbiny {
7161b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7171b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7181b5daf11SMajd Dibbiny 
7191b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7201b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7211b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
7221b5daf11SMajd Dibbiny 
7231b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7241b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7251b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
7261b5daf11SMajd Dibbiny 						pkey_table_size));
7271b5daf11SMajd Dibbiny 		return 0;
7281b5daf11SMajd Dibbiny 
7291b5daf11SMajd Dibbiny 	default:
7301b5daf11SMajd Dibbiny 		return -EINVAL;
7311b5daf11SMajd Dibbiny 	}
7321b5daf11SMajd Dibbiny }
7331b5daf11SMajd Dibbiny 
7341b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
7351b5daf11SMajd Dibbiny 				u32 *vendor_id)
7361b5daf11SMajd Dibbiny {
7371b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7381b5daf11SMajd Dibbiny 
7391b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7401b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7411b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
7421b5daf11SMajd Dibbiny 
7431b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7441b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7451b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
7461b5daf11SMajd Dibbiny 
7471b5daf11SMajd Dibbiny 	default:
7481b5daf11SMajd Dibbiny 		return -EINVAL;
7491b5daf11SMajd Dibbiny 	}
7501b5daf11SMajd Dibbiny }
7511b5daf11SMajd Dibbiny 
7521b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
7531b5daf11SMajd Dibbiny 				__be64 *node_guid)
7541b5daf11SMajd Dibbiny {
7551b5daf11SMajd Dibbiny 	u64 tmp;
7561b5daf11SMajd Dibbiny 	int err;
7571b5daf11SMajd Dibbiny 
7581b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7591b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7601b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7611b5daf11SMajd Dibbiny 
7621b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7631b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7643f89a643SAchiad Shochat 		break;
7653f89a643SAchiad Shochat 
7663f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7673f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7683f89a643SAchiad Shochat 		break;
7691b5daf11SMajd Dibbiny 
7701b5daf11SMajd Dibbiny 	default:
7711b5daf11SMajd Dibbiny 		return -EINVAL;
7721b5daf11SMajd Dibbiny 	}
7733f89a643SAchiad Shochat 
7743f89a643SAchiad Shochat 	if (!err)
7753f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7763f89a643SAchiad Shochat 
7773f89a643SAchiad Shochat 	return err;
7781b5daf11SMajd Dibbiny }
7791b5daf11SMajd Dibbiny 
7801b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
781bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
7821b5daf11SMajd Dibbiny };
7831b5daf11SMajd Dibbiny 
7841b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
7851b5daf11SMajd Dibbiny {
7861b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
7871b5daf11SMajd Dibbiny 
7881b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
7891b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
7901b5daf11SMajd Dibbiny 
7911b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
7921b5daf11SMajd Dibbiny 
7931b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
7941b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
7951b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
7961b5daf11SMajd Dibbiny }
7971b5daf11SMajd Dibbiny 
798e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
7992528e33eSMatan Barak 				struct ib_device_attr *props,
8002528e33eSMatan Barak 				struct ib_udata *uhw)
801e126ba97SEli Cohen {
80248357091SLeon Romanovsky 	size_t uhw_outlen = (uhw) ? uhw->outlen : 0;
803e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
804938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
805e126ba97SEli Cohen 	int err = -ENOMEM;
806288c01b7SEli Cohen 	int max_sq_desc;
807e126ba97SEli Cohen 	int max_rq_sg;
808e126ba97SEli Cohen 	int max_sq_sg;
809e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
81085c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
811402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
812402ca536SBodong Wang 	size_t resp_len;
813402ca536SBodong Wang 	u64 max_tso;
814e126ba97SEli Cohen 
815402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
81648357091SLeon Romanovsky 	if (uhw_outlen && uhw_outlen < resp_len)
817402ca536SBodong Wang 		return -EINVAL;
8186f26b2acSErez Alfasi 
819402ca536SBodong Wang 	resp.response_length = resp_len;
820402ca536SBodong Wang 
82148357091SLeon Romanovsky 	if (uhw && uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
8222528e33eSMatan Barak 		return -EINVAL;
8232528e33eSMatan Barak 
824e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
8251b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
8261b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
8271b5daf11SMajd Dibbiny 	if (err)
8281b5daf11SMajd Dibbiny 		return err;
8291b5daf11SMajd Dibbiny 
8302019d70eSParav Pandit 	props->max_pkeys = dev->pkey_table_len;
8311b5daf11SMajd Dibbiny 
8321b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
8331b5daf11SMajd Dibbiny 	if (err)
8341b5daf11SMajd Dibbiny 		return err;
835e126ba97SEli Cohen 
8369603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
8379603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
8389603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
839e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
840e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
841e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
8421a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
843938fe83cSSaeed Mahameed 
844938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
845e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
846938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
847e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
848938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
849e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
850938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
851e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
852d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
853d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
854d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
855d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
856b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
857e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_SG_GAPS_REG;
858d2370e0aSMatan Barak 	}
8590ec52f01SJason Gunthorpe 	/* IB_WR_REG_MR always requires changing the entity size with UMR */
8600ec52f01SJason Gunthorpe 	if (!MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled))
861e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
862938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
863e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_INTEGRITY_HANDOVER;
8642dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8652dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8662dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8672dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8682dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8692dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8702dea9094SSagi Grimberg 	}
871938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
872e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_BLOCK_MULTICAST_LOOPBACK;
873e126ba97SEli Cohen 
87485c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
875e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
876e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
87788115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
878e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
879e8161334SNoa Osherovich 		}
880e8161334SNoa Osherovich 
881e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
882e8161334SNoa Osherovich 			props->raw_packet_caps |=
883e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
88488115fe7SBodong Wang 
885a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) {
886402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
887402ca536SBodong Wang 			if (max_tso) {
888402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
889402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
890402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
891402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
892402ca536SBodong Wang 			}
893402ca536SBodong Wang 		}
89431f69a82SYishai Hadas 
895a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) {
89631f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
89731f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
89831f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
89931f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
90031f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
90131f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
90231f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
90331f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
90431f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
90531f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
9064e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
9074e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
90831f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
90931f69a82SYishai Hadas 		}
91031f69a82SYishai Hadas 	} else {
911a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen)
91231f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
913a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen)
91431f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
915402ca536SBodong Wang 	}
916402ca536SBodong Wang 
917f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
918f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
919e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_UD_TSO;
920f0313965SErez Shitrit 	}
921f0313965SErez Shitrit 
92203404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
92385c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
92485c7c014SDaniel Jurgens 	    raw_support)
92503404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
92603404e8aSMaor Gottlieb 
9271d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
9281d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
9291d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
9301d54f890SYishai Hadas 
931cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
93285c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
93385c7c014SDaniel Jurgens 	    raw_support) {
934e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
935cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
936e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
937e8161334SNoa Osherovich 	}
938cff5a0f3SMajd Dibbiny 
93924da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
94024da0016SAriel Levkovich 		props->max_dm_size =
94124da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
94224da0016SAriel Levkovich 	}
94324da0016SAriel Levkovich 
944da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
945da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
946da6d6ba3SMaor Gottlieb 
947b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
948b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
949b1383aa6SNoa Osherovich 
9501b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
9511b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
952e126ba97SEli Cohen 
953e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
954e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
955938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
956938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
957938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
958938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
959288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
960288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
961288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
962e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
96333023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
96433023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
965986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
966938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9679f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
968938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
969938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
970938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
971938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
972938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
973938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
974938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
975e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
976e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
977911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
978911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
97962e3c379SMax Gurtovoy 	props->max_pi_fast_reg_page_list_len =
98062e3c379SMax Gurtovoy 		props->max_fast_reg_page_list_len / 2;
98136609056SYamin Friedman 	props->max_sgl_rd =
98236609056SYamin Friedman 		MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance);
983776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
98481bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
985938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
986938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
987e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
988e126ba97SEli Cohen 					   props->max_mcast_grp;
98986695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
9907c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
9917c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
992e126ba97SEli Cohen 
993e502b8b0SLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
99400815752SMoni Shoua 		if (dev->odp_caps.general_caps & IB_ODP_SUPPORT)
995e945c653SJason Gunthorpe 			props->kernel_cap_flags |= IBK_ON_DEMAND_PAGING;
9968cdd312cSHaggai Eran 		props->odp_caps = dev->odp_caps;
997a73a8955SMoni Shoua 		if (!uhw) {
998a73a8955SMoni Shoua 			/* ODP for kernel QPs is not implemented for receive
999a73a8955SMoni Shoua 			 * WQEs and SRQ WQEs
1000a73a8955SMoni Shoua 			 */
1001a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.rc_odp_caps &=
1002a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1003a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1004a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.uc_odp_caps &=
1005a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1006a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1007a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.ud_odp_caps &=
1008a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1009a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1010a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.xrc_odp_caps &=
1011a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1012a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1013a73a8955SMoni Shoua 		}
1014e502b8b0SLeon Romanovsky 	}
10158cdd312cSHaggai Eran 
1016e53a9d26SParav Pandit 	if (mlx5_core_is_vf(mdev))
1017e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_VIRTUAL_FUNCTION;
1018eff901d3SEli Cohen 
101931f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
102085c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
102131f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
102231f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
102331f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
102431f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
102531f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
102631f69a82SYishai Hadas 		props->max_wq_type_rq =
102731f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
102831f69a82SYishai Hadas 	}
102931f69a82SYishai Hadas 
1030eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
103178b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
1032eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
103378b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
1034eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
103578b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1036eb761894SArtemy Kovalyov 	}
1037eb761894SArtemy Kovalyov 
103889705e92SDanit Goldberg 	if (MLX5_CAP_GEN(mdev, tag_matching) &&
103989705e92SDanit Goldberg 	    MLX5_CAP_GEN(mdev, rndv_offload_rc)) {
104089705e92SDanit Goldberg 		props->tm_caps.flags = IB_TM_CAP_RNDV_RC;
104189705e92SDanit Goldberg 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
104289705e92SDanit Goldberg 	}
104389705e92SDanit Goldberg 
104487ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
104587ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
104687ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
104787ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
104887ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
104987ab3f52SYonatan Cohen 	}
105087ab3f52SYonatan Cohen 
1051a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), cqe_comp_caps) <= uhw_outlen) {
1052572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
1053572f46bfSYonatan Cohen 
1054572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
10557e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
1056572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
1057572f46bfSYonatan Cohen 					     cqe_compression_max_num);
1058572f46bfSYonatan Cohen 
10597e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
10607e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
10617e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
10626f1006a4SYonatan Cohen 
10636f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
10646f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
10656f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1066572f46bfSYonatan Cohen 		}
10677e43a2a5SBodong Wang 	}
10687e43a2a5SBodong Wang 
1069a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), packet_pacing_caps) <= uhw_outlen &&
107085c7c014SDaniel Jurgens 	    raw_support) {
1071d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1072d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
1073d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
1074d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1075d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
1076d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1077d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
1078d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
107961147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
108061147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
108161147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
108261147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
1083d949167dSBodong Wang 		}
1084d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1085d949167dSBodong Wang 	}
1086d949167dSBodong Wang 
1087a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes) <=
1088a762d460SLeon Romanovsky 	    uhw_outlen) {
1089795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
10909f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1091795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1092050da902SBodong Wang 
1093050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1094050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1095050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1096050da902SBodong Wang 
10979f885201SLeon Romanovsky 		resp.response_length +=
10989f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
10999f885201SLeon Romanovsky 	}
11009f885201SLeon Romanovsky 
1101a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), flags) <= uhw_outlen) {
1102de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
11037a0c8f42SGuy Levi 
1104de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1105de57f2adSGuy Levi 			resp.flags |=
1106de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
11077a0c8f42SGuy Levi 
11087a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
11097a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
11107e11b911SDanit Goldberg 		if (MLX5_CAP_GEN(mdev, qp_packet_based))
11117e11b911SDanit Goldberg 			resp.flags |=
11127e11b911SDanit Goldberg 				MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
11137249c8eaSGuy Levi 
11147249c8eaSGuy Levi 		resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1115de57f2adSGuy Levi 	}
11169f885201SLeon Romanovsky 
1117a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), sw_parsing_caps) <= uhw_outlen) {
111896dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
111996dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
112096dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
112196dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
112296dc3fc5SNoa Osherovich 
112396dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
112496dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
112596dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
112696dc3fc5SNoa Osherovich 
112796dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
112896dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
112996dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
113096dc3fc5SNoa Osherovich 
113196dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
113296dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
113396dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
113496dc3fc5SNoa Osherovich 		}
113596dc3fc5SNoa Osherovich 	}
113696dc3fc5SNoa Osherovich 
1137a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), striding_rq_caps) <= uhw_outlen &&
113885c7c014SDaniel Jurgens 	    raw_support) {
1139b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1140b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1141b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1142b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1143b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1144b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1145c16339b6SMark Zhang 			if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range))
1146c16339b6SMark Zhang 				resp.striding_rq_caps
1147c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1148c16339b6SMark Zhang 					MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1149c16339b6SMark Zhang 			else
1150c16339b6SMark Zhang 				resp.striding_rq_caps
1151c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1152b4f34597SNoa Osherovich 					MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1153b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1154b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1155b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1156b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1157b4f34597SNoa Osherovich 		}
1158b4f34597SNoa Osherovich 	}
1159b4f34597SNoa Osherovich 
1160a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), tunnel_offloads_caps) <= uhw_outlen) {
1161f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1162f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1163f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1164f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1165f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1166f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1167f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1168f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1169f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1170f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
117141e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre))
1172e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1173e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
117441e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_udp))
1175e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1176e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1177f95ef6cbSMaor Gottlieb 	}
1178f95ef6cbSMaor Gottlieb 
117911656f59SLior Nahmanson 	if (offsetofend(typeof(resp), dci_streams_caps) <= uhw_outlen) {
118011656f59SLior Nahmanson 		resp.response_length += sizeof(resp.dci_streams_caps);
118111656f59SLior Nahmanson 
118211656f59SLior Nahmanson 		resp.dci_streams_caps.max_log_num_concurent =
118311656f59SLior Nahmanson 			MLX5_CAP_GEN(mdev, log_max_dci_stream_channels);
118411656f59SLior Nahmanson 
118511656f59SLior Nahmanson 		resp.dci_streams_caps.max_log_num_errored =
118611656f59SLior Nahmanson 			MLX5_CAP_GEN(mdev, log_max_dci_errored_streams);
118711656f59SLior Nahmanson 	}
118811656f59SLior Nahmanson 
118948357091SLeon Romanovsky 	if (uhw_outlen) {
1190402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1191402ca536SBodong Wang 
1192402ca536SBodong Wang 		if (err)
1193402ca536SBodong Wang 			return err;
1194402ca536SBodong Wang 	}
1195402ca536SBodong Wang 
11961b5daf11SMajd Dibbiny 	return 0;
11971b5daf11SMajd Dibbiny }
1198e126ba97SEli Cohen 
1199639bf441SAharon Landau static void translate_active_width(struct ib_device *ibdev, u16 active_width,
12001b5daf11SMajd Dibbiny 				   u8 *ib_width)
12011b5daf11SMajd Dibbiny {
12021b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12031b5daf11SMajd Dibbiny 
1204e27014bdSAharon Landau 	if (active_width & MLX5_PTYS_WIDTH_1X)
12051b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1206e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_2X)
1207d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1208e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_4X)
12091b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1210e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_8X)
12111b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1212e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_12X)
12131b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1214db7a691aSMichael Guralnik 	else {
1215db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
1216e27014bdSAharon Landau 			    active_width);
1217db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
12181b5daf11SMajd Dibbiny 	}
12191b5daf11SMajd Dibbiny 
1220db7a691aSMichael Guralnik 	return;
12211b5daf11SMajd Dibbiny }
12221b5daf11SMajd Dibbiny 
12231b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
12241b5daf11SMajd Dibbiny {
12251b5daf11SMajd Dibbiny 	switch (mtu) {
12261b5daf11SMajd Dibbiny 	case 256: return 1;
12271b5daf11SMajd Dibbiny 	case 512: return 2;
12281b5daf11SMajd Dibbiny 	case 1024: return 3;
12291b5daf11SMajd Dibbiny 	case 2048: return 4;
12301b5daf11SMajd Dibbiny 	case 4096: return 5;
12311b5daf11SMajd Dibbiny 	default:
12321b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
12331b5daf11SMajd Dibbiny 		return -1;
12341b5daf11SMajd Dibbiny 	}
12351b5daf11SMajd Dibbiny }
12361b5daf11SMajd Dibbiny 
12371b5daf11SMajd Dibbiny enum ib_max_vl_num {
12381b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
12391b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
12401b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
12411b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
12421b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
12431b5daf11SMajd Dibbiny };
12441b5daf11SMajd Dibbiny 
12451b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
12461b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
12471b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
12481b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
12491b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
12501b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
12511b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
12521b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
12531b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
12541b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
12551b5daf11SMajd Dibbiny };
12561b5daf11SMajd Dibbiny 
12571b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
12581b5daf11SMajd Dibbiny 				u8 *max_vl_num)
12591b5daf11SMajd Dibbiny {
12601b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
12611b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
12621b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
12631b5daf11SMajd Dibbiny 		break;
12641b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
12651b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
12661b5daf11SMajd Dibbiny 		break;
12671b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
12681b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
12691b5daf11SMajd Dibbiny 		break;
12701b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
12711b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
12721b5daf11SMajd Dibbiny 		break;
12731b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
12741b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
12751b5daf11SMajd Dibbiny 		break;
12761b5daf11SMajd Dibbiny 
12771b5daf11SMajd Dibbiny 	default:
12781b5daf11SMajd Dibbiny 		return -EINVAL;
12791b5daf11SMajd Dibbiny 	}
12801b5daf11SMajd Dibbiny 
12811b5daf11SMajd Dibbiny 	return 0;
12821b5daf11SMajd Dibbiny }
12831b5daf11SMajd Dibbiny 
12841fb7f897SMark Bloch static int mlx5_query_hca_port(struct ib_device *ibdev, u32 port,
12851b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
12861b5daf11SMajd Dibbiny {
12871b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12881b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
12891b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1290046339eaSSaeed Mahameed 	u16 max_mtu;
1291046339eaSSaeed Mahameed 	u16 oper_mtu;
12921b5daf11SMajd Dibbiny 	int err;
1293639bf441SAharon Landau 	u16 ib_link_width_oper;
12941b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
12951b5daf11SMajd Dibbiny 
12961b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
12971b5daf11SMajd Dibbiny 	if (!rep) {
12981b5daf11SMajd Dibbiny 		err = -ENOMEM;
12991b5daf11SMajd Dibbiny 		goto out;
13001b5daf11SMajd Dibbiny 	}
13011b5daf11SMajd Dibbiny 
1302c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
13031b5daf11SMajd Dibbiny 
13041b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
13051b5daf11SMajd Dibbiny 	if (err)
13061b5daf11SMajd Dibbiny 		goto out;
13071b5daf11SMajd Dibbiny 
13081b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
13091b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
13101b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
13111b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
13121b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
13131b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
13141b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
13151b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
13161b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
13171b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
13181b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
13191b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
13201b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
13211b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
13221b5daf11SMajd Dibbiny 
13234106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
13244106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
13254106a758SMichael Guralnik 
1326639bf441SAharon Landau 	err = mlx5_query_ib_port_oper(mdev, &ib_link_width_oper,
1327376ceb31SAharon Landau 				      &props->active_speed, port);
13281b5daf11SMajd Dibbiny 	if (err)
13291b5daf11SMajd Dibbiny 		goto out;
13301b5daf11SMajd Dibbiny 
1331db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1332db7a691aSMichael Guralnik 
1333facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
13341b5daf11SMajd Dibbiny 
13351b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
13361b5daf11SMajd Dibbiny 
1337facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
13381b5daf11SMajd Dibbiny 
13391b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
13401b5daf11SMajd Dibbiny 
13411b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
13421b5daf11SMajd Dibbiny 	if (err)
13431b5daf11SMajd Dibbiny 		goto out;
13441b5daf11SMajd Dibbiny 
13451b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
13461b5daf11SMajd Dibbiny 				   &props->max_vl_num);
13471b5daf11SMajd Dibbiny out:
13481b5daf11SMajd Dibbiny 	kfree(rep);
1349e126ba97SEli Cohen 	return err;
1350e126ba97SEli Cohen }
1351e126ba97SEli Cohen 
13521fb7f897SMark Bloch int mlx5_ib_query_port(struct ib_device *ibdev, u32 port,
1353e126ba97SEli Cohen 		       struct ib_port_attr *props)
1354e126ba97SEli Cohen {
1355095b0927SIlan Tayari 	unsigned int count;
1356095b0927SIlan Tayari 	int ret;
1357095b0927SIlan Tayari 
13581b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13591b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1360095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1361095b0927SIlan Tayari 		break;
1362e126ba97SEli Cohen 
13631b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1364095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1365095b0927SIlan Tayari 		break;
13661b5daf11SMajd Dibbiny 
13673f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1368095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1369095b0927SIlan Tayari 		break;
13703f89a643SAchiad Shochat 
13711b5daf11SMajd Dibbiny 	default:
1372095b0927SIlan Tayari 		ret = -EINVAL;
1373e126ba97SEli Cohen 	}
1374095b0927SIlan Tayari 
1375095b0927SIlan Tayari 	if (!ret && props) {
1376b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1377b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1378b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1379b3cbd6f0SDaniel Jurgens 
1380b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1381b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1382b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1383b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1384b3cbd6f0SDaniel Jurgens 			 */
1385b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1386b3cbd6f0SDaniel Jurgens 			port = 1;
1387b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1388b3cbd6f0SDaniel Jurgens 		}
1389b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1390b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1391b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1392095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1393095b0927SIlan Tayari 	}
1394095b0927SIlan Tayari 	return ret;
1395e126ba97SEli Cohen }
1396e126ba97SEli Cohen 
13971fb7f897SMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u32 port,
13988e6efa3aSMark Bloch 				  struct ib_port_attr *props)
13998e6efa3aSMark Bloch {
14007a58779eSParav Pandit 	return mlx5_query_port_roce(ibdev, port, props);
14018e6efa3aSMark Bloch }
14028e6efa3aSMark Bloch 
14031fb7f897SMark Bloch static int mlx5_ib_rep_query_pkey(struct ib_device *ibdev, u32 port, u16 index,
1404d6fd59e1SParav Pandit 				  u16 *pkey)
1405d6fd59e1SParav Pandit {
1406d6fd59e1SParav Pandit 	/* Default special Pkey for representor device port as per the
1407d6fd59e1SParav Pandit 	 * IB specification 1.3 section 10.9.1.2.
1408d6fd59e1SParav Pandit 	 */
1409d6fd59e1SParav Pandit 	*pkey = 0xffff;
1410d6fd59e1SParav Pandit 	return 0;
1411d6fd59e1SParav Pandit }
1412d6fd59e1SParav Pandit 
14131fb7f897SMark Bloch static int mlx5_ib_query_gid(struct ib_device *ibdev, u32 port, int index,
1414e126ba97SEli Cohen 			     union ib_gid *gid)
1415e126ba97SEli Cohen {
14161b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
14171b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1418e126ba97SEli Cohen 
14191b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14201b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14211b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1422e126ba97SEli Cohen 
14231b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14241b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1425e126ba97SEli Cohen 
14261b5daf11SMajd Dibbiny 	default:
14271b5daf11SMajd Dibbiny 		return -EINVAL;
14281b5daf11SMajd Dibbiny 	}
1429e126ba97SEli Cohen 
1430e126ba97SEli Cohen }
1431e126ba97SEli Cohen 
14321fb7f897SMark Bloch static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u32 port,
1433b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1434b3cbd6f0SDaniel Jurgens {
1435b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1436b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1437b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
14381fb7f897SMark Bloch 	u32 mdev_port_num;
1439b3cbd6f0SDaniel Jurgens 	int err;
1440b3cbd6f0SDaniel Jurgens 
1441b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1442b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1443b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1444b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1445b3cbd6f0SDaniel Jurgens 		 */
1446b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1447b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1448b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1449b3cbd6f0SDaniel Jurgens 	}
1450b3cbd6f0SDaniel Jurgens 
1451b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1452b3cbd6f0SDaniel Jurgens 					index, pkey);
1453b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1454b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1455b3cbd6f0SDaniel Jurgens 
1456b3cbd6f0SDaniel Jurgens 	return err;
1457b3cbd6f0SDaniel Jurgens }
1458b3cbd6f0SDaniel Jurgens 
14591fb7f897SMark Bloch static int mlx5_ib_query_pkey(struct ib_device *ibdev, u32 port, u16 index,
1460e126ba97SEli Cohen 			      u16 *pkey)
1461e126ba97SEli Cohen {
14621b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14631b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14641b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1465e126ba97SEli Cohen 
14661b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14671b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1468b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
14691b5daf11SMajd Dibbiny 	default:
14701b5daf11SMajd Dibbiny 		return -EINVAL;
1471e126ba97SEli Cohen 	}
14721b5daf11SMajd Dibbiny }
1473e126ba97SEli Cohen 
1474e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1475e126ba97SEli Cohen 				 struct ib_device_modify *props)
1476e126ba97SEli Cohen {
1477e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1478e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1479e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1480e126ba97SEli Cohen 	int err;
1481e126ba97SEli Cohen 
1482e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1483e126ba97SEli Cohen 		return -EOPNOTSUPP;
1484e126ba97SEli Cohen 
1485e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1486e126ba97SEli Cohen 		return 0;
1487e126ba97SEli Cohen 
1488e126ba97SEli Cohen 	/*
1489e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1490e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1491e126ba97SEli Cohen 	 */
1492bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
14939603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1494e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1495e126ba97SEli Cohen 	if (err)
1496e126ba97SEli Cohen 		return err;
1497e126ba97SEli Cohen 
1498bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1499e126ba97SEli Cohen 
1500e126ba97SEli Cohen 	return err;
1501e126ba97SEli Cohen }
1502e126ba97SEli Cohen 
15031fb7f897SMark Bloch static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u32 port_num, u32 mask,
1504cdbe33d0SEli Cohen 				u32 value)
1505cdbe33d0SEli Cohen {
1506cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1507b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
15081fb7f897SMark Bloch 	u32 mdev_port_num;
1509cdbe33d0SEli Cohen 	int err;
1510cdbe33d0SEli Cohen 
1511b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1512b3cbd6f0SDaniel Jurgens 	if (!mdev)
1513b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1514b3cbd6f0SDaniel Jurgens 
1515b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1516cdbe33d0SEli Cohen 	if (err)
1517b3cbd6f0SDaniel Jurgens 		goto out;
1518cdbe33d0SEli Cohen 
1519cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1520cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1521cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1522b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1523b3cbd6f0SDaniel Jurgens 		goto out;
1524cdbe33d0SEli Cohen 	}
1525cdbe33d0SEli Cohen 
1526cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1527cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1528b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1529b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1530b3cbd6f0SDaniel Jurgens 
1531b3cbd6f0SDaniel Jurgens out:
1532b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1533cdbe33d0SEli Cohen 
1534cdbe33d0SEli Cohen 	return err;
1535cdbe33d0SEli Cohen }
1536cdbe33d0SEli Cohen 
15371fb7f897SMark Bloch static int mlx5_ib_modify_port(struct ib_device *ibdev, u32 port, int mask,
1538e126ba97SEli Cohen 			       struct ib_port_modify *props)
1539e126ba97SEli Cohen {
1540e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1541e126ba97SEli Cohen 	struct ib_port_attr attr;
1542e126ba97SEli Cohen 	u32 tmp;
1543e126ba97SEli Cohen 	int err;
1544cdbe33d0SEli Cohen 	u32 change_mask;
1545cdbe33d0SEli Cohen 	u32 value;
1546cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1547cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1548cdbe33d0SEli Cohen 
1549ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1550ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1551ec255879SMajd Dibbiny 	 */
1552ec255879SMajd Dibbiny 	if (!is_ib)
1553ec255879SMajd Dibbiny 		return 0;
1554ec255879SMajd Dibbiny 
1555cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1556cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1557cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1558cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1559cdbe33d0SEli Cohen 	}
1560e126ba97SEli Cohen 
1561e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1562e126ba97SEli Cohen 
1563c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1564e126ba97SEli Cohen 	if (err)
1565e126ba97SEli Cohen 		goto out;
1566e126ba97SEli Cohen 
1567e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1568e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1569e126ba97SEli Cohen 
15709603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1571e126ba97SEli Cohen 
1572e126ba97SEli Cohen out:
1573e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1574e126ba97SEli Cohen 	return err;
1575e126ba97SEli Cohen }
1576e126ba97SEli Cohen 
157730aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
157830aa60b3SEli Cohen {
157930aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
158030aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
158130aa60b3SEli Cohen }
158230aa60b3SEli Cohen 
158331a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
158431a78a5aSYishai Hadas {
158531a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
158631a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
158731a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
158831a78a5aSYishai Hadas 
158931a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
159031a78a5aSYishai Hadas }
159131a78a5aSYishai Hadas 
1592b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1593b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
159431a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1595b037c29aSEli Cohen {
1596b037c29aSEli Cohen 	int uars_per_sys_page;
1597b037c29aSEli Cohen 	int bfregs_per_sys_page;
1598b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1599b037c29aSEli Cohen 
1600b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1601b037c29aSEli Cohen 		return -EINVAL;
1602b037c29aSEli Cohen 
1603b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1604b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1605b037c29aSEli Cohen 
1606b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1607b037c29aSEli Cohen 		return -ENOMEM;
1608b037c29aSEli Cohen 
1609b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1610b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
161131a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1612b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1613b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1614b037c29aSEli Cohen 		return -EINVAL;
1615b037c29aSEli Cohen 
161631a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
161731a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
161831a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
161931a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
162031a78a5aSYishai Hadas 
162131a78a5aSYishai 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",
1622b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1623b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
162431a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
162531a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1626b037c29aSEli Cohen 
1627b037c29aSEli Cohen 	return 0;
1628b037c29aSEli Cohen }
1629b037c29aSEli Cohen 
1630b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1631b037c29aSEli Cohen {
1632b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1633b037c29aSEli Cohen 	int err;
1634b037c29aSEli Cohen 	int i;
1635b037c29aSEli Cohen 
1636b037c29aSEli Cohen 	bfregi = &context->bfregi;
163731a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1638d2c8a155SMeir Lichtinger 		err = mlx5_cmd_uar_alloc(dev->mdev, &bfregi->sys_pages[i],
1639d2c8a155SMeir Lichtinger 					 context->devx_uid);
1640b037c29aSEli Cohen 		if (err)
1641b037c29aSEli Cohen 			goto error;
1642b037c29aSEli Cohen 
1643b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1644b037c29aSEli Cohen 	}
16454ed131d0SYishai Hadas 
16464ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
16474ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
16484ed131d0SYishai Hadas 
1649b037c29aSEli Cohen 	return 0;
1650b037c29aSEli Cohen 
1651b037c29aSEli Cohen error:
1652b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1653d2c8a155SMeir Lichtinger 		if (mlx5_cmd_uar_dealloc(dev->mdev, bfregi->sys_pages[i],
1654d2c8a155SMeir Lichtinger 					 context->devx_uid))
1655b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1656b037c29aSEli Cohen 
1657b037c29aSEli Cohen 	return err;
1658b037c29aSEli Cohen }
1659b037c29aSEli Cohen 
166015177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
166115177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1662b037c29aSEli Cohen {
1663b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1664b037c29aSEli Cohen 	int i;
1665b037c29aSEli Cohen 
1666b037c29aSEli Cohen 	bfregi = &context->bfregi;
166715177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
16684ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
166915177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
1670d2c8a155SMeir Lichtinger 			mlx5_cmd_uar_dealloc(dev->mdev, bfregi->sys_pages[i],
1671d2c8a155SMeir Lichtinger 					     context->devx_uid);
1672b037c29aSEli Cohen }
1673b037c29aSEli Cohen 
16740042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1675a560f1d9SMark Bloch {
1676a560f1d9SMark Bloch 	int err = 0;
1677a560f1d9SMark Bloch 
1678a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16790042f9e4SMark Bloch 	if (td)
1680a560f1d9SMark Bloch 		dev->lb.user_td++;
16810042f9e4SMark Bloch 	if (qp)
16820042f9e4SMark Bloch 		dev->lb.qps++;
1683a560f1d9SMark Bloch 
16840042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
16850042f9e4SMark Bloch 	    dev->lb.qps == 1) {
16860042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1687a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
16880042f9e4SMark Bloch 			dev->lb.enabled = true;
16890042f9e4SMark Bloch 		}
16900042f9e4SMark Bloch 	}
1691a560f1d9SMark Bloch 
1692a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1693a560f1d9SMark Bloch 
1694a560f1d9SMark Bloch 	return err;
1695a560f1d9SMark Bloch }
1696a560f1d9SMark Bloch 
16970042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1698a560f1d9SMark Bloch {
1699a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
17000042f9e4SMark Bloch 	if (td)
1701a560f1d9SMark Bloch 		dev->lb.user_td--;
17020042f9e4SMark Bloch 	if (qp)
17030042f9e4SMark Bloch 		dev->lb.qps--;
1704a560f1d9SMark Bloch 
17050042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
17060042f9e4SMark Bloch 	    dev->lb.qps == 0) {
17070042f9e4SMark Bloch 		if (dev->lb.enabled) {
1708a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
17090042f9e4SMark Bloch 			dev->lb.enabled = false;
17100042f9e4SMark Bloch 		}
17110042f9e4SMark Bloch 	}
1712a560f1d9SMark Bloch 
1713a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1714a560f1d9SMark Bloch }
1715a560f1d9SMark Bloch 
1716d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1717d2d19121SYishai Hadas 					  u16 uid)
1718c85023e1SHuy Nguyen {
1719c85023e1SHuy Nguyen 	int err;
1720c85023e1SHuy Nguyen 
1721cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1722cfdeb893SLeon Romanovsky 		return 0;
1723cfdeb893SLeon Romanovsky 
1724d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1725c85023e1SHuy Nguyen 	if (err)
1726c85023e1SHuy Nguyen 		return err;
1727c85023e1SHuy Nguyen 
1728c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17298978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17308978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1731c85023e1SHuy Nguyen 		return err;
1732c85023e1SHuy Nguyen 
17330042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1734c85023e1SHuy Nguyen }
1735c85023e1SHuy Nguyen 
1736d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1737d2d19121SYishai Hadas 					     u16 uid)
1738c85023e1SHuy Nguyen {
1739cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1740cfdeb893SLeon Romanovsky 		return;
1741cfdeb893SLeon Romanovsky 
1742d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1743c85023e1SHuy Nguyen 
1744c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17458978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17468978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1747c85023e1SHuy Nguyen 		return;
1748c85023e1SHuy Nguyen 
17490042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1750c85023e1SHuy Nguyen }
1751c85023e1SHuy Nguyen 
175245ec21c9SYishai Hadas static int set_ucontext_resp(struct ib_ucontext *uctx,
175345ec21c9SYishai Hadas 			     struct mlx5_ib_alloc_ucontext_resp *resp)
175445ec21c9SYishai Hadas {
175545ec21c9SYishai Hadas 	struct ib_device *ibdev = uctx->device;
175645ec21c9SYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
175745ec21c9SYishai Hadas 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
175845ec21c9SYishai Hadas 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
175945ec21c9SYishai Hadas 	int err;
176045ec21c9SYishai Hadas 
176145ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
176245ec21c9SYishai Hadas 		err = mlx5_cmd_dump_fill_mkey(dev->mdev,
176345ec21c9SYishai Hadas 					      &resp->dump_fill_mkey);
176445ec21c9SYishai Hadas 		if (err)
176545ec21c9SYishai Hadas 			return err;
176645ec21c9SYishai Hadas 		resp->comp_mask |=
176745ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
176845ec21c9SYishai Hadas 	}
176945ec21c9SYishai Hadas 
177045ec21c9SYishai Hadas 	resp->qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
177145ec21c9SYishai Hadas 	if (dev->wc_support)
177245ec21c9SYishai Hadas 		resp->bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev,
177345ec21c9SYishai Hadas 						      log_bf_reg_size);
177445ec21c9SYishai Hadas 	resp->cache_line_size = cache_line_size();
177545ec21c9SYishai Hadas 	resp->max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
177645ec21c9SYishai Hadas 	resp->max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
177745ec21c9SYishai Hadas 	resp->max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
177845ec21c9SYishai Hadas 	resp->max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
177945ec21c9SYishai Hadas 	resp->max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
178045ec21c9SYishai Hadas 	resp->cqe_version = context->cqe_version;
178145ec21c9SYishai Hadas 	resp->log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
178245ec21c9SYishai Hadas 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
178345ec21c9SYishai Hadas 	resp->num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
178445ec21c9SYishai Hadas 					MLX5_CAP_GEN(dev->mdev,
178545ec21c9SYishai Hadas 						     num_of_uars_per_page) : 1;
178645ec21c9SYishai Hadas 	resp->tot_bfregs = bfregi->lib_uar_dyn ? 0 :
178745ec21c9SYishai Hadas 			bfregi->total_num_bfregs - bfregi->num_dyn_bfregs;
178845ec21c9SYishai Hadas 	resp->num_ports = dev->num_ports;
178945ec21c9SYishai Hadas 	resp->cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
179045ec21c9SYishai Hadas 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
179145ec21c9SYishai Hadas 
179245ec21c9SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
179345ec21c9SYishai Hadas 		mlx5_query_min_inline(dev->mdev, &resp->eth_min_inline);
179445ec21c9SYishai Hadas 		resp->eth_min_inline++;
179545ec21c9SYishai Hadas 	}
179645ec21c9SYishai Hadas 
179745ec21c9SYishai Hadas 	if (dev->mdev->clock_info)
179845ec21c9SYishai Hadas 		resp->clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
179945ec21c9SYishai Hadas 
180045ec21c9SYishai Hadas 	/*
180145ec21c9SYishai Hadas 	 * We don't want to expose information from the PCI bar that is located
180245ec21c9SYishai Hadas 	 * after 4096 bytes, so if the arch only supports larger pages, let's
180345ec21c9SYishai Hadas 	 * pretend we don't support reading the HCA's core clock. This is also
180445ec21c9SYishai Hadas 	 * forced by mmap function.
180545ec21c9SYishai Hadas 	 */
180645ec21c9SYishai Hadas 	if (PAGE_SIZE <= 4096) {
180745ec21c9SYishai Hadas 		resp->comp_mask |=
180845ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
180945ec21c9SYishai Hadas 		resp->hca_core_clock_offset =
181045ec21c9SYishai Hadas 			offsetof(struct mlx5_init_seg,
181145ec21c9SYishai Hadas 				 internal_timer_h) % PAGE_SIZE;
181245ec21c9SYishai Hadas 	}
181345ec21c9SYishai Hadas 
181445ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, ece_support))
181545ec21c9SYishai Hadas 		resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_ECE;
181645ec21c9SYishai Hadas 
181733652951SAharon Landau 	if (rt_supported(MLX5_CAP_GEN(dev->mdev, sq_ts_format)) &&
181833652951SAharon Landau 	    rt_supported(MLX5_CAP_GEN(dev->mdev, rq_ts_format)) &&
181933652951SAharon Landau 	    rt_supported(MLX5_CAP_ROCE(dev->mdev, qp_ts_format)))
182033652951SAharon Landau 		resp->comp_mask |=
182133652951SAharon Landau 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_REAL_TIME_TS;
182233652951SAharon Landau 
182345ec21c9SYishai Hadas 	resp->num_dyn_bfregs = bfregi->num_dyn_bfregs;
1824c906b86eSSergey Gorenko 
1825c906b86eSSergey Gorenko 	if (MLX5_CAP_GEN(dev->mdev, drain_sigerr))
1826c906b86eSSergey Gorenko 		resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_SQD2RTS;
1827c906b86eSSergey Gorenko 
182813ad1125SAharon Landau 	resp->comp_mask |=
182913ad1125SAharon Landau 		MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_MKEY_UPDATE_TAG;
183013ad1125SAharon Landau 
183145ec21c9SYishai Hadas 	return 0;
183245ec21c9SYishai Hadas }
183345ec21c9SYishai Hadas 
1834a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1835e126ba97SEli Cohen 				  struct ib_udata *udata)
1836e126ba97SEli Cohen {
1837a2a074efSLeon Romanovsky 	struct ib_device *ibdev = uctx->device;
1838e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1839b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1840b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1841a2a074efSLeon Romanovsky 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
18422f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
184378c0f98cSEli Cohen 	int ver;
1844e126ba97SEli Cohen 	int err;
1845a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1846a168a41cSMajd Dibbiny 				     max_cqe_version);
1847b037c29aSEli Cohen 	bool lib_uar_4k;
18480a2fd01cSYishai Hadas 	bool lib_uar_dyn;
1849e126ba97SEli Cohen 
1850e126ba97SEli Cohen 	if (!dev->ib_active)
1851a2a074efSLeon Romanovsky 		return -EAGAIN;
1852e126ba97SEli Cohen 
1853e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
185478c0f98cSEli Cohen 		ver = 0;
1855e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
185678c0f98cSEli Cohen 		ver = 2;
185778c0f98cSEli Cohen 	else
1858a2a074efSLeon Romanovsky 		return -EINVAL;
185978c0f98cSEli Cohen 
1860e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1861e126ba97SEli Cohen 	if (err)
1862a2a074efSLeon Romanovsky 		return err;
1863e126ba97SEli Cohen 
1864a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1865a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
186678c0f98cSEli Cohen 
1867f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1868a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
1869b368d7cbSMatan Barak 
18702f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
18712f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
18722f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1873a2a074efSLeon Romanovsky 		return -EINVAL;
1874e126ba97SEli Cohen 
1875d2c8a155SMeir Lichtinger 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1876d2c8a155SMeir Lichtinger 		err = mlx5_ib_devx_create(dev, true);
1877d2c8a155SMeir Lichtinger 		if (err < 0)
1878d2c8a155SMeir Lichtinger 			goto out_ctx;
1879d2c8a155SMeir Lichtinger 		context->devx_uid = err;
1880d2c8a155SMeir Lichtinger 	}
1881d2c8a155SMeir Lichtinger 
188230aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
18830a2fd01cSYishai Hadas 	lib_uar_dyn = req.lib_caps & MLX5_LIB_CAP_DYN_UAR;
18842f5ff264SEli Cohen 	bfregi = &context->bfregi;
1885b037c29aSEli Cohen 
18860a2fd01cSYishai Hadas 	if (lib_uar_dyn) {
18870a2fd01cSYishai Hadas 		bfregi->lib_uar_dyn = lib_uar_dyn;
18880a2fd01cSYishai Hadas 		goto uar_done;
18890a2fd01cSYishai Hadas 	}
18900a2fd01cSYishai Hadas 
1891b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
189231a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1893b037c29aSEli Cohen 	if (err)
1894d2c8a155SMeir Lichtinger 		goto out_devx;
1895b037c29aSEli Cohen 
18962f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1897b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
189831a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1899b037c29aSEli Cohen 				GFP_KERNEL);
1900b037c29aSEli Cohen 	if (!bfregi->count) {
1901e126ba97SEli Cohen 		err = -ENOMEM;
1902d2c8a155SMeir Lichtinger 		goto out_devx;
1903e126ba97SEli Cohen 	}
1904e126ba97SEli Cohen 
1905b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1906b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1907e126ba97SEli Cohen 				    GFP_KERNEL);
1908b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1909e126ba97SEli Cohen 		err = -ENOMEM;
1910e126ba97SEli Cohen 		goto out_count;
1911e126ba97SEli Cohen 	}
1912e126ba97SEli Cohen 
1913b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1914b037c29aSEli Cohen 	if (err)
1915b037c29aSEli Cohen 		goto out_sys_pages;
1916b037c29aSEli Cohen 
19170a2fd01cSYishai Hadas uar_done:
1918d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1919d2d19121SYishai Hadas 					     context->devx_uid);
1920a8b92ca1SYishai Hadas 	if (err)
1921d2c8a155SMeir Lichtinger 		goto out_uars;
1922a8b92ca1SYishai Hadas 
1923e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1924e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1925e126ba97SEli Cohen 
192645ec21c9SYishai Hadas 	context->cqe_version = min_t(__u8,
192745ec21c9SYishai Hadas 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
192845ec21c9SYishai Hadas 				 req.max_cqe_version);
1929b368d7cbSMatan Barak 
193045ec21c9SYishai Hadas 	err = set_ucontext_resp(uctx, &resp);
193145ec21c9SYishai Hadas 	if (err)
193245ec21c9SYishai Hadas 		goto out_mdev;
1933b368d7cbSMatan Barak 
193445ec21c9SYishai Hadas 	resp.response_length = min(udata->outlen, sizeof(resp));
1935b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1936e126ba97SEli Cohen 	if (err)
1937a8b92ca1SYishai Hadas 		goto out_mdev;
1938e126ba97SEli Cohen 
19392f5ff264SEli Cohen 	bfregi->ver = ver;
19402f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
194130aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
194230aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1943f72300c5SHaggai Abramovsky 
1944802dcc7fSMark Zhang 	if (mlx5_ib_lag_should_assign_affinity(dev)) {
19451fb7f897SMark Bloch 		u32 port = mlx5_core_native_port_num(dev->mdev) - 1;
1946c6a21c38SMajd Dibbiny 
1947c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1948c6a21c38SMajd Dibbiny 			   atomic_add_return(
194995579e78SMark Bloch 				   1, &dev->port[port].roce.tx_port_affinity));
1950c6a21c38SMajd Dibbiny 	}
1951c6a21c38SMajd Dibbiny 
1952a2a074efSLeon Romanovsky 	return 0;
1953e126ba97SEli Cohen 
1954a8b92ca1SYishai Hadas out_mdev:
1955d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1956146d2f1aSmajd@mellanox.com 
1957e126ba97SEli Cohen out_uars:
1958b037c29aSEli Cohen 	deallocate_uars(dev, context);
1959b037c29aSEli Cohen 
1960b037c29aSEli Cohen out_sys_pages:
1961b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1962b037c29aSEli Cohen 
1963e126ba97SEli Cohen out_count:
19642f5ff264SEli Cohen 	kfree(bfregi->count);
1965e126ba97SEli Cohen 
1966d2c8a155SMeir Lichtinger out_devx:
1967d2c8a155SMeir Lichtinger 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
1968d2c8a155SMeir Lichtinger 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1969d2c8a155SMeir Lichtinger 
1970e126ba97SEli Cohen out_ctx:
1971a2a074efSLeon Romanovsky 	return err;
1972e126ba97SEli Cohen }
1973e126ba97SEli Cohen 
19740fb556b2SYishai Hadas static int mlx5_ib_query_ucontext(struct ib_ucontext *ibcontext,
19750fb556b2SYishai Hadas 				  struct uverbs_attr_bundle *attrs)
19760fb556b2SYishai Hadas {
19770fb556b2SYishai Hadas 	struct mlx5_ib_alloc_ucontext_resp uctx_resp = {};
19780fb556b2SYishai Hadas 	int ret;
19790fb556b2SYishai Hadas 
19800fb556b2SYishai Hadas 	ret = set_ucontext_resp(ibcontext, &uctx_resp);
19810fb556b2SYishai Hadas 	if (ret)
19820fb556b2SYishai Hadas 		return ret;
19830fb556b2SYishai Hadas 
19840fb556b2SYishai Hadas 	uctx_resp.response_length =
19850fb556b2SYishai Hadas 		min_t(size_t,
19860fb556b2SYishai Hadas 		      uverbs_attr_get_len(attrs,
19870fb556b2SYishai Hadas 				MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX),
19880fb556b2SYishai Hadas 		      sizeof(uctx_resp));
19890fb556b2SYishai Hadas 
19900fb556b2SYishai Hadas 	ret = uverbs_copy_to_struct_or_zero(attrs,
19910fb556b2SYishai Hadas 					MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
19920fb556b2SYishai Hadas 					&uctx_resp,
19930fb556b2SYishai Hadas 					sizeof(uctx_resp));
19940fb556b2SYishai Hadas 	return ret;
19950fb556b2SYishai Hadas }
19960fb556b2SYishai Hadas 
1997a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1998e126ba97SEli Cohen {
1999e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2000e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
2001b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
2002e126ba97SEli Cohen 
2003b037c29aSEli Cohen 	bfregi = &context->bfregi;
2004d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2005d2d19121SYishai Hadas 
2006b037c29aSEli Cohen 	deallocate_uars(dev, context);
2007b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
20082f5ff264SEli Cohen 	kfree(bfregi->count);
2009d2c8a155SMeir Lichtinger 
2010d2c8a155SMeir Lichtinger 	if (context->devx_uid)
2011d2c8a155SMeir Lichtinger 		mlx5_ib_devx_destroy(dev, context->devx_uid);
2012e126ba97SEli Cohen }
2013e126ba97SEli Cohen 
2014b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
20154ed131d0SYishai Hadas 				 int uar_idx)
2016e126ba97SEli Cohen {
2017b037c29aSEli Cohen 	int fw_uars_per_page;
2018b037c29aSEli Cohen 
2019b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2020b037c29aSEli Cohen 
2021aa8106f1SHuy Nguyen 	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2022e126ba97SEli Cohen }
2023e126ba97SEli Cohen 
2024342ee59dSYishai Hadas static u64 uar_index2paddress(struct mlx5_ib_dev *dev,
2025342ee59dSYishai Hadas 				 int uar_idx)
2026342ee59dSYishai Hadas {
2027342ee59dSYishai Hadas 	unsigned int fw_uars_per_page;
2028342ee59dSYishai Hadas 
2029342ee59dSYishai Hadas 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
2030342ee59dSYishai Hadas 				MLX5_UARS_IN_PAGE : 1;
2031342ee59dSYishai Hadas 
2032342ee59dSYishai Hadas 	return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE);
2033342ee59dSYishai Hadas }
2034342ee59dSYishai Hadas 
2035e126ba97SEli Cohen static int get_command(unsigned long offset)
2036e126ba97SEli Cohen {
2037e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2038e126ba97SEli Cohen }
2039e126ba97SEli Cohen 
2040e126ba97SEli Cohen static int get_arg(unsigned long offset)
2041e126ba97SEli Cohen {
2042e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2043e126ba97SEli Cohen }
2044e126ba97SEli Cohen 
2045e126ba97SEli Cohen static int get_index(unsigned long offset)
2046e126ba97SEli Cohen {
2047e126ba97SEli Cohen 	return get_arg(offset);
2048e126ba97SEli Cohen }
2049e126ba97SEli Cohen 
20504ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
20514ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20524ed131d0SYishai Hadas {
20534ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20544ed131d0SYishai Hadas }
20554ed131d0SYishai Hadas 
20567c2344c3SMaor Gottlieb 
20577c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20587c2344c3SMaor Gottlieb {
20597c2344c3SMaor Gottlieb }
20607c2344c3SMaor Gottlieb 
206137aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2062e126ba97SEli Cohen {
206337aa5c36SGuy Levi 	switch (cmd) {
206437aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
206537aa5c36SGuy Levi 		return "WC";
2066e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
206737aa5c36SGuy Levi 		return "best effort WC";
206837aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
206937aa5c36SGuy Levi 		return "NC";
207024da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
207124da0016SAriel Levkovich 		return "Device Memory";
207237aa5c36SGuy Levi 	default:
207337aa5c36SGuy Levi 		return NULL;
207437aa5c36SGuy Levi 	}
207537aa5c36SGuy Levi }
207637aa5c36SGuy Levi 
20775c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20785c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20795c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20805c99eaecSFeras Daoud {
20814eb6ab13SJason Gunthorpe 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
20824eb6ab13SJason Gunthorpe 	    !(vma->vm_flags & VM_SHARED))
20835c99eaecSFeras Daoud 		return -EINVAL;
20845c99eaecSFeras Daoud 
20855c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20865c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20875c99eaecSFeras Daoud 
20884eb6ab13SJason Gunthorpe 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
20895c99eaecSFeras Daoud 		return -EPERM;
2090c660133cSJason Gunthorpe 	vma->vm_flags &= ~VM_MAYWRITE;
20915c99eaecSFeras Daoud 
2092ddcdc368SJason Gunthorpe 	if (!dev->mdev->clock_info)
20935c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20945c99eaecSFeras Daoud 
20954eb6ab13SJason Gunthorpe 	return vm_insert_page(vma, vma->vm_start,
20964eb6ab13SJason Gunthorpe 			      virt_to_page(dev->mdev->clock_info));
20975c99eaecSFeras Daoud }
20985c99eaecSFeras Daoud 
2099dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
2100dc2316ebSYishai Hadas {
2101dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
2102dc2316ebSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device);
21037be76befSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
2104d2c8a155SMeir Lichtinger 	struct mlx5_ib_ucontext *context = to_mucontext(entry->ucontext);
2105dc2316ebSYishai Hadas 
2106dc2316ebSYishai Hadas 	switch (mentry->mmap_flag) {
2107dc2316ebSYishai Hadas 	case MLX5_IB_MMAP_TYPE_MEMIC:
2108cea85fa5SMaor Gottlieb 	case MLX5_IB_MMAP_TYPE_MEMIC_OP:
2109cea85fa5SMaor Gottlieb 		mlx5_ib_dm_mmap_free(dev, mentry);
2110dc2316ebSYishai Hadas 		break;
21117be76befSYishai Hadas 	case MLX5_IB_MMAP_TYPE_VAR:
21127be76befSYishai Hadas 		mutex_lock(&var_table->bitmap_lock);
21137be76befSYishai Hadas 		clear_bit(mentry->page_idx, var_table->bitmap);
21147be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
21157be76befSYishai Hadas 		kfree(mentry);
21167be76befSYishai Hadas 		break;
2117342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_WC:
2118342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_NC:
2119d2c8a155SMeir Lichtinger 		mlx5_cmd_uar_dealloc(dev->mdev, mentry->page_idx,
2120d2c8a155SMeir Lichtinger 				     context->devx_uid);
2121342ee59dSYishai Hadas 		kfree(mentry);
2122342ee59dSYishai Hadas 		break;
2123dc2316ebSYishai Hadas 	default:
2124dc2316ebSYishai Hadas 		WARN_ON(true);
2125dc2316ebSYishai Hadas 	}
2126dc2316ebSYishai Hadas }
2127dc2316ebSYishai Hadas 
212837aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
21297c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
21307c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
213137aa5c36SGuy Levi {
21322f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
213337aa5c36SGuy Levi 	int err;
213437aa5c36SGuy Levi 	unsigned long idx;
2135aa09ea6eSKamal Heib 	phys_addr_t pfn;
213637aa5c36SGuy Levi 	pgprot_t prot;
21374ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
21384ed131d0SYishai Hadas 	u32 uar_index;
21394ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
21404ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
21414ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2142b037c29aSEli Cohen 
21430a2fd01cSYishai Hadas 	if (bfregi->lib_uar_dyn)
21440a2fd01cSYishai Hadas 		return -EINVAL;
21450a2fd01cSYishai Hadas 
2146b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2147b037c29aSEli Cohen 		return -EINVAL;
2148b037c29aSEli Cohen 
21494ed131d0SYishai Hadas 	if (dyn_uar)
21504ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
21514ed131d0SYishai Hadas 	else
2152b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
21534ed131d0SYishai Hadas 
21544ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21554ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21564ed131d0SYishai Hadas 			     idx, max_valid_idx);
2157b037c29aSEli Cohen 		return -EINVAL;
2158b037c29aSEli Cohen 	}
215937aa5c36SGuy Levi 
216037aa5c36SGuy Levi 	switch (cmd) {
216137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21624ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
216337aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
216437aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
216537aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
216637aa5c36SGuy Levi 		break;
216737aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
216837aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
216937aa5c36SGuy Levi 		break;
217037aa5c36SGuy Levi 	default:
217137aa5c36SGuy Levi 		return -EINVAL;
217237aa5c36SGuy Levi 	}
217337aa5c36SGuy Levi 
21744ed131d0SYishai Hadas 	if (dyn_uar) {
21754ed131d0SYishai Hadas 		int uars_per_page;
21764ed131d0SYishai Hadas 
21774ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21784ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21794ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21804ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21814ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21824ed131d0SYishai Hadas 			return -EINVAL;
21834ed131d0SYishai Hadas 		}
21844ed131d0SYishai Hadas 
21854ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21864ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21874ed131d0SYishai Hadas 		 * page holds its count.
21884ed131d0SYishai Hadas 		 */
21894ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
21904ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
21914ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
21924ed131d0SYishai Hadas 			return -EINVAL;
21934ed131d0SYishai Hadas 		}
21944ed131d0SYishai Hadas 
21954ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
21964ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
21974ed131d0SYishai Hadas 
2198d2c8a155SMeir Lichtinger 		err = mlx5_cmd_uar_alloc(dev->mdev, &uar_index,
2199d2c8a155SMeir Lichtinger 					 context->devx_uid);
22004ed131d0SYishai Hadas 		if (err) {
22014ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
22024ed131d0SYishai Hadas 			goto free_bfreg;
22034ed131d0SYishai Hadas 		}
22044ed131d0SYishai Hadas 	} else {
22054ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
22064ed131d0SYishai Hadas 	}
22074ed131d0SYishai Hadas 
22084ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
220937aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2210e126ba97SEli Cohen 
2211e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2212c043ff2cSMichal Kalderon 				prot, NULL);
221337aa5c36SGuy Levi 	if (err) {
22148f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2215e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
22168f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
22174ed131d0SYishai Hadas 		goto err;
221837aa5c36SGuy Levi 	}
2219e126ba97SEli Cohen 
22204ed131d0SYishai Hadas 	if (dyn_uar)
22214ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
22224ed131d0SYishai Hadas 	return 0;
22234ed131d0SYishai Hadas 
22244ed131d0SYishai Hadas err:
22254ed131d0SYishai Hadas 	if (!dyn_uar)
22264ed131d0SYishai Hadas 		return err;
22274ed131d0SYishai Hadas 
2228d2c8a155SMeir Lichtinger 	mlx5_cmd_uar_dealloc(dev->mdev, idx, context->devx_uid);
22294ed131d0SYishai Hadas 
22304ed131d0SYishai Hadas free_bfreg:
22314ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
22324ed131d0SYishai Hadas 
22334ed131d0SYishai Hadas 	return err;
223437aa5c36SGuy Levi }
223537aa5c36SGuy Levi 
2236dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma)
2237dc2316ebSYishai Hadas {
2238dc2316ebSYishai Hadas 	unsigned long idx;
2239dc2316ebSYishai Hadas 	u8 command;
2240dc2316ebSYishai Hadas 
2241dc2316ebSYishai Hadas 	command = get_command(vma->vm_pgoff);
2242dc2316ebSYishai Hadas 	idx = get_extended_index(vma->vm_pgoff);
2243dc2316ebSYishai Hadas 
2244dc2316ebSYishai Hadas 	return (command << 16 | idx);
2245dc2316ebSYishai Hadas }
2246dc2316ebSYishai Hadas 
2247dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev,
2248dc2316ebSYishai Hadas 			       struct vm_area_struct *vma,
2249dc2316ebSYishai Hadas 			       struct ib_ucontext *ucontext)
2250dc2316ebSYishai Hadas {
2251dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry;
2252dc2316ebSYishai Hadas 	struct rdma_user_mmap_entry *entry;
2253dc2316ebSYishai Hadas 	unsigned long pgoff;
2254dc2316ebSYishai Hadas 	pgprot_t prot;
2255dc2316ebSYishai Hadas 	phys_addr_t pfn;
2256dc2316ebSYishai Hadas 	int ret;
2257dc2316ebSYishai Hadas 
2258dc2316ebSYishai Hadas 	pgoff = mlx5_vma_to_pgoff(vma);
2259dc2316ebSYishai Hadas 	entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff);
2260dc2316ebSYishai Hadas 	if (!entry)
226124da0016SAriel Levkovich 		return -EINVAL;
226224da0016SAriel Levkovich 
2263dc2316ebSYishai Hadas 	mentry = to_mmmap(entry);
2264dc2316ebSYishai Hadas 	pfn = (mentry->address >> PAGE_SHIFT);
2265342ee59dSYishai Hadas 	if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR ||
2266342ee59dSYishai Hadas 	    mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC)
22673f59b6c3SYishai Hadas 		prot = pgprot_noncached(vma->vm_page_prot);
22683f59b6c3SYishai Hadas 	else
2269dc2316ebSYishai Hadas 		prot = pgprot_writecombine(vma->vm_page_prot);
2270dc2316ebSYishai Hadas 	ret = rdma_user_mmap_io(ucontext, vma, pfn,
2271dc2316ebSYishai Hadas 				entry->npages * PAGE_SIZE,
2272dc2316ebSYishai Hadas 				prot,
2273dc2316ebSYishai Hadas 				entry);
2274dc2316ebSYishai Hadas 	rdma_user_mmap_entry_put(&mentry->rdma_entry);
2275dc2316ebSYishai Hadas 	return ret;
227624da0016SAriel Levkovich }
227724da0016SAriel Levkovich 
22787be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry)
22797be76befSYishai Hadas {
22809b6d3bbcSLeon Romanovsky 	u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF;
22819b6d3bbcSLeon Romanovsky 	u64 index = entry->rdma_entry.start_pgoff & 0xFFFF;
22827be76befSYishai Hadas 
22837be76befSYishai Hadas 	return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) |
22847be76befSYishai Hadas 		(index & 0xFF)) << PAGE_SHIFT;
22857be76befSYishai Hadas }
22867be76befSYishai Hadas 
228737aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
228837aa5c36SGuy Levi {
228937aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
229037aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
229137aa5c36SGuy Levi 	unsigned long command;
229237aa5c36SGuy Levi 	phys_addr_t pfn;
229337aa5c36SGuy Levi 
229437aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
229537aa5c36SGuy Levi 	switch (command) {
229637aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
22971f3db161SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
22981f3db161SYishai Hadas 		if (!dev->wc_support)
22991f3db161SYishai Hadas 			return -EPERM;
23001f3db161SYishai Hadas 		fallthrough;
230137aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
230237aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
23037c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2304e126ba97SEli Cohen 
2305e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2306e126ba97SEli Cohen 		return -ENOSYS;
2307e126ba97SEli Cohen 
2308d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2309d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2310d69e3bcfSMatan Barak 			return -EINVAL;
2311d69e3bcfSMatan Barak 
23126cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2313d69e3bcfSMatan Barak 			return -EPERM;
2314c660133cSJason Gunthorpe 		vma->vm_flags &= ~VM_MAYWRITE;
2315d69e3bcfSMatan Barak 
2316d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2317d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2318d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2319d69e3bcfSMatan Barak 
2320d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2321d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2322d69e3bcfSMatan Barak 			PAGE_SHIFT;
2323d5e560d3SJason Gunthorpe 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2324d5e560d3SJason Gunthorpe 					 PAGE_SIZE,
2325c043ff2cSMichal Kalderon 					 pgprot_noncached(vma->vm_page_prot),
2326c043ff2cSMichal Kalderon 					 NULL);
23275c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
23285c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2329d69e3bcfSMatan Barak 
2330e126ba97SEli Cohen 	default:
2331dc2316ebSYishai Hadas 		return mlx5_ib_mmap_offset(dev, vma, ibcontext);
2332e126ba97SEli Cohen 	}
2333e126ba97SEli Cohen 
2334e126ba97SEli Cohen 	return 0;
2335e126ba97SEli Cohen }
2336e126ba97SEli Cohen 
2337ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2338e126ba97SEli Cohen {
233921a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
234021a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2341e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2342e126ba97SEli Cohen 	int err;
2343a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2344a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2345a1069c1cSYishai Hadas 	u16 uid = 0;
2346ff23dfa1SShamir Rabinovitch 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2347ff23dfa1SShamir Rabinovitch 		udata, struct mlx5_ib_ucontext, ibucontext);
2348e126ba97SEli Cohen 
2349ff23dfa1SShamir Rabinovitch 	uid = context ? context->devx_uid : 0;
2350a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2351a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
235231578defSLeon Romanovsky 	err = mlx5_cmd_exec_inout(to_mdev(ibdev)->mdev, alloc_pd, in, out);
235321a428a0SLeon Romanovsky 	if (err)
235421a428a0SLeon Romanovsky 		return err;
2355e126ba97SEli Cohen 
2356a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2357a1069c1cSYishai Hadas 	pd->uid = uid;
2358ff23dfa1SShamir Rabinovitch 	if (udata) {
2359e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2360e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2361a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
236221a428a0SLeon Romanovsky 			return -EFAULT;
2363e126ba97SEli Cohen 		}
2364e126ba97SEli Cohen 	}
2365e126ba97SEli Cohen 
236621a428a0SLeon Romanovsky 	return 0;
2367e126ba97SEli Cohen }
2368e126ba97SEli Cohen 
236991a7c58fSLeon Romanovsky static int mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2370e126ba97SEli Cohen {
2371e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2372e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2373e126ba97SEli Cohen 
237491a7c58fSLeon Romanovsky 	return mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2375e126ba97SEli Cohen }
2376e126ba97SEli Cohen 
2377e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2378e126ba97SEli Cohen {
2379e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
238081e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
2381e126ba97SEli Cohen 	int err;
2382539ec982SYishai Hadas 	u16 uid;
2383539ec982SYishai Hadas 
2384539ec982SYishai Hadas 	uid = ibqp->pd ?
2385539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
2386e126ba97SEli Cohen 
23872be08c30SLeon Romanovsky 	if (mqp->flags & IB_QP_CREATE_SOURCE_QPN) {
238881e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
238981e30880SYishai Hadas 		return -EOPNOTSUPP;
239081e30880SYishai Hadas 	}
239181e30880SYishai Hadas 
2392539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2393e126ba97SEli Cohen 	if (err)
2394e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2395e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2396e126ba97SEli Cohen 
2397e126ba97SEli Cohen 	return err;
2398e126ba97SEli Cohen }
2399e126ba97SEli Cohen 
2400e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2401e126ba97SEli Cohen {
2402e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2403e126ba97SEli Cohen 	int err;
2404539ec982SYishai Hadas 	u16 uid;
2405e126ba97SEli Cohen 
2406539ec982SYishai Hadas 	uid = ibqp->pd ?
2407539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
2408539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2409e126ba97SEli Cohen 	if (err)
2410e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2411e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2412e126ba97SEli Cohen 
2413e126ba97SEli Cohen 	return err;
2414e126ba97SEli Cohen }
2415e126ba97SEli Cohen 
2416e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2417e126ba97SEli Cohen {
24181b5daf11SMajd Dibbiny 	int err;
2419e126ba97SEli Cohen 
24201b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2421e126ba97SEli Cohen 	if (err)
2422e126ba97SEli Cohen 		return err;
24231b5daf11SMajd Dibbiny 
24241b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
24251b5daf11SMajd Dibbiny 
24261b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2427e126ba97SEli Cohen }
2428e126ba97SEli Cohen 
2429508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
2430508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2431e126ba97SEli Cohen {
2432e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
243354747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2434e126ba97SEli Cohen 
24351c7fd726SJoe Perches 	return sysfs_emit(buf, "%d\n", dev->mdev->priv.fw_pages);
2436e126ba97SEli Cohen }
2437508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
2438e126ba97SEli Cohen 
2439508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
2440e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2441e126ba97SEli Cohen {
2442e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
244354747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2444e126ba97SEli Cohen 
24451c7fd726SJoe Perches 	return sysfs_emit(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2446e126ba97SEli Cohen }
2447508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
2448e126ba97SEli Cohen 
2449508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
2450508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2451e126ba97SEli Cohen {
2452e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
245354747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
245454747231SParav Pandit 
24551c7fd726SJoe Perches 	return sysfs_emit(buf, "MT%d\n", dev->mdev->pdev->device);
2456e126ba97SEli Cohen }
2457508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
2458e126ba97SEli Cohen 
2459508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
2460508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
2461e126ba97SEli Cohen {
2462e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
246354747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
246454747231SParav Pandit 
24651c7fd726SJoe Perches 	return sysfs_emit(buf, "%x\n", dev->mdev->rev_id);
2466e126ba97SEli Cohen }
2467508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
2468e126ba97SEli Cohen 
2469508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
2470508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2471e126ba97SEli Cohen {
2472e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
247354747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
247454747231SParav Pandit 
24751c7fd726SJoe Perches 	return sysfs_emit(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
24769603b61dSJack Morgenstein 			  dev->mdev->board_id);
2477e126ba97SEli Cohen }
2478508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
2479e126ba97SEli Cohen 
2480508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
2481508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
2482508a523fSParav Pandit 	&dev_attr_hca_type.attr,
2483508a523fSParav Pandit 	&dev_attr_board_id.attr,
2484508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
2485508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
2486508a523fSParav Pandit 	NULL,
2487508a523fSParav Pandit };
2488e126ba97SEli Cohen 
2489508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
2490508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
2491e126ba97SEli Cohen };
2492e126ba97SEli Cohen 
24937722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
24947722f47eSHaggai Eran {
24957722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
24967722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
24977722f47eSHaggai Eran 			     pkey_change_work);
24987722f47eSHaggai Eran 
24998c9e7f03SLeon Romanovsky 	if (!ports->gsi)
25008c9e7f03SLeon Romanovsky 		/*
25018c9e7f03SLeon Romanovsky 		 * We got this event before device was fully configured
25028c9e7f03SLeon Romanovsky 		 * and MAD registration code wasn't called/finished yet.
25038c9e7f03SLeon Romanovsky 		 */
25048c9e7f03SLeon Romanovsky 		return;
25058c9e7f03SLeon Romanovsky 
25067722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
25077722f47eSHaggai Eran }
25087722f47eSHaggai Eran 
250989ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
251089ea94a7SMaor Gottlieb {
251189ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
251289ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
251389ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
251489ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
251589ea94a7SMaor Gottlieb 	unsigned long flags_qp;
251689ea94a7SMaor Gottlieb 	unsigned long flags_cq;
251789ea94a7SMaor Gottlieb 	unsigned long flags;
251889ea94a7SMaor Gottlieb 
251989ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
252089ea94a7SMaor Gottlieb 
252189ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
252289ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
252389ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
252489ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
252589ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
252689ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
252789ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
252889ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
252989ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
253089ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
253189ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
253289ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
253389ea94a7SMaor Gottlieb 						      &cq_armed_list);
253489ea94a7SMaor Gottlieb 				}
253589ea94a7SMaor Gottlieb 			}
253689ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
253789ea94a7SMaor Gottlieb 		}
253889ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
253989ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
254089ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
254189ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
254289ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
254389ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
254489ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
254589ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
254689ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
254789ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
254889ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
254989ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
255089ea94a7SMaor Gottlieb 							      &cq_armed_list);
255189ea94a7SMaor Gottlieb 					}
255289ea94a7SMaor Gottlieb 				}
255389ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
255489ea94a7SMaor Gottlieb 						       flags_cq);
255589ea94a7SMaor Gottlieb 			}
255689ea94a7SMaor Gottlieb 		}
255789ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
255889ea94a7SMaor Gottlieb 	}
255989ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
256089ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
256189ea94a7SMaor Gottlieb 	 */
256289ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
25634e0e2ea1SYishai Hadas 		mcq->comp(mcq, NULL);
256489ea94a7SMaor Gottlieb 	}
256589ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
256689ea94a7SMaor Gottlieb }
256789ea94a7SMaor Gottlieb 
256803404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
256903404e8aSMaor Gottlieb {
257003404e8aSMaor Gottlieb 	int err;
257103404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
257203404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
257303404e8aSMaor Gottlieb 			     delay_drop_work);
257403404e8aSMaor Gottlieb 
2575fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
2576fe248c3aSMaor Gottlieb 
257703404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
2578333fbaa0SLeon Romanovsky 	err = mlx5_core_set_delay_drop(delay_drop->dev, delay_drop->timeout);
257903404e8aSMaor Gottlieb 	if (err) {
258003404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
258103404e8aSMaor Gottlieb 			     delay_drop->timeout);
258203404e8aSMaor Gottlieb 		delay_drop->activate = false;
258303404e8aSMaor Gottlieb 	}
258403404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
258503404e8aSMaor Gottlieb }
258603404e8aSMaor Gottlieb 
258709e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
258809e574faSSaeed Mahameed 				 struct ib_event *ibev)
258909e574faSSaeed Mahameed {
25901fb7f897SMark Bloch 	u32 port = (eqe->data.port.port >> 4) & 0xf;
25916cfdc7e4SAya Levin 
259209e574faSSaeed Mahameed 	switch (eqe->sub_type) {
259309e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
25946cfdc7e4SAya Levin 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
25956cfdc7e4SAya Levin 					    IB_LINK_LAYER_ETHERNET)
259609e574faSSaeed Mahameed 			schedule_work(&ibdev->delay_drop.delay_drop_work);
259709e574faSSaeed Mahameed 		break;
259809e574faSSaeed Mahameed 	default: /* do nothing */
259909e574faSSaeed Mahameed 		return;
260009e574faSSaeed Mahameed 	}
260109e574faSSaeed Mahameed }
260209e574faSSaeed Mahameed 
2603134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
2604134e9349SSaeed Mahameed 			      struct ib_event *ibev)
2605134e9349SSaeed Mahameed {
26061fb7f897SMark Bloch 	u32 port = (eqe->data.port.port >> 4) & 0xf;
2607134e9349SSaeed Mahameed 
2608134e9349SSaeed Mahameed 	ibev->element.port_num = port;
2609134e9349SSaeed Mahameed 
2610134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
2611134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
2612134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
2613134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
2614134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
2615134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
2616134e9349SSaeed Mahameed 		 */
2617134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2618134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
2619134e9349SSaeed Mahameed 			return -EINVAL;
2620134e9349SSaeed Mahameed 
2621134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
2622134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2623134e9349SSaeed Mahameed 		break;
2624134e9349SSaeed Mahameed 
2625134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
2626134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
2627134e9349SSaeed Mahameed 		break;
2628134e9349SSaeed Mahameed 
2629134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
2630134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
2631134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2632134e9349SSaeed Mahameed 		break;
2633134e9349SSaeed Mahameed 
2634134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
2635134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
2636134e9349SSaeed Mahameed 		break;
2637134e9349SSaeed Mahameed 
2638134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
2639134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
2640134e9349SSaeed Mahameed 		break;
2641134e9349SSaeed Mahameed 	default:
2642134e9349SSaeed Mahameed 		return -EINVAL;
2643134e9349SSaeed Mahameed 	}
2644134e9349SSaeed Mahameed 
2645134e9349SSaeed Mahameed 	return 0;
2646134e9349SSaeed Mahameed }
2647134e9349SSaeed Mahameed 
2648d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
2649e126ba97SEli Cohen {
2650d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
2651d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
2652d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
2653e126ba97SEli Cohen 	struct ib_event ibev;
2654dbaaff2aSEli Cohen 	bool fatal = false;
2655e126ba97SEli Cohen 
2656df097a27SSaeed Mahameed 	if (work->is_slave) {
2657df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
2658d69a24e0SDaniel Jurgens 		if (!ibdev)
2659d69a24e0SDaniel Jurgens 			goto out;
2660d69a24e0SDaniel Jurgens 	} else {
2661df097a27SSaeed Mahameed 		ibdev = work->dev;
2662d69a24e0SDaniel Jurgens 	}
2663d69a24e0SDaniel Jurgens 
2664d69a24e0SDaniel Jurgens 	switch (work->event) {
2665e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2666e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
266789ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2668134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
2669dbaaff2aSEli Cohen 		fatal = true;
2670e126ba97SEli Cohen 		break;
2671134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
2672134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
2673d69a24e0SDaniel Jurgens 			goto out;
2674e126ba97SEli Cohen 		break;
267509e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
267609e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
2677df561f66SGustavo A. R. Silva 		fallthrough;
2678bdc37924SSaeed Mahameed 	default:
267903404e8aSMaor Gottlieb 		goto out;
2680e126ba97SEli Cohen 	}
2681e126ba97SEli Cohen 
2682e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
2683e126ba97SEli Cohen 
2684134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
2685134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
268603404e8aSMaor Gottlieb 		goto out;
2687a0c84c32SEli Cohen 	}
2688a0c84c32SEli Cohen 
2689e126ba97SEli Cohen 	if (ibdev->ib_active)
2690e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2691dbaaff2aSEli Cohen 
2692dbaaff2aSEli Cohen 	if (fatal)
2693dbaaff2aSEli Cohen 		ibdev->ib_active = false;
269403404e8aSMaor Gottlieb out:
2695d69a24e0SDaniel Jurgens 	kfree(work);
2696d69a24e0SDaniel Jurgens }
2697d69a24e0SDaniel Jurgens 
2698df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
2699df097a27SSaeed Mahameed 			 unsigned long event, void *param)
2700d69a24e0SDaniel Jurgens {
2701d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
2702d69a24e0SDaniel Jurgens 
2703d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
270410bea9c8SLeon Romanovsky 	if (!work)
2705df097a27SSaeed Mahameed 		return NOTIFY_DONE;
270610bea9c8SLeon Romanovsky 
2707d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
2708df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
2709df097a27SSaeed Mahameed 	work->is_slave = false;
2710d69a24e0SDaniel Jurgens 	work->param = param;
2711d69a24e0SDaniel Jurgens 	work->event = event;
2712d69a24e0SDaniel Jurgens 
2713d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
2714df097a27SSaeed Mahameed 
2715df097a27SSaeed Mahameed 	return NOTIFY_OK;
2716df097a27SSaeed Mahameed }
2717df097a27SSaeed Mahameed 
2718df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
2719df097a27SSaeed Mahameed 				    unsigned long event, void *param)
2720df097a27SSaeed Mahameed {
2721df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
2722df097a27SSaeed Mahameed 
2723df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
2724df097a27SSaeed Mahameed 	if (!work)
2725df097a27SSaeed Mahameed 		return NOTIFY_DONE;
2726df097a27SSaeed Mahameed 
2727df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
2728df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
2729df097a27SSaeed Mahameed 	work->is_slave = true;
2730df097a27SSaeed Mahameed 	work->param = param;
2731df097a27SSaeed Mahameed 	work->event = event;
2732df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
2733df097a27SSaeed Mahameed 
2734df097a27SSaeed Mahameed 	return NOTIFY_OK;
2735d69a24e0SDaniel Jurgens }
2736d69a24e0SDaniel Jurgens 
2737c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2738c43f1112SMaor Gottlieb {
2739c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
2740c43f1112SMaor Gottlieb 	int err;
2741c43f1112SMaor Gottlieb 	int port;
2742c43f1112SMaor Gottlieb 
27434b83c3caSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_IB)
27444b83c3caSMark Bloch 		return 0;
27454b83c3caSMark Bloch 
27464b83c3caSMark Bloch 	for (port = 1; port <= dev->num_ports; port++) {
27474b83c3caSMark Bloch 		if (!MLX5_CAP_GEN(dev->mdev, ib_virt)) {
27484b83c3caSMark Bloch 			dev->port_caps[port - 1].has_smi = true;
27494b83c3caSMark Bloch 			continue;
27504b83c3caSMark Bloch 		}
27514b83c3caSMark Bloch 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port, 0,
2752c43f1112SMaor Gottlieb 						   &vport_ctx);
2753c43f1112SMaor Gottlieb 		if (err) {
2754c43f1112SMaor Gottlieb 			mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2755c43f1112SMaor Gottlieb 				    port, err);
2756c43f1112SMaor Gottlieb 			return err;
2757c43f1112SMaor Gottlieb 		}
27584b83c3caSMark Bloch 		dev->port_caps[port - 1].has_smi = vport_ctx.has_smi;
2759c43f1112SMaor Gottlieb 	}
27604b83c3caSMark Bloch 
2761c43f1112SMaor Gottlieb 	return 0;
2762c43f1112SMaor Gottlieb }
2763c43f1112SMaor Gottlieb 
2764e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2765e126ba97SEli Cohen {
276613179652SParav Pandit 	unsigned int port;
2767e126ba97SEli Cohen 
276813179652SParav Pandit 	rdma_for_each_port (&dev->ib_dev, port)
2769e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
2770e126ba97SEli Cohen }
2771e126ba97SEli Cohen 
27726e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
27736e8484c5SMax Gurtovoy {
27746e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
27756e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
27766e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
27776e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
27786e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
27796e8484c5SMax Gurtovoy 	default:
27806e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
27816e8484c5SMax Gurtovoy 	}
27826e8484c5SMax Gurtovoy }
27836e8484c5SMax Gurtovoy 
27841e2b5a90SLeon Romanovsky static int mlx5_ib_dev_res_init(struct mlx5_ib_dev *dev)
2785e126ba97SEli Cohen {
27861e2b5a90SLeon Romanovsky 	struct mlx5_ib_resources *devr = &dev->devr;
2787e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
278821a428a0SLeon Romanovsky 	struct ib_device *ibdev;
2789bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
27907722f47eSHaggai Eran 	int port;
2791e126ba97SEli Cohen 	int ret = 0;
2792e126ba97SEli Cohen 
279321a428a0SLeon Romanovsky 	ibdev = &dev->ib_dev;
2794e126ba97SEli Cohen 
2795f4375443SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, xrc))
2796f4375443SLeon Romanovsky 		return -EOPNOTSUPP;
2797f4375443SLeon Romanovsky 
279820da44dfSLeon Romanovsky 	devr->p0 = ib_alloc_pd(ibdev, 0);
279920da44dfSLeon Romanovsky 	if (IS_ERR(devr->p0))
280020da44dfSLeon Romanovsky 		return PTR_ERR(devr->p0);
2801d16e91daSHaggai Eran 
280220da44dfSLeon Romanovsky 	devr->c0 = ib_create_cq(ibdev, NULL, NULL, NULL, &cq_attr);
280320da44dfSLeon Romanovsky 	if (IS_ERR(devr->c0)) {
280420da44dfSLeon Romanovsky 		ret = PTR_ERR(devr->c0);
2805e126ba97SEli Cohen 		goto error1;
2806e126ba97SEli Cohen 	}
2807e39afe3dSLeon Romanovsky 
2808f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn0, 0);
2809f4375443SLeon Romanovsky 	if (ret)
2810e126ba97SEli Cohen 		goto error2;
2811e126ba97SEli Cohen 
2812f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn1, 0);
2813f4375443SLeon Romanovsky 	if (ret)
2814e126ba97SEli Cohen 		goto error3;
2815e126ba97SEli Cohen 
2816e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
2817e126ba97SEli Cohen 	attr.attr.max_sge = 1;
2818e126ba97SEli Cohen 	attr.attr.max_wr = 1;
2819e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
28201a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
2821e126ba97SEli Cohen 
282220da44dfSLeon Romanovsky 	devr->s0 = ib_create_srq(devr->p0, &attr);
282320da44dfSLeon Romanovsky 	if (IS_ERR(devr->s0)) {
282420da44dfSLeon Romanovsky 		ret = PTR_ERR(devr->s0);
282568e326deSLeon Romanovsky 		goto err_create;
282620da44dfSLeon Romanovsky 	}
2827e126ba97SEli Cohen 
28284aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
28294aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
28304aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
28314aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
283268e326deSLeon Romanovsky 
283320da44dfSLeon Romanovsky 	devr->s1 = ib_create_srq(devr->p0, &attr);
283420da44dfSLeon Romanovsky 	if (IS_ERR(devr->s1)) {
283520da44dfSLeon Romanovsky 		ret = PTR_ERR(devr->s1);
283668e326deSLeon Romanovsky 		goto error6;
283720da44dfSLeon Romanovsky 	}
28384aa17b28SHaggai Abramonvsky 
2839f8225e34SLeon Romanovsky 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
28407722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
28417722f47eSHaggai Eran 			  pkey_change_handler);
28427722f47eSHaggai Eran 
2843e126ba97SEli Cohen 	return 0;
2844e126ba97SEli Cohen 
284568e326deSLeon Romanovsky error6:
284620da44dfSLeon Romanovsky 	ib_destroy_srq(devr->s0);
284768e326deSLeon Romanovsky err_create:
2848f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
2849e126ba97SEli Cohen error3:
2850f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
2851e126ba97SEli Cohen error2:
285220da44dfSLeon Romanovsky 	ib_destroy_cq(devr->c0);
2853e126ba97SEli Cohen error1:
285420da44dfSLeon Romanovsky 	ib_dealloc_pd(devr->p0);
2855e126ba97SEli Cohen 	return ret;
2856e126ba97SEli Cohen }
2857e126ba97SEli Cohen 
28581e2b5a90SLeon Romanovsky static void mlx5_ib_dev_res_cleanup(struct mlx5_ib_dev *dev)
2859e126ba97SEli Cohen {
28601e2b5a90SLeon Romanovsky 	struct mlx5_ib_resources *devr = &dev->devr;
28617722f47eSHaggai Eran 	int port;
28627722f47eSHaggai Eran 
2863b0791dbfSLeon Romanovsky 	/*
2864b0791dbfSLeon Romanovsky 	 * Make sure no change P_Key work items are still executing.
2865b0791dbfSLeon Romanovsky 	 *
2866b0791dbfSLeon Romanovsky 	 * At this stage, the mlx5_ib_event should be unregistered
2867b0791dbfSLeon Romanovsky 	 * and it ensures that no new works are added.
2868b0791dbfSLeon Romanovsky 	 */
28695d8f6a0eSMark Bloch 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
28707722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
2871b0791dbfSLeon Romanovsky 
287220da44dfSLeon Romanovsky 	ib_destroy_srq(devr->s1);
287320da44dfSLeon Romanovsky 	ib_destroy_srq(devr->s0);
2874e126ba97SEli Cohen 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
2875e126ba97SEli Cohen 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
287620da44dfSLeon Romanovsky 	ib_destroy_cq(devr->c0);
287720da44dfSLeon Romanovsky 	ib_dealloc_pd(devr->p0);
2878e126ba97SEli Cohen }
2879e126ba97SEli Cohen 
2880b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
2881b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
2882e53505a8SAchiad Shochat {
2883e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2884e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
2885e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
2886e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
288785c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
2888e53505a8SAchiad Shochat 	u32 ret = 0;
2889e53505a8SAchiad Shochat 
2890b02289b3SArtemy Kovalyov 	if (rep->grh_required)
2891b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
2892b02289b3SArtemy Kovalyov 
2893e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
2894b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
2895e53505a8SAchiad Shochat 
289685c7c014SDaniel Jurgens 	if (raw_support)
2897b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
289872cd5717SOr Gerlitz 
2899e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
290072cd5717SOr Gerlitz 		return ret;
2901e53505a8SAchiad Shochat 
2902e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
290372cd5717SOr Gerlitz 		return ret;
2904e53505a8SAchiad Shochat 
2905e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
2906e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
2907e53505a8SAchiad Shochat 
2908e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
2909e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
2910e53505a8SAchiad Shochat 
2911e53505a8SAchiad Shochat 	return ret;
2912e53505a8SAchiad Shochat }
2913e53505a8SAchiad Shochat 
29141fb7f897SMark Bloch static int mlx5_port_immutable(struct ib_device *ibdev, u32 port_num,
29157738613eSIra Weiny 			       struct ib_port_immutable *immutable)
29167738613eSIra Weiny {
29177738613eSIra Weiny 	struct ib_port_attr attr;
2918ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2919ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
2920b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
29217738613eSIra Weiny 	int err;
29227738613eSIra Weiny 
2923c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
29247738613eSIra Weiny 	if (err)
29257738613eSIra Weiny 		return err;
29267738613eSIra Weiny 
2927b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
2928b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
2929b02289b3SArtemy Kovalyov 						   &rep);
2930b02289b3SArtemy Kovalyov 		if (err)
2931b02289b3SArtemy Kovalyov 			return err;
2932b02289b3SArtemy Kovalyov 	}
2933b02289b3SArtemy Kovalyov 
29347738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
29357738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
2936b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
2937337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
29387738613eSIra Weiny 
29397738613eSIra Weiny 	return 0;
29407738613eSIra Weiny }
29417738613eSIra Weiny 
29421fb7f897SMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u32 port_num,
29438e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
29448e6efa3aSMark Bloch {
29458e6efa3aSMark Bloch 	struct ib_port_attr attr;
29468e6efa3aSMark Bloch 	int err;
29478e6efa3aSMark Bloch 
29488e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
29498e6efa3aSMark Bloch 
29508e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
29518e6efa3aSMark Bloch 	if (err)
29528e6efa3aSMark Bloch 		return err;
29538e6efa3aSMark Bloch 
29548e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
29558e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
29568e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
29578e6efa3aSMark Bloch 
29588e6efa3aSMark Bloch 	return 0;
29598e6efa3aSMark Bloch }
29608e6efa3aSMark Bloch 
29619abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
2962c7342823SIra Weiny {
2963c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
2964c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
29659abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
29669abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
29679abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
2968c7342823SIra Weiny }
2969c7342823SIra Weiny 
297045f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
29719ef9c640SAviv Heller {
29729ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
29739ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
29749ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
29759ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
29769ef9c640SAviv Heller 	int err;
29779ef9c640SAviv Heller 
2978c446d9daSMark Bloch 	if (!ns || !mlx5_lag_is_active(mdev))
29799ef9c640SAviv Heller 		return 0;
29809ef9c640SAviv Heller 
29819ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
29829ef9c640SAviv Heller 	if (err)
29839ef9c640SAviv Heller 		return err;
29849ef9c640SAviv Heller 
29859ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
29869ef9c640SAviv Heller 	if (IS_ERR(ft)) {
29879ef9c640SAviv Heller 		err = PTR_ERR(ft);
29889ef9c640SAviv Heller 		goto err_destroy_vport_lag;
29899ef9c640SAviv Heller 	}
29909ef9c640SAviv Heller 
29919a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
299234a30d76SMark Bloch 	dev->lag_ports = mlx5_lag_get_num_ports(mdev);
29937c34ec19SAviv Heller 	dev->lag_active = true;
29949ef9c640SAviv Heller 	return 0;
29959ef9c640SAviv Heller 
29969ef9c640SAviv Heller err_destroy_vport_lag:
29979ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
29989ef9c640SAviv Heller 	return err;
29999ef9c640SAviv Heller }
30009ef9c640SAviv Heller 
300145f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
30029ef9c640SAviv Heller {
30039ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
30049ef9c640SAviv Heller 
30057c34ec19SAviv Heller 	if (dev->lag_active) {
30067c34ec19SAviv Heller 		dev->lag_active = false;
30077c34ec19SAviv Heller 
30089a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
30099a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
30109ef9c640SAviv Heller 
30119ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
30129ef9c640SAviv Heller 	}
30139ef9c640SAviv Heller }
30149ef9c640SAviv Heller 
30151fb7f897SMark Bloch static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u32 port_num)
3016fc24fc5eSAchiad Shochat {
3017e53505a8SAchiad Shochat 	int err;
3018e53505a8SAchiad Shochat 
301995579e78SMark Bloch 	dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
3020de641d74SParav Pandit 	err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
30215ec8c83eSAviv Heller 	if (err) {
302295579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
3023e53505a8SAchiad Shochat 		return err;
30245ec8c83eSAviv Heller 	}
3025e53505a8SAchiad Shochat 
3026d012f5d6SOr Gerlitz 	return 0;
3027d012f5d6SOr Gerlitz }
3028d012f5d6SOr Gerlitz 
30291fb7f897SMark Bloch static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u32 port_num)
3030e126ba97SEli Cohen {
303195579e78SMark Bloch 	if (dev->port[port_num].roce.nb.notifier_call) {
3032de641d74SParav Pandit 		unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
303395579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
3034e126ba97SEli Cohen 	}
3035e126ba97SEli Cohen }
3036e126ba97SEli Cohen 
3037e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3038e126ba97SEli Cohen {
3039e126ba97SEli Cohen 	int err;
3040e126ba97SEli Cohen 
3041c446d9daSMark Bloch 	if (!dev->is_rep && dev->profile != &raw_eth_profile) {
3042e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3043e53505a8SAchiad Shochat 		if (err)
30448e6efa3aSMark Bloch 			return err;
3045c446d9daSMark Bloch 	}
3046e53505a8SAchiad Shochat 
304745f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
30489ef9c640SAviv Heller 	if (err)
30499ef9c640SAviv Heller 		goto err_disable_roce;
30509ef9c640SAviv Heller 
3051e53505a8SAchiad Shochat 	return 0;
3052e53505a8SAchiad Shochat 
30539ef9c640SAviv Heller err_disable_roce:
3054c446d9daSMark Bloch 	if (!dev->is_rep && dev->profile != &raw_eth_profile)
30559ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
30569ef9c640SAviv Heller 
3057e53505a8SAchiad Shochat 	return err;
3058fc24fc5eSAchiad Shochat }
3059fc24fc5eSAchiad Shochat 
306045f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3061fc24fc5eSAchiad Shochat {
306245f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3063c446d9daSMark Bloch 	if (!dev->is_rep && dev->profile != &raw_eth_profile)
3064e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
3065fc24fc5eSAchiad Shochat }
3066fc24fc5eSAchiad Shochat 
30671fb7f897SMark Bloch static int mlx5_ib_rn_get_params(struct ib_device *device, u32 port_num,
3068693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
3069f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
3070693dfd5aSErez Shitrit {
3071693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
3072f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
3073693dfd5aSErez Shitrit 
3074f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
3075693dfd5aSErez Shitrit }
3076693dfd5aSErez Shitrit 
3077fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3078fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
3079fe248c3aSMaor Gottlieb {
3080fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3081fe248c3aSMaor Gottlieb 	char lbuf[20];
3082fe248c3aSMaor Gottlieb 	int len;
3083fe248c3aSMaor Gottlieb 
3084fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3085fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
3086fe248c3aSMaor Gottlieb }
3087fe248c3aSMaor Gottlieb 
3088fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3089fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
3090fe248c3aSMaor Gottlieb {
3091fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3092fe248c3aSMaor Gottlieb 	u32 timeout;
3093fe248c3aSMaor Gottlieb 	u32 var;
3094fe248c3aSMaor Gottlieb 
3095fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
3096fe248c3aSMaor Gottlieb 		return -EFAULT;
3097fe248c3aSMaor Gottlieb 
3098fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3099fe248c3aSMaor Gottlieb 			1000);
3100fe248c3aSMaor Gottlieb 	if (timeout != var)
3101fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3102fe248c3aSMaor Gottlieb 			    timeout);
3103fe248c3aSMaor Gottlieb 
3104fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
3105fe248c3aSMaor Gottlieb 
3106fe248c3aSMaor Gottlieb 	return count;
3107fe248c3aSMaor Gottlieb }
3108fe248c3aSMaor Gottlieb 
3109fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
3110fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
3111fe248c3aSMaor Gottlieb 	.open	= simple_open,
3112fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
3113fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
3114fe248c3aSMaor Gottlieb };
3115fe248c3aSMaor Gottlieb 
311632f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
311732f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
311832f69e4bSDaniel Jurgens {
31191fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
312032f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
312132f69e4bSDaniel Jurgens 	int comps;
312232f69e4bSDaniel Jurgens 	int err;
312332f69e4bSDaniel Jurgens 	int i;
312432f69e4bSDaniel Jurgens 
31259dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
31269dc4cfffSLeon Romanovsky 
3127a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
3128a9e546e7SParav Pandit 
312932f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
313032f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
313132f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
313232f69e4bSDaniel Jurgens 		return;
313332f69e4bSDaniel Jurgens 	}
3134df097a27SSaeed Mahameed 
313532f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
313632f69e4bSDaniel Jurgens 
313732f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
313823eaf3b5SLeon Romanovsky 	if (mpi->mdev_events.notifier_call)
313923eaf3b5SLeon Romanovsky 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
314023eaf3b5SLeon Romanovsky 	mpi->mdev_events.notifier_call = NULL;
314132f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
314232f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
314332f69e4bSDaniel Jurgens 
314432f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
314532f69e4bSDaniel Jurgens 	if (comps) {
314632f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
314732f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
314832f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
314932f69e4bSDaniel Jurgens 
315032f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
315132f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
315232f69e4bSDaniel Jurgens 
315332f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
315432f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
315532f69e4bSDaniel Jurgens 	}
315632f69e4bSDaniel Jurgens 
315732f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
315832f69e4bSDaniel Jurgens 
315932f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
316032f69e4bSDaniel Jurgens 
316132f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
316232f69e4bSDaniel Jurgens 
31631fb7f897SMark Bloch 	mlx5_ib_dbg(ibdev, "unaffiliated port %u\n", port_num + 1);
316432f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
316532f69e4bSDaniel Jurgens 	 * it back to the list.
316632f69e4bSDaniel Jurgens 	 */
316732f69e4bSDaniel Jurgens 	if (err)
316832f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
316932f69e4bSDaniel Jurgens 			    port_num + 1);
317032f69e4bSDaniel Jurgens 
317195579e78SMark Bloch 	ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
317232f69e4bSDaniel Jurgens }
317332f69e4bSDaniel Jurgens 
317432f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
317532f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
317632f69e4bSDaniel Jurgens {
31771fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
317832f69e4bSDaniel Jurgens 	int err;
317932f69e4bSDaniel Jurgens 
31809dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
31819dc4cfffSLeon Romanovsky 
318232f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
318332f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
31841fb7f897SMark Bloch 		mlx5_ib_dbg(ibdev, "port %u already affiliated.\n",
318532f69e4bSDaniel Jurgens 			    port_num + 1);
318632f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
318732f69e4bSDaniel Jurgens 		return false;
318832f69e4bSDaniel Jurgens 	}
318932f69e4bSDaniel Jurgens 
319032f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
319132f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
3192df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
319332f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
319432f69e4bSDaniel Jurgens 
319532f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
319632f69e4bSDaniel Jurgens 	if (err)
319732f69e4bSDaniel Jurgens 		goto unbind;
319832f69e4bSDaniel Jurgens 
319932f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
320032f69e4bSDaniel Jurgens 	if (err) {
320132f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
320232f69e4bSDaniel Jurgens 			    port_num + 1);
320332f69e4bSDaniel Jurgens 		goto unbind;
320432f69e4bSDaniel Jurgens 	}
320532f69e4bSDaniel Jurgens 
3206df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
3207df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
3208df097a27SSaeed Mahameed 
320973eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
3210a9e546e7SParav Pandit 
321132f69e4bSDaniel Jurgens 	return true;
321232f69e4bSDaniel Jurgens 
321332f69e4bSDaniel Jurgens unbind:
321432f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
321532f69e4bSDaniel Jurgens 	return false;
321632f69e4bSDaniel Jurgens }
321732f69e4bSDaniel Jurgens 
321832f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
321932f69e4bSDaniel Jurgens {
32201fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
322132f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
322232f69e4bSDaniel Jurgens 							  port_num + 1);
322332f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
322432f69e4bSDaniel Jurgens 	int err;
32251fb7f897SMark Bloch 	u32 i;
322632f69e4bSDaniel Jurgens 
322732f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
322832f69e4bSDaniel Jurgens 		return 0;
322932f69e4bSDaniel Jurgens 
323032f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
323132f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
323232f69e4bSDaniel Jurgens 	if (err)
323332f69e4bSDaniel Jurgens 		return err;
323432f69e4bSDaniel Jurgens 
323532f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
323632f69e4bSDaniel Jurgens 	if (err)
323732f69e4bSDaniel Jurgens 		return err;
323832f69e4bSDaniel Jurgens 
323932f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
324032f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
324132f69e4bSDaniel Jurgens 		bool bound = false;
324232f69e4bSDaniel Jurgens 
324332f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
324432f69e4bSDaniel Jurgens 		if (i == port_num) {
324532f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
324632f69e4bSDaniel Jurgens 			if (!mpi) {
324732f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
324832f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
324932f69e4bSDaniel Jurgens 				return -ENOMEM;
325032f69e4bSDaniel Jurgens 			}
325132f69e4bSDaniel Jurgens 
325232f69e4bSDaniel Jurgens 			mpi->is_master = true;
325332f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
325432f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
325532f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
325632f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
325732f69e4bSDaniel Jurgens 			mpi = NULL;
325832f69e4bSDaniel Jurgens 			continue;
325932f69e4bSDaniel Jurgens 		}
326032f69e4bSDaniel Jurgens 
326132f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
326232f69e4bSDaniel Jurgens 				    list) {
326332f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
326432f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
326532f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
326632f69e4bSDaniel Jurgens 			}
326732f69e4bSDaniel Jurgens 
326832f69e4bSDaniel Jurgens 			if (bound) {
3269c42260f1SVu Pham 				dev_dbg(mpi->mdev->device,
3270c42260f1SVu Pham 					"removing port from unaffiliated list.\n");
327132f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
327232f69e4bSDaniel Jurgens 				list_del(&mpi->list);
327332f69e4bSDaniel Jurgens 				break;
327432f69e4bSDaniel Jurgens 			}
327532f69e4bSDaniel Jurgens 		}
32767416790eSParav Pandit 		if (!bound)
327732f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
327832f69e4bSDaniel Jurgens 				    i + 1);
327932f69e4bSDaniel Jurgens 	}
328032f69e4bSDaniel Jurgens 
328132f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
328232f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
328332f69e4bSDaniel Jurgens 	return err;
328432f69e4bSDaniel Jurgens }
328532f69e4bSDaniel Jurgens 
328632f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
328732f69e4bSDaniel Jurgens {
32881fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
328932f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
329032f69e4bSDaniel Jurgens 							  port_num + 1);
32911fb7f897SMark Bloch 	u32 i;
329232f69e4bSDaniel Jurgens 
329332f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
329432f69e4bSDaniel Jurgens 		return;
329532f69e4bSDaniel Jurgens 
329632f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
329732f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
329832f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
329932f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
330032f69e4bSDaniel Jurgens 			if (i == port_num) {
330132f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
330232f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
330332f69e4bSDaniel Jurgens 			} else {
33041fb7f897SMark Bloch 				mlx5_ib_dbg(dev, "unbinding port_num: %u\n",
33051fb7f897SMark Bloch 					    i + 1);
33067ce6095eSLeon Romanovsky 				list_add_tail(&dev->port[i].mp.mpi->list,
33077ce6095eSLeon Romanovsky 					      &mlx5_ib_unaffiliated_port_list);
33084a754d76SLeon Romanovsky 				mlx5_ib_unbind_slave_port(dev,
33094a754d76SLeon Romanovsky 							  dev->port[i].mp.mpi);
331032f69e4bSDaniel Jurgens 			}
331132f69e4bSDaniel Jurgens 		}
331232f69e4bSDaniel Jurgens 	}
331332f69e4bSDaniel Jurgens 
331432f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
331532f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
331632f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
331732f69e4bSDaniel Jurgens 
331832f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
331932f69e4bSDaniel Jurgens }
332032f69e4bSDaniel Jurgens 
3321342ee59dSYishai Hadas static int mmap_obj_cleanup(struct ib_uobject *uobject,
33227be76befSYishai Hadas 			    enum rdma_remove_reason why,
33237be76befSYishai Hadas 			    struct uverbs_attr_bundle *attrs)
33247be76befSYishai Hadas {
33257be76befSYishai Hadas 	struct mlx5_user_mmap_entry *obj = uobject->object;
33267be76befSYishai Hadas 
33277be76befSYishai Hadas 	rdma_user_mmap_entry_remove(&obj->rdma_entry);
33287be76befSYishai Hadas 	return 0;
33297be76befSYishai Hadas }
33307be76befSYishai Hadas 
3331342ee59dSYishai Hadas static int mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext *c,
3332342ee59dSYishai Hadas 					    struct mlx5_user_mmap_entry *entry,
3333342ee59dSYishai Hadas 					    size_t length)
3334342ee59dSYishai Hadas {
3335342ee59dSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
3336342ee59dSYishai Hadas 		&c->ibucontext, &entry->rdma_entry, length,
3337342ee59dSYishai Hadas 		(MLX5_IB_MMAP_OFFSET_START << 16),
3338342ee59dSYishai Hadas 		((MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1));
3339342ee59dSYishai Hadas }
3340342ee59dSYishai Hadas 
33417be76befSYishai Hadas static struct mlx5_user_mmap_entry *
33427be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c)
33437be76befSYishai Hadas {
33447be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
33457be76befSYishai Hadas 	struct mlx5_var_table *var_table;
33467be76befSYishai Hadas 	u32 page_idx;
33477be76befSYishai Hadas 	int err;
33487be76befSYishai Hadas 
33497be76befSYishai Hadas 	var_table = &to_mdev(c->ibucontext.device)->var_table;
33507be76befSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
33517be76befSYishai Hadas 	if (!entry)
33527be76befSYishai Hadas 		return ERR_PTR(-ENOMEM);
33537be76befSYishai Hadas 
33547be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
33557be76befSYishai Hadas 	page_idx = find_first_zero_bit(var_table->bitmap,
33567be76befSYishai Hadas 				       var_table->num_var_hw_entries);
33577be76befSYishai Hadas 	if (page_idx >= var_table->num_var_hw_entries) {
33587be76befSYishai Hadas 		err = -ENOSPC;
33597be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
33607be76befSYishai Hadas 		goto end;
33617be76befSYishai Hadas 	}
33627be76befSYishai Hadas 
33637be76befSYishai Hadas 	set_bit(page_idx, var_table->bitmap);
33647be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
33657be76befSYishai Hadas 
33667be76befSYishai Hadas 	entry->address = var_table->hw_start_addr +
33677be76befSYishai Hadas 				(page_idx * var_table->stride_size);
33687be76befSYishai Hadas 	entry->page_idx = page_idx;
33697be76befSYishai Hadas 	entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR;
33707be76befSYishai Hadas 
3371342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry,
3372342ee59dSYishai Hadas 					       var_table->stride_size);
33737be76befSYishai Hadas 	if (err)
33747be76befSYishai Hadas 		goto err_insert;
33757be76befSYishai Hadas 
33767be76befSYishai Hadas 	return entry;
33777be76befSYishai Hadas 
33787be76befSYishai Hadas err_insert:
33797be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
33807be76befSYishai Hadas 	clear_bit(page_idx, var_table->bitmap);
33817be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
33827be76befSYishai Hadas end:
33837be76befSYishai Hadas 	kfree(entry);
33847be76befSYishai Hadas 	return ERR_PTR(err);
33857be76befSYishai Hadas }
33867be76befSYishai Hadas 
33877be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)(
33887be76befSYishai Hadas 	struct uverbs_attr_bundle *attrs)
33897be76befSYishai Hadas {
33907be76befSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
33917be76befSYishai Hadas 		attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
33927be76befSYishai Hadas 	struct mlx5_ib_ucontext *c;
33937be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
33947be76befSYishai Hadas 	u64 mmap_offset;
33957be76befSYishai Hadas 	u32 length;
33967be76befSYishai Hadas 	int err;
33977be76befSYishai Hadas 
33987be76befSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
33997be76befSYishai Hadas 	if (IS_ERR(c))
34007be76befSYishai Hadas 		return PTR_ERR(c);
34017be76befSYishai Hadas 
34027be76befSYishai Hadas 	entry = alloc_var_entry(c);
34037be76befSYishai Hadas 	if (IS_ERR(entry))
34047be76befSYishai Hadas 		return PTR_ERR(entry);
34057be76befSYishai Hadas 
34067be76befSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
34077be76befSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
34087be76befSYishai Hadas 	uobj->object = entry;
34090ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
34107be76befSYishai Hadas 
34117be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
34127be76befSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
34137be76befSYishai Hadas 	if (err)
34140ac8903cSJason Gunthorpe 		return err;
34157be76befSYishai Hadas 
34167be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
34177be76befSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
34187be76befSYishai Hadas 	if (err)
34190ac8903cSJason Gunthorpe 		return err;
34207be76befSYishai Hadas 
34217be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
34227be76befSYishai Hadas 			     &length, sizeof(length));
34237be76befSYishai Hadas 	return err;
34247be76befSYishai Hadas }
34257be76befSYishai Hadas 
34267be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
34277be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_ALLOC,
34287be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE,
34297be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
34307be76befSYishai Hadas 			UVERBS_ACCESS_NEW,
34317be76befSYishai Hadas 			UA_MANDATORY),
34327be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
34337be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
34347be76befSYishai Hadas 			   UA_MANDATORY),
34357be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
34367be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
34377be76befSYishai Hadas 			   UA_MANDATORY),
34387be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
34397be76befSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
34407be76befSYishai Hadas 			    UA_MANDATORY));
34417be76befSYishai Hadas 
34427be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
34437be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_DESTROY,
34447be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE,
34457be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
34467be76befSYishai Hadas 			UVERBS_ACCESS_DESTROY,
34477be76befSYishai Hadas 			UA_MANDATORY));
34487be76befSYishai Hadas 
34497be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR,
3450342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
34517be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC),
34527be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY));
34537be76befSYishai Hadas 
34547be76befSYishai Hadas static bool var_is_supported(struct ib_device *device)
34557be76befSYishai Hadas {
34567be76befSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(device);
34577be76befSYishai Hadas 
34587be76befSYishai Hadas 	return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
34597be76befSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q);
34607be76befSYishai Hadas }
34617be76befSYishai Hadas 
3462342ee59dSYishai Hadas static struct mlx5_user_mmap_entry *
3463342ee59dSYishai Hadas alloc_uar_entry(struct mlx5_ib_ucontext *c,
3464342ee59dSYishai Hadas 		enum mlx5_ib_uapi_uar_alloc_type alloc_type)
3465342ee59dSYishai Hadas {
3466342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
3467342ee59dSYishai Hadas 	struct mlx5_ib_dev *dev;
3468342ee59dSYishai Hadas 	u32 uar_index;
3469342ee59dSYishai Hadas 	int err;
3470342ee59dSYishai Hadas 
3471342ee59dSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
3472342ee59dSYishai Hadas 	if (!entry)
3473342ee59dSYishai Hadas 		return ERR_PTR(-ENOMEM);
3474342ee59dSYishai Hadas 
3475342ee59dSYishai Hadas 	dev = to_mdev(c->ibucontext.device);
3476d2c8a155SMeir Lichtinger 	err = mlx5_cmd_uar_alloc(dev->mdev, &uar_index, c->devx_uid);
3477342ee59dSYishai Hadas 	if (err)
3478342ee59dSYishai Hadas 		goto end;
3479342ee59dSYishai Hadas 
3480342ee59dSYishai Hadas 	entry->page_idx = uar_index;
3481342ee59dSYishai Hadas 	entry->address = uar_index2paddress(dev, uar_index);
3482342ee59dSYishai Hadas 	if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3483342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC;
3484342ee59dSYishai Hadas 	else
3485342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_NC;
3486342ee59dSYishai Hadas 
3487342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry, PAGE_SIZE);
3488342ee59dSYishai Hadas 	if (err)
3489342ee59dSYishai Hadas 		goto err_insert;
3490342ee59dSYishai Hadas 
3491342ee59dSYishai Hadas 	return entry;
3492342ee59dSYishai Hadas 
3493342ee59dSYishai Hadas err_insert:
3494d2c8a155SMeir Lichtinger 	mlx5_cmd_uar_dealloc(dev->mdev, uar_index, c->devx_uid);
3495342ee59dSYishai Hadas end:
3496342ee59dSYishai Hadas 	kfree(entry);
3497342ee59dSYishai Hadas 	return ERR_PTR(err);
3498342ee59dSYishai Hadas }
3499342ee59dSYishai Hadas 
3500342ee59dSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)(
3501342ee59dSYishai Hadas 	struct uverbs_attr_bundle *attrs)
3502342ee59dSYishai Hadas {
3503342ee59dSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
3504342ee59dSYishai Hadas 		attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3505342ee59dSYishai Hadas 	enum mlx5_ib_uapi_uar_alloc_type alloc_type;
3506342ee59dSYishai Hadas 	struct mlx5_ib_ucontext *c;
3507342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
3508342ee59dSYishai Hadas 	u64 mmap_offset;
3509342ee59dSYishai Hadas 	u32 length;
3510342ee59dSYishai Hadas 	int err;
3511342ee59dSYishai Hadas 
3512342ee59dSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
3513342ee59dSYishai Hadas 	if (IS_ERR(c))
3514342ee59dSYishai Hadas 		return PTR_ERR(c);
3515342ee59dSYishai Hadas 
3516342ee59dSYishai Hadas 	err = uverbs_get_const(&alloc_type, attrs,
3517342ee59dSYishai Hadas 			       MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE);
3518342ee59dSYishai Hadas 	if (err)
3519342ee59dSYishai Hadas 		return err;
3520342ee59dSYishai Hadas 
3521342ee59dSYishai Hadas 	if (alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF &&
3522342ee59dSYishai Hadas 	    alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_NC)
3523342ee59dSYishai Hadas 		return -EOPNOTSUPP;
3524342ee59dSYishai Hadas 
3525342ee59dSYishai Hadas 	if (!to_mdev(c->ibucontext.device)->wc_support &&
3526342ee59dSYishai Hadas 	    alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3527342ee59dSYishai Hadas 		return -EOPNOTSUPP;
3528342ee59dSYishai Hadas 
3529342ee59dSYishai Hadas 	entry = alloc_uar_entry(c, alloc_type);
3530342ee59dSYishai Hadas 	if (IS_ERR(entry))
3531342ee59dSYishai Hadas 		return PTR_ERR(entry);
3532342ee59dSYishai Hadas 
3533342ee59dSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
3534342ee59dSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
3535342ee59dSYishai Hadas 	uobj->object = entry;
35360ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3537342ee59dSYishai Hadas 
3538342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3539342ee59dSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
3540342ee59dSYishai Hadas 	if (err)
35410ac8903cSJason Gunthorpe 		return err;
3542342ee59dSYishai Hadas 
3543342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3544342ee59dSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
3545342ee59dSYishai Hadas 	if (err)
35460ac8903cSJason Gunthorpe 		return err;
3547342ee59dSYishai Hadas 
3548342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3549342ee59dSYishai Hadas 			     &length, sizeof(length));
3550342ee59dSYishai Hadas 	return err;
3551342ee59dSYishai Hadas }
3552342ee59dSYishai Hadas 
3553342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
3554342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_ALLOC,
3555342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE,
3556342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
3557342ee59dSYishai Hadas 			UVERBS_ACCESS_NEW,
3558342ee59dSYishai Hadas 			UA_MANDATORY),
3559342ee59dSYishai Hadas 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE,
3560342ee59dSYishai Hadas 			     enum mlx5_ib_uapi_uar_alloc_type,
3561342ee59dSYishai Hadas 			     UA_MANDATORY),
3562342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3563342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
3564342ee59dSYishai Hadas 			   UA_MANDATORY),
3565342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3566342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
3567342ee59dSYishai Hadas 			   UA_MANDATORY),
3568342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3569342ee59dSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
3570342ee59dSYishai Hadas 			    UA_MANDATORY));
3571342ee59dSYishai Hadas 
3572342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
3573342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_DESTROY,
3574342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_DESTROY_HANDLE,
3575342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
3576342ee59dSYishai Hadas 			UVERBS_ACCESS_DESTROY,
3577342ee59dSYishai Hadas 			UA_MANDATORY));
3578342ee59dSYishai Hadas 
3579342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_UAR,
3580342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
3581342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_ALLOC),
3582342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_DESTROY));
3583342ee59dSYishai Hadas 
35849a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
35850fb556b2SYishai Hadas 	mlx5_ib_query_context,
35860fb556b2SYishai Hadas 	UVERBS_OBJECT_DEVICE,
35870fb556b2SYishai Hadas 	UVERBS_METHOD_QUERY_CONTEXT,
35880fb556b2SYishai Hadas 	UVERBS_ATTR_PTR_OUT(
35890fb556b2SYishai Hadas 		MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
35900fb556b2SYishai Hadas 		UVERBS_ATTR_STRUCT(struct mlx5_ib_alloc_ucontext_resp,
35910fb556b2SYishai Hadas 				   dump_fill_mkey),
35920fb556b2SYishai Hadas 		UA_MANDATORY));
35930fb556b2SYishai Hadas 
35940cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
359536e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
35960cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
359730f2fe40SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_qos_defs),
359805f71ef9SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_std_types_defs),
3599831df883SMaor Gottlieb 	UAPI_DEF_CHAIN(mlx5_ib_dm_defs),
36008c84660bSMatan Barak 
36010fb556b2SYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DEVICE, &mlx5_ib_query_context),
36027be76befSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR,
36037be76befSYishai Hadas 				UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)),
3604342ee59dSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_UAR),
36050cbf432dSJason Gunthorpe 	{}
36060cbf432dSJason Gunthorpe };
36078c84660bSMatan Barak 
3608fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
3609e126ba97SEli Cohen {
361032f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
3611806b101bSJason Gunthorpe 	WARN_ON(!xa_empty(&dev->odp_mkeys));
3612ab40530aSParav Pandit 	mutex_destroy(&dev->cap_mask_mutex);
361350211ec9SJason Gunthorpe 	WARN_ON(!xa_empty(&dev->sig_mrs));
36144056b12eSAriel Levkovich 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
361516c1975fSMark Bloch }
361616c1975fSMark Bloch 
3617fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
361816c1975fSMark Bloch {
361916c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
3620e126ba97SEli Cohen 	int err;
362132f69e4bSDaniel Jurgens 	int i;
3622e126ba97SEli Cohen 
362313179652SParav Pandit 	dev->ib_dev.node_type = RDMA_NODE_IB_CA;
362413179652SParav Pandit 	dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
362513179652SParav Pandit 	dev->ib_dev.phys_port_cnt = dev->num_ports;
362613179652SParav Pandit 	dev->ib_dev.dev.parent = mdev->device;
362713179652SParav Pandit 	dev->ib_dev.lag_flags = RDMA_LAG_FLAGS_HASH_ALL_SLAVES;
362813179652SParav Pandit 
362932f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
363032f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
363195579e78SMark Bloch 		rwlock_init(&dev->port[i].roce.netdev_lock);
3632d3b5cc1cSMark Bloch 		dev->port[i].roce.dev = dev;
3633d3b5cc1cSMark Bloch 		dev->port[i].roce.native_port_num = i + 1;
3634d3b5cc1cSMark Bloch 		dev->port[i].roce.last_port_state = IB_PORT_DOWN;
363532f69e4bSDaniel Jurgens 	}
363632f69e4bSDaniel Jurgens 
363732f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
3638e126ba97SEli Cohen 	if (err)
3639da796ccbSMark Bloch 		return err;
3640e126ba97SEli Cohen 
3641a989ea01SMark Bloch 	err = set_has_smi_cap(dev);
3642a989ea01SMark Bloch 	if (err)
36432cb091f6SParav Pandit 		goto err_mp;
3644e126ba97SEli Cohen 
36452019d70eSParav Pandit 	err = mlx5_query_max_pkeys(&dev->ib_dev, &dev->pkey_table_len);
36462019d70eSParav Pandit 	if (err)
36472019d70eSParav Pandit 		goto err_mp;
36482019d70eSParav Pandit 
36491b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3650e126ba97SEli Cohen 		get_ext_port_caps(dev);
3651e126ba97SEli Cohen 
3652f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
3653e126ba97SEli Cohen 
36543cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
36553cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
36563cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
3657806b101bSJason Gunthorpe 	xa_init(&dev->odp_mkeys);
365850211ec9SJason Gunthorpe 	xa_init(&dev->sig_mrs);
3659f743ff3bSSaeed Mahameed 	atomic_set(&dev->mkey_var, 0);
36603cc297dbSMark Bloch 
36613b113a1eSAriel Levkovich 	spin_lock_init(&dev->dm.lock);
36623b113a1eSAriel Levkovich 	dev->dm.dev = mdev;
366316c1975fSMark Bloch 	return 0;
366425c13324SAriel Levkovich 
366532f69e4bSDaniel Jurgens err_mp:
366632f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
3667d286ac1dSParav Pandit 	return err;
366816c1975fSMark Bloch }
366916c1975fSMark Bloch 
3670ac47bf5eSLeon Romanovsky static int mlx5_ib_enable_driver(struct ib_device *dev)
36719a4ca38dSMark Bloch {
3672ac47bf5eSLeon Romanovsky 	struct mlx5_ib_dev *mdev = to_mdev(dev);
3673ac47bf5eSLeon Romanovsky 	int ret;
36749a4ca38dSMark Bloch 
3675ac47bf5eSLeon Romanovsky 	ret = mlx5_ib_test_wc(mdev);
3676ac47bf5eSLeon Romanovsky 	mlx5_ib_dbg(mdev, "Write-Combining %s",
3677ac47bf5eSLeon Romanovsky 		    mdev->wc_support ? "supported" : "not supported");
36789a4ca38dSMark Bloch 
3679ac47bf5eSLeon Romanovsky 	return ret;
36809a4ca38dSMark Bloch }
36819a4ca38dSMark Bloch 
368296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
36837a154142SJason Gunthorpe 	.owner = THIS_MODULE,
3684b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_MLX5,
368572c6ec18SJason Gunthorpe 	.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION,
3686b9560a41SJason Gunthorpe 
368796458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
368896458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
36896c984472SMax Gurtovoy 	.alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
369096458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
369196458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
369296458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
369396458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
369496458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
369596458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
369696458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
369796458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
3698676a80adSJason Gunthorpe 	.create_user_ah = mlx5_ib_create_ah,
369996458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
370096458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
370196458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
370296458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
370396458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
370496458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
370596458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
370696458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
370796458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
370896458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
370996458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
371096458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
3711915e4af5SJason Gunthorpe 	.device_group = &mlx5_attr_group,
371211f552e2SMichael Guralnik 	.enable_driver = mlx5_ib_enable_driver,
371396458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
371496458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
371596458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
371696458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
37176c984472SMax Gurtovoy 	.map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
371896458233SKamal Heib 	.mmap = mlx5_ib_mmap,
3719dc2316ebSYishai Hadas 	.mmap_free = mlx5_ib_mmap_free,
372096458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
372196458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
372296458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
372396458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
372496458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
372596458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
3726029e88fdSLeon Romanovsky 	.post_recv = mlx5_ib_post_recv_nodrain,
3727029e88fdSLeon Romanovsky 	.post_send = mlx5_ib_post_send_nodrain,
372896458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
372996458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
373096458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
373196458233SKamal Heib 	.query_device = mlx5_ib_query_device,
373296458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
373396458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
373496458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
373596458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
37360fb556b2SYishai Hadas 	.query_ucontext = mlx5_ib_query_ucontext,
373796458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
373890da7dc8SJianxin Xiong 	.reg_user_mr_dmabuf = mlx5_ib_reg_user_mr_dmabuf,
373996458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
374096458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
374196458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
3742d3456914SLeon Romanovsky 
3743d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
37443b023e1bSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_counters, mlx5_ib_mcounters, ibcntrs),
3745e39afe3dSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
374621a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
3747514aee66SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_qp, mlx5_ib_qp, ibqp),
374868e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
3749a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
375096458233SKamal Heib };
375196458233SKamal Heib 
375296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
375396458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
375496458233SKamal Heib };
375596458233SKamal Heib 
375696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
375796458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
37589c0015efSDanit Goldberg 	.get_vf_guid = mlx5_ib_get_vf_guid,
375996458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
376096458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
376196458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
376296458233SKamal Heib };
376396458233SKamal Heib 
376496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
376596458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
376696458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
3767d18bb3e1SLeon Romanovsky 
3768d18bb3e1SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_mw, mlx5_ib_mw, ibmw),
376996458233SKamal Heib };
377096458233SKamal Heib 
377196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
377296458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
377396458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
377428ad5f65SLeon Romanovsky 
377528ad5f65SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_xrcd, mlx5_ib_xrcd, ibxrcd),
377696458233SKamal Heib };
377796458233SKamal Heib 
3778f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev)
3779f164be8cSYishai Hadas {
3780f164be8cSYishai Hadas 	struct mlx5_core_dev *mdev = dev->mdev;
3781f164be8cSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
3782f164be8cSYishai Hadas 	u8 log_doorbell_bar_size;
3783f164be8cSYishai Hadas 	u8 log_doorbell_stride;
3784f164be8cSYishai Hadas 	u64 bar_size;
3785f164be8cSYishai Hadas 
3786f164be8cSYishai Hadas 	log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
3787f164be8cSYishai Hadas 					log_doorbell_bar_size);
3788f164be8cSYishai Hadas 	log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
3789f164be8cSYishai Hadas 					log_doorbell_stride);
3790f164be8cSYishai Hadas 	var_table->hw_start_addr = dev->mdev->bar_addr +
3791f164be8cSYishai Hadas 				MLX5_CAP64_DEV_VDPA_EMULATION(mdev,
3792f164be8cSYishai Hadas 					doorbell_bar_offset);
3793f164be8cSYishai Hadas 	bar_size = (1ULL << log_doorbell_bar_size) * 4096;
3794f164be8cSYishai Hadas 	var_table->stride_size = 1ULL << log_doorbell_stride;
379591b74bf5SAlexander Lobakin 	var_table->num_var_hw_entries = div_u64(bar_size,
379691b74bf5SAlexander Lobakin 						var_table->stride_size);
3797f164be8cSYishai Hadas 	mutex_init(&var_table->bitmap_lock);
3798f164be8cSYishai Hadas 	var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries,
3799f164be8cSYishai Hadas 					  GFP_KERNEL);
3800f164be8cSYishai Hadas 	return (var_table->bitmap) ? 0 : -ENOMEM;
3801f164be8cSYishai Hadas }
3802f164be8cSYishai Hadas 
3803f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev)
3804f164be8cSYishai Hadas {
3805f164be8cSYishai Hadas 	bitmap_free(dev->var_table.bitmap);
3806f164be8cSYishai Hadas }
3807f164be8cSYishai Hadas 
3808fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
380916c1975fSMark Bloch {
381016c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
381116c1975fSMark Bloch 	int err;
381216c1975fSMark Bloch 
3813f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
3814f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
381596458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
381696458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
38178e959601SNiranjana Vishwanathapura 
381896458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
381996458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
38207c2344c3SMaor Gottlieb 
38216e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
38226e8484c5SMax Gurtovoy 
382344ce37bcSJason Gunthorpe 	if (MLX5_CAP_GEN(mdev, imaicl))
382496458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
3825d2370e0aSMatan Barak 
382644ce37bcSJason Gunthorpe 	if (MLX5_CAP_GEN(mdev, xrc))
382796458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
3828e126ba97SEli Cohen 
382925c13324SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic) ||
383025c13324SAriel Levkovich 	    MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
383125c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
383296458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
383324da0016SAriel Levkovich 
383496458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
383581e30880SYishai Hadas 
383636e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
383736e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
383881e30880SYishai Hadas 
3839e126ba97SEli Cohen 	err = init_node_data(dev);
3840e126ba97SEli Cohen 	if (err)
384116c1975fSMark Bloch 		return err;
3842e126ba97SEli Cohen 
3843c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
3844e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
3845e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
3846a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
3847c8b89924SMark Bloch 
3848f164be8cSYishai Hadas 	if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
3849f164be8cSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) {
3850f164be8cSYishai Hadas 		err = mlx5_ib_init_var_table(dev);
3851f164be8cSYishai Hadas 		if (err)
3852f164be8cSYishai Hadas 			return err;
3853f164be8cSYishai Hadas 	}
3854f164be8cSYishai Hadas 
385596e2fd73SLeon Romanovsky 	dev->ib_dev.use_cq_dim = true;
385696e2fd73SLeon Romanovsky 
385716c1975fSMark Bloch 	return 0;
385816c1975fSMark Bloch }
385916c1975fSMark Bloch 
386096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
386196458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
386296458233SKamal Heib 	.query_port = mlx5_ib_query_port,
386396458233SKamal Heib };
386496458233SKamal Heib 
38658e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
386616c1975fSMark Bloch {
386796458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
38688e6efa3aSMark Bloch 	return 0;
38698e6efa3aSMark Bloch }
38708e6efa3aSMark Bloch 
387196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
387296458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
387396458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
3874d6fd59e1SParav Pandit 	.query_pkey = mlx5_ib_rep_query_pkey,
387596458233SKamal Heib };
387696458233SKamal Heib 
3877b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev)
38788e6efa3aSMark Bloch {
387996458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
38808e6efa3aSMark Bloch 	return 0;
38818e6efa3aSMark Bloch }
38828e6efa3aSMark Bloch 
388396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
388496458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
388596458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
388696458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
388796458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
388896458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
388996458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
3890c0a6b5ecSLeon Romanovsky 
3891c0a6b5ecSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_rwq_ind_table, mlx5_ib_rwq_ind_table,
3892c0a6b5ecSLeon Romanovsky 			   ib_rwq_ind_tbl),
389396458233SKamal Heib };
389496458233SKamal Heib 
38951e2b5a90SLeon Romanovsky static int mlx5_ib_roce_init(struct mlx5_ib_dev *dev)
38968e6efa3aSMark Bloch {
38971e2b5a90SLeon Romanovsky 	struct mlx5_core_dev *mdev = dev->mdev;
38981e2b5a90SLeon Romanovsky 	enum rdma_link_layer ll;
38991e2b5a90SLeon Romanovsky 	int port_type_cap;
39001fb7f897SMark Bloch 	u32 port_num = 0;
39011e2b5a90SLeon Romanovsky 	int err;
39027fd8aefbSDaniel Jurgens 
39031e2b5a90SLeon Romanovsky 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
39041e2b5a90SLeon Romanovsky 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
39051e2b5a90SLeon Romanovsky 
39061e2b5a90SLeon Romanovsky 	if (ll == IB_LINK_LAYER_ETHERNET) {
390796458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
39088e6efa3aSMark Bloch 
3909e3f1ed1fSLeon Romanovsky 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
3910e3f1ed1fSLeon Romanovsky 
391126628e2dSMark Bloch 		/* Register only for native ports */
39121e2b5a90SLeon Romanovsky 		err = mlx5_add_netdev_notifier(dev, port_num);
3913c446d9daSMark Bloch 		if (err)
39148e6efa3aSMark Bloch 			return err;
39158e6efa3aSMark Bloch 
3916e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
39178e6efa3aSMark Bloch 		if (err)
39188e6efa3aSMark Bloch 			goto cleanup;
39198e6efa3aSMark Bloch 	}
39208e6efa3aSMark Bloch 
392116c1975fSMark Bloch 	return 0;
39228e6efa3aSMark Bloch cleanup:
39231e2b5a90SLeon Romanovsky 	mlx5_remove_netdev_notifier(dev, port_num);
39248e6efa3aSMark Bloch 	return err;
392545bded2cSKamal Heib }
39266aec21f6SHaggai Eran 
39271e2b5a90SLeon Romanovsky static void mlx5_ib_roce_cleanup(struct mlx5_ib_dev *dev)
392816c1975fSMark Bloch {
392916c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
393016c1975fSMark Bloch 	enum rdma_link_layer ll;
393116c1975fSMark Bloch 	int port_type_cap;
39321fb7f897SMark Bloch 	u32 port_num;
39334a2da0b8SParav Pandit 
393416c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
393516c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
393616c1975fSMark Bloch 
393716c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
393816c1975fSMark Bloch 		mlx5_disable_eth(dev);
393916c1975fSMark Bloch 
39401e2b5a90SLeon Romanovsky 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
39411e2b5a90SLeon Romanovsky 		mlx5_remove_netdev_notifier(dev, port_num);
394216c1975fSMark Bloch 	}
394316c1975fSMark Bloch }
394416c1975fSMark Bloch 
394516c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
394616c1975fSMark Bloch {
394773eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
3948a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
394973eb8f03SGreg Kroah-Hartman 	return 0;
395016c1975fSMark Bloch }
395116c1975fSMark Bloch 
395216c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
395316c1975fSMark Bloch {
3954a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
3955a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
395616c1975fSMark Bloch }
395716c1975fSMark Bloch 
395816c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
395916c1975fSMark Bloch {
39605fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
3961444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
396216c1975fSMark Bloch }
396316c1975fSMark Bloch 
396416c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
396516c1975fSMark Bloch {
396616c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
396716c1975fSMark Bloch }
396816c1975fSMark Bloch 
3969fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
397016c1975fSMark Bloch {
397116c1975fSMark Bloch 	int err;
39725fe9dec0SEli Cohen 
39735fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
39745fe9dec0SEli Cohen 	if (err)
397516c1975fSMark Bloch 		return err;
39765fe9dec0SEli Cohen 
39775fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
39785fe9dec0SEli Cohen 	if (err)
39791c3aa6bdSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->bfreg);
39805fe9dec0SEli Cohen 
398116c1975fSMark Bloch 	return err;
398216c1975fSMark Bloch }
39830837e86aSMark Bloch 
3984fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
398516c1975fSMark Bloch {
398616c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
398716c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
398816c1975fSMark Bloch }
3989e126ba97SEli Cohen 
3990fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
399116c1975fSMark Bloch {
3992e349f858SJason Gunthorpe 	const char *name;
3993e349f858SJason Gunthorpe 
3994c446d9daSMark Bloch 	if (!mlx5_lag_is_active(dev->mdev))
3995e349f858SJason Gunthorpe 		name = "mlx5_%d";
3996e349f858SJason Gunthorpe 	else
3997e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
3998e0477b34SJason Gunthorpe 	return ib_register_device(&dev->ib_dev, name, &dev->mdev->pdev->dev);
399916c1975fSMark Bloch }
400016c1975fSMark Bloch 
4001fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
400242cea83fSMark Bloch {
40031e2b5a90SLeon Romanovsky 	int err;
40041e2b5a90SLeon Romanovsky 
400501137808SAharon Landau 	err = mlx5_mkey_cache_cleanup(dev);
40061e2b5a90SLeon Romanovsky 	if (err)
40071e2b5a90SLeon Romanovsky 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
40081e2b5a90SLeon Romanovsky 
400904876c12SAharon Landau 	mlx5r_umr_resource_cleanup(dev);
401042cea83fSMark Bloch }
401142cea83fSMark Bloch 
4012fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
401316c1975fSMark Bloch {
401416c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
401516c1975fSMark Bloch }
401616c1975fSMark Bloch 
4017fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
401816c1975fSMark Bloch {
40191e2b5a90SLeon Romanovsky 	int ret;
40201e2b5a90SLeon Romanovsky 
402104876c12SAharon Landau 	ret = mlx5r_umr_resource_init(dev);
402204876c12SAharon Landau 	if (ret)
402304876c12SAharon Landau 		return ret;
40241e2b5a90SLeon Romanovsky 
402501137808SAharon Landau 	ret = mlx5_mkey_cache_init(dev);
40261e2b5a90SLeon Romanovsky 	if (ret) {
40271e2b5a90SLeon Romanovsky 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
402804876c12SAharon Landau 		mlx5r_umr_resource_cleanup(dev);
40291e2b5a90SLeon Romanovsky 	}
40301e2b5a90SLeon Romanovsky 	return ret;
403116c1975fSMark Bloch }
403216c1975fSMark Bloch 
403316c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
403416c1975fSMark Bloch {
40351e2b5a90SLeon Romanovsky 	struct dentry *root;
403603404e8aSMaor Gottlieb 
40371e2b5a90SLeon Romanovsky 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
40381e2b5a90SLeon Romanovsky 		return 0;
40391e2b5a90SLeon Romanovsky 
40401e2b5a90SLeon Romanovsky 	mutex_init(&dev->delay_drop.lock);
40411e2b5a90SLeon Romanovsky 	dev->delay_drop.dev = dev;
40421e2b5a90SLeon Romanovsky 	dev->delay_drop.activate = false;
40431e2b5a90SLeon Romanovsky 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
40441e2b5a90SLeon Romanovsky 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
40451e2b5a90SLeon Romanovsky 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
40461e2b5a90SLeon Romanovsky 	atomic_set(&dev->delay_drop.events_cnt, 0);
40471e2b5a90SLeon Romanovsky 
40481e2b5a90SLeon Romanovsky 	if (!mlx5_debugfs_root)
40491e2b5a90SLeon Romanovsky 		return 0;
40501e2b5a90SLeon Romanovsky 
405166771a1cSMoshe Shemesh 	root = debugfs_create_dir("delay_drop", mlx5_debugfs_get_dev_root(dev->mdev));
40521e2b5a90SLeon Romanovsky 	dev->delay_drop.dir_debugfs = root;
40531e2b5a90SLeon Romanovsky 
40541e2b5a90SLeon Romanovsky 	debugfs_create_atomic_t("num_timeout_events", 0400, root,
40551e2b5a90SLeon Romanovsky 				&dev->delay_drop.events_cnt);
40561e2b5a90SLeon Romanovsky 	debugfs_create_atomic_t("num_rqs", 0400, root,
40571e2b5a90SLeon Romanovsky 				&dev->delay_drop.rqs_cnt);
40581e2b5a90SLeon Romanovsky 	debugfs_create_file("timeout", 0600, root, &dev->delay_drop,
40591e2b5a90SLeon Romanovsky 			    &fops_delay_drop_timeout);
406016c1975fSMark Bloch 	return 0;
406116c1975fSMark Bloch }
406216c1975fSMark Bloch 
406316c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
406416c1975fSMark Bloch {
40651e2b5a90SLeon Romanovsky 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
40661e2b5a90SLeon Romanovsky 		return;
40671e2b5a90SLeon Romanovsky 
40681e2b5a90SLeon Romanovsky 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
40691e2b5a90SLeon Romanovsky 	if (!dev->delay_drop.dir_debugfs)
40701e2b5a90SLeon Romanovsky 		return;
40711e2b5a90SLeon Romanovsky 
40721e2b5a90SLeon Romanovsky 	debugfs_remove_recursive(dev->delay_drop.dir_debugfs);
40731e2b5a90SLeon Romanovsky 	dev->delay_drop.dir_debugfs = NULL;
407416c1975fSMark Bloch }
407516c1975fSMark Bloch 
4076df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
4077df097a27SSaeed Mahameed {
4078df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
4079df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
4080df097a27SSaeed Mahameed 	return 0;
4081df097a27SSaeed Mahameed }
4082df097a27SSaeed Mahameed 
4083df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
4084df097a27SSaeed Mahameed {
4085df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
4086df097a27SSaeed Mahameed }
4087df097a27SSaeed Mahameed 
4088b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
408916c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
409016c1975fSMark Bloch 		      int stage)
409116c1975fSMark Bloch {
40924cca96a8SParav Pandit 	dev->ib_active = false;
40934cca96a8SParav Pandit 
409416c1975fSMark Bloch 	/* Number of stages to cleanup */
409516c1975fSMark Bloch 	while (stage) {
409616c1975fSMark Bloch 		stage--;
409716c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
409816c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
409916c1975fSMark Bloch 	}
41004a6dc855SMark Bloch 
4101da796ccbSMark Bloch 	kfree(dev->port);
41024a6dc855SMark Bloch 	ib_dealloc_device(&dev->ib_dev);
410316c1975fSMark Bloch }
410416c1975fSMark Bloch 
410593f82444SLeon Romanovsky int __mlx5_ib_add(struct mlx5_ib_dev *dev,
410616c1975fSMark Bloch 		  const struct mlx5_ib_profile *profile)
410716c1975fSMark Bloch {
410816c1975fSMark Bloch 	int err;
410916c1975fSMark Bloch 	int i;
411016c1975fSMark Bloch 
41118d93efb8SMark Bloch 	dev->profile = profile;
41128d93efb8SMark Bloch 
411316c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
411416c1975fSMark Bloch 		if (profile->stage[i].init) {
411516c1975fSMark Bloch 			err = profile->stage[i].init(dev);
411616c1975fSMark Bloch 			if (err)
411716c1975fSMark Bloch 				goto err_out;
411816c1975fSMark Bloch 		}
411916c1975fSMark Bloch 	}
412016c1975fSMark Bloch 
4121e126ba97SEli Cohen 	dev->ib_active = true;
412293f82444SLeon Romanovsky 	return 0;
4123e126ba97SEli Cohen 
412416c1975fSMark Bloch err_out:
412593f82444SLeon Romanovsky 	/* Clean up stages which were initialized */
412693f82444SLeon Romanovsky 	while (i) {
412793f82444SLeon Romanovsky 		i--;
412893f82444SLeon Romanovsky 		if (profile->stage[i].cleanup)
412993f82444SLeon Romanovsky 			profile->stage[i].cleanup(dev);
413093f82444SLeon Romanovsky 	}
413193f82444SLeon Romanovsky 	return -ENOMEM;
4132e126ba97SEli Cohen }
4133e126ba97SEli Cohen 
413416c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
413516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
413616c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
413716c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
4138f7c4ffdaSLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_FS,
4139f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_init,
4140f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_cleanup),
414116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
414216c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
4143f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
41448e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
41458e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
41468e6efa3aSMark Bloch 		     NULL),
414716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
41481e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_init,
41491e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_cleanup),
4150333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
4151333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
4152333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
4153f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4154f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
4155f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
415616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
41571e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_init,
41581e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_cleanup),
4159df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4160df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
4161df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
416216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
41631e2b5a90SLeon Romanovsky 		     mlx5_ib_odp_init_one,
41641e2b5a90SLeon Romanovsky 		     mlx5_ib_odp_cleanup_one),
416516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
416664825827SLeon Romanovsky 		     mlx5_ib_counters_init,
416764825827SLeon Romanovsky 		     mlx5_ib_counters_cleanup),
416816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
416916c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
417016c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
417116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
417216c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
417316c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
417416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
417516c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
417616c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
417742cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
417842cea83fSMark Bloch 		     NULL,
417942cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
418081773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4181d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_init,
4182d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_cleanup),
418316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
418416c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
418516c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
418642cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
418742cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
418842cea83fSMark Bloch 		     NULL),
418916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
419016c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
419116c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
4192b572ebe6SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4193b572ebe6SLeon Romanovsky 		     mlx5_ib_restrack_init,
4194b572ebe6SLeon Romanovsky 		     NULL),
419516c1975fSMark Bloch };
419616c1975fSMark Bloch 
4197b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = {
4198b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
4199b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
4200b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
4201f7c4ffdaSLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_FS,
4202f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_init,
4203f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_cleanup),
4204b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
4205b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
4206f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
4207b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
4208b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_non_default_cb,
4209b5ca15adSMark Bloch 		     NULL),
4210b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
42111e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_init,
42121e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_cleanup),
4213333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
4214333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
4215333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
4216f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4217f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
4218f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
4219b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
42201e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_init,
42211e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_cleanup),
4222df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4223df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
4224df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
4225b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
422664825827SLeon Romanovsky 		     mlx5_ib_counters_init,
422764825827SLeon Romanovsky 		     mlx5_ib_counters_cleanup),
422879db784eSParav Pandit 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
422979db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_init,
423079db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_cleanup),
4231b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
4232b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
4233b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
4234b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
4235b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
4236b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
423703fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
423803fe2debSDavid S. Miller 		     NULL,
423903fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
42407f575103SMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4241d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_init,
4242d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_cleanup),
4243b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
4244b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
4245b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
424603fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
424703fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
424803fe2debSDavid S. Miller 		     NULL),
4249b572ebe6SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4250b572ebe6SLeon Romanovsky 		     mlx5_ib_restrack_init,
4251b572ebe6SLeon Romanovsky 		     NULL),
4252b5ca15adSMark Bloch };
4253b5ca15adSMark Bloch 
425493f82444SLeon Romanovsky static int mlx5r_mp_probe(struct auxiliary_device *adev,
425593f82444SLeon Romanovsky 			  const struct auxiliary_device_id *id)
425632f69e4bSDaniel Jurgens {
425793f82444SLeon Romanovsky 	struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
425893f82444SLeon Romanovsky 	struct mlx5_core_dev *mdev = idev->mdev;
425932f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
426032f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
426132f69e4bSDaniel Jurgens 	bool bound = false;
426232f69e4bSDaniel Jurgens 	int err;
426332f69e4bSDaniel Jurgens 
426432f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
426532f69e4bSDaniel Jurgens 	if (!mpi)
426693f82444SLeon Romanovsky 		return -ENOMEM;
426732f69e4bSDaniel Jurgens 
426832f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
426932f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
427032f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
427132f69e4bSDaniel Jurgens 	if (err) {
427232f69e4bSDaniel Jurgens 		kfree(mpi);
427393f82444SLeon Romanovsky 		return err;
427432f69e4bSDaniel Jurgens 	}
427532f69e4bSDaniel Jurgens 
427632f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
427732f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
427832f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
427932f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
428032f69e4bSDaniel Jurgens 
428132f69e4bSDaniel Jurgens 		if (bound) {
428232f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
428397f30d32SMaor Gottlieb 			mpi->ibdev->ib_active = true;
428432f69e4bSDaniel Jurgens 			break;
428532f69e4bSDaniel Jurgens 		}
428632f69e4bSDaniel Jurgens 	}
428732f69e4bSDaniel Jurgens 
428832f69e4bSDaniel Jurgens 	if (!bound) {
428932f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
4290c42260f1SVu Pham 		dev_dbg(mdev->device,
4291c42260f1SVu Pham 			"no suitable IB device found to bind to, added to unaffiliated list.\n");
429232f69e4bSDaniel Jurgens 	}
429332f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
429432f69e4bSDaniel Jurgens 
429527963d3dSDavid E. Box 	auxiliary_set_drvdata(adev, mpi);
429693f82444SLeon Romanovsky 	return 0;
429732f69e4bSDaniel Jurgens }
429832f69e4bSDaniel Jurgens 
429993f82444SLeon Romanovsky static void mlx5r_mp_remove(struct auxiliary_device *adev)
430016c1975fSMark Bloch {
430193f82444SLeon Romanovsky 	struct mlx5_ib_multiport_info *mpi;
430293f82444SLeon Romanovsky 
430327963d3dSDavid E. Box 	mpi = auxiliary_get_drvdata(adev);
430493f82444SLeon Romanovsky 	mutex_lock(&mlx5_ib_multiport_mutex);
430593f82444SLeon Romanovsky 	if (mpi->ibdev)
430693f82444SLeon Romanovsky 		mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
4307da78fe5fSMaor Gottlieb 	else
430893f82444SLeon Romanovsky 		list_del(&mpi->list);
430993f82444SLeon Romanovsky 	mutex_unlock(&mlx5_ib_multiport_mutex);
431093f82444SLeon Romanovsky 	kfree(mpi);
431193f82444SLeon Romanovsky }
431293f82444SLeon Romanovsky 
431393f82444SLeon Romanovsky static int mlx5r_probe(struct auxiliary_device *adev,
431493f82444SLeon Romanovsky 		       const struct auxiliary_device_id *id)
431593f82444SLeon Romanovsky {
431693f82444SLeon Romanovsky 	struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
431793f82444SLeon Romanovsky 	struct mlx5_core_dev *mdev = idev->mdev;
431894de879cSMichael Guralnik 	const struct mlx5_ib_profile *profile;
431993f82444SLeon Romanovsky 	int port_type_cap, num_ports, ret;
432032f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
4321b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
4322f0666f1fSBodong Wang 
432332f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
432432f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
432532f69e4bSDaniel Jurgens 
4326da796ccbSMark Bloch 	num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
4327da796ccbSMark Bloch 			MLX5_CAP_GEN(mdev, num_vhca_ports));
4328459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
4329b5ca15adSMark Bloch 	if (!dev)
433093f82444SLeon Romanovsky 		return -ENOMEM;
4331da796ccbSMark Bloch 	dev->port = kcalloc(num_ports, sizeof(*dev->port),
4332da796ccbSMark Bloch 			     GFP_KERNEL);
4333da796ccbSMark Bloch 	if (!dev->port) {
4334a5c9c299SParav Pandit 		ib_dealloc_device(&dev->ib_dev);
433593f82444SLeon Romanovsky 		return -ENOMEM;
4336da796ccbSMark Bloch 	}
4337b5ca15adSMark Bloch 
4338b5ca15adSMark Bloch 	dev->mdev = mdev;
4339da796ccbSMark Bloch 	dev->num_ports = num_ports;
4340b5ca15adSMark Bloch 
43419ca05b0fSMaher Sanalla 	if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_get_roce_state(mdev))
434294de879cSMichael Guralnik 		profile = &raw_eth_profile;
434394de879cSMichael Guralnik 	else
434494de879cSMichael Guralnik 		profile = &pf_profile;
434594de879cSMichael Guralnik 
434693f82444SLeon Romanovsky 	ret = __mlx5_ib_add(dev, profile);
434793f82444SLeon Romanovsky 	if (ret) {
434893f82444SLeon Romanovsky 		kfree(dev->port);
434993f82444SLeon Romanovsky 		ib_dealloc_device(&dev->ib_dev);
435093f82444SLeon Romanovsky 		return ret;
435116c1975fSMark Bloch 	}
435216c1975fSMark Bloch 
435327963d3dSDavid E. Box 	auxiliary_set_drvdata(adev, dev);
435493f82444SLeon Romanovsky 	return 0;
435593f82444SLeon Romanovsky }
435693f82444SLeon Romanovsky 
435793f82444SLeon Romanovsky static void mlx5r_remove(struct auxiliary_device *adev)
4358e126ba97SEli Cohen {
435932f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
43606aec21f6SHaggai Eran 
436127963d3dSDavid E. Box 	dev = auxiliary_get_drvdata(adev);
436216c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
4363e126ba97SEli Cohen }
4364e126ba97SEli Cohen 
436593f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_mp_id_table[] = {
436693f82444SLeon Romanovsky 	{ .name = MLX5_ADEV_NAME ".multiport", },
436793f82444SLeon Romanovsky 	{},
436893f82444SLeon Romanovsky };
436993f82444SLeon Romanovsky 
437093f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_id_table[] = {
437193f82444SLeon Romanovsky 	{ .name = MLX5_ADEV_NAME ".rdma", },
437293f82444SLeon Romanovsky 	{},
437393f82444SLeon Romanovsky };
437493f82444SLeon Romanovsky 
437593f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_mp_id_table);
437693f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_id_table);
437793f82444SLeon Romanovsky 
437893f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_mp_driver = {
437993f82444SLeon Romanovsky 	.name = "multiport",
438093f82444SLeon Romanovsky 	.probe = mlx5r_mp_probe,
438193f82444SLeon Romanovsky 	.remove = mlx5r_mp_remove,
438293f82444SLeon Romanovsky 	.id_table = mlx5r_mp_id_table,
438393f82444SLeon Romanovsky };
438493f82444SLeon Romanovsky 
438593f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_driver = {
438693f82444SLeon Romanovsky 	.name = "rdma",
438793f82444SLeon Romanovsky 	.probe = mlx5r_probe,
438893f82444SLeon Romanovsky 	.remove = mlx5r_remove,
438993f82444SLeon Romanovsky 	.id_table = mlx5r_id_table,
4390e126ba97SEli Cohen };
4391e126ba97SEli Cohen 
4392e126ba97SEli Cohen static int __init mlx5_ib_init(void)
4393e126ba97SEli Cohen {
439493f82444SLeon Romanovsky 	int ret;
43956aec21f6SHaggai Eran 
43968010d74bSJason Gunthorpe 	xlt_emergency_page = (void *)__get_free_page(GFP_KERNEL);
4397c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
4398d69a24e0SDaniel Jurgens 		return -ENOMEM;
4399d69a24e0SDaniel Jurgens 
4400c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
4401c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
44028010d74bSJason Gunthorpe 		free_page((unsigned long)xlt_emergency_page);
4403c44ef998SIlya Lesokhin 		return -ENOMEM;
4404c44ef998SIlya Lesokhin 	}
4405c44ef998SIlya Lesokhin 
4406*312b8f79SMark Zhang 	ret = mlx5_ib_qp_event_init();
4407*312b8f79SMark Zhang 	if (ret)
4408*312b8f79SMark Zhang 		goto qp_event_err;
4409*312b8f79SMark Zhang 
441081713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
441193f82444SLeon Romanovsky 	ret = mlx5r_rep_init();
441293f82444SLeon Romanovsky 	if (ret)
441393f82444SLeon Romanovsky 		goto rep_err;
441493f82444SLeon Romanovsky 	ret = auxiliary_driver_register(&mlx5r_mp_driver);
441593f82444SLeon Romanovsky 	if (ret)
441693f82444SLeon Romanovsky 		goto mp_err;
441793f82444SLeon Romanovsky 	ret = auxiliary_driver_register(&mlx5r_driver);
441893f82444SLeon Romanovsky 	if (ret)
441993f82444SLeon Romanovsky 		goto drv_err;
442093f82444SLeon Romanovsky 	return 0;
44219603b61dSJack Morgenstein 
442293f82444SLeon Romanovsky drv_err:
442393f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_mp_driver);
442493f82444SLeon Romanovsky mp_err:
442593f82444SLeon Romanovsky 	mlx5r_rep_cleanup();
442693f82444SLeon Romanovsky rep_err:
4427*312b8f79SMark Zhang 	mlx5_ib_qp_event_cleanup();
4428*312b8f79SMark Zhang qp_event_err:
442993f82444SLeon Romanovsky 	destroy_workqueue(mlx5_ib_event_wq);
443093f82444SLeon Romanovsky 	free_page((unsigned long)xlt_emergency_page);
443193f82444SLeon Romanovsky 	return ret;
4432e126ba97SEli Cohen }
4433e126ba97SEli Cohen 
4434e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
4435e126ba97SEli Cohen {
443693f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_driver);
443793f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_mp_driver);
443893f82444SLeon Romanovsky 	mlx5r_rep_cleanup();
443993f82444SLeon Romanovsky 
4440*312b8f79SMark Zhang 	mlx5_ib_qp_event_cleanup();
4441d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
44428010d74bSJason Gunthorpe 	free_page((unsigned long)xlt_emergency_page);
4443e126ba97SEli Cohen }
4444e126ba97SEli Cohen 
4445e126ba97SEli Cohen module_init(mlx5_ib_init);
4446e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
4447