xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 6387f269d84e6e149499408c4d1fc805017729b2)
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>
49758ce14aSPatrisious Haddad #include "macsec.h"
508c84660bSMatan Barak 
518c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
528c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
53e126ba97SEli Cohen 
54e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
55daeee976SShay Drory MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) IB driver");
56e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
57e126ba97SEli Cohen 
58d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
59d69a24e0SDaniel Jurgens 	struct work_struct	work;
60df097a27SSaeed Mahameed 	union {
61df097a27SSaeed Mahameed 		struct mlx5_ib_dev	      *dev;
62df097a27SSaeed Mahameed 		struct mlx5_ib_multiport_info *mpi;
63df097a27SSaeed Mahameed 	};
64df097a27SSaeed Mahameed 	bool			is_slave;
65134e9349SSaeed Mahameed 	unsigned int		event;
66df097a27SSaeed Mahameed 	void			*param;
67d69a24e0SDaniel Jurgens };
68d69a24e0SDaniel Jurgens 
69da7525d2SEran Ben Elisha enum {
70da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
71da7525d2SEran Ben Elisha };
721b5daf11SMajd Dibbiny 
73d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
7432f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
7532f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
7632f69e4bSDaniel Jurgens /*
7732f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
7832f69e4bSDaniel Jurgens  */
7932f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
8032f69e4bSDaniel Jurgens 
8132f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
8232f69e4bSDaniel Jurgens {
8332f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
8432f69e4bSDaniel Jurgens 
8532f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
8632f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
8732f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
8832f69e4bSDaniel Jurgens 	return dev;
8932f69e4bSDaniel Jurgens }
9032f69e4bSDaniel Jurgens 
911b5daf11SMajd Dibbiny static enum rdma_link_layer
92ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
931b5daf11SMajd Dibbiny {
94ebd61f68SAchiad Shochat 	switch (port_type_cap) {
951b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
961b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
971b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
981b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
991b5daf11SMajd Dibbiny 	default:
1001b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1011b5daf11SMajd Dibbiny 	}
1021b5daf11SMajd Dibbiny }
1031b5daf11SMajd Dibbiny 
104ebd61f68SAchiad Shochat static enum rdma_link_layer
1051fb7f897SMark Bloch mlx5_ib_port_link_layer(struct ib_device *device, u32 port_num)
106ebd61f68SAchiad Shochat {
107ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
108ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
109ebd61f68SAchiad Shochat 
110ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
111ebd61f68SAchiad Shochat }
112ebd61f68SAchiad Shochat 
113fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
1141fb7f897SMark Bloch 			  u32 port_num,
115fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
116fd65f1b8SMoni Shoua {
117fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
118fd65f1b8SMoni Shoua 	int ret;
119fd65f1b8SMoni Shoua 
120fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1213023a1e9SKamal Heib 	ret = ibdev->ops.query_port(ibdev, port_num, &attr);
122fd65f1b8SMoni Shoua 	if (!ret)
123fd65f1b8SMoni Shoua 		*state = attr.state;
124fd65f1b8SMoni Shoua 	return ret;
125fd65f1b8SMoni Shoua }
126fd65f1b8SMoni Shoua 
12735b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
12835b0aa67SMark Bloch 					   struct net_device *ndev,
129c446d9daSMark Bloch 					   struct net_device *upper,
1301fb7f897SMark Bloch 					   u32 *port_num)
13135b0aa67SMark Bloch {
13235b0aa67SMark Bloch 	struct net_device *rep_ndev;
13335b0aa67SMark Bloch 	struct mlx5_ib_port *port;
13435b0aa67SMark Bloch 	int i;
13535b0aa67SMark Bloch 
13635b0aa67SMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
13735b0aa67SMark Bloch 		port  = &dev->port[i];
13835b0aa67SMark Bloch 		if (!port->rep)
13935b0aa67SMark Bloch 			continue;
14035b0aa67SMark Bloch 
141c446d9daSMark Bloch 		if (upper == ndev && port->rep->vport == MLX5_VPORT_UPLINK) {
142c446d9daSMark Bloch 			*port_num = i + 1;
143c446d9daSMark Bloch 			return &port->roce;
144c446d9daSMark Bloch 		}
145c446d9daSMark Bloch 
146c446d9daSMark Bloch 		if (upper && port->rep->vport == MLX5_VPORT_UPLINK)
147c446d9daSMark Bloch 			continue;
148c446d9daSMark Bloch 
14935b0aa67SMark Bloch 		read_lock(&port->roce.netdev_lock);
150658cfcebSMark Bloch 		rep_ndev = mlx5_ib_get_rep_netdev(port->rep->esw,
15135b0aa67SMark Bloch 						  port->rep->vport);
15235b0aa67SMark Bloch 		if (rep_ndev == ndev) {
15335b0aa67SMark Bloch 			read_unlock(&port->roce.netdev_lock);
15435b0aa67SMark Bloch 			*port_num = i + 1;
15535b0aa67SMark Bloch 			return &port->roce;
15635b0aa67SMark Bloch 		}
15735b0aa67SMark Bloch 		read_unlock(&port->roce.netdev_lock);
15835b0aa67SMark Bloch 	}
15935b0aa67SMark Bloch 
16035b0aa67SMark Bloch 	return NULL;
16135b0aa67SMark Bloch }
16235b0aa67SMark Bloch 
163fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
164fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
165fc24fc5eSAchiad Shochat {
1667fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
167fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1681fb7f897SMark Bloch 	u32 port_num = roce->native_port_num;
1697fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1707fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1717fd8aefbSDaniel Jurgens 
1727fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
17332f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
17432f69e4bSDaniel Jurgens 	if (!mdev)
17532f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
176fc24fc5eSAchiad Shochat 
1775ec8c83eSAviv Heller 	switch (event) {
1785ec8c83eSAviv Heller 	case NETDEV_REGISTER:
17935b0aa67SMark Bloch 		/* Should already be registered during the load */
18035b0aa67SMark Bloch 		if (ibdev->is_rep)
18135b0aa67SMark Bloch 			break;
1827fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
183dce45af5SLinus Torvalds 		if (ndev->dev.parent == mdev->device)
184842a9c83SOr Gerlitz 			roce->netdev = ndev;
1857fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
1865ec8c83eSAviv Heller 		break;
1875ec8c83eSAviv Heller 
188842a9c83SOr Gerlitz 	case NETDEV_UNREGISTER:
18935b0aa67SMark Bloch 		/* In case of reps, ib device goes away before the netdevs */
190842a9c83SOr Gerlitz 		write_lock(&roce->netdev_lock);
191842a9c83SOr Gerlitz 		if (roce->netdev == ndev)
192842a9c83SOr Gerlitz 			roce->netdev = NULL;
193842a9c83SOr Gerlitz 		write_unlock(&roce->netdev_lock);
194842a9c83SOr Gerlitz 		break;
195842a9c83SOr Gerlitz 
196fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
1975ec8c83eSAviv Heller 	case NETDEV_UP:
19888621dfeSAviv Heller 	case NETDEV_DOWN: {
1997fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
20088621dfeSAviv Heller 		struct net_device *upper = NULL;
20188621dfeSAviv Heller 
20288621dfeSAviv Heller 		if (lag_ndev) {
20388621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
20488621dfeSAviv Heller 			dev_put(lag_ndev);
20588621dfeSAviv Heller 		}
20688621dfeSAviv Heller 
20735b0aa67SMark Bloch 		if (ibdev->is_rep)
208c446d9daSMark Bloch 			roce = mlx5_get_rep_roce(ibdev, ndev, upper, &port_num);
20935b0aa67SMark Bloch 		if (!roce)
21035b0aa67SMark Bloch 			return NOTIFY_DONE;
211c446d9daSMark Bloch 		if ((upper == ndev ||
212c446d9daSMark Bloch 		     ((!upper || ibdev->is_rep) && ndev == roce->netdev)) &&
213c446d9daSMark Bloch 		    ibdev->ib_active) {
214626bc02dSBart Van Assche 			struct ib_event ibev = { };
215fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2165ec8c83eSAviv Heller 
2177fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2187fd8aefbSDaniel Jurgens 					   &port_state))
2197fd8aefbSDaniel Jurgens 				goto done;
220fd65f1b8SMoni Shoua 
2217fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2227fd8aefbSDaniel Jurgens 				goto done;
223fd65f1b8SMoni Shoua 
2247fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2255ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
226fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
227fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
228fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
229fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
230fd65f1b8SMoni Shoua 			else
2317fd8aefbSDaniel Jurgens 				goto done;
232fd65f1b8SMoni Shoua 
2337fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2345ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2355ec8c83eSAviv Heller 		}
2365ec8c83eSAviv Heller 		break;
23788621dfeSAviv Heller 	}
2385ec8c83eSAviv Heller 
2395ec8c83eSAviv Heller 	default:
2405ec8c83eSAviv Heller 		break;
2415ec8c83eSAviv Heller 	}
2427fd8aefbSDaniel Jurgens done:
24332f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
244fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
245fc24fc5eSAchiad Shochat }
246fc24fc5eSAchiad Shochat 
247fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
2481fb7f897SMark Bloch 					     u32 port_num)
249fc24fc5eSAchiad Shochat {
250fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
251fc24fc5eSAchiad Shochat 	struct net_device *ndev;
25232f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
253fc24fc5eSAchiad Shochat 
25432f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
25532f69e4bSDaniel Jurgens 	if (!mdev)
25632f69e4bSDaniel Jurgens 		return NULL;
25732f69e4bSDaniel Jurgens 
25832f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
25988621dfeSAviv Heller 	if (ndev)
26032f69e4bSDaniel Jurgens 		goto out;
26188621dfeSAviv Heller 
262fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
263fc24fc5eSAchiad Shochat 	 */
26495579e78SMark Bloch 	read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
26595579e78SMark Bloch 	ndev = ibdev->port[port_num - 1].roce.netdev;
266fc24fc5eSAchiad Shochat 	if (ndev)
267fc24fc5eSAchiad Shochat 		dev_hold(ndev);
26895579e78SMark Bloch 	read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
269fc24fc5eSAchiad Shochat 
27032f69e4bSDaniel Jurgens out:
27132f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
272fc24fc5eSAchiad Shochat 	return ndev;
273fc24fc5eSAchiad Shochat }
274fc24fc5eSAchiad Shochat 
27532f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
2761fb7f897SMark Bloch 						   u32 ib_port_num,
2771fb7f897SMark Bloch 						   u32 *native_port_num)
27832f69e4bSDaniel Jurgens {
27932f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
28032f69e4bSDaniel Jurgens 							  ib_port_num);
28132f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
28232f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
28332f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
28432f69e4bSDaniel Jurgens 
285210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
286210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
287210b1f78SMark Bloch 		if (native_port_num)
288210b1f78SMark Bloch 			*native_port_num = ib_port_num;
289210b1f78SMark Bloch 		return ibdev->mdev;
290210b1f78SMark Bloch 	}
291210b1f78SMark Bloch 
29232f69e4bSDaniel Jurgens 	if (native_port_num)
29332f69e4bSDaniel Jurgens 		*native_port_num = 1;
29432f69e4bSDaniel Jurgens 
29532f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
29632f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
29732f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
29832f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
29932f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
30032f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
30132f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
30232f69e4bSDaniel Jurgens 		 */
30332f69e4bSDaniel Jurgens 		if (!mpi->is_master)
30432f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
30532f69e4bSDaniel Jurgens 	}
30632f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
30732f69e4bSDaniel Jurgens 
30832f69e4bSDaniel Jurgens 	return mdev;
30932f69e4bSDaniel Jurgens }
31032f69e4bSDaniel Jurgens 
3111fb7f897SMark Bloch void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u32 port_num)
31232f69e4bSDaniel Jurgens {
31332f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
31432f69e4bSDaniel Jurgens 							  port_num);
31532f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
31632f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
31732f69e4bSDaniel Jurgens 
31832f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
31932f69e4bSDaniel Jurgens 		return;
32032f69e4bSDaniel Jurgens 
32132f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
32232f69e4bSDaniel Jurgens 
32332f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
32432f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
32532f69e4bSDaniel Jurgens 	if (mpi->is_master)
32632f69e4bSDaniel Jurgens 		goto out;
32732f69e4bSDaniel Jurgens 
32832f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
32932f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
33032f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
33132f69e4bSDaniel Jurgens out:
33232f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
33332f69e4bSDaniel Jurgens }
33432f69e4bSDaniel Jurgens 
335639bf441SAharon Landau static int translate_eth_legacy_proto_oper(u32 eth_proto_oper,
336639bf441SAharon Landau 					   u16 *active_speed, u8 *active_width)
337f1b65df5SNoa Osherovich {
338f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
339f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
340f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
341f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
342f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
343f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
344f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
345f1b65df5SNoa Osherovich 		break;
346f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
347f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
348f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
349f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
350f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
351f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
352f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
353f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
354f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
355f1b65df5SNoa Osherovich 		break;
356f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
357f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
358f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
359f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
360f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
361f1b65df5SNoa Osherovich 		break;
362f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
363f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
364f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
365f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
366f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
367f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
368f1b65df5SNoa Osherovich 		break;
369f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
370f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
371f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
372f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
373f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
374f1b65df5SNoa Osherovich 		break;
375f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
376f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
377f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
378f1b65df5SNoa Osherovich 		break;
379f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
380f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
381f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
382f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
383f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
384f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
385f1b65df5SNoa Osherovich 		break;
386f1b65df5SNoa Osherovich 	default:
387f1b65df5SNoa Osherovich 		return -EINVAL;
388f1b65df5SNoa Osherovich 	}
389f1b65df5SNoa Osherovich 
390f1b65df5SNoa Osherovich 	return 0;
391f1b65df5SNoa Osherovich }
392f1b65df5SNoa Osherovich 
393639bf441SAharon Landau static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u16 *active_speed,
39408e8676fSAya Levin 					u8 *active_width)
39508e8676fSAya Levin {
39608e8676fSAya Levin 	switch (eth_proto_oper) {
39708e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
39808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
39908e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
40008e8676fSAya Levin 		*active_speed = IB_SPEED_SDR;
40108e8676fSAya Levin 		break;
40208e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
40308e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
40408e8676fSAya Levin 		*active_speed = IB_SPEED_DDR;
40508e8676fSAya Levin 		break;
40608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
40708e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
40808e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
40908e8676fSAya Levin 		break;
41008e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
41108e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
41208e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
41308e8676fSAya Levin 		break;
41408e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
41508e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
41608e8676fSAya Levin 		*active_speed = IB_SPEED_EDR;
41708e8676fSAya Levin 		break;
41808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
419cd272875SAya Levin 		*active_width = IB_WIDTH_2X;
420cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
421cd272875SAya Levin 		break;
42208e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
42308e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
42408e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
42508e8676fSAya Levin 		break;
426cd272875SAya Levin 	case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
427cd272875SAya Levin 		*active_width = IB_WIDTH_4X;
428cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
429cd272875SAya Levin 		break;
43008e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
43108e8676fSAya Levin 		*active_width = IB_WIDTH_2X;
43208e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
43308e8676fSAya Levin 		break;
434f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_100GAUI_1_100GBASE_CR_KR):
435f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_1X;
436f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
437f946e45fSMeir Lichtinger 		break;
43808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
43908e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
44008e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
44108e8676fSAya Levin 		break;
442f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_200GAUI_2_200GBASE_CR2_KR2):
443f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_2X;
444f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
445f946e45fSMeir Lichtinger 		break;
44688c9483fSMaher Sanalla 	case MLX5E_PROT_MASK(MLX5E_400GAUI_8):
44788c9483fSMaher Sanalla 		*active_width = IB_WIDTH_8X;
44888c9483fSMaher Sanalla 		*active_speed = IB_SPEED_HDR;
44988c9483fSMaher Sanalla 		break;
450f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_400GAUI_4_400GBASE_CR4_KR4):
451f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_4X;
452f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
453f946e45fSMeir Lichtinger 		break;
45408e8676fSAya Levin 	default:
45508e8676fSAya Levin 		return -EINVAL;
45608e8676fSAya Levin 	}
45708e8676fSAya Levin 
45808e8676fSAya Levin 	return 0;
45908e8676fSAya Levin }
46008e8676fSAya Levin 
461639bf441SAharon Landau static int translate_eth_proto_oper(u32 eth_proto_oper, u16 *active_speed,
46208e8676fSAya Levin 				    u8 *active_width, bool ext)
46308e8676fSAya Levin {
46408e8676fSAya Levin 	return ext ?
46508e8676fSAya Levin 		translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
46608e8676fSAya Levin 					     active_width) :
46708e8676fSAya Levin 		translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
46808e8676fSAya Levin 						active_width);
46908e8676fSAya Levin }
47008e8676fSAya Levin 
4711fb7f897SMark Bloch static int mlx5_query_port_roce(struct ib_device *device, u32 port_num,
4723f89a643SAchiad Shochat 				struct ib_port_attr *props)
4733f89a643SAchiad Shochat {
4743f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
475bc4e12ffSAya Levin 	u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
476da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
47788621dfeSAviv Heller 	struct net_device *ndev, *upper;
4783f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
479b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
480f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
4811fb7f897SMark Bloch 	u32 mdev_port_num;
48208e8676fSAya Levin 	bool ext;
483095b0927SIlan Tayari 	int err;
4843f89a643SAchiad Shochat 
485b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
486b3cbd6f0SDaniel Jurgens 	if (!mdev) {
487b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
488b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
489b3cbd6f0SDaniel Jurgens 		 */
490b3cbd6f0SDaniel Jurgens 		put_mdev = false;
491b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
492b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
493b3cbd6f0SDaniel Jurgens 		port_num = 1;
494b3cbd6f0SDaniel Jurgens 	}
495b3cbd6f0SDaniel Jurgens 
496f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
497f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
49826628e2dSMark Bloch 	 * Use native port in case of reps
49950f22fd8SNoa Osherovich 	 */
50026628e2dSMark Bloch 	if (dev->is_rep)
50126628e2dSMark Bloch 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
50226628e2dSMark Bloch 					   1);
50326628e2dSMark Bloch 	else
504bc4e12ffSAya Levin 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
505b3cbd6f0SDaniel Jurgens 					   mdev_port_num);
506095b0927SIlan Tayari 	if (err)
507b3cbd6f0SDaniel Jurgens 		goto out;
508530c8632SAya Levin 	ext = !!MLX5_GET_ETH_PROTO(ptys_reg, out, true, eth_proto_capability);
50908e8676fSAya Levin 	eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
510f1b65df5SNoa Osherovich 
5117672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
5127672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
5137672ed33SHonggang Li 
514f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
51508e8676fSAya Levin 				 &props->active_width, ext);
5163f89a643SAchiad Shochat 
5177852546fSMaor Gottlieb 	if (!dev->is_rep && dev->mdev->roce.roce_en) {
5187a58779eSParav Pandit 		u16 qkey_viol_cntr;
5197a58779eSParav Pandit 
5203f89a643SAchiad Shochat 		props->port_cap_flags |= IB_PORT_CM_SUP;
5212f944c0fSJason Gunthorpe 		props->ip_gids = true;
5223f89a643SAchiad Shochat 		props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
5233f89a643SAchiad Shochat 						   roce_address_table_size);
5247a58779eSParav Pandit 		mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
5257a58779eSParav Pandit 		props->qkey_viol_cntr = qkey_viol_cntr;
5267a58779eSParav Pandit 	}
5273f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
5283f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
5293f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
5303f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
53172a7720fSKamal Heib 	props->phys_state       = IB_PORT_PHYS_STATE_DISABLED;
5323f89a643SAchiad Shochat 
533b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
534b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
535b3cbd6f0SDaniel Jurgens 		goto out;
536b3cbd6f0SDaniel Jurgens 
5373f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
5383f89a643SAchiad Shochat 	if (!ndev)
539b3cbd6f0SDaniel Jurgens 		goto out;
5403f89a643SAchiad Shochat 
5417c34ec19SAviv Heller 	if (dev->lag_active) {
54288621dfeSAviv Heller 		rcu_read_lock();
54388621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
54488621dfeSAviv Heller 		if (upper) {
54588621dfeSAviv Heller 			dev_put(ndev);
54688621dfeSAviv Heller 			ndev = upper;
54788621dfeSAviv Heller 			dev_hold(ndev);
54888621dfeSAviv Heller 		}
54988621dfeSAviv Heller 		rcu_read_unlock();
55088621dfeSAviv Heller 	}
55188621dfeSAviv Heller 
5523f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
5533f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
55472a7720fSKamal Heib 		props->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
5553f89a643SAchiad Shochat 	}
5563f89a643SAchiad Shochat 
5573f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
5583f89a643SAchiad Shochat 
5593f89a643SAchiad Shochat 	dev_put(ndev);
5603f89a643SAchiad Shochat 
5613f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
562b3cbd6f0SDaniel Jurgens out:
563b3cbd6f0SDaniel Jurgens 	if (put_mdev)
564b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
565b3cbd6f0SDaniel Jurgens 	return err;
5663f89a643SAchiad Shochat }
5673f89a643SAchiad Shochat 
568758ce14aSPatrisious Haddad int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num,
569095b0927SIlan Tayari 		  unsigned int index, const union ib_gid *gid,
570095b0927SIlan Tayari 		  const struct ib_gid_attr *attr)
5713cca2606SAchiad Shochat {
572dedbc2d3SParav Pandit 	enum ib_gid_type gid_type;
573a70c0739SParav Pandit 	u16 vlan_id = 0xffff;
574095b0927SIlan Tayari 	u8 roce_version = 0;
575095b0927SIlan Tayari 	u8 roce_l3_type = 0;
576095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
577a70c0739SParav Pandit 	int ret;
5783cca2606SAchiad Shochat 
579095b0927SIlan Tayari 	gid_type = attr->gid_type;
580dedbc2d3SParav Pandit 	if (gid) {
581a70c0739SParav Pandit 		ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
582a70c0739SParav Pandit 		if (ret)
583a70c0739SParav Pandit 			return ret;
5843cca2606SAchiad Shochat 	}
5853cca2606SAchiad Shochat 
586095b0927SIlan Tayari 	switch (gid_type) {
5871c15b4f2SAvihai Horon 	case IB_GID_TYPE_ROCE:
588095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
5893cca2606SAchiad Shochat 		break;
5903cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
591095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
592dedbc2d3SParav Pandit 		if (gid && ipv6_addr_v4mapped((void *)gid))
593095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
594095b0927SIlan Tayari 		else
595095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
5963cca2606SAchiad Shochat 		break;
5973cca2606SAchiad Shochat 
5983cca2606SAchiad Shochat 	default:
599095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
6003cca2606SAchiad Shochat 	}
6013cca2606SAchiad Shochat 
602095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
603cf34e1feSParav Pandit 				      roce_l3_type, gid->raw, mac,
604a70c0739SParav Pandit 				      vlan_id < VLAN_CFI_MASK, vlan_id,
605cf34e1feSParav Pandit 				      port_num);
6063cca2606SAchiad Shochat }
6073cca2606SAchiad Shochat 
608f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
6093cca2606SAchiad Shochat 			   __always_unused void **context)
6103cca2606SAchiad Shochat {
611758ce14aSPatrisious Haddad 	int ret;
612758ce14aSPatrisious Haddad 
613758ce14aSPatrisious Haddad 	ret = mlx5r_add_gid_macsec_operations(attr);
614758ce14aSPatrisious Haddad 	if (ret)
615758ce14aSPatrisious Haddad 		return ret;
616758ce14aSPatrisious Haddad 
617414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
618f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
6193cca2606SAchiad Shochat }
6203cca2606SAchiad Shochat 
621414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
622414448d2SParav Pandit 			   __always_unused void **context)
6233cca2606SAchiad Shochat {
624758ce14aSPatrisious Haddad 	int ret;
625758ce14aSPatrisious Haddad 
626758ce14aSPatrisious Haddad 	ret = set_roce_addr(to_mdev(attr->device), attr->port_num,
627dedbc2d3SParav Pandit 			    attr->index, NULL, attr);
628758ce14aSPatrisious Haddad 	if (ret)
629758ce14aSPatrisious Haddad 		return ret;
630758ce14aSPatrisious Haddad 
631758ce14aSPatrisious Haddad 	mlx5r_del_gid_macsec_operations(attr);
632758ce14aSPatrisious Haddad 	return 0;
6333cca2606SAchiad Shochat }
6343cca2606SAchiad Shochat 
6355ac55dfcSMark Zhang __be16 mlx5_get_roce_udp_sport_min(const struct mlx5_ib_dev *dev,
63647ec3866SParav Pandit 				   const struct ib_gid_attr *attr)
6372811ba51SAchiad Shochat {
63847ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
6392811ba51SAchiad Shochat 		return 0;
6402811ba51SAchiad Shochat 
6412811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
6422811ba51SAchiad Shochat }
6432811ba51SAchiad Shochat 
6441b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
6451b5daf11SMajd Dibbiny {
6467fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
647d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
6487fae6655SNoa Osherovich 	return 0;
6491b5daf11SMajd Dibbiny }
6501b5daf11SMajd Dibbiny 
6511b5daf11SMajd Dibbiny enum {
6521b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
6531b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
6541b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
6551b5daf11SMajd Dibbiny };
6561b5daf11SMajd Dibbiny 
6571b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
6581b5daf11SMajd Dibbiny {
6591b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
6601b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
6611b5daf11SMajd Dibbiny 
662ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
6631b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
6641b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
6651b5daf11SMajd Dibbiny 
6661b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
6671b5daf11SMajd Dibbiny }
6681b5daf11SMajd Dibbiny 
669da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
670776a3906SMoni Shoua 			    u8 atomic_size_qp,
671da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
672da7525d2SEran Ben Elisha {
673da7525d2SEran Ben Elisha 	u8 tmp;
674da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
675da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
676bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
677da7525d2SEran Ben Elisha 
678da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
679da7525d2SEran Ben Elisha 	 * of host endianness respond
680da7525d2SEran Ben Elisha 	 */
681da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
682da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
683da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
684da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
685da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
686da7525d2SEran Ben Elisha 	} else {
687da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
688da7525d2SEran Ben Elisha 	}
689da7525d2SEran Ben Elisha }
690da7525d2SEran Ben Elisha 
691776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
692776a3906SMoni Shoua 			       struct ib_device_attr *props)
693776a3906SMoni Shoua {
694776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
695776a3906SMoni Shoua 
696776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
697776a3906SMoni Shoua }
698776a3906SMoni Shoua 
6991b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
7001b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
7011b5daf11SMajd Dibbiny {
7021b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7031b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7041b5daf11SMajd Dibbiny 	u64 tmp;
7051b5daf11SMajd Dibbiny 	int err;
7061b5daf11SMajd Dibbiny 
7071b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7081b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7091b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
7101b5daf11SMajd Dibbiny 							    sys_image_guid);
7111b5daf11SMajd Dibbiny 
7121b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7131b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
7143f89a643SAchiad Shochat 		break;
7153f89a643SAchiad Shochat 
7163f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7173f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
7183f89a643SAchiad Shochat 		break;
7191b5daf11SMajd Dibbiny 
7201b5daf11SMajd Dibbiny 	default:
7211b5daf11SMajd Dibbiny 		return -EINVAL;
7221b5daf11SMajd Dibbiny 	}
7233f89a643SAchiad Shochat 
7243f89a643SAchiad Shochat 	if (!err)
7253f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
7263f89a643SAchiad Shochat 
7273f89a643SAchiad Shochat 	return err;
7283f89a643SAchiad Shochat 
7291b5daf11SMajd Dibbiny }
7301b5daf11SMajd Dibbiny 
7311b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
7321b5daf11SMajd Dibbiny 				u16 *max_pkeys)
7331b5daf11SMajd Dibbiny {
7341b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7351b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7361b5daf11SMajd Dibbiny 
7371b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7381b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7391b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
7401b5daf11SMajd Dibbiny 
7411b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7421b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7431b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
7441b5daf11SMajd Dibbiny 						pkey_table_size));
7451b5daf11SMajd Dibbiny 		return 0;
7461b5daf11SMajd Dibbiny 
7471b5daf11SMajd Dibbiny 	default:
7481b5daf11SMajd Dibbiny 		return -EINVAL;
7491b5daf11SMajd Dibbiny 	}
7501b5daf11SMajd Dibbiny }
7511b5daf11SMajd Dibbiny 
7521b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
7531b5daf11SMajd Dibbiny 				u32 *vendor_id)
7541b5daf11SMajd Dibbiny {
7551b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7561b5daf11SMajd Dibbiny 
7571b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7581b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7591b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
7601b5daf11SMajd Dibbiny 
7611b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7621b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7631b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
7641b5daf11SMajd Dibbiny 
7651b5daf11SMajd Dibbiny 	default:
7661b5daf11SMajd Dibbiny 		return -EINVAL;
7671b5daf11SMajd Dibbiny 	}
7681b5daf11SMajd Dibbiny }
7691b5daf11SMajd Dibbiny 
7701b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
7711b5daf11SMajd Dibbiny 				__be64 *node_guid)
7721b5daf11SMajd Dibbiny {
7731b5daf11SMajd Dibbiny 	u64 tmp;
7741b5daf11SMajd Dibbiny 	int err;
7751b5daf11SMajd Dibbiny 
7761b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7771b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7781b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7791b5daf11SMajd Dibbiny 
7801b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7811b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7823f89a643SAchiad Shochat 		break;
7833f89a643SAchiad Shochat 
7843f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7853f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7863f89a643SAchiad Shochat 		break;
7871b5daf11SMajd Dibbiny 
7881b5daf11SMajd Dibbiny 	default:
7891b5daf11SMajd Dibbiny 		return -EINVAL;
7901b5daf11SMajd Dibbiny 	}
7913f89a643SAchiad Shochat 
7923f89a643SAchiad Shochat 	if (!err)
7933f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7943f89a643SAchiad Shochat 
7953f89a643SAchiad Shochat 	return err;
7961b5daf11SMajd Dibbiny }
7971b5daf11SMajd Dibbiny 
7981b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
799bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
8001b5daf11SMajd Dibbiny };
8011b5daf11SMajd Dibbiny 
8021b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
8031b5daf11SMajd Dibbiny {
8041b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
8051b5daf11SMajd Dibbiny 
8061b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
8071b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
8081b5daf11SMajd Dibbiny 
8091b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
8101b5daf11SMajd Dibbiny 
8111b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
8121b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
8131b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
8141b5daf11SMajd Dibbiny }
8151b5daf11SMajd Dibbiny 
816e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
8172528e33eSMatan Barak 				struct ib_device_attr *props,
8182528e33eSMatan Barak 				struct ib_udata *uhw)
819e126ba97SEli Cohen {
82048357091SLeon Romanovsky 	size_t uhw_outlen = (uhw) ? uhw->outlen : 0;
821e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
822938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
823e126ba97SEli Cohen 	int err = -ENOMEM;
824288c01b7SEli Cohen 	int max_sq_desc;
825e126ba97SEli Cohen 	int max_rq_sg;
826e126ba97SEli Cohen 	int max_sq_sg;
827e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
82885c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
829402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
830402ca536SBodong Wang 	size_t resp_len;
831402ca536SBodong Wang 	u64 max_tso;
832e126ba97SEli Cohen 
833402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
83448357091SLeon Romanovsky 	if (uhw_outlen && uhw_outlen < resp_len)
835402ca536SBodong Wang 		return -EINVAL;
8366f26b2acSErez Alfasi 
837402ca536SBodong Wang 	resp.response_length = resp_len;
838402ca536SBodong Wang 
83948357091SLeon Romanovsky 	if (uhw && uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
8402528e33eSMatan Barak 		return -EINVAL;
8412528e33eSMatan Barak 
842e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
8431b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
8441b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
8451b5daf11SMajd Dibbiny 	if (err)
8461b5daf11SMajd Dibbiny 		return err;
8471b5daf11SMajd Dibbiny 
8482019d70eSParav Pandit 	props->max_pkeys = dev->pkey_table_len;
8491b5daf11SMajd Dibbiny 
8501b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
8511b5daf11SMajd Dibbiny 	if (err)
8521b5daf11SMajd Dibbiny 		return err;
853e126ba97SEli Cohen 
8549603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
8559603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
8569603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
857e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
858e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
859e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
8601a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
861938fe83cSSaeed Mahameed 
862938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
863e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
864938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
865e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
866938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
867e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
868938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
869e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
870d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
871d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
872d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
873d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
874b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
875e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_SG_GAPS_REG;
876d2370e0aSMatan Barak 	}
8770ec52f01SJason Gunthorpe 	/* IB_WR_REG_MR always requires changing the entity size with UMR */
8780ec52f01SJason Gunthorpe 	if (!MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled))
879e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
880938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
881e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_INTEGRITY_HANDOVER;
8822dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8832dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8842dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8852dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8862dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8872dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8882dea9094SSagi Grimberg 	}
889938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
890e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_BLOCK_MULTICAST_LOOPBACK;
891e126ba97SEli Cohen 
89285c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
893e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
894e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
89588115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
896e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
897e8161334SNoa Osherovich 		}
898e8161334SNoa Osherovich 
899e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
900e8161334SNoa Osherovich 			props->raw_packet_caps |=
901e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
90288115fe7SBodong Wang 
903a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) {
904402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
905402ca536SBodong Wang 			if (max_tso) {
906402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
907402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
908402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
909402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
910402ca536SBodong Wang 			}
911402ca536SBodong Wang 		}
91231f69a82SYishai Hadas 
913a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) {
91431f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
91531f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
91631f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
91731f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
91831f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
91931f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
92031f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
92131f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
92231f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
92331f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
9244e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
9254e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
92631f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
92731f69a82SYishai Hadas 		}
92831f69a82SYishai Hadas 	} else {
929a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen)
93031f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
931a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen)
93231f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
933402ca536SBodong Wang 	}
934402ca536SBodong Wang 
935f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
936f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
937e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_UD_TSO;
938f0313965SErez Shitrit 	}
939f0313965SErez Shitrit 
94003404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
94185c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
94285c7c014SDaniel Jurgens 	    raw_support)
94303404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
94403404e8aSMaor Gottlieb 
9451d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
9461d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
9471d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
9481d54f890SYishai Hadas 
949cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
95085c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
95185c7c014SDaniel Jurgens 	    raw_support) {
952e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
953cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
954e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
955e8161334SNoa Osherovich 	}
956cff5a0f3SMajd Dibbiny 
95724da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
95824da0016SAriel Levkovich 		props->max_dm_size =
95924da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
96024da0016SAriel Levkovich 	}
96124da0016SAriel Levkovich 
962da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
963da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
964da6d6ba3SMaor Gottlieb 
965b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
966b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
967b1383aa6SNoa Osherovich 
9681b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
9691b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
970e126ba97SEli Cohen 
971e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
972e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
973938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
974938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
975938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
976938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
977288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
978288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
979288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
980e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
98133023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
98233023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
983986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
984938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9859f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
986938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
987938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
988938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
989938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
990938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
991938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
992938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
993e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
994e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
995911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
996911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
99762e3c379SMax Gurtovoy 	props->max_pi_fast_reg_page_list_len =
99862e3c379SMax Gurtovoy 		props->max_fast_reg_page_list_len / 2;
99936609056SYamin Friedman 	props->max_sgl_rd =
100036609056SYamin Friedman 		MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance);
1001776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
100281bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
1003938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1004938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
1005e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1006e126ba97SEli Cohen 					   props->max_mcast_grp;
100786695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
10087c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
10097c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
1010e126ba97SEli Cohen 
1011e502b8b0SLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
101200815752SMoni Shoua 		if (dev->odp_caps.general_caps & IB_ODP_SUPPORT)
1013e945c653SJason Gunthorpe 			props->kernel_cap_flags |= IBK_ON_DEMAND_PAGING;
10148cdd312cSHaggai Eran 		props->odp_caps = dev->odp_caps;
1015a73a8955SMoni Shoua 		if (!uhw) {
1016a73a8955SMoni Shoua 			/* ODP for kernel QPs is not implemented for receive
1017a73a8955SMoni Shoua 			 * WQEs and SRQ WQEs
1018a73a8955SMoni Shoua 			 */
1019a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.rc_odp_caps &=
1020a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1021a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1022a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.uc_odp_caps &=
1023a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1024a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1025a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.ud_odp_caps &=
1026a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1027a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1028a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.xrc_odp_caps &=
1029a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1030a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1031a73a8955SMoni Shoua 		}
1032e502b8b0SLeon Romanovsky 	}
10338cdd312cSHaggai Eran 
1034e53a9d26SParav Pandit 	if (mlx5_core_is_vf(mdev))
1035e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_VIRTUAL_FUNCTION;
1036eff901d3SEli Cohen 
103731f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
103885c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
103931f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
104031f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
104131f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
104231f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
104331f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
104431f69a82SYishai Hadas 		props->max_wq_type_rq =
104531f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
104631f69a82SYishai Hadas 	}
104731f69a82SYishai Hadas 
1048eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
104978b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
1050eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
105178b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
1052eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
105378b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1054eb761894SArtemy Kovalyov 	}
1055eb761894SArtemy Kovalyov 
105689705e92SDanit Goldberg 	if (MLX5_CAP_GEN(mdev, tag_matching) &&
105789705e92SDanit Goldberg 	    MLX5_CAP_GEN(mdev, rndv_offload_rc)) {
105889705e92SDanit Goldberg 		props->tm_caps.flags = IB_TM_CAP_RNDV_RC;
105989705e92SDanit Goldberg 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
106089705e92SDanit Goldberg 	}
106189705e92SDanit Goldberg 
106287ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
106387ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
106487ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
106587ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
106687ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
106787ab3f52SYonatan Cohen 	}
106887ab3f52SYonatan Cohen 
1069a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), cqe_comp_caps) <= uhw_outlen) {
1070572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
1071572f46bfSYonatan Cohen 
1072572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
10737e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
1074572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
1075572f46bfSYonatan Cohen 					     cqe_compression_max_num);
1076572f46bfSYonatan Cohen 
10777e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
10787e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
10797e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
10806f1006a4SYonatan Cohen 
10816f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
10826f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
10836f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1084572f46bfSYonatan Cohen 		}
10857e43a2a5SBodong Wang 	}
10867e43a2a5SBodong Wang 
1087a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), packet_pacing_caps) <= uhw_outlen &&
108885c7c014SDaniel Jurgens 	    raw_support) {
1089d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1090d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
1091d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
1092d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1093d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
1094d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1095d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
1096d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
109761147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
109861147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
109961147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
110061147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
1101d949167dSBodong Wang 		}
1102d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1103d949167dSBodong Wang 	}
1104d949167dSBodong Wang 
1105a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes) <=
1106a762d460SLeon Romanovsky 	    uhw_outlen) {
1107795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
11089f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1109795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1110050da902SBodong Wang 
1111050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1112050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1113050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1114050da902SBodong Wang 
11159f885201SLeon Romanovsky 		resp.response_length +=
11169f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
11179f885201SLeon Romanovsky 	}
11189f885201SLeon Romanovsky 
1119a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), flags) <= uhw_outlen) {
1120de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
11217a0c8f42SGuy Levi 
1122de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1123de57f2adSGuy Levi 			resp.flags |=
1124de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
11257a0c8f42SGuy Levi 
11267a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
11277a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
11287e11b911SDanit Goldberg 		if (MLX5_CAP_GEN(mdev, qp_packet_based))
11297e11b911SDanit Goldberg 			resp.flags |=
11307e11b911SDanit Goldberg 				MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
11317249c8eaSGuy Levi 
11327249c8eaSGuy Levi 		resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1133de57f2adSGuy Levi 	}
11349f885201SLeon Romanovsky 
1135a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), sw_parsing_caps) <= uhw_outlen) {
113696dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
113796dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
113896dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
113996dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
114096dc3fc5SNoa Osherovich 
114196dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
114296dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
114396dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
114496dc3fc5SNoa Osherovich 
114596dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
114696dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
114796dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
114896dc3fc5SNoa Osherovich 
114996dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
115096dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
115196dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
115296dc3fc5SNoa Osherovich 		}
115396dc3fc5SNoa Osherovich 	}
115496dc3fc5SNoa Osherovich 
1155a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), striding_rq_caps) <= uhw_outlen &&
115685c7c014SDaniel Jurgens 	    raw_support) {
1157b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1158b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1159b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1160b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1161b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1162b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1163c16339b6SMark Zhang 			if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range))
1164c16339b6SMark Zhang 				resp.striding_rq_caps
1165c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1166c16339b6SMark Zhang 					MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1167c16339b6SMark Zhang 			else
1168c16339b6SMark Zhang 				resp.striding_rq_caps
1169c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1170b4f34597SNoa Osherovich 					MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1171b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1172b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1173b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1174b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1175b4f34597SNoa Osherovich 		}
1176b4f34597SNoa Osherovich 	}
1177b4f34597SNoa Osherovich 
1178a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), tunnel_offloads_caps) <= uhw_outlen) {
1179f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1180f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1181f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1182f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1183f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1184f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1185f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1186f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1187f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1188f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
118941e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre))
1190e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1191e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
119241e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_udp))
1193e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1194e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1195f95ef6cbSMaor Gottlieb 	}
1196f95ef6cbSMaor Gottlieb 
119711656f59SLior Nahmanson 	if (offsetofend(typeof(resp), dci_streams_caps) <= uhw_outlen) {
119811656f59SLior Nahmanson 		resp.response_length += sizeof(resp.dci_streams_caps);
119911656f59SLior Nahmanson 
120011656f59SLior Nahmanson 		resp.dci_streams_caps.max_log_num_concurent =
120111656f59SLior Nahmanson 			MLX5_CAP_GEN(mdev, log_max_dci_stream_channels);
120211656f59SLior Nahmanson 
120311656f59SLior Nahmanson 		resp.dci_streams_caps.max_log_num_errored =
120411656f59SLior Nahmanson 			MLX5_CAP_GEN(mdev, log_max_dci_errored_streams);
120511656f59SLior Nahmanson 	}
120611656f59SLior Nahmanson 
120748357091SLeon Romanovsky 	if (uhw_outlen) {
1208402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1209402ca536SBodong Wang 
1210402ca536SBodong Wang 		if (err)
1211402ca536SBodong Wang 			return err;
1212402ca536SBodong Wang 	}
1213402ca536SBodong Wang 
12141b5daf11SMajd Dibbiny 	return 0;
12151b5daf11SMajd Dibbiny }
1216e126ba97SEli Cohen 
1217639bf441SAharon Landau static void translate_active_width(struct ib_device *ibdev, u16 active_width,
12181b5daf11SMajd Dibbiny 				   u8 *ib_width)
12191b5daf11SMajd Dibbiny {
12201b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12211b5daf11SMajd Dibbiny 
1222e27014bdSAharon Landau 	if (active_width & MLX5_PTYS_WIDTH_1X)
12231b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1224e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_2X)
1225d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1226e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_4X)
12271b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1228e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_8X)
12291b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1230e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_12X)
12311b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1232db7a691aSMichael Guralnik 	else {
1233db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
1234e27014bdSAharon Landau 			    active_width);
1235db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
12361b5daf11SMajd Dibbiny 	}
12371b5daf11SMajd Dibbiny 
1238db7a691aSMichael Guralnik 	return;
12391b5daf11SMajd Dibbiny }
12401b5daf11SMajd Dibbiny 
12411b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
12421b5daf11SMajd Dibbiny {
12431b5daf11SMajd Dibbiny 	switch (mtu) {
12441b5daf11SMajd Dibbiny 	case 256: return 1;
12451b5daf11SMajd Dibbiny 	case 512: return 2;
12461b5daf11SMajd Dibbiny 	case 1024: return 3;
12471b5daf11SMajd Dibbiny 	case 2048: return 4;
12481b5daf11SMajd Dibbiny 	case 4096: return 5;
12491b5daf11SMajd Dibbiny 	default:
12501b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
12511b5daf11SMajd Dibbiny 		return -1;
12521b5daf11SMajd Dibbiny 	}
12531b5daf11SMajd Dibbiny }
12541b5daf11SMajd Dibbiny 
12551b5daf11SMajd Dibbiny enum ib_max_vl_num {
12561b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
12571b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
12581b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
12591b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
12601b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
12611b5daf11SMajd Dibbiny };
12621b5daf11SMajd Dibbiny 
12631b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
12641b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
12651b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
12661b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
12671b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
12681b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
12691b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
12701b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
12711b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
12721b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
12731b5daf11SMajd Dibbiny };
12741b5daf11SMajd Dibbiny 
12751b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
12761b5daf11SMajd Dibbiny 				u8 *max_vl_num)
12771b5daf11SMajd Dibbiny {
12781b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
12791b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
12801b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
12811b5daf11SMajd Dibbiny 		break;
12821b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
12831b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
12841b5daf11SMajd Dibbiny 		break;
12851b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
12861b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
12871b5daf11SMajd Dibbiny 		break;
12881b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
12891b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
12901b5daf11SMajd Dibbiny 		break;
12911b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
12921b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
12931b5daf11SMajd Dibbiny 		break;
12941b5daf11SMajd Dibbiny 
12951b5daf11SMajd Dibbiny 	default:
12961b5daf11SMajd Dibbiny 		return -EINVAL;
12971b5daf11SMajd Dibbiny 	}
12981b5daf11SMajd Dibbiny 
12991b5daf11SMajd Dibbiny 	return 0;
13001b5daf11SMajd Dibbiny }
13011b5daf11SMajd Dibbiny 
13021fb7f897SMark Bloch static int mlx5_query_hca_port(struct ib_device *ibdev, u32 port,
13031b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
13041b5daf11SMajd Dibbiny {
13051b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
13061b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
13071b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1308046339eaSSaeed Mahameed 	u16 max_mtu;
1309046339eaSSaeed Mahameed 	u16 oper_mtu;
13101b5daf11SMajd Dibbiny 	int err;
1311639bf441SAharon Landau 	u16 ib_link_width_oper;
13121b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
13131b5daf11SMajd Dibbiny 
13141b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
13151b5daf11SMajd Dibbiny 	if (!rep) {
13161b5daf11SMajd Dibbiny 		err = -ENOMEM;
13171b5daf11SMajd Dibbiny 		goto out;
13181b5daf11SMajd Dibbiny 	}
13191b5daf11SMajd Dibbiny 
1320c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
13211b5daf11SMajd Dibbiny 
13221b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
13231b5daf11SMajd Dibbiny 	if (err)
13241b5daf11SMajd Dibbiny 		goto out;
13251b5daf11SMajd Dibbiny 
13261b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
13271b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
13281b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
13291b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
13301b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
13311b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
13321b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
13331b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
13341b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
13351b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
13361b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
13371b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
13381b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
13391b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
13401b5daf11SMajd Dibbiny 
13414106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
13424106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
13434106a758SMichael Guralnik 
1344639bf441SAharon Landau 	err = mlx5_query_ib_port_oper(mdev, &ib_link_width_oper,
1345376ceb31SAharon Landau 				      &props->active_speed, port);
13461b5daf11SMajd Dibbiny 	if (err)
13471b5daf11SMajd Dibbiny 		goto out;
13481b5daf11SMajd Dibbiny 
1349db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1350db7a691aSMichael Guralnik 
1351facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
13521b5daf11SMajd Dibbiny 
13531b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
13541b5daf11SMajd Dibbiny 
1355facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
13561b5daf11SMajd Dibbiny 
13571b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
13581b5daf11SMajd Dibbiny 
13591b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
13601b5daf11SMajd Dibbiny 	if (err)
13611b5daf11SMajd Dibbiny 		goto out;
13621b5daf11SMajd Dibbiny 
13631b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
13641b5daf11SMajd Dibbiny 				   &props->max_vl_num);
13651b5daf11SMajd Dibbiny out:
13661b5daf11SMajd Dibbiny 	kfree(rep);
1367e126ba97SEli Cohen 	return err;
1368e126ba97SEli Cohen }
1369e126ba97SEli Cohen 
13701fb7f897SMark Bloch int mlx5_ib_query_port(struct ib_device *ibdev, u32 port,
1371e126ba97SEli Cohen 		       struct ib_port_attr *props)
1372e126ba97SEli Cohen {
1373095b0927SIlan Tayari 	unsigned int count;
1374095b0927SIlan Tayari 	int ret;
1375095b0927SIlan Tayari 
13761b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13771b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1378095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1379095b0927SIlan Tayari 		break;
1380e126ba97SEli Cohen 
13811b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1382095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1383095b0927SIlan Tayari 		break;
13841b5daf11SMajd Dibbiny 
13853f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1386095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1387095b0927SIlan Tayari 		break;
13883f89a643SAchiad Shochat 
13891b5daf11SMajd Dibbiny 	default:
1390095b0927SIlan Tayari 		ret = -EINVAL;
1391e126ba97SEli Cohen 	}
1392095b0927SIlan Tayari 
1393095b0927SIlan Tayari 	if (!ret && props) {
1394b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1395b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1396b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1397b3cbd6f0SDaniel Jurgens 
1398b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1399b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1400b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1401b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1402b3cbd6f0SDaniel Jurgens 			 */
1403b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1404b3cbd6f0SDaniel Jurgens 			port = 1;
1405b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1406b3cbd6f0SDaniel Jurgens 		}
1407b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1408b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1409b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1410095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1411095b0927SIlan Tayari 	}
1412095b0927SIlan Tayari 	return ret;
1413e126ba97SEli Cohen }
1414e126ba97SEli Cohen 
14151fb7f897SMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u32 port,
14168e6efa3aSMark Bloch 				  struct ib_port_attr *props)
14178e6efa3aSMark Bloch {
14187a58779eSParav Pandit 	return mlx5_query_port_roce(ibdev, port, props);
14198e6efa3aSMark Bloch }
14208e6efa3aSMark Bloch 
14211fb7f897SMark Bloch static int mlx5_ib_rep_query_pkey(struct ib_device *ibdev, u32 port, u16 index,
1422d6fd59e1SParav Pandit 				  u16 *pkey)
1423d6fd59e1SParav Pandit {
1424d6fd59e1SParav Pandit 	/* Default special Pkey for representor device port as per the
1425d6fd59e1SParav Pandit 	 * IB specification 1.3 section 10.9.1.2.
1426d6fd59e1SParav Pandit 	 */
1427d6fd59e1SParav Pandit 	*pkey = 0xffff;
1428d6fd59e1SParav Pandit 	return 0;
1429d6fd59e1SParav Pandit }
1430d6fd59e1SParav Pandit 
14311fb7f897SMark Bloch static int mlx5_ib_query_gid(struct ib_device *ibdev, u32 port, int index,
1432e126ba97SEli Cohen 			     union ib_gid *gid)
1433e126ba97SEli Cohen {
14341b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
14351b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1436e126ba97SEli Cohen 
14371b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14381b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14391b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1440e126ba97SEli Cohen 
14411b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14421b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1443e126ba97SEli Cohen 
14441b5daf11SMajd Dibbiny 	default:
14451b5daf11SMajd Dibbiny 		return -EINVAL;
14461b5daf11SMajd Dibbiny 	}
1447e126ba97SEli Cohen 
1448e126ba97SEli Cohen }
1449e126ba97SEli Cohen 
14501fb7f897SMark Bloch static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u32 port,
1451b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1452b3cbd6f0SDaniel Jurgens {
1453b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1454b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1455b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
14561fb7f897SMark Bloch 	u32 mdev_port_num;
1457b3cbd6f0SDaniel Jurgens 	int err;
1458b3cbd6f0SDaniel Jurgens 
1459b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1460b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1461b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1462b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1463b3cbd6f0SDaniel Jurgens 		 */
1464b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1465b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1466b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1467b3cbd6f0SDaniel Jurgens 	}
1468b3cbd6f0SDaniel Jurgens 
1469b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1470b3cbd6f0SDaniel Jurgens 					index, pkey);
1471b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1472b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1473b3cbd6f0SDaniel Jurgens 
1474b3cbd6f0SDaniel Jurgens 	return err;
1475b3cbd6f0SDaniel Jurgens }
1476b3cbd6f0SDaniel Jurgens 
14771fb7f897SMark Bloch static int mlx5_ib_query_pkey(struct ib_device *ibdev, u32 port, u16 index,
1478e126ba97SEli Cohen 			      u16 *pkey)
1479e126ba97SEli Cohen {
14801b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14811b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14821b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1483e126ba97SEli Cohen 
14841b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14851b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1486b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
14871b5daf11SMajd Dibbiny 	default:
14881b5daf11SMajd Dibbiny 		return -EINVAL;
1489e126ba97SEli Cohen 	}
14901b5daf11SMajd Dibbiny }
1491e126ba97SEli Cohen 
1492e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1493e126ba97SEli Cohen 				 struct ib_device_modify *props)
1494e126ba97SEli Cohen {
1495e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1496e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1497e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1498e126ba97SEli Cohen 	int err;
1499e126ba97SEli Cohen 
1500e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1501e126ba97SEli Cohen 		return -EOPNOTSUPP;
1502e126ba97SEli Cohen 
1503e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1504e126ba97SEli Cohen 		return 0;
1505e126ba97SEli Cohen 
1506e126ba97SEli Cohen 	/*
1507e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1508e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1509e126ba97SEli Cohen 	 */
1510bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
15119603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1512e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1513e126ba97SEli Cohen 	if (err)
1514e126ba97SEli Cohen 		return err;
1515e126ba97SEli Cohen 
1516bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1517e126ba97SEli Cohen 
1518e126ba97SEli Cohen 	return err;
1519e126ba97SEli Cohen }
1520e126ba97SEli Cohen 
15211fb7f897SMark Bloch static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u32 port_num, u32 mask,
1522cdbe33d0SEli Cohen 				u32 value)
1523cdbe33d0SEli Cohen {
1524cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1525b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
15261fb7f897SMark Bloch 	u32 mdev_port_num;
1527cdbe33d0SEli Cohen 	int err;
1528cdbe33d0SEli Cohen 
1529b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1530b3cbd6f0SDaniel Jurgens 	if (!mdev)
1531b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1532b3cbd6f0SDaniel Jurgens 
1533b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1534cdbe33d0SEli Cohen 	if (err)
1535b3cbd6f0SDaniel Jurgens 		goto out;
1536cdbe33d0SEli Cohen 
1537cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1538cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1539cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1540b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1541b3cbd6f0SDaniel Jurgens 		goto out;
1542cdbe33d0SEli Cohen 	}
1543cdbe33d0SEli Cohen 
1544cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1545cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1546b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1547b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1548b3cbd6f0SDaniel Jurgens 
1549b3cbd6f0SDaniel Jurgens out:
1550b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1551cdbe33d0SEli Cohen 
1552cdbe33d0SEli Cohen 	return err;
1553cdbe33d0SEli Cohen }
1554cdbe33d0SEli Cohen 
15551fb7f897SMark Bloch static int mlx5_ib_modify_port(struct ib_device *ibdev, u32 port, int mask,
1556e126ba97SEli Cohen 			       struct ib_port_modify *props)
1557e126ba97SEli Cohen {
1558e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1559e126ba97SEli Cohen 	struct ib_port_attr attr;
1560e126ba97SEli Cohen 	u32 tmp;
1561e126ba97SEli Cohen 	int err;
1562cdbe33d0SEli Cohen 	u32 change_mask;
1563cdbe33d0SEli Cohen 	u32 value;
1564cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1565cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1566cdbe33d0SEli Cohen 
1567ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1568ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1569ec255879SMajd Dibbiny 	 */
1570ec255879SMajd Dibbiny 	if (!is_ib)
1571ec255879SMajd Dibbiny 		return 0;
1572ec255879SMajd Dibbiny 
1573cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1574cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1575cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1576cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1577cdbe33d0SEli Cohen 	}
1578e126ba97SEli Cohen 
1579e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1580e126ba97SEli Cohen 
1581c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1582e126ba97SEli Cohen 	if (err)
1583e126ba97SEli Cohen 		goto out;
1584e126ba97SEli Cohen 
1585e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1586e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1587e126ba97SEli Cohen 
15889603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1589e126ba97SEli Cohen 
1590e126ba97SEli Cohen out:
1591e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1592e126ba97SEli Cohen 	return err;
1593e126ba97SEli Cohen }
1594e126ba97SEli Cohen 
159530aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
159630aa60b3SEli Cohen {
159730aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
159830aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
159930aa60b3SEli Cohen }
160030aa60b3SEli Cohen 
160131a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
160231a78a5aSYishai Hadas {
160331a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
160431a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
160531a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
160631a78a5aSYishai Hadas 
160731a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
160831a78a5aSYishai Hadas }
160931a78a5aSYishai Hadas 
1610b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1611b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
161231a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1613b037c29aSEli Cohen {
1614b037c29aSEli Cohen 	int uars_per_sys_page;
1615b037c29aSEli Cohen 	int bfregs_per_sys_page;
1616b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1617b037c29aSEli Cohen 
1618b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1619b037c29aSEli Cohen 		return -EINVAL;
1620b037c29aSEli Cohen 
1621b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1622b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1623b037c29aSEli Cohen 
1624b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1625b037c29aSEli Cohen 		return -ENOMEM;
1626b037c29aSEli Cohen 
1627b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1628b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
162931a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1630b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1631b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1632b037c29aSEli Cohen 		return -EINVAL;
1633b037c29aSEli Cohen 
163431a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
163531a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
163631a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
163731a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
163831a78a5aSYishai Hadas 
163931a78a5aSYishai 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",
1640b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1641b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
164231a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
164331a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1644b037c29aSEli Cohen 
1645b037c29aSEli Cohen 	return 0;
1646b037c29aSEli Cohen }
1647b037c29aSEli Cohen 
1648b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1649b037c29aSEli Cohen {
1650b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1651b037c29aSEli Cohen 	int err;
1652b037c29aSEli Cohen 	int i;
1653b037c29aSEli Cohen 
1654b037c29aSEli Cohen 	bfregi = &context->bfregi;
165531a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1656d2c8a155SMeir Lichtinger 		err = mlx5_cmd_uar_alloc(dev->mdev, &bfregi->sys_pages[i],
1657d2c8a155SMeir Lichtinger 					 context->devx_uid);
1658b037c29aSEli Cohen 		if (err)
1659b037c29aSEli Cohen 			goto error;
1660b037c29aSEli Cohen 
1661b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1662b037c29aSEli Cohen 	}
16634ed131d0SYishai Hadas 
16644ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
16654ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
16664ed131d0SYishai Hadas 
1667b037c29aSEli Cohen 	return 0;
1668b037c29aSEli Cohen 
1669b037c29aSEli Cohen error:
1670b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1671d2c8a155SMeir Lichtinger 		if (mlx5_cmd_uar_dealloc(dev->mdev, bfregi->sys_pages[i],
1672d2c8a155SMeir Lichtinger 					 context->devx_uid))
1673b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1674b037c29aSEli Cohen 
1675b037c29aSEli Cohen 	return err;
1676b037c29aSEli Cohen }
1677b037c29aSEli Cohen 
167815177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
167915177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1680b037c29aSEli Cohen {
1681b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1682b037c29aSEli Cohen 	int i;
1683b037c29aSEli Cohen 
1684b037c29aSEli Cohen 	bfregi = &context->bfregi;
168515177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
16864ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
168715177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
1688d2c8a155SMeir Lichtinger 			mlx5_cmd_uar_dealloc(dev->mdev, bfregi->sys_pages[i],
1689d2c8a155SMeir Lichtinger 					     context->devx_uid);
1690b037c29aSEli Cohen }
1691b037c29aSEli Cohen 
16920042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1693a560f1d9SMark Bloch {
1694a560f1d9SMark Bloch 	int err = 0;
1695a560f1d9SMark Bloch 
1696a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16970042f9e4SMark Bloch 	if (td)
1698a560f1d9SMark Bloch 		dev->lb.user_td++;
16990042f9e4SMark Bloch 	if (qp)
17000042f9e4SMark Bloch 		dev->lb.qps++;
1701a560f1d9SMark Bloch 
17020042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
17030042f9e4SMark Bloch 	    dev->lb.qps == 1) {
17040042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1705a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
17060042f9e4SMark Bloch 			dev->lb.enabled = true;
17070042f9e4SMark Bloch 		}
17080042f9e4SMark Bloch 	}
1709a560f1d9SMark Bloch 
1710a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1711a560f1d9SMark Bloch 
1712a560f1d9SMark Bloch 	return err;
1713a560f1d9SMark Bloch }
1714a560f1d9SMark Bloch 
17150042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1716a560f1d9SMark Bloch {
1717a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
17180042f9e4SMark Bloch 	if (td)
1719a560f1d9SMark Bloch 		dev->lb.user_td--;
17200042f9e4SMark Bloch 	if (qp)
17210042f9e4SMark Bloch 		dev->lb.qps--;
1722a560f1d9SMark Bloch 
17230042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
17240042f9e4SMark Bloch 	    dev->lb.qps == 0) {
17250042f9e4SMark Bloch 		if (dev->lb.enabled) {
1726a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
17270042f9e4SMark Bloch 			dev->lb.enabled = false;
17280042f9e4SMark Bloch 		}
17290042f9e4SMark Bloch 	}
1730a560f1d9SMark Bloch 
1731a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1732a560f1d9SMark Bloch }
1733a560f1d9SMark Bloch 
1734d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1735d2d19121SYishai Hadas 					  u16 uid)
1736c85023e1SHuy Nguyen {
1737c85023e1SHuy Nguyen 	int err;
1738c85023e1SHuy Nguyen 
1739cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1740cfdeb893SLeon Romanovsky 		return 0;
1741cfdeb893SLeon Romanovsky 
1742d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1743c85023e1SHuy Nguyen 	if (err)
1744c85023e1SHuy Nguyen 		return err;
1745c85023e1SHuy Nguyen 
1746c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17478978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17488978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1749c85023e1SHuy Nguyen 		return err;
1750c85023e1SHuy Nguyen 
17510042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1752c85023e1SHuy Nguyen }
1753c85023e1SHuy Nguyen 
1754d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1755d2d19121SYishai Hadas 					     u16 uid)
1756c85023e1SHuy Nguyen {
1757cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1758cfdeb893SLeon Romanovsky 		return;
1759cfdeb893SLeon Romanovsky 
1760d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1761c85023e1SHuy Nguyen 
1762c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17638978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17648978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1765c85023e1SHuy Nguyen 		return;
1766c85023e1SHuy Nguyen 
17670042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1768c85023e1SHuy Nguyen }
1769c85023e1SHuy Nguyen 
177045ec21c9SYishai Hadas static int set_ucontext_resp(struct ib_ucontext *uctx,
177145ec21c9SYishai Hadas 			     struct mlx5_ib_alloc_ucontext_resp *resp)
177245ec21c9SYishai Hadas {
177345ec21c9SYishai Hadas 	struct ib_device *ibdev = uctx->device;
177445ec21c9SYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
177545ec21c9SYishai Hadas 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
177645ec21c9SYishai Hadas 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
177745ec21c9SYishai Hadas 
177845ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1779594cac11SOr Har-Toov 		resp->dump_fill_mkey = dev->mkeys.dump_fill_mkey;
178045ec21c9SYishai Hadas 		resp->comp_mask |=
178145ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
178245ec21c9SYishai Hadas 	}
178345ec21c9SYishai Hadas 
178445ec21c9SYishai Hadas 	resp->qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
178545ec21c9SYishai Hadas 	if (dev->wc_support)
178645ec21c9SYishai Hadas 		resp->bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev,
178745ec21c9SYishai Hadas 						      log_bf_reg_size);
178845ec21c9SYishai Hadas 	resp->cache_line_size = cache_line_size();
178945ec21c9SYishai Hadas 	resp->max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
179045ec21c9SYishai Hadas 	resp->max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
179145ec21c9SYishai Hadas 	resp->max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
179245ec21c9SYishai Hadas 	resp->max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
179345ec21c9SYishai Hadas 	resp->max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
179445ec21c9SYishai Hadas 	resp->cqe_version = context->cqe_version;
179545ec21c9SYishai Hadas 	resp->log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
179645ec21c9SYishai Hadas 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
179745ec21c9SYishai Hadas 	resp->num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
179845ec21c9SYishai Hadas 					MLX5_CAP_GEN(dev->mdev,
179945ec21c9SYishai Hadas 						     num_of_uars_per_page) : 1;
180045ec21c9SYishai Hadas 	resp->tot_bfregs = bfregi->lib_uar_dyn ? 0 :
180145ec21c9SYishai Hadas 			bfregi->total_num_bfregs - bfregi->num_dyn_bfregs;
180245ec21c9SYishai Hadas 	resp->num_ports = dev->num_ports;
180345ec21c9SYishai Hadas 	resp->cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
180445ec21c9SYishai Hadas 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
180545ec21c9SYishai Hadas 
180645ec21c9SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
180745ec21c9SYishai Hadas 		mlx5_query_min_inline(dev->mdev, &resp->eth_min_inline);
180845ec21c9SYishai Hadas 		resp->eth_min_inline++;
180945ec21c9SYishai Hadas 	}
181045ec21c9SYishai Hadas 
181145ec21c9SYishai Hadas 	if (dev->mdev->clock_info)
181245ec21c9SYishai Hadas 		resp->clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
181345ec21c9SYishai Hadas 
181445ec21c9SYishai Hadas 	/*
181545ec21c9SYishai Hadas 	 * We don't want to expose information from the PCI bar that is located
181645ec21c9SYishai Hadas 	 * after 4096 bytes, so if the arch only supports larger pages, let's
181745ec21c9SYishai Hadas 	 * pretend we don't support reading the HCA's core clock. This is also
181845ec21c9SYishai Hadas 	 * forced by mmap function.
181945ec21c9SYishai Hadas 	 */
182045ec21c9SYishai Hadas 	if (PAGE_SIZE <= 4096) {
182145ec21c9SYishai Hadas 		resp->comp_mask |=
182245ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
182345ec21c9SYishai Hadas 		resp->hca_core_clock_offset =
182445ec21c9SYishai Hadas 			offsetof(struct mlx5_init_seg,
182545ec21c9SYishai Hadas 				 internal_timer_h) % PAGE_SIZE;
182645ec21c9SYishai Hadas 	}
182745ec21c9SYishai Hadas 
182845ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, ece_support))
182945ec21c9SYishai Hadas 		resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_ECE;
183045ec21c9SYishai Hadas 
183133652951SAharon Landau 	if (rt_supported(MLX5_CAP_GEN(dev->mdev, sq_ts_format)) &&
183233652951SAharon Landau 	    rt_supported(MLX5_CAP_GEN(dev->mdev, rq_ts_format)) &&
183333652951SAharon Landau 	    rt_supported(MLX5_CAP_ROCE(dev->mdev, qp_ts_format)))
183433652951SAharon Landau 		resp->comp_mask |=
183533652951SAharon Landau 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_REAL_TIME_TS;
183633652951SAharon Landau 
183745ec21c9SYishai Hadas 	resp->num_dyn_bfregs = bfregi->num_dyn_bfregs;
1838c906b86eSSergey Gorenko 
1839c906b86eSSergey Gorenko 	if (MLX5_CAP_GEN(dev->mdev, drain_sigerr))
1840c906b86eSSergey Gorenko 		resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_SQD2RTS;
1841c906b86eSSergey Gorenko 
184213ad1125SAharon Landau 	resp->comp_mask |=
184313ad1125SAharon Landau 		MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_MKEY_UPDATE_TAG;
184413ad1125SAharon Landau 
184545ec21c9SYishai Hadas 	return 0;
184645ec21c9SYishai Hadas }
184745ec21c9SYishai Hadas 
1848a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1849e126ba97SEli Cohen 				  struct ib_udata *udata)
1850e126ba97SEli Cohen {
1851a2a074efSLeon Romanovsky 	struct ib_device *ibdev = uctx->device;
1852e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1853b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1854b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1855a2a074efSLeon Romanovsky 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
18562f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
185778c0f98cSEli Cohen 	int ver;
1858e126ba97SEli Cohen 	int err;
1859a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1860a168a41cSMajd Dibbiny 				     max_cqe_version);
1861b037c29aSEli Cohen 	bool lib_uar_4k;
18620a2fd01cSYishai Hadas 	bool lib_uar_dyn;
1863e126ba97SEli Cohen 
1864e126ba97SEli Cohen 	if (!dev->ib_active)
1865a2a074efSLeon Romanovsky 		return -EAGAIN;
1866e126ba97SEli Cohen 
1867e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
186878c0f98cSEli Cohen 		ver = 0;
1869e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
187078c0f98cSEli Cohen 		ver = 2;
187178c0f98cSEli Cohen 	else
1872a2a074efSLeon Romanovsky 		return -EINVAL;
187378c0f98cSEli Cohen 
1874e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1875e126ba97SEli Cohen 	if (err)
1876a2a074efSLeon Romanovsky 		return err;
1877e126ba97SEli Cohen 
1878a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1879a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
188078c0f98cSEli Cohen 
1881f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1882a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
1883b368d7cbSMatan Barak 
18842f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
18852f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
18862f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1887a2a074efSLeon Romanovsky 		return -EINVAL;
1888e126ba97SEli Cohen 
1889d2c8a155SMeir Lichtinger 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1890d2c8a155SMeir Lichtinger 		err = mlx5_ib_devx_create(dev, true);
1891d2c8a155SMeir Lichtinger 		if (err < 0)
1892d2c8a155SMeir Lichtinger 			goto out_ctx;
1893d2c8a155SMeir Lichtinger 		context->devx_uid = err;
1894d2c8a155SMeir Lichtinger 	}
1895d2c8a155SMeir Lichtinger 
189630aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
18970a2fd01cSYishai Hadas 	lib_uar_dyn = req.lib_caps & MLX5_LIB_CAP_DYN_UAR;
18982f5ff264SEli Cohen 	bfregi = &context->bfregi;
1899b037c29aSEli Cohen 
19000a2fd01cSYishai Hadas 	if (lib_uar_dyn) {
19010a2fd01cSYishai Hadas 		bfregi->lib_uar_dyn = lib_uar_dyn;
19020a2fd01cSYishai Hadas 		goto uar_done;
19030a2fd01cSYishai Hadas 	}
19040a2fd01cSYishai Hadas 
1905b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
190631a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1907b037c29aSEli Cohen 	if (err)
1908d2c8a155SMeir Lichtinger 		goto out_devx;
1909b037c29aSEli Cohen 
19102f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1911b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
191231a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1913b037c29aSEli Cohen 				GFP_KERNEL);
1914b037c29aSEli Cohen 	if (!bfregi->count) {
1915e126ba97SEli Cohen 		err = -ENOMEM;
1916d2c8a155SMeir Lichtinger 		goto out_devx;
1917e126ba97SEli Cohen 	}
1918e126ba97SEli Cohen 
1919b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1920b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1921e126ba97SEli Cohen 				    GFP_KERNEL);
1922b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1923e126ba97SEli Cohen 		err = -ENOMEM;
1924e126ba97SEli Cohen 		goto out_count;
1925e126ba97SEli Cohen 	}
1926e126ba97SEli Cohen 
1927b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1928b037c29aSEli Cohen 	if (err)
1929b037c29aSEli Cohen 		goto out_sys_pages;
1930b037c29aSEli Cohen 
19310a2fd01cSYishai Hadas uar_done:
1932d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1933d2d19121SYishai Hadas 					     context->devx_uid);
1934a8b92ca1SYishai Hadas 	if (err)
1935d2c8a155SMeir Lichtinger 		goto out_uars;
1936a8b92ca1SYishai Hadas 
1937e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1938e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1939e126ba97SEli Cohen 
194045ec21c9SYishai Hadas 	context->cqe_version = min_t(__u8,
194145ec21c9SYishai Hadas 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
194245ec21c9SYishai Hadas 				 req.max_cqe_version);
1943b368d7cbSMatan Barak 
194445ec21c9SYishai Hadas 	err = set_ucontext_resp(uctx, &resp);
194545ec21c9SYishai Hadas 	if (err)
194645ec21c9SYishai Hadas 		goto out_mdev;
1947b368d7cbSMatan Barak 
194845ec21c9SYishai Hadas 	resp.response_length = min(udata->outlen, sizeof(resp));
1949b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1950e126ba97SEli Cohen 	if (err)
1951a8b92ca1SYishai Hadas 		goto out_mdev;
1952e126ba97SEli Cohen 
19532f5ff264SEli Cohen 	bfregi->ver = ver;
19542f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
195530aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
195630aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1957f72300c5SHaggai Abramovsky 
1958802dcc7fSMark Zhang 	if (mlx5_ib_lag_should_assign_affinity(dev)) {
19591fb7f897SMark Bloch 		u32 port = mlx5_core_native_port_num(dev->mdev) - 1;
1960c6a21c38SMajd Dibbiny 
1961c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1962c6a21c38SMajd Dibbiny 			   atomic_add_return(
196395579e78SMark Bloch 				   1, &dev->port[port].roce.tx_port_affinity));
1964c6a21c38SMajd Dibbiny 	}
1965c6a21c38SMajd Dibbiny 
1966a2a074efSLeon Romanovsky 	return 0;
1967e126ba97SEli Cohen 
1968a8b92ca1SYishai Hadas out_mdev:
1969d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1970146d2f1aSmajd@mellanox.com 
1971e126ba97SEli Cohen out_uars:
1972b037c29aSEli Cohen 	deallocate_uars(dev, context);
1973b037c29aSEli Cohen 
1974b037c29aSEli Cohen out_sys_pages:
1975b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1976b037c29aSEli Cohen 
1977e126ba97SEli Cohen out_count:
19782f5ff264SEli Cohen 	kfree(bfregi->count);
1979e126ba97SEli Cohen 
1980d2c8a155SMeir Lichtinger out_devx:
1981d2c8a155SMeir Lichtinger 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
1982d2c8a155SMeir Lichtinger 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1983d2c8a155SMeir Lichtinger 
1984e126ba97SEli Cohen out_ctx:
1985a2a074efSLeon Romanovsky 	return err;
1986e126ba97SEli Cohen }
1987e126ba97SEli Cohen 
19880fb556b2SYishai Hadas static int mlx5_ib_query_ucontext(struct ib_ucontext *ibcontext,
19890fb556b2SYishai Hadas 				  struct uverbs_attr_bundle *attrs)
19900fb556b2SYishai Hadas {
19910fb556b2SYishai Hadas 	struct mlx5_ib_alloc_ucontext_resp uctx_resp = {};
19920fb556b2SYishai Hadas 	int ret;
19930fb556b2SYishai Hadas 
19940fb556b2SYishai Hadas 	ret = set_ucontext_resp(ibcontext, &uctx_resp);
19950fb556b2SYishai Hadas 	if (ret)
19960fb556b2SYishai Hadas 		return ret;
19970fb556b2SYishai Hadas 
19980fb556b2SYishai Hadas 	uctx_resp.response_length =
19990fb556b2SYishai Hadas 		min_t(size_t,
20000fb556b2SYishai Hadas 		      uverbs_attr_get_len(attrs,
20010fb556b2SYishai Hadas 				MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX),
20020fb556b2SYishai Hadas 		      sizeof(uctx_resp));
20030fb556b2SYishai Hadas 
20040fb556b2SYishai Hadas 	ret = uverbs_copy_to_struct_or_zero(attrs,
20050fb556b2SYishai Hadas 					MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
20060fb556b2SYishai Hadas 					&uctx_resp,
20070fb556b2SYishai Hadas 					sizeof(uctx_resp));
20080fb556b2SYishai Hadas 	return ret;
20090fb556b2SYishai Hadas }
20100fb556b2SYishai Hadas 
2011a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
2012e126ba97SEli Cohen {
2013e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2014e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
2015b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
2016e126ba97SEli Cohen 
2017b037c29aSEli Cohen 	bfregi = &context->bfregi;
2018d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2019d2d19121SYishai Hadas 
2020b037c29aSEli Cohen 	deallocate_uars(dev, context);
2021b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
20222f5ff264SEli Cohen 	kfree(bfregi->count);
2023d2c8a155SMeir Lichtinger 
2024d2c8a155SMeir Lichtinger 	if (context->devx_uid)
2025d2c8a155SMeir Lichtinger 		mlx5_ib_devx_destroy(dev, context->devx_uid);
2026e126ba97SEli Cohen }
2027e126ba97SEli Cohen 
2028b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
20294ed131d0SYishai Hadas 				 int uar_idx)
2030e126ba97SEli Cohen {
2031b037c29aSEli Cohen 	int fw_uars_per_page;
2032b037c29aSEli Cohen 
2033b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2034b037c29aSEli Cohen 
2035aa8106f1SHuy Nguyen 	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2036e126ba97SEli Cohen }
2037e126ba97SEli Cohen 
2038342ee59dSYishai Hadas static u64 uar_index2paddress(struct mlx5_ib_dev *dev,
2039342ee59dSYishai Hadas 				 int uar_idx)
2040342ee59dSYishai Hadas {
2041342ee59dSYishai Hadas 	unsigned int fw_uars_per_page;
2042342ee59dSYishai Hadas 
2043342ee59dSYishai Hadas 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
2044342ee59dSYishai Hadas 				MLX5_UARS_IN_PAGE : 1;
2045342ee59dSYishai Hadas 
2046342ee59dSYishai Hadas 	return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE);
2047342ee59dSYishai Hadas }
2048342ee59dSYishai Hadas 
2049e126ba97SEli Cohen static int get_command(unsigned long offset)
2050e126ba97SEli Cohen {
2051e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2052e126ba97SEli Cohen }
2053e126ba97SEli Cohen 
2054e126ba97SEli Cohen static int get_arg(unsigned long offset)
2055e126ba97SEli Cohen {
2056e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2057e126ba97SEli Cohen }
2058e126ba97SEli Cohen 
2059e126ba97SEli Cohen static int get_index(unsigned long offset)
2060e126ba97SEli Cohen {
2061e126ba97SEli Cohen 	return get_arg(offset);
2062e126ba97SEli Cohen }
2063e126ba97SEli Cohen 
20644ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
20654ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20664ed131d0SYishai Hadas {
20674ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20684ed131d0SYishai Hadas }
20694ed131d0SYishai Hadas 
20707c2344c3SMaor Gottlieb 
20717c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20727c2344c3SMaor Gottlieb {
20737c2344c3SMaor Gottlieb }
20747c2344c3SMaor Gottlieb 
207537aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2076e126ba97SEli Cohen {
207737aa5c36SGuy Levi 	switch (cmd) {
207837aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
207937aa5c36SGuy Levi 		return "WC";
2080e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
208137aa5c36SGuy Levi 		return "best effort WC";
208237aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
208337aa5c36SGuy Levi 		return "NC";
208424da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
208524da0016SAriel Levkovich 		return "Device Memory";
208637aa5c36SGuy Levi 	default:
2087dab994bcSShay Drory 		return "Unknown";
208837aa5c36SGuy Levi 	}
208937aa5c36SGuy Levi }
209037aa5c36SGuy Levi 
20915c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20925c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20935c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20945c99eaecSFeras Daoud {
20954eb6ab13SJason Gunthorpe 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
20964eb6ab13SJason Gunthorpe 	    !(vma->vm_flags & VM_SHARED))
20975c99eaecSFeras Daoud 		return -EINVAL;
20985c99eaecSFeras Daoud 
20995c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
21005c99eaecSFeras Daoud 		return -EOPNOTSUPP;
21015c99eaecSFeras Daoud 
21024eb6ab13SJason Gunthorpe 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
21035c99eaecSFeras Daoud 		return -EPERM;
21041c71222eSSuren Baghdasaryan 	vm_flags_clear(vma, VM_MAYWRITE);
21055c99eaecSFeras Daoud 
2106ddcdc368SJason Gunthorpe 	if (!dev->mdev->clock_info)
21075c99eaecSFeras Daoud 		return -EOPNOTSUPP;
21085c99eaecSFeras Daoud 
21094eb6ab13SJason Gunthorpe 	return vm_insert_page(vma, vma->vm_start,
21104eb6ab13SJason Gunthorpe 			      virt_to_page(dev->mdev->clock_info));
21115c99eaecSFeras Daoud }
21125c99eaecSFeras Daoud 
2113dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
2114dc2316ebSYishai Hadas {
2115dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
2116dc2316ebSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device);
21177be76befSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
2118d2c8a155SMeir Lichtinger 	struct mlx5_ib_ucontext *context = to_mucontext(entry->ucontext);
2119dc2316ebSYishai Hadas 
2120dc2316ebSYishai Hadas 	switch (mentry->mmap_flag) {
2121dc2316ebSYishai Hadas 	case MLX5_IB_MMAP_TYPE_MEMIC:
2122cea85fa5SMaor Gottlieb 	case MLX5_IB_MMAP_TYPE_MEMIC_OP:
2123cea85fa5SMaor Gottlieb 		mlx5_ib_dm_mmap_free(dev, mentry);
2124dc2316ebSYishai Hadas 		break;
21257be76befSYishai Hadas 	case MLX5_IB_MMAP_TYPE_VAR:
21267be76befSYishai Hadas 		mutex_lock(&var_table->bitmap_lock);
21277be76befSYishai Hadas 		clear_bit(mentry->page_idx, var_table->bitmap);
21287be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
21297be76befSYishai Hadas 		kfree(mentry);
21307be76befSYishai Hadas 		break;
2131342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_WC:
2132342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_NC:
2133d2c8a155SMeir Lichtinger 		mlx5_cmd_uar_dealloc(dev->mdev, mentry->page_idx,
2134d2c8a155SMeir Lichtinger 				     context->devx_uid);
2135342ee59dSYishai Hadas 		kfree(mentry);
2136342ee59dSYishai Hadas 		break;
2137dc2316ebSYishai Hadas 	default:
2138dc2316ebSYishai Hadas 		WARN_ON(true);
2139dc2316ebSYishai Hadas 	}
2140dc2316ebSYishai Hadas }
2141dc2316ebSYishai Hadas 
214237aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
21437c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
21447c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
214537aa5c36SGuy Levi {
21462f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
214737aa5c36SGuy Levi 	int err;
214837aa5c36SGuy Levi 	unsigned long idx;
2149aa09ea6eSKamal Heib 	phys_addr_t pfn;
215037aa5c36SGuy Levi 	pgprot_t prot;
21514ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
21524ed131d0SYishai Hadas 	u32 uar_index;
21534ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
21544ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
21554ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2156b037c29aSEli Cohen 
21570a2fd01cSYishai Hadas 	if (bfregi->lib_uar_dyn)
21580a2fd01cSYishai Hadas 		return -EINVAL;
21590a2fd01cSYishai Hadas 
2160b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2161b037c29aSEli Cohen 		return -EINVAL;
2162b037c29aSEli Cohen 
21634ed131d0SYishai Hadas 	if (dyn_uar)
21644ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
21654ed131d0SYishai Hadas 	else
2166b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
21674ed131d0SYishai Hadas 
21684ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21694ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21704ed131d0SYishai Hadas 			     idx, max_valid_idx);
2171b037c29aSEli Cohen 		return -EINVAL;
2172b037c29aSEli Cohen 	}
217337aa5c36SGuy Levi 
217437aa5c36SGuy Levi 	switch (cmd) {
217537aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21764ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
217737aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
217837aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
217937aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
218037aa5c36SGuy Levi 		break;
218137aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
218237aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
218337aa5c36SGuy Levi 		break;
218437aa5c36SGuy Levi 	default:
218537aa5c36SGuy Levi 		return -EINVAL;
218637aa5c36SGuy Levi 	}
218737aa5c36SGuy Levi 
21884ed131d0SYishai Hadas 	if (dyn_uar) {
21894ed131d0SYishai Hadas 		int uars_per_page;
21904ed131d0SYishai Hadas 
21914ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21924ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21934ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21944ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21954ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21964ed131d0SYishai Hadas 			return -EINVAL;
21974ed131d0SYishai Hadas 		}
21984ed131d0SYishai Hadas 
21994ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
22004ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
22014ed131d0SYishai Hadas 		 * page holds its count.
22024ed131d0SYishai Hadas 		 */
22034ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
22044ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
22054ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
22064ed131d0SYishai Hadas 			return -EINVAL;
22074ed131d0SYishai Hadas 		}
22084ed131d0SYishai Hadas 
22094ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
22104ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
22114ed131d0SYishai Hadas 
2212d2c8a155SMeir Lichtinger 		err = mlx5_cmd_uar_alloc(dev->mdev, &uar_index,
2213d2c8a155SMeir Lichtinger 					 context->devx_uid);
22144ed131d0SYishai Hadas 		if (err) {
22154ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
22164ed131d0SYishai Hadas 			goto free_bfreg;
22174ed131d0SYishai Hadas 		}
22184ed131d0SYishai Hadas 	} else {
22194ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
22204ed131d0SYishai Hadas 	}
22214ed131d0SYishai Hadas 
22224ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
222337aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2224e126ba97SEli Cohen 
2225e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2226c043ff2cSMichal Kalderon 				prot, NULL);
222737aa5c36SGuy Levi 	if (err) {
22288f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2229e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
22308f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
22314ed131d0SYishai Hadas 		goto err;
223237aa5c36SGuy Levi 	}
2233e126ba97SEli Cohen 
22344ed131d0SYishai Hadas 	if (dyn_uar)
22354ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
22364ed131d0SYishai Hadas 	return 0;
22374ed131d0SYishai Hadas 
22384ed131d0SYishai Hadas err:
22394ed131d0SYishai Hadas 	if (!dyn_uar)
22404ed131d0SYishai Hadas 		return err;
22414ed131d0SYishai Hadas 
2242d2c8a155SMeir Lichtinger 	mlx5_cmd_uar_dealloc(dev->mdev, idx, context->devx_uid);
22434ed131d0SYishai Hadas 
22444ed131d0SYishai Hadas free_bfreg:
22454ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
22464ed131d0SYishai Hadas 
22474ed131d0SYishai Hadas 	return err;
224837aa5c36SGuy Levi }
224937aa5c36SGuy Levi 
2250dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma)
2251dc2316ebSYishai Hadas {
2252dc2316ebSYishai Hadas 	unsigned long idx;
2253dc2316ebSYishai Hadas 	u8 command;
2254dc2316ebSYishai Hadas 
2255dc2316ebSYishai Hadas 	command = get_command(vma->vm_pgoff);
2256dc2316ebSYishai Hadas 	idx = get_extended_index(vma->vm_pgoff);
2257dc2316ebSYishai Hadas 
2258dc2316ebSYishai Hadas 	return (command << 16 | idx);
2259dc2316ebSYishai Hadas }
2260dc2316ebSYishai Hadas 
2261dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev,
2262dc2316ebSYishai Hadas 			       struct vm_area_struct *vma,
2263dc2316ebSYishai Hadas 			       struct ib_ucontext *ucontext)
2264dc2316ebSYishai Hadas {
2265dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry;
2266dc2316ebSYishai Hadas 	struct rdma_user_mmap_entry *entry;
2267dc2316ebSYishai Hadas 	unsigned long pgoff;
2268dc2316ebSYishai Hadas 	pgprot_t prot;
2269dc2316ebSYishai Hadas 	phys_addr_t pfn;
2270dc2316ebSYishai Hadas 	int ret;
2271dc2316ebSYishai Hadas 
2272dc2316ebSYishai Hadas 	pgoff = mlx5_vma_to_pgoff(vma);
2273dc2316ebSYishai Hadas 	entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff);
2274dc2316ebSYishai Hadas 	if (!entry)
227524da0016SAriel Levkovich 		return -EINVAL;
227624da0016SAriel Levkovich 
2277dc2316ebSYishai Hadas 	mentry = to_mmmap(entry);
2278dc2316ebSYishai Hadas 	pfn = (mentry->address >> PAGE_SHIFT);
2279342ee59dSYishai Hadas 	if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR ||
2280342ee59dSYishai Hadas 	    mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC)
22813f59b6c3SYishai Hadas 		prot = pgprot_noncached(vma->vm_page_prot);
22823f59b6c3SYishai Hadas 	else
2283dc2316ebSYishai Hadas 		prot = pgprot_writecombine(vma->vm_page_prot);
2284dc2316ebSYishai Hadas 	ret = rdma_user_mmap_io(ucontext, vma, pfn,
2285dc2316ebSYishai Hadas 				entry->npages * PAGE_SIZE,
2286dc2316ebSYishai Hadas 				prot,
2287dc2316ebSYishai Hadas 				entry);
2288dc2316ebSYishai Hadas 	rdma_user_mmap_entry_put(&mentry->rdma_entry);
2289dc2316ebSYishai Hadas 	return ret;
229024da0016SAriel Levkovich }
229124da0016SAriel Levkovich 
22927be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry)
22937be76befSYishai Hadas {
22949b6d3bbcSLeon Romanovsky 	u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF;
22959b6d3bbcSLeon Romanovsky 	u64 index = entry->rdma_entry.start_pgoff & 0xFFFF;
22967be76befSYishai Hadas 
22977be76befSYishai Hadas 	return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) |
22987be76befSYishai Hadas 		(index & 0xFF)) << PAGE_SHIFT;
22997be76befSYishai Hadas }
23007be76befSYishai Hadas 
230137aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
230237aa5c36SGuy Levi {
230337aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
230437aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
230537aa5c36SGuy Levi 	unsigned long command;
230637aa5c36SGuy Levi 	phys_addr_t pfn;
230737aa5c36SGuy Levi 
230837aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
230937aa5c36SGuy Levi 	switch (command) {
231037aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
23111f3db161SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
23121f3db161SYishai Hadas 		if (!dev->wc_support)
23131f3db161SYishai Hadas 			return -EPERM;
23141f3db161SYishai Hadas 		fallthrough;
231537aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
231637aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
23177c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2318e126ba97SEli Cohen 
2319e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2320e126ba97SEli Cohen 		return -ENOSYS;
2321e126ba97SEli Cohen 
2322d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2323d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2324d69e3bcfSMatan Barak 			return -EINVAL;
2325d69e3bcfSMatan Barak 
23266cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2327d69e3bcfSMatan Barak 			return -EPERM;
23281c71222eSSuren Baghdasaryan 		vm_flags_clear(vma, VM_MAYWRITE);
2329d69e3bcfSMatan Barak 
2330d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2331d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2332d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2333d69e3bcfSMatan Barak 
2334d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2335d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2336d69e3bcfSMatan Barak 			PAGE_SHIFT;
2337d5e560d3SJason Gunthorpe 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2338d5e560d3SJason Gunthorpe 					 PAGE_SIZE,
2339c043ff2cSMichal Kalderon 					 pgprot_noncached(vma->vm_page_prot),
2340c043ff2cSMichal Kalderon 					 NULL);
23415c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
23425c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2343d69e3bcfSMatan Barak 
2344e126ba97SEli Cohen 	default:
2345dc2316ebSYishai Hadas 		return mlx5_ib_mmap_offset(dev, vma, ibcontext);
2346e126ba97SEli Cohen 	}
2347e126ba97SEli Cohen 
2348e126ba97SEli Cohen 	return 0;
2349e126ba97SEli Cohen }
2350e126ba97SEli Cohen 
2351ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2352e126ba97SEli Cohen {
235321a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
235421a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2355e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2356e126ba97SEli Cohen 	int err;
2357a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2358a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2359a1069c1cSYishai Hadas 	u16 uid = 0;
2360ff23dfa1SShamir Rabinovitch 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2361ff23dfa1SShamir Rabinovitch 		udata, struct mlx5_ib_ucontext, ibucontext);
2362e126ba97SEli Cohen 
2363ff23dfa1SShamir Rabinovitch 	uid = context ? context->devx_uid : 0;
2364a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2365a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
236631578defSLeon Romanovsky 	err = mlx5_cmd_exec_inout(to_mdev(ibdev)->mdev, alloc_pd, in, out);
236721a428a0SLeon Romanovsky 	if (err)
236821a428a0SLeon Romanovsky 		return err;
2369e126ba97SEli Cohen 
2370a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2371a1069c1cSYishai Hadas 	pd->uid = uid;
2372ff23dfa1SShamir Rabinovitch 	if (udata) {
2373e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2374e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2375a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
237621a428a0SLeon Romanovsky 			return -EFAULT;
2377e126ba97SEli Cohen 		}
2378e126ba97SEli Cohen 	}
2379e126ba97SEli Cohen 
238021a428a0SLeon Romanovsky 	return 0;
2381e126ba97SEli Cohen }
2382e126ba97SEli Cohen 
238391a7c58fSLeon Romanovsky static int mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2384e126ba97SEli Cohen {
2385e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2386e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2387e126ba97SEli Cohen 
238891a7c58fSLeon Romanovsky 	return mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2389e126ba97SEli Cohen }
2390e126ba97SEli Cohen 
2391e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2392e126ba97SEli Cohen {
2393e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
239481e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
2395e126ba97SEli Cohen 	int err;
2396539ec982SYishai Hadas 	u16 uid;
2397539ec982SYishai Hadas 
2398539ec982SYishai Hadas 	uid = ibqp->pd ?
2399539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
2400e126ba97SEli Cohen 
24012be08c30SLeon Romanovsky 	if (mqp->flags & IB_QP_CREATE_SOURCE_QPN) {
240281e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
240381e30880SYishai Hadas 		return -EOPNOTSUPP;
240481e30880SYishai Hadas 	}
240581e30880SYishai Hadas 
2406539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2407e126ba97SEli Cohen 	if (err)
2408e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2409e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2410e126ba97SEli Cohen 
2411e126ba97SEli Cohen 	return err;
2412e126ba97SEli Cohen }
2413e126ba97SEli Cohen 
2414e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2415e126ba97SEli Cohen {
2416e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2417e126ba97SEli Cohen 	int err;
2418539ec982SYishai Hadas 	u16 uid;
2419e126ba97SEli Cohen 
2420539ec982SYishai Hadas 	uid = ibqp->pd ?
2421539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
2422539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2423e126ba97SEli Cohen 	if (err)
2424e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2425e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2426e126ba97SEli Cohen 
2427e126ba97SEli Cohen 	return err;
2428e126ba97SEli Cohen }
2429e126ba97SEli Cohen 
2430e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2431e126ba97SEli Cohen {
24321b5daf11SMajd Dibbiny 	int err;
2433e126ba97SEli Cohen 
24341b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2435e126ba97SEli Cohen 	if (err)
2436e126ba97SEli Cohen 		return err;
24371b5daf11SMajd Dibbiny 
24381b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
24391b5daf11SMajd Dibbiny 
24401b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2441e126ba97SEli Cohen }
2442e126ba97SEli Cohen 
2443508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
2444508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2445e126ba97SEli Cohen {
2446e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
244754747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2448e126ba97SEli Cohen 
24491c7fd726SJoe Perches 	return sysfs_emit(buf, "%d\n", dev->mdev->priv.fw_pages);
2450e126ba97SEli Cohen }
2451508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
2452e126ba97SEli Cohen 
2453508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
2454e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2455e126ba97SEli Cohen {
2456e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
245754747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2458e126ba97SEli Cohen 
24591c7fd726SJoe Perches 	return sysfs_emit(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2460e126ba97SEli Cohen }
2461508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
2462e126ba97SEli Cohen 
2463508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
2464508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2465e126ba97SEli Cohen {
2466e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
246754747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
246854747231SParav Pandit 
24691c7fd726SJoe Perches 	return sysfs_emit(buf, "MT%d\n", dev->mdev->pdev->device);
2470e126ba97SEli Cohen }
2471508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
2472e126ba97SEli Cohen 
2473508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
2474508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
2475e126ba97SEli Cohen {
2476e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
247754747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
247854747231SParav Pandit 
24791c7fd726SJoe Perches 	return sysfs_emit(buf, "%x\n", dev->mdev->rev_id);
2480e126ba97SEli Cohen }
2481508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
2482e126ba97SEli Cohen 
2483508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
2484508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2485e126ba97SEli Cohen {
2486e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
248754747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
248854747231SParav Pandit 
24891c7fd726SJoe Perches 	return sysfs_emit(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
24909603b61dSJack Morgenstein 			  dev->mdev->board_id);
2491e126ba97SEli Cohen }
2492508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
2493e126ba97SEli Cohen 
2494508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
2495508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
2496508a523fSParav Pandit 	&dev_attr_hca_type.attr,
2497508a523fSParav Pandit 	&dev_attr_board_id.attr,
2498508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
2499508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
2500508a523fSParav Pandit 	NULL,
2501508a523fSParav Pandit };
2502e126ba97SEli Cohen 
2503508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
2504508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
2505e126ba97SEli Cohen };
2506e126ba97SEli Cohen 
25077722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
25087722f47eSHaggai Eran {
25097722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
25107722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
25117722f47eSHaggai Eran 			     pkey_change_work);
25127722f47eSHaggai Eran 
25138c9e7f03SLeon Romanovsky 	if (!ports->gsi)
25148c9e7f03SLeon Romanovsky 		/*
25158c9e7f03SLeon Romanovsky 		 * We got this event before device was fully configured
25168c9e7f03SLeon Romanovsky 		 * and MAD registration code wasn't called/finished yet.
25178c9e7f03SLeon Romanovsky 		 */
25188c9e7f03SLeon Romanovsky 		return;
25198c9e7f03SLeon Romanovsky 
25207722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
25217722f47eSHaggai Eran }
25227722f47eSHaggai Eran 
252389ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
252489ea94a7SMaor Gottlieb {
252589ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
252689ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
252789ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
252889ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
252989ea94a7SMaor Gottlieb 	unsigned long flags_qp;
253089ea94a7SMaor Gottlieb 	unsigned long flags_cq;
253189ea94a7SMaor Gottlieb 	unsigned long flags;
253289ea94a7SMaor Gottlieb 
253389ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
253489ea94a7SMaor Gottlieb 
253589ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
253689ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
253789ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
253889ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
253989ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
254089ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
254189ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
254289ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
254389ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
254489ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
254589ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
254689ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
254789ea94a7SMaor Gottlieb 						      &cq_armed_list);
254889ea94a7SMaor Gottlieb 				}
254989ea94a7SMaor Gottlieb 			}
255089ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
255189ea94a7SMaor Gottlieb 		}
255289ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
255389ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
255489ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
255589ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
255689ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
255789ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
255889ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
255989ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
256089ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
256189ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
256289ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
256389ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
256489ea94a7SMaor Gottlieb 							      &cq_armed_list);
256589ea94a7SMaor Gottlieb 					}
256689ea94a7SMaor Gottlieb 				}
256789ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
256889ea94a7SMaor Gottlieb 						       flags_cq);
256989ea94a7SMaor Gottlieb 			}
257089ea94a7SMaor Gottlieb 		}
257189ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
257289ea94a7SMaor Gottlieb 	}
257389ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
257489ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
257589ea94a7SMaor Gottlieb 	 */
257689ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
25774e0e2ea1SYishai Hadas 		mcq->comp(mcq, NULL);
257889ea94a7SMaor Gottlieb 	}
257989ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
258089ea94a7SMaor Gottlieb }
258189ea94a7SMaor Gottlieb 
258203404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
258303404e8aSMaor Gottlieb {
258403404e8aSMaor Gottlieb 	int err;
258503404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
258603404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
258703404e8aSMaor Gottlieb 			     delay_drop_work);
258803404e8aSMaor Gottlieb 
2589fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
2590fe248c3aSMaor Gottlieb 
259103404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
2592333fbaa0SLeon Romanovsky 	err = mlx5_core_set_delay_drop(delay_drop->dev, delay_drop->timeout);
259303404e8aSMaor Gottlieb 	if (err) {
259403404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
259503404e8aSMaor Gottlieb 			     delay_drop->timeout);
259603404e8aSMaor Gottlieb 		delay_drop->activate = false;
259703404e8aSMaor Gottlieb 	}
259803404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
259903404e8aSMaor Gottlieb }
260003404e8aSMaor Gottlieb 
260109e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
260209e574faSSaeed Mahameed 				 struct ib_event *ibev)
260309e574faSSaeed Mahameed {
26041fb7f897SMark Bloch 	u32 port = (eqe->data.port.port >> 4) & 0xf;
26056cfdc7e4SAya Levin 
260609e574faSSaeed Mahameed 	switch (eqe->sub_type) {
260709e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
26086cfdc7e4SAya Levin 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
26096cfdc7e4SAya Levin 					    IB_LINK_LAYER_ETHERNET)
261009e574faSSaeed Mahameed 			schedule_work(&ibdev->delay_drop.delay_drop_work);
261109e574faSSaeed Mahameed 		break;
261209e574faSSaeed Mahameed 	default: /* do nothing */
261309e574faSSaeed Mahameed 		return;
261409e574faSSaeed Mahameed 	}
261509e574faSSaeed Mahameed }
261609e574faSSaeed Mahameed 
2617134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
2618134e9349SSaeed Mahameed 			      struct ib_event *ibev)
2619134e9349SSaeed Mahameed {
26201fb7f897SMark Bloch 	u32 port = (eqe->data.port.port >> 4) & 0xf;
2621134e9349SSaeed Mahameed 
2622134e9349SSaeed Mahameed 	ibev->element.port_num = port;
2623134e9349SSaeed Mahameed 
2624134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
2625134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
2626134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
2627134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
2628134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
2629134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
2630134e9349SSaeed Mahameed 		 */
2631134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2632134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
2633134e9349SSaeed Mahameed 			return -EINVAL;
2634134e9349SSaeed Mahameed 
2635134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
2636134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2637134e9349SSaeed Mahameed 		break;
2638134e9349SSaeed Mahameed 
2639134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
2640134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
2641134e9349SSaeed Mahameed 		break;
2642134e9349SSaeed Mahameed 
2643134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
2644134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
2645134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2646134e9349SSaeed Mahameed 		break;
2647134e9349SSaeed Mahameed 
2648134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
2649134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
2650134e9349SSaeed Mahameed 		break;
2651134e9349SSaeed Mahameed 
2652134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
2653134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
2654134e9349SSaeed Mahameed 		break;
2655134e9349SSaeed Mahameed 	default:
2656134e9349SSaeed Mahameed 		return -EINVAL;
2657134e9349SSaeed Mahameed 	}
2658134e9349SSaeed Mahameed 
2659134e9349SSaeed Mahameed 	return 0;
2660134e9349SSaeed Mahameed }
2661134e9349SSaeed Mahameed 
2662d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
2663e126ba97SEli Cohen {
2664d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
2665d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
2666d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
2667e126ba97SEli Cohen 	struct ib_event ibev;
2668dbaaff2aSEli Cohen 	bool fatal = false;
2669e126ba97SEli Cohen 
2670df097a27SSaeed Mahameed 	if (work->is_slave) {
2671df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
2672d69a24e0SDaniel Jurgens 		if (!ibdev)
2673d69a24e0SDaniel Jurgens 			goto out;
2674d69a24e0SDaniel Jurgens 	} else {
2675df097a27SSaeed Mahameed 		ibdev = work->dev;
2676d69a24e0SDaniel Jurgens 	}
2677d69a24e0SDaniel Jurgens 
2678d69a24e0SDaniel Jurgens 	switch (work->event) {
2679e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2680e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
268189ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2682134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
2683dbaaff2aSEli Cohen 		fatal = true;
2684e126ba97SEli Cohen 		break;
2685134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
2686134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
2687d69a24e0SDaniel Jurgens 			goto out;
2688e126ba97SEli Cohen 		break;
268909e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
269009e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
2691df561f66SGustavo A. R. Silva 		fallthrough;
2692bdc37924SSaeed Mahameed 	default:
269303404e8aSMaor Gottlieb 		goto out;
2694e126ba97SEli Cohen 	}
2695e126ba97SEli Cohen 
2696e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
2697e126ba97SEli Cohen 
2698134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
2699134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
270003404e8aSMaor Gottlieb 		goto out;
2701a0c84c32SEli Cohen 	}
2702a0c84c32SEli Cohen 
2703e126ba97SEli Cohen 	if (ibdev->ib_active)
2704e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2705dbaaff2aSEli Cohen 
2706dbaaff2aSEli Cohen 	if (fatal)
2707dbaaff2aSEli Cohen 		ibdev->ib_active = false;
270803404e8aSMaor Gottlieb out:
2709d69a24e0SDaniel Jurgens 	kfree(work);
2710d69a24e0SDaniel Jurgens }
2711d69a24e0SDaniel Jurgens 
2712df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
2713df097a27SSaeed Mahameed 			 unsigned long event, void *param)
2714d69a24e0SDaniel Jurgens {
2715d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
2716d69a24e0SDaniel Jurgens 
2717d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
271810bea9c8SLeon Romanovsky 	if (!work)
2719df097a27SSaeed Mahameed 		return NOTIFY_DONE;
272010bea9c8SLeon Romanovsky 
2721d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
2722df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
2723df097a27SSaeed Mahameed 	work->is_slave = false;
2724d69a24e0SDaniel Jurgens 	work->param = param;
2725d69a24e0SDaniel Jurgens 	work->event = event;
2726d69a24e0SDaniel Jurgens 
2727d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
2728df097a27SSaeed Mahameed 
2729df097a27SSaeed Mahameed 	return NOTIFY_OK;
2730df097a27SSaeed Mahameed }
2731df097a27SSaeed Mahameed 
2732df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
2733df097a27SSaeed Mahameed 				    unsigned long event, void *param)
2734df097a27SSaeed Mahameed {
2735df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
2736df097a27SSaeed Mahameed 
2737df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
2738df097a27SSaeed Mahameed 	if (!work)
2739df097a27SSaeed Mahameed 		return NOTIFY_DONE;
2740df097a27SSaeed Mahameed 
2741df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
2742df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
2743df097a27SSaeed Mahameed 	work->is_slave = true;
2744df097a27SSaeed Mahameed 	work->param = param;
2745df097a27SSaeed Mahameed 	work->event = event;
2746df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
2747df097a27SSaeed Mahameed 
2748df097a27SSaeed Mahameed 	return NOTIFY_OK;
2749d69a24e0SDaniel Jurgens }
2750d69a24e0SDaniel Jurgens 
2751c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2752c43f1112SMaor Gottlieb {
2753c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
2754c43f1112SMaor Gottlieb 	int err;
2755c43f1112SMaor Gottlieb 	int port;
2756c43f1112SMaor Gottlieb 
27574b83c3caSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_IB)
27584b83c3caSMark Bloch 		return 0;
27594b83c3caSMark Bloch 
27604b83c3caSMark Bloch 	for (port = 1; port <= dev->num_ports; port++) {
27614b83c3caSMark Bloch 		if (!MLX5_CAP_GEN(dev->mdev, ib_virt)) {
27624b83c3caSMark Bloch 			dev->port_caps[port - 1].has_smi = true;
27634b83c3caSMark Bloch 			continue;
27644b83c3caSMark Bloch 		}
27654b83c3caSMark Bloch 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port, 0,
2766c43f1112SMaor Gottlieb 						   &vport_ctx);
2767c43f1112SMaor Gottlieb 		if (err) {
2768c43f1112SMaor Gottlieb 			mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2769c43f1112SMaor Gottlieb 				    port, err);
2770c43f1112SMaor Gottlieb 			return err;
2771c43f1112SMaor Gottlieb 		}
27724b83c3caSMark Bloch 		dev->port_caps[port - 1].has_smi = vport_ctx.has_smi;
2773c43f1112SMaor Gottlieb 	}
27744b83c3caSMark Bloch 
2775c43f1112SMaor Gottlieb 	return 0;
2776c43f1112SMaor Gottlieb }
2777c43f1112SMaor Gottlieb 
2778e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2779e126ba97SEli Cohen {
278013179652SParav Pandit 	unsigned int port;
2781e126ba97SEli Cohen 
278213179652SParav Pandit 	rdma_for_each_port (&dev->ib_dev, port)
2783e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
2784e126ba97SEli Cohen }
2785e126ba97SEli Cohen 
27866e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
27876e8484c5SMax Gurtovoy {
27886e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
27896e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
27906e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
27916e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
27926e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
27936e8484c5SMax Gurtovoy 	default:
27946e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
27956e8484c5SMax Gurtovoy 	}
27966e8484c5SMax Gurtovoy }
27976e8484c5SMax Gurtovoy 
27981e2b5a90SLeon Romanovsky static int mlx5_ib_dev_res_init(struct mlx5_ib_dev *dev)
2799e126ba97SEli Cohen {
28001e2b5a90SLeon Romanovsky 	struct mlx5_ib_resources *devr = &dev->devr;
2801e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
280221a428a0SLeon Romanovsky 	struct ib_device *ibdev;
2803bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
28047722f47eSHaggai Eran 	int port;
2805e126ba97SEli Cohen 	int ret = 0;
2806e126ba97SEli Cohen 
280721a428a0SLeon Romanovsky 	ibdev = &dev->ib_dev;
2808e126ba97SEli Cohen 
2809f4375443SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, xrc))
2810f4375443SLeon Romanovsky 		return -EOPNOTSUPP;
2811f4375443SLeon Romanovsky 
281220da44dfSLeon Romanovsky 	devr->p0 = ib_alloc_pd(ibdev, 0);
281320da44dfSLeon Romanovsky 	if (IS_ERR(devr->p0))
281420da44dfSLeon Romanovsky 		return PTR_ERR(devr->p0);
2815d16e91daSHaggai Eran 
281620da44dfSLeon Romanovsky 	devr->c0 = ib_create_cq(ibdev, NULL, NULL, NULL, &cq_attr);
281720da44dfSLeon Romanovsky 	if (IS_ERR(devr->c0)) {
281820da44dfSLeon Romanovsky 		ret = PTR_ERR(devr->c0);
2819e126ba97SEli Cohen 		goto error1;
2820e126ba97SEli Cohen 	}
2821e39afe3dSLeon Romanovsky 
2822f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn0, 0);
2823f4375443SLeon Romanovsky 	if (ret)
2824e126ba97SEli Cohen 		goto error2;
2825e126ba97SEli Cohen 
2826f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn1, 0);
2827f4375443SLeon Romanovsky 	if (ret)
2828e126ba97SEli Cohen 		goto error3;
2829e126ba97SEli Cohen 
2830e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
2831e126ba97SEli Cohen 	attr.attr.max_sge = 1;
2832e126ba97SEli Cohen 	attr.attr.max_wr = 1;
2833e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
28341a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
2835e126ba97SEli Cohen 
283620da44dfSLeon Romanovsky 	devr->s0 = ib_create_srq(devr->p0, &attr);
283720da44dfSLeon Romanovsky 	if (IS_ERR(devr->s0)) {
283820da44dfSLeon Romanovsky 		ret = PTR_ERR(devr->s0);
283968e326deSLeon Romanovsky 		goto err_create;
284020da44dfSLeon Romanovsky 	}
2841e126ba97SEli Cohen 
28424aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
28434aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
28444aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
28454aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
284668e326deSLeon Romanovsky 
284720da44dfSLeon Romanovsky 	devr->s1 = ib_create_srq(devr->p0, &attr);
284820da44dfSLeon Romanovsky 	if (IS_ERR(devr->s1)) {
284920da44dfSLeon Romanovsky 		ret = PTR_ERR(devr->s1);
285068e326deSLeon Romanovsky 		goto error6;
285120da44dfSLeon Romanovsky 	}
28524aa17b28SHaggai Abramonvsky 
2853f8225e34SLeon Romanovsky 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
28547722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
28557722f47eSHaggai Eran 			  pkey_change_handler);
28567722f47eSHaggai Eran 
2857e126ba97SEli Cohen 	return 0;
2858e126ba97SEli Cohen 
285968e326deSLeon Romanovsky error6:
286020da44dfSLeon Romanovsky 	ib_destroy_srq(devr->s0);
286168e326deSLeon Romanovsky err_create:
2862f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
2863e126ba97SEli Cohen error3:
2864f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
2865e126ba97SEli Cohen error2:
286620da44dfSLeon Romanovsky 	ib_destroy_cq(devr->c0);
2867e126ba97SEli Cohen error1:
286820da44dfSLeon Romanovsky 	ib_dealloc_pd(devr->p0);
2869e126ba97SEli Cohen 	return ret;
2870e126ba97SEli Cohen }
2871e126ba97SEli Cohen 
28721e2b5a90SLeon Romanovsky static void mlx5_ib_dev_res_cleanup(struct mlx5_ib_dev *dev)
2873e126ba97SEli Cohen {
28741e2b5a90SLeon Romanovsky 	struct mlx5_ib_resources *devr = &dev->devr;
28757722f47eSHaggai Eran 	int port;
28767722f47eSHaggai Eran 
2877b0791dbfSLeon Romanovsky 	/*
2878b0791dbfSLeon Romanovsky 	 * Make sure no change P_Key work items are still executing.
2879b0791dbfSLeon Romanovsky 	 *
2880b0791dbfSLeon Romanovsky 	 * At this stage, the mlx5_ib_event should be unregistered
2881b0791dbfSLeon Romanovsky 	 * and it ensures that no new works are added.
2882b0791dbfSLeon Romanovsky 	 */
28835d8f6a0eSMark Bloch 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
28847722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
2885b0791dbfSLeon Romanovsky 
288620da44dfSLeon Romanovsky 	ib_destroy_srq(devr->s1);
288720da44dfSLeon Romanovsky 	ib_destroy_srq(devr->s0);
2888e126ba97SEli Cohen 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
2889e126ba97SEli Cohen 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
289020da44dfSLeon Romanovsky 	ib_destroy_cq(devr->c0);
289120da44dfSLeon Romanovsky 	ib_dealloc_pd(devr->p0);
2892e126ba97SEli Cohen }
2893e126ba97SEli Cohen 
2894b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
2895b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
2896e53505a8SAchiad Shochat {
2897e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2898e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
2899e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
2900e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
290185c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
2902e53505a8SAchiad Shochat 	u32 ret = 0;
2903e53505a8SAchiad Shochat 
2904b02289b3SArtemy Kovalyov 	if (rep->grh_required)
2905b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
2906b02289b3SArtemy Kovalyov 
2907e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
2908b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
2909e53505a8SAchiad Shochat 
291085c7c014SDaniel Jurgens 	if (raw_support)
2911b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
291272cd5717SOr Gerlitz 
2913e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
291472cd5717SOr Gerlitz 		return ret;
2915e53505a8SAchiad Shochat 
2916e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
291772cd5717SOr Gerlitz 		return ret;
2918e53505a8SAchiad Shochat 
2919e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
2920e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
2921e53505a8SAchiad Shochat 
2922e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
2923e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
2924e53505a8SAchiad Shochat 
2925e53505a8SAchiad Shochat 	return ret;
2926e53505a8SAchiad Shochat }
2927e53505a8SAchiad Shochat 
29281fb7f897SMark Bloch static int mlx5_port_immutable(struct ib_device *ibdev, u32 port_num,
29297738613eSIra Weiny 			       struct ib_port_immutable *immutable)
29307738613eSIra Weiny {
29317738613eSIra Weiny 	struct ib_port_attr attr;
2932ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2933ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
2934b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
29357738613eSIra Weiny 	int err;
29367738613eSIra Weiny 
2937c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
29387738613eSIra Weiny 	if (err)
29397738613eSIra Weiny 		return err;
29407738613eSIra Weiny 
2941b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
2942b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
2943b02289b3SArtemy Kovalyov 						   &rep);
2944b02289b3SArtemy Kovalyov 		if (err)
2945b02289b3SArtemy Kovalyov 			return err;
2946b02289b3SArtemy Kovalyov 	}
2947b02289b3SArtemy Kovalyov 
29487738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
29497738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
2950b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
2951337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
29527738613eSIra Weiny 
29537738613eSIra Weiny 	return 0;
29547738613eSIra Weiny }
29557738613eSIra Weiny 
29561fb7f897SMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u32 port_num,
29578e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
29588e6efa3aSMark Bloch {
29598e6efa3aSMark Bloch 	struct ib_port_attr attr;
29608e6efa3aSMark Bloch 	int err;
29618e6efa3aSMark Bloch 
29628e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
29638e6efa3aSMark Bloch 
29648e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
29658e6efa3aSMark Bloch 	if (err)
29668e6efa3aSMark Bloch 		return err;
29678e6efa3aSMark Bloch 
29688e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
29698e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
29708e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
29718e6efa3aSMark Bloch 
29728e6efa3aSMark Bloch 	return 0;
29738e6efa3aSMark Bloch }
29748e6efa3aSMark Bloch 
29759abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
2976c7342823SIra Weiny {
2977c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
2978c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
29799abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
29809abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
29819abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
2982c7342823SIra Weiny }
2983c7342823SIra Weiny 
298445f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
29859ef9c640SAviv Heller {
29869ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
29879ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
29889ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
29899ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
29909ef9c640SAviv Heller 	int err;
29919ef9c640SAviv Heller 
2992c446d9daSMark Bloch 	if (!ns || !mlx5_lag_is_active(mdev))
29939ef9c640SAviv Heller 		return 0;
29949ef9c640SAviv Heller 
29959ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
29969ef9c640SAviv Heller 	if (err)
29979ef9c640SAviv Heller 		return err;
29989ef9c640SAviv Heller 
29999ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
30009ef9c640SAviv Heller 	if (IS_ERR(ft)) {
30019ef9c640SAviv Heller 		err = PTR_ERR(ft);
30029ef9c640SAviv Heller 		goto err_destroy_vport_lag;
30039ef9c640SAviv Heller 	}
30049ef9c640SAviv Heller 
30059a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
300634a30d76SMark Bloch 	dev->lag_ports = mlx5_lag_get_num_ports(mdev);
30077c34ec19SAviv Heller 	dev->lag_active = true;
30089ef9c640SAviv Heller 	return 0;
30099ef9c640SAviv Heller 
30109ef9c640SAviv Heller err_destroy_vport_lag:
30119ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
30129ef9c640SAviv Heller 	return err;
30139ef9c640SAviv Heller }
30149ef9c640SAviv Heller 
301545f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
30169ef9c640SAviv Heller {
30179ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
30189ef9c640SAviv Heller 
30197c34ec19SAviv Heller 	if (dev->lag_active) {
30207c34ec19SAviv Heller 		dev->lag_active = false;
30217c34ec19SAviv Heller 
30229a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
30239a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
30249ef9c640SAviv Heller 
30259ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
30269ef9c640SAviv Heller 	}
30279ef9c640SAviv Heller }
30289ef9c640SAviv Heller 
3029dca55da0SJiri Pirko static void mlx5_netdev_notifier_register(struct mlx5_roce *roce,
3030dca55da0SJiri Pirko 					  struct net_device *netdev)
3031fc24fc5eSAchiad Shochat {
3032e53505a8SAchiad Shochat 	int err;
3033e53505a8SAchiad Shochat 
3034dca55da0SJiri Pirko 	if (roce->tracking_netdev)
3035dca55da0SJiri Pirko 		return;
3036dca55da0SJiri Pirko 	roce->tracking_netdev = netdev;
3037dca55da0SJiri Pirko 	roce->nb.notifier_call = mlx5_netdev_event;
3038dca55da0SJiri Pirko 	err = register_netdevice_notifier_dev_net(netdev, &roce->nb, &roce->nn);
3039dca55da0SJiri Pirko 	WARN_ON(err);
30405ec8c83eSAviv Heller }
3041e53505a8SAchiad Shochat 
3042dca55da0SJiri Pirko static void mlx5_netdev_notifier_unregister(struct mlx5_roce *roce)
3043e126ba97SEli Cohen {
3044dca55da0SJiri Pirko 	if (!roce->tracking_netdev)
3045dca55da0SJiri Pirko 		return;
3046dca55da0SJiri Pirko 	unregister_netdevice_notifier_dev_net(roce->tracking_netdev, &roce->nb,
3047dca55da0SJiri Pirko 					      &roce->nn);
3048dca55da0SJiri Pirko 	roce->tracking_netdev = NULL;
3049e126ba97SEli Cohen }
3050dca55da0SJiri Pirko 
3051dca55da0SJiri Pirko static int mlx5e_mdev_notifier_event(struct notifier_block *nb,
3052dca55da0SJiri Pirko 				     unsigned long event, void *data)
3053dca55da0SJiri Pirko {
3054dca55da0SJiri Pirko 	struct mlx5_roce *roce = container_of(nb, struct mlx5_roce, mdev_nb);
3055dca55da0SJiri Pirko 	struct net_device *netdev = data;
3056dca55da0SJiri Pirko 
3057dca55da0SJiri Pirko 	switch (event) {
3058dca55da0SJiri Pirko 	case MLX5_DRIVER_EVENT_UPLINK_NETDEV:
3059dca55da0SJiri Pirko 		if (netdev)
3060dca55da0SJiri Pirko 			mlx5_netdev_notifier_register(roce, netdev);
3061dca55da0SJiri Pirko 		else
3062dca55da0SJiri Pirko 			mlx5_netdev_notifier_unregister(roce);
3063dca55da0SJiri Pirko 		break;
3064dca55da0SJiri Pirko 	default:
3065dca55da0SJiri Pirko 		return NOTIFY_DONE;
3066dca55da0SJiri Pirko 	}
3067dca55da0SJiri Pirko 
3068dca55da0SJiri Pirko 	return NOTIFY_OK;
3069dca55da0SJiri Pirko }
3070dca55da0SJiri Pirko 
3071dca55da0SJiri Pirko static void mlx5_mdev_netdev_track(struct mlx5_ib_dev *dev, u32 port_num)
3072dca55da0SJiri Pirko {
3073dca55da0SJiri Pirko 	struct mlx5_roce *roce = &dev->port[port_num].roce;
3074dca55da0SJiri Pirko 
3075dca55da0SJiri Pirko 	roce->mdev_nb.notifier_call = mlx5e_mdev_notifier_event;
3076dca55da0SJiri Pirko 	mlx5_blocking_notifier_register(dev->mdev, &roce->mdev_nb);
3077dca55da0SJiri Pirko 	mlx5_core_uplink_netdev_event_replay(dev->mdev);
3078dca55da0SJiri Pirko }
3079dca55da0SJiri Pirko 
3080dca55da0SJiri Pirko static void mlx5_mdev_netdev_untrack(struct mlx5_ib_dev *dev, u32 port_num)
3081dca55da0SJiri Pirko {
3082dca55da0SJiri Pirko 	struct mlx5_roce *roce = &dev->port[port_num].roce;
3083dca55da0SJiri Pirko 
3084dca55da0SJiri Pirko 	mlx5_blocking_notifier_unregister(dev->mdev, &roce->mdev_nb);
3085dca55da0SJiri Pirko 	mlx5_netdev_notifier_unregister(roce);
3086e126ba97SEli Cohen }
3087e126ba97SEli Cohen 
3088e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3089e126ba97SEli Cohen {
3090e126ba97SEli Cohen 	int err;
3091e126ba97SEli Cohen 
3092c446d9daSMark Bloch 	if (!dev->is_rep && dev->profile != &raw_eth_profile) {
3093e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3094e53505a8SAchiad Shochat 		if (err)
30958e6efa3aSMark Bloch 			return err;
3096c446d9daSMark Bloch 	}
3097e53505a8SAchiad Shochat 
309845f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
30999ef9c640SAviv Heller 	if (err)
31009ef9c640SAviv Heller 		goto err_disable_roce;
31019ef9c640SAviv Heller 
3102e53505a8SAchiad Shochat 	return 0;
3103e53505a8SAchiad Shochat 
31049ef9c640SAviv Heller err_disable_roce:
3105c446d9daSMark Bloch 	if (!dev->is_rep && dev->profile != &raw_eth_profile)
31069ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
31079ef9c640SAviv Heller 
3108e53505a8SAchiad Shochat 	return err;
3109fc24fc5eSAchiad Shochat }
3110fc24fc5eSAchiad Shochat 
311145f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3112fc24fc5eSAchiad Shochat {
311345f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3114c446d9daSMark Bloch 	if (!dev->is_rep && dev->profile != &raw_eth_profile)
3115e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
3116fc24fc5eSAchiad Shochat }
3117fc24fc5eSAchiad Shochat 
31181fb7f897SMark Bloch static int mlx5_ib_rn_get_params(struct ib_device *device, u32 port_num,
3119693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
3120f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
3121693dfd5aSErez Shitrit {
3122693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
3123f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
3124693dfd5aSErez Shitrit 
3125f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
3126693dfd5aSErez Shitrit }
3127693dfd5aSErez Shitrit 
3128fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3129fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
3130fe248c3aSMaor Gottlieb {
3131fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3132fe248c3aSMaor Gottlieb 	char lbuf[20];
3133fe248c3aSMaor Gottlieb 	int len;
3134fe248c3aSMaor Gottlieb 
3135fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3136fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
3137fe248c3aSMaor Gottlieb }
3138fe248c3aSMaor Gottlieb 
3139fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3140fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
3141fe248c3aSMaor Gottlieb {
3142fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3143fe248c3aSMaor Gottlieb 	u32 timeout;
3144fe248c3aSMaor Gottlieb 	u32 var;
3145fe248c3aSMaor Gottlieb 
3146fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
3147fe248c3aSMaor Gottlieb 		return -EFAULT;
3148fe248c3aSMaor Gottlieb 
3149fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3150fe248c3aSMaor Gottlieb 			1000);
3151fe248c3aSMaor Gottlieb 	if (timeout != var)
3152fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3153fe248c3aSMaor Gottlieb 			    timeout);
3154fe248c3aSMaor Gottlieb 
3155fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
3156fe248c3aSMaor Gottlieb 
3157fe248c3aSMaor Gottlieb 	return count;
3158fe248c3aSMaor Gottlieb }
3159fe248c3aSMaor Gottlieb 
3160fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
3161fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
3162fe248c3aSMaor Gottlieb 	.open	= simple_open,
3163fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
3164fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
3165fe248c3aSMaor Gottlieb };
3166fe248c3aSMaor Gottlieb 
316732f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
316832f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
316932f69e4bSDaniel Jurgens {
31701fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
317132f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
317232f69e4bSDaniel Jurgens 	int comps;
317332f69e4bSDaniel Jurgens 	int err;
317432f69e4bSDaniel Jurgens 	int i;
317532f69e4bSDaniel Jurgens 
31769dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
31779dc4cfffSLeon Romanovsky 
3178a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
3179a9e546e7SParav Pandit 
318032f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
318132f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
318232f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
318332f69e4bSDaniel Jurgens 		return;
318432f69e4bSDaniel Jurgens 	}
3185df097a27SSaeed Mahameed 
318632f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
318732f69e4bSDaniel Jurgens 
318832f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
318923eaf3b5SLeon Romanovsky 	if (mpi->mdev_events.notifier_call)
319023eaf3b5SLeon Romanovsky 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
319123eaf3b5SLeon Romanovsky 	mpi->mdev_events.notifier_call = NULL;
3192dca55da0SJiri Pirko 	mlx5_mdev_netdev_untrack(ibdev, port_num);
319332f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
319432f69e4bSDaniel Jurgens 
319532f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
319632f69e4bSDaniel Jurgens 	if (comps) {
319732f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
319832f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
319932f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
320032f69e4bSDaniel Jurgens 
320132f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
320232f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
320332f69e4bSDaniel Jurgens 
320432f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
320532f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
320632f69e4bSDaniel Jurgens 	}
320732f69e4bSDaniel Jurgens 
320832f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
320932f69e4bSDaniel Jurgens 
321032f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
321132f69e4bSDaniel Jurgens 
321232f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
321332f69e4bSDaniel Jurgens 
32141fb7f897SMark Bloch 	mlx5_ib_dbg(ibdev, "unaffiliated port %u\n", port_num + 1);
321532f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
321632f69e4bSDaniel Jurgens 	 * it back to the list.
321732f69e4bSDaniel Jurgens 	 */
321832f69e4bSDaniel Jurgens 	if (err)
321932f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
322032f69e4bSDaniel Jurgens 			    port_num + 1);
322132f69e4bSDaniel Jurgens 
322295579e78SMark Bloch 	ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
322332f69e4bSDaniel Jurgens }
322432f69e4bSDaniel Jurgens 
322532f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
322632f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
322732f69e4bSDaniel Jurgens {
32281fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
322932f69e4bSDaniel Jurgens 	int err;
323032f69e4bSDaniel Jurgens 
32319dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
32329dc4cfffSLeon Romanovsky 
323332f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
323432f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
32351fb7f897SMark Bloch 		mlx5_ib_dbg(ibdev, "port %u already affiliated.\n",
323632f69e4bSDaniel Jurgens 			    port_num + 1);
323732f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
323832f69e4bSDaniel Jurgens 		return false;
323932f69e4bSDaniel Jurgens 	}
324032f69e4bSDaniel Jurgens 
324132f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
324232f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
3243df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
324432f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
324532f69e4bSDaniel Jurgens 
324632f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
324732f69e4bSDaniel Jurgens 	if (err)
324832f69e4bSDaniel Jurgens 		goto unbind;
324932f69e4bSDaniel Jurgens 
3250dca55da0SJiri Pirko 	mlx5_mdev_netdev_track(ibdev, port_num);
325132f69e4bSDaniel Jurgens 
3252df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
3253df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
3254df097a27SSaeed Mahameed 
325573eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
3256a9e546e7SParav Pandit 
325732f69e4bSDaniel Jurgens 	return true;
325832f69e4bSDaniel Jurgens 
325932f69e4bSDaniel Jurgens unbind:
326032f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
326132f69e4bSDaniel Jurgens 	return false;
326232f69e4bSDaniel Jurgens }
326332f69e4bSDaniel Jurgens 
326432f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
326532f69e4bSDaniel Jurgens {
32661fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
326732f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
326832f69e4bSDaniel Jurgens 							  port_num + 1);
326932f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
327032f69e4bSDaniel Jurgens 	int err;
32711fb7f897SMark Bloch 	u32 i;
327232f69e4bSDaniel Jurgens 
327332f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
327432f69e4bSDaniel Jurgens 		return 0;
327532f69e4bSDaniel Jurgens 
327632f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
327732f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
327832f69e4bSDaniel Jurgens 	if (err)
327932f69e4bSDaniel Jurgens 		return err;
328032f69e4bSDaniel Jurgens 
328132f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
328232f69e4bSDaniel Jurgens 	if (err)
328332f69e4bSDaniel Jurgens 		return err;
328432f69e4bSDaniel Jurgens 
328532f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
328632f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
328732f69e4bSDaniel Jurgens 		bool bound = false;
328832f69e4bSDaniel Jurgens 
328932f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
329032f69e4bSDaniel Jurgens 		if (i == port_num) {
329132f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
329232f69e4bSDaniel Jurgens 			if (!mpi) {
329332f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
329432f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
329532f69e4bSDaniel Jurgens 				return -ENOMEM;
329632f69e4bSDaniel Jurgens 			}
329732f69e4bSDaniel Jurgens 
329832f69e4bSDaniel Jurgens 			mpi->is_master = true;
329932f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
330032f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
330132f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
330232f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
330332f69e4bSDaniel Jurgens 			mpi = NULL;
330432f69e4bSDaniel Jurgens 			continue;
330532f69e4bSDaniel Jurgens 		}
330632f69e4bSDaniel Jurgens 
330732f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
330832f69e4bSDaniel Jurgens 				    list) {
330932f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
331032f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
331132f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
331232f69e4bSDaniel Jurgens 			}
331332f69e4bSDaniel Jurgens 
331432f69e4bSDaniel Jurgens 			if (bound) {
3315c42260f1SVu Pham 				dev_dbg(mpi->mdev->device,
3316c42260f1SVu Pham 					"removing port from unaffiliated list.\n");
331732f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
331832f69e4bSDaniel Jurgens 				list_del(&mpi->list);
331932f69e4bSDaniel Jurgens 				break;
332032f69e4bSDaniel Jurgens 			}
332132f69e4bSDaniel Jurgens 		}
33227416790eSParav Pandit 		if (!bound)
332332f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
332432f69e4bSDaniel Jurgens 				    i + 1);
332532f69e4bSDaniel Jurgens 	}
332632f69e4bSDaniel Jurgens 
332732f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
332832f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
332932f69e4bSDaniel Jurgens 	return err;
333032f69e4bSDaniel Jurgens }
333132f69e4bSDaniel Jurgens 
333232f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
333332f69e4bSDaniel Jurgens {
33341fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
333532f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
333632f69e4bSDaniel Jurgens 							  port_num + 1);
33371fb7f897SMark Bloch 	u32 i;
333832f69e4bSDaniel Jurgens 
333932f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
334032f69e4bSDaniel Jurgens 		return;
334132f69e4bSDaniel Jurgens 
334232f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
334332f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
334432f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
334532f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
334632f69e4bSDaniel Jurgens 			if (i == port_num) {
334732f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
334832f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
334932f69e4bSDaniel Jurgens 			} else {
33501fb7f897SMark Bloch 				mlx5_ib_dbg(dev, "unbinding port_num: %u\n",
33511fb7f897SMark Bloch 					    i + 1);
33527ce6095eSLeon Romanovsky 				list_add_tail(&dev->port[i].mp.mpi->list,
33537ce6095eSLeon Romanovsky 					      &mlx5_ib_unaffiliated_port_list);
33544a754d76SLeon Romanovsky 				mlx5_ib_unbind_slave_port(dev,
33554a754d76SLeon Romanovsky 							  dev->port[i].mp.mpi);
335632f69e4bSDaniel Jurgens 			}
335732f69e4bSDaniel Jurgens 		}
335832f69e4bSDaniel Jurgens 	}
335932f69e4bSDaniel Jurgens 
336032f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
336132f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
336232f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
336332f69e4bSDaniel Jurgens 
336432f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
336532f69e4bSDaniel Jurgens }
336632f69e4bSDaniel Jurgens 
3367342ee59dSYishai Hadas static int mmap_obj_cleanup(struct ib_uobject *uobject,
33687be76befSYishai Hadas 			    enum rdma_remove_reason why,
33697be76befSYishai Hadas 			    struct uverbs_attr_bundle *attrs)
33707be76befSYishai Hadas {
33717be76befSYishai Hadas 	struct mlx5_user_mmap_entry *obj = uobject->object;
33727be76befSYishai Hadas 
33737be76befSYishai Hadas 	rdma_user_mmap_entry_remove(&obj->rdma_entry);
33747be76befSYishai Hadas 	return 0;
33757be76befSYishai Hadas }
33767be76befSYishai Hadas 
3377342ee59dSYishai Hadas static int mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext *c,
3378342ee59dSYishai Hadas 					    struct mlx5_user_mmap_entry *entry,
3379342ee59dSYishai Hadas 					    size_t length)
3380342ee59dSYishai Hadas {
3381342ee59dSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
3382342ee59dSYishai Hadas 		&c->ibucontext, &entry->rdma_entry, length,
3383342ee59dSYishai Hadas 		(MLX5_IB_MMAP_OFFSET_START << 16),
3384342ee59dSYishai Hadas 		((MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1));
3385342ee59dSYishai Hadas }
3386342ee59dSYishai Hadas 
33877be76befSYishai Hadas static struct mlx5_user_mmap_entry *
33887be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c)
33897be76befSYishai Hadas {
33907be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
33917be76befSYishai Hadas 	struct mlx5_var_table *var_table;
33927be76befSYishai Hadas 	u32 page_idx;
33937be76befSYishai Hadas 	int err;
33947be76befSYishai Hadas 
33957be76befSYishai Hadas 	var_table = &to_mdev(c->ibucontext.device)->var_table;
33967be76befSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
33977be76befSYishai Hadas 	if (!entry)
33987be76befSYishai Hadas 		return ERR_PTR(-ENOMEM);
33997be76befSYishai Hadas 
34007be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
34017be76befSYishai Hadas 	page_idx = find_first_zero_bit(var_table->bitmap,
34027be76befSYishai Hadas 				       var_table->num_var_hw_entries);
34037be76befSYishai Hadas 	if (page_idx >= var_table->num_var_hw_entries) {
34047be76befSYishai Hadas 		err = -ENOSPC;
34057be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
34067be76befSYishai Hadas 		goto end;
34077be76befSYishai Hadas 	}
34087be76befSYishai Hadas 
34097be76befSYishai Hadas 	set_bit(page_idx, var_table->bitmap);
34107be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
34117be76befSYishai Hadas 
34127be76befSYishai Hadas 	entry->address = var_table->hw_start_addr +
34137be76befSYishai Hadas 				(page_idx * var_table->stride_size);
34147be76befSYishai Hadas 	entry->page_idx = page_idx;
34157be76befSYishai Hadas 	entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR;
34167be76befSYishai Hadas 
3417342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry,
3418342ee59dSYishai Hadas 					       var_table->stride_size);
34197be76befSYishai Hadas 	if (err)
34207be76befSYishai Hadas 		goto err_insert;
34217be76befSYishai Hadas 
34227be76befSYishai Hadas 	return entry;
34237be76befSYishai Hadas 
34247be76befSYishai Hadas err_insert:
34257be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
34267be76befSYishai Hadas 	clear_bit(page_idx, var_table->bitmap);
34277be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
34287be76befSYishai Hadas end:
34297be76befSYishai Hadas 	kfree(entry);
34307be76befSYishai Hadas 	return ERR_PTR(err);
34317be76befSYishai Hadas }
34327be76befSYishai Hadas 
34337be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)(
34347be76befSYishai Hadas 	struct uverbs_attr_bundle *attrs)
34357be76befSYishai Hadas {
34367be76befSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
34377be76befSYishai Hadas 		attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
34387be76befSYishai Hadas 	struct mlx5_ib_ucontext *c;
34397be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
34407be76befSYishai Hadas 	u64 mmap_offset;
34417be76befSYishai Hadas 	u32 length;
34427be76befSYishai Hadas 	int err;
34437be76befSYishai Hadas 
34447be76befSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
34457be76befSYishai Hadas 	if (IS_ERR(c))
34467be76befSYishai Hadas 		return PTR_ERR(c);
34477be76befSYishai Hadas 
34487be76befSYishai Hadas 	entry = alloc_var_entry(c);
34497be76befSYishai Hadas 	if (IS_ERR(entry))
34507be76befSYishai Hadas 		return PTR_ERR(entry);
34517be76befSYishai Hadas 
34527be76befSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
34537be76befSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
34547be76befSYishai Hadas 	uobj->object = entry;
34550ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
34567be76befSYishai Hadas 
34577be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
34587be76befSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
34597be76befSYishai Hadas 	if (err)
34600ac8903cSJason Gunthorpe 		return err;
34617be76befSYishai Hadas 
34627be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
34637be76befSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
34647be76befSYishai Hadas 	if (err)
34650ac8903cSJason Gunthorpe 		return err;
34667be76befSYishai Hadas 
34677be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
34687be76befSYishai Hadas 			     &length, sizeof(length));
34697be76befSYishai Hadas 	return err;
34707be76befSYishai Hadas }
34717be76befSYishai Hadas 
34727be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
34737be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_ALLOC,
34747be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE,
34757be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
34767be76befSYishai Hadas 			UVERBS_ACCESS_NEW,
34777be76befSYishai Hadas 			UA_MANDATORY),
34787be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
34797be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
34807be76befSYishai Hadas 			   UA_MANDATORY),
34817be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
34827be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
34837be76befSYishai Hadas 			   UA_MANDATORY),
34847be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
34857be76befSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
34867be76befSYishai Hadas 			    UA_MANDATORY));
34877be76befSYishai Hadas 
34887be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
34897be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_DESTROY,
34907be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE,
34917be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
34927be76befSYishai Hadas 			UVERBS_ACCESS_DESTROY,
34937be76befSYishai Hadas 			UA_MANDATORY));
34947be76befSYishai Hadas 
34957be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR,
3496342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
34977be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC),
34987be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY));
34997be76befSYishai Hadas 
35007be76befSYishai Hadas static bool var_is_supported(struct ib_device *device)
35017be76befSYishai Hadas {
35027be76befSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(device);
35037be76befSYishai Hadas 
35047be76befSYishai Hadas 	return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
35057be76befSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q);
35067be76befSYishai Hadas }
35077be76befSYishai Hadas 
3508342ee59dSYishai Hadas static struct mlx5_user_mmap_entry *
3509342ee59dSYishai Hadas alloc_uar_entry(struct mlx5_ib_ucontext *c,
3510342ee59dSYishai Hadas 		enum mlx5_ib_uapi_uar_alloc_type alloc_type)
3511342ee59dSYishai Hadas {
3512342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
3513342ee59dSYishai Hadas 	struct mlx5_ib_dev *dev;
3514342ee59dSYishai Hadas 	u32 uar_index;
3515342ee59dSYishai Hadas 	int err;
3516342ee59dSYishai Hadas 
3517342ee59dSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
3518342ee59dSYishai Hadas 	if (!entry)
3519342ee59dSYishai Hadas 		return ERR_PTR(-ENOMEM);
3520342ee59dSYishai Hadas 
3521342ee59dSYishai Hadas 	dev = to_mdev(c->ibucontext.device);
3522d2c8a155SMeir Lichtinger 	err = mlx5_cmd_uar_alloc(dev->mdev, &uar_index, c->devx_uid);
3523342ee59dSYishai Hadas 	if (err)
3524342ee59dSYishai Hadas 		goto end;
3525342ee59dSYishai Hadas 
3526342ee59dSYishai Hadas 	entry->page_idx = uar_index;
3527342ee59dSYishai Hadas 	entry->address = uar_index2paddress(dev, uar_index);
3528342ee59dSYishai Hadas 	if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3529342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC;
3530342ee59dSYishai Hadas 	else
3531342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_NC;
3532342ee59dSYishai Hadas 
3533342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry, PAGE_SIZE);
3534342ee59dSYishai Hadas 	if (err)
3535342ee59dSYishai Hadas 		goto err_insert;
3536342ee59dSYishai Hadas 
3537342ee59dSYishai Hadas 	return entry;
3538342ee59dSYishai Hadas 
3539342ee59dSYishai Hadas err_insert:
3540d2c8a155SMeir Lichtinger 	mlx5_cmd_uar_dealloc(dev->mdev, uar_index, c->devx_uid);
3541342ee59dSYishai Hadas end:
3542342ee59dSYishai Hadas 	kfree(entry);
3543342ee59dSYishai Hadas 	return ERR_PTR(err);
3544342ee59dSYishai Hadas }
3545342ee59dSYishai Hadas 
3546342ee59dSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)(
3547342ee59dSYishai Hadas 	struct uverbs_attr_bundle *attrs)
3548342ee59dSYishai Hadas {
3549342ee59dSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
3550342ee59dSYishai Hadas 		attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3551342ee59dSYishai Hadas 	enum mlx5_ib_uapi_uar_alloc_type alloc_type;
3552342ee59dSYishai Hadas 	struct mlx5_ib_ucontext *c;
3553342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
3554342ee59dSYishai Hadas 	u64 mmap_offset;
3555342ee59dSYishai Hadas 	u32 length;
3556342ee59dSYishai Hadas 	int err;
3557342ee59dSYishai Hadas 
3558342ee59dSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
3559342ee59dSYishai Hadas 	if (IS_ERR(c))
3560342ee59dSYishai Hadas 		return PTR_ERR(c);
3561342ee59dSYishai Hadas 
3562342ee59dSYishai Hadas 	err = uverbs_get_const(&alloc_type, attrs,
3563342ee59dSYishai Hadas 			       MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE);
3564342ee59dSYishai Hadas 	if (err)
3565342ee59dSYishai Hadas 		return err;
3566342ee59dSYishai Hadas 
3567342ee59dSYishai Hadas 	if (alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF &&
3568342ee59dSYishai Hadas 	    alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_NC)
3569342ee59dSYishai Hadas 		return -EOPNOTSUPP;
3570342ee59dSYishai Hadas 
3571342ee59dSYishai Hadas 	if (!to_mdev(c->ibucontext.device)->wc_support &&
3572342ee59dSYishai Hadas 	    alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3573342ee59dSYishai Hadas 		return -EOPNOTSUPP;
3574342ee59dSYishai Hadas 
3575342ee59dSYishai Hadas 	entry = alloc_uar_entry(c, alloc_type);
3576342ee59dSYishai Hadas 	if (IS_ERR(entry))
3577342ee59dSYishai Hadas 		return PTR_ERR(entry);
3578342ee59dSYishai Hadas 
3579342ee59dSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
3580342ee59dSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
3581342ee59dSYishai Hadas 	uobj->object = entry;
35820ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3583342ee59dSYishai Hadas 
3584342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3585342ee59dSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
3586342ee59dSYishai Hadas 	if (err)
35870ac8903cSJason Gunthorpe 		return err;
3588342ee59dSYishai Hadas 
3589342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3590342ee59dSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
3591342ee59dSYishai Hadas 	if (err)
35920ac8903cSJason Gunthorpe 		return err;
3593342ee59dSYishai Hadas 
3594342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3595342ee59dSYishai Hadas 			     &length, sizeof(length));
3596342ee59dSYishai Hadas 	return err;
3597342ee59dSYishai Hadas }
3598342ee59dSYishai Hadas 
3599342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
3600342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_ALLOC,
3601342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE,
3602342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
3603342ee59dSYishai Hadas 			UVERBS_ACCESS_NEW,
3604342ee59dSYishai Hadas 			UA_MANDATORY),
3605342ee59dSYishai Hadas 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE,
3606342ee59dSYishai Hadas 			     enum mlx5_ib_uapi_uar_alloc_type,
3607342ee59dSYishai Hadas 			     UA_MANDATORY),
3608342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3609342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
3610342ee59dSYishai Hadas 			   UA_MANDATORY),
3611342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3612342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
3613342ee59dSYishai Hadas 			   UA_MANDATORY),
3614342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3615342ee59dSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
3616342ee59dSYishai Hadas 			    UA_MANDATORY));
3617342ee59dSYishai Hadas 
3618342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
3619342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_DESTROY,
3620342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_DESTROY_HANDLE,
3621342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
3622342ee59dSYishai Hadas 			UVERBS_ACCESS_DESTROY,
3623342ee59dSYishai Hadas 			UA_MANDATORY));
3624342ee59dSYishai Hadas 
3625342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_UAR,
3626342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
3627342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_ALLOC),
3628342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_DESTROY));
3629342ee59dSYishai Hadas 
36309a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
36310fb556b2SYishai Hadas 	mlx5_ib_query_context,
36320fb556b2SYishai Hadas 	UVERBS_OBJECT_DEVICE,
36330fb556b2SYishai Hadas 	UVERBS_METHOD_QUERY_CONTEXT,
36340fb556b2SYishai Hadas 	UVERBS_ATTR_PTR_OUT(
36350fb556b2SYishai Hadas 		MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
36360fb556b2SYishai Hadas 		UVERBS_ATTR_STRUCT(struct mlx5_ib_alloc_ucontext_resp,
36370fb556b2SYishai Hadas 				   dump_fill_mkey),
36380fb556b2SYishai Hadas 		UA_MANDATORY));
36390fb556b2SYishai Hadas 
36400cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
364136e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
36420cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
364330f2fe40SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_qos_defs),
364405f71ef9SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_std_types_defs),
3645831df883SMaor Gottlieb 	UAPI_DEF_CHAIN(mlx5_ib_dm_defs),
36468c84660bSMatan Barak 
36470fb556b2SYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DEVICE, &mlx5_ib_query_context),
36487be76befSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR,
36497be76befSYishai Hadas 				UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)),
3650342ee59dSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_UAR),
36510cbf432dSJason Gunthorpe 	{}
36520cbf432dSJason Gunthorpe };
36538c84660bSMatan Barak 
3654fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
3655e126ba97SEli Cohen {
365632f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
3657806b101bSJason Gunthorpe 	WARN_ON(!xa_empty(&dev->odp_mkeys));
3658ab40530aSParav Pandit 	mutex_destroy(&dev->cap_mask_mutex);
365950211ec9SJason Gunthorpe 	WARN_ON(!xa_empty(&dev->sig_mrs));
36604056b12eSAriel Levkovich 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
3661758ce14aSPatrisious Haddad 	mlx5r_macsec_dealloc_gids(dev);
366216c1975fSMark Bloch }
366316c1975fSMark Bloch 
3664fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
366516c1975fSMark Bloch {
366616c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
3667758ce14aSPatrisious Haddad 	int err, i;
3668e126ba97SEli Cohen 
366913179652SParav Pandit 	dev->ib_dev.node_type = RDMA_NODE_IB_CA;
367013179652SParav Pandit 	dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
367113179652SParav Pandit 	dev->ib_dev.phys_port_cnt = dev->num_ports;
367213179652SParav Pandit 	dev->ib_dev.dev.parent = mdev->device;
367313179652SParav Pandit 	dev->ib_dev.lag_flags = RDMA_LAG_FLAGS_HASH_ALL_SLAVES;
367413179652SParav Pandit 
367532f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
367632f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
367795579e78SMark Bloch 		rwlock_init(&dev->port[i].roce.netdev_lock);
3678d3b5cc1cSMark Bloch 		dev->port[i].roce.dev = dev;
3679d3b5cc1cSMark Bloch 		dev->port[i].roce.native_port_num = i + 1;
3680d3b5cc1cSMark Bloch 		dev->port[i].roce.last_port_state = IB_PORT_DOWN;
368132f69e4bSDaniel Jurgens 	}
368232f69e4bSDaniel Jurgens 
3683594cac11SOr Har-Toov 	err = mlx5r_cmd_query_special_mkeys(dev);
3684594cac11SOr Har-Toov 	if (err)
3685594cac11SOr Har-Toov 		return err;
3686594cac11SOr Har-Toov 
368758dbd642SPatrisious Haddad 	err = mlx5r_macsec_init_gids_and_devlist(dev);
3688e126ba97SEli Cohen 	if (err)
3689da796ccbSMark Bloch 		return err;
3690e126ba97SEli Cohen 
3691758ce14aSPatrisious Haddad 	err = mlx5_ib_init_multiport_master(dev);
3692758ce14aSPatrisious Haddad 	if (err)
3693758ce14aSPatrisious Haddad 		goto err;
3694758ce14aSPatrisious Haddad 
3695a989ea01SMark Bloch 	err = set_has_smi_cap(dev);
3696a989ea01SMark Bloch 	if (err)
36972cb091f6SParav Pandit 		goto err_mp;
3698e126ba97SEli Cohen 
36992019d70eSParav Pandit 	err = mlx5_query_max_pkeys(&dev->ib_dev, &dev->pkey_table_len);
37002019d70eSParav Pandit 	if (err)
37012019d70eSParav Pandit 		goto err_mp;
37022019d70eSParav Pandit 
37031b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3704e126ba97SEli Cohen 		get_ext_port_caps(dev);
3705e126ba97SEli Cohen 
3706674dd4e2SMaher Sanalla 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_max(mdev);
3707e126ba97SEli Cohen 
37083cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
37093cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
37103cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
3711806b101bSJason Gunthorpe 	xa_init(&dev->odp_mkeys);
371250211ec9SJason Gunthorpe 	xa_init(&dev->sig_mrs);
3713f743ff3bSSaeed Mahameed 	atomic_set(&dev->mkey_var, 0);
37143cc297dbSMark Bloch 
37153b113a1eSAriel Levkovich 	spin_lock_init(&dev->dm.lock);
37163b113a1eSAriel Levkovich 	dev->dm.dev = mdev;
371716c1975fSMark Bloch 	return 0;
3718758ce14aSPatrisious Haddad err:
3719758ce14aSPatrisious Haddad 	mlx5r_macsec_dealloc_gids(dev);
372032f69e4bSDaniel Jurgens err_mp:
372132f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
3722d286ac1dSParav Pandit 	return err;
372316c1975fSMark Bloch }
372416c1975fSMark Bloch 
3725ac47bf5eSLeon Romanovsky static int mlx5_ib_enable_driver(struct ib_device *dev)
37269a4ca38dSMark Bloch {
3727ac47bf5eSLeon Romanovsky 	struct mlx5_ib_dev *mdev = to_mdev(dev);
3728ac47bf5eSLeon Romanovsky 	int ret;
37299a4ca38dSMark Bloch 
3730ac47bf5eSLeon Romanovsky 	ret = mlx5_ib_test_wc(mdev);
3731ac47bf5eSLeon Romanovsky 	mlx5_ib_dbg(mdev, "Write-Combining %s",
3732ac47bf5eSLeon Romanovsky 		    mdev->wc_support ? "supported" : "not supported");
37339a4ca38dSMark Bloch 
3734ac47bf5eSLeon Romanovsky 	return ret;
37359a4ca38dSMark Bloch }
37369a4ca38dSMark Bloch 
373796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
37387a154142SJason Gunthorpe 	.owner = THIS_MODULE,
3739b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_MLX5,
374072c6ec18SJason Gunthorpe 	.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION,
3741b9560a41SJason Gunthorpe 
374296458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
374396458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
37446c984472SMax Gurtovoy 	.alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
374596458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
374696458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
374796458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
374896458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
374996458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
375096458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
375196458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
375296458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
3753676a80adSJason Gunthorpe 	.create_user_ah = mlx5_ib_create_ah,
375496458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
375596458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
375696458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
375796458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
375896458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
375996458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
376096458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
376196458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
376296458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
376396458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
376496458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
376596458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
3766915e4af5SJason Gunthorpe 	.device_group = &mlx5_attr_group,
376711f552e2SMichael Guralnik 	.enable_driver = mlx5_ib_enable_driver,
376896458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
376996458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
377096458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
377196458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
37726c984472SMax Gurtovoy 	.map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
377396458233SKamal Heib 	.mmap = mlx5_ib_mmap,
3774dc2316ebSYishai Hadas 	.mmap_free = mlx5_ib_mmap_free,
377596458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
377696458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
377796458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
377896458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
377996458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
378096458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
3781029e88fdSLeon Romanovsky 	.post_recv = mlx5_ib_post_recv_nodrain,
3782029e88fdSLeon Romanovsky 	.post_send = mlx5_ib_post_send_nodrain,
378396458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
378496458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
378596458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
378696458233SKamal Heib 	.query_device = mlx5_ib_query_device,
378796458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
378896458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
378996458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
379096458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
37910fb556b2SYishai Hadas 	.query_ucontext = mlx5_ib_query_ucontext,
379296458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
379390da7dc8SJianxin Xiong 	.reg_user_mr_dmabuf = mlx5_ib_reg_user_mr_dmabuf,
379496458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
379596458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
379696458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
3797d3456914SLeon Romanovsky 
3798d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
37993b023e1bSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_counters, mlx5_ib_mcounters, ibcntrs),
3800e39afe3dSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
380121a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
3802514aee66SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_qp, mlx5_ib_qp, ibqp),
380368e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
3804a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
380596458233SKamal Heib };
380696458233SKamal Heib 
380796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
380896458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
380996458233SKamal Heib };
381096458233SKamal Heib 
381196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
381296458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
38139c0015efSDanit Goldberg 	.get_vf_guid = mlx5_ib_get_vf_guid,
381496458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
381596458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
381696458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
381796458233SKamal Heib };
381896458233SKamal Heib 
381996458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
382096458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
382196458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
3822d18bb3e1SLeon Romanovsky 
3823d18bb3e1SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_mw, mlx5_ib_mw, ibmw),
382496458233SKamal Heib };
382596458233SKamal Heib 
382696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
382796458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
382896458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
382928ad5f65SLeon Romanovsky 
383028ad5f65SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_xrcd, mlx5_ib_xrcd, ibxrcd),
383196458233SKamal Heib };
383296458233SKamal Heib 
3833f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev)
3834f164be8cSYishai Hadas {
3835f164be8cSYishai Hadas 	struct mlx5_core_dev *mdev = dev->mdev;
3836f164be8cSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
3837f164be8cSYishai Hadas 	u8 log_doorbell_bar_size;
3838f164be8cSYishai Hadas 	u8 log_doorbell_stride;
3839f164be8cSYishai Hadas 	u64 bar_size;
3840f164be8cSYishai Hadas 
3841f164be8cSYishai Hadas 	log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
3842f164be8cSYishai Hadas 					log_doorbell_bar_size);
3843f164be8cSYishai Hadas 	log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
3844f164be8cSYishai Hadas 					log_doorbell_stride);
3845f164be8cSYishai Hadas 	var_table->hw_start_addr = dev->mdev->bar_addr +
3846f164be8cSYishai Hadas 				MLX5_CAP64_DEV_VDPA_EMULATION(mdev,
3847f164be8cSYishai Hadas 					doorbell_bar_offset);
3848f164be8cSYishai Hadas 	bar_size = (1ULL << log_doorbell_bar_size) * 4096;
3849f164be8cSYishai Hadas 	var_table->stride_size = 1ULL << log_doorbell_stride;
385091b74bf5SAlexander Lobakin 	var_table->num_var_hw_entries = div_u64(bar_size,
385191b74bf5SAlexander Lobakin 						var_table->stride_size);
3852f164be8cSYishai Hadas 	mutex_init(&var_table->bitmap_lock);
3853f164be8cSYishai Hadas 	var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries,
3854f164be8cSYishai Hadas 					  GFP_KERNEL);
3855f164be8cSYishai Hadas 	return (var_table->bitmap) ? 0 : -ENOMEM;
3856f164be8cSYishai Hadas }
3857f164be8cSYishai Hadas 
3858f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev)
3859f164be8cSYishai Hadas {
3860f164be8cSYishai Hadas 	bitmap_free(dev->var_table.bitmap);
3861f164be8cSYishai Hadas }
3862f164be8cSYishai Hadas 
3863fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
386416c1975fSMark Bloch {
386516c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
386616c1975fSMark Bloch 	int err;
386716c1975fSMark Bloch 
3868f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
3869f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
387096458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
387196458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
38728e959601SNiranjana Vishwanathapura 
387396458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
387496458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
38757c2344c3SMaor Gottlieb 
38766e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
38776e8484c5SMax Gurtovoy 
387844ce37bcSJason Gunthorpe 	if (MLX5_CAP_GEN(mdev, imaicl))
387996458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
3880d2370e0aSMatan Barak 
388144ce37bcSJason Gunthorpe 	if (MLX5_CAP_GEN(mdev, xrc))
388296458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
3883e126ba97SEli Cohen 
388425c13324SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic) ||
388525c13324SAriel Levkovich 	    MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
388625c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
388796458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
388824da0016SAriel Levkovich 
388996458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
389081e30880SYishai Hadas 
389136e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
389236e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
389381e30880SYishai Hadas 
3894e126ba97SEli Cohen 	err = init_node_data(dev);
3895e126ba97SEli Cohen 	if (err)
389616c1975fSMark Bloch 		return err;
3897e126ba97SEli Cohen 
3898c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
3899e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
3900e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
3901a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
3902c8b89924SMark Bloch 
3903f164be8cSYishai Hadas 	if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
3904f164be8cSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) {
3905f164be8cSYishai Hadas 		err = mlx5_ib_init_var_table(dev);
3906f164be8cSYishai Hadas 		if (err)
3907f164be8cSYishai Hadas 			return err;
3908f164be8cSYishai Hadas 	}
3909f164be8cSYishai Hadas 
391096e2fd73SLeon Romanovsky 	dev->ib_dev.use_cq_dim = true;
391196e2fd73SLeon Romanovsky 
391216c1975fSMark Bloch 	return 0;
391316c1975fSMark Bloch }
391416c1975fSMark Bloch 
391596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
391696458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
391796458233SKamal Heib 	.query_port = mlx5_ib_query_port,
391896458233SKamal Heib };
391996458233SKamal Heib 
39208e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
392116c1975fSMark Bloch {
392296458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
39238e6efa3aSMark Bloch 	return 0;
39248e6efa3aSMark Bloch }
39258e6efa3aSMark Bloch 
392696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
392796458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
392896458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
3929d6fd59e1SParav Pandit 	.query_pkey = mlx5_ib_rep_query_pkey,
393096458233SKamal Heib };
393196458233SKamal Heib 
3932b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev)
39338e6efa3aSMark Bloch {
393496458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
39358e6efa3aSMark Bloch 	return 0;
39368e6efa3aSMark Bloch }
39378e6efa3aSMark Bloch 
393896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
393996458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
394096458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
394196458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
394296458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
394396458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
394496458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
3945c0a6b5ecSLeon Romanovsky 
3946c0a6b5ecSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_rwq_ind_table, mlx5_ib_rwq_ind_table,
3947c0a6b5ecSLeon Romanovsky 			   ib_rwq_ind_tbl),
394896458233SKamal Heib };
394996458233SKamal Heib 
39501e2b5a90SLeon Romanovsky static int mlx5_ib_roce_init(struct mlx5_ib_dev *dev)
39518e6efa3aSMark Bloch {
39521e2b5a90SLeon Romanovsky 	struct mlx5_core_dev *mdev = dev->mdev;
39531e2b5a90SLeon Romanovsky 	enum rdma_link_layer ll;
39541e2b5a90SLeon Romanovsky 	int port_type_cap;
39551fb7f897SMark Bloch 	u32 port_num = 0;
39561e2b5a90SLeon Romanovsky 	int err;
39577fd8aefbSDaniel Jurgens 
39581e2b5a90SLeon Romanovsky 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
39591e2b5a90SLeon Romanovsky 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
39601e2b5a90SLeon Romanovsky 
39611e2b5a90SLeon Romanovsky 	if (ll == IB_LINK_LAYER_ETHERNET) {
396296458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
39638e6efa3aSMark Bloch 
3964e3f1ed1fSLeon Romanovsky 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
3965e3f1ed1fSLeon Romanovsky 
396626628e2dSMark Bloch 		/* Register only for native ports */
3967dca55da0SJiri Pirko 		mlx5_mdev_netdev_track(dev, port_num);
39688e6efa3aSMark Bloch 
3969e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
39708e6efa3aSMark Bloch 		if (err)
39718e6efa3aSMark Bloch 			goto cleanup;
39728e6efa3aSMark Bloch 	}
39738e6efa3aSMark Bloch 
397416c1975fSMark Bloch 	return 0;
39758e6efa3aSMark Bloch cleanup:
3976dca55da0SJiri Pirko 	mlx5_mdev_netdev_untrack(dev, port_num);
39778e6efa3aSMark Bloch 	return err;
397845bded2cSKamal Heib }
39796aec21f6SHaggai Eran 
39801e2b5a90SLeon Romanovsky static void mlx5_ib_roce_cleanup(struct mlx5_ib_dev *dev)
398116c1975fSMark Bloch {
398216c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
398316c1975fSMark Bloch 	enum rdma_link_layer ll;
398416c1975fSMark Bloch 	int port_type_cap;
39851fb7f897SMark Bloch 	u32 port_num;
39864a2da0b8SParav Pandit 
398716c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
398816c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
398916c1975fSMark Bloch 
399016c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
399116c1975fSMark Bloch 		mlx5_disable_eth(dev);
399216c1975fSMark Bloch 
39931e2b5a90SLeon Romanovsky 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
3994dca55da0SJiri Pirko 		mlx5_mdev_netdev_untrack(dev, port_num);
399516c1975fSMark Bloch 	}
399616c1975fSMark Bloch }
399716c1975fSMark Bloch 
399816c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
399916c1975fSMark Bloch {
400073eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
4001a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
400273eb8f03SGreg Kroah-Hartman 	return 0;
400316c1975fSMark Bloch }
400416c1975fSMark Bloch 
400516c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
400616c1975fSMark Bloch {
4007a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
4008a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
400916c1975fSMark Bloch }
401016c1975fSMark Bloch 
401116c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
401216c1975fSMark Bloch {
40135fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
4014444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
401516c1975fSMark Bloch }
401616c1975fSMark Bloch 
401716c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
401816c1975fSMark Bloch {
401916c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
402016c1975fSMark Bloch }
402116c1975fSMark Bloch 
4022fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
402316c1975fSMark Bloch {
402416c1975fSMark Bloch 	int err;
40255fe9dec0SEli Cohen 
40265fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
40275fe9dec0SEli Cohen 	if (err)
402816c1975fSMark Bloch 		return err;
40295fe9dec0SEli Cohen 
40305fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
40315fe9dec0SEli Cohen 	if (err)
40321c3aa6bdSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->bfreg);
40335fe9dec0SEli Cohen 
403416c1975fSMark Bloch 	return err;
403516c1975fSMark Bloch }
40360837e86aSMark Bloch 
4037fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
403816c1975fSMark Bloch {
403916c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
404016c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
404116c1975fSMark Bloch }
4042e126ba97SEli Cohen 
4043fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
404416c1975fSMark Bloch {
4045e349f858SJason Gunthorpe 	const char *name;
4046e349f858SJason Gunthorpe 
4047c446d9daSMark Bloch 	if (!mlx5_lag_is_active(dev->mdev))
4048e349f858SJason Gunthorpe 		name = "mlx5_%d";
4049e349f858SJason Gunthorpe 	else
4050e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
4051e0477b34SJason Gunthorpe 	return ib_register_device(&dev->ib_dev, name, &dev->mdev->pdev->dev);
405216c1975fSMark Bloch }
405316c1975fSMark Bloch 
4054fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
405542cea83fSMark Bloch {
405685f9e38aSLeon Romanovsky 	mlx5_mkey_cache_cleanup(dev);
405704876c12SAharon Landau 	mlx5r_umr_resource_cleanup(dev);
405842cea83fSMark Bloch }
405942cea83fSMark Bloch 
4060fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
406116c1975fSMark Bloch {
406216c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
406316c1975fSMark Bloch }
406416c1975fSMark Bloch 
4065fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
406616c1975fSMark Bloch {
40671e2b5a90SLeon Romanovsky 	int ret;
40681e2b5a90SLeon Romanovsky 
406904876c12SAharon Landau 	ret = mlx5r_umr_resource_init(dev);
407004876c12SAharon Landau 	if (ret)
407104876c12SAharon Landau 		return ret;
40721e2b5a90SLeon Romanovsky 
407301137808SAharon Landau 	ret = mlx5_mkey_cache_init(dev);
4074*6387f269SGeorge Kennedy 	if (ret)
40751e2b5a90SLeon Romanovsky 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
40761e2b5a90SLeon Romanovsky 	return ret;
407716c1975fSMark Bloch }
407816c1975fSMark Bloch 
407916c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
408016c1975fSMark Bloch {
40811e2b5a90SLeon Romanovsky 	struct dentry *root;
408203404e8aSMaor Gottlieb 
40831e2b5a90SLeon Romanovsky 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
40841e2b5a90SLeon Romanovsky 		return 0;
40851e2b5a90SLeon Romanovsky 
40861e2b5a90SLeon Romanovsky 	mutex_init(&dev->delay_drop.lock);
40871e2b5a90SLeon Romanovsky 	dev->delay_drop.dev = dev;
40881e2b5a90SLeon Romanovsky 	dev->delay_drop.activate = false;
40891e2b5a90SLeon Romanovsky 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
40901e2b5a90SLeon Romanovsky 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
40911e2b5a90SLeon Romanovsky 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
40921e2b5a90SLeon Romanovsky 	atomic_set(&dev->delay_drop.events_cnt, 0);
40931e2b5a90SLeon Romanovsky 
40941e2b5a90SLeon Romanovsky 	if (!mlx5_debugfs_root)
40951e2b5a90SLeon Romanovsky 		return 0;
40961e2b5a90SLeon Romanovsky 
409766771a1cSMoshe Shemesh 	root = debugfs_create_dir("delay_drop", mlx5_debugfs_get_dev_root(dev->mdev));
40981e2b5a90SLeon Romanovsky 	dev->delay_drop.dir_debugfs = root;
40991e2b5a90SLeon Romanovsky 
41001e2b5a90SLeon Romanovsky 	debugfs_create_atomic_t("num_timeout_events", 0400, root,
41011e2b5a90SLeon Romanovsky 				&dev->delay_drop.events_cnt);
41021e2b5a90SLeon Romanovsky 	debugfs_create_atomic_t("num_rqs", 0400, root,
41031e2b5a90SLeon Romanovsky 				&dev->delay_drop.rqs_cnt);
41041e2b5a90SLeon Romanovsky 	debugfs_create_file("timeout", 0600, root, &dev->delay_drop,
41051e2b5a90SLeon Romanovsky 			    &fops_delay_drop_timeout);
410616c1975fSMark Bloch 	return 0;
410716c1975fSMark Bloch }
410816c1975fSMark Bloch 
410916c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
411016c1975fSMark Bloch {
41111e2b5a90SLeon Romanovsky 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
41121e2b5a90SLeon Romanovsky 		return;
41131e2b5a90SLeon Romanovsky 
41141e2b5a90SLeon Romanovsky 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
41151e2b5a90SLeon Romanovsky 	if (!dev->delay_drop.dir_debugfs)
41161e2b5a90SLeon Romanovsky 		return;
41171e2b5a90SLeon Romanovsky 
41181e2b5a90SLeon Romanovsky 	debugfs_remove_recursive(dev->delay_drop.dir_debugfs);
41191e2b5a90SLeon Romanovsky 	dev->delay_drop.dir_debugfs = NULL;
412016c1975fSMark Bloch }
412116c1975fSMark Bloch 
4122df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
4123df097a27SSaeed Mahameed {
4124df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
4125df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
412658dbd642SPatrisious Haddad 
412758dbd642SPatrisious Haddad 	mlx5r_macsec_event_register(dev);
412858dbd642SPatrisious Haddad 
4129df097a27SSaeed Mahameed 	return 0;
4130df097a27SSaeed Mahameed }
4131df097a27SSaeed Mahameed 
4132df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
4133df097a27SSaeed Mahameed {
413458dbd642SPatrisious Haddad 	mlx5r_macsec_event_unregister(dev);
4135df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
4136df097a27SSaeed Mahameed }
4137df097a27SSaeed Mahameed 
4138b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
413916c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
414016c1975fSMark Bloch 		      int stage)
414116c1975fSMark Bloch {
41424cca96a8SParav Pandit 	dev->ib_active = false;
41434cca96a8SParav Pandit 
414416c1975fSMark Bloch 	/* Number of stages to cleanup */
414516c1975fSMark Bloch 	while (stage) {
414616c1975fSMark Bloch 		stage--;
414716c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
414816c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
414916c1975fSMark Bloch 	}
41504a6dc855SMark Bloch 
4151da796ccbSMark Bloch 	kfree(dev->port);
41524a6dc855SMark Bloch 	ib_dealloc_device(&dev->ib_dev);
415316c1975fSMark Bloch }
415416c1975fSMark Bloch 
415593f82444SLeon Romanovsky int __mlx5_ib_add(struct mlx5_ib_dev *dev,
415616c1975fSMark Bloch 		  const struct mlx5_ib_profile *profile)
415716c1975fSMark Bloch {
415816c1975fSMark Bloch 	int err;
415916c1975fSMark Bloch 	int i;
416016c1975fSMark Bloch 
41618d93efb8SMark Bloch 	dev->profile = profile;
41628d93efb8SMark Bloch 
416316c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
416416c1975fSMark Bloch 		if (profile->stage[i].init) {
416516c1975fSMark Bloch 			err = profile->stage[i].init(dev);
416616c1975fSMark Bloch 			if (err)
416716c1975fSMark Bloch 				goto err_out;
416816c1975fSMark Bloch 		}
416916c1975fSMark Bloch 	}
417016c1975fSMark Bloch 
4171e126ba97SEli Cohen 	dev->ib_active = true;
417293f82444SLeon Romanovsky 	return 0;
4173e126ba97SEli Cohen 
417416c1975fSMark Bloch err_out:
417593f82444SLeon Romanovsky 	/* Clean up stages which were initialized */
417693f82444SLeon Romanovsky 	while (i) {
417793f82444SLeon Romanovsky 		i--;
417893f82444SLeon Romanovsky 		if (profile->stage[i].cleanup)
417993f82444SLeon Romanovsky 			profile->stage[i].cleanup(dev);
418093f82444SLeon Romanovsky 	}
418193f82444SLeon Romanovsky 	return -ENOMEM;
4182e126ba97SEli Cohen }
4183e126ba97SEli Cohen 
418416c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
418516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
418616c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
418716c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
4188f7c4ffdaSLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_FS,
4189f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_init,
4190f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_cleanup),
419116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
419216c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
4193f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
41948e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
41958e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
41968e6efa3aSMark Bloch 		     NULL),
419716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
41981e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_init,
41991e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_cleanup),
4200333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
4201333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
4202333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
4203f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4204f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
4205f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
420616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
42071e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_init,
42081e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_cleanup),
4209df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4210df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
4211df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
421216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
42131e2b5a90SLeon Romanovsky 		     mlx5_ib_odp_init_one,
42141e2b5a90SLeon Romanovsky 		     mlx5_ib_odp_cleanup_one),
421516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
421664825827SLeon Romanovsky 		     mlx5_ib_counters_init,
421764825827SLeon Romanovsky 		     mlx5_ib_counters_cleanup),
421816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
421916c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
422016c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
422116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
422216c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
422316c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
422416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
422516c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
422616c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
422742cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
422842cea83fSMark Bloch 		     NULL,
422942cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
423081773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4231d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_init,
4232d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_cleanup),
423316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
423416c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
423516c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
423642cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
423742cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
423842cea83fSMark Bloch 		     NULL),
423916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
424016c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
424116c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
4242b572ebe6SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4243b572ebe6SLeon Romanovsky 		     mlx5_ib_restrack_init,
4244b572ebe6SLeon Romanovsky 		     NULL),
424516c1975fSMark Bloch };
424616c1975fSMark Bloch 
4247b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = {
4248b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
4249b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
4250b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
4251f7c4ffdaSLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_FS,
4252f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_init,
4253f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_cleanup),
4254b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
4255b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
4256f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
4257b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
4258b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_non_default_cb,
4259b5ca15adSMark Bloch 		     NULL),
4260b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
42611e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_init,
42621e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_cleanup),
4263333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
4264333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
4265333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
4266f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4267f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
4268f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
4269b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
42701e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_init,
42711e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_cleanup),
4272df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4273df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
4274df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
4275b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
427664825827SLeon Romanovsky 		     mlx5_ib_counters_init,
427764825827SLeon Romanovsky 		     mlx5_ib_counters_cleanup),
427879db784eSParav Pandit 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
427979db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_init,
428079db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_cleanup),
4281b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
4282b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
4283b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
4284b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
4285b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
4286b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
428703fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
428803fe2debSDavid S. Miller 		     NULL,
428903fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
42907f575103SMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4291d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_init,
4292d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_cleanup),
4293b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
4294b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
4295b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
429603fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
429703fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
429803fe2debSDavid S. Miller 		     NULL),
4299ee4d269eSMaher Sanalla 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
4300ee4d269eSMaher Sanalla 		     mlx5_ib_stage_delay_drop_init,
4301ee4d269eSMaher Sanalla 		     mlx5_ib_stage_delay_drop_cleanup),
4302b572ebe6SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4303b572ebe6SLeon Romanovsky 		     mlx5_ib_restrack_init,
4304b572ebe6SLeon Romanovsky 		     NULL),
4305b5ca15adSMark Bloch };
4306b5ca15adSMark Bloch 
430793f82444SLeon Romanovsky static int mlx5r_mp_probe(struct auxiliary_device *adev,
430893f82444SLeon Romanovsky 			  const struct auxiliary_device_id *id)
430932f69e4bSDaniel Jurgens {
431093f82444SLeon Romanovsky 	struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
431193f82444SLeon Romanovsky 	struct mlx5_core_dev *mdev = idev->mdev;
431232f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
431332f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
431432f69e4bSDaniel Jurgens 	bool bound = false;
431532f69e4bSDaniel Jurgens 	int err;
431632f69e4bSDaniel Jurgens 
431732f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
431832f69e4bSDaniel Jurgens 	if (!mpi)
431993f82444SLeon Romanovsky 		return -ENOMEM;
432032f69e4bSDaniel Jurgens 
432132f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
432232f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
432332f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
432432f69e4bSDaniel Jurgens 	if (err) {
432532f69e4bSDaniel Jurgens 		kfree(mpi);
432693f82444SLeon Romanovsky 		return err;
432732f69e4bSDaniel Jurgens 	}
432832f69e4bSDaniel Jurgens 
432932f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
433032f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
433132f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
433232f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
433332f69e4bSDaniel Jurgens 
433432f69e4bSDaniel Jurgens 		if (bound) {
433532f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
433697f30d32SMaor Gottlieb 			mpi->ibdev->ib_active = true;
433732f69e4bSDaniel Jurgens 			break;
433832f69e4bSDaniel Jurgens 		}
433932f69e4bSDaniel Jurgens 	}
434032f69e4bSDaniel Jurgens 
434132f69e4bSDaniel Jurgens 	if (!bound) {
434232f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
4343c42260f1SVu Pham 		dev_dbg(mdev->device,
4344c42260f1SVu Pham 			"no suitable IB device found to bind to, added to unaffiliated list.\n");
434532f69e4bSDaniel Jurgens 	}
434632f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
434732f69e4bSDaniel Jurgens 
434827963d3dSDavid E. Box 	auxiliary_set_drvdata(adev, mpi);
434993f82444SLeon Romanovsky 	return 0;
435032f69e4bSDaniel Jurgens }
435132f69e4bSDaniel Jurgens 
435293f82444SLeon Romanovsky static void mlx5r_mp_remove(struct auxiliary_device *adev)
435316c1975fSMark Bloch {
435493f82444SLeon Romanovsky 	struct mlx5_ib_multiport_info *mpi;
435593f82444SLeon Romanovsky 
435627963d3dSDavid E. Box 	mpi = auxiliary_get_drvdata(adev);
435793f82444SLeon Romanovsky 	mutex_lock(&mlx5_ib_multiport_mutex);
435893f82444SLeon Romanovsky 	if (mpi->ibdev)
435993f82444SLeon Romanovsky 		mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
4360da78fe5fSMaor Gottlieb 	else
436193f82444SLeon Romanovsky 		list_del(&mpi->list);
436293f82444SLeon Romanovsky 	mutex_unlock(&mlx5_ib_multiport_mutex);
436393f82444SLeon Romanovsky 	kfree(mpi);
436493f82444SLeon Romanovsky }
436593f82444SLeon Romanovsky 
436693f82444SLeon Romanovsky static int mlx5r_probe(struct auxiliary_device *adev,
436793f82444SLeon Romanovsky 		       const struct auxiliary_device_id *id)
436893f82444SLeon Romanovsky {
436993f82444SLeon Romanovsky 	struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
437093f82444SLeon Romanovsky 	struct mlx5_core_dev *mdev = idev->mdev;
437194de879cSMichael Guralnik 	const struct mlx5_ib_profile *profile;
437293f82444SLeon Romanovsky 	int port_type_cap, num_ports, ret;
437332f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
4374b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
4375f0666f1fSBodong Wang 
437632f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
437732f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
437832f69e4bSDaniel Jurgens 
4379da796ccbSMark Bloch 	num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
4380da796ccbSMark Bloch 			MLX5_CAP_GEN(mdev, num_vhca_ports));
4381459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
4382b5ca15adSMark Bloch 	if (!dev)
438393f82444SLeon Romanovsky 		return -ENOMEM;
4384da796ccbSMark Bloch 	dev->port = kcalloc(num_ports, sizeof(*dev->port),
4385da796ccbSMark Bloch 			     GFP_KERNEL);
4386da796ccbSMark Bloch 	if (!dev->port) {
4387a5c9c299SParav Pandit 		ib_dealloc_device(&dev->ib_dev);
438893f82444SLeon Romanovsky 		return -ENOMEM;
4389da796ccbSMark Bloch 	}
4390b5ca15adSMark Bloch 
4391b5ca15adSMark Bloch 	dev->mdev = mdev;
4392da796ccbSMark Bloch 	dev->num_ports = num_ports;
4393b5ca15adSMark Bloch 
43949ca05b0fSMaher Sanalla 	if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_get_roce_state(mdev))
439594de879cSMichael Guralnik 		profile = &raw_eth_profile;
439694de879cSMichael Guralnik 	else
439794de879cSMichael Guralnik 		profile = &pf_profile;
439894de879cSMichael Guralnik 
439993f82444SLeon Romanovsky 	ret = __mlx5_ib_add(dev, profile);
440093f82444SLeon Romanovsky 	if (ret) {
440193f82444SLeon Romanovsky 		kfree(dev->port);
440293f82444SLeon Romanovsky 		ib_dealloc_device(&dev->ib_dev);
440393f82444SLeon Romanovsky 		return ret;
440416c1975fSMark Bloch 	}
440516c1975fSMark Bloch 
440627963d3dSDavid E. Box 	auxiliary_set_drvdata(adev, dev);
440793f82444SLeon Romanovsky 	return 0;
440893f82444SLeon Romanovsky }
440993f82444SLeon Romanovsky 
441093f82444SLeon Romanovsky static void mlx5r_remove(struct auxiliary_device *adev)
4411e126ba97SEli Cohen {
441232f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
44136aec21f6SHaggai Eran 
441427963d3dSDavid E. Box 	dev = auxiliary_get_drvdata(adev);
441516c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
4416e126ba97SEli Cohen }
4417e126ba97SEli Cohen 
441893f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_mp_id_table[] = {
441993f82444SLeon Romanovsky 	{ .name = MLX5_ADEV_NAME ".multiport", },
442093f82444SLeon Romanovsky 	{},
442193f82444SLeon Romanovsky };
442293f82444SLeon Romanovsky 
442393f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_id_table[] = {
442493f82444SLeon Romanovsky 	{ .name = MLX5_ADEV_NAME ".rdma", },
442593f82444SLeon Romanovsky 	{},
442693f82444SLeon Romanovsky };
442793f82444SLeon Romanovsky 
442893f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_mp_id_table);
442993f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_id_table);
443093f82444SLeon Romanovsky 
443193f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_mp_driver = {
443293f82444SLeon Romanovsky 	.name = "multiport",
443393f82444SLeon Romanovsky 	.probe = mlx5r_mp_probe,
443493f82444SLeon Romanovsky 	.remove = mlx5r_mp_remove,
443593f82444SLeon Romanovsky 	.id_table = mlx5r_mp_id_table,
443693f82444SLeon Romanovsky };
443793f82444SLeon Romanovsky 
443893f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_driver = {
443993f82444SLeon Romanovsky 	.name = "rdma",
444093f82444SLeon Romanovsky 	.probe = mlx5r_probe,
444193f82444SLeon Romanovsky 	.remove = mlx5r_remove,
444293f82444SLeon Romanovsky 	.id_table = mlx5r_id_table,
4443e126ba97SEli Cohen };
4444e126ba97SEli Cohen 
4445e126ba97SEli Cohen static int __init mlx5_ib_init(void)
4446e126ba97SEli Cohen {
444793f82444SLeon Romanovsky 	int ret;
44486aec21f6SHaggai Eran 
44498010d74bSJason Gunthorpe 	xlt_emergency_page = (void *)__get_free_page(GFP_KERNEL);
4450c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
4451d69a24e0SDaniel Jurgens 		return -ENOMEM;
4452d69a24e0SDaniel Jurgens 
4453c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
4454c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
44558010d74bSJason Gunthorpe 		free_page((unsigned long)xlt_emergency_page);
4456c44ef998SIlya Lesokhin 		return -ENOMEM;
4457c44ef998SIlya Lesokhin 	}
4458c44ef998SIlya Lesokhin 
4459312b8f79SMark Zhang 	ret = mlx5_ib_qp_event_init();
4460312b8f79SMark Zhang 	if (ret)
4461312b8f79SMark Zhang 		goto qp_event_err;
4462312b8f79SMark Zhang 
446381713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
446493f82444SLeon Romanovsky 	ret = mlx5r_rep_init();
446593f82444SLeon Romanovsky 	if (ret)
446693f82444SLeon Romanovsky 		goto rep_err;
446793f82444SLeon Romanovsky 	ret = auxiliary_driver_register(&mlx5r_mp_driver);
446893f82444SLeon Romanovsky 	if (ret)
446993f82444SLeon Romanovsky 		goto mp_err;
447093f82444SLeon Romanovsky 	ret = auxiliary_driver_register(&mlx5r_driver);
447193f82444SLeon Romanovsky 	if (ret)
447293f82444SLeon Romanovsky 		goto drv_err;
447393f82444SLeon Romanovsky 	return 0;
44749603b61dSJack Morgenstein 
447593f82444SLeon Romanovsky drv_err:
447693f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_mp_driver);
447793f82444SLeon Romanovsky mp_err:
447893f82444SLeon Romanovsky 	mlx5r_rep_cleanup();
447993f82444SLeon Romanovsky rep_err:
4480312b8f79SMark Zhang 	mlx5_ib_qp_event_cleanup();
4481312b8f79SMark Zhang qp_event_err:
448293f82444SLeon Romanovsky 	destroy_workqueue(mlx5_ib_event_wq);
448393f82444SLeon Romanovsky 	free_page((unsigned long)xlt_emergency_page);
448493f82444SLeon Romanovsky 	return ret;
4485e126ba97SEli Cohen }
4486e126ba97SEli Cohen 
4487e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
4488e126ba97SEli Cohen {
448993f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_driver);
449093f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_mp_driver);
449193f82444SLeon Romanovsky 	mlx5r_rep_cleanup();
449293f82444SLeon Romanovsky 
4493312b8f79SMark Zhang 	mlx5_ib_qp_event_cleanup();
4494d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
44958010d74bSJason Gunthorpe 	free_page((unsigned long)xlt_emergency_page);
4496e126ba97SEli Cohen }
4497e126ba97SEli Cohen 
4498e126ba97SEli Cohen module_init(mlx5_ib_init);
4499e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
4500