xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 9144f784f852f9a125cabe9927b986d909bfa439)
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>
27fdd350feSPatrisious Haddad #include <linux/mlx5/driver.h>
287c2344c3SMaor Gottlieb #include <linux/list.h>
29e126ba97SEli Cohen #include <rdma/ib_smi.h>
30ca7ef7adSDaisuke Matsuda #include <rdma/ib_umem_odp.h>
31cfc1a89eSMaor Gottlieb #include <rdma/lag.h>
32038d2ef8SMaor Gottlieb #include <linux/in.h>
33038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
34e126ba97SEli Cohen #include "mlx5_ib.h"
35fc385b7aSMark Bloch #include "ib_rep.h"
36e1f24a79SParav Pandit #include "cmd.h"
37d8b7515eSLeon Romanovsky #include "devx.h"
38831df883SMaor Gottlieb #include "dm.h"
39f7c4ffdaSLeon Romanovsky #include "fs.h"
40f3da6577SLeon Romanovsky #include "srq.h"
41333fbaa0SLeon Romanovsky #include "qp.h"
42029e88fdSLeon Romanovsky #include "wr.h"
43b572ebe6SLeon Romanovsky #include "restrack.h"
4464825827SLeon Romanovsky #include "counters.h"
4504876c12SAharon Landau #include "umr.h"
468c84660bSMatan Barak #include <rdma/uverbs_std_types.h>
472904bb37SYishai Hadas #include <rdma/uverbs_ioctl.h>
48c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h>
49c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h>
50758ce14aSPatrisious Haddad #include "macsec.h"
518c84660bSMatan Barak 
528c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
538c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
54e126ba97SEli Cohen 
55e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
56daeee976SShay Drory MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) IB driver");
57e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
58e126ba97SEli Cohen 
59d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
60d69a24e0SDaniel Jurgens 	struct work_struct	work;
61df097a27SSaeed Mahameed 	union {
62df097a27SSaeed Mahameed 		struct mlx5_ib_dev	      *dev;
63df097a27SSaeed Mahameed 		struct mlx5_ib_multiport_info *mpi;
64df097a27SSaeed Mahameed 	};
65df097a27SSaeed Mahameed 	bool			is_slave;
66134e9349SSaeed Mahameed 	unsigned int		event;
67df097a27SSaeed Mahameed 	void			*param;
68d69a24e0SDaniel Jurgens };
69d69a24e0SDaniel Jurgens 
70da7525d2SEran Ben Elisha enum {
71da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
72da7525d2SEran Ben Elisha };
731b5daf11SMajd Dibbiny 
74d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
7532f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
7632f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
7732f69e4bSDaniel Jurgens /*
7832f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
7932f69e4bSDaniel Jurgens  */
8032f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
8132f69e4bSDaniel Jurgens 
mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info * mpi)8232f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
8332f69e4bSDaniel Jurgens {
8432f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
8532f69e4bSDaniel Jurgens 
8632f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
8732f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
8832f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
8932f69e4bSDaniel Jurgens 	return dev;
9032f69e4bSDaniel Jurgens }
9132f69e4bSDaniel Jurgens 
921b5daf11SMajd Dibbiny static enum rdma_link_layer
mlx5_port_type_cap_to_rdma_ll(int port_type_cap)93ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
941b5daf11SMajd Dibbiny {
95ebd61f68SAchiad Shochat 	switch (port_type_cap) {
961b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
971b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
981b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
991b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
1001b5daf11SMajd Dibbiny 	default:
1011b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1021b5daf11SMajd Dibbiny 	}
1031b5daf11SMajd Dibbiny }
1041b5daf11SMajd Dibbiny 
105ebd61f68SAchiad Shochat static enum rdma_link_layer
mlx5_ib_port_link_layer(struct ib_device * device,u32 port_num)1061fb7f897SMark Bloch mlx5_ib_port_link_layer(struct ib_device *device, u32 port_num)
107ebd61f68SAchiad Shochat {
108ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
109ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
110ebd61f68SAchiad Shochat 
111ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
112ebd61f68SAchiad Shochat }
113ebd61f68SAchiad Shochat 
get_port_state(struct ib_device * ibdev,u32 port_num,enum ib_port_state * state)114fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
1151fb7f897SMark Bloch 			  u32 port_num,
116fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
117fd65f1b8SMoni Shoua {
118fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
119fd65f1b8SMoni Shoua 	int ret;
120fd65f1b8SMoni Shoua 
121fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1223023a1e9SKamal Heib 	ret = ibdev->ops.query_port(ibdev, port_num, &attr);
123fd65f1b8SMoni Shoua 	if (!ret)
124fd65f1b8SMoni Shoua 		*state = attr.state;
125fd65f1b8SMoni Shoua 	return ret;
126fd65f1b8SMoni Shoua }
127fd65f1b8SMoni Shoua 
mlx5_get_rep_roce(struct mlx5_ib_dev * dev,struct net_device * ndev,struct net_device * upper,u32 * port_num)12835b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
12935b0aa67SMark Bloch 					   struct net_device *ndev,
130c446d9daSMark Bloch 					   struct net_device *upper,
1311fb7f897SMark Bloch 					   u32 *port_num)
13235b0aa67SMark Bloch {
13335b0aa67SMark Bloch 	struct net_device *rep_ndev;
13435b0aa67SMark Bloch 	struct mlx5_ib_port *port;
13535b0aa67SMark Bloch 	int i;
13635b0aa67SMark Bloch 
13735b0aa67SMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
13835b0aa67SMark Bloch 		port  = &dev->port[i];
13935b0aa67SMark Bloch 		if (!port->rep)
14035b0aa67SMark Bloch 			continue;
14135b0aa67SMark Bloch 
142c446d9daSMark Bloch 		if (upper == ndev && port->rep->vport == MLX5_VPORT_UPLINK) {
143c446d9daSMark Bloch 			*port_num = i + 1;
144c446d9daSMark Bloch 			return &port->roce;
145c446d9daSMark Bloch 		}
146c446d9daSMark Bloch 
147c446d9daSMark Bloch 		if (upper && port->rep->vport == MLX5_VPORT_UPLINK)
148c446d9daSMark Bloch 			continue;
149c446d9daSMark Bloch 
15035b0aa67SMark Bloch 		read_lock(&port->roce.netdev_lock);
151658cfcebSMark Bloch 		rep_ndev = mlx5_ib_get_rep_netdev(port->rep->esw,
15235b0aa67SMark Bloch 						  port->rep->vport);
15335b0aa67SMark Bloch 		if (rep_ndev == ndev) {
15435b0aa67SMark Bloch 			read_unlock(&port->roce.netdev_lock);
15535b0aa67SMark Bloch 			*port_num = i + 1;
15635b0aa67SMark Bloch 			return &port->roce;
15735b0aa67SMark Bloch 		}
15835b0aa67SMark Bloch 		read_unlock(&port->roce.netdev_lock);
15935b0aa67SMark Bloch 	}
16035b0aa67SMark Bloch 
16135b0aa67SMark Bloch 	return NULL;
16235b0aa67SMark Bloch }
16335b0aa67SMark Bloch 
mlx5_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)164fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
165fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
166fc24fc5eSAchiad Shochat {
1677fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
168fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1691fb7f897SMark Bloch 	u32 port_num = roce->native_port_num;
1707fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1717fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1727fd8aefbSDaniel Jurgens 
1737fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
17432f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
17532f69e4bSDaniel Jurgens 	if (!mdev)
17632f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
177fc24fc5eSAchiad Shochat 
1785ec8c83eSAviv Heller 	switch (event) {
1795ec8c83eSAviv Heller 	case NETDEV_REGISTER:
18035b0aa67SMark Bloch 		/* Should already be registered during the load */
18135b0aa67SMark Bloch 		if (ibdev->is_rep)
18235b0aa67SMark Bloch 			break;
1837fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
184dce45af5SLinus Torvalds 		if (ndev->dev.parent == mdev->device)
185842a9c83SOr Gerlitz 			roce->netdev = ndev;
1867fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
1875ec8c83eSAviv Heller 		break;
1885ec8c83eSAviv Heller 
189842a9c83SOr Gerlitz 	case NETDEV_UNREGISTER:
19035b0aa67SMark Bloch 		/* In case of reps, ib device goes away before the netdevs */
191842a9c83SOr Gerlitz 		write_lock(&roce->netdev_lock);
192842a9c83SOr Gerlitz 		if (roce->netdev == ndev)
193842a9c83SOr Gerlitz 			roce->netdev = NULL;
194842a9c83SOr Gerlitz 		write_unlock(&roce->netdev_lock);
195842a9c83SOr Gerlitz 		break;
196842a9c83SOr Gerlitz 
197fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
1985ec8c83eSAviv Heller 	case NETDEV_UP:
19988621dfeSAviv Heller 	case NETDEV_DOWN: {
2007fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
20188621dfeSAviv Heller 		struct net_device *upper = NULL;
20288621dfeSAviv Heller 
20388621dfeSAviv Heller 		if (lag_ndev) {
20488621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
20588621dfeSAviv Heller 			dev_put(lag_ndev);
20688621dfeSAviv Heller 		}
20788621dfeSAviv Heller 
20835b0aa67SMark Bloch 		if (ibdev->is_rep)
209c446d9daSMark Bloch 			roce = mlx5_get_rep_roce(ibdev, ndev, upper, &port_num);
21035b0aa67SMark Bloch 		if (!roce)
21135b0aa67SMark Bloch 			return NOTIFY_DONE;
212c446d9daSMark Bloch 		if ((upper == ndev ||
213c446d9daSMark Bloch 		     ((!upper || ibdev->is_rep) && ndev == roce->netdev)) &&
214c446d9daSMark Bloch 		    ibdev->ib_active) {
215626bc02dSBart Van Assche 			struct ib_event ibev = { };
216fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2175ec8c83eSAviv Heller 
2187fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2197fd8aefbSDaniel Jurgens 					   &port_state))
2207fd8aefbSDaniel Jurgens 				goto done;
221fd65f1b8SMoni Shoua 
2227fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2237fd8aefbSDaniel Jurgens 				goto done;
224fd65f1b8SMoni Shoua 
2257fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2265ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
227fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
228fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
229fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
230fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
231fd65f1b8SMoni Shoua 			else
2327fd8aefbSDaniel Jurgens 				goto done;
233fd65f1b8SMoni Shoua 
2347fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2355ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2365ec8c83eSAviv Heller 		}
2375ec8c83eSAviv Heller 		break;
23888621dfeSAviv Heller 	}
2395ec8c83eSAviv Heller 
2405ec8c83eSAviv Heller 	default:
2415ec8c83eSAviv Heller 		break;
2425ec8c83eSAviv Heller 	}
2437fd8aefbSDaniel Jurgens done:
24432f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
245fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
246fc24fc5eSAchiad Shochat }
247fc24fc5eSAchiad Shochat 
mlx5_ib_get_netdev(struct ib_device * device,u32 port_num)248fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
2491fb7f897SMark Bloch 					     u32 port_num)
250fc24fc5eSAchiad Shochat {
251fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
252fc24fc5eSAchiad Shochat 	struct net_device *ndev;
25332f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
254fc24fc5eSAchiad Shochat 
25532f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
25632f69e4bSDaniel Jurgens 	if (!mdev)
25732f69e4bSDaniel Jurgens 		return NULL;
25832f69e4bSDaniel Jurgens 
25932f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
26088621dfeSAviv Heller 	if (ndev)
26132f69e4bSDaniel Jurgens 		goto out;
26288621dfeSAviv Heller 
263fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
264fc24fc5eSAchiad Shochat 	 */
26595579e78SMark Bloch 	read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
26695579e78SMark Bloch 	ndev = ibdev->port[port_num - 1].roce.netdev;
267fc24fc5eSAchiad Shochat 	if (ndev)
268fc24fc5eSAchiad Shochat 		dev_hold(ndev);
26995579e78SMark Bloch 	read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
270fc24fc5eSAchiad Shochat 
27132f69e4bSDaniel Jurgens out:
27232f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
273fc24fc5eSAchiad Shochat 	return ndev;
274fc24fc5eSAchiad Shochat }
275fc24fc5eSAchiad Shochat 
mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev * ibdev,u32 ib_port_num,u32 * native_port_num)27632f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
2771fb7f897SMark Bloch 						   u32 ib_port_num,
2781fb7f897SMark Bloch 						   u32 *native_port_num)
27932f69e4bSDaniel Jurgens {
28032f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
28132f69e4bSDaniel Jurgens 							  ib_port_num);
28232f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
28332f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
28432f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
28532f69e4bSDaniel Jurgens 
286210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
287210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
288210b1f78SMark Bloch 		if (native_port_num)
289210b1f78SMark Bloch 			*native_port_num = ib_port_num;
290210b1f78SMark Bloch 		return ibdev->mdev;
291210b1f78SMark Bloch 	}
292210b1f78SMark Bloch 
29332f69e4bSDaniel Jurgens 	if (native_port_num)
29432f69e4bSDaniel Jurgens 		*native_port_num = 1;
29532f69e4bSDaniel Jurgens 
29632f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
29732f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
29832f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
29932f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
30032f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
30132f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
30232f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
30332f69e4bSDaniel Jurgens 		 */
30432f69e4bSDaniel Jurgens 		if (!mpi->is_master)
30532f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
30632f69e4bSDaniel Jurgens 	}
30732f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
30832f69e4bSDaniel Jurgens 
30932f69e4bSDaniel Jurgens 	return mdev;
31032f69e4bSDaniel Jurgens }
31132f69e4bSDaniel Jurgens 
mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev * ibdev,u32 port_num)3121fb7f897SMark Bloch void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u32 port_num)
31332f69e4bSDaniel Jurgens {
31432f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
31532f69e4bSDaniel Jurgens 							  port_num);
31632f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
31732f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
31832f69e4bSDaniel Jurgens 
31932f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
32032f69e4bSDaniel Jurgens 		return;
32132f69e4bSDaniel Jurgens 
32232f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
32332f69e4bSDaniel Jurgens 
32432f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
32532f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
32632f69e4bSDaniel Jurgens 	if (mpi->is_master)
32732f69e4bSDaniel Jurgens 		goto out;
32832f69e4bSDaniel Jurgens 
32932f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
33032f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
33132f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
33232f69e4bSDaniel Jurgens out:
33332f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
33432f69e4bSDaniel Jurgens }
33532f69e4bSDaniel Jurgens 
translate_eth_legacy_proto_oper(u32 eth_proto_oper,u16 * active_speed,u8 * active_width)336639bf441SAharon Landau static int translate_eth_legacy_proto_oper(u32 eth_proto_oper,
337639bf441SAharon Landau 					   u16 *active_speed, u8 *active_width)
338f1b65df5SNoa Osherovich {
339f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
340f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
341f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
342f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
343f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
344f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
345f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
346f1b65df5SNoa Osherovich 		break;
347f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
348f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
349f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
350f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
351f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
352f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
353f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
354f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
355f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
356f1b65df5SNoa Osherovich 		break;
357f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
358f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
359f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
360f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
361f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
362f1b65df5SNoa Osherovich 		break;
363f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
364f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
365f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
366f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
367f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
368f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
369f1b65df5SNoa Osherovich 		break;
370f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
371f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
372f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
373f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
374f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
375f1b65df5SNoa Osherovich 		break;
376f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
377f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
378f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
379f1b65df5SNoa Osherovich 		break;
380f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
381f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
382f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
383f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
384f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
385f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
386f1b65df5SNoa Osherovich 		break;
387f1b65df5SNoa Osherovich 	default:
388f1b65df5SNoa Osherovich 		return -EINVAL;
389f1b65df5SNoa Osherovich 	}
390f1b65df5SNoa Osherovich 
391f1b65df5SNoa Osherovich 	return 0;
392f1b65df5SNoa Osherovich }
393f1b65df5SNoa Osherovich 
translate_eth_ext_proto_oper(u32 eth_proto_oper,u16 * active_speed,u8 * active_width)394639bf441SAharon Landau static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u16 *active_speed,
39508e8676fSAya Levin 					u8 *active_width)
39608e8676fSAya Levin {
39708e8676fSAya Levin 	switch (eth_proto_oper) {
39808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
39908e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
40008e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
40108e8676fSAya Levin 		*active_speed = IB_SPEED_SDR;
40208e8676fSAya Levin 		break;
40308e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
40408e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
40508e8676fSAya Levin 		*active_speed = IB_SPEED_DDR;
40608e8676fSAya Levin 		break;
40708e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
40808e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
40908e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
41008e8676fSAya Levin 		break;
41108e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
41208e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
41308e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
41408e8676fSAya Levin 		break;
41508e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
41608e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
41708e8676fSAya Levin 		*active_speed = IB_SPEED_EDR;
41808e8676fSAya Levin 		break;
41908e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
420cd272875SAya Levin 		*active_width = IB_WIDTH_2X;
421cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
422cd272875SAya Levin 		break;
42308e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
42408e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
42508e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
42608e8676fSAya Levin 		break;
427cd272875SAya Levin 	case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
428cd272875SAya Levin 		*active_width = IB_WIDTH_4X;
429cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
430cd272875SAya Levin 		break;
43108e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
43208e8676fSAya Levin 		*active_width = IB_WIDTH_2X;
43308e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
43408e8676fSAya Levin 		break;
435f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_100GAUI_1_100GBASE_CR_KR):
436f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_1X;
437f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
438f946e45fSMeir Lichtinger 		break;
43908e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
44008e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
44108e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
44208e8676fSAya Levin 		break;
443f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_200GAUI_2_200GBASE_CR2_KR2):
444f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_2X;
445f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
446f946e45fSMeir Lichtinger 		break;
4474ce59074SPatrisious Haddad 	case MLX5E_PROT_MASK(MLX5E_400GAUI_8_400GBASE_CR8):
44888c9483fSMaher Sanalla 		*active_width = IB_WIDTH_8X;
44988c9483fSMaher Sanalla 		*active_speed = IB_SPEED_HDR;
45088c9483fSMaher Sanalla 		break;
451f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_400GAUI_4_400GBASE_CR4_KR4):
452f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_4X;
453f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
454f946e45fSMeir Lichtinger 		break;
45508e8676fSAya Levin 	default:
45608e8676fSAya Levin 		return -EINVAL;
45708e8676fSAya Levin 	}
45808e8676fSAya Levin 
45908e8676fSAya Levin 	return 0;
46008e8676fSAya Levin }
46108e8676fSAya Levin 
translate_eth_proto_oper(u32 eth_proto_oper,u16 * active_speed,u8 * active_width,bool ext)462639bf441SAharon Landau static int translate_eth_proto_oper(u32 eth_proto_oper, u16 *active_speed,
46308e8676fSAya Levin 				    u8 *active_width, bool ext)
46408e8676fSAya Levin {
46508e8676fSAya Levin 	return ext ?
46608e8676fSAya Levin 		translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
46708e8676fSAya Levin 					     active_width) :
46808e8676fSAya Levin 		translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
46908e8676fSAya Levin 						active_width);
47008e8676fSAya Levin }
47108e8676fSAya Levin 
mlx5_query_port_roce(struct ib_device * device,u32 port_num,struct ib_port_attr * props)4721fb7f897SMark Bloch static int mlx5_query_port_roce(struct ib_device *device, u32 port_num,
4733f89a643SAchiad Shochat 				struct ib_port_attr *props)
4743f89a643SAchiad Shochat {
4753f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
476bc4e12ffSAya Levin 	u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
477da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
47888621dfeSAviv Heller 	struct net_device *ndev, *upper;
4793f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
480b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
481f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
4821fb7f897SMark Bloch 	u32 mdev_port_num;
48308e8676fSAya Levin 	bool ext;
484095b0927SIlan Tayari 	int err;
4853f89a643SAchiad Shochat 
486b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
487b3cbd6f0SDaniel Jurgens 	if (!mdev) {
488b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
489b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
490b3cbd6f0SDaniel Jurgens 		 */
491b3cbd6f0SDaniel Jurgens 		put_mdev = false;
492b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
493b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
494b3cbd6f0SDaniel Jurgens 		port_num = 1;
495b3cbd6f0SDaniel Jurgens 	}
496b3cbd6f0SDaniel Jurgens 
497f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
498f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
49926628e2dSMark Bloch 	 * Use native port in case of reps
50050f22fd8SNoa Osherovich 	 */
50126628e2dSMark Bloch 	if (dev->is_rep)
50226628e2dSMark Bloch 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
50326628e2dSMark Bloch 					   1);
50426628e2dSMark Bloch 	else
505bc4e12ffSAya Levin 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
506b3cbd6f0SDaniel Jurgens 					   mdev_port_num);
507095b0927SIlan Tayari 	if (err)
508b3cbd6f0SDaniel Jurgens 		goto out;
509530c8632SAya Levin 	ext = !!MLX5_GET_ETH_PROTO(ptys_reg, out, true, eth_proto_capability);
51008e8676fSAya Levin 	eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
511f1b65df5SNoa Osherovich 
5127672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
5137672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
5147672ed33SHonggang Li 
515f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
51608e8676fSAya Levin 				 &props->active_width, ext);
5173f89a643SAchiad Shochat 
5187852546fSMaor Gottlieb 	if (!dev->is_rep && dev->mdev->roce.roce_en) {
5197a58779eSParav Pandit 		u16 qkey_viol_cntr;
5207a58779eSParav Pandit 
5213f89a643SAchiad Shochat 		props->port_cap_flags |= IB_PORT_CM_SUP;
5222f944c0fSJason Gunthorpe 		props->ip_gids = true;
5233f89a643SAchiad Shochat 		props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
5243f89a643SAchiad Shochat 						   roce_address_table_size);
5257a58779eSParav Pandit 		mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
5267a58779eSParav Pandit 		props->qkey_viol_cntr = qkey_viol_cntr;
5277a58779eSParav Pandit 	}
5283f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
5293f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
5303f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
5313f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
53272a7720fSKamal Heib 	props->phys_state       = IB_PORT_PHYS_STATE_DISABLED;
5333f89a643SAchiad Shochat 
534b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
535b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
536b3cbd6f0SDaniel Jurgens 		goto out;
537b3cbd6f0SDaniel Jurgens 
5383f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
5393f89a643SAchiad Shochat 	if (!ndev)
540b3cbd6f0SDaniel Jurgens 		goto out;
5413f89a643SAchiad Shochat 
5426eff336bSMark Bloch 	if (mlx5_lag_is_roce(mdev) || mlx5_lag_is_sriov(mdev)) {
54388621dfeSAviv Heller 		rcu_read_lock();
54488621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
54588621dfeSAviv Heller 		if (upper) {
54688621dfeSAviv Heller 			dev_put(ndev);
54788621dfeSAviv Heller 			ndev = upper;
54888621dfeSAviv Heller 			dev_hold(ndev);
54988621dfeSAviv Heller 		}
55088621dfeSAviv Heller 		rcu_read_unlock();
55188621dfeSAviv Heller 	}
55288621dfeSAviv Heller 
5533f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
5543f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
55572a7720fSKamal Heib 		props->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
5563f89a643SAchiad Shochat 	}
5573f89a643SAchiad Shochat 
5583f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
5593f89a643SAchiad Shochat 
5603f89a643SAchiad Shochat 	dev_put(ndev);
5613f89a643SAchiad Shochat 
5623f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
563b3cbd6f0SDaniel Jurgens out:
564b3cbd6f0SDaniel Jurgens 	if (put_mdev)
565b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
566b3cbd6f0SDaniel Jurgens 	return err;
5673f89a643SAchiad Shochat }
5683f89a643SAchiad Shochat 
set_roce_addr(struct mlx5_ib_dev * dev,u32 port_num,unsigned int index,const union ib_gid * gid,const struct ib_gid_attr * attr)569758ce14aSPatrisious Haddad int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num,
570095b0927SIlan Tayari 		  unsigned int index, const union ib_gid *gid,
571095b0927SIlan Tayari 		  const struct ib_gid_attr *attr)
5723cca2606SAchiad Shochat {
573dedbc2d3SParav Pandit 	enum ib_gid_type gid_type;
574a70c0739SParav Pandit 	u16 vlan_id = 0xffff;
575095b0927SIlan Tayari 	u8 roce_version = 0;
576095b0927SIlan Tayari 	u8 roce_l3_type = 0;
577095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
578a70c0739SParav Pandit 	int ret;
5793cca2606SAchiad Shochat 
580095b0927SIlan Tayari 	gid_type = attr->gid_type;
581dedbc2d3SParav Pandit 	if (gid) {
582a70c0739SParav Pandit 		ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
583a70c0739SParav Pandit 		if (ret)
584a70c0739SParav Pandit 			return ret;
5853cca2606SAchiad Shochat 	}
5863cca2606SAchiad Shochat 
587095b0927SIlan Tayari 	switch (gid_type) {
5881c15b4f2SAvihai Horon 	case IB_GID_TYPE_ROCE:
589095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
5903cca2606SAchiad Shochat 		break;
5913cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
592095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
593dedbc2d3SParav Pandit 		if (gid && ipv6_addr_v4mapped((void *)gid))
594095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
595095b0927SIlan Tayari 		else
596095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
5973cca2606SAchiad Shochat 		break;
5983cca2606SAchiad Shochat 
5993cca2606SAchiad Shochat 	default:
600095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
6013cca2606SAchiad Shochat 	}
6023cca2606SAchiad Shochat 
603095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
604cf34e1feSParav Pandit 				      roce_l3_type, gid->raw, mac,
605a70c0739SParav Pandit 				      vlan_id < VLAN_CFI_MASK, vlan_id,
606cf34e1feSParav Pandit 				      port_num);
6073cca2606SAchiad Shochat }
6083cca2606SAchiad Shochat 
mlx5_ib_add_gid(const struct ib_gid_attr * attr,__always_unused void ** context)609f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
6103cca2606SAchiad Shochat 			   __always_unused void **context)
6113cca2606SAchiad Shochat {
612758ce14aSPatrisious Haddad 	int ret;
613758ce14aSPatrisious Haddad 
614758ce14aSPatrisious Haddad 	ret = mlx5r_add_gid_macsec_operations(attr);
615758ce14aSPatrisious Haddad 	if (ret)
616758ce14aSPatrisious Haddad 		return ret;
617758ce14aSPatrisious Haddad 
618414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
619f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
6203cca2606SAchiad Shochat }
6213cca2606SAchiad Shochat 
mlx5_ib_del_gid(const struct ib_gid_attr * attr,__always_unused void ** context)622414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
623414448d2SParav Pandit 			   __always_unused void **context)
6243cca2606SAchiad Shochat {
625758ce14aSPatrisious Haddad 	int ret;
626758ce14aSPatrisious Haddad 
627758ce14aSPatrisious Haddad 	ret = set_roce_addr(to_mdev(attr->device), attr->port_num,
628dedbc2d3SParav Pandit 			    attr->index, NULL, attr);
629758ce14aSPatrisious Haddad 	if (ret)
630758ce14aSPatrisious Haddad 		return ret;
631758ce14aSPatrisious Haddad 
632758ce14aSPatrisious Haddad 	mlx5r_del_gid_macsec_operations(attr);
633758ce14aSPatrisious Haddad 	return 0;
6343cca2606SAchiad Shochat }
6353cca2606SAchiad Shochat 
mlx5_get_roce_udp_sport_min(const struct mlx5_ib_dev * dev,const struct ib_gid_attr * attr)6365ac55dfcSMark Zhang __be16 mlx5_get_roce_udp_sport_min(const struct mlx5_ib_dev *dev,
63747ec3866SParav Pandit 				   const struct ib_gid_attr *attr)
6382811ba51SAchiad Shochat {
63947ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
6402811ba51SAchiad Shochat 		return 0;
6412811ba51SAchiad Shochat 
6422811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
6432811ba51SAchiad Shochat }
6442811ba51SAchiad Shochat 
mlx5_use_mad_ifc(struct mlx5_ib_dev * dev)6451b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
6461b5daf11SMajd Dibbiny {
6477fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
648d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
6497fae6655SNoa Osherovich 	return 0;
6501b5daf11SMajd Dibbiny }
6511b5daf11SMajd Dibbiny 
6521b5daf11SMajd Dibbiny enum {
6531b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
6541b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
6551b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
6561b5daf11SMajd Dibbiny };
6571b5daf11SMajd Dibbiny 
mlx5_get_vport_access_method(struct ib_device * ibdev)6581b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
6591b5daf11SMajd Dibbiny {
6601b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
6611b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
6621b5daf11SMajd Dibbiny 
663ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
6641b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
6651b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
6661b5daf11SMajd Dibbiny 
6671b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
6681b5daf11SMajd Dibbiny }
6691b5daf11SMajd Dibbiny 
get_atomic_caps(struct mlx5_ib_dev * dev,u8 atomic_size_qp,struct ib_device_attr * props)670da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
671776a3906SMoni Shoua 			    u8 atomic_size_qp,
672da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
673da7525d2SEran Ben Elisha {
674da7525d2SEran Ben Elisha 	u8 tmp;
675da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
676da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
677bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
678da7525d2SEran Ben Elisha 
679da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
680da7525d2SEran Ben Elisha 	 * of host endianness respond
681da7525d2SEran Ben Elisha 	 */
682da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
683da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
684da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
685da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
686da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
687da7525d2SEran Ben Elisha 	} else {
688da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
689da7525d2SEran Ben Elisha 	}
690da7525d2SEran Ben Elisha }
691da7525d2SEran Ben Elisha 
get_atomic_caps_qp(struct mlx5_ib_dev * dev,struct ib_device_attr * props)692776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
693776a3906SMoni Shoua 			       struct ib_device_attr *props)
694776a3906SMoni Shoua {
695776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
696776a3906SMoni Shoua 
697776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
698776a3906SMoni Shoua }
699776a3906SMoni Shoua 
mlx5_query_system_image_guid(struct ib_device * ibdev,__be64 * sys_image_guid)7001b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
7011b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
7021b5daf11SMajd Dibbiny {
7031b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7041b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7051b5daf11SMajd Dibbiny 	u64 tmp;
7061b5daf11SMajd Dibbiny 	int err;
7071b5daf11SMajd Dibbiny 
7081b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7091b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7101b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
7111b5daf11SMajd Dibbiny 							    sys_image_guid);
7121b5daf11SMajd Dibbiny 
7131b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7141b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
7153f89a643SAchiad Shochat 		break;
7163f89a643SAchiad Shochat 
7173f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7183f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
7193f89a643SAchiad Shochat 		break;
7201b5daf11SMajd Dibbiny 
7211b5daf11SMajd Dibbiny 	default:
7221b5daf11SMajd Dibbiny 		return -EINVAL;
7231b5daf11SMajd Dibbiny 	}
7243f89a643SAchiad Shochat 
7253f89a643SAchiad Shochat 	if (!err)
7263f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
7273f89a643SAchiad Shochat 
7283f89a643SAchiad Shochat 	return err;
7293f89a643SAchiad Shochat 
7301b5daf11SMajd Dibbiny }
7311b5daf11SMajd Dibbiny 
mlx5_query_max_pkeys(struct ib_device * ibdev,u16 * max_pkeys)7321b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
7331b5daf11SMajd Dibbiny 				u16 *max_pkeys)
7341b5daf11SMajd Dibbiny {
7351b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7361b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7371b5daf11SMajd Dibbiny 
7381b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7391b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7401b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
7411b5daf11SMajd Dibbiny 
7421b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7431b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7441b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
7451b5daf11SMajd Dibbiny 						pkey_table_size));
7461b5daf11SMajd Dibbiny 		return 0;
7471b5daf11SMajd Dibbiny 
7481b5daf11SMajd Dibbiny 	default:
7491b5daf11SMajd Dibbiny 		return -EINVAL;
7501b5daf11SMajd Dibbiny 	}
7511b5daf11SMajd Dibbiny }
7521b5daf11SMajd Dibbiny 
mlx5_query_vendor_id(struct ib_device * ibdev,u32 * vendor_id)7531b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
7541b5daf11SMajd Dibbiny 				u32 *vendor_id)
7551b5daf11SMajd Dibbiny {
7561b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7571b5daf11SMajd Dibbiny 
7581b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7591b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7601b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
7611b5daf11SMajd Dibbiny 
7621b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7631b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7641b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
7651b5daf11SMajd Dibbiny 
7661b5daf11SMajd Dibbiny 	default:
7671b5daf11SMajd Dibbiny 		return -EINVAL;
7681b5daf11SMajd Dibbiny 	}
7691b5daf11SMajd Dibbiny }
7701b5daf11SMajd Dibbiny 
mlx5_query_node_guid(struct mlx5_ib_dev * dev,__be64 * node_guid)7711b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
7721b5daf11SMajd Dibbiny 				__be64 *node_guid)
7731b5daf11SMajd Dibbiny {
7741b5daf11SMajd Dibbiny 	u64 tmp;
7751b5daf11SMajd Dibbiny 	int err;
7761b5daf11SMajd Dibbiny 
7771b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7781b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7791b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7801b5daf11SMajd Dibbiny 
7811b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7821b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7833f89a643SAchiad Shochat 		break;
7843f89a643SAchiad Shochat 
7853f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7863f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7873f89a643SAchiad Shochat 		break;
7881b5daf11SMajd Dibbiny 
7891b5daf11SMajd Dibbiny 	default:
7901b5daf11SMajd Dibbiny 		return -EINVAL;
7911b5daf11SMajd Dibbiny 	}
7923f89a643SAchiad Shochat 
7933f89a643SAchiad Shochat 	if (!err)
7943f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7953f89a643SAchiad Shochat 
7963f89a643SAchiad Shochat 	return err;
7971b5daf11SMajd Dibbiny }
7981b5daf11SMajd Dibbiny 
7991b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
800bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
8011b5daf11SMajd Dibbiny };
8021b5daf11SMajd Dibbiny 
mlx5_query_node_desc(struct mlx5_ib_dev * dev,char * node_desc)8031b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
8041b5daf11SMajd Dibbiny {
8051b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
8061b5daf11SMajd Dibbiny 
8071b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
8081b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
8091b5daf11SMajd Dibbiny 
8101b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
8111b5daf11SMajd Dibbiny 
8121b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
8131b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
8141b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
8151b5daf11SMajd Dibbiny }
8161b5daf11SMajd Dibbiny 
mlx5_ib_query_device(struct ib_device * ibdev,struct ib_device_attr * props,struct ib_udata * uhw)817e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
8182528e33eSMatan Barak 				struct ib_device_attr *props,
8192528e33eSMatan Barak 				struct ib_udata *uhw)
820e126ba97SEli Cohen {
82148357091SLeon Romanovsky 	size_t uhw_outlen = (uhw) ? uhw->outlen : 0;
822e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
823938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
824e126ba97SEli Cohen 	int err = -ENOMEM;
825288c01b7SEli Cohen 	int max_sq_desc;
826e126ba97SEli Cohen 	int max_rq_sg;
827e126ba97SEli Cohen 	int max_sq_sg;
828e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
82985c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
830402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
831402ca536SBodong Wang 	size_t resp_len;
832402ca536SBodong Wang 	u64 max_tso;
833e126ba97SEli Cohen 
834402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
83548357091SLeon Romanovsky 	if (uhw_outlen && uhw_outlen < resp_len)
836402ca536SBodong Wang 		return -EINVAL;
8376f26b2acSErez Alfasi 
838402ca536SBodong Wang 	resp.response_length = resp_len;
839402ca536SBodong Wang 
84048357091SLeon Romanovsky 	if (uhw && uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
8412528e33eSMatan Barak 		return -EINVAL;
8422528e33eSMatan Barak 
843e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
8441b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
8451b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
8461b5daf11SMajd Dibbiny 	if (err)
8471b5daf11SMajd Dibbiny 		return err;
8481b5daf11SMajd Dibbiny 
8492019d70eSParav Pandit 	props->max_pkeys = dev->pkey_table_len;
8501b5daf11SMajd Dibbiny 
8511b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
8521b5daf11SMajd Dibbiny 	if (err)
8531b5daf11SMajd Dibbiny 		return err;
854e126ba97SEli Cohen 
8559603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
8569603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
8579603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
858e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
859e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
860e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
8611a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
862938fe83cSSaeed Mahameed 
863938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
864e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
865938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
866e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
867938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
868e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
869938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
870e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
871d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
872d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
873d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
874d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
875b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
876e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_SG_GAPS_REG;
877d2370e0aSMatan Barak 	}
8780ec52f01SJason Gunthorpe 	/* IB_WR_REG_MR always requires changing the entity size with UMR */
8790ec52f01SJason Gunthorpe 	if (!MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled))
880e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
881938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
882e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_INTEGRITY_HANDOVER;
8832dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8842dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8852dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8862dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8872dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8882dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8892dea9094SSagi Grimberg 	}
890938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
891e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_BLOCK_MULTICAST_LOOPBACK;
892e126ba97SEli Cohen 
89385c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
894e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
895e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
89688115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
897e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
898e8161334SNoa Osherovich 		}
899e8161334SNoa Osherovich 
900e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
901e8161334SNoa Osherovich 			props->raw_packet_caps |=
902e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
90388115fe7SBodong Wang 
904a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) {
905402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
906402ca536SBodong Wang 			if (max_tso) {
907402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
908402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
909402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
910402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
911402ca536SBodong Wang 			}
912402ca536SBodong Wang 		}
91331f69a82SYishai Hadas 
914a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) {
91531f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
91631f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
91731f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
91831f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
91931f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
92031f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
92131f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
92231f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
92331f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
92431f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
9254e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
9264e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
92731f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
92831f69a82SYishai Hadas 		}
92931f69a82SYishai Hadas 	} else {
930a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen)
93131f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
932a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen)
93331f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
934402ca536SBodong Wang 	}
935402ca536SBodong Wang 
936f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
937f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
938e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_UD_TSO;
939f0313965SErez Shitrit 	}
940f0313965SErez Shitrit 
94103404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
94285c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
94385c7c014SDaniel Jurgens 	    raw_support)
94403404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
94503404e8aSMaor Gottlieb 
9461d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
9471d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
9481d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
9491d54f890SYishai Hadas 
950cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
95185c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
95285c7c014SDaniel Jurgens 	    raw_support) {
953e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
954cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
955e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
956e8161334SNoa Osherovich 	}
957cff5a0f3SMajd Dibbiny 
95824da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
95924da0016SAriel Levkovich 		props->max_dm_size =
96024da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
96124da0016SAriel Levkovich 	}
96224da0016SAriel Levkovich 
963da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
964da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
965da6d6ba3SMaor Gottlieb 
966b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
967b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
968b1383aa6SNoa Osherovich 
9691b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
9701b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
971e126ba97SEli Cohen 
972e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
973e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
974938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
975938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
976938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
977938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
978288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
979288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
980288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
981e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
98233023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
98333023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
984986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
985938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9869f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
987938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
988938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
989938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
990938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
991938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
992938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
993938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
994e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
995e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
996911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
997911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
99862e3c379SMax Gurtovoy 	props->max_pi_fast_reg_page_list_len =
99962e3c379SMax Gurtovoy 		props->max_fast_reg_page_list_len / 2;
100036609056SYamin Friedman 	props->max_sgl_rd =
100136609056SYamin Friedman 		MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance);
1002776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
100381bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
1004938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1005938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
1006e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1007e126ba97SEli Cohen 					   props->max_mcast_grp;
100886695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
10097c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
10107c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
1011e126ba97SEli Cohen 
1012e502b8b0SLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
101300815752SMoni Shoua 		if (dev->odp_caps.general_caps & IB_ODP_SUPPORT)
1014e945c653SJason Gunthorpe 			props->kernel_cap_flags |= IBK_ON_DEMAND_PAGING;
10158cdd312cSHaggai Eran 		props->odp_caps = dev->odp_caps;
1016a73a8955SMoni Shoua 		if (!uhw) {
1017a73a8955SMoni Shoua 			/* ODP for kernel QPs is not implemented for receive
1018a73a8955SMoni Shoua 			 * WQEs and SRQ WQEs
1019a73a8955SMoni Shoua 			 */
1020a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.rc_odp_caps &=
1021a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1022a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1023a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.uc_odp_caps &=
1024a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1025a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1026a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.ud_odp_caps &=
1027a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1028a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1029a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.xrc_odp_caps &=
1030a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1031a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1032a73a8955SMoni Shoua 		}
1033e502b8b0SLeon Romanovsky 	}
10348cdd312cSHaggai Eran 
1035e53a9d26SParav Pandit 	if (mlx5_core_is_vf(mdev))
1036e945c653SJason Gunthorpe 		props->kernel_cap_flags |= IBK_VIRTUAL_FUNCTION;
1037eff901d3SEli Cohen 
103831f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
103985c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
104031f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
104131f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
104231f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
104331f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
104431f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
104531f69a82SYishai Hadas 		props->max_wq_type_rq =
104631f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
104731f69a82SYishai Hadas 	}
104831f69a82SYishai Hadas 
1049eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
105078b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
1051eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
105278b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
1053eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
105478b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1055eb761894SArtemy Kovalyov 	}
1056eb761894SArtemy Kovalyov 
105789705e92SDanit Goldberg 	if (MLX5_CAP_GEN(mdev, tag_matching) &&
105889705e92SDanit Goldberg 	    MLX5_CAP_GEN(mdev, rndv_offload_rc)) {
105989705e92SDanit Goldberg 		props->tm_caps.flags = IB_TM_CAP_RNDV_RC;
106089705e92SDanit Goldberg 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
106189705e92SDanit Goldberg 	}
106289705e92SDanit Goldberg 
106387ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
106487ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
106587ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
106687ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
106787ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
106887ab3f52SYonatan Cohen 	}
106987ab3f52SYonatan Cohen 
1070a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), cqe_comp_caps) <= uhw_outlen) {
1071572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
1072572f46bfSYonatan Cohen 
1073572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
10747e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
1075572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
1076572f46bfSYonatan Cohen 					     cqe_compression_max_num);
1077572f46bfSYonatan Cohen 
10787e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
10797e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
10807e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
10816f1006a4SYonatan Cohen 
10826f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
10836f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
10846f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1085572f46bfSYonatan Cohen 		}
10867e43a2a5SBodong Wang 	}
10877e43a2a5SBodong Wang 
1088a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), packet_pacing_caps) <= uhw_outlen &&
108985c7c014SDaniel Jurgens 	    raw_support) {
1090d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1091d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
1092d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
1093d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1094d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
1095d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1096d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
1097d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
109861147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
109961147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
110061147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
110161147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
1102d949167dSBodong Wang 		}
1103d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1104d949167dSBodong Wang 	}
1105d949167dSBodong Wang 
1106a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes) <=
1107a762d460SLeon Romanovsky 	    uhw_outlen) {
1108795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
11099f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1110795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1111050da902SBodong Wang 
1112050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1113050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1114050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1115050da902SBodong Wang 
11169f885201SLeon Romanovsky 		resp.response_length +=
11179f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
11189f885201SLeon Romanovsky 	}
11199f885201SLeon Romanovsky 
1120a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), flags) <= uhw_outlen) {
1121de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
11227a0c8f42SGuy Levi 
1123de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1124de57f2adSGuy Levi 			resp.flags |=
1125de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
11267a0c8f42SGuy Levi 
11277a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
11287a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
11297e11b911SDanit Goldberg 		if (MLX5_CAP_GEN(mdev, qp_packet_based))
11307e11b911SDanit Goldberg 			resp.flags |=
11317e11b911SDanit Goldberg 				MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
11327249c8eaSGuy Levi 
11337249c8eaSGuy Levi 		resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1134de57f2adSGuy Levi 	}
11359f885201SLeon Romanovsky 
1136a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), sw_parsing_caps) <= uhw_outlen) {
113796dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
113896dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
113996dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
114096dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
114196dc3fc5SNoa Osherovich 
114296dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
114396dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
114496dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
114596dc3fc5SNoa Osherovich 
114696dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
114796dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
114896dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
114996dc3fc5SNoa Osherovich 
115096dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
115196dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
115296dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
115396dc3fc5SNoa Osherovich 		}
115496dc3fc5SNoa Osherovich 	}
115596dc3fc5SNoa Osherovich 
1156a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), striding_rq_caps) <= uhw_outlen &&
115785c7c014SDaniel Jurgens 	    raw_support) {
1158b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1159b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1160b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1161b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1162b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1163b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1164c16339b6SMark Zhang 			if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range))
1165c16339b6SMark Zhang 				resp.striding_rq_caps
1166c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1167c16339b6SMark Zhang 					MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1168c16339b6SMark Zhang 			else
1169c16339b6SMark Zhang 				resp.striding_rq_caps
1170c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1171b4f34597SNoa Osherovich 					MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1172b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1173b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1174b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1175b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1176b4f34597SNoa Osherovich 		}
1177b4f34597SNoa Osherovich 	}
1178b4f34597SNoa Osherovich 
1179a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), tunnel_offloads_caps) <= uhw_outlen) {
1180f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1181f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1182f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1183f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1184f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1185f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1186f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1187f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1188f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1189f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
119041e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre))
1191e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1192e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
119341e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_udp))
1194e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1195e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1196f95ef6cbSMaor Gottlieb 	}
1197f95ef6cbSMaor Gottlieb 
119811656f59SLior Nahmanson 	if (offsetofend(typeof(resp), dci_streams_caps) <= uhw_outlen) {
119911656f59SLior Nahmanson 		resp.response_length += sizeof(resp.dci_streams_caps);
120011656f59SLior Nahmanson 
120111656f59SLior Nahmanson 		resp.dci_streams_caps.max_log_num_concurent =
120211656f59SLior Nahmanson 			MLX5_CAP_GEN(mdev, log_max_dci_stream_channels);
120311656f59SLior Nahmanson 
120411656f59SLior Nahmanson 		resp.dci_streams_caps.max_log_num_errored =
120511656f59SLior Nahmanson 			MLX5_CAP_GEN(mdev, log_max_dci_errored_streams);
120611656f59SLior Nahmanson 	}
120711656f59SLior Nahmanson 
120848357091SLeon Romanovsky 	if (uhw_outlen) {
1209402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1210402ca536SBodong Wang 
1211402ca536SBodong Wang 		if (err)
1212402ca536SBodong Wang 			return err;
1213402ca536SBodong Wang 	}
1214402ca536SBodong Wang 
12151b5daf11SMajd Dibbiny 	return 0;
12161b5daf11SMajd Dibbiny }
1217e126ba97SEli Cohen 
translate_active_width(struct ib_device * ibdev,u16 active_width,u8 * ib_width)1218639bf441SAharon Landau static void translate_active_width(struct ib_device *ibdev, u16 active_width,
12191b5daf11SMajd Dibbiny 				   u8 *ib_width)
12201b5daf11SMajd Dibbiny {
12211b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12221b5daf11SMajd Dibbiny 
1223e27014bdSAharon Landau 	if (active_width & MLX5_PTYS_WIDTH_1X)
12241b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1225e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_2X)
1226d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1227e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_4X)
12281b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1229e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_8X)
12301b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1231e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_12X)
12321b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1233db7a691aSMichael Guralnik 	else {
1234db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
1235e27014bdSAharon Landau 			    active_width);
1236db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
12371b5daf11SMajd Dibbiny 	}
12381b5daf11SMajd Dibbiny 
1239db7a691aSMichael Guralnik 	return;
12401b5daf11SMajd Dibbiny }
12411b5daf11SMajd Dibbiny 
mlx5_mtu_to_ib_mtu(int mtu)12421b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
12431b5daf11SMajd Dibbiny {
12441b5daf11SMajd Dibbiny 	switch (mtu) {
12451b5daf11SMajd Dibbiny 	case 256: return 1;
12461b5daf11SMajd Dibbiny 	case 512: return 2;
12471b5daf11SMajd Dibbiny 	case 1024: return 3;
12481b5daf11SMajd Dibbiny 	case 2048: return 4;
12491b5daf11SMajd Dibbiny 	case 4096: return 5;
12501b5daf11SMajd Dibbiny 	default:
12511b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
12521b5daf11SMajd Dibbiny 		return -1;
12531b5daf11SMajd Dibbiny 	}
12541b5daf11SMajd Dibbiny }
12551b5daf11SMajd Dibbiny 
12561b5daf11SMajd Dibbiny enum ib_max_vl_num {
12571b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
12581b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
12591b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
12601b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
12611b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
12621b5daf11SMajd Dibbiny };
12631b5daf11SMajd Dibbiny 
12641b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
12651b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
12661b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
12671b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
12681b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
12691b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
12701b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
12711b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
12721b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
12731b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
12741b5daf11SMajd Dibbiny };
12751b5daf11SMajd Dibbiny 
translate_max_vl_num(struct ib_device * ibdev,u8 vl_hw_cap,u8 * max_vl_num)12761b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
12771b5daf11SMajd Dibbiny 				u8 *max_vl_num)
12781b5daf11SMajd Dibbiny {
12791b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
12801b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
12811b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
12821b5daf11SMajd Dibbiny 		break;
12831b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
12841b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
12851b5daf11SMajd Dibbiny 		break;
12861b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
12871b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
12881b5daf11SMajd Dibbiny 		break;
12891b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
12901b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
12911b5daf11SMajd Dibbiny 		break;
12921b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
12931b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
12941b5daf11SMajd Dibbiny 		break;
12951b5daf11SMajd Dibbiny 
12961b5daf11SMajd Dibbiny 	default:
12971b5daf11SMajd Dibbiny 		return -EINVAL;
12981b5daf11SMajd Dibbiny 	}
12991b5daf11SMajd Dibbiny 
13001b5daf11SMajd Dibbiny 	return 0;
13011b5daf11SMajd Dibbiny }
13021b5daf11SMajd Dibbiny 
mlx5_query_hca_port(struct ib_device * ibdev,u32 port,struct ib_port_attr * props)13031fb7f897SMark Bloch static int mlx5_query_hca_port(struct ib_device *ibdev, u32 port,
13041b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
13051b5daf11SMajd Dibbiny {
13061b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
13071b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
13081b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1309046339eaSSaeed Mahameed 	u16 max_mtu;
1310046339eaSSaeed Mahameed 	u16 oper_mtu;
13111b5daf11SMajd Dibbiny 	int err;
1312639bf441SAharon Landau 	u16 ib_link_width_oper;
13131b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
13141b5daf11SMajd Dibbiny 
13151b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
13161b5daf11SMajd Dibbiny 	if (!rep) {
13171b5daf11SMajd Dibbiny 		err = -ENOMEM;
13181b5daf11SMajd Dibbiny 		goto out;
13191b5daf11SMajd Dibbiny 	}
13201b5daf11SMajd Dibbiny 
1321c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
13221b5daf11SMajd Dibbiny 
13231b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
13241b5daf11SMajd Dibbiny 	if (err)
13251b5daf11SMajd Dibbiny 		goto out;
13261b5daf11SMajd Dibbiny 
13271b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
13281b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
13291b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
13301b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
13311b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
13321b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
13331b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
13341b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
13351b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
13361b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
13371b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
13381b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
13391b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
13401b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
13411b5daf11SMajd Dibbiny 
13424106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
13434106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
13444106a758SMichael Guralnik 
1345639bf441SAharon Landau 	err = mlx5_query_ib_port_oper(mdev, &ib_link_width_oper,
1346376ceb31SAharon Landau 				      &props->active_speed, port);
13471b5daf11SMajd Dibbiny 	if (err)
13481b5daf11SMajd Dibbiny 		goto out;
13491b5daf11SMajd Dibbiny 
1350db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1351db7a691aSMichael Guralnik 
1352facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
13531b5daf11SMajd Dibbiny 
13541b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
13551b5daf11SMajd Dibbiny 
1356facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
13571b5daf11SMajd Dibbiny 
13581b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
13591b5daf11SMajd Dibbiny 
13601b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
13611b5daf11SMajd Dibbiny 	if (err)
13621b5daf11SMajd Dibbiny 		goto out;
13631b5daf11SMajd Dibbiny 
13641b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
13651b5daf11SMajd Dibbiny 				   &props->max_vl_num);
13661b5daf11SMajd Dibbiny out:
13671b5daf11SMajd Dibbiny 	kfree(rep);
1368e126ba97SEli Cohen 	return err;
1369e126ba97SEli Cohen }
1370e126ba97SEli Cohen 
mlx5_ib_query_port(struct ib_device * ibdev,u32 port,struct ib_port_attr * props)13711fb7f897SMark Bloch int mlx5_ib_query_port(struct ib_device *ibdev, u32 port,
1372e126ba97SEli Cohen 		       struct ib_port_attr *props)
1373e126ba97SEli Cohen {
1374095b0927SIlan Tayari 	unsigned int count;
1375095b0927SIlan Tayari 	int ret;
1376095b0927SIlan Tayari 
13771b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13781b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1379095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1380095b0927SIlan Tayari 		break;
1381e126ba97SEli Cohen 
13821b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1383095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1384095b0927SIlan Tayari 		break;
13851b5daf11SMajd Dibbiny 
13863f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1387095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1388095b0927SIlan Tayari 		break;
13893f89a643SAchiad Shochat 
13901b5daf11SMajd Dibbiny 	default:
1391095b0927SIlan Tayari 		ret = -EINVAL;
1392e126ba97SEli Cohen 	}
1393095b0927SIlan Tayari 
1394095b0927SIlan Tayari 	if (!ret && props) {
1395b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1396b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1397b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1398b3cbd6f0SDaniel Jurgens 
1399b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1400b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1401b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1402b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1403b3cbd6f0SDaniel Jurgens 			 */
1404b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1405b3cbd6f0SDaniel Jurgens 			port = 1;
1406b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1407b3cbd6f0SDaniel Jurgens 		}
1408b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1409b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1410b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1411095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1412095b0927SIlan Tayari 	}
1413095b0927SIlan Tayari 	return ret;
1414e126ba97SEli Cohen }
1415e126ba97SEli Cohen 
mlx5_ib_rep_query_port(struct ib_device * ibdev,u32 port,struct ib_port_attr * props)14161fb7f897SMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u32 port,
14178e6efa3aSMark Bloch 				  struct ib_port_attr *props)
14188e6efa3aSMark Bloch {
14197a58779eSParav Pandit 	return mlx5_query_port_roce(ibdev, port, props);
14208e6efa3aSMark Bloch }
14218e6efa3aSMark Bloch 
mlx5_ib_rep_query_pkey(struct ib_device * ibdev,u32 port,u16 index,u16 * pkey)14221fb7f897SMark Bloch static int mlx5_ib_rep_query_pkey(struct ib_device *ibdev, u32 port, u16 index,
1423d6fd59e1SParav Pandit 				  u16 *pkey)
1424d6fd59e1SParav Pandit {
1425d6fd59e1SParav Pandit 	/* Default special Pkey for representor device port as per the
1426d6fd59e1SParav Pandit 	 * IB specification 1.3 section 10.9.1.2.
1427d6fd59e1SParav Pandit 	 */
1428d6fd59e1SParav Pandit 	*pkey = 0xffff;
1429d6fd59e1SParav Pandit 	return 0;
1430d6fd59e1SParav Pandit }
1431d6fd59e1SParav Pandit 
mlx5_ib_query_gid(struct ib_device * ibdev,u32 port,int index,union ib_gid * gid)14321fb7f897SMark Bloch static int mlx5_ib_query_gid(struct ib_device *ibdev, u32 port, int index,
1433e126ba97SEli Cohen 			     union ib_gid *gid)
1434e126ba97SEli Cohen {
14351b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
14361b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1437e126ba97SEli Cohen 
14381b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14391b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14401b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1441e126ba97SEli Cohen 
14421b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14431b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1444e126ba97SEli Cohen 
14451b5daf11SMajd Dibbiny 	default:
14461b5daf11SMajd Dibbiny 		return -EINVAL;
14471b5daf11SMajd Dibbiny 	}
1448e126ba97SEli Cohen 
1449e126ba97SEli Cohen }
1450e126ba97SEli Cohen 
mlx5_query_hca_nic_pkey(struct ib_device * ibdev,u32 port,u16 index,u16 * pkey)14511fb7f897SMark Bloch static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u32 port,
1452b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1453b3cbd6f0SDaniel Jurgens {
1454b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1455b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1456b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
14571fb7f897SMark Bloch 	u32 mdev_port_num;
1458b3cbd6f0SDaniel Jurgens 	int err;
1459b3cbd6f0SDaniel Jurgens 
1460b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1461b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1462b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1463b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1464b3cbd6f0SDaniel Jurgens 		 */
1465b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1466b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1467b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1468b3cbd6f0SDaniel Jurgens 	}
1469b3cbd6f0SDaniel Jurgens 
1470b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1471b3cbd6f0SDaniel Jurgens 					index, pkey);
1472b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1473b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1474b3cbd6f0SDaniel Jurgens 
1475b3cbd6f0SDaniel Jurgens 	return err;
1476b3cbd6f0SDaniel Jurgens }
1477b3cbd6f0SDaniel Jurgens 
mlx5_ib_query_pkey(struct ib_device * ibdev,u32 port,u16 index,u16 * pkey)14781fb7f897SMark Bloch static int mlx5_ib_query_pkey(struct ib_device *ibdev, u32 port, u16 index,
1479e126ba97SEli Cohen 			      u16 *pkey)
1480e126ba97SEli Cohen {
14811b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14821b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14831b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1484e126ba97SEli Cohen 
14851b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14861b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1487b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
14881b5daf11SMajd Dibbiny 	default:
14891b5daf11SMajd Dibbiny 		return -EINVAL;
1490e126ba97SEli Cohen 	}
14911b5daf11SMajd Dibbiny }
1492e126ba97SEli Cohen 
mlx5_ib_modify_device(struct ib_device * ibdev,int mask,struct ib_device_modify * props)1493e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1494e126ba97SEli Cohen 				 struct ib_device_modify *props)
1495e126ba97SEli Cohen {
1496e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1497e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1498e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1499e126ba97SEli Cohen 	int err;
1500e126ba97SEli Cohen 
1501e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1502e126ba97SEli Cohen 		return -EOPNOTSUPP;
1503e126ba97SEli Cohen 
1504e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1505e126ba97SEli Cohen 		return 0;
1506e126ba97SEli Cohen 
1507e126ba97SEli Cohen 	/*
1508e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1509e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1510e126ba97SEli Cohen 	 */
1511bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
15129603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1513e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1514e126ba97SEli Cohen 	if (err)
1515e126ba97SEli Cohen 		return err;
1516e126ba97SEli Cohen 
1517bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1518e126ba97SEli Cohen 
1519e126ba97SEli Cohen 	return err;
1520e126ba97SEli Cohen }
1521e126ba97SEli Cohen 
set_port_caps_atomic(struct mlx5_ib_dev * dev,u32 port_num,u32 mask,u32 value)15221fb7f897SMark Bloch static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u32 port_num, u32 mask,
1523cdbe33d0SEli Cohen 				u32 value)
1524cdbe33d0SEli Cohen {
1525cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1526b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
15271fb7f897SMark Bloch 	u32 mdev_port_num;
1528cdbe33d0SEli Cohen 	int err;
1529cdbe33d0SEli Cohen 
1530b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1531b3cbd6f0SDaniel Jurgens 	if (!mdev)
1532b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1533b3cbd6f0SDaniel Jurgens 
1534b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1535cdbe33d0SEli Cohen 	if (err)
1536b3cbd6f0SDaniel Jurgens 		goto out;
1537cdbe33d0SEli Cohen 
1538cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1539cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1540cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1541b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1542b3cbd6f0SDaniel Jurgens 		goto out;
1543cdbe33d0SEli Cohen 	}
1544cdbe33d0SEli Cohen 
1545cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1546cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1547b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1548b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1549b3cbd6f0SDaniel Jurgens 
1550b3cbd6f0SDaniel Jurgens out:
1551b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1552cdbe33d0SEli Cohen 
1553cdbe33d0SEli Cohen 	return err;
1554cdbe33d0SEli Cohen }
1555cdbe33d0SEli Cohen 
mlx5_ib_modify_port(struct ib_device * ibdev,u32 port,int mask,struct ib_port_modify * props)15561fb7f897SMark Bloch static int mlx5_ib_modify_port(struct ib_device *ibdev, u32 port, int mask,
1557e126ba97SEli Cohen 			       struct ib_port_modify *props)
1558e126ba97SEli Cohen {
1559e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1560e126ba97SEli Cohen 	struct ib_port_attr attr;
1561e126ba97SEli Cohen 	u32 tmp;
1562e126ba97SEli Cohen 	int err;
1563cdbe33d0SEli Cohen 	u32 change_mask;
1564cdbe33d0SEli Cohen 	u32 value;
1565cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1566cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1567cdbe33d0SEli Cohen 
1568ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1569ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1570ec255879SMajd Dibbiny 	 */
1571ec255879SMajd Dibbiny 	if (!is_ib)
1572ec255879SMajd Dibbiny 		return 0;
1573ec255879SMajd Dibbiny 
1574cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1575cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1576cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1577cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1578cdbe33d0SEli Cohen 	}
1579e126ba97SEli Cohen 
1580e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1581e126ba97SEli Cohen 
1582c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1583e126ba97SEli Cohen 	if (err)
1584e126ba97SEli Cohen 		goto out;
1585e126ba97SEli Cohen 
1586e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1587e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1588e126ba97SEli Cohen 
15899603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1590e126ba97SEli Cohen 
1591e126ba97SEli Cohen out:
1592e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1593e126ba97SEli Cohen 	return err;
1594e126ba97SEli Cohen }
1595e126ba97SEli Cohen 
print_lib_caps(struct mlx5_ib_dev * dev,u64 caps)159630aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
159730aa60b3SEli Cohen {
159830aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
159930aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
160030aa60b3SEli Cohen }
160130aa60b3SEli Cohen 
calc_dynamic_bfregs(int uars_per_sys_page)160231a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
160331a78a5aSYishai Hadas {
160431a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
160531a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
160631a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
160731a78a5aSYishai Hadas 
160831a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
160931a78a5aSYishai Hadas }
161031a78a5aSYishai Hadas 
calc_total_bfregs(struct mlx5_ib_dev * dev,bool lib_uar_4k,struct mlx5_ib_alloc_ucontext_req_v2 * req,struct mlx5_bfreg_info * bfregi)1611b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1612b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
161331a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1614b037c29aSEli Cohen {
1615b037c29aSEli Cohen 	int uars_per_sys_page;
1616b037c29aSEli Cohen 	int bfregs_per_sys_page;
1617b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1618b037c29aSEli Cohen 
1619b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1620b037c29aSEli Cohen 		return -EINVAL;
1621b037c29aSEli Cohen 
1622b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1623b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1624b037c29aSEli Cohen 
1625b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1626b037c29aSEli Cohen 		return -ENOMEM;
1627b037c29aSEli Cohen 
1628b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1629b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
163031a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1631b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1632b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1633b037c29aSEli Cohen 		return -EINVAL;
1634b037c29aSEli Cohen 
163531a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
163631a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
163731a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
163831a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
163931a78a5aSYishai Hadas 
164031a78a5aSYishai 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",
1641b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1642b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
164331a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
164431a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1645b037c29aSEli Cohen 
1646b037c29aSEli Cohen 	return 0;
1647b037c29aSEli Cohen }
1648b037c29aSEli Cohen 
allocate_uars(struct mlx5_ib_dev * dev,struct mlx5_ib_ucontext * context)1649b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1650b037c29aSEli Cohen {
1651b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1652b037c29aSEli Cohen 	int err;
1653b037c29aSEli Cohen 	int i;
1654b037c29aSEli Cohen 
1655b037c29aSEli Cohen 	bfregi = &context->bfregi;
165631a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1657d2c8a155SMeir Lichtinger 		err = mlx5_cmd_uar_alloc(dev->mdev, &bfregi->sys_pages[i],
1658d2c8a155SMeir Lichtinger 					 context->devx_uid);
1659b037c29aSEli Cohen 		if (err)
1660b037c29aSEli Cohen 			goto error;
1661b037c29aSEli Cohen 
1662b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1663b037c29aSEli Cohen 	}
16644ed131d0SYishai Hadas 
16654ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
16664ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
16674ed131d0SYishai Hadas 
1668b037c29aSEli Cohen 	return 0;
1669b037c29aSEli Cohen 
1670b037c29aSEli Cohen error:
1671b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1672d2c8a155SMeir Lichtinger 		if (mlx5_cmd_uar_dealloc(dev->mdev, bfregi->sys_pages[i],
1673d2c8a155SMeir Lichtinger 					 context->devx_uid))
1674b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1675b037c29aSEli Cohen 
1676b037c29aSEli Cohen 	return err;
1677b037c29aSEli Cohen }
1678b037c29aSEli Cohen 
deallocate_uars(struct mlx5_ib_dev * dev,struct mlx5_ib_ucontext * context)167915177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
168015177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1681b037c29aSEli Cohen {
1682b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1683b037c29aSEli Cohen 	int i;
1684b037c29aSEli Cohen 
1685b037c29aSEli Cohen 	bfregi = &context->bfregi;
168615177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
16874ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
168815177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
1689d2c8a155SMeir Lichtinger 			mlx5_cmd_uar_dealloc(dev->mdev, bfregi->sys_pages[i],
1690d2c8a155SMeir Lichtinger 					     context->devx_uid);
1691b037c29aSEli Cohen }
1692b037c29aSEli Cohen 
mlx5_ib_enable_lb(struct mlx5_ib_dev * dev,bool td,bool qp)16930042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1694a560f1d9SMark Bloch {
1695a560f1d9SMark Bloch 	int err = 0;
1696a560f1d9SMark Bloch 
1697a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16980042f9e4SMark Bloch 	if (td)
1699a560f1d9SMark Bloch 		dev->lb.user_td++;
17000042f9e4SMark Bloch 	if (qp)
17010042f9e4SMark Bloch 		dev->lb.qps++;
1702a560f1d9SMark Bloch 
17030042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
17040042f9e4SMark Bloch 	    dev->lb.qps == 1) {
17050042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1706a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
17070042f9e4SMark Bloch 			dev->lb.enabled = true;
17080042f9e4SMark Bloch 		}
17090042f9e4SMark Bloch 	}
1710a560f1d9SMark Bloch 
1711a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1712a560f1d9SMark Bloch 
1713a560f1d9SMark Bloch 	return err;
1714a560f1d9SMark Bloch }
1715a560f1d9SMark Bloch 
mlx5_ib_disable_lb(struct mlx5_ib_dev * dev,bool td,bool qp)17160042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1717a560f1d9SMark Bloch {
1718a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
17190042f9e4SMark Bloch 	if (td)
1720a560f1d9SMark Bloch 		dev->lb.user_td--;
17210042f9e4SMark Bloch 	if (qp)
17220042f9e4SMark Bloch 		dev->lb.qps--;
1723a560f1d9SMark Bloch 
17240042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
17250042f9e4SMark Bloch 	    dev->lb.qps == 0) {
17260042f9e4SMark Bloch 		if (dev->lb.enabled) {
1727a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
17280042f9e4SMark Bloch 			dev->lb.enabled = false;
17290042f9e4SMark Bloch 		}
17300042f9e4SMark Bloch 	}
1731a560f1d9SMark Bloch 
1732a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1733a560f1d9SMark Bloch }
1734a560f1d9SMark Bloch 
mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev * dev,u32 * tdn,u16 uid)1735d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1736d2d19121SYishai Hadas 					  u16 uid)
1737c85023e1SHuy Nguyen {
1738c85023e1SHuy Nguyen 	int err;
1739c85023e1SHuy Nguyen 
1740cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1741cfdeb893SLeon Romanovsky 		return 0;
1742cfdeb893SLeon Romanovsky 
1743d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1744c85023e1SHuy Nguyen 	if (err)
1745c85023e1SHuy Nguyen 		return err;
1746c85023e1SHuy Nguyen 
1747c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17488978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17498978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1750c85023e1SHuy Nguyen 		return err;
1751c85023e1SHuy Nguyen 
17520042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1753c85023e1SHuy Nguyen }
1754c85023e1SHuy Nguyen 
mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev * dev,u32 tdn,u16 uid)1755d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1756d2d19121SYishai Hadas 					     u16 uid)
1757c85023e1SHuy Nguyen {
1758cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1759cfdeb893SLeon Romanovsky 		return;
1760cfdeb893SLeon Romanovsky 
1761d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1762c85023e1SHuy Nguyen 
1763c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17648978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17658978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1766c85023e1SHuy Nguyen 		return;
1767c85023e1SHuy Nguyen 
17680042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1769c85023e1SHuy Nguyen }
1770c85023e1SHuy Nguyen 
set_ucontext_resp(struct ib_ucontext * uctx,struct mlx5_ib_alloc_ucontext_resp * resp)177145ec21c9SYishai Hadas static int set_ucontext_resp(struct ib_ucontext *uctx,
177245ec21c9SYishai Hadas 			     struct mlx5_ib_alloc_ucontext_resp *resp)
177345ec21c9SYishai Hadas {
177445ec21c9SYishai Hadas 	struct ib_device *ibdev = uctx->device;
177545ec21c9SYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
177645ec21c9SYishai Hadas 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
177745ec21c9SYishai Hadas 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
177845ec21c9SYishai Hadas 
177945ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1780594cac11SOr Har-Toov 		resp->dump_fill_mkey = dev->mkeys.dump_fill_mkey;
178145ec21c9SYishai Hadas 		resp->comp_mask |=
178245ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
178345ec21c9SYishai Hadas 	}
178445ec21c9SYishai Hadas 
178545ec21c9SYishai Hadas 	resp->qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
178645ec21c9SYishai Hadas 	if (dev->wc_support)
178745ec21c9SYishai Hadas 		resp->bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev,
178845ec21c9SYishai Hadas 						      log_bf_reg_size);
178945ec21c9SYishai Hadas 	resp->cache_line_size = cache_line_size();
179045ec21c9SYishai Hadas 	resp->max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
179145ec21c9SYishai Hadas 	resp->max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
179245ec21c9SYishai Hadas 	resp->max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
179345ec21c9SYishai Hadas 	resp->max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
179445ec21c9SYishai Hadas 	resp->max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
179545ec21c9SYishai Hadas 	resp->cqe_version = context->cqe_version;
179645ec21c9SYishai Hadas 	resp->log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
179745ec21c9SYishai Hadas 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
179845ec21c9SYishai Hadas 	resp->num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
179945ec21c9SYishai Hadas 					MLX5_CAP_GEN(dev->mdev,
180045ec21c9SYishai Hadas 						     num_of_uars_per_page) : 1;
180145ec21c9SYishai Hadas 	resp->tot_bfregs = bfregi->lib_uar_dyn ? 0 :
180245ec21c9SYishai Hadas 			bfregi->total_num_bfregs - bfregi->num_dyn_bfregs;
180345ec21c9SYishai Hadas 	resp->num_ports = dev->num_ports;
180445ec21c9SYishai Hadas 	resp->cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
180545ec21c9SYishai Hadas 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
180645ec21c9SYishai Hadas 
180745ec21c9SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
180845ec21c9SYishai Hadas 		mlx5_query_min_inline(dev->mdev, &resp->eth_min_inline);
180945ec21c9SYishai Hadas 		resp->eth_min_inline++;
181045ec21c9SYishai Hadas 	}
181145ec21c9SYishai Hadas 
181245ec21c9SYishai Hadas 	if (dev->mdev->clock_info)
181345ec21c9SYishai Hadas 		resp->clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
181445ec21c9SYishai Hadas 
181545ec21c9SYishai Hadas 	/*
181645ec21c9SYishai Hadas 	 * We don't want to expose information from the PCI bar that is located
181745ec21c9SYishai Hadas 	 * after 4096 bytes, so if the arch only supports larger pages, let's
181845ec21c9SYishai Hadas 	 * pretend we don't support reading the HCA's core clock. This is also
181945ec21c9SYishai Hadas 	 * forced by mmap function.
182045ec21c9SYishai Hadas 	 */
182145ec21c9SYishai Hadas 	if (PAGE_SIZE <= 4096) {
182245ec21c9SYishai Hadas 		resp->comp_mask |=
182345ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
182445ec21c9SYishai Hadas 		resp->hca_core_clock_offset =
182545ec21c9SYishai Hadas 			offsetof(struct mlx5_init_seg,
182645ec21c9SYishai Hadas 				 internal_timer_h) % PAGE_SIZE;
182745ec21c9SYishai Hadas 	}
182845ec21c9SYishai Hadas 
182945ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, ece_support))
183045ec21c9SYishai Hadas 		resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_ECE;
183145ec21c9SYishai Hadas 
183233652951SAharon Landau 	if (rt_supported(MLX5_CAP_GEN(dev->mdev, sq_ts_format)) &&
183333652951SAharon Landau 	    rt_supported(MLX5_CAP_GEN(dev->mdev, rq_ts_format)) &&
183433652951SAharon Landau 	    rt_supported(MLX5_CAP_ROCE(dev->mdev, qp_ts_format)))
183533652951SAharon Landau 		resp->comp_mask |=
183633652951SAharon Landau 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_REAL_TIME_TS;
183733652951SAharon Landau 
183845ec21c9SYishai Hadas 	resp->num_dyn_bfregs = bfregi->num_dyn_bfregs;
1839c906b86eSSergey Gorenko 
1840c906b86eSSergey Gorenko 	if (MLX5_CAP_GEN(dev->mdev, drain_sigerr))
1841c906b86eSSergey Gorenko 		resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_SQD2RTS;
1842c906b86eSSergey Gorenko 
184313ad1125SAharon Landau 	resp->comp_mask |=
184413ad1125SAharon Landau 		MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_MKEY_UPDATE_TAG;
184513ad1125SAharon Landau 
184645ec21c9SYishai Hadas 	return 0;
184745ec21c9SYishai Hadas }
184845ec21c9SYishai Hadas 
mlx5_ib_alloc_ucontext(struct ib_ucontext * uctx,struct ib_udata * udata)1849a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1850e126ba97SEli Cohen 				  struct ib_udata *udata)
1851e126ba97SEli Cohen {
1852a2a074efSLeon Romanovsky 	struct ib_device *ibdev = uctx->device;
1853e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1854b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1855b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1856a2a074efSLeon Romanovsky 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
18572f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
185878c0f98cSEli Cohen 	int ver;
1859e126ba97SEli Cohen 	int err;
1860a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1861a168a41cSMajd Dibbiny 				     max_cqe_version);
1862b037c29aSEli Cohen 	bool lib_uar_4k;
18630a2fd01cSYishai Hadas 	bool lib_uar_dyn;
1864e126ba97SEli Cohen 
1865e126ba97SEli Cohen 	if (!dev->ib_active)
1866a2a074efSLeon Romanovsky 		return -EAGAIN;
1867e126ba97SEli Cohen 
1868e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
186978c0f98cSEli Cohen 		ver = 0;
1870e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
187178c0f98cSEli Cohen 		ver = 2;
187278c0f98cSEli Cohen 	else
1873a2a074efSLeon Romanovsky 		return -EINVAL;
187478c0f98cSEli Cohen 
1875e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1876e126ba97SEli Cohen 	if (err)
1877a2a074efSLeon Romanovsky 		return err;
1878e126ba97SEli Cohen 
1879a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1880a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
188178c0f98cSEli Cohen 
1882f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1883a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
1884b368d7cbSMatan Barak 
18852f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
18862f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
18872f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1888a2a074efSLeon Romanovsky 		return -EINVAL;
1889e126ba97SEli Cohen 
1890d2c8a155SMeir Lichtinger 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1891d2c8a155SMeir Lichtinger 		err = mlx5_ib_devx_create(dev, true);
1892d2c8a155SMeir Lichtinger 		if (err < 0)
1893d2c8a155SMeir Lichtinger 			goto out_ctx;
1894d2c8a155SMeir Lichtinger 		context->devx_uid = err;
1895d2c8a155SMeir Lichtinger 	}
1896d2c8a155SMeir Lichtinger 
189730aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
18980a2fd01cSYishai Hadas 	lib_uar_dyn = req.lib_caps & MLX5_LIB_CAP_DYN_UAR;
18992f5ff264SEli Cohen 	bfregi = &context->bfregi;
1900b037c29aSEli Cohen 
19010a2fd01cSYishai Hadas 	if (lib_uar_dyn) {
19020a2fd01cSYishai Hadas 		bfregi->lib_uar_dyn = lib_uar_dyn;
19030a2fd01cSYishai Hadas 		goto uar_done;
19040a2fd01cSYishai Hadas 	}
19050a2fd01cSYishai Hadas 
1906b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
190731a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1908b037c29aSEli Cohen 	if (err)
1909d2c8a155SMeir Lichtinger 		goto out_devx;
1910b037c29aSEli Cohen 
19112f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1912b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
191331a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1914b037c29aSEli Cohen 				GFP_KERNEL);
1915b037c29aSEli Cohen 	if (!bfregi->count) {
1916e126ba97SEli Cohen 		err = -ENOMEM;
1917d2c8a155SMeir Lichtinger 		goto out_devx;
1918e126ba97SEli Cohen 	}
1919e126ba97SEli Cohen 
1920b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1921b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1922e126ba97SEli Cohen 				    GFP_KERNEL);
1923b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1924e126ba97SEli Cohen 		err = -ENOMEM;
1925e126ba97SEli Cohen 		goto out_count;
1926e126ba97SEli Cohen 	}
1927e126ba97SEli Cohen 
1928b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1929b037c29aSEli Cohen 	if (err)
1930b037c29aSEli Cohen 		goto out_sys_pages;
1931b037c29aSEli Cohen 
19320a2fd01cSYishai Hadas uar_done:
1933d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1934d2d19121SYishai Hadas 					     context->devx_uid);
1935a8b92ca1SYishai Hadas 	if (err)
1936d2c8a155SMeir Lichtinger 		goto out_uars;
1937a8b92ca1SYishai Hadas 
1938e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1939e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1940e126ba97SEli Cohen 
194145ec21c9SYishai Hadas 	context->cqe_version = min_t(__u8,
194245ec21c9SYishai Hadas 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
194345ec21c9SYishai Hadas 				 req.max_cqe_version);
1944b368d7cbSMatan Barak 
194545ec21c9SYishai Hadas 	err = set_ucontext_resp(uctx, &resp);
194645ec21c9SYishai Hadas 	if (err)
194745ec21c9SYishai Hadas 		goto out_mdev;
1948b368d7cbSMatan Barak 
194945ec21c9SYishai Hadas 	resp.response_length = min(udata->outlen, sizeof(resp));
1950b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1951e126ba97SEli Cohen 	if (err)
1952a8b92ca1SYishai Hadas 		goto out_mdev;
1953e126ba97SEli Cohen 
19542f5ff264SEli Cohen 	bfregi->ver = ver;
19552f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
195630aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
195730aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1958f72300c5SHaggai Abramovsky 
1959802dcc7fSMark Zhang 	if (mlx5_ib_lag_should_assign_affinity(dev)) {
19601fb7f897SMark Bloch 		u32 port = mlx5_core_native_port_num(dev->mdev) - 1;
1961c6a21c38SMajd Dibbiny 
1962c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1963c6a21c38SMajd Dibbiny 			   atomic_add_return(
196495579e78SMark Bloch 				   1, &dev->port[port].roce.tx_port_affinity));
1965c6a21c38SMajd Dibbiny 	}
1966c6a21c38SMajd Dibbiny 
1967a2a074efSLeon Romanovsky 	return 0;
1968e126ba97SEli Cohen 
1969a8b92ca1SYishai Hadas out_mdev:
1970d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1971146d2f1aSmajd@mellanox.com 
1972e126ba97SEli Cohen out_uars:
1973b037c29aSEli Cohen 	deallocate_uars(dev, context);
1974b037c29aSEli Cohen 
1975b037c29aSEli Cohen out_sys_pages:
1976b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1977b037c29aSEli Cohen 
1978e126ba97SEli Cohen out_count:
19792f5ff264SEli Cohen 	kfree(bfregi->count);
1980e126ba97SEli Cohen 
1981d2c8a155SMeir Lichtinger out_devx:
1982d2c8a155SMeir Lichtinger 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
1983d2c8a155SMeir Lichtinger 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1984d2c8a155SMeir Lichtinger 
1985e126ba97SEli Cohen out_ctx:
1986a2a074efSLeon Romanovsky 	return err;
1987e126ba97SEli Cohen }
1988e126ba97SEli Cohen 
mlx5_ib_query_ucontext(struct ib_ucontext * ibcontext,struct uverbs_attr_bundle * attrs)19890fb556b2SYishai Hadas static int mlx5_ib_query_ucontext(struct ib_ucontext *ibcontext,
19900fb556b2SYishai Hadas 				  struct uverbs_attr_bundle *attrs)
19910fb556b2SYishai Hadas {
19920fb556b2SYishai Hadas 	struct mlx5_ib_alloc_ucontext_resp uctx_resp = {};
19930fb556b2SYishai Hadas 	int ret;
19940fb556b2SYishai Hadas 
19950fb556b2SYishai Hadas 	ret = set_ucontext_resp(ibcontext, &uctx_resp);
19960fb556b2SYishai Hadas 	if (ret)
19970fb556b2SYishai Hadas 		return ret;
19980fb556b2SYishai Hadas 
19990fb556b2SYishai Hadas 	uctx_resp.response_length =
20000fb556b2SYishai Hadas 		min_t(size_t,
20010fb556b2SYishai Hadas 		      uverbs_attr_get_len(attrs,
20020fb556b2SYishai Hadas 				MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX),
20030fb556b2SYishai Hadas 		      sizeof(uctx_resp));
20040fb556b2SYishai Hadas 
20050fb556b2SYishai Hadas 	ret = uverbs_copy_to_struct_or_zero(attrs,
20060fb556b2SYishai Hadas 					MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
20070fb556b2SYishai Hadas 					&uctx_resp,
20080fb556b2SYishai Hadas 					sizeof(uctx_resp));
20090fb556b2SYishai Hadas 	return ret;
20100fb556b2SYishai Hadas }
20110fb556b2SYishai Hadas 
mlx5_ib_dealloc_ucontext(struct ib_ucontext * ibcontext)2012a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
2013e126ba97SEli Cohen {
2014e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2015e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
2016b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
2017e126ba97SEli Cohen 
2018b037c29aSEli Cohen 	bfregi = &context->bfregi;
2019d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2020d2d19121SYishai Hadas 
2021b037c29aSEli Cohen 	deallocate_uars(dev, context);
2022b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
20232f5ff264SEli Cohen 	kfree(bfregi->count);
2024d2c8a155SMeir Lichtinger 
2025d2c8a155SMeir Lichtinger 	if (context->devx_uid)
2026d2c8a155SMeir Lichtinger 		mlx5_ib_devx_destroy(dev, context->devx_uid);
2027e126ba97SEli Cohen }
2028e126ba97SEli Cohen 
uar_index2pfn(struct mlx5_ib_dev * dev,int uar_idx)2029b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
20304ed131d0SYishai Hadas 				 int uar_idx)
2031e126ba97SEli Cohen {
2032b037c29aSEli Cohen 	int fw_uars_per_page;
2033b037c29aSEli Cohen 
2034b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2035b037c29aSEli Cohen 
2036aa8106f1SHuy Nguyen 	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2037e126ba97SEli Cohen }
2038e126ba97SEli Cohen 
uar_index2paddress(struct mlx5_ib_dev * dev,int uar_idx)2039342ee59dSYishai Hadas static u64 uar_index2paddress(struct mlx5_ib_dev *dev,
2040342ee59dSYishai Hadas 				 int uar_idx)
2041342ee59dSYishai Hadas {
2042342ee59dSYishai Hadas 	unsigned int fw_uars_per_page;
2043342ee59dSYishai Hadas 
2044342ee59dSYishai Hadas 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
2045342ee59dSYishai Hadas 				MLX5_UARS_IN_PAGE : 1;
2046342ee59dSYishai Hadas 
2047342ee59dSYishai Hadas 	return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE);
2048342ee59dSYishai Hadas }
2049342ee59dSYishai Hadas 
get_command(unsigned long offset)2050e126ba97SEli Cohen static int get_command(unsigned long offset)
2051e126ba97SEli Cohen {
2052e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2053e126ba97SEli Cohen }
2054e126ba97SEli Cohen 
get_arg(unsigned long offset)2055e126ba97SEli Cohen static int get_arg(unsigned long offset)
2056e126ba97SEli Cohen {
2057e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2058e126ba97SEli Cohen }
2059e126ba97SEli Cohen 
get_index(unsigned long offset)2060e126ba97SEli Cohen static int get_index(unsigned long offset)
2061e126ba97SEli Cohen {
2062e126ba97SEli Cohen 	return get_arg(offset);
2063e126ba97SEli Cohen }
2064e126ba97SEli Cohen 
20654ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
get_extended_index(unsigned long offset)20664ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20674ed131d0SYishai Hadas {
20684ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20694ed131d0SYishai Hadas }
20704ed131d0SYishai Hadas 
20717c2344c3SMaor Gottlieb 
mlx5_ib_disassociate_ucontext(struct ib_ucontext * ibcontext)20727c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20737c2344c3SMaor Gottlieb {
20747c2344c3SMaor Gottlieb }
20757c2344c3SMaor Gottlieb 
mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)207637aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2077e126ba97SEli Cohen {
207837aa5c36SGuy Levi 	switch (cmd) {
207937aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
208037aa5c36SGuy Levi 		return "WC";
2081e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
208237aa5c36SGuy Levi 		return "best effort WC";
208337aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
208437aa5c36SGuy Levi 		return "NC";
208524da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
208624da0016SAriel Levkovich 		return "Device Memory";
208737aa5c36SGuy Levi 	default:
2088dab994bcSShay Drory 		return "Unknown";
208937aa5c36SGuy Levi 	}
209037aa5c36SGuy Levi }
209137aa5c36SGuy Levi 
mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev * dev,struct vm_area_struct * vma,struct mlx5_ib_ucontext * context)20925c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20935c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20945c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20955c99eaecSFeras Daoud {
20964eb6ab13SJason Gunthorpe 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
20974eb6ab13SJason Gunthorpe 	    !(vma->vm_flags & VM_SHARED))
20985c99eaecSFeras Daoud 		return -EINVAL;
20995c99eaecSFeras Daoud 
21005c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
21015c99eaecSFeras Daoud 		return -EOPNOTSUPP;
21025c99eaecSFeras Daoud 
21034eb6ab13SJason Gunthorpe 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
21045c99eaecSFeras Daoud 		return -EPERM;
21051c71222eSSuren Baghdasaryan 	vm_flags_clear(vma, VM_MAYWRITE);
21065c99eaecSFeras Daoud 
2107ddcdc368SJason Gunthorpe 	if (!dev->mdev->clock_info)
21085c99eaecSFeras Daoud 		return -EOPNOTSUPP;
21095c99eaecSFeras Daoud 
21104eb6ab13SJason Gunthorpe 	return vm_insert_page(vma, vma->vm_start,
21114eb6ab13SJason Gunthorpe 			      virt_to_page(dev->mdev->clock_info));
21125c99eaecSFeras Daoud }
21135c99eaecSFeras Daoud 
mlx5_ib_mmap_free(struct rdma_user_mmap_entry * entry)2114dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
2115dc2316ebSYishai Hadas {
2116dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
2117dc2316ebSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device);
21187be76befSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
2119d2c8a155SMeir Lichtinger 	struct mlx5_ib_ucontext *context = to_mucontext(entry->ucontext);
2120dc2316ebSYishai Hadas 
2121dc2316ebSYishai Hadas 	switch (mentry->mmap_flag) {
2122dc2316ebSYishai Hadas 	case MLX5_IB_MMAP_TYPE_MEMIC:
2123cea85fa5SMaor Gottlieb 	case MLX5_IB_MMAP_TYPE_MEMIC_OP:
2124cea85fa5SMaor Gottlieb 		mlx5_ib_dm_mmap_free(dev, mentry);
2125dc2316ebSYishai Hadas 		break;
21267be76befSYishai Hadas 	case MLX5_IB_MMAP_TYPE_VAR:
21277be76befSYishai Hadas 		mutex_lock(&var_table->bitmap_lock);
21287be76befSYishai Hadas 		clear_bit(mentry->page_idx, var_table->bitmap);
21297be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
21307be76befSYishai Hadas 		kfree(mentry);
21317be76befSYishai Hadas 		break;
2132342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_WC:
2133342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_NC:
2134d2c8a155SMeir Lichtinger 		mlx5_cmd_uar_dealloc(dev->mdev, mentry->page_idx,
2135d2c8a155SMeir Lichtinger 				     context->devx_uid);
2136342ee59dSYishai Hadas 		kfree(mentry);
2137342ee59dSYishai Hadas 		break;
2138dc2316ebSYishai Hadas 	default:
2139dc2316ebSYishai Hadas 		WARN_ON(true);
2140dc2316ebSYishai Hadas 	}
2141dc2316ebSYishai Hadas }
2142dc2316ebSYishai Hadas 
uar_mmap(struct mlx5_ib_dev * dev,enum mlx5_ib_mmap_cmd cmd,struct vm_area_struct * vma,struct mlx5_ib_ucontext * context)214337aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
21447c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
21457c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
214637aa5c36SGuy Levi {
21472f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
214837aa5c36SGuy Levi 	int err;
214937aa5c36SGuy Levi 	unsigned long idx;
2150aa09ea6eSKamal Heib 	phys_addr_t pfn;
215137aa5c36SGuy Levi 	pgprot_t prot;
21524ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
21534ed131d0SYishai Hadas 	u32 uar_index;
21544ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
21554ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
21564ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2157b037c29aSEli Cohen 
21580a2fd01cSYishai Hadas 	if (bfregi->lib_uar_dyn)
21590a2fd01cSYishai Hadas 		return -EINVAL;
21600a2fd01cSYishai Hadas 
2161b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2162b037c29aSEli Cohen 		return -EINVAL;
2163b037c29aSEli Cohen 
21644ed131d0SYishai Hadas 	if (dyn_uar)
21654ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
21664ed131d0SYishai Hadas 	else
2167b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
21684ed131d0SYishai Hadas 
21694ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21704ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21714ed131d0SYishai Hadas 			     idx, max_valid_idx);
2172b037c29aSEli Cohen 		return -EINVAL;
2173b037c29aSEli Cohen 	}
217437aa5c36SGuy Levi 
217537aa5c36SGuy Levi 	switch (cmd) {
217637aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21774ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
217837aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
217937aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
218037aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
218137aa5c36SGuy Levi 		break;
218237aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
218337aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
218437aa5c36SGuy Levi 		break;
218537aa5c36SGuy Levi 	default:
218637aa5c36SGuy Levi 		return -EINVAL;
218737aa5c36SGuy Levi 	}
218837aa5c36SGuy Levi 
21894ed131d0SYishai Hadas 	if (dyn_uar) {
21904ed131d0SYishai Hadas 		int uars_per_page;
21914ed131d0SYishai Hadas 
21924ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21934ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21944ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21954ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21964ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21974ed131d0SYishai Hadas 			return -EINVAL;
21984ed131d0SYishai Hadas 		}
21994ed131d0SYishai Hadas 
22004ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
22014ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
22024ed131d0SYishai Hadas 		 * page holds its count.
22034ed131d0SYishai Hadas 		 */
22044ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
22054ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
22064ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
22074ed131d0SYishai Hadas 			return -EINVAL;
22084ed131d0SYishai Hadas 		}
22094ed131d0SYishai Hadas 
22104ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
22114ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
22124ed131d0SYishai Hadas 
2213d2c8a155SMeir Lichtinger 		err = mlx5_cmd_uar_alloc(dev->mdev, &uar_index,
2214d2c8a155SMeir Lichtinger 					 context->devx_uid);
22154ed131d0SYishai Hadas 		if (err) {
22164ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
22174ed131d0SYishai Hadas 			goto free_bfreg;
22184ed131d0SYishai Hadas 		}
22194ed131d0SYishai Hadas 	} else {
22204ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
22214ed131d0SYishai Hadas 	}
22224ed131d0SYishai Hadas 
22234ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
222437aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2225e126ba97SEli Cohen 
2226e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2227c043ff2cSMichal Kalderon 				prot, NULL);
222837aa5c36SGuy Levi 	if (err) {
22298f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2230e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
22318f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
22324ed131d0SYishai Hadas 		goto err;
223337aa5c36SGuy Levi 	}
2234e126ba97SEli Cohen 
22354ed131d0SYishai Hadas 	if (dyn_uar)
22364ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
22374ed131d0SYishai Hadas 	return 0;
22384ed131d0SYishai Hadas 
22394ed131d0SYishai Hadas err:
22404ed131d0SYishai Hadas 	if (!dyn_uar)
22414ed131d0SYishai Hadas 		return err;
22424ed131d0SYishai Hadas 
2243d2c8a155SMeir Lichtinger 	mlx5_cmd_uar_dealloc(dev->mdev, idx, context->devx_uid);
22444ed131d0SYishai Hadas 
22454ed131d0SYishai Hadas free_bfreg:
22464ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
22474ed131d0SYishai Hadas 
22484ed131d0SYishai Hadas 	return err;
224937aa5c36SGuy Levi }
225037aa5c36SGuy Levi 
mlx5_vma_to_pgoff(struct vm_area_struct * vma)2251dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma)
2252dc2316ebSYishai Hadas {
2253dc2316ebSYishai Hadas 	unsigned long idx;
2254dc2316ebSYishai Hadas 	u8 command;
2255dc2316ebSYishai Hadas 
2256dc2316ebSYishai Hadas 	command = get_command(vma->vm_pgoff);
2257dc2316ebSYishai Hadas 	idx = get_extended_index(vma->vm_pgoff);
2258dc2316ebSYishai Hadas 
2259dc2316ebSYishai Hadas 	return (command << 16 | idx);
2260dc2316ebSYishai Hadas }
2261dc2316ebSYishai Hadas 
mlx5_ib_mmap_offset(struct mlx5_ib_dev * dev,struct vm_area_struct * vma,struct ib_ucontext * ucontext)2262dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev,
2263dc2316ebSYishai Hadas 			       struct vm_area_struct *vma,
2264dc2316ebSYishai Hadas 			       struct ib_ucontext *ucontext)
2265dc2316ebSYishai Hadas {
2266dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry;
2267dc2316ebSYishai Hadas 	struct rdma_user_mmap_entry *entry;
2268dc2316ebSYishai Hadas 	unsigned long pgoff;
2269dc2316ebSYishai Hadas 	pgprot_t prot;
2270dc2316ebSYishai Hadas 	phys_addr_t pfn;
2271dc2316ebSYishai Hadas 	int ret;
2272dc2316ebSYishai Hadas 
2273dc2316ebSYishai Hadas 	pgoff = mlx5_vma_to_pgoff(vma);
2274dc2316ebSYishai Hadas 	entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff);
2275dc2316ebSYishai Hadas 	if (!entry)
227624da0016SAriel Levkovich 		return -EINVAL;
227724da0016SAriel Levkovich 
2278dc2316ebSYishai Hadas 	mentry = to_mmmap(entry);
2279dc2316ebSYishai Hadas 	pfn = (mentry->address >> PAGE_SHIFT);
2280342ee59dSYishai Hadas 	if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR ||
2281342ee59dSYishai Hadas 	    mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC)
22823f59b6c3SYishai Hadas 		prot = pgprot_noncached(vma->vm_page_prot);
22833f59b6c3SYishai Hadas 	else
2284dc2316ebSYishai Hadas 		prot = pgprot_writecombine(vma->vm_page_prot);
2285dc2316ebSYishai Hadas 	ret = rdma_user_mmap_io(ucontext, vma, pfn,
2286dc2316ebSYishai Hadas 				entry->npages * PAGE_SIZE,
2287dc2316ebSYishai Hadas 				prot,
2288dc2316ebSYishai Hadas 				entry);
2289dc2316ebSYishai Hadas 	rdma_user_mmap_entry_put(&mentry->rdma_entry);
2290dc2316ebSYishai Hadas 	return ret;
229124da0016SAriel Levkovich }
229224da0016SAriel Levkovich 
mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry * entry)22937be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry)
22947be76befSYishai Hadas {
22959b6d3bbcSLeon Romanovsky 	u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF;
22969b6d3bbcSLeon Romanovsky 	u64 index = entry->rdma_entry.start_pgoff & 0xFFFF;
22977be76befSYishai Hadas 
22987be76befSYishai Hadas 	return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) |
22997be76befSYishai Hadas 		(index & 0xFF)) << PAGE_SHIFT;
23007be76befSYishai Hadas }
23017be76befSYishai Hadas 
mlx5_ib_mmap(struct ib_ucontext * ibcontext,struct vm_area_struct * vma)230237aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
230337aa5c36SGuy Levi {
230437aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
230537aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
230637aa5c36SGuy Levi 	unsigned long command;
230737aa5c36SGuy Levi 	phys_addr_t pfn;
230837aa5c36SGuy Levi 
230937aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
231037aa5c36SGuy Levi 	switch (command) {
231137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
23121f3db161SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
23131f3db161SYishai Hadas 		if (!dev->wc_support)
23141f3db161SYishai Hadas 			return -EPERM;
23151f3db161SYishai Hadas 		fallthrough;
231637aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
231737aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
23187c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2319e126ba97SEli Cohen 
2320e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2321e126ba97SEli Cohen 		return -ENOSYS;
2322e126ba97SEli Cohen 
2323d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2324d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2325d69e3bcfSMatan Barak 			return -EINVAL;
2326d69e3bcfSMatan Barak 
23276cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2328d69e3bcfSMatan Barak 			return -EPERM;
23291c71222eSSuren Baghdasaryan 		vm_flags_clear(vma, VM_MAYWRITE);
2330d69e3bcfSMatan Barak 
2331d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2332d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2333d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2334d69e3bcfSMatan Barak 
2335d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2336d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2337d69e3bcfSMatan Barak 			PAGE_SHIFT;
2338d5e560d3SJason Gunthorpe 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2339d5e560d3SJason Gunthorpe 					 PAGE_SIZE,
2340c043ff2cSMichal Kalderon 					 pgprot_noncached(vma->vm_page_prot),
2341c043ff2cSMichal Kalderon 					 NULL);
23425c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
23435c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2344d69e3bcfSMatan Barak 
2345e126ba97SEli Cohen 	default:
2346dc2316ebSYishai Hadas 		return mlx5_ib_mmap_offset(dev, vma, ibcontext);
2347e126ba97SEli Cohen 	}
2348e126ba97SEli Cohen 
2349e126ba97SEli Cohen 	return 0;
2350e126ba97SEli Cohen }
2351e126ba97SEli Cohen 
mlx5_ib_alloc_pd(struct ib_pd * ibpd,struct ib_udata * udata)2352ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2353e126ba97SEli Cohen {
235421a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
235521a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2356e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2357e126ba97SEli Cohen 	int err;
2358a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2359a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2360a1069c1cSYishai Hadas 	u16 uid = 0;
2361ff23dfa1SShamir Rabinovitch 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2362ff23dfa1SShamir Rabinovitch 		udata, struct mlx5_ib_ucontext, ibucontext);
2363e126ba97SEli Cohen 
2364ff23dfa1SShamir Rabinovitch 	uid = context ? context->devx_uid : 0;
2365a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2366a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
236731578defSLeon Romanovsky 	err = mlx5_cmd_exec_inout(to_mdev(ibdev)->mdev, alloc_pd, in, out);
236821a428a0SLeon Romanovsky 	if (err)
236921a428a0SLeon Romanovsky 		return err;
2370e126ba97SEli Cohen 
2371a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2372a1069c1cSYishai Hadas 	pd->uid = uid;
2373ff23dfa1SShamir Rabinovitch 	if (udata) {
2374e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2375e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2376a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
237721a428a0SLeon Romanovsky 			return -EFAULT;
2378e126ba97SEli Cohen 		}
2379e126ba97SEli Cohen 	}
2380e126ba97SEli Cohen 
238121a428a0SLeon Romanovsky 	return 0;
2382e126ba97SEli Cohen }
2383e126ba97SEli Cohen 
mlx5_ib_dealloc_pd(struct ib_pd * pd,struct ib_udata * udata)238491a7c58fSLeon Romanovsky static int mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2385e126ba97SEli Cohen {
2386e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2387e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2388e126ba97SEli Cohen 
238991a7c58fSLeon Romanovsky 	return mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2390e126ba97SEli Cohen }
2391e126ba97SEli Cohen 
mlx5_ib_mcg_attach(struct ib_qp * ibqp,union ib_gid * gid,u16 lid)2392e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2393e126ba97SEli Cohen {
2394e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
239581e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
2396e126ba97SEli Cohen 	int err;
2397539ec982SYishai Hadas 	u16 uid;
2398539ec982SYishai Hadas 
2399539ec982SYishai Hadas 	uid = ibqp->pd ?
2400539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
2401e126ba97SEli Cohen 
24022be08c30SLeon Romanovsky 	if (mqp->flags & IB_QP_CREATE_SOURCE_QPN) {
240381e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
240481e30880SYishai Hadas 		return -EOPNOTSUPP;
240581e30880SYishai Hadas 	}
240681e30880SYishai Hadas 
2407539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2408e126ba97SEli Cohen 	if (err)
2409e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2410e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2411e126ba97SEli Cohen 
2412e126ba97SEli Cohen 	return err;
2413e126ba97SEli Cohen }
2414e126ba97SEli Cohen 
mlx5_ib_mcg_detach(struct ib_qp * ibqp,union ib_gid * gid,u16 lid)2415e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2416e126ba97SEli Cohen {
2417e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2418e126ba97SEli Cohen 	int err;
2419539ec982SYishai Hadas 	u16 uid;
2420e126ba97SEli Cohen 
2421539ec982SYishai Hadas 	uid = ibqp->pd ?
2422539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
2423539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2424e126ba97SEli Cohen 	if (err)
2425e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2426e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2427e126ba97SEli Cohen 
2428e126ba97SEli Cohen 	return err;
2429e126ba97SEli Cohen }
2430e126ba97SEli Cohen 
init_node_data(struct mlx5_ib_dev * dev)2431e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2432e126ba97SEli Cohen {
24331b5daf11SMajd Dibbiny 	int err;
2434e126ba97SEli Cohen 
24351b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2436e126ba97SEli Cohen 	if (err)
2437e126ba97SEli Cohen 		return err;
24381b5daf11SMajd Dibbiny 
24391b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
24401b5daf11SMajd Dibbiny 
24411b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2442e126ba97SEli Cohen }
2443e126ba97SEli Cohen 
fw_pages_show(struct device * device,struct device_attribute * attr,char * buf)2444508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
2445508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2446e126ba97SEli Cohen {
2447e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
244854747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2449e126ba97SEli Cohen 
24501c7fd726SJoe Perches 	return sysfs_emit(buf, "%d\n", dev->mdev->priv.fw_pages);
2451e126ba97SEli Cohen }
2452508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
2453e126ba97SEli Cohen 
reg_pages_show(struct device * device,struct device_attribute * attr,char * buf)2454508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
2455e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2456e126ba97SEli Cohen {
2457e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
245854747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2459e126ba97SEli Cohen 
24601c7fd726SJoe Perches 	return sysfs_emit(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2461e126ba97SEli Cohen }
2462508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
2463e126ba97SEli Cohen 
hca_type_show(struct device * device,struct device_attribute * attr,char * buf)2464508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
2465508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2466e126ba97SEli Cohen {
2467e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
246854747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
246954747231SParav Pandit 
24701c7fd726SJoe Perches 	return sysfs_emit(buf, "MT%d\n", dev->mdev->pdev->device);
2471e126ba97SEli Cohen }
2472508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
2473e126ba97SEli Cohen 
hw_rev_show(struct device * device,struct device_attribute * attr,char * buf)2474508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
2475508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
2476e126ba97SEli Cohen {
2477e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
247854747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
247954747231SParav Pandit 
24801c7fd726SJoe Perches 	return sysfs_emit(buf, "%x\n", dev->mdev->rev_id);
2481e126ba97SEli Cohen }
2482508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
2483e126ba97SEli Cohen 
board_id_show(struct device * device,struct device_attribute * attr,char * buf)2484508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
2485508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2486e126ba97SEli Cohen {
2487e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
248854747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
248954747231SParav Pandit 
24901c7fd726SJoe Perches 	return sysfs_emit(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
24919603b61dSJack Morgenstein 			  dev->mdev->board_id);
2492e126ba97SEli Cohen }
2493508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
2494e126ba97SEli Cohen 
2495508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
2496508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
2497508a523fSParav Pandit 	&dev_attr_hca_type.attr,
2498508a523fSParav Pandit 	&dev_attr_board_id.attr,
2499508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
2500508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
2501508a523fSParav Pandit 	NULL,
2502508a523fSParav Pandit };
2503e126ba97SEli Cohen 
2504508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
2505508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
2506e126ba97SEli Cohen };
2507e126ba97SEli Cohen 
pkey_change_handler(struct work_struct * work)25087722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
25097722f47eSHaggai Eran {
25107722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
25117722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
25127722f47eSHaggai Eran 			     pkey_change_work);
25137722f47eSHaggai Eran 
25148c9e7f03SLeon Romanovsky 	if (!ports->gsi)
25158c9e7f03SLeon Romanovsky 		/*
25168c9e7f03SLeon Romanovsky 		 * We got this event before device was fully configured
25178c9e7f03SLeon Romanovsky 		 * and MAD registration code wasn't called/finished yet.
25188c9e7f03SLeon Romanovsky 		 */
25198c9e7f03SLeon Romanovsky 		return;
25208c9e7f03SLeon Romanovsky 
25217722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
25227722f47eSHaggai Eran }
25237722f47eSHaggai Eran 
mlx5_ib_handle_internal_error(struct mlx5_ib_dev * ibdev)252489ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
252589ea94a7SMaor Gottlieb {
252689ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
252789ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
252889ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
252989ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
253089ea94a7SMaor Gottlieb 	unsigned long flags_qp;
253189ea94a7SMaor Gottlieb 	unsigned long flags_cq;
253289ea94a7SMaor Gottlieb 	unsigned long flags;
253389ea94a7SMaor Gottlieb 
253489ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
253589ea94a7SMaor Gottlieb 
253689ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
253789ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
253889ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
253989ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
254089ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
254189ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
254289ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
254389ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
254489ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
254589ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
254689ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
254789ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
254889ea94a7SMaor Gottlieb 						      &cq_armed_list);
254989ea94a7SMaor Gottlieb 				}
255089ea94a7SMaor Gottlieb 			}
255189ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
255289ea94a7SMaor Gottlieb 		}
255389ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
255489ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
255589ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
255689ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
255789ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
255889ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
255989ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
256089ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
256189ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
256289ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
256389ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
256489ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
256589ea94a7SMaor Gottlieb 							      &cq_armed_list);
256689ea94a7SMaor Gottlieb 					}
256789ea94a7SMaor Gottlieb 				}
256889ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
256989ea94a7SMaor Gottlieb 						       flags_cq);
257089ea94a7SMaor Gottlieb 			}
257189ea94a7SMaor Gottlieb 		}
257289ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
257389ea94a7SMaor Gottlieb 	}
257489ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
257589ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
257689ea94a7SMaor Gottlieb 	 */
257789ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
25784e0e2ea1SYishai Hadas 		mcq->comp(mcq, NULL);
257989ea94a7SMaor Gottlieb 	}
258089ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
258189ea94a7SMaor Gottlieb }
258289ea94a7SMaor Gottlieb 
delay_drop_handler(struct work_struct * work)258303404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
258403404e8aSMaor Gottlieb {
258503404e8aSMaor Gottlieb 	int err;
258603404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
258703404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
258803404e8aSMaor Gottlieb 			     delay_drop_work);
258903404e8aSMaor Gottlieb 
2590fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
2591fe248c3aSMaor Gottlieb 
259203404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
2593333fbaa0SLeon Romanovsky 	err = mlx5_core_set_delay_drop(delay_drop->dev, delay_drop->timeout);
259403404e8aSMaor Gottlieb 	if (err) {
259503404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
259603404e8aSMaor Gottlieb 			     delay_drop->timeout);
259703404e8aSMaor Gottlieb 		delay_drop->activate = false;
259803404e8aSMaor Gottlieb 	}
259903404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
260003404e8aSMaor Gottlieb }
260103404e8aSMaor Gottlieb 
handle_general_event(struct mlx5_ib_dev * ibdev,struct mlx5_eqe * eqe,struct ib_event * ibev)260209e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
260309e574faSSaeed Mahameed 				 struct ib_event *ibev)
260409e574faSSaeed Mahameed {
26051fb7f897SMark Bloch 	u32 port = (eqe->data.port.port >> 4) & 0xf;
26066cfdc7e4SAya Levin 
260709e574faSSaeed Mahameed 	switch (eqe->sub_type) {
260809e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
26096cfdc7e4SAya Levin 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
26106cfdc7e4SAya Levin 					    IB_LINK_LAYER_ETHERNET)
261109e574faSSaeed Mahameed 			schedule_work(&ibdev->delay_drop.delay_drop_work);
261209e574faSSaeed Mahameed 		break;
261309e574faSSaeed Mahameed 	default: /* do nothing */
261409e574faSSaeed Mahameed 		return;
261509e574faSSaeed Mahameed 	}
261609e574faSSaeed Mahameed }
261709e574faSSaeed Mahameed 
handle_port_change(struct mlx5_ib_dev * ibdev,struct mlx5_eqe * eqe,struct ib_event * ibev)2618134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
2619134e9349SSaeed Mahameed 			      struct ib_event *ibev)
2620134e9349SSaeed Mahameed {
26211fb7f897SMark Bloch 	u32 port = (eqe->data.port.port >> 4) & 0xf;
2622134e9349SSaeed Mahameed 
2623134e9349SSaeed Mahameed 	ibev->element.port_num = port;
2624134e9349SSaeed Mahameed 
2625134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
2626134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
2627134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
2628134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
2629134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
2630134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
2631134e9349SSaeed Mahameed 		 */
2632134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2633134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
2634134e9349SSaeed Mahameed 			return -EINVAL;
2635134e9349SSaeed Mahameed 
2636134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
2637134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2638134e9349SSaeed Mahameed 		break;
2639134e9349SSaeed Mahameed 
2640134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
2641134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
2642134e9349SSaeed Mahameed 		break;
2643134e9349SSaeed Mahameed 
2644134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
2645134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
2646134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2647134e9349SSaeed Mahameed 		break;
2648134e9349SSaeed Mahameed 
2649134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
2650134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
2651134e9349SSaeed Mahameed 		break;
2652134e9349SSaeed Mahameed 
2653134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
2654134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
2655134e9349SSaeed Mahameed 		break;
2656134e9349SSaeed Mahameed 	default:
2657134e9349SSaeed Mahameed 		return -EINVAL;
2658134e9349SSaeed Mahameed 	}
2659134e9349SSaeed Mahameed 
2660134e9349SSaeed Mahameed 	return 0;
2661134e9349SSaeed Mahameed }
2662134e9349SSaeed Mahameed 
mlx5_ib_handle_event(struct work_struct * _work)2663d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
2664e126ba97SEli Cohen {
2665d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
2666d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
2667d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
2668e126ba97SEli Cohen 	struct ib_event ibev;
2669dbaaff2aSEli Cohen 	bool fatal = false;
2670e126ba97SEli Cohen 
2671df097a27SSaeed Mahameed 	if (work->is_slave) {
2672df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
2673d69a24e0SDaniel Jurgens 		if (!ibdev)
2674d69a24e0SDaniel Jurgens 			goto out;
2675d69a24e0SDaniel Jurgens 	} else {
2676df097a27SSaeed Mahameed 		ibdev = work->dev;
2677d69a24e0SDaniel Jurgens 	}
2678d69a24e0SDaniel Jurgens 
2679d69a24e0SDaniel Jurgens 	switch (work->event) {
2680e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2681e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
268289ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2683134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
2684dbaaff2aSEli Cohen 		fatal = true;
2685e126ba97SEli Cohen 		break;
2686134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
2687134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
2688d69a24e0SDaniel Jurgens 			goto out;
2689e126ba97SEli Cohen 		break;
269009e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
269109e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
2692df561f66SGustavo A. R. Silva 		fallthrough;
2693bdc37924SSaeed Mahameed 	default:
269403404e8aSMaor Gottlieb 		goto out;
2695e126ba97SEli Cohen 	}
2696e126ba97SEli Cohen 
2697e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
2698e126ba97SEli Cohen 
2699134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
2700134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
270103404e8aSMaor Gottlieb 		goto out;
2702a0c84c32SEli Cohen 	}
2703a0c84c32SEli Cohen 
2704e126ba97SEli Cohen 	if (ibdev->ib_active)
2705e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2706dbaaff2aSEli Cohen 
2707dbaaff2aSEli Cohen 	if (fatal)
2708dbaaff2aSEli Cohen 		ibdev->ib_active = false;
270903404e8aSMaor Gottlieb out:
2710d69a24e0SDaniel Jurgens 	kfree(work);
2711d69a24e0SDaniel Jurgens }
2712d69a24e0SDaniel Jurgens 
mlx5_ib_event(struct notifier_block * nb,unsigned long event,void * param)2713df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
2714df097a27SSaeed Mahameed 			 unsigned long event, void *param)
2715d69a24e0SDaniel Jurgens {
2716d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
2717d69a24e0SDaniel Jurgens 
2718d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
271910bea9c8SLeon Romanovsky 	if (!work)
2720df097a27SSaeed Mahameed 		return NOTIFY_DONE;
272110bea9c8SLeon Romanovsky 
2722d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
2723df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
2724df097a27SSaeed Mahameed 	work->is_slave = false;
2725d69a24e0SDaniel Jurgens 	work->param = param;
2726d69a24e0SDaniel Jurgens 	work->event = event;
2727d69a24e0SDaniel Jurgens 
2728d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
2729df097a27SSaeed Mahameed 
2730df097a27SSaeed Mahameed 	return NOTIFY_OK;
2731df097a27SSaeed Mahameed }
2732df097a27SSaeed Mahameed 
mlx5_ib_event_slave_port(struct notifier_block * nb,unsigned long event,void * param)2733df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
2734df097a27SSaeed Mahameed 				    unsigned long event, void *param)
2735df097a27SSaeed Mahameed {
2736df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
2737df097a27SSaeed Mahameed 
2738df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
2739df097a27SSaeed Mahameed 	if (!work)
2740df097a27SSaeed Mahameed 		return NOTIFY_DONE;
2741df097a27SSaeed Mahameed 
2742df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
2743df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
2744df097a27SSaeed Mahameed 	work->is_slave = true;
2745df097a27SSaeed Mahameed 	work->param = param;
2746df097a27SSaeed Mahameed 	work->event = event;
2747df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
2748df097a27SSaeed Mahameed 
2749df097a27SSaeed Mahameed 	return NOTIFY_OK;
2750d69a24e0SDaniel Jurgens }
2751d69a24e0SDaniel Jurgens 
set_has_smi_cap(struct mlx5_ib_dev * dev)2752c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2753c43f1112SMaor Gottlieb {
2754c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
2755c43f1112SMaor Gottlieb 	int err;
2756c43f1112SMaor Gottlieb 	int port;
2757c43f1112SMaor Gottlieb 
27584b83c3caSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_IB)
27594b83c3caSMark Bloch 		return 0;
27604b83c3caSMark Bloch 
27614b83c3caSMark Bloch 	for (port = 1; port <= dev->num_ports; port++) {
27624b83c3caSMark Bloch 		if (!MLX5_CAP_GEN(dev->mdev, ib_virt)) {
27634b83c3caSMark Bloch 			dev->port_caps[port - 1].has_smi = true;
27644b83c3caSMark Bloch 			continue;
27654b83c3caSMark Bloch 		}
27664b83c3caSMark Bloch 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port, 0,
2767c43f1112SMaor Gottlieb 						   &vport_ctx);
2768c43f1112SMaor Gottlieb 		if (err) {
2769c43f1112SMaor Gottlieb 			mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2770c43f1112SMaor Gottlieb 				    port, err);
2771c43f1112SMaor Gottlieb 			return err;
2772c43f1112SMaor Gottlieb 		}
27734b83c3caSMark Bloch 		dev->port_caps[port - 1].has_smi = vport_ctx.has_smi;
2774c43f1112SMaor Gottlieb 	}
27754b83c3caSMark Bloch 
2776c43f1112SMaor Gottlieb 	return 0;
2777c43f1112SMaor Gottlieb }
2778c43f1112SMaor Gottlieb 
get_ext_port_caps(struct mlx5_ib_dev * dev)2779e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2780e126ba97SEli Cohen {
278113179652SParav Pandit 	unsigned int port;
2782e126ba97SEli Cohen 
278313179652SParav Pandit 	rdma_for_each_port (&dev->ib_dev, port)
2784e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
2785e126ba97SEli Cohen }
2786e126ba97SEli Cohen 
mlx5_get_umr_fence(u8 umr_fence_cap)27876e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
27886e8484c5SMax Gurtovoy {
27896e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
27906e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
27916e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
27926e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
27936e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
27946e8484c5SMax Gurtovoy 	default:
27956e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
27966e8484c5SMax Gurtovoy 	}
27976e8484c5SMax Gurtovoy }
27986e8484c5SMax Gurtovoy 
mlx5_ib_dev_res_cq_init(struct mlx5_ib_dev * dev)2799b6334d23SJianbo Liu int mlx5_ib_dev_res_cq_init(struct mlx5_ib_dev *dev)
2800b6334d23SJianbo Liu {
2801b6334d23SJianbo Liu 	struct mlx5_ib_resources *devr = &dev->devr;
2802b6334d23SJianbo Liu 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
2803b6334d23SJianbo Liu 	struct ib_device *ibdev;
2804b6334d23SJianbo Liu 	struct ib_pd *pd;
2805b6334d23SJianbo Liu 	struct ib_cq *cq;
2806b6334d23SJianbo Liu 	int ret = 0;
2807b6334d23SJianbo Liu 
2808b6334d23SJianbo Liu 
2809b6334d23SJianbo Liu 	/*
2810b6334d23SJianbo Liu 	 * devr->c0 is set once, never changed until device unload.
2811b6334d23SJianbo Liu 	 * Avoid taking the mutex if initialization is already done.
2812b6334d23SJianbo Liu 	 */
2813b6334d23SJianbo Liu 	if (devr->c0)
2814b6334d23SJianbo Liu 		return 0;
2815b6334d23SJianbo Liu 
2816b6334d23SJianbo Liu 	mutex_lock(&devr->cq_lock);
2817b6334d23SJianbo Liu 	if (devr->c0)
2818b6334d23SJianbo Liu 		goto unlock;
2819b6334d23SJianbo Liu 
2820b6334d23SJianbo Liu 	ibdev = &dev->ib_dev;
2821b6334d23SJianbo Liu 	pd = ib_alloc_pd(ibdev, 0);
2822b6334d23SJianbo Liu 	if (IS_ERR(pd)) {
2823b6334d23SJianbo Liu 		ret = PTR_ERR(pd);
2824b6334d23SJianbo Liu 		mlx5_ib_err(dev, "Couldn't allocate PD for res init, err=%d\n", ret);
2825b6334d23SJianbo Liu 		goto unlock;
2826b6334d23SJianbo Liu 	}
2827b6334d23SJianbo Liu 
2828b6334d23SJianbo Liu 	cq = ib_create_cq(ibdev, NULL, NULL, NULL, &cq_attr);
2829b6334d23SJianbo Liu 	if (IS_ERR(cq)) {
2830b6334d23SJianbo Liu 		ret = PTR_ERR(cq);
2831b6334d23SJianbo Liu 		mlx5_ib_err(dev, "Couldn't create CQ for res init, err=%d\n", ret);
2832b6334d23SJianbo Liu 		ib_dealloc_pd(pd);
2833b6334d23SJianbo Liu 		goto unlock;
2834b6334d23SJianbo Liu 	}
2835b6334d23SJianbo Liu 
2836b6334d23SJianbo Liu 	devr->p0 = pd;
2837b6334d23SJianbo Liu 	devr->c0 = cq;
2838b6334d23SJianbo Liu 
2839b6334d23SJianbo Liu unlock:
2840b6334d23SJianbo Liu 	mutex_unlock(&devr->cq_lock);
2841b6334d23SJianbo Liu 	return ret;
2842b6334d23SJianbo Liu }
2843b6334d23SJianbo Liu 
mlx5_ib_dev_res_srq_init(struct mlx5_ib_dev * dev)2844b6334d23SJianbo Liu int mlx5_ib_dev_res_srq_init(struct mlx5_ib_dev *dev)
2845e126ba97SEli Cohen {
28461e2b5a90SLeon Romanovsky 	struct mlx5_ib_resources *devr = &dev->devr;
2847e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
2848b6334d23SJianbo Liu 	struct ib_srq *s0, *s1;
2849e126ba97SEli Cohen 	int ret = 0;
2850e126ba97SEli Cohen 
2851b6334d23SJianbo Liu 	/*
2852b6334d23SJianbo Liu 	 * devr->s1 is set once, never changed until device unload.
2853b6334d23SJianbo Liu 	 * Avoid taking the mutex if initialization is already done.
2854b6334d23SJianbo Liu 	 */
2855b6334d23SJianbo Liu 	if (devr->s1)
2856b6334d23SJianbo Liu 		return 0;
2857e126ba97SEli Cohen 
2858b6334d23SJianbo Liu 	mutex_lock(&devr->srq_lock);
2859b6334d23SJianbo Liu 	if (devr->s1)
2860b6334d23SJianbo Liu 		goto unlock;
2861f4375443SLeon Romanovsky 
2862b6334d23SJianbo Liu 	ret = mlx5_ib_dev_res_cq_init(dev);
2863f4375443SLeon Romanovsky 	if (ret)
2864b6334d23SJianbo Liu 		goto unlock;
2865e126ba97SEli Cohen 
2866e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
2867e126ba97SEli Cohen 	attr.attr.max_sge = 1;
2868e126ba97SEli Cohen 	attr.attr.max_wr = 1;
2869e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
28701a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
2871e126ba97SEli Cohen 
2872b6334d23SJianbo Liu 	s0 = ib_create_srq(devr->p0, &attr);
2873b6334d23SJianbo Liu 	if (IS_ERR(s0)) {
2874b6334d23SJianbo Liu 		ret = PTR_ERR(s0);
2875b6334d23SJianbo Liu 		mlx5_ib_err(dev, "Couldn't create SRQ 0 for res init, err=%d\n", ret);
2876b6334d23SJianbo Liu 		goto unlock;
287720da44dfSLeon Romanovsky 	}
2878e126ba97SEli Cohen 
28794aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
28804aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
28814aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
28824aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
288368e326deSLeon Romanovsky 
2884b6334d23SJianbo Liu 	s1 = ib_create_srq(devr->p0, &attr);
2885b6334d23SJianbo Liu 	if (IS_ERR(s1)) {
2886b6334d23SJianbo Liu 		ret = PTR_ERR(s1);
2887b6334d23SJianbo Liu 		mlx5_ib_err(dev, "Couldn't create SRQ 1 for res init, err=%d\n", ret);
2888b6334d23SJianbo Liu 		ib_destroy_srq(s0);
2889b6334d23SJianbo Liu 	}
2890b6334d23SJianbo Liu 
2891b6334d23SJianbo Liu 	devr->s0 = s0;
2892b6334d23SJianbo Liu 	devr->s1 = s1;
2893b6334d23SJianbo Liu 
2894b6334d23SJianbo Liu unlock:
2895b6334d23SJianbo Liu 	mutex_unlock(&devr->srq_lock);
2896b6334d23SJianbo Liu 	return ret;
2897b6334d23SJianbo Liu }
2898b6334d23SJianbo Liu 
mlx5_ib_dev_res_init(struct mlx5_ib_dev * dev)2899b6334d23SJianbo Liu static int mlx5_ib_dev_res_init(struct mlx5_ib_dev *dev)
2900b6334d23SJianbo Liu {
2901b6334d23SJianbo Liu 	struct mlx5_ib_resources *devr = &dev->devr;
2902b6334d23SJianbo Liu 	int ret;
2903b6334d23SJianbo Liu 
2904b6334d23SJianbo Liu 	if (!MLX5_CAP_GEN(dev->mdev, xrc))
2905b6334d23SJianbo Liu 		return -EOPNOTSUPP;
2906b6334d23SJianbo Liu 
2907b6334d23SJianbo Liu 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn0, 0);
2908b6334d23SJianbo Liu 	if (ret)
2909b6334d23SJianbo Liu 		return ret;
2910b6334d23SJianbo Liu 
2911b6334d23SJianbo Liu 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn1, 0);
2912b6334d23SJianbo Liu 	if (ret) {
2913b6334d23SJianbo Liu 		mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
2914b6334d23SJianbo Liu 		return ret;
291520da44dfSLeon Romanovsky 	}
29164aa17b28SHaggai Abramonvsky 
2917b6334d23SJianbo Liu 	mutex_init(&devr->cq_lock);
2918b6334d23SJianbo Liu 	mutex_init(&devr->srq_lock);
2919e126ba97SEli Cohen 
2920b6334d23SJianbo Liu 	return 0;
2921e126ba97SEli Cohen }
2922e126ba97SEli Cohen 
mlx5_ib_dev_res_cleanup(struct mlx5_ib_dev * dev)29231e2b5a90SLeon Romanovsky static void mlx5_ib_dev_res_cleanup(struct mlx5_ib_dev *dev)
2924e126ba97SEli Cohen {
29251e2b5a90SLeon Romanovsky 	struct mlx5_ib_resources *devr = &dev->devr;
2926b0791dbfSLeon Romanovsky 
2927b6334d23SJianbo Liu 	/* After s0/s1 init, they are not unset during the device lifetime. */
2928b6334d23SJianbo Liu 	if (devr->s1) {
292920da44dfSLeon Romanovsky 		ib_destroy_srq(devr->s1);
293020da44dfSLeon Romanovsky 		ib_destroy_srq(devr->s0);
2931b6334d23SJianbo Liu 	}
2932f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
2933f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
2934b6334d23SJianbo Liu 	/* After p0/c0 init, they are not unset during the device lifetime. */
2935b6334d23SJianbo Liu 	if (devr->c0) {
293620da44dfSLeon Romanovsky 		ib_destroy_cq(devr->c0);
293720da44dfSLeon Romanovsky 		ib_dealloc_pd(devr->p0);
2938e126ba97SEli Cohen 	}
2939b6334d23SJianbo Liu 	mutex_destroy(&devr->cq_lock);
2940b6334d23SJianbo Liu 	mutex_destroy(&devr->srq_lock);
2941b6334d23SJianbo Liu }
2942e126ba97SEli Cohen 
get_core_cap_flags(struct ib_device * ibdev,struct mlx5_hca_vport_context * rep)2943b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
2944b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
2945e53505a8SAchiad Shochat {
2946e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2947e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
2948e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
2949e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
295085c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
2951e53505a8SAchiad Shochat 	u32 ret = 0;
2952e53505a8SAchiad Shochat 
2953b02289b3SArtemy Kovalyov 	if (rep->grh_required)
2954b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
2955b02289b3SArtemy Kovalyov 
2956e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
2957b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
2958e53505a8SAchiad Shochat 
295985c7c014SDaniel Jurgens 	if (raw_support)
2960b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
296172cd5717SOr Gerlitz 
2962e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
296372cd5717SOr Gerlitz 		return ret;
2964e53505a8SAchiad Shochat 
2965e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
296672cd5717SOr Gerlitz 		return ret;
2967e53505a8SAchiad Shochat 
2968e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
2969e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
2970e53505a8SAchiad Shochat 
2971e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
2972e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
2973e53505a8SAchiad Shochat 
2974e53505a8SAchiad Shochat 	return ret;
2975e53505a8SAchiad Shochat }
2976e53505a8SAchiad Shochat 
mlx5_port_immutable(struct ib_device * ibdev,u32 port_num,struct ib_port_immutable * immutable)29771fb7f897SMark Bloch static int mlx5_port_immutable(struct ib_device *ibdev, u32 port_num,
29787738613eSIra Weiny 			       struct ib_port_immutable *immutable)
29797738613eSIra Weiny {
29807738613eSIra Weiny 	struct ib_port_attr attr;
2981ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2982ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
2983b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
29847738613eSIra Weiny 	int err;
29857738613eSIra Weiny 
2986c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
29877738613eSIra Weiny 	if (err)
29887738613eSIra Weiny 		return err;
29897738613eSIra Weiny 
2990b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
2991b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
2992b02289b3SArtemy Kovalyov 						   &rep);
2993b02289b3SArtemy Kovalyov 		if (err)
2994b02289b3SArtemy Kovalyov 			return err;
2995b02289b3SArtemy Kovalyov 	}
2996b02289b3SArtemy Kovalyov 
29977738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
29987738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
2999b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
3000337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
30017738613eSIra Weiny 
30027738613eSIra Weiny 	return 0;
30037738613eSIra Weiny }
30047738613eSIra Weiny 
mlx5_port_rep_immutable(struct ib_device * ibdev,u32 port_num,struct ib_port_immutable * immutable)30051fb7f897SMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u32 port_num,
30068e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
30078e6efa3aSMark Bloch {
30088e6efa3aSMark Bloch 	struct ib_port_attr attr;
30098e6efa3aSMark Bloch 	int err;
30108e6efa3aSMark Bloch 
30118e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
30128e6efa3aSMark Bloch 
30138e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
30148e6efa3aSMark Bloch 	if (err)
30158e6efa3aSMark Bloch 		return err;
30168e6efa3aSMark Bloch 
30178e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
30188e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
30198e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
30208e6efa3aSMark Bloch 
30218e6efa3aSMark Bloch 	return 0;
30228e6efa3aSMark Bloch }
30238e6efa3aSMark Bloch 
get_dev_fw_str(struct ib_device * ibdev,char * str)30249abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
3025c7342823SIra Weiny {
3026c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
3027c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
30289abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
30299abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
30309abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
3031c7342823SIra Weiny }
3032c7342823SIra Weiny 
mlx5_eth_lag_init(struct mlx5_ib_dev * dev)303345f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
30349ef9c640SAviv Heller {
30359ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
30369ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
30379ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
30389ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
30399ef9c640SAviv Heller 	int err;
30409ef9c640SAviv Heller 
3041c446d9daSMark Bloch 	if (!ns || !mlx5_lag_is_active(mdev))
30429ef9c640SAviv Heller 		return 0;
30439ef9c640SAviv Heller 
30449ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
30459ef9c640SAviv Heller 	if (err)
30469ef9c640SAviv Heller 		return err;
30479ef9c640SAviv Heller 
30489ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
30499ef9c640SAviv Heller 	if (IS_ERR(ft)) {
30509ef9c640SAviv Heller 		err = PTR_ERR(ft);
30519ef9c640SAviv Heller 		goto err_destroy_vport_lag;
30529ef9c640SAviv Heller 	}
30539ef9c640SAviv Heller 
30549a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
305534a30d76SMark Bloch 	dev->lag_ports = mlx5_lag_get_num_ports(mdev);
30567c34ec19SAviv Heller 	dev->lag_active = true;
30579ef9c640SAviv Heller 	return 0;
30589ef9c640SAviv Heller 
30599ef9c640SAviv Heller err_destroy_vport_lag:
30609ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
30619ef9c640SAviv Heller 	return err;
30629ef9c640SAviv Heller }
30639ef9c640SAviv Heller 
mlx5_eth_lag_cleanup(struct mlx5_ib_dev * dev)306445f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
30659ef9c640SAviv Heller {
30669ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
30679ef9c640SAviv Heller 
30687c34ec19SAviv Heller 	if (dev->lag_active) {
30697c34ec19SAviv Heller 		dev->lag_active = false;
30707c34ec19SAviv Heller 
30719a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
30729a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
30739ef9c640SAviv Heller 
30749ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
30759ef9c640SAviv Heller 	}
30769ef9c640SAviv Heller }
30779ef9c640SAviv Heller 
mlx5_netdev_notifier_register(struct mlx5_roce * roce,struct net_device * netdev)3078dca55da0SJiri Pirko static void mlx5_netdev_notifier_register(struct mlx5_roce *roce,
3079dca55da0SJiri Pirko 					  struct net_device *netdev)
3080fc24fc5eSAchiad Shochat {
3081e53505a8SAchiad Shochat 	int err;
3082e53505a8SAchiad Shochat 
3083dca55da0SJiri Pirko 	if (roce->tracking_netdev)
3084dca55da0SJiri Pirko 		return;
3085dca55da0SJiri Pirko 	roce->tracking_netdev = netdev;
3086dca55da0SJiri Pirko 	roce->nb.notifier_call = mlx5_netdev_event;
3087dca55da0SJiri Pirko 	err = register_netdevice_notifier_dev_net(netdev, &roce->nb, &roce->nn);
3088dca55da0SJiri Pirko 	WARN_ON(err);
30895ec8c83eSAviv Heller }
3090e53505a8SAchiad Shochat 
mlx5_netdev_notifier_unregister(struct mlx5_roce * roce)3091dca55da0SJiri Pirko static void mlx5_netdev_notifier_unregister(struct mlx5_roce *roce)
3092e126ba97SEli Cohen {
3093dca55da0SJiri Pirko 	if (!roce->tracking_netdev)
3094dca55da0SJiri Pirko 		return;
3095dca55da0SJiri Pirko 	unregister_netdevice_notifier_dev_net(roce->tracking_netdev, &roce->nb,
3096dca55da0SJiri Pirko 					      &roce->nn);
3097dca55da0SJiri Pirko 	roce->tracking_netdev = NULL;
3098e126ba97SEli Cohen }
3099dca55da0SJiri Pirko 
mlx5e_mdev_notifier_event(struct notifier_block * nb,unsigned long event,void * data)3100dca55da0SJiri Pirko static int mlx5e_mdev_notifier_event(struct notifier_block *nb,
3101dca55da0SJiri Pirko 				     unsigned long event, void *data)
3102dca55da0SJiri Pirko {
3103dca55da0SJiri Pirko 	struct mlx5_roce *roce = container_of(nb, struct mlx5_roce, mdev_nb);
3104dca55da0SJiri Pirko 	struct net_device *netdev = data;
3105dca55da0SJiri Pirko 
3106dca55da0SJiri Pirko 	switch (event) {
3107dca55da0SJiri Pirko 	case MLX5_DRIVER_EVENT_UPLINK_NETDEV:
3108dca55da0SJiri Pirko 		if (netdev)
3109dca55da0SJiri Pirko 			mlx5_netdev_notifier_register(roce, netdev);
3110dca55da0SJiri Pirko 		else
3111dca55da0SJiri Pirko 			mlx5_netdev_notifier_unregister(roce);
3112dca55da0SJiri Pirko 		break;
3113dca55da0SJiri Pirko 	default:
3114dca55da0SJiri Pirko 		return NOTIFY_DONE;
3115dca55da0SJiri Pirko 	}
3116dca55da0SJiri Pirko 
3117dca55da0SJiri Pirko 	return NOTIFY_OK;
3118dca55da0SJiri Pirko }
3119dca55da0SJiri Pirko 
mlx5_mdev_netdev_track(struct mlx5_ib_dev * dev,u32 port_num)3120dca55da0SJiri Pirko static void mlx5_mdev_netdev_track(struct mlx5_ib_dev *dev, u32 port_num)
3121dca55da0SJiri Pirko {
3122dca55da0SJiri Pirko 	struct mlx5_roce *roce = &dev->port[port_num].roce;
3123dca55da0SJiri Pirko 
3124dca55da0SJiri Pirko 	roce->mdev_nb.notifier_call = mlx5e_mdev_notifier_event;
3125dca55da0SJiri Pirko 	mlx5_blocking_notifier_register(dev->mdev, &roce->mdev_nb);
3126dca55da0SJiri Pirko 	mlx5_core_uplink_netdev_event_replay(dev->mdev);
3127dca55da0SJiri Pirko }
3128dca55da0SJiri Pirko 
mlx5_mdev_netdev_untrack(struct mlx5_ib_dev * dev,u32 port_num)3129dca55da0SJiri Pirko static void mlx5_mdev_netdev_untrack(struct mlx5_ib_dev *dev, u32 port_num)
3130dca55da0SJiri Pirko {
3131dca55da0SJiri Pirko 	struct mlx5_roce *roce = &dev->port[port_num].roce;
3132dca55da0SJiri Pirko 
3133dca55da0SJiri Pirko 	mlx5_blocking_notifier_unregister(dev->mdev, &roce->mdev_nb);
3134dca55da0SJiri Pirko 	mlx5_netdev_notifier_unregister(roce);
3135e126ba97SEli Cohen }
3136e126ba97SEli Cohen 
mlx5_enable_eth(struct mlx5_ib_dev * dev)3137e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3138e126ba97SEli Cohen {
3139e126ba97SEli Cohen 	int err;
3140e126ba97SEli Cohen 
3141c446d9daSMark Bloch 	if (!dev->is_rep && dev->profile != &raw_eth_profile) {
3142e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3143e53505a8SAchiad Shochat 		if (err)
31448e6efa3aSMark Bloch 			return err;
3145c446d9daSMark Bloch 	}
3146e53505a8SAchiad Shochat 
314745f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
31489ef9c640SAviv Heller 	if (err)
31499ef9c640SAviv Heller 		goto err_disable_roce;
31509ef9c640SAviv Heller 
3151e53505a8SAchiad Shochat 	return 0;
3152e53505a8SAchiad Shochat 
31539ef9c640SAviv Heller err_disable_roce:
3154c446d9daSMark Bloch 	if (!dev->is_rep && dev->profile != &raw_eth_profile)
31559ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
31569ef9c640SAviv Heller 
3157e53505a8SAchiad Shochat 	return err;
3158fc24fc5eSAchiad Shochat }
3159fc24fc5eSAchiad Shochat 
mlx5_disable_eth(struct mlx5_ib_dev * dev)316045f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3161fc24fc5eSAchiad Shochat {
316245f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3163c446d9daSMark Bloch 	if (!dev->is_rep && dev->profile != &raw_eth_profile)
3164e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
3165fc24fc5eSAchiad Shochat }
3166fc24fc5eSAchiad Shochat 
mlx5_ib_rn_get_params(struct ib_device * device,u32 port_num,enum rdma_netdev_t type,struct rdma_netdev_alloc_params * params)31671fb7f897SMark Bloch static int mlx5_ib_rn_get_params(struct ib_device *device, u32 port_num,
3168693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
3169f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
3170693dfd5aSErez Shitrit {
3171693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
3172f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
3173693dfd5aSErez Shitrit 
3174f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
3175693dfd5aSErez Shitrit }
3176693dfd5aSErez Shitrit 
delay_drop_timeout_read(struct file * filp,char __user * buf,size_t count,loff_t * pos)3177fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3178fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
3179fe248c3aSMaor Gottlieb {
3180fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3181fe248c3aSMaor Gottlieb 	char lbuf[20];
3182fe248c3aSMaor Gottlieb 	int len;
3183fe248c3aSMaor Gottlieb 
3184fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3185fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
3186fe248c3aSMaor Gottlieb }
3187fe248c3aSMaor Gottlieb 
delay_drop_timeout_write(struct file * filp,const char __user * buf,size_t count,loff_t * pos)3188fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3189fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
3190fe248c3aSMaor Gottlieb {
3191fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3192fe248c3aSMaor Gottlieb 	u32 timeout;
3193fe248c3aSMaor Gottlieb 	u32 var;
3194fe248c3aSMaor Gottlieb 
3195fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
3196fe248c3aSMaor Gottlieb 		return -EFAULT;
3197fe248c3aSMaor Gottlieb 
3198fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3199fe248c3aSMaor Gottlieb 			1000);
3200fe248c3aSMaor Gottlieb 	if (timeout != var)
3201fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3202fe248c3aSMaor Gottlieb 			    timeout);
3203fe248c3aSMaor Gottlieb 
3204fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
3205fe248c3aSMaor Gottlieb 
3206fe248c3aSMaor Gottlieb 	return count;
3207fe248c3aSMaor Gottlieb }
3208fe248c3aSMaor Gottlieb 
3209fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
3210fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
3211fe248c3aSMaor Gottlieb 	.open	= simple_open,
3212fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
3213fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
3214fe248c3aSMaor Gottlieb };
3215fe248c3aSMaor Gottlieb 
mlx5_ib_unbind_slave_port(struct mlx5_ib_dev * ibdev,struct mlx5_ib_multiport_info * mpi)321632f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
321732f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
321832f69e4bSDaniel Jurgens {
32191fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
322032f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
322132f69e4bSDaniel Jurgens 	int comps;
322232f69e4bSDaniel Jurgens 	int err;
322332f69e4bSDaniel Jurgens 	int i;
322432f69e4bSDaniel Jurgens 
32259dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
32269dc4cfffSLeon Romanovsky 
3227fdd350feSPatrisious Haddad 	mlx5_core_mp_event_replay(ibdev->mdev,
3228fdd350feSPatrisious Haddad 				  MLX5_DRIVER_EVENT_AFFILIATION_REMOVED,
3229fdd350feSPatrisious Haddad 				  NULL);
3230fdd350feSPatrisious Haddad 	mlx5_core_mp_event_replay(mpi->mdev,
3231fdd350feSPatrisious Haddad 				  MLX5_DRIVER_EVENT_AFFILIATION_REMOVED,
3232fdd350feSPatrisious Haddad 				  NULL);
3233fdd350feSPatrisious Haddad 
3234a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
3235a9e546e7SParav Pandit 
323632f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
323732f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
323832f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
323932f69e4bSDaniel Jurgens 		return;
324032f69e4bSDaniel Jurgens 	}
3241df097a27SSaeed Mahameed 
324232f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
324332f69e4bSDaniel Jurgens 
324432f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
324523eaf3b5SLeon Romanovsky 	if (mpi->mdev_events.notifier_call)
324623eaf3b5SLeon Romanovsky 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
324723eaf3b5SLeon Romanovsky 	mpi->mdev_events.notifier_call = NULL;
3248dca55da0SJiri Pirko 	mlx5_mdev_netdev_untrack(ibdev, port_num);
324932f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
325032f69e4bSDaniel Jurgens 
325132f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
325232f69e4bSDaniel Jurgens 	if (comps) {
325332f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
325432f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
325532f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
325632f69e4bSDaniel Jurgens 
325732f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
325832f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
325932f69e4bSDaniel Jurgens 
326032f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
326132f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
326232f69e4bSDaniel Jurgens 	}
326332f69e4bSDaniel Jurgens 
326432f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
326532f69e4bSDaniel Jurgens 
326632f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
326732f69e4bSDaniel Jurgens 
326832f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
326932f69e4bSDaniel Jurgens 
32701fb7f897SMark Bloch 	mlx5_ib_dbg(ibdev, "unaffiliated port %u\n", port_num + 1);
327132f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
327232f69e4bSDaniel Jurgens 	 * it back to the list.
327332f69e4bSDaniel Jurgens 	 */
327432f69e4bSDaniel Jurgens 	if (err)
327532f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
327632f69e4bSDaniel Jurgens 			    port_num + 1);
327732f69e4bSDaniel Jurgens 
327895579e78SMark Bloch 	ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
327932f69e4bSDaniel Jurgens }
328032f69e4bSDaniel Jurgens 
mlx5_ib_bind_slave_port(struct mlx5_ib_dev * ibdev,struct mlx5_ib_multiport_info * mpi)328132f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
328232f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
328332f69e4bSDaniel Jurgens {
32841fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
3285fdd350feSPatrisious Haddad 	u64 key;
328632f69e4bSDaniel Jurgens 	int err;
328732f69e4bSDaniel Jurgens 
32889dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
32899dc4cfffSLeon Romanovsky 
329032f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
329132f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
32921fb7f897SMark Bloch 		mlx5_ib_dbg(ibdev, "port %u already affiliated.\n",
329332f69e4bSDaniel Jurgens 			    port_num + 1);
329432f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
329532f69e4bSDaniel Jurgens 		return false;
329632f69e4bSDaniel Jurgens 	}
329732f69e4bSDaniel Jurgens 
329832f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
329932f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
3300df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
330132f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
330232f69e4bSDaniel Jurgens 
330332f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
330432f69e4bSDaniel Jurgens 	if (err)
330532f69e4bSDaniel Jurgens 		goto unbind;
330632f69e4bSDaniel Jurgens 
3307dca55da0SJiri Pirko 	mlx5_mdev_netdev_track(ibdev, port_num);
330832f69e4bSDaniel Jurgens 
3309df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
3310df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
3311df097a27SSaeed Mahameed 
331273eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
3313a9e546e7SParav Pandit 
3314885faf3cSPatrisious Haddad 	key = mpi->mdev->priv.adev_idx;
3315fdd350feSPatrisious Haddad 	mlx5_core_mp_event_replay(mpi->mdev,
3316fdd350feSPatrisious Haddad 				  MLX5_DRIVER_EVENT_AFFILIATION_DONE,
3317fdd350feSPatrisious Haddad 				  &key);
3318fdd350feSPatrisious Haddad 	mlx5_core_mp_event_replay(ibdev->mdev,
3319fdd350feSPatrisious Haddad 				  MLX5_DRIVER_EVENT_AFFILIATION_DONE,
3320fdd350feSPatrisious Haddad 				  &key);
3321fdd350feSPatrisious Haddad 
332232f69e4bSDaniel Jurgens 	return true;
332332f69e4bSDaniel Jurgens 
332432f69e4bSDaniel Jurgens unbind:
332532f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
332632f69e4bSDaniel Jurgens 	return false;
332732f69e4bSDaniel Jurgens }
332832f69e4bSDaniel Jurgens 
mlx5_ib_init_multiport_master(struct mlx5_ib_dev * dev)332932f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
333032f69e4bSDaniel Jurgens {
33311fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
333232f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
333332f69e4bSDaniel Jurgens 							  port_num + 1);
333432f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
333532f69e4bSDaniel Jurgens 	int err;
33361fb7f897SMark Bloch 	u32 i;
333732f69e4bSDaniel Jurgens 
333832f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
333932f69e4bSDaniel Jurgens 		return 0;
334032f69e4bSDaniel Jurgens 
334132f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
334232f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
334332f69e4bSDaniel Jurgens 	if (err)
334432f69e4bSDaniel Jurgens 		return err;
334532f69e4bSDaniel Jurgens 
334632f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
334732f69e4bSDaniel Jurgens 	if (err)
334832f69e4bSDaniel Jurgens 		return err;
334932f69e4bSDaniel Jurgens 
335032f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
335132f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
335232f69e4bSDaniel Jurgens 		bool bound = false;
335332f69e4bSDaniel Jurgens 
335432f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
335532f69e4bSDaniel Jurgens 		if (i == port_num) {
335632f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
335732f69e4bSDaniel Jurgens 			if (!mpi) {
335832f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
335932f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
336032f69e4bSDaniel Jurgens 				return -ENOMEM;
336132f69e4bSDaniel Jurgens 			}
336232f69e4bSDaniel Jurgens 
336332f69e4bSDaniel Jurgens 			mpi->is_master = true;
336432f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
336532f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
336632f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
336732f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
336832f69e4bSDaniel Jurgens 			mpi = NULL;
336932f69e4bSDaniel Jurgens 			continue;
337032f69e4bSDaniel Jurgens 		}
337132f69e4bSDaniel Jurgens 
337232f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
337332f69e4bSDaniel Jurgens 				    list) {
337432f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
3375*25e6e9daSPatrisious Haddad 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i &&
3376*25e6e9daSPatrisious Haddad 			    mlx5_core_same_coredev_type(dev->mdev, mpi->mdev)) {
337732f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
337832f69e4bSDaniel Jurgens 			}
337932f69e4bSDaniel Jurgens 
338032f69e4bSDaniel Jurgens 			if (bound) {
3381c42260f1SVu Pham 				dev_dbg(mpi->mdev->device,
3382c42260f1SVu Pham 					"removing port from unaffiliated list.\n");
338332f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
338432f69e4bSDaniel Jurgens 				list_del(&mpi->list);
338532f69e4bSDaniel Jurgens 				break;
338632f69e4bSDaniel Jurgens 			}
338732f69e4bSDaniel Jurgens 		}
33887416790eSParav Pandit 		if (!bound)
338932f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
339032f69e4bSDaniel Jurgens 				    i + 1);
339132f69e4bSDaniel Jurgens 	}
339232f69e4bSDaniel Jurgens 
339332f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
339432f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
339532f69e4bSDaniel Jurgens 	return err;
339632f69e4bSDaniel Jurgens }
339732f69e4bSDaniel Jurgens 
mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev * dev)339832f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
339932f69e4bSDaniel Jurgens {
34001fb7f897SMark Bloch 	u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
340132f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
340232f69e4bSDaniel Jurgens 							  port_num + 1);
34031fb7f897SMark Bloch 	u32 i;
340432f69e4bSDaniel Jurgens 
340532f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
340632f69e4bSDaniel Jurgens 		return;
340732f69e4bSDaniel Jurgens 
340832f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
340932f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
341032f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
341132f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
341232f69e4bSDaniel Jurgens 			if (i == port_num) {
341332f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
341432f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
341532f69e4bSDaniel Jurgens 			} else {
34161fb7f897SMark Bloch 				mlx5_ib_dbg(dev, "unbinding port_num: %u\n",
34171fb7f897SMark Bloch 					    i + 1);
34187ce6095eSLeon Romanovsky 				list_add_tail(&dev->port[i].mp.mpi->list,
34197ce6095eSLeon Romanovsky 					      &mlx5_ib_unaffiliated_port_list);
34204a754d76SLeon Romanovsky 				mlx5_ib_unbind_slave_port(dev,
34214a754d76SLeon Romanovsky 							  dev->port[i].mp.mpi);
342232f69e4bSDaniel Jurgens 			}
342332f69e4bSDaniel Jurgens 		}
342432f69e4bSDaniel Jurgens 	}
342532f69e4bSDaniel Jurgens 
342632f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
342732f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
342832f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
342932f69e4bSDaniel Jurgens 
343032f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
343132f69e4bSDaniel Jurgens }
343232f69e4bSDaniel Jurgens 
mmap_obj_cleanup(struct ib_uobject * uobject,enum rdma_remove_reason why,struct uverbs_attr_bundle * attrs)3433342ee59dSYishai Hadas static int mmap_obj_cleanup(struct ib_uobject *uobject,
34347be76befSYishai Hadas 			    enum rdma_remove_reason why,
34357be76befSYishai Hadas 			    struct uverbs_attr_bundle *attrs)
34367be76befSYishai Hadas {
34377be76befSYishai Hadas 	struct mlx5_user_mmap_entry *obj = uobject->object;
34387be76befSYishai Hadas 
34397be76befSYishai Hadas 	rdma_user_mmap_entry_remove(&obj->rdma_entry);
34407be76befSYishai Hadas 	return 0;
34417be76befSYishai Hadas }
34427be76befSYishai Hadas 
mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext * c,struct mlx5_user_mmap_entry * entry,size_t length)3443342ee59dSYishai Hadas static int mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext *c,
3444342ee59dSYishai Hadas 					    struct mlx5_user_mmap_entry *entry,
3445342ee59dSYishai Hadas 					    size_t length)
3446342ee59dSYishai Hadas {
3447342ee59dSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
3448342ee59dSYishai Hadas 		&c->ibucontext, &entry->rdma_entry, length,
3449342ee59dSYishai Hadas 		(MLX5_IB_MMAP_OFFSET_START << 16),
3450342ee59dSYishai Hadas 		((MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1));
3451342ee59dSYishai Hadas }
3452342ee59dSYishai Hadas 
34537be76befSYishai Hadas static struct mlx5_user_mmap_entry *
alloc_var_entry(struct mlx5_ib_ucontext * c)34547be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c)
34557be76befSYishai Hadas {
34567be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
34577be76befSYishai Hadas 	struct mlx5_var_table *var_table;
34587be76befSYishai Hadas 	u32 page_idx;
34597be76befSYishai Hadas 	int err;
34607be76befSYishai Hadas 
34617be76befSYishai Hadas 	var_table = &to_mdev(c->ibucontext.device)->var_table;
34627be76befSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
34637be76befSYishai Hadas 	if (!entry)
34647be76befSYishai Hadas 		return ERR_PTR(-ENOMEM);
34657be76befSYishai Hadas 
34667be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
34677be76befSYishai Hadas 	page_idx = find_first_zero_bit(var_table->bitmap,
34687be76befSYishai Hadas 				       var_table->num_var_hw_entries);
34697be76befSYishai Hadas 	if (page_idx >= var_table->num_var_hw_entries) {
34707be76befSYishai Hadas 		err = -ENOSPC;
34717be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
34727be76befSYishai Hadas 		goto end;
34737be76befSYishai Hadas 	}
34747be76befSYishai Hadas 
34757be76befSYishai Hadas 	set_bit(page_idx, var_table->bitmap);
34767be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
34777be76befSYishai Hadas 
34787be76befSYishai Hadas 	entry->address = var_table->hw_start_addr +
34797be76befSYishai Hadas 				(page_idx * var_table->stride_size);
34807be76befSYishai Hadas 	entry->page_idx = page_idx;
34817be76befSYishai Hadas 	entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR;
34827be76befSYishai Hadas 
3483342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry,
3484342ee59dSYishai Hadas 					       var_table->stride_size);
34857be76befSYishai Hadas 	if (err)
34867be76befSYishai Hadas 		goto err_insert;
34877be76befSYishai Hadas 
34887be76befSYishai Hadas 	return entry;
34897be76befSYishai Hadas 
34907be76befSYishai Hadas err_insert:
34917be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
34927be76befSYishai Hadas 	clear_bit(page_idx, var_table->bitmap);
34937be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
34947be76befSYishai Hadas end:
34957be76befSYishai Hadas 	kfree(entry);
34967be76befSYishai Hadas 	return ERR_PTR(err);
34977be76befSYishai Hadas }
34987be76befSYishai Hadas 
UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)34997be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)(
35007be76befSYishai Hadas 	struct uverbs_attr_bundle *attrs)
35017be76befSYishai Hadas {
35027be76befSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
35037be76befSYishai Hadas 		attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
35047be76befSYishai Hadas 	struct mlx5_ib_ucontext *c;
35057be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
35067be76befSYishai Hadas 	u64 mmap_offset;
35077be76befSYishai Hadas 	u32 length;
35087be76befSYishai Hadas 	int err;
35097be76befSYishai Hadas 
35107be76befSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
35117be76befSYishai Hadas 	if (IS_ERR(c))
35127be76befSYishai Hadas 		return PTR_ERR(c);
35137be76befSYishai Hadas 
35147be76befSYishai Hadas 	entry = alloc_var_entry(c);
35157be76befSYishai Hadas 	if (IS_ERR(entry))
35167be76befSYishai Hadas 		return PTR_ERR(entry);
35177be76befSYishai Hadas 
35187be76befSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
35197be76befSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
35207be76befSYishai Hadas 	uobj->object = entry;
35210ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
35227be76befSYishai Hadas 
35237be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
35247be76befSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
35257be76befSYishai Hadas 	if (err)
35260ac8903cSJason Gunthorpe 		return err;
35277be76befSYishai Hadas 
35287be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
35297be76befSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
35307be76befSYishai Hadas 	if (err)
35310ac8903cSJason Gunthorpe 		return err;
35327be76befSYishai Hadas 
35337be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
35347be76befSYishai Hadas 			     &length, sizeof(length));
35357be76befSYishai Hadas 	return err;
35367be76befSYishai Hadas }
35377be76befSYishai Hadas 
35387be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
35397be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_ALLOC,
35407be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE,
35417be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
35427be76befSYishai Hadas 			UVERBS_ACCESS_NEW,
35437be76befSYishai Hadas 			UA_MANDATORY),
35447be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
35457be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
35467be76befSYishai Hadas 			   UA_MANDATORY),
35477be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
35487be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
35497be76befSYishai Hadas 			   UA_MANDATORY),
35507be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
35517be76befSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
35527be76befSYishai Hadas 			    UA_MANDATORY));
35537be76befSYishai Hadas 
35547be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
35557be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_DESTROY,
35567be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE,
35577be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
35587be76befSYishai Hadas 			UVERBS_ACCESS_DESTROY,
35597be76befSYishai Hadas 			UA_MANDATORY));
35607be76befSYishai Hadas 
35617be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR,
3562342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
35637be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC),
35647be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY));
35657be76befSYishai Hadas 
var_is_supported(struct ib_device * device)35667be76befSYishai Hadas static bool var_is_supported(struct ib_device *device)
35677be76befSYishai Hadas {
35687be76befSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(device);
35697be76befSYishai Hadas 
35707be76befSYishai Hadas 	return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
35717be76befSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q);
35727be76befSYishai Hadas }
35737be76befSYishai Hadas 
3574342ee59dSYishai Hadas static struct mlx5_user_mmap_entry *
alloc_uar_entry(struct mlx5_ib_ucontext * c,enum mlx5_ib_uapi_uar_alloc_type alloc_type)3575342ee59dSYishai Hadas alloc_uar_entry(struct mlx5_ib_ucontext *c,
3576342ee59dSYishai Hadas 		enum mlx5_ib_uapi_uar_alloc_type alloc_type)
3577342ee59dSYishai Hadas {
3578342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
3579342ee59dSYishai Hadas 	struct mlx5_ib_dev *dev;
3580342ee59dSYishai Hadas 	u32 uar_index;
3581342ee59dSYishai Hadas 	int err;
3582342ee59dSYishai Hadas 
3583342ee59dSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
3584342ee59dSYishai Hadas 	if (!entry)
3585342ee59dSYishai Hadas 		return ERR_PTR(-ENOMEM);
3586342ee59dSYishai Hadas 
3587342ee59dSYishai Hadas 	dev = to_mdev(c->ibucontext.device);
3588d2c8a155SMeir Lichtinger 	err = mlx5_cmd_uar_alloc(dev->mdev, &uar_index, c->devx_uid);
3589342ee59dSYishai Hadas 	if (err)
3590342ee59dSYishai Hadas 		goto end;
3591342ee59dSYishai Hadas 
3592342ee59dSYishai Hadas 	entry->page_idx = uar_index;
3593342ee59dSYishai Hadas 	entry->address = uar_index2paddress(dev, uar_index);
3594342ee59dSYishai Hadas 	if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3595342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC;
3596342ee59dSYishai Hadas 	else
3597342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_NC;
3598342ee59dSYishai Hadas 
3599342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry, PAGE_SIZE);
3600342ee59dSYishai Hadas 	if (err)
3601342ee59dSYishai Hadas 		goto err_insert;
3602342ee59dSYishai Hadas 
3603342ee59dSYishai Hadas 	return entry;
3604342ee59dSYishai Hadas 
3605342ee59dSYishai Hadas err_insert:
3606d2c8a155SMeir Lichtinger 	mlx5_cmd_uar_dealloc(dev->mdev, uar_index, c->devx_uid);
3607342ee59dSYishai Hadas end:
3608342ee59dSYishai Hadas 	kfree(entry);
3609342ee59dSYishai Hadas 	return ERR_PTR(err);
3610342ee59dSYishai Hadas }
3611342ee59dSYishai Hadas 
UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)3612342ee59dSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)(
3613342ee59dSYishai Hadas 	struct uverbs_attr_bundle *attrs)
3614342ee59dSYishai Hadas {
3615342ee59dSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
3616342ee59dSYishai Hadas 		attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3617342ee59dSYishai Hadas 	enum mlx5_ib_uapi_uar_alloc_type alloc_type;
3618342ee59dSYishai Hadas 	struct mlx5_ib_ucontext *c;
3619342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
3620342ee59dSYishai Hadas 	u64 mmap_offset;
3621342ee59dSYishai Hadas 	u32 length;
3622342ee59dSYishai Hadas 	int err;
3623342ee59dSYishai Hadas 
3624342ee59dSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
3625342ee59dSYishai Hadas 	if (IS_ERR(c))
3626342ee59dSYishai Hadas 		return PTR_ERR(c);
3627342ee59dSYishai Hadas 
3628342ee59dSYishai Hadas 	err = uverbs_get_const(&alloc_type, attrs,
3629342ee59dSYishai Hadas 			       MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE);
3630342ee59dSYishai Hadas 	if (err)
3631342ee59dSYishai Hadas 		return err;
3632342ee59dSYishai Hadas 
3633342ee59dSYishai Hadas 	if (alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF &&
3634342ee59dSYishai Hadas 	    alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_NC)
3635342ee59dSYishai Hadas 		return -EOPNOTSUPP;
3636342ee59dSYishai Hadas 
3637342ee59dSYishai Hadas 	if (!to_mdev(c->ibucontext.device)->wc_support &&
3638342ee59dSYishai Hadas 	    alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3639342ee59dSYishai Hadas 		return -EOPNOTSUPP;
3640342ee59dSYishai Hadas 
3641342ee59dSYishai Hadas 	entry = alloc_uar_entry(c, alloc_type);
3642342ee59dSYishai Hadas 	if (IS_ERR(entry))
3643342ee59dSYishai Hadas 		return PTR_ERR(entry);
3644342ee59dSYishai Hadas 
3645342ee59dSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
3646342ee59dSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
3647342ee59dSYishai Hadas 	uobj->object = entry;
36480ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3649342ee59dSYishai Hadas 
3650342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3651342ee59dSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
3652342ee59dSYishai Hadas 	if (err)
36530ac8903cSJason Gunthorpe 		return err;
3654342ee59dSYishai Hadas 
3655342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3656342ee59dSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
3657342ee59dSYishai Hadas 	if (err)
36580ac8903cSJason Gunthorpe 		return err;
3659342ee59dSYishai Hadas 
3660342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3661342ee59dSYishai Hadas 			     &length, sizeof(length));
3662342ee59dSYishai Hadas 	return err;
3663342ee59dSYishai Hadas }
3664342ee59dSYishai Hadas 
3665342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
3666342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_ALLOC,
3667342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE,
3668342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
3669342ee59dSYishai Hadas 			UVERBS_ACCESS_NEW,
3670342ee59dSYishai Hadas 			UA_MANDATORY),
3671342ee59dSYishai Hadas 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE,
3672342ee59dSYishai Hadas 			     enum mlx5_ib_uapi_uar_alloc_type,
3673342ee59dSYishai Hadas 			     UA_MANDATORY),
3674342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3675342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
3676342ee59dSYishai Hadas 			   UA_MANDATORY),
3677342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3678342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
3679342ee59dSYishai Hadas 			   UA_MANDATORY),
3680342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3681342ee59dSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
3682342ee59dSYishai Hadas 			    UA_MANDATORY));
3683342ee59dSYishai Hadas 
3684342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
3685342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_DESTROY,
3686342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_DESTROY_HANDLE,
3687342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
3688342ee59dSYishai Hadas 			UVERBS_ACCESS_DESTROY,
3689342ee59dSYishai Hadas 			UA_MANDATORY));
3690342ee59dSYishai Hadas 
3691342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_UAR,
3692342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
3693342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_ALLOC),
3694342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_DESTROY));
3695342ee59dSYishai Hadas 
36969a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
36970fb556b2SYishai Hadas 	mlx5_ib_query_context,
36980fb556b2SYishai Hadas 	UVERBS_OBJECT_DEVICE,
36990fb556b2SYishai Hadas 	UVERBS_METHOD_QUERY_CONTEXT,
37000fb556b2SYishai Hadas 	UVERBS_ATTR_PTR_OUT(
37010fb556b2SYishai Hadas 		MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
37020fb556b2SYishai Hadas 		UVERBS_ATTR_STRUCT(struct mlx5_ib_alloc_ucontext_resp,
37030fb556b2SYishai Hadas 				   dump_fill_mkey),
37040fb556b2SYishai Hadas 		UA_MANDATORY));
37050fb556b2SYishai Hadas 
37060cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
370736e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
37080cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
370930f2fe40SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_qos_defs),
371005f71ef9SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_std_types_defs),
3711831df883SMaor Gottlieb 	UAPI_DEF_CHAIN(mlx5_ib_dm_defs),
37128c84660bSMatan Barak 
37130fb556b2SYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DEVICE, &mlx5_ib_query_context),
37147be76befSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR,
37157be76befSYishai Hadas 				UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)),
3716342ee59dSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_UAR),
37170cbf432dSJason Gunthorpe 	{}
37180cbf432dSJason Gunthorpe };
37198c84660bSMatan Barak 
mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev * dev)3720fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
3721e126ba97SEli Cohen {
372232f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
3723806b101bSJason Gunthorpe 	WARN_ON(!xa_empty(&dev->odp_mkeys));
3724ab40530aSParav Pandit 	mutex_destroy(&dev->cap_mask_mutex);
372550211ec9SJason Gunthorpe 	WARN_ON(!xa_empty(&dev->sig_mrs));
37264056b12eSAriel Levkovich 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
3727758ce14aSPatrisious Haddad 	mlx5r_macsec_dealloc_gids(dev);
372816c1975fSMark Bloch }
372916c1975fSMark Bloch 
mlx5_ib_stage_init_init(struct mlx5_ib_dev * dev)3730fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
373116c1975fSMark Bloch {
373216c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
3733758ce14aSPatrisious Haddad 	int err, i;
3734e126ba97SEli Cohen 
373513179652SParav Pandit 	dev->ib_dev.node_type = RDMA_NODE_IB_CA;
373613179652SParav Pandit 	dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
373713179652SParav Pandit 	dev->ib_dev.phys_port_cnt = dev->num_ports;
373813179652SParav Pandit 	dev->ib_dev.dev.parent = mdev->device;
373913179652SParav Pandit 	dev->ib_dev.lag_flags = RDMA_LAG_FLAGS_HASH_ALL_SLAVES;
374013179652SParav Pandit 
374132f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
374232f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
374395579e78SMark Bloch 		rwlock_init(&dev->port[i].roce.netdev_lock);
3744d3b5cc1cSMark Bloch 		dev->port[i].roce.dev = dev;
3745d3b5cc1cSMark Bloch 		dev->port[i].roce.native_port_num = i + 1;
3746d3b5cc1cSMark Bloch 		dev->port[i].roce.last_port_state = IB_PORT_DOWN;
374732f69e4bSDaniel Jurgens 	}
374832f69e4bSDaniel Jurgens 
3749594cac11SOr Har-Toov 	err = mlx5r_cmd_query_special_mkeys(dev);
3750594cac11SOr Har-Toov 	if (err)
3751594cac11SOr Har-Toov 		return err;
3752594cac11SOr Har-Toov 
375358dbd642SPatrisious Haddad 	err = mlx5r_macsec_init_gids_and_devlist(dev);
3754e126ba97SEli Cohen 	if (err)
3755da796ccbSMark Bloch 		return err;
3756e126ba97SEli Cohen 
3757758ce14aSPatrisious Haddad 	err = mlx5_ib_init_multiport_master(dev);
3758758ce14aSPatrisious Haddad 	if (err)
3759758ce14aSPatrisious Haddad 		goto err;
3760758ce14aSPatrisious Haddad 
3761a989ea01SMark Bloch 	err = set_has_smi_cap(dev);
3762a989ea01SMark Bloch 	if (err)
37632cb091f6SParav Pandit 		goto err_mp;
3764e126ba97SEli Cohen 
37652019d70eSParav Pandit 	err = mlx5_query_max_pkeys(&dev->ib_dev, &dev->pkey_table_len);
37662019d70eSParav Pandit 	if (err)
37672019d70eSParav Pandit 		goto err_mp;
37682019d70eSParav Pandit 
37691b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3770e126ba97SEli Cohen 		get_ext_port_caps(dev);
3771e126ba97SEli Cohen 
3772674dd4e2SMaher Sanalla 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_max(mdev);
3773e126ba97SEli Cohen 
37743cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
37753cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
37763cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
3777806b101bSJason Gunthorpe 	xa_init(&dev->odp_mkeys);
377850211ec9SJason Gunthorpe 	xa_init(&dev->sig_mrs);
3779f743ff3bSSaeed Mahameed 	atomic_set(&dev->mkey_var, 0);
37803cc297dbSMark Bloch 
37813b113a1eSAriel Levkovich 	spin_lock_init(&dev->dm.lock);
37823b113a1eSAriel Levkovich 	dev->dm.dev = mdev;
378316c1975fSMark Bloch 	return 0;
378432f69e4bSDaniel Jurgens err_mp:
378532f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
37863b733016SYishai Hadas err:
37873b733016SYishai Hadas 	mlx5r_macsec_dealloc_gids(dev);
3788d286ac1dSParav Pandit 	return err;
378916c1975fSMark Bloch }
379016c1975fSMark Bloch 
mlx5_ib_enable_driver(struct ib_device * dev)3791ac47bf5eSLeon Romanovsky static int mlx5_ib_enable_driver(struct ib_device *dev)
37929a4ca38dSMark Bloch {
3793ac47bf5eSLeon Romanovsky 	struct mlx5_ib_dev *mdev = to_mdev(dev);
3794ac47bf5eSLeon Romanovsky 	int ret;
37959a4ca38dSMark Bloch 
3796ac47bf5eSLeon Romanovsky 	ret = mlx5_ib_test_wc(mdev);
3797ac47bf5eSLeon Romanovsky 	mlx5_ib_dbg(mdev, "Write-Combining %s",
3798ac47bf5eSLeon Romanovsky 		    mdev->wc_support ? "supported" : "not supported");
37999a4ca38dSMark Bloch 
3800ac47bf5eSLeon Romanovsky 	return ret;
38019a4ca38dSMark Bloch }
38029a4ca38dSMark Bloch 
380396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
38047a154142SJason Gunthorpe 	.owner = THIS_MODULE,
3805b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_MLX5,
380672c6ec18SJason Gunthorpe 	.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION,
3807b9560a41SJason Gunthorpe 
380896458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
380996458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
38106c984472SMax Gurtovoy 	.alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
381196458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
381296458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
381396458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
381496458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
381596458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
381696458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
381796458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
381896458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
3819676a80adSJason Gunthorpe 	.create_user_ah = mlx5_ib_create_ah,
382096458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
382196458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
382296458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
382396458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
382496458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
382596458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
382696458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
382796458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
382896458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
382996458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
383096458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
383196458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
3832915e4af5SJason Gunthorpe 	.device_group = &mlx5_attr_group,
383311f552e2SMichael Guralnik 	.enable_driver = mlx5_ib_enable_driver,
383496458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
383596458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
383696458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
383796458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
38386c984472SMax Gurtovoy 	.map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
383996458233SKamal Heib 	.mmap = mlx5_ib_mmap,
3840dc2316ebSYishai Hadas 	.mmap_free = mlx5_ib_mmap_free,
384196458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
384296458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
384396458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
384496458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
384596458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
384696458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
3847029e88fdSLeon Romanovsky 	.post_recv = mlx5_ib_post_recv_nodrain,
3848029e88fdSLeon Romanovsky 	.post_send = mlx5_ib_post_send_nodrain,
384996458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
385096458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
385196458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
385296458233SKamal Heib 	.query_device = mlx5_ib_query_device,
385396458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
385496458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
385596458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
385696458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
38570fb556b2SYishai Hadas 	.query_ucontext = mlx5_ib_query_ucontext,
385896458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
385990da7dc8SJianxin Xiong 	.reg_user_mr_dmabuf = mlx5_ib_reg_user_mr_dmabuf,
386096458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
386196458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
386296458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
3863d3456914SLeon Romanovsky 
3864d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
38653b023e1bSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_counters, mlx5_ib_mcounters, ibcntrs),
3866e39afe3dSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
386721a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
3868514aee66SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_qp, mlx5_ib_qp, ibqp),
386968e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
3870a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
387196458233SKamal Heib };
387296458233SKamal Heib 
387396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
387496458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
387596458233SKamal Heib };
387696458233SKamal Heib 
387796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
387896458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
38799c0015efSDanit Goldberg 	.get_vf_guid = mlx5_ib_get_vf_guid,
388096458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
388196458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
388296458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
388396458233SKamal Heib };
388496458233SKamal Heib 
388596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
388696458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
388796458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
3888d18bb3e1SLeon Romanovsky 
3889d18bb3e1SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_mw, mlx5_ib_mw, ibmw),
389096458233SKamal Heib };
389196458233SKamal Heib 
389296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
389396458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
389496458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
389528ad5f65SLeon Romanovsky 
389628ad5f65SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_xrcd, mlx5_ib_xrcd, ibxrcd),
389796458233SKamal Heib };
389896458233SKamal Heib 
mlx5_ib_init_var_table(struct mlx5_ib_dev * dev)3899f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev)
3900f164be8cSYishai Hadas {
3901f164be8cSYishai Hadas 	struct mlx5_core_dev *mdev = dev->mdev;
3902f164be8cSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
3903f164be8cSYishai Hadas 	u8 log_doorbell_bar_size;
3904f164be8cSYishai Hadas 	u8 log_doorbell_stride;
3905f164be8cSYishai Hadas 	u64 bar_size;
3906f164be8cSYishai Hadas 
3907f164be8cSYishai Hadas 	log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
3908f164be8cSYishai Hadas 					log_doorbell_bar_size);
3909f164be8cSYishai Hadas 	log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
3910f164be8cSYishai Hadas 					log_doorbell_stride);
3911f164be8cSYishai Hadas 	var_table->hw_start_addr = dev->mdev->bar_addr +
3912f164be8cSYishai Hadas 				MLX5_CAP64_DEV_VDPA_EMULATION(mdev,
3913f164be8cSYishai Hadas 					doorbell_bar_offset);
3914f164be8cSYishai Hadas 	bar_size = (1ULL << log_doorbell_bar_size) * 4096;
3915f164be8cSYishai Hadas 	var_table->stride_size = 1ULL << log_doorbell_stride;
391691b74bf5SAlexander Lobakin 	var_table->num_var_hw_entries = div_u64(bar_size,
391791b74bf5SAlexander Lobakin 						var_table->stride_size);
3918f164be8cSYishai Hadas 	mutex_init(&var_table->bitmap_lock);
3919f164be8cSYishai Hadas 	var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries,
3920f164be8cSYishai Hadas 					  GFP_KERNEL);
3921f164be8cSYishai Hadas 	return (var_table->bitmap) ? 0 : -ENOMEM;
3922f164be8cSYishai Hadas }
3923f164be8cSYishai Hadas 
mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev * dev)3924f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev)
3925f164be8cSYishai Hadas {
3926f164be8cSYishai Hadas 	bitmap_free(dev->var_table.bitmap);
3927f164be8cSYishai Hadas }
3928f164be8cSYishai Hadas 
mlx5_ib_stage_caps_init(struct mlx5_ib_dev * dev)3929fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
393016c1975fSMark Bloch {
393116c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
393216c1975fSMark Bloch 	int err;
393316c1975fSMark Bloch 
3934f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
3935f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
393696458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
393796458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
39388e959601SNiranjana Vishwanathapura 
393996458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
394096458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
39417c2344c3SMaor Gottlieb 
39426e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
39436e8484c5SMax Gurtovoy 
394444ce37bcSJason Gunthorpe 	if (MLX5_CAP_GEN(mdev, imaicl))
394596458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
3946d2370e0aSMatan Barak 
394744ce37bcSJason Gunthorpe 	if (MLX5_CAP_GEN(mdev, xrc))
394896458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
3949e126ba97SEli Cohen 
395025c13324SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic) ||
395125c13324SAriel Levkovich 	    MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
395225c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
395396458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
395424da0016SAriel Levkovich 
395596458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
395681e30880SYishai Hadas 
395736e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
395836e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
395981e30880SYishai Hadas 
3960e126ba97SEli Cohen 	err = init_node_data(dev);
3961e126ba97SEli Cohen 	if (err)
396216c1975fSMark Bloch 		return err;
3963e126ba97SEli Cohen 
3964c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
3965e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
3966e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
3967a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
3968c8b89924SMark Bloch 
3969f164be8cSYishai Hadas 	if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
3970f164be8cSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) {
3971f164be8cSYishai Hadas 		err = mlx5_ib_init_var_table(dev);
3972f164be8cSYishai Hadas 		if (err)
3973f164be8cSYishai Hadas 			return err;
3974f164be8cSYishai Hadas 	}
3975f164be8cSYishai Hadas 
397696e2fd73SLeon Romanovsky 	dev->ib_dev.use_cq_dim = true;
397796e2fd73SLeon Romanovsky 
397816c1975fSMark Bloch 	return 0;
397916c1975fSMark Bloch }
398016c1975fSMark Bloch 
398196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
398296458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
398396458233SKamal Heib 	.query_port = mlx5_ib_query_port,
398496458233SKamal Heib };
398596458233SKamal Heib 
mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev * dev)39868e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
398716c1975fSMark Bloch {
398896458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
39898e6efa3aSMark Bloch 	return 0;
39908e6efa3aSMark Bloch }
39918e6efa3aSMark Bloch 
399296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
399396458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
399496458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
3995d6fd59e1SParav Pandit 	.query_pkey = mlx5_ib_rep_query_pkey,
399696458233SKamal Heib };
399796458233SKamal Heib 
mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev * dev)3998b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev)
39998e6efa3aSMark Bloch {
400096458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
40018e6efa3aSMark Bloch 	return 0;
40028e6efa3aSMark Bloch }
40038e6efa3aSMark Bloch 
400496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
400596458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
400696458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
400796458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
400896458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
400996458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
401096458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
4011c0a6b5ecSLeon Romanovsky 
4012c0a6b5ecSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_rwq_ind_table, mlx5_ib_rwq_ind_table,
4013c0a6b5ecSLeon Romanovsky 			   ib_rwq_ind_tbl),
401496458233SKamal Heib };
401596458233SKamal Heib 
mlx5_ib_roce_init(struct mlx5_ib_dev * dev)40161e2b5a90SLeon Romanovsky static int mlx5_ib_roce_init(struct mlx5_ib_dev *dev)
40178e6efa3aSMark Bloch {
40181e2b5a90SLeon Romanovsky 	struct mlx5_core_dev *mdev = dev->mdev;
40191e2b5a90SLeon Romanovsky 	enum rdma_link_layer ll;
40201e2b5a90SLeon Romanovsky 	int port_type_cap;
40211fb7f897SMark Bloch 	u32 port_num = 0;
40221e2b5a90SLeon Romanovsky 	int err;
40237fd8aefbSDaniel Jurgens 
40241e2b5a90SLeon Romanovsky 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
40251e2b5a90SLeon Romanovsky 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
40261e2b5a90SLeon Romanovsky 
40271e2b5a90SLeon Romanovsky 	if (ll == IB_LINK_LAYER_ETHERNET) {
402896458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
40298e6efa3aSMark Bloch 
4030e3f1ed1fSLeon Romanovsky 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4031e3f1ed1fSLeon Romanovsky 
403226628e2dSMark Bloch 		/* Register only for native ports */
4033dca55da0SJiri Pirko 		mlx5_mdev_netdev_track(dev, port_num);
40348e6efa3aSMark Bloch 
4035e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
40368e6efa3aSMark Bloch 		if (err)
40378e6efa3aSMark Bloch 			goto cleanup;
40388e6efa3aSMark Bloch 	}
40398e6efa3aSMark Bloch 
404016c1975fSMark Bloch 	return 0;
40418e6efa3aSMark Bloch cleanup:
4042dca55da0SJiri Pirko 	mlx5_mdev_netdev_untrack(dev, port_num);
40438e6efa3aSMark Bloch 	return err;
404445bded2cSKamal Heib }
40456aec21f6SHaggai Eran 
mlx5_ib_roce_cleanup(struct mlx5_ib_dev * dev)40461e2b5a90SLeon Romanovsky static void mlx5_ib_roce_cleanup(struct mlx5_ib_dev *dev)
404716c1975fSMark Bloch {
404816c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
404916c1975fSMark Bloch 	enum rdma_link_layer ll;
405016c1975fSMark Bloch 	int port_type_cap;
40511fb7f897SMark Bloch 	u32 port_num;
40524a2da0b8SParav Pandit 
405316c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
405416c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
405516c1975fSMark Bloch 
405616c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
405716c1975fSMark Bloch 		mlx5_disable_eth(dev);
405816c1975fSMark Bloch 
40591e2b5a90SLeon Romanovsky 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4060dca55da0SJiri Pirko 		mlx5_mdev_netdev_untrack(dev, port_num);
406116c1975fSMark Bloch 	}
406216c1975fSMark Bloch }
406316c1975fSMark Bloch 
mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev * dev)406416c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
406516c1975fSMark Bloch {
406673eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
4067a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
406873eb8f03SGreg Kroah-Hartman 	return 0;
406916c1975fSMark Bloch }
407016c1975fSMark Bloch 
mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev * dev)407116c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
407216c1975fSMark Bloch {
4073a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
4074a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
407516c1975fSMark Bloch }
407616c1975fSMark Bloch 
mlx5_ib_stage_uar_init(struct mlx5_ib_dev * dev)407716c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
407816c1975fSMark Bloch {
40795fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
4080444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
408116c1975fSMark Bloch }
408216c1975fSMark Bloch 
mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev * dev)408316c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
408416c1975fSMark Bloch {
408516c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
408616c1975fSMark Bloch }
408716c1975fSMark Bloch 
mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev * dev)4088fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
408916c1975fSMark Bloch {
409016c1975fSMark Bloch 	int err;
40915fe9dec0SEli Cohen 
40925fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
40935fe9dec0SEli Cohen 	if (err)
409416c1975fSMark Bloch 		return err;
40955fe9dec0SEli Cohen 
40965fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
40975fe9dec0SEli Cohen 	if (err)
40981c3aa6bdSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->bfreg);
40995fe9dec0SEli Cohen 
410016c1975fSMark Bloch 	return err;
410116c1975fSMark Bloch }
41020837e86aSMark Bloch 
mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev * dev)4103fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
410416c1975fSMark Bloch {
410516c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
410616c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
410716c1975fSMark Bloch }
4108e126ba97SEli Cohen 
mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev * dev)4109fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
411016c1975fSMark Bloch {
4111e349f858SJason Gunthorpe 	const char *name;
4112e349f858SJason Gunthorpe 
4113c446d9daSMark Bloch 	if (!mlx5_lag_is_active(dev->mdev))
4114e349f858SJason Gunthorpe 		name = "mlx5_%d";
4115e349f858SJason Gunthorpe 	else
4116e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
4117e0477b34SJason Gunthorpe 	return ib_register_device(&dev->ib_dev, name, &dev->mdev->pdev->dev);
411816c1975fSMark Bloch }
411916c1975fSMark Bloch 
mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev * dev)4120fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
412142cea83fSMark Bloch {
412285f9e38aSLeon Romanovsky 	mlx5_mkey_cache_cleanup(dev);
412304876c12SAharon Landau 	mlx5r_umr_resource_cleanup(dev);
412442cea83fSMark Bloch }
412542cea83fSMark Bloch 
mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev * dev)4126fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
412716c1975fSMark Bloch {
412816c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
412916c1975fSMark Bloch }
413016c1975fSMark Bloch 
mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev * dev)4131fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
413216c1975fSMark Bloch {
41331e2b5a90SLeon Romanovsky 	int ret;
41341e2b5a90SLeon Romanovsky 
413504876c12SAharon Landau 	ret = mlx5r_umr_resource_init(dev);
413604876c12SAharon Landau 	if (ret)
413704876c12SAharon Landau 		return ret;
41381e2b5a90SLeon Romanovsky 
413901137808SAharon Landau 	ret = mlx5_mkey_cache_init(dev);
41406387f269SGeorge Kennedy 	if (ret)
41411e2b5a90SLeon Romanovsky 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
41421e2b5a90SLeon Romanovsky 	return ret;
414316c1975fSMark Bloch }
414416c1975fSMark Bloch 
mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev * dev)414516c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
414616c1975fSMark Bloch {
41471e2b5a90SLeon Romanovsky 	struct dentry *root;
414803404e8aSMaor Gottlieb 
41491e2b5a90SLeon Romanovsky 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
41501e2b5a90SLeon Romanovsky 		return 0;
41511e2b5a90SLeon Romanovsky 
41521e2b5a90SLeon Romanovsky 	mutex_init(&dev->delay_drop.lock);
41531e2b5a90SLeon Romanovsky 	dev->delay_drop.dev = dev;
41541e2b5a90SLeon Romanovsky 	dev->delay_drop.activate = false;
41551e2b5a90SLeon Romanovsky 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
41561e2b5a90SLeon Romanovsky 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
41571e2b5a90SLeon Romanovsky 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
41581e2b5a90SLeon Romanovsky 	atomic_set(&dev->delay_drop.events_cnt, 0);
41591e2b5a90SLeon Romanovsky 
41601e2b5a90SLeon Romanovsky 	if (!mlx5_debugfs_root)
41611e2b5a90SLeon Romanovsky 		return 0;
41621e2b5a90SLeon Romanovsky 
416366771a1cSMoshe Shemesh 	root = debugfs_create_dir("delay_drop", mlx5_debugfs_get_dev_root(dev->mdev));
41641e2b5a90SLeon Romanovsky 	dev->delay_drop.dir_debugfs = root;
41651e2b5a90SLeon Romanovsky 
41661e2b5a90SLeon Romanovsky 	debugfs_create_atomic_t("num_timeout_events", 0400, root,
41671e2b5a90SLeon Romanovsky 				&dev->delay_drop.events_cnt);
41681e2b5a90SLeon Romanovsky 	debugfs_create_atomic_t("num_rqs", 0400, root,
41691e2b5a90SLeon Romanovsky 				&dev->delay_drop.rqs_cnt);
41701e2b5a90SLeon Romanovsky 	debugfs_create_file("timeout", 0600, root, &dev->delay_drop,
41711e2b5a90SLeon Romanovsky 			    &fops_delay_drop_timeout);
417216c1975fSMark Bloch 	return 0;
417316c1975fSMark Bloch }
417416c1975fSMark Bloch 
mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev * dev)417516c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
417616c1975fSMark Bloch {
41771e2b5a90SLeon Romanovsky 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
41781e2b5a90SLeon Romanovsky 		return;
41791e2b5a90SLeon Romanovsky 
41801e2b5a90SLeon Romanovsky 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
41811e2b5a90SLeon Romanovsky 	if (!dev->delay_drop.dir_debugfs)
41821e2b5a90SLeon Romanovsky 		return;
41831e2b5a90SLeon Romanovsky 
41841e2b5a90SLeon Romanovsky 	debugfs_remove_recursive(dev->delay_drop.dir_debugfs);
41851e2b5a90SLeon Romanovsky 	dev->delay_drop.dir_debugfs = NULL;
418616c1975fSMark Bloch }
418716c1975fSMark Bloch 
mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev * dev)4188df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
4189df097a27SSaeed Mahameed {
4190921fcf29SPatrisious Haddad 	struct mlx5_ib_resources *devr = &dev->devr;
4191921fcf29SPatrisious Haddad 	int port;
4192921fcf29SPatrisious Haddad 
4193921fcf29SPatrisious Haddad 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
4194921fcf29SPatrisious Haddad 		INIT_WORK(&devr->ports[port].pkey_change_work,
4195921fcf29SPatrisious Haddad 			  pkey_change_handler);
4196921fcf29SPatrisious Haddad 
4197df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
4198df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
419958dbd642SPatrisious Haddad 
420058dbd642SPatrisious Haddad 	mlx5r_macsec_event_register(dev);
420158dbd642SPatrisious Haddad 
4202df097a27SSaeed Mahameed 	return 0;
4203df097a27SSaeed Mahameed }
4204df097a27SSaeed Mahameed 
mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev * dev)4205df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
4206df097a27SSaeed Mahameed {
4207921fcf29SPatrisious Haddad 	struct mlx5_ib_resources *devr = &dev->devr;
4208921fcf29SPatrisious Haddad 	int port;
4209921fcf29SPatrisious Haddad 
421058dbd642SPatrisious Haddad 	mlx5r_macsec_event_unregister(dev);
4211df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
4212921fcf29SPatrisious Haddad 
4213921fcf29SPatrisious Haddad 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
4214921fcf29SPatrisious Haddad 		cancel_work_sync(&devr->ports[port].pkey_change_work);
4215df097a27SSaeed Mahameed }
4216df097a27SSaeed Mahameed 
__mlx5_ib_remove(struct mlx5_ib_dev * dev,const struct mlx5_ib_profile * profile,int stage)4217b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
421816c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
421916c1975fSMark Bloch 		      int stage)
422016c1975fSMark Bloch {
42214cca96a8SParav Pandit 	dev->ib_active = false;
42224cca96a8SParav Pandit 
422316c1975fSMark Bloch 	/* Number of stages to cleanup */
422416c1975fSMark Bloch 	while (stage) {
422516c1975fSMark Bloch 		stage--;
422616c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
422716c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
422816c1975fSMark Bloch 	}
42294a6dc855SMark Bloch 
4230da796ccbSMark Bloch 	kfree(dev->port);
42314a6dc855SMark Bloch 	ib_dealloc_device(&dev->ib_dev);
423216c1975fSMark Bloch }
423316c1975fSMark Bloch 
__mlx5_ib_add(struct mlx5_ib_dev * dev,const struct mlx5_ib_profile * profile)423493f82444SLeon Romanovsky int __mlx5_ib_add(struct mlx5_ib_dev *dev,
423516c1975fSMark Bloch 		  const struct mlx5_ib_profile *profile)
423616c1975fSMark Bloch {
423716c1975fSMark Bloch 	int err;
423816c1975fSMark Bloch 	int i;
423916c1975fSMark Bloch 
42408d93efb8SMark Bloch 	dev->profile = profile;
42418d93efb8SMark Bloch 
424216c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
424316c1975fSMark Bloch 		if (profile->stage[i].init) {
424416c1975fSMark Bloch 			err = profile->stage[i].init(dev);
424516c1975fSMark Bloch 			if (err)
424616c1975fSMark Bloch 				goto err_out;
424716c1975fSMark Bloch 		}
424816c1975fSMark Bloch 	}
424916c1975fSMark Bloch 
4250e126ba97SEli Cohen 	dev->ib_active = true;
425193f82444SLeon Romanovsky 	return 0;
4252e126ba97SEli Cohen 
425316c1975fSMark Bloch err_out:
425493f82444SLeon Romanovsky 	/* Clean up stages which were initialized */
425593f82444SLeon Romanovsky 	while (i) {
425693f82444SLeon Romanovsky 		i--;
425793f82444SLeon Romanovsky 		if (profile->stage[i].cleanup)
425893f82444SLeon Romanovsky 			profile->stage[i].cleanup(dev);
425993f82444SLeon Romanovsky 	}
426093f82444SLeon Romanovsky 	return -ENOMEM;
4261e126ba97SEli Cohen }
4262e126ba97SEli Cohen 
426316c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
426416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
426516c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
426616c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
4267f7c4ffdaSLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_FS,
4268f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_init,
4269f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_cleanup),
427016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
427116c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
4272f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
42738e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
42748e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
42758e6efa3aSMark Bloch 		     NULL),
427616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
42771e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_init,
42781e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_cleanup),
4279333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
4280333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
4281333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
4282f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4283f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
4284f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
428516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
42861e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_init,
42871e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_cleanup),
428816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
42891e2b5a90SLeon Romanovsky 		     mlx5_ib_odp_init_one,
42901e2b5a90SLeon Romanovsky 		     mlx5_ib_odp_cleanup_one),
429116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
429264825827SLeon Romanovsky 		     mlx5_ib_counters_init,
429364825827SLeon Romanovsky 		     mlx5_ib_counters_cleanup),
429416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
429516c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
429616c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
429716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
429816c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
429916c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
430016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
430116c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
430216c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
430342cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
430442cea83fSMark Bloch 		     NULL,
430542cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
430681773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4307d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_init,
4308d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_cleanup),
430916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
431016c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
431116c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
4312921fcf29SPatrisious Haddad 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4313921fcf29SPatrisious Haddad 		     mlx5_ib_stage_dev_notifier_init,
4314921fcf29SPatrisious Haddad 		     mlx5_ib_stage_dev_notifier_cleanup),
431542cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
431642cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
431742cea83fSMark Bloch 		     NULL),
431816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
431916c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
432016c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
4321b572ebe6SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4322b572ebe6SLeon Romanovsky 		     mlx5_ib_restrack_init,
4323b572ebe6SLeon Romanovsky 		     NULL),
432416c1975fSMark Bloch };
432516c1975fSMark Bloch 
4326b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = {
4327b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
4328b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
4329b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
4330f7c4ffdaSLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_FS,
4331f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_init,
4332f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_cleanup),
4333b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
4334b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
4335f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
4336b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
4337b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_non_default_cb,
4338b5ca15adSMark Bloch 		     NULL),
4339b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
43401e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_init,
43411e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_cleanup),
4342333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
4343333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
4344333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
4345f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4346f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
4347f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
4348b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
43491e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_init,
43501e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_cleanup),
4351b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
435264825827SLeon Romanovsky 		     mlx5_ib_counters_init,
435364825827SLeon Romanovsky 		     mlx5_ib_counters_cleanup),
435479db784eSParav Pandit 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
435579db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_init,
435679db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_cleanup),
4357b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
4358b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
4359b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
4360b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
4361b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
4362b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
436303fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
436403fe2debSDavid S. Miller 		     NULL,
436503fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
43667f575103SMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4367d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_init,
4368d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_cleanup),
4369b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
4370b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
4371b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
4372921fcf29SPatrisious Haddad 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4373921fcf29SPatrisious Haddad 		     mlx5_ib_stage_dev_notifier_init,
4374921fcf29SPatrisious Haddad 		     mlx5_ib_stage_dev_notifier_cleanup),
437503fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
437603fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
437703fe2debSDavid S. Miller 		     NULL),
4378ee4d269eSMaher Sanalla 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
4379ee4d269eSMaher Sanalla 		     mlx5_ib_stage_delay_drop_init,
4380ee4d269eSMaher Sanalla 		     mlx5_ib_stage_delay_drop_cleanup),
4381b572ebe6SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4382b572ebe6SLeon Romanovsky 		     mlx5_ib_restrack_init,
4383b572ebe6SLeon Romanovsky 		     NULL),
4384b5ca15adSMark Bloch };
4385b5ca15adSMark Bloch 
mlx5r_mp_probe(struct auxiliary_device * adev,const struct auxiliary_device_id * id)438693f82444SLeon Romanovsky static int mlx5r_mp_probe(struct auxiliary_device *adev,
438793f82444SLeon Romanovsky 			  const struct auxiliary_device_id *id)
438832f69e4bSDaniel Jurgens {
438993f82444SLeon Romanovsky 	struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
439093f82444SLeon Romanovsky 	struct mlx5_core_dev *mdev = idev->mdev;
439132f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
439232f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
439332f69e4bSDaniel Jurgens 	bool bound = false;
439432f69e4bSDaniel Jurgens 	int err;
439532f69e4bSDaniel Jurgens 
439632f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
439732f69e4bSDaniel Jurgens 	if (!mpi)
439893f82444SLeon Romanovsky 		return -ENOMEM;
439932f69e4bSDaniel Jurgens 
440032f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
440132f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
440232f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
440332f69e4bSDaniel Jurgens 	if (err) {
440432f69e4bSDaniel Jurgens 		kfree(mpi);
440593f82444SLeon Romanovsky 		return err;
440632f69e4bSDaniel Jurgens 	}
440732f69e4bSDaniel Jurgens 
440832f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
440932f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
4410*25e6e9daSPatrisious Haddad 		if (dev->sys_image_guid == mpi->sys_image_guid &&
4411*25e6e9daSPatrisious Haddad 		    mlx5_core_same_coredev_type(dev->mdev, mpi->mdev))
441232f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
441332f69e4bSDaniel Jurgens 
441432f69e4bSDaniel Jurgens 		if (bound) {
441532f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
441697f30d32SMaor Gottlieb 			mpi->ibdev->ib_active = true;
441732f69e4bSDaniel Jurgens 			break;
441832f69e4bSDaniel Jurgens 		}
441932f69e4bSDaniel Jurgens 	}
442032f69e4bSDaniel Jurgens 
442132f69e4bSDaniel Jurgens 	if (!bound) {
442232f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
4423c42260f1SVu Pham 		dev_dbg(mdev->device,
4424c42260f1SVu Pham 			"no suitable IB device found to bind to, added to unaffiliated list.\n");
442532f69e4bSDaniel Jurgens 	}
442632f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
442732f69e4bSDaniel Jurgens 
442827963d3dSDavid E. Box 	auxiliary_set_drvdata(adev, mpi);
442993f82444SLeon Romanovsky 	return 0;
443032f69e4bSDaniel Jurgens }
443132f69e4bSDaniel Jurgens 
mlx5r_mp_remove(struct auxiliary_device * adev)443293f82444SLeon Romanovsky static void mlx5r_mp_remove(struct auxiliary_device *adev)
443316c1975fSMark Bloch {
443493f82444SLeon Romanovsky 	struct mlx5_ib_multiport_info *mpi;
443593f82444SLeon Romanovsky 
443627963d3dSDavid E. Box 	mpi = auxiliary_get_drvdata(adev);
443793f82444SLeon Romanovsky 	mutex_lock(&mlx5_ib_multiport_mutex);
443893f82444SLeon Romanovsky 	if (mpi->ibdev)
443993f82444SLeon Romanovsky 		mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
4440da78fe5fSMaor Gottlieb 	else
444193f82444SLeon Romanovsky 		list_del(&mpi->list);
444293f82444SLeon Romanovsky 	mutex_unlock(&mlx5_ib_multiport_mutex);
444393f82444SLeon Romanovsky 	kfree(mpi);
444493f82444SLeon Romanovsky }
444593f82444SLeon Romanovsky 
mlx5r_probe(struct auxiliary_device * adev,const struct auxiliary_device_id * id)444693f82444SLeon Romanovsky static int mlx5r_probe(struct auxiliary_device *adev,
444793f82444SLeon Romanovsky 		       const struct auxiliary_device_id *id)
444893f82444SLeon Romanovsky {
444993f82444SLeon Romanovsky 	struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
445093f82444SLeon Romanovsky 	struct mlx5_core_dev *mdev = idev->mdev;
445194de879cSMichael Guralnik 	const struct mlx5_ib_profile *profile;
445293f82444SLeon Romanovsky 	int port_type_cap, num_ports, ret;
445332f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
4454b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
4455f0666f1fSBodong Wang 
445632f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
445732f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
445832f69e4bSDaniel Jurgens 
4459da796ccbSMark Bloch 	num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
4460da796ccbSMark Bloch 			MLX5_CAP_GEN(mdev, num_vhca_ports));
4461459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
4462b5ca15adSMark Bloch 	if (!dev)
446393f82444SLeon Romanovsky 		return -ENOMEM;
4464da796ccbSMark Bloch 	dev->port = kcalloc(num_ports, sizeof(*dev->port),
4465da796ccbSMark Bloch 			     GFP_KERNEL);
4466da796ccbSMark Bloch 	if (!dev->port) {
4467a5c9c299SParav Pandit 		ib_dealloc_device(&dev->ib_dev);
446893f82444SLeon Romanovsky 		return -ENOMEM;
4469da796ccbSMark Bloch 	}
4470b5ca15adSMark Bloch 
4471b5ca15adSMark Bloch 	dev->mdev = mdev;
4472da796ccbSMark Bloch 	dev->num_ports = num_ports;
4473b5ca15adSMark Bloch 
44749ca05b0fSMaher Sanalla 	if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_get_roce_state(mdev))
447594de879cSMichael Guralnik 		profile = &raw_eth_profile;
447694de879cSMichael Guralnik 	else
447794de879cSMichael Guralnik 		profile = &pf_profile;
447894de879cSMichael Guralnik 
447993f82444SLeon Romanovsky 	ret = __mlx5_ib_add(dev, profile);
448093f82444SLeon Romanovsky 	if (ret) {
448193f82444SLeon Romanovsky 		kfree(dev->port);
448293f82444SLeon Romanovsky 		ib_dealloc_device(&dev->ib_dev);
448393f82444SLeon Romanovsky 		return ret;
448416c1975fSMark Bloch 	}
448516c1975fSMark Bloch 
448627963d3dSDavid E. Box 	auxiliary_set_drvdata(adev, dev);
448793f82444SLeon Romanovsky 	return 0;
448893f82444SLeon Romanovsky }
448993f82444SLeon Romanovsky 
mlx5r_remove(struct auxiliary_device * adev)449093f82444SLeon Romanovsky static void mlx5r_remove(struct auxiliary_device *adev)
4491e126ba97SEli Cohen {
449232f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
44936aec21f6SHaggai Eran 
449427963d3dSDavid E. Box 	dev = auxiliary_get_drvdata(adev);
449516c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
4496e126ba97SEli Cohen }
4497e126ba97SEli Cohen 
449893f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_mp_id_table[] = {
449993f82444SLeon Romanovsky 	{ .name = MLX5_ADEV_NAME ".multiport", },
450093f82444SLeon Romanovsky 	{},
450193f82444SLeon Romanovsky };
450293f82444SLeon Romanovsky 
450393f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_id_table[] = {
450493f82444SLeon Romanovsky 	{ .name = MLX5_ADEV_NAME ".rdma", },
450593f82444SLeon Romanovsky 	{},
450693f82444SLeon Romanovsky };
450793f82444SLeon Romanovsky 
450893f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_mp_id_table);
450993f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_id_table);
451093f82444SLeon Romanovsky 
451193f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_mp_driver = {
451293f82444SLeon Romanovsky 	.name = "multiport",
451393f82444SLeon Romanovsky 	.probe = mlx5r_mp_probe,
451493f82444SLeon Romanovsky 	.remove = mlx5r_mp_remove,
451593f82444SLeon Romanovsky 	.id_table = mlx5r_mp_id_table,
451693f82444SLeon Romanovsky };
451793f82444SLeon Romanovsky 
451893f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_driver = {
451993f82444SLeon Romanovsky 	.name = "rdma",
452093f82444SLeon Romanovsky 	.probe = mlx5r_probe,
452193f82444SLeon Romanovsky 	.remove = mlx5r_remove,
452293f82444SLeon Romanovsky 	.id_table = mlx5r_id_table,
4523e126ba97SEli Cohen };
4524e126ba97SEli Cohen 
mlx5_ib_init(void)4525e126ba97SEli Cohen static int __init mlx5_ib_init(void)
4526e126ba97SEli Cohen {
452793f82444SLeon Romanovsky 	int ret;
45286aec21f6SHaggai Eran 
45298010d74bSJason Gunthorpe 	xlt_emergency_page = (void *)__get_free_page(GFP_KERNEL);
4530c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
4531d69a24e0SDaniel Jurgens 		return -ENOMEM;
4532d69a24e0SDaniel Jurgens 
4533c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
4534c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
45358010d74bSJason Gunthorpe 		free_page((unsigned long)xlt_emergency_page);
4536c44ef998SIlya Lesokhin 		return -ENOMEM;
4537c44ef998SIlya Lesokhin 	}
4538c44ef998SIlya Lesokhin 
4539312b8f79SMark Zhang 	ret = mlx5_ib_qp_event_init();
4540312b8f79SMark Zhang 	if (ret)
4541312b8f79SMark Zhang 		goto qp_event_err;
4542312b8f79SMark Zhang 
454381713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
454493f82444SLeon Romanovsky 	ret = mlx5r_rep_init();
454593f82444SLeon Romanovsky 	if (ret)
454693f82444SLeon Romanovsky 		goto rep_err;
454793f82444SLeon Romanovsky 	ret = auxiliary_driver_register(&mlx5r_mp_driver);
454893f82444SLeon Romanovsky 	if (ret)
454993f82444SLeon Romanovsky 		goto mp_err;
455093f82444SLeon Romanovsky 	ret = auxiliary_driver_register(&mlx5r_driver);
455193f82444SLeon Romanovsky 	if (ret)
455293f82444SLeon Romanovsky 		goto drv_err;
455393f82444SLeon Romanovsky 	return 0;
45549603b61dSJack Morgenstein 
455593f82444SLeon Romanovsky drv_err:
455693f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_mp_driver);
455793f82444SLeon Romanovsky mp_err:
455893f82444SLeon Romanovsky 	mlx5r_rep_cleanup();
455993f82444SLeon Romanovsky rep_err:
4560312b8f79SMark Zhang 	mlx5_ib_qp_event_cleanup();
4561312b8f79SMark Zhang qp_event_err:
456293f82444SLeon Romanovsky 	destroy_workqueue(mlx5_ib_event_wq);
456393f82444SLeon Romanovsky 	free_page((unsigned long)xlt_emergency_page);
456493f82444SLeon Romanovsky 	return ret;
4565e126ba97SEli Cohen }
4566e126ba97SEli Cohen 
mlx5_ib_cleanup(void)4567e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
4568e126ba97SEli Cohen {
456993f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_driver);
457093f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_mp_driver);
457193f82444SLeon Romanovsky 	mlx5r_rep_cleanup();
457293f82444SLeon Romanovsky 
4573312b8f79SMark Zhang 	mlx5_ib_qp_event_cleanup();
4574d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
45758010d74bSJason Gunthorpe 	free_page((unsigned long)xlt_emergency_page);
4576e126ba97SEli Cohen }
4577e126ba97SEli Cohen 
4578e126ba97SEli Cohen module_init(mlx5_ib_init);
4579e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
4580