xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision d6fd59e14ed2975d9b372876c45a09d76d1e70c9)
1b572ebe6SLeon Romanovsky // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2e126ba97SEli Cohen /*
3b572ebe6SLeon Romanovsky  * Copyright (c) 2013-2020, Mellanox Technologies inc. All rights reserved.
490da7dc8SJianxin Xiong  * Copyright (c) 2020, Intel Corporation. All rights reserved.
5e126ba97SEli Cohen  */
6e126ba97SEli Cohen 
7fe248c3aSMaor Gottlieb #include <linux/debugfs.h>
8adec640eSChristoph Hellwig #include <linux/highmem.h>
9e126ba97SEli Cohen #include <linux/module.h>
10e126ba97SEli Cohen #include <linux/init.h>
11e126ba97SEli Cohen #include <linux/errno.h>
12e126ba97SEli Cohen #include <linux/pci.h>
13e126ba97SEli Cohen #include <linux/dma-mapping.h>
14e126ba97SEli Cohen #include <linux/slab.h>
1524da0016SAriel Levkovich #include <linux/bitmap.h>
16e126ba97SEli Cohen #include <linux/sched.h>
176e84f315SIngo Molnar #include <linux/sched/mm.h>
180881e7bdSIngo Molnar #include <linux/sched/task.h>
197c2344c3SMaor Gottlieb #include <linux/delay.h>
20e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
213f89a643SAchiad Shochat #include <rdma/ib_addr.h>
222811ba51SAchiad Shochat #include <rdma/ib_cache.h>
23ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
241b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
2572c7fe90SPravin Shedge #include <linux/mlx5/fs.h>
26cecae747SMaor Gottlieb #include <linux/mlx5/eswitch.h>
277c2344c3SMaor Gottlieb #include <linux/list.h>
28e126ba97SEli Cohen #include <rdma/ib_smi.h>
29e126ba97SEli Cohen #include <rdma/ib_umem.h>
30cfc1a89eSMaor Gottlieb #include <rdma/lag.h>
31038d2ef8SMaor Gottlieb #include <linux/in.h>
32038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
33e126ba97SEli Cohen #include "mlx5_ib.h"
34fc385b7aSMark Bloch #include "ib_rep.h"
35e1f24a79SParav Pandit #include "cmd.h"
36d8b7515eSLeon Romanovsky #include "devx.h"
37f7c4ffdaSLeon Romanovsky #include "fs.h"
38f3da6577SLeon Romanovsky #include "srq.h"
39333fbaa0SLeon Romanovsky #include "qp.h"
40029e88fdSLeon Romanovsky #include "wr.h"
41b572ebe6SLeon Romanovsky #include "restrack.h"
4264825827SLeon Romanovsky #include "counters.h"
43c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h>
448c84660bSMatan Barak #include <rdma/uverbs_std_types.h>
45c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h>
46c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h>
474061ff7aSErez Alfasi #include <rdma/ib_umem_odp.h>
488c84660bSMatan Barak 
498c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
508c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
51e126ba97SEli Cohen 
52e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
53daeee976SShay Drory MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) IB driver");
54e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
55e126ba97SEli Cohen 
56d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
57d69a24e0SDaniel Jurgens 	struct work_struct	work;
58df097a27SSaeed Mahameed 	union {
59df097a27SSaeed Mahameed 		struct mlx5_ib_dev	      *dev;
60df097a27SSaeed Mahameed 		struct mlx5_ib_multiport_info *mpi;
61df097a27SSaeed Mahameed 	};
62df097a27SSaeed Mahameed 	bool			is_slave;
63134e9349SSaeed Mahameed 	unsigned int		event;
64df097a27SSaeed Mahameed 	void			*param;
65d69a24e0SDaniel Jurgens };
66d69a24e0SDaniel Jurgens 
67da7525d2SEran Ben Elisha enum {
68da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
69da7525d2SEran Ben Elisha };
701b5daf11SMajd Dibbiny 
71d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
7232f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
7332f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
7432f69e4bSDaniel Jurgens /*
7532f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
7632f69e4bSDaniel Jurgens  */
7732f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
7832f69e4bSDaniel Jurgens 
7932f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
8032f69e4bSDaniel Jurgens {
8132f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
8232f69e4bSDaniel Jurgens 
8332f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
8432f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
8532f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
8632f69e4bSDaniel Jurgens 	return dev;
8732f69e4bSDaniel Jurgens }
8832f69e4bSDaniel Jurgens 
891b5daf11SMajd Dibbiny static enum rdma_link_layer
90ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
911b5daf11SMajd Dibbiny {
92ebd61f68SAchiad Shochat 	switch (port_type_cap) {
931b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
941b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
951b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
961b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
971b5daf11SMajd Dibbiny 	default:
981b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
991b5daf11SMajd Dibbiny 	}
1001b5daf11SMajd Dibbiny }
1011b5daf11SMajd Dibbiny 
102ebd61f68SAchiad Shochat static enum rdma_link_layer
103ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
104ebd61f68SAchiad Shochat {
105ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
106ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
107ebd61f68SAchiad Shochat 
108ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
109ebd61f68SAchiad Shochat }
110ebd61f68SAchiad Shochat 
111fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
112fd65f1b8SMoni Shoua 			  u8 port_num,
113fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
114fd65f1b8SMoni Shoua {
115fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
116fd65f1b8SMoni Shoua 	int ret;
117fd65f1b8SMoni Shoua 
118fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1193023a1e9SKamal Heib 	ret = ibdev->ops.query_port(ibdev, port_num, &attr);
120fd65f1b8SMoni Shoua 	if (!ret)
121fd65f1b8SMoni Shoua 		*state = attr.state;
122fd65f1b8SMoni Shoua 	return ret;
123fd65f1b8SMoni Shoua }
124fd65f1b8SMoni Shoua 
12535b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
12635b0aa67SMark Bloch 					   struct net_device *ndev,
12735b0aa67SMark Bloch 					   u8 *port_num)
12835b0aa67SMark Bloch {
12935b0aa67SMark Bloch 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
13035b0aa67SMark Bloch 	struct net_device *rep_ndev;
13135b0aa67SMark Bloch 	struct mlx5_ib_port *port;
13235b0aa67SMark Bloch 	int i;
13335b0aa67SMark Bloch 
13435b0aa67SMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
13535b0aa67SMark Bloch 		port  = &dev->port[i];
13635b0aa67SMark Bloch 		if (!port->rep)
13735b0aa67SMark Bloch 			continue;
13835b0aa67SMark Bloch 
13935b0aa67SMark Bloch 		read_lock(&port->roce.netdev_lock);
14035b0aa67SMark Bloch 		rep_ndev = mlx5_ib_get_rep_netdev(esw,
14135b0aa67SMark Bloch 						  port->rep->vport);
14235b0aa67SMark Bloch 		if (rep_ndev == ndev) {
14335b0aa67SMark Bloch 			read_unlock(&port->roce.netdev_lock);
14435b0aa67SMark Bloch 			*port_num = i + 1;
14535b0aa67SMark Bloch 			return &port->roce;
14635b0aa67SMark Bloch 		}
14735b0aa67SMark Bloch 		read_unlock(&port->roce.netdev_lock);
14835b0aa67SMark Bloch 	}
14935b0aa67SMark Bloch 
15035b0aa67SMark Bloch 	return NULL;
15135b0aa67SMark Bloch }
15235b0aa67SMark Bloch 
153fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
154fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
155fc24fc5eSAchiad Shochat {
1567fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
157fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1587fd8aefbSDaniel Jurgens 	u8 port_num = roce->native_port_num;
1597fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1607fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1617fd8aefbSDaniel Jurgens 
1627fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
16332f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
16432f69e4bSDaniel Jurgens 	if (!mdev)
16532f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
166fc24fc5eSAchiad Shochat 
1675ec8c83eSAviv Heller 	switch (event) {
1685ec8c83eSAviv Heller 	case NETDEV_REGISTER:
16935b0aa67SMark Bloch 		/* Should already be registered during the load */
17035b0aa67SMark Bloch 		if (ibdev->is_rep)
17135b0aa67SMark Bloch 			break;
1727fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
173dce45af5SLinus Torvalds 		if (ndev->dev.parent == mdev->device)
174842a9c83SOr Gerlitz 			roce->netdev = ndev;
1757fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
1765ec8c83eSAviv Heller 		break;
1775ec8c83eSAviv Heller 
178842a9c83SOr Gerlitz 	case NETDEV_UNREGISTER:
17935b0aa67SMark Bloch 		/* In case of reps, ib device goes away before the netdevs */
180842a9c83SOr Gerlitz 		write_lock(&roce->netdev_lock);
181842a9c83SOr Gerlitz 		if (roce->netdev == ndev)
182842a9c83SOr Gerlitz 			roce->netdev = NULL;
183842a9c83SOr Gerlitz 		write_unlock(&roce->netdev_lock);
184842a9c83SOr Gerlitz 		break;
185842a9c83SOr Gerlitz 
186fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
1875ec8c83eSAviv Heller 	case NETDEV_UP:
18888621dfeSAviv Heller 	case NETDEV_DOWN: {
1897fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
19088621dfeSAviv Heller 		struct net_device *upper = NULL;
19188621dfeSAviv Heller 
19288621dfeSAviv Heller 		if (lag_ndev) {
19388621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
19488621dfeSAviv Heller 			dev_put(lag_ndev);
19588621dfeSAviv Heller 		}
19688621dfeSAviv Heller 
19735b0aa67SMark Bloch 		if (ibdev->is_rep)
19835b0aa67SMark Bloch 			roce = mlx5_get_rep_roce(ibdev, ndev, &port_num);
19935b0aa67SMark Bloch 		if (!roce)
20035b0aa67SMark Bloch 			return NOTIFY_DONE;
2017fd8aefbSDaniel Jurgens 		if ((upper == ndev || (!upper && ndev == roce->netdev))
20288621dfeSAviv Heller 		    && ibdev->ib_active) {
203626bc02dSBart Van Assche 			struct ib_event ibev = { };
204fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2055ec8c83eSAviv Heller 
2067fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2077fd8aefbSDaniel Jurgens 					   &port_state))
2087fd8aefbSDaniel Jurgens 				goto done;
209fd65f1b8SMoni Shoua 
2107fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2117fd8aefbSDaniel Jurgens 				goto done;
212fd65f1b8SMoni Shoua 
2137fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2145ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
215fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
216fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
217fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
218fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
219fd65f1b8SMoni Shoua 			else
2207fd8aefbSDaniel Jurgens 				goto done;
221fd65f1b8SMoni Shoua 
2227fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2235ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2245ec8c83eSAviv Heller 		}
2255ec8c83eSAviv Heller 		break;
22688621dfeSAviv Heller 	}
2275ec8c83eSAviv Heller 
2285ec8c83eSAviv Heller 	default:
2295ec8c83eSAviv Heller 		break;
2305ec8c83eSAviv Heller 	}
2317fd8aefbSDaniel Jurgens done:
23232f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
233fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
234fc24fc5eSAchiad Shochat }
235fc24fc5eSAchiad Shochat 
236fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
237fc24fc5eSAchiad Shochat 					     u8 port_num)
238fc24fc5eSAchiad Shochat {
239fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
240fc24fc5eSAchiad Shochat 	struct net_device *ndev;
24132f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
242fc24fc5eSAchiad Shochat 
24332f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
24432f69e4bSDaniel Jurgens 	if (!mdev)
24532f69e4bSDaniel Jurgens 		return NULL;
24632f69e4bSDaniel Jurgens 
24732f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
24888621dfeSAviv Heller 	if (ndev)
24932f69e4bSDaniel Jurgens 		goto out;
25088621dfeSAviv Heller 
251fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
252fc24fc5eSAchiad Shochat 	 */
25395579e78SMark Bloch 	read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
25495579e78SMark Bloch 	ndev = ibdev->port[port_num - 1].roce.netdev;
255fc24fc5eSAchiad Shochat 	if (ndev)
256fc24fc5eSAchiad Shochat 		dev_hold(ndev);
25795579e78SMark Bloch 	read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
258fc24fc5eSAchiad Shochat 
25932f69e4bSDaniel Jurgens out:
26032f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
261fc24fc5eSAchiad Shochat 	return ndev;
262fc24fc5eSAchiad Shochat }
263fc24fc5eSAchiad Shochat 
26432f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
26532f69e4bSDaniel Jurgens 						   u8 ib_port_num,
26632f69e4bSDaniel Jurgens 						   u8 *native_port_num)
26732f69e4bSDaniel Jurgens {
26832f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
26932f69e4bSDaniel Jurgens 							  ib_port_num);
27032f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
27132f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
27232f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
27332f69e4bSDaniel Jurgens 
274210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
275210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
276210b1f78SMark Bloch 		if (native_port_num)
277210b1f78SMark Bloch 			*native_port_num = ib_port_num;
278210b1f78SMark Bloch 		return ibdev->mdev;
279210b1f78SMark Bloch 	}
280210b1f78SMark Bloch 
28132f69e4bSDaniel Jurgens 	if (native_port_num)
28232f69e4bSDaniel Jurgens 		*native_port_num = 1;
28332f69e4bSDaniel Jurgens 
28432f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
28532f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
28632f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
28732f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
28832f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
28932f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
29032f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
29132f69e4bSDaniel Jurgens 		 */
29232f69e4bSDaniel Jurgens 		if (!mpi->is_master)
29332f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
29432f69e4bSDaniel Jurgens 	}
29532f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
29632f69e4bSDaniel Jurgens 
29732f69e4bSDaniel Jurgens 	return mdev;
29832f69e4bSDaniel Jurgens }
29932f69e4bSDaniel Jurgens 
30032f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
30132f69e4bSDaniel Jurgens {
30232f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
30332f69e4bSDaniel Jurgens 							  port_num);
30432f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
30532f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
30632f69e4bSDaniel Jurgens 
30732f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
30832f69e4bSDaniel Jurgens 		return;
30932f69e4bSDaniel Jurgens 
31032f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
31132f69e4bSDaniel Jurgens 
31232f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
31332f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
31432f69e4bSDaniel Jurgens 	if (mpi->is_master)
31532f69e4bSDaniel Jurgens 		goto out;
31632f69e4bSDaniel Jurgens 
31732f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
31832f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
31932f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
32032f69e4bSDaniel Jurgens out:
32132f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
32232f69e4bSDaniel Jurgens }
32332f69e4bSDaniel Jurgens 
324639bf441SAharon Landau static int translate_eth_legacy_proto_oper(u32 eth_proto_oper,
325639bf441SAharon Landau 					   u16 *active_speed, u8 *active_width)
326f1b65df5SNoa Osherovich {
327f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
328f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
329f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
330f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
331f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
332f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
333f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
334f1b65df5SNoa Osherovich 		break;
335f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
336f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
337f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
338f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
339f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
340f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
341f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
342f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
343f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
344f1b65df5SNoa Osherovich 		break;
345f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
346f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
347f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
348f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
349f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
350f1b65df5SNoa Osherovich 		break;
351f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
352f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
353f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
354f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
355f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
356f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
357f1b65df5SNoa Osherovich 		break;
358f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
359f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
360f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
361f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
362f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
363f1b65df5SNoa Osherovich 		break;
364f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
365f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
366f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
367f1b65df5SNoa Osherovich 		break;
368f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
369f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
370f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
371f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
372f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
373f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
374f1b65df5SNoa Osherovich 		break;
375f1b65df5SNoa Osherovich 	default:
376f1b65df5SNoa Osherovich 		return -EINVAL;
377f1b65df5SNoa Osherovich 	}
378f1b65df5SNoa Osherovich 
379f1b65df5SNoa Osherovich 	return 0;
380f1b65df5SNoa Osherovich }
381f1b65df5SNoa Osherovich 
382639bf441SAharon Landau static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u16 *active_speed,
38308e8676fSAya Levin 					u8 *active_width)
38408e8676fSAya Levin {
38508e8676fSAya Levin 	switch (eth_proto_oper) {
38608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
38708e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
38808e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
38908e8676fSAya Levin 		*active_speed = IB_SPEED_SDR;
39008e8676fSAya Levin 		break;
39108e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
39208e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
39308e8676fSAya Levin 		*active_speed = IB_SPEED_DDR;
39408e8676fSAya Levin 		break;
39508e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
39608e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
39708e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
39808e8676fSAya Levin 		break;
39908e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
40008e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
40108e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
40208e8676fSAya Levin 		break;
40308e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
40408e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
40508e8676fSAya Levin 		*active_speed = IB_SPEED_EDR;
40608e8676fSAya Levin 		break;
40708e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
408cd272875SAya Levin 		*active_width = IB_WIDTH_2X;
409cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
410cd272875SAya Levin 		break;
41108e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
41208e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
41308e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
41408e8676fSAya Levin 		break;
415cd272875SAya Levin 	case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
416cd272875SAya Levin 		*active_width = IB_WIDTH_4X;
417cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
418cd272875SAya Levin 		break;
41908e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
42008e8676fSAya Levin 		*active_width = IB_WIDTH_2X;
42108e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
42208e8676fSAya Levin 		break;
423f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_100GAUI_1_100GBASE_CR_KR):
424f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_1X;
425f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
426f946e45fSMeir Lichtinger 		break;
42708e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
42808e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
42908e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
43008e8676fSAya Levin 		break;
431f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_200GAUI_2_200GBASE_CR2_KR2):
432f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_2X;
433f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
434f946e45fSMeir Lichtinger 		break;
435f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_400GAUI_4_400GBASE_CR4_KR4):
436f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_4X;
437f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
438f946e45fSMeir Lichtinger 		break;
43908e8676fSAya Levin 	default:
44008e8676fSAya Levin 		return -EINVAL;
44108e8676fSAya Levin 	}
44208e8676fSAya Levin 
44308e8676fSAya Levin 	return 0;
44408e8676fSAya Levin }
44508e8676fSAya Levin 
446639bf441SAharon Landau static int translate_eth_proto_oper(u32 eth_proto_oper, u16 *active_speed,
44708e8676fSAya Levin 				    u8 *active_width, bool ext)
44808e8676fSAya Levin {
44908e8676fSAya Levin 	return ext ?
45008e8676fSAya Levin 		translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
45108e8676fSAya Levin 					     active_width) :
45208e8676fSAya Levin 		translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
45308e8676fSAya Levin 						active_width);
45408e8676fSAya Levin }
45508e8676fSAya Levin 
456095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
4573f89a643SAchiad Shochat 				struct ib_port_attr *props)
4583f89a643SAchiad Shochat {
4593f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
460bc4e12ffSAya Levin 	u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
461da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
46288621dfeSAviv Heller 	struct net_device *ndev, *upper;
4633f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
464b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
465c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
466f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
467b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
46808e8676fSAya Levin 	bool ext;
469095b0927SIlan Tayari 	int err;
4703f89a643SAchiad Shochat 
471b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
472b3cbd6f0SDaniel Jurgens 	if (!mdev) {
473b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
474b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
475b3cbd6f0SDaniel Jurgens 		 */
476b3cbd6f0SDaniel Jurgens 		put_mdev = false;
477b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
478b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
479b3cbd6f0SDaniel Jurgens 		port_num = 1;
480b3cbd6f0SDaniel Jurgens 	}
481b3cbd6f0SDaniel Jurgens 
482f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
483f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
48426628e2dSMark Bloch 	 * Use native port in case of reps
48550f22fd8SNoa Osherovich 	 */
48626628e2dSMark Bloch 	if (dev->is_rep)
48726628e2dSMark Bloch 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
48826628e2dSMark Bloch 					   1);
48926628e2dSMark Bloch 	else
490bc4e12ffSAya Levin 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
491b3cbd6f0SDaniel Jurgens 					   mdev_port_num);
492095b0927SIlan Tayari 	if (err)
493b3cbd6f0SDaniel Jurgens 		goto out;
494530c8632SAya Levin 	ext = !!MLX5_GET_ETH_PROTO(ptys_reg, out, true, eth_proto_capability);
49508e8676fSAya Levin 	eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
496f1b65df5SNoa Osherovich 
4977672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
4987672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
4997672ed33SHonggang Li 
500f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
50108e8676fSAya Levin 				 &props->active_width, ext);
5023f89a643SAchiad Shochat 
5033f89a643SAchiad Shochat 	props->port_cap_flags |= IB_PORT_CM_SUP;
5042f944c0fSJason Gunthorpe 	props->ip_gids = true;
5053f89a643SAchiad Shochat 
5063f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
5073f89a643SAchiad Shochat 						roce_address_table_size);
5083f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
5093f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
5103f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
5113f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
51272a7720fSKamal Heib 	props->phys_state       = IB_PORT_PHYS_STATE_DISABLED;
5133f89a643SAchiad Shochat 
514b3cbd6f0SDaniel Jurgens 	mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
515c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
5163f89a643SAchiad Shochat 
517b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
518b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
519b3cbd6f0SDaniel Jurgens 		goto out;
520b3cbd6f0SDaniel Jurgens 
5213f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
5223f89a643SAchiad Shochat 	if (!ndev)
523b3cbd6f0SDaniel Jurgens 		goto out;
5243f89a643SAchiad Shochat 
5257c34ec19SAviv Heller 	if (dev->lag_active) {
52688621dfeSAviv Heller 		rcu_read_lock();
52788621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
52888621dfeSAviv Heller 		if (upper) {
52988621dfeSAviv Heller 			dev_put(ndev);
53088621dfeSAviv Heller 			ndev = upper;
53188621dfeSAviv Heller 			dev_hold(ndev);
53288621dfeSAviv Heller 		}
53388621dfeSAviv Heller 		rcu_read_unlock();
53488621dfeSAviv Heller 	}
53588621dfeSAviv Heller 
5363f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
5373f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
53872a7720fSKamal Heib 		props->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
5393f89a643SAchiad Shochat 	}
5403f89a643SAchiad Shochat 
5413f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
5423f89a643SAchiad Shochat 
5433f89a643SAchiad Shochat 	dev_put(ndev);
5443f89a643SAchiad Shochat 
5453f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
546b3cbd6f0SDaniel Jurgens out:
547b3cbd6f0SDaniel Jurgens 	if (put_mdev)
548b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
549b3cbd6f0SDaniel Jurgens 	return err;
5503f89a643SAchiad Shochat }
5513f89a643SAchiad Shochat 
552095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
553095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
554095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
5553cca2606SAchiad Shochat {
5561c15b4f2SAvihai Horon 	enum ib_gid_type gid_type = IB_GID_TYPE_ROCE;
557a70c0739SParav Pandit 	u16 vlan_id = 0xffff;
558095b0927SIlan Tayari 	u8 roce_version = 0;
559095b0927SIlan Tayari 	u8 roce_l3_type = 0;
560095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
561a70c0739SParav Pandit 	int ret;
5623cca2606SAchiad Shochat 
563095b0927SIlan Tayari 	if (gid) {
564095b0927SIlan Tayari 		gid_type = attr->gid_type;
565a70c0739SParav Pandit 		ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
566a70c0739SParav Pandit 		if (ret)
567a70c0739SParav Pandit 			return ret;
5683cca2606SAchiad Shochat 	}
5693cca2606SAchiad Shochat 
570095b0927SIlan Tayari 	switch (gid_type) {
5711c15b4f2SAvihai Horon 	case IB_GID_TYPE_ROCE:
572095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
5733cca2606SAchiad Shochat 		break;
5743cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
575095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
576095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
577095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
578095b0927SIlan Tayari 		else
579095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
5803cca2606SAchiad Shochat 		break;
5813cca2606SAchiad Shochat 
5823cca2606SAchiad Shochat 	default:
583095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
5843cca2606SAchiad Shochat 	}
5853cca2606SAchiad Shochat 
586095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
587cf34e1feSParav Pandit 				      roce_l3_type, gid->raw, mac,
588a70c0739SParav Pandit 				      vlan_id < VLAN_CFI_MASK, vlan_id,
589cf34e1feSParav Pandit 				      port_num);
5903cca2606SAchiad Shochat }
5913cca2606SAchiad Shochat 
592f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
5933cca2606SAchiad Shochat 			   __always_unused void **context)
5943cca2606SAchiad Shochat {
595414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
596f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
5973cca2606SAchiad Shochat }
5983cca2606SAchiad Shochat 
599414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
600414448d2SParav Pandit 			   __always_unused void **context)
6013cca2606SAchiad Shochat {
602414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
603414448d2SParav Pandit 			     attr->index, NULL, NULL);
6043cca2606SAchiad Shochat }
6053cca2606SAchiad Shochat 
6065ac55dfcSMark Zhang __be16 mlx5_get_roce_udp_sport_min(const struct mlx5_ib_dev *dev,
60747ec3866SParav Pandit 				   const struct ib_gid_attr *attr)
6082811ba51SAchiad Shochat {
60947ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
6102811ba51SAchiad Shochat 		return 0;
6112811ba51SAchiad Shochat 
6122811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
6132811ba51SAchiad Shochat }
6142811ba51SAchiad Shochat 
6151b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
6161b5daf11SMajd Dibbiny {
6177fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
618d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
6197fae6655SNoa Osherovich 	return 0;
6201b5daf11SMajd Dibbiny }
6211b5daf11SMajd Dibbiny 
6221b5daf11SMajd Dibbiny enum {
6231b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
6241b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
6251b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
6261b5daf11SMajd Dibbiny };
6271b5daf11SMajd Dibbiny 
6281b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
6291b5daf11SMajd Dibbiny {
6301b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
6311b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
6321b5daf11SMajd Dibbiny 
633ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
6341b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
6351b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
6361b5daf11SMajd Dibbiny 
6371b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
6381b5daf11SMajd Dibbiny }
6391b5daf11SMajd Dibbiny 
640da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
641776a3906SMoni Shoua 			    u8 atomic_size_qp,
642da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
643da7525d2SEran Ben Elisha {
644da7525d2SEran Ben Elisha 	u8 tmp;
645da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
646da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
647bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
648da7525d2SEran Ben Elisha 
649da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
650da7525d2SEran Ben Elisha 	 * of host endianness respond
651da7525d2SEran Ben Elisha 	 */
652da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
653da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
654da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
655da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
656da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
657da7525d2SEran Ben Elisha 	} else {
658da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
659da7525d2SEran Ben Elisha 	}
660da7525d2SEran Ben Elisha }
661da7525d2SEran Ben Elisha 
662776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
663776a3906SMoni Shoua 			       struct ib_device_attr *props)
664776a3906SMoni Shoua {
665776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
666776a3906SMoni Shoua 
667776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
668776a3906SMoni Shoua }
669776a3906SMoni Shoua 
6701b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
6711b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
6721b5daf11SMajd Dibbiny {
6731b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6741b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6751b5daf11SMajd Dibbiny 	u64 tmp;
6761b5daf11SMajd Dibbiny 	int err;
6771b5daf11SMajd Dibbiny 
6781b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6791b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6801b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
6811b5daf11SMajd Dibbiny 							    sys_image_guid);
6821b5daf11SMajd Dibbiny 
6831b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6841b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
6853f89a643SAchiad Shochat 		break;
6863f89a643SAchiad Shochat 
6873f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6883f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
6893f89a643SAchiad Shochat 		break;
6901b5daf11SMajd Dibbiny 
6911b5daf11SMajd Dibbiny 	default:
6921b5daf11SMajd Dibbiny 		return -EINVAL;
6931b5daf11SMajd Dibbiny 	}
6943f89a643SAchiad Shochat 
6953f89a643SAchiad Shochat 	if (!err)
6963f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
6973f89a643SAchiad Shochat 
6983f89a643SAchiad Shochat 	return err;
6993f89a643SAchiad Shochat 
7001b5daf11SMajd Dibbiny }
7011b5daf11SMajd Dibbiny 
7021b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
7031b5daf11SMajd Dibbiny 				u16 *max_pkeys)
7041b5daf11SMajd Dibbiny {
7051b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7061b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
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_max_pkeys(ibdev, max_pkeys);
7111b5daf11SMajd Dibbiny 
7121b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7131b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7141b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
7151b5daf11SMajd Dibbiny 						pkey_table_size));
7161b5daf11SMajd Dibbiny 		return 0;
7171b5daf11SMajd Dibbiny 
7181b5daf11SMajd Dibbiny 	default:
7191b5daf11SMajd Dibbiny 		return -EINVAL;
7201b5daf11SMajd Dibbiny 	}
7211b5daf11SMajd Dibbiny }
7221b5daf11SMajd Dibbiny 
7231b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
7241b5daf11SMajd Dibbiny 				u32 *vendor_id)
7251b5daf11SMajd Dibbiny {
7261b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7271b5daf11SMajd Dibbiny 
7281b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7291b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7301b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
7311b5daf11SMajd Dibbiny 
7321b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7331b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7341b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
7351b5daf11SMajd Dibbiny 
7361b5daf11SMajd Dibbiny 	default:
7371b5daf11SMajd Dibbiny 		return -EINVAL;
7381b5daf11SMajd Dibbiny 	}
7391b5daf11SMajd Dibbiny }
7401b5daf11SMajd Dibbiny 
7411b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
7421b5daf11SMajd Dibbiny 				__be64 *node_guid)
7431b5daf11SMajd Dibbiny {
7441b5daf11SMajd Dibbiny 	u64 tmp;
7451b5daf11SMajd Dibbiny 	int err;
7461b5daf11SMajd Dibbiny 
7471b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7481b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7491b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7501b5daf11SMajd Dibbiny 
7511b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7521b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7533f89a643SAchiad Shochat 		break;
7543f89a643SAchiad Shochat 
7553f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7563f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7573f89a643SAchiad Shochat 		break;
7581b5daf11SMajd Dibbiny 
7591b5daf11SMajd Dibbiny 	default:
7601b5daf11SMajd Dibbiny 		return -EINVAL;
7611b5daf11SMajd Dibbiny 	}
7623f89a643SAchiad Shochat 
7633f89a643SAchiad Shochat 	if (!err)
7643f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7653f89a643SAchiad Shochat 
7663f89a643SAchiad Shochat 	return err;
7671b5daf11SMajd Dibbiny }
7681b5daf11SMajd Dibbiny 
7691b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
770bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
7711b5daf11SMajd Dibbiny };
7721b5daf11SMajd Dibbiny 
7731b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
7741b5daf11SMajd Dibbiny {
7751b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
7761b5daf11SMajd Dibbiny 
7771b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
7781b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
7791b5daf11SMajd Dibbiny 
7801b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
7811b5daf11SMajd Dibbiny 
7821b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
7831b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
7841b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
7851b5daf11SMajd Dibbiny }
7861b5daf11SMajd Dibbiny 
787e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
7882528e33eSMatan Barak 				struct ib_device_attr *props,
7892528e33eSMatan Barak 				struct ib_udata *uhw)
790e126ba97SEli Cohen {
79148357091SLeon Romanovsky 	size_t uhw_outlen = (uhw) ? uhw->outlen : 0;
792e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
793938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
794e126ba97SEli Cohen 	int err = -ENOMEM;
795288c01b7SEli Cohen 	int max_sq_desc;
796e126ba97SEli Cohen 	int max_rq_sg;
797e126ba97SEli Cohen 	int max_sq_sg;
798e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
79985c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
800402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
801402ca536SBodong Wang 	size_t resp_len;
802402ca536SBodong Wang 	u64 max_tso;
803e126ba97SEli Cohen 
804402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
80548357091SLeon Romanovsky 	if (uhw_outlen && uhw_outlen < resp_len)
806402ca536SBodong Wang 		return -EINVAL;
8076f26b2acSErez Alfasi 
808402ca536SBodong Wang 	resp.response_length = resp_len;
809402ca536SBodong Wang 
81048357091SLeon Romanovsky 	if (uhw && uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
8112528e33eSMatan Barak 		return -EINVAL;
8122528e33eSMatan Barak 
813e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
8141b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
8151b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
8161b5daf11SMajd Dibbiny 	if (err)
8171b5daf11SMajd Dibbiny 		return err;
8181b5daf11SMajd Dibbiny 
8191b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
8201b5daf11SMajd Dibbiny 	if (err)
8211b5daf11SMajd Dibbiny 		return err;
8221b5daf11SMajd Dibbiny 
8231b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
8241b5daf11SMajd Dibbiny 	if (err)
8251b5daf11SMajd Dibbiny 		return err;
826e126ba97SEli Cohen 
8279603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
8289603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
8299603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
830e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
831e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
832e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
8331a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
834938fe83cSSaeed Mahameed 
835938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
836e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
837938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
838e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
839938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
840e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
841938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
842e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
843d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
844d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
845d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
846d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
847b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
848b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
849d2370e0aSMatan Barak 	}
8500ec52f01SJason Gunthorpe 	/* IB_WR_REG_MR always requires changing the entity size with UMR */
8510ec52f01SJason Gunthorpe 	if (!MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled))
852e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
853938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
854c0a6cbb9SIsrael Rukshin 		props->device_cap_flags |= IB_DEVICE_INTEGRITY_HANDOVER;
8552dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8562dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8572dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8582dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8592dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8602dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8612dea9094SSagi Grimberg 	}
862938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
863f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
864e126ba97SEli Cohen 
86585c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
866e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
867e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
86888115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
869e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
870e8161334SNoa Osherovich 		}
871e8161334SNoa Osherovich 
872e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
873e8161334SNoa Osherovich 			props->raw_packet_caps |=
874e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
87588115fe7SBodong Wang 
876a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) {
877402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
878402ca536SBodong Wang 			if (max_tso) {
879402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
880402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
881402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
882402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
883402ca536SBodong Wang 			}
884402ca536SBodong Wang 		}
88531f69a82SYishai Hadas 
886a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) {
88731f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
88831f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
88931f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
89031f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
89131f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
89231f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
89331f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
89431f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
89531f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
89631f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
8974e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
8984e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
8992d93fc85SMatan Barak 			if (mlx5_accel_ipsec_device_caps(dev->mdev) &
9002d93fc85SMatan Barak 			    MLX5_ACCEL_IPSEC_CAP_DEVICE)
9012d93fc85SMatan Barak 				resp.rss_caps.rx_hash_fields_mask |=
9022d93fc85SMatan Barak 					MLX5_RX_HASH_IPSEC_SPI;
90331f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
90431f69a82SYishai Hadas 		}
90531f69a82SYishai Hadas 	} else {
906a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen)
90731f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
908a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen)
90931f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
910402ca536SBodong Wang 	}
911402ca536SBodong Wang 
912f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
913f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
914f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
915f0313965SErez Shitrit 	}
916f0313965SErez Shitrit 
91703404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
91885c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
91985c7c014SDaniel Jurgens 	    raw_support)
92003404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
92103404e8aSMaor Gottlieb 
9221d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
9231d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
9241d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
9251d54f890SYishai Hadas 
926cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
92785c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
92885c7c014SDaniel Jurgens 	    raw_support) {
929e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
930cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
931e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
932e8161334SNoa Osherovich 	}
933cff5a0f3SMajd Dibbiny 
93424da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
93524da0016SAriel Levkovich 		props->max_dm_size =
93624da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
93724da0016SAriel Levkovich 	}
93824da0016SAriel Levkovich 
939da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
940da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
941da6d6ba3SMaor Gottlieb 
942b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
943b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
944b1383aa6SNoa Osherovich 
9451b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
9461b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
947e126ba97SEli Cohen 
948e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
949e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
950938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
951938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
952938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
953938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
954288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
955288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
956288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
957e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
95833023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
95933023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
960986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
961938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9629f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
963938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
964938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
965938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
966938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
967938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
968938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
969938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
970e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
971e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
972911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
973911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
97462e3c379SMax Gurtovoy 	props->max_pi_fast_reg_page_list_len =
97562e3c379SMax Gurtovoy 		props->max_fast_reg_page_list_len / 2;
97636609056SYamin Friedman 	props->max_sgl_rd =
97736609056SYamin Friedman 		MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance);
978776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
97981bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
980938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
981938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
982e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
983e126ba97SEli Cohen 					   props->max_mcast_grp;
98486695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
9857c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
9867c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
987e126ba97SEli Cohen 
988e502b8b0SLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
98900815752SMoni Shoua 		if (dev->odp_caps.general_caps & IB_ODP_SUPPORT)
9908cdd312cSHaggai Eran 			props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
9918cdd312cSHaggai Eran 		props->odp_caps = dev->odp_caps;
992a73a8955SMoni Shoua 		if (!uhw) {
993a73a8955SMoni Shoua 			/* ODP for kernel QPs is not implemented for receive
994a73a8955SMoni Shoua 			 * WQEs and SRQ WQEs
995a73a8955SMoni Shoua 			 */
996a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.rc_odp_caps &=
997a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
998a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
999a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.uc_odp_caps &=
1000a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1001a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1002a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.ud_odp_caps &=
1003a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1004a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1005a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.xrc_odp_caps &=
1006a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1007a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1008a73a8955SMoni Shoua 		}
1009e502b8b0SLeon Romanovsky 	}
10108cdd312cSHaggai Eran 
1011051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
1012051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1013051f2630SLeon Romanovsky 
1014e53a9d26SParav Pandit 	if (mlx5_core_is_vf(mdev))
1015eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1016eff901d3SEli Cohen 
101731f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
101885c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
101931f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
102031f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
102131f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
102231f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
102331f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
102431f69a82SYishai Hadas 		props->max_wq_type_rq =
102531f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
102631f69a82SYishai Hadas 	}
102731f69a82SYishai Hadas 
1028eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
102978b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
1030eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
103178b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
1032eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
103378b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1034eb761894SArtemy Kovalyov 	}
1035eb761894SArtemy Kovalyov 
103689705e92SDanit Goldberg 	if (MLX5_CAP_GEN(mdev, tag_matching) &&
103789705e92SDanit Goldberg 	    MLX5_CAP_GEN(mdev, rndv_offload_rc)) {
103889705e92SDanit Goldberg 		props->tm_caps.flags = IB_TM_CAP_RNDV_RC;
103989705e92SDanit Goldberg 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
104089705e92SDanit Goldberg 	}
104189705e92SDanit Goldberg 
104287ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
104387ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
104487ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
104587ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
104687ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
104787ab3f52SYonatan Cohen 	}
104887ab3f52SYonatan Cohen 
1049a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), cqe_comp_caps) <= uhw_outlen) {
1050572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
1051572f46bfSYonatan Cohen 
1052572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
10537e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
1054572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
1055572f46bfSYonatan Cohen 					     cqe_compression_max_num);
1056572f46bfSYonatan Cohen 
10577e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
10587e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
10597e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
10606f1006a4SYonatan Cohen 
10616f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
10626f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
10636f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1064572f46bfSYonatan Cohen 		}
10657e43a2a5SBodong Wang 	}
10667e43a2a5SBodong Wang 
1067a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), packet_pacing_caps) <= uhw_outlen &&
106885c7c014SDaniel Jurgens 	    raw_support) {
1069d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1070d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
1071d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
1072d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1073d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
1074d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1075d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
1076d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
107761147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
107861147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
107961147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
108061147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
1081d949167dSBodong Wang 		}
1082d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1083d949167dSBodong Wang 	}
1084d949167dSBodong Wang 
1085a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes) <=
1086a762d460SLeon Romanovsky 	    uhw_outlen) {
1087795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
10889f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1089795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1090050da902SBodong Wang 
1091050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1092050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1093050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1094050da902SBodong Wang 
10959f885201SLeon Romanovsky 		resp.response_length +=
10969f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
10979f885201SLeon Romanovsky 	}
10989f885201SLeon Romanovsky 
1099a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), flags) <= uhw_outlen) {
1100de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
11017a0c8f42SGuy Levi 
1102de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1103de57f2adSGuy Levi 			resp.flags |=
1104de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
11057a0c8f42SGuy Levi 
11067a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
11077a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
11087e11b911SDanit Goldberg 		if (MLX5_CAP_GEN(mdev, qp_packet_based))
11097e11b911SDanit Goldberg 			resp.flags |=
11107e11b911SDanit Goldberg 				MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
11117249c8eaSGuy Levi 
11127249c8eaSGuy Levi 		resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1113de57f2adSGuy Levi 	}
11149f885201SLeon Romanovsky 
1115a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), sw_parsing_caps) <= uhw_outlen) {
111696dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
111796dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
111896dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
111996dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
112096dc3fc5SNoa Osherovich 
112196dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
112296dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
112396dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
112496dc3fc5SNoa Osherovich 
112596dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
112696dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
112796dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
112896dc3fc5SNoa Osherovich 
112996dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
113096dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
113196dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
113296dc3fc5SNoa Osherovich 		}
113396dc3fc5SNoa Osherovich 	}
113496dc3fc5SNoa Osherovich 
1135a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), striding_rq_caps) <= uhw_outlen &&
113685c7c014SDaniel Jurgens 	    raw_support) {
1137b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1138b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1139b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1140b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1141b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1142b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1143c16339b6SMark Zhang 			if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range))
1144c16339b6SMark Zhang 				resp.striding_rq_caps
1145c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1146c16339b6SMark Zhang 					MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1147c16339b6SMark Zhang 			else
1148c16339b6SMark Zhang 				resp.striding_rq_caps
1149c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1150b4f34597SNoa Osherovich 					MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1151b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1152b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1153b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1154b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1155b4f34597SNoa Osherovich 		}
1156b4f34597SNoa Osherovich 	}
1157b4f34597SNoa Osherovich 
1158a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), tunnel_offloads_caps) <= uhw_outlen) {
1159f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1160f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1161f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1162f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1163f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1164f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1165f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1166f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1167f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1168f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
116941e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre))
1170e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1171e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
117241e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_udp))
1173e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1174e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1175f95ef6cbSMaor Gottlieb 	}
1176f95ef6cbSMaor Gottlieb 
117748357091SLeon Romanovsky 	if (uhw_outlen) {
1178402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1179402ca536SBodong Wang 
1180402ca536SBodong Wang 		if (err)
1181402ca536SBodong Wang 			return err;
1182402ca536SBodong Wang 	}
1183402ca536SBodong Wang 
11841b5daf11SMajd Dibbiny 	return 0;
11851b5daf11SMajd Dibbiny }
1186e126ba97SEli Cohen 
1187639bf441SAharon Landau static void translate_active_width(struct ib_device *ibdev, u16 active_width,
11881b5daf11SMajd Dibbiny 				   u8 *ib_width)
11891b5daf11SMajd Dibbiny {
11901b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11911b5daf11SMajd Dibbiny 
1192e27014bdSAharon Landau 	if (active_width & MLX5_PTYS_WIDTH_1X)
11931b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1194e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_2X)
1195d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1196e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_4X)
11971b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1198e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_8X)
11991b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1200e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_12X)
12011b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1202db7a691aSMichael Guralnik 	else {
1203db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
1204e27014bdSAharon Landau 			    active_width);
1205db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
12061b5daf11SMajd Dibbiny 	}
12071b5daf11SMajd Dibbiny 
1208db7a691aSMichael Guralnik 	return;
12091b5daf11SMajd Dibbiny }
12101b5daf11SMajd Dibbiny 
12111b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
12121b5daf11SMajd Dibbiny {
12131b5daf11SMajd Dibbiny 	switch (mtu) {
12141b5daf11SMajd Dibbiny 	case 256: return 1;
12151b5daf11SMajd Dibbiny 	case 512: return 2;
12161b5daf11SMajd Dibbiny 	case 1024: return 3;
12171b5daf11SMajd Dibbiny 	case 2048: return 4;
12181b5daf11SMajd Dibbiny 	case 4096: return 5;
12191b5daf11SMajd Dibbiny 	default:
12201b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
12211b5daf11SMajd Dibbiny 		return -1;
12221b5daf11SMajd Dibbiny 	}
12231b5daf11SMajd Dibbiny }
12241b5daf11SMajd Dibbiny 
12251b5daf11SMajd Dibbiny enum ib_max_vl_num {
12261b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
12271b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
12281b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
12291b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
12301b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
12311b5daf11SMajd Dibbiny };
12321b5daf11SMajd Dibbiny 
12331b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
12341b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
12351b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
12361b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
12371b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
12381b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
12391b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
12401b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
12411b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
12421b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
12431b5daf11SMajd Dibbiny };
12441b5daf11SMajd Dibbiny 
12451b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
12461b5daf11SMajd Dibbiny 				u8 *max_vl_num)
12471b5daf11SMajd Dibbiny {
12481b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
12491b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
12501b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
12511b5daf11SMajd Dibbiny 		break;
12521b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
12531b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
12541b5daf11SMajd Dibbiny 		break;
12551b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
12561b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
12571b5daf11SMajd Dibbiny 		break;
12581b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
12591b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
12601b5daf11SMajd Dibbiny 		break;
12611b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
12621b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
12631b5daf11SMajd Dibbiny 		break;
12641b5daf11SMajd Dibbiny 
12651b5daf11SMajd Dibbiny 	default:
12661b5daf11SMajd Dibbiny 		return -EINVAL;
12671b5daf11SMajd Dibbiny 	}
12681b5daf11SMajd Dibbiny 
12691b5daf11SMajd Dibbiny 	return 0;
12701b5daf11SMajd Dibbiny }
12711b5daf11SMajd Dibbiny 
12721b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
12731b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
12741b5daf11SMajd Dibbiny {
12751b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12761b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
12771b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1278046339eaSSaeed Mahameed 	u16 max_mtu;
1279046339eaSSaeed Mahameed 	u16 oper_mtu;
12801b5daf11SMajd Dibbiny 	int err;
1281639bf441SAharon Landau 	u16 ib_link_width_oper;
12821b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
12831b5daf11SMajd Dibbiny 
12841b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
12851b5daf11SMajd Dibbiny 	if (!rep) {
12861b5daf11SMajd Dibbiny 		err = -ENOMEM;
12871b5daf11SMajd Dibbiny 		goto out;
12881b5daf11SMajd Dibbiny 	}
12891b5daf11SMajd Dibbiny 
1290c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
12911b5daf11SMajd Dibbiny 
12921b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
12931b5daf11SMajd Dibbiny 	if (err)
12941b5daf11SMajd Dibbiny 		goto out;
12951b5daf11SMajd Dibbiny 
12961b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
12971b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
12981b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
12991b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
13001b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
13011b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
13021b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
13031b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
13041b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
13051b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
13061b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
13071b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
13081b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
13091b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
13101b5daf11SMajd Dibbiny 
13114106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
13124106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
13134106a758SMichael Guralnik 
1314639bf441SAharon Landau 	err = mlx5_query_ib_port_oper(mdev, &ib_link_width_oper,
1315376ceb31SAharon Landau 				      &props->active_speed, port);
13161b5daf11SMajd Dibbiny 	if (err)
13171b5daf11SMajd Dibbiny 		goto out;
13181b5daf11SMajd Dibbiny 
1319db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1320db7a691aSMichael Guralnik 
1321facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
13221b5daf11SMajd Dibbiny 
13231b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
13241b5daf11SMajd Dibbiny 
1325facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
13261b5daf11SMajd Dibbiny 
13271b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
13281b5daf11SMajd Dibbiny 
13291b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
13301b5daf11SMajd Dibbiny 	if (err)
13311b5daf11SMajd Dibbiny 		goto out;
13321b5daf11SMajd Dibbiny 
13331b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
13341b5daf11SMajd Dibbiny 				   &props->max_vl_num);
13351b5daf11SMajd Dibbiny out:
13361b5daf11SMajd Dibbiny 	kfree(rep);
1337e126ba97SEli Cohen 	return err;
1338e126ba97SEli Cohen }
1339e126ba97SEli Cohen 
1340e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1341e126ba97SEli Cohen 		       struct ib_port_attr *props)
1342e126ba97SEli Cohen {
1343095b0927SIlan Tayari 	unsigned int count;
1344095b0927SIlan Tayari 	int ret;
1345095b0927SIlan Tayari 
13461b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13471b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1348095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1349095b0927SIlan Tayari 		break;
1350e126ba97SEli Cohen 
13511b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1352095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1353095b0927SIlan Tayari 		break;
13541b5daf11SMajd Dibbiny 
13553f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1356095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1357095b0927SIlan Tayari 		break;
13583f89a643SAchiad Shochat 
13591b5daf11SMajd Dibbiny 	default:
1360095b0927SIlan Tayari 		ret = -EINVAL;
1361e126ba97SEli Cohen 	}
1362095b0927SIlan Tayari 
1363095b0927SIlan Tayari 	if (!ret && props) {
1364b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1365b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1366b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1367b3cbd6f0SDaniel Jurgens 
1368b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1369b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1370b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1371b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1372b3cbd6f0SDaniel Jurgens 			 */
1373b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1374b3cbd6f0SDaniel Jurgens 			port = 1;
1375b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1376b3cbd6f0SDaniel Jurgens 		}
1377b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1378b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1379b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1380095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1381095b0927SIlan Tayari 	}
1382095b0927SIlan Tayari 	return ret;
1383e126ba97SEli Cohen }
1384e126ba97SEli Cohen 
13858e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
13868e6efa3aSMark Bloch 				  struct ib_port_attr *props)
13878e6efa3aSMark Bloch {
13888e6efa3aSMark Bloch 	int ret;
13898e6efa3aSMark Bloch 
139026628e2dSMark Bloch 	/* Only link layer == ethernet is valid for representors
139126628e2dSMark Bloch 	 * and we always use port 1
139226628e2dSMark Bloch 	 */
13938e6efa3aSMark Bloch 	ret = mlx5_query_port_roce(ibdev, port, props);
13948e6efa3aSMark Bloch 	if (ret || !props)
13958e6efa3aSMark Bloch 		return ret;
13968e6efa3aSMark Bloch 
13978e6efa3aSMark Bloch 	/* We don't support GIDS */
13988e6efa3aSMark Bloch 	props->gid_tbl_len = 0;
13998e6efa3aSMark Bloch 
14008e6efa3aSMark Bloch 	return ret;
14018e6efa3aSMark Bloch }
14028e6efa3aSMark Bloch 
1403*d6fd59e1SParav Pandit static int mlx5_ib_rep_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1404*d6fd59e1SParav Pandit 				  u16 *pkey)
1405*d6fd59e1SParav Pandit {
1406*d6fd59e1SParav Pandit 	/* Default special Pkey for representor device port as per the
1407*d6fd59e1SParav Pandit 	 * IB specification 1.3 section 10.9.1.2.
1408*d6fd59e1SParav Pandit 	 */
1409*d6fd59e1SParav Pandit 	*pkey = 0xffff;
1410*d6fd59e1SParav Pandit 	return 0;
1411*d6fd59e1SParav Pandit }
1412*d6fd59e1SParav Pandit 
1413e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1414e126ba97SEli Cohen 			     union ib_gid *gid)
1415e126ba97SEli Cohen {
14161b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
14171b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1418e126ba97SEli Cohen 
14191b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14201b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14211b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1422e126ba97SEli Cohen 
14231b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14241b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1425e126ba97SEli Cohen 
14261b5daf11SMajd Dibbiny 	default:
14271b5daf11SMajd Dibbiny 		return -EINVAL;
14281b5daf11SMajd Dibbiny 	}
1429e126ba97SEli Cohen 
1430e126ba97SEli Cohen }
1431e126ba97SEli Cohen 
1432b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1433b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1434b3cbd6f0SDaniel Jurgens {
1435b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1436b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1437b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1438b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1439b3cbd6f0SDaniel Jurgens 	int err;
1440b3cbd6f0SDaniel Jurgens 
1441b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1442b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1443b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1444b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1445b3cbd6f0SDaniel Jurgens 		 */
1446b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1447b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1448b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1449b3cbd6f0SDaniel Jurgens 	}
1450b3cbd6f0SDaniel Jurgens 
1451b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1452b3cbd6f0SDaniel Jurgens 					index, pkey);
1453b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1454b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1455b3cbd6f0SDaniel Jurgens 
1456b3cbd6f0SDaniel Jurgens 	return err;
1457b3cbd6f0SDaniel Jurgens }
1458b3cbd6f0SDaniel Jurgens 
1459e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1460e126ba97SEli Cohen 			      u16 *pkey)
1461e126ba97SEli Cohen {
14621b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14631b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14641b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1465e126ba97SEli Cohen 
14661b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14671b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1468b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
14691b5daf11SMajd Dibbiny 	default:
14701b5daf11SMajd Dibbiny 		return -EINVAL;
1471e126ba97SEli Cohen 	}
14721b5daf11SMajd Dibbiny }
1473e126ba97SEli Cohen 
1474e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1475e126ba97SEli Cohen 				 struct ib_device_modify *props)
1476e126ba97SEli Cohen {
1477e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1478e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1479e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1480e126ba97SEli Cohen 	int err;
1481e126ba97SEli Cohen 
1482e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1483e126ba97SEli Cohen 		return -EOPNOTSUPP;
1484e126ba97SEli Cohen 
1485e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1486e126ba97SEli Cohen 		return 0;
1487e126ba97SEli Cohen 
1488e126ba97SEli Cohen 	/*
1489e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1490e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1491e126ba97SEli Cohen 	 */
1492bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
14939603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1494e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1495e126ba97SEli Cohen 	if (err)
1496e126ba97SEli Cohen 		return err;
1497e126ba97SEli Cohen 
1498bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1499e126ba97SEli Cohen 
1500e126ba97SEli Cohen 	return err;
1501e126ba97SEli Cohen }
1502e126ba97SEli Cohen 
1503cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1504cdbe33d0SEli Cohen 				u32 value)
1505cdbe33d0SEli Cohen {
1506cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1507b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1508b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1509cdbe33d0SEli Cohen 	int err;
1510cdbe33d0SEli Cohen 
1511b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1512b3cbd6f0SDaniel Jurgens 	if (!mdev)
1513b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1514b3cbd6f0SDaniel Jurgens 
1515b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1516cdbe33d0SEli Cohen 	if (err)
1517b3cbd6f0SDaniel Jurgens 		goto out;
1518cdbe33d0SEli Cohen 
1519cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1520cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1521cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1522b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1523b3cbd6f0SDaniel Jurgens 		goto out;
1524cdbe33d0SEli Cohen 	}
1525cdbe33d0SEli Cohen 
1526cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1527cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1528b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1529b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1530b3cbd6f0SDaniel Jurgens 
1531b3cbd6f0SDaniel Jurgens out:
1532b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1533cdbe33d0SEli Cohen 
1534cdbe33d0SEli Cohen 	return err;
1535cdbe33d0SEli Cohen }
1536cdbe33d0SEli Cohen 
1537e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1538e126ba97SEli Cohen 			       struct ib_port_modify *props)
1539e126ba97SEli Cohen {
1540e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1541e126ba97SEli Cohen 	struct ib_port_attr attr;
1542e126ba97SEli Cohen 	u32 tmp;
1543e126ba97SEli Cohen 	int err;
1544cdbe33d0SEli Cohen 	u32 change_mask;
1545cdbe33d0SEli Cohen 	u32 value;
1546cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1547cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1548cdbe33d0SEli Cohen 
1549ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1550ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1551ec255879SMajd Dibbiny 	 */
1552ec255879SMajd Dibbiny 	if (!is_ib)
1553ec255879SMajd Dibbiny 		return 0;
1554ec255879SMajd Dibbiny 
1555cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1556cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1557cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1558cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1559cdbe33d0SEli Cohen 	}
1560e126ba97SEli Cohen 
1561e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1562e126ba97SEli Cohen 
1563c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1564e126ba97SEli Cohen 	if (err)
1565e126ba97SEli Cohen 		goto out;
1566e126ba97SEli Cohen 
1567e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1568e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1569e126ba97SEli Cohen 
15709603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1571e126ba97SEli Cohen 
1572e126ba97SEli Cohen out:
1573e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1574e126ba97SEli Cohen 	return err;
1575e126ba97SEli Cohen }
1576e126ba97SEli Cohen 
157730aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
157830aa60b3SEli Cohen {
157930aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
158030aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
158130aa60b3SEli Cohen }
158230aa60b3SEli Cohen 
158331a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
158431a78a5aSYishai Hadas {
158531a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
158631a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
158731a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
158831a78a5aSYishai Hadas 
158931a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
159031a78a5aSYishai Hadas }
159131a78a5aSYishai Hadas 
1592b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1593b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
159431a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1595b037c29aSEli Cohen {
1596b037c29aSEli Cohen 	int uars_per_sys_page;
1597b037c29aSEli Cohen 	int bfregs_per_sys_page;
1598b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1599b037c29aSEli Cohen 
1600b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1601b037c29aSEli Cohen 		return -EINVAL;
1602b037c29aSEli Cohen 
1603b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1604b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1605b037c29aSEli Cohen 
1606b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1607b037c29aSEli Cohen 		return -ENOMEM;
1608b037c29aSEli Cohen 
1609b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1610b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
161131a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1612b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1613b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1614b037c29aSEli Cohen 		return -EINVAL;
1615b037c29aSEli Cohen 
161631a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
161731a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
161831a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
161931a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
162031a78a5aSYishai Hadas 
162131a78a5aSYishai Hadas 	mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, allocated %d, total bfregs %d, using %d sys pages\n",
1622b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1623b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
162431a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
162531a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1626b037c29aSEli Cohen 
1627b037c29aSEli Cohen 	return 0;
1628b037c29aSEli Cohen }
1629b037c29aSEli Cohen 
1630b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1631b037c29aSEli Cohen {
1632b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1633b037c29aSEli Cohen 	int err;
1634b037c29aSEli Cohen 	int i;
1635b037c29aSEli Cohen 
1636b037c29aSEli Cohen 	bfregi = &context->bfregi;
163731a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1638b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1639b037c29aSEli Cohen 		if (err)
1640b037c29aSEli Cohen 			goto error;
1641b037c29aSEli Cohen 
1642b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1643b037c29aSEli Cohen 	}
16444ed131d0SYishai Hadas 
16454ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
16464ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
16474ed131d0SYishai Hadas 
1648b037c29aSEli Cohen 	return 0;
1649b037c29aSEli Cohen 
1650b037c29aSEli Cohen error:
1651b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1652b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1653b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1654b037c29aSEli Cohen 
1655b037c29aSEli Cohen 	return err;
1656b037c29aSEli Cohen }
1657b037c29aSEli Cohen 
165815177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
165915177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1660b037c29aSEli Cohen {
1661b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1662b037c29aSEli Cohen 	int i;
1663b037c29aSEli Cohen 
1664b037c29aSEli Cohen 	bfregi = &context->bfregi;
166515177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
16664ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
166715177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
166815177999SLeon Romanovsky 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1669b037c29aSEli Cohen }
1670b037c29aSEli Cohen 
16710042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1672a560f1d9SMark Bloch {
1673a560f1d9SMark Bloch 	int err = 0;
1674a560f1d9SMark Bloch 
1675a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16760042f9e4SMark Bloch 	if (td)
1677a560f1d9SMark Bloch 		dev->lb.user_td++;
16780042f9e4SMark Bloch 	if (qp)
16790042f9e4SMark Bloch 		dev->lb.qps++;
1680a560f1d9SMark Bloch 
16810042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
16820042f9e4SMark Bloch 	    dev->lb.qps == 1) {
16830042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1684a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
16850042f9e4SMark Bloch 			dev->lb.enabled = true;
16860042f9e4SMark Bloch 		}
16870042f9e4SMark Bloch 	}
1688a560f1d9SMark Bloch 
1689a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1690a560f1d9SMark Bloch 
1691a560f1d9SMark Bloch 	return err;
1692a560f1d9SMark Bloch }
1693a560f1d9SMark Bloch 
16940042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1695a560f1d9SMark Bloch {
1696a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16970042f9e4SMark Bloch 	if (td)
1698a560f1d9SMark Bloch 		dev->lb.user_td--;
16990042f9e4SMark Bloch 	if (qp)
17000042f9e4SMark Bloch 		dev->lb.qps--;
1701a560f1d9SMark Bloch 
17020042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
17030042f9e4SMark Bloch 	    dev->lb.qps == 0) {
17040042f9e4SMark Bloch 		if (dev->lb.enabled) {
1705a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
17060042f9e4SMark Bloch 			dev->lb.enabled = false;
17070042f9e4SMark Bloch 		}
17080042f9e4SMark Bloch 	}
1709a560f1d9SMark Bloch 
1710a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1711a560f1d9SMark Bloch }
1712a560f1d9SMark Bloch 
1713d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1714d2d19121SYishai Hadas 					  u16 uid)
1715c85023e1SHuy Nguyen {
1716c85023e1SHuy Nguyen 	int err;
1717c85023e1SHuy Nguyen 
1718cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1719cfdeb893SLeon Romanovsky 		return 0;
1720cfdeb893SLeon Romanovsky 
1721d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1722c85023e1SHuy Nguyen 	if (err)
1723c85023e1SHuy Nguyen 		return err;
1724c85023e1SHuy Nguyen 
1725c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17268978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17278978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1728c85023e1SHuy Nguyen 		return err;
1729c85023e1SHuy Nguyen 
17300042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1731c85023e1SHuy Nguyen }
1732c85023e1SHuy Nguyen 
1733d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1734d2d19121SYishai Hadas 					     u16 uid)
1735c85023e1SHuy Nguyen {
1736cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1737cfdeb893SLeon Romanovsky 		return;
1738cfdeb893SLeon Romanovsky 
1739d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1740c85023e1SHuy Nguyen 
1741c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17428978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17438978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1744c85023e1SHuy Nguyen 		return;
1745c85023e1SHuy Nguyen 
17460042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1747c85023e1SHuy Nguyen }
1748c85023e1SHuy Nguyen 
174945ec21c9SYishai Hadas static int set_ucontext_resp(struct ib_ucontext *uctx,
175045ec21c9SYishai Hadas 			     struct mlx5_ib_alloc_ucontext_resp *resp)
175145ec21c9SYishai Hadas {
175245ec21c9SYishai Hadas 	struct ib_device *ibdev = uctx->device;
175345ec21c9SYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
175445ec21c9SYishai Hadas 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
175545ec21c9SYishai Hadas 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
175645ec21c9SYishai Hadas 	int err;
175745ec21c9SYishai Hadas 
175845ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
175945ec21c9SYishai Hadas 		err = mlx5_cmd_dump_fill_mkey(dev->mdev,
176045ec21c9SYishai Hadas 					      &resp->dump_fill_mkey);
176145ec21c9SYishai Hadas 		if (err)
176245ec21c9SYishai Hadas 			return err;
176345ec21c9SYishai Hadas 		resp->comp_mask |=
176445ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
176545ec21c9SYishai Hadas 	}
176645ec21c9SYishai Hadas 
176745ec21c9SYishai Hadas 	resp->qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
176845ec21c9SYishai Hadas 	if (dev->wc_support)
176945ec21c9SYishai Hadas 		resp->bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev,
177045ec21c9SYishai Hadas 						      log_bf_reg_size);
177145ec21c9SYishai Hadas 	resp->cache_line_size = cache_line_size();
177245ec21c9SYishai Hadas 	resp->max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
177345ec21c9SYishai Hadas 	resp->max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
177445ec21c9SYishai Hadas 	resp->max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
177545ec21c9SYishai Hadas 	resp->max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
177645ec21c9SYishai Hadas 	resp->max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
177745ec21c9SYishai Hadas 	resp->cqe_version = context->cqe_version;
177845ec21c9SYishai Hadas 	resp->log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
177945ec21c9SYishai Hadas 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
178045ec21c9SYishai Hadas 	resp->num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
178145ec21c9SYishai Hadas 					MLX5_CAP_GEN(dev->mdev,
178245ec21c9SYishai Hadas 						     num_of_uars_per_page) : 1;
178345ec21c9SYishai Hadas 
178445ec21c9SYishai Hadas 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
178545ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_DEVICE) {
178645ec21c9SYishai Hadas 		if (mlx5_get_flow_namespace(dev->mdev,
178745ec21c9SYishai Hadas 				MLX5_FLOW_NAMESPACE_EGRESS))
178845ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
178945ec21c9SYishai Hadas 		if (mlx5_accel_ipsec_device_caps(dev->mdev) &
179045ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
179145ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
179245ec21c9SYishai Hadas 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
179345ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
179445ec21c9SYishai Hadas 		if (mlx5_accel_ipsec_device_caps(dev->mdev) &
179545ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
179645ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
179745ec21c9SYishai Hadas 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
179845ec21c9SYishai Hadas 	}
179945ec21c9SYishai Hadas 
180045ec21c9SYishai Hadas 	resp->tot_bfregs = bfregi->lib_uar_dyn ? 0 :
180145ec21c9SYishai Hadas 			bfregi->total_num_bfregs - bfregi->num_dyn_bfregs;
180245ec21c9SYishai Hadas 	resp->num_ports = dev->num_ports;
180345ec21c9SYishai Hadas 	resp->cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
180445ec21c9SYishai Hadas 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
180545ec21c9SYishai Hadas 
180645ec21c9SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
180745ec21c9SYishai Hadas 		mlx5_query_min_inline(dev->mdev, &resp->eth_min_inline);
180845ec21c9SYishai Hadas 		resp->eth_min_inline++;
180945ec21c9SYishai Hadas 	}
181045ec21c9SYishai Hadas 
181145ec21c9SYishai Hadas 	if (dev->mdev->clock_info)
181245ec21c9SYishai Hadas 		resp->clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
181345ec21c9SYishai Hadas 
181445ec21c9SYishai Hadas 	/*
181545ec21c9SYishai Hadas 	 * We don't want to expose information from the PCI bar that is located
181645ec21c9SYishai Hadas 	 * after 4096 bytes, so if the arch only supports larger pages, let's
181745ec21c9SYishai Hadas 	 * pretend we don't support reading the HCA's core clock. This is also
181845ec21c9SYishai Hadas 	 * forced by mmap function.
181945ec21c9SYishai Hadas 	 */
182045ec21c9SYishai Hadas 	if (PAGE_SIZE <= 4096) {
182145ec21c9SYishai Hadas 		resp->comp_mask |=
182245ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
182345ec21c9SYishai Hadas 		resp->hca_core_clock_offset =
182445ec21c9SYishai Hadas 			offsetof(struct mlx5_init_seg,
182545ec21c9SYishai Hadas 				 internal_timer_h) % PAGE_SIZE;
182645ec21c9SYishai Hadas 	}
182745ec21c9SYishai Hadas 
182845ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, ece_support))
182945ec21c9SYishai Hadas 		resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_ECE;
183045ec21c9SYishai Hadas 
183145ec21c9SYishai Hadas 	resp->num_dyn_bfregs = bfregi->num_dyn_bfregs;
183245ec21c9SYishai Hadas 	return 0;
183345ec21c9SYishai Hadas }
183445ec21c9SYishai Hadas 
1835a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1836e126ba97SEli Cohen 				  struct ib_udata *udata)
1837e126ba97SEli Cohen {
1838a2a074efSLeon Romanovsky 	struct ib_device *ibdev = uctx->device;
1839e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1840b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1841b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1842a2a074efSLeon Romanovsky 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
18432f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
184478c0f98cSEli Cohen 	int ver;
1845e126ba97SEli Cohen 	int err;
1846a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1847a168a41cSMajd Dibbiny 				     max_cqe_version);
1848b037c29aSEli Cohen 	bool lib_uar_4k;
18490a2fd01cSYishai Hadas 	bool lib_uar_dyn;
1850e126ba97SEli Cohen 
1851e126ba97SEli Cohen 	if (!dev->ib_active)
1852a2a074efSLeon Romanovsky 		return -EAGAIN;
1853e126ba97SEli Cohen 
1854e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
185578c0f98cSEli Cohen 		ver = 0;
1856e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
185778c0f98cSEli Cohen 		ver = 2;
185878c0f98cSEli Cohen 	else
1859a2a074efSLeon Romanovsky 		return -EINVAL;
186078c0f98cSEli Cohen 
1861e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1862e126ba97SEli Cohen 	if (err)
1863a2a074efSLeon Romanovsky 		return err;
1864e126ba97SEli Cohen 
1865a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1866a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
186778c0f98cSEli Cohen 
1868f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1869a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
1870b368d7cbSMatan Barak 
18712f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
18722f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
18732f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1874a2a074efSLeon Romanovsky 		return -EINVAL;
1875e126ba97SEli Cohen 
187630aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
18770a2fd01cSYishai Hadas 	lib_uar_dyn = req.lib_caps & MLX5_LIB_CAP_DYN_UAR;
18782f5ff264SEli Cohen 	bfregi = &context->bfregi;
1879b037c29aSEli Cohen 
18800a2fd01cSYishai Hadas 	if (lib_uar_dyn) {
18810a2fd01cSYishai Hadas 		bfregi->lib_uar_dyn = lib_uar_dyn;
18820a2fd01cSYishai Hadas 		goto uar_done;
18830a2fd01cSYishai Hadas 	}
18840a2fd01cSYishai Hadas 
1885b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
188631a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1887b037c29aSEli Cohen 	if (err)
1888b037c29aSEli Cohen 		goto out_ctx;
1889b037c29aSEli Cohen 
18902f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1891b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
189231a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1893b037c29aSEli Cohen 				GFP_KERNEL);
1894b037c29aSEli Cohen 	if (!bfregi->count) {
1895e126ba97SEli Cohen 		err = -ENOMEM;
1896e126ba97SEli Cohen 		goto out_ctx;
1897e126ba97SEli Cohen 	}
1898e126ba97SEli Cohen 
1899b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1900b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1901e126ba97SEli Cohen 				    GFP_KERNEL);
1902b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1903e126ba97SEli Cohen 		err = -ENOMEM;
1904e126ba97SEli Cohen 		goto out_count;
1905e126ba97SEli Cohen 	}
1906e126ba97SEli Cohen 
1907b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1908b037c29aSEli Cohen 	if (err)
1909b037c29aSEli Cohen 		goto out_sys_pages;
1910b037c29aSEli Cohen 
19110a2fd01cSYishai Hadas uar_done:
1912a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1913fb98153bSYishai Hadas 		err = mlx5_ib_devx_create(dev, true);
191476dc5a84SYishai Hadas 		if (err < 0)
1915d2d19121SYishai Hadas 			goto out_uars;
191676dc5a84SYishai Hadas 		context->devx_uid = err;
1917a8b92ca1SYishai Hadas 	}
1918a8b92ca1SYishai Hadas 
1919d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1920d2d19121SYishai Hadas 					     context->devx_uid);
1921a8b92ca1SYishai Hadas 	if (err)
1922d2d19121SYishai Hadas 		goto out_devx;
1923a8b92ca1SYishai Hadas 
1924e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1925e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1926e126ba97SEli Cohen 
192745ec21c9SYishai Hadas 	context->cqe_version = min_t(__u8,
192845ec21c9SYishai Hadas 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
192945ec21c9SYishai Hadas 				 req.max_cqe_version);
1930b368d7cbSMatan Barak 
193145ec21c9SYishai Hadas 	err = set_ucontext_resp(uctx, &resp);
193245ec21c9SYishai Hadas 	if (err)
193345ec21c9SYishai Hadas 		goto out_mdev;
1934b368d7cbSMatan Barak 
193545ec21c9SYishai Hadas 	resp.response_length = min(udata->outlen, sizeof(resp));
1936b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1937e126ba97SEli Cohen 	if (err)
1938a8b92ca1SYishai Hadas 		goto out_mdev;
1939e126ba97SEli Cohen 
19402f5ff264SEli Cohen 	bfregi->ver = ver;
19412f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
194230aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
194330aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1944f72300c5SHaggai Abramovsky 
1945802dcc7fSMark Zhang 	if (mlx5_ib_lag_should_assign_affinity(dev)) {
194695579e78SMark Bloch 		u8 port = mlx5_core_native_port_num(dev->mdev) - 1;
1947c6a21c38SMajd Dibbiny 
1948c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1949c6a21c38SMajd Dibbiny 			   atomic_add_return(
195095579e78SMark Bloch 				   1, &dev->port[port].roce.tx_port_affinity));
1951c6a21c38SMajd Dibbiny 	}
1952c6a21c38SMajd Dibbiny 
1953a2a074efSLeon Romanovsky 	return 0;
1954e126ba97SEli Cohen 
1955a8b92ca1SYishai Hadas out_mdev:
1956d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1957d2d19121SYishai Hadas out_devx:
1958a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
195976dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1960146d2f1aSmajd@mellanox.com 
1961e126ba97SEli Cohen out_uars:
1962b037c29aSEli Cohen 	deallocate_uars(dev, context);
1963b037c29aSEli Cohen 
1964b037c29aSEli Cohen out_sys_pages:
1965b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1966b037c29aSEli Cohen 
1967e126ba97SEli Cohen out_count:
19682f5ff264SEli Cohen 	kfree(bfregi->count);
1969e126ba97SEli Cohen 
1970e126ba97SEli Cohen out_ctx:
1971a2a074efSLeon Romanovsky 	return err;
1972e126ba97SEli Cohen }
1973e126ba97SEli Cohen 
19740fb556b2SYishai Hadas static int mlx5_ib_query_ucontext(struct ib_ucontext *ibcontext,
19750fb556b2SYishai Hadas 				  struct uverbs_attr_bundle *attrs)
19760fb556b2SYishai Hadas {
19770fb556b2SYishai Hadas 	struct mlx5_ib_alloc_ucontext_resp uctx_resp = {};
19780fb556b2SYishai Hadas 	int ret;
19790fb556b2SYishai Hadas 
19800fb556b2SYishai Hadas 	ret = set_ucontext_resp(ibcontext, &uctx_resp);
19810fb556b2SYishai Hadas 	if (ret)
19820fb556b2SYishai Hadas 		return ret;
19830fb556b2SYishai Hadas 
19840fb556b2SYishai Hadas 	uctx_resp.response_length =
19850fb556b2SYishai Hadas 		min_t(size_t,
19860fb556b2SYishai Hadas 		      uverbs_attr_get_len(attrs,
19870fb556b2SYishai Hadas 				MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX),
19880fb556b2SYishai Hadas 		      sizeof(uctx_resp));
19890fb556b2SYishai Hadas 
19900fb556b2SYishai Hadas 	ret = uverbs_copy_to_struct_or_zero(attrs,
19910fb556b2SYishai Hadas 					MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
19920fb556b2SYishai Hadas 					&uctx_resp,
19930fb556b2SYishai Hadas 					sizeof(uctx_resp));
19940fb556b2SYishai Hadas 	return ret;
19950fb556b2SYishai Hadas }
19960fb556b2SYishai Hadas 
1997a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1998e126ba97SEli Cohen {
1999e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2000e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
2001b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
2002e126ba97SEli Cohen 
2003b037c29aSEli Cohen 	bfregi = &context->bfregi;
2004d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2005d2d19121SYishai Hadas 
2006b037c29aSEli Cohen 	if (context->devx_uid)
200776dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
2008e126ba97SEli Cohen 
2009e126ba97SEli Cohen 	deallocate_uars(dev, context);
20102f5ff264SEli Cohen 	kfree(bfregi->sys_pages);
20112f5ff264SEli Cohen 	kfree(bfregi->count);
2012e126ba97SEli Cohen }
2013e126ba97SEli Cohen 
2014e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2015e126ba97SEli Cohen 				 int uar_idx)
2016e126ba97SEli Cohen {
2017b037c29aSEli Cohen 	int fw_uars_per_page;
2018b037c29aSEli Cohen 
2019b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2020b037c29aSEli Cohen 
2021aa8106f1SHuy Nguyen 	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2022e126ba97SEli Cohen }
2023e126ba97SEli Cohen 
2024342ee59dSYishai Hadas static u64 uar_index2paddress(struct mlx5_ib_dev *dev,
2025342ee59dSYishai Hadas 				 int uar_idx)
2026342ee59dSYishai Hadas {
2027342ee59dSYishai Hadas 	unsigned int fw_uars_per_page;
2028342ee59dSYishai Hadas 
2029342ee59dSYishai Hadas 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
2030342ee59dSYishai Hadas 				MLX5_UARS_IN_PAGE : 1;
2031342ee59dSYishai Hadas 
2032342ee59dSYishai Hadas 	return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE);
2033342ee59dSYishai Hadas }
2034342ee59dSYishai Hadas 
2035e126ba97SEli Cohen static int get_command(unsigned long offset)
2036e126ba97SEli Cohen {
2037e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2038e126ba97SEli Cohen }
2039e126ba97SEli Cohen 
2040e126ba97SEli Cohen static int get_arg(unsigned long offset)
2041e126ba97SEli Cohen {
2042e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2043e126ba97SEli Cohen }
2044e126ba97SEli Cohen 
2045e126ba97SEli Cohen static int get_index(unsigned long offset)
2046e126ba97SEli Cohen {
2047e126ba97SEli Cohen 	return get_arg(offset);
2048e126ba97SEli Cohen }
2049e126ba97SEli Cohen 
20504ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
20514ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20524ed131d0SYishai Hadas {
20534ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20544ed131d0SYishai Hadas }
20554ed131d0SYishai Hadas 
20567c2344c3SMaor Gottlieb 
20577c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20587c2344c3SMaor Gottlieb {
20597c2344c3SMaor Gottlieb }
20607c2344c3SMaor Gottlieb 
206137aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2062e126ba97SEli Cohen {
206337aa5c36SGuy Levi 	switch (cmd) {
206437aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
206537aa5c36SGuy Levi 		return "WC";
2066e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
206737aa5c36SGuy Levi 		return "best effort WC";
206837aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
206937aa5c36SGuy Levi 		return "NC";
207024da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
207124da0016SAriel Levkovich 		return "Device Memory";
207237aa5c36SGuy Levi 	default:
207337aa5c36SGuy Levi 		return NULL;
207437aa5c36SGuy Levi 	}
207537aa5c36SGuy Levi }
207637aa5c36SGuy Levi 
20775c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20785c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20795c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20805c99eaecSFeras Daoud {
20814eb6ab13SJason Gunthorpe 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
20824eb6ab13SJason Gunthorpe 	    !(vma->vm_flags & VM_SHARED))
20835c99eaecSFeras Daoud 		return -EINVAL;
20845c99eaecSFeras Daoud 
20855c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20865c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20875c99eaecSFeras Daoud 
20884eb6ab13SJason Gunthorpe 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
20895c99eaecSFeras Daoud 		return -EPERM;
2090c660133cSJason Gunthorpe 	vma->vm_flags &= ~VM_MAYWRITE;
20915c99eaecSFeras Daoud 
2092ddcdc368SJason Gunthorpe 	if (!dev->mdev->clock_info)
20935c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20945c99eaecSFeras Daoud 
20954eb6ab13SJason Gunthorpe 	return vm_insert_page(vma, vma->vm_start,
20964eb6ab13SJason Gunthorpe 			      virt_to_page(dev->mdev->clock_info));
20975c99eaecSFeras Daoud }
20985c99eaecSFeras Daoud 
2099dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
2100dc2316ebSYishai Hadas {
2101dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
2102dc2316ebSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device);
21037be76befSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
2104dc2316ebSYishai Hadas 	struct mlx5_ib_dm *mdm;
2105dc2316ebSYishai Hadas 
2106dc2316ebSYishai Hadas 	switch (mentry->mmap_flag) {
2107dc2316ebSYishai Hadas 	case MLX5_IB_MMAP_TYPE_MEMIC:
2108dc2316ebSYishai Hadas 		mdm = container_of(mentry, struct mlx5_ib_dm, mentry);
2109dc2316ebSYishai Hadas 		mlx5_cmd_dealloc_memic(&dev->dm, mdm->dev_addr,
2110dc2316ebSYishai Hadas 				       mdm->size);
2111dc2316ebSYishai Hadas 		kfree(mdm);
2112dc2316ebSYishai Hadas 		break;
21137be76befSYishai Hadas 	case MLX5_IB_MMAP_TYPE_VAR:
21147be76befSYishai Hadas 		mutex_lock(&var_table->bitmap_lock);
21157be76befSYishai Hadas 		clear_bit(mentry->page_idx, var_table->bitmap);
21167be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
21177be76befSYishai Hadas 		kfree(mentry);
21187be76befSYishai Hadas 		break;
2119342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_WC:
2120342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_NC:
2121342ee59dSYishai Hadas 		mlx5_cmd_free_uar(dev->mdev, mentry->page_idx);
2122342ee59dSYishai Hadas 		kfree(mentry);
2123342ee59dSYishai Hadas 		break;
2124dc2316ebSYishai Hadas 	default:
2125dc2316ebSYishai Hadas 		WARN_ON(true);
2126dc2316ebSYishai Hadas 	}
2127dc2316ebSYishai Hadas }
2128dc2316ebSYishai Hadas 
212937aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
21307c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
21317c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
213237aa5c36SGuy Levi {
21332f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
213437aa5c36SGuy Levi 	int err;
213537aa5c36SGuy Levi 	unsigned long idx;
2136aa09ea6eSKamal Heib 	phys_addr_t pfn;
213737aa5c36SGuy Levi 	pgprot_t prot;
21384ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
21394ed131d0SYishai Hadas 	u32 uar_index;
21404ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
21414ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
21424ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2143b037c29aSEli Cohen 
21440a2fd01cSYishai Hadas 	if (bfregi->lib_uar_dyn)
21450a2fd01cSYishai Hadas 		return -EINVAL;
21460a2fd01cSYishai Hadas 
2147b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2148b037c29aSEli Cohen 		return -EINVAL;
2149b037c29aSEli Cohen 
21504ed131d0SYishai Hadas 	if (dyn_uar)
21514ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
21524ed131d0SYishai Hadas 	else
2153b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
21544ed131d0SYishai Hadas 
21554ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21564ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21574ed131d0SYishai Hadas 			     idx, max_valid_idx);
2158b037c29aSEli Cohen 		return -EINVAL;
2159b037c29aSEli Cohen 	}
216037aa5c36SGuy Levi 
216137aa5c36SGuy Levi 	switch (cmd) {
216237aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21634ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
216437aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
216537aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
216637aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
216737aa5c36SGuy Levi 		break;
216837aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
216937aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
217037aa5c36SGuy Levi 		break;
217137aa5c36SGuy Levi 	default:
217237aa5c36SGuy Levi 		return -EINVAL;
217337aa5c36SGuy Levi 	}
217437aa5c36SGuy Levi 
21754ed131d0SYishai Hadas 	if (dyn_uar) {
21764ed131d0SYishai Hadas 		int uars_per_page;
21774ed131d0SYishai Hadas 
21784ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21794ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21804ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21814ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21824ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21834ed131d0SYishai Hadas 			return -EINVAL;
21844ed131d0SYishai Hadas 		}
21854ed131d0SYishai Hadas 
21864ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21874ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21884ed131d0SYishai Hadas 		 * page holds its count.
21894ed131d0SYishai Hadas 		 */
21904ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
21914ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
21924ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
21934ed131d0SYishai Hadas 			return -EINVAL;
21944ed131d0SYishai Hadas 		}
21954ed131d0SYishai Hadas 
21964ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
21974ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
21984ed131d0SYishai Hadas 
21994ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
22004ed131d0SYishai Hadas 		if (err) {
22014ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
22024ed131d0SYishai Hadas 			goto free_bfreg;
22034ed131d0SYishai Hadas 		}
22044ed131d0SYishai Hadas 	} else {
22054ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
22064ed131d0SYishai Hadas 	}
22074ed131d0SYishai Hadas 
22084ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
220937aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2210e126ba97SEli Cohen 
2211e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2212c043ff2cSMichal Kalderon 				prot, NULL);
221337aa5c36SGuy Levi 	if (err) {
22148f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2215e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
22168f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
22174ed131d0SYishai Hadas 		goto err;
221837aa5c36SGuy Levi 	}
2219e126ba97SEli Cohen 
22204ed131d0SYishai Hadas 	if (dyn_uar)
22214ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
22224ed131d0SYishai Hadas 	return 0;
22234ed131d0SYishai Hadas 
22244ed131d0SYishai Hadas err:
22254ed131d0SYishai Hadas 	if (!dyn_uar)
22264ed131d0SYishai Hadas 		return err;
22274ed131d0SYishai Hadas 
22284ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
22294ed131d0SYishai Hadas 
22304ed131d0SYishai Hadas free_bfreg:
22314ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
22324ed131d0SYishai Hadas 
22334ed131d0SYishai Hadas 	return err;
223437aa5c36SGuy Levi }
223537aa5c36SGuy Levi 
2236dc2316ebSYishai Hadas static int add_dm_mmap_entry(struct ib_ucontext *context,
2237dc2316ebSYishai Hadas 			     struct mlx5_ib_dm *mdm,
2238dc2316ebSYishai Hadas 			     u64 address)
223924da0016SAriel Levkovich {
2240dc2316ebSYishai Hadas 	mdm->mentry.mmap_flag = MLX5_IB_MMAP_TYPE_MEMIC;
2241dc2316ebSYishai Hadas 	mdm->mentry.address = address;
2242dc2316ebSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
2243dc2316ebSYishai Hadas 			context, &mdm->mentry.rdma_entry,
2244dc2316ebSYishai Hadas 			mdm->size,
2245dc2316ebSYishai Hadas 			MLX5_IB_MMAP_DEVICE_MEM << 16,
2246dc2316ebSYishai Hadas 			(MLX5_IB_MMAP_DEVICE_MEM << 16) + (1UL << 16) - 1);
2247dc2316ebSYishai Hadas }
224824da0016SAriel Levkovich 
2249dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma)
2250dc2316ebSYishai Hadas {
2251dc2316ebSYishai Hadas 	unsigned long idx;
2252dc2316ebSYishai Hadas 	u8 command;
2253dc2316ebSYishai Hadas 
2254dc2316ebSYishai Hadas 	command = get_command(vma->vm_pgoff);
2255dc2316ebSYishai Hadas 	idx = get_extended_index(vma->vm_pgoff);
2256dc2316ebSYishai Hadas 
2257dc2316ebSYishai Hadas 	return (command << 16 | idx);
2258dc2316ebSYishai Hadas }
2259dc2316ebSYishai Hadas 
2260dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev,
2261dc2316ebSYishai Hadas 			       struct vm_area_struct *vma,
2262dc2316ebSYishai Hadas 			       struct ib_ucontext *ucontext)
2263dc2316ebSYishai Hadas {
2264dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry;
2265dc2316ebSYishai Hadas 	struct rdma_user_mmap_entry *entry;
2266dc2316ebSYishai Hadas 	unsigned long pgoff;
2267dc2316ebSYishai Hadas 	pgprot_t prot;
2268dc2316ebSYishai Hadas 	phys_addr_t pfn;
2269dc2316ebSYishai Hadas 	int ret;
2270dc2316ebSYishai Hadas 
2271dc2316ebSYishai Hadas 	pgoff = mlx5_vma_to_pgoff(vma);
2272dc2316ebSYishai Hadas 	entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff);
2273dc2316ebSYishai Hadas 	if (!entry)
227424da0016SAriel Levkovich 		return -EINVAL;
227524da0016SAriel Levkovich 
2276dc2316ebSYishai Hadas 	mentry = to_mmmap(entry);
2277dc2316ebSYishai Hadas 	pfn = (mentry->address >> PAGE_SHIFT);
2278342ee59dSYishai Hadas 	if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR ||
2279342ee59dSYishai Hadas 	    mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC)
22803f59b6c3SYishai Hadas 		prot = pgprot_noncached(vma->vm_page_prot);
22813f59b6c3SYishai Hadas 	else
2282dc2316ebSYishai Hadas 		prot = pgprot_writecombine(vma->vm_page_prot);
2283dc2316ebSYishai Hadas 	ret = rdma_user_mmap_io(ucontext, vma, pfn,
2284dc2316ebSYishai Hadas 				entry->npages * PAGE_SIZE,
2285dc2316ebSYishai Hadas 				prot,
2286dc2316ebSYishai Hadas 				entry);
2287dc2316ebSYishai Hadas 	rdma_user_mmap_entry_put(&mentry->rdma_entry);
2288dc2316ebSYishai Hadas 	return ret;
228924da0016SAriel Levkovich }
229024da0016SAriel Levkovich 
22917be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry)
22927be76befSYishai Hadas {
22939b6d3bbcSLeon Romanovsky 	u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF;
22949b6d3bbcSLeon Romanovsky 	u64 index = entry->rdma_entry.start_pgoff & 0xFFFF;
22957be76befSYishai Hadas 
22967be76befSYishai Hadas 	return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) |
22977be76befSYishai Hadas 		(index & 0xFF)) << PAGE_SHIFT;
22987be76befSYishai Hadas }
22997be76befSYishai Hadas 
230037aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
230137aa5c36SGuy Levi {
230237aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
230337aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
230437aa5c36SGuy Levi 	unsigned long command;
230537aa5c36SGuy Levi 	phys_addr_t pfn;
230637aa5c36SGuy Levi 
230737aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
230837aa5c36SGuy Levi 	switch (command) {
230937aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
23101f3db161SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
23111f3db161SYishai Hadas 		if (!dev->wc_support)
23121f3db161SYishai Hadas 			return -EPERM;
23131f3db161SYishai Hadas 		fallthrough;
231437aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
231537aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
23167c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2317e126ba97SEli Cohen 
2318e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2319e126ba97SEli Cohen 		return -ENOSYS;
2320e126ba97SEli Cohen 
2321d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2322d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2323d69e3bcfSMatan Barak 			return -EINVAL;
2324d69e3bcfSMatan Barak 
23256cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2326d69e3bcfSMatan Barak 			return -EPERM;
2327c660133cSJason Gunthorpe 		vma->vm_flags &= ~VM_MAYWRITE;
2328d69e3bcfSMatan Barak 
2329d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2330d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2331d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2332d69e3bcfSMatan Barak 
2333d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2334d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2335d69e3bcfSMatan Barak 			PAGE_SHIFT;
2336d5e560d3SJason Gunthorpe 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2337d5e560d3SJason Gunthorpe 					 PAGE_SIZE,
2338c043ff2cSMichal Kalderon 					 pgprot_noncached(vma->vm_page_prot),
2339c043ff2cSMichal Kalderon 					 NULL);
23405c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
23415c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2342d69e3bcfSMatan Barak 
2343e126ba97SEli Cohen 	default:
2344dc2316ebSYishai Hadas 		return mlx5_ib_mmap_offset(dev, vma, ibcontext);
2345e126ba97SEli Cohen 	}
2346e126ba97SEli Cohen 
2347e126ba97SEli Cohen 	return 0;
2348e126ba97SEli Cohen }
2349e126ba97SEli Cohen 
235025c13324SAriel Levkovich static inline int check_dm_type_support(struct mlx5_ib_dev *dev,
235125c13324SAriel Levkovich 					u32 type)
235225c13324SAriel Levkovich {
235325c13324SAriel Levkovich 	switch (type) {
235425c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
235525c13324SAriel Levkovich 		if (!MLX5_CAP_DEV_MEM(dev->mdev, memic))
235625c13324SAriel Levkovich 			return -EOPNOTSUPP;
235725c13324SAriel Levkovich 		break;
235825c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2359c9b9dcb4SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
236025c13324SAriel Levkovich 		if (!capable(CAP_SYS_RAWIO) ||
236125c13324SAriel Levkovich 		    !capable(CAP_NET_RAW))
236225c13324SAriel Levkovich 			return -EPERM;
236325c13324SAriel Levkovich 
236425c13324SAriel Levkovich 		if (!(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner) ||
23658310e327SAlex Vesker 		      MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner) ||
23668310e327SAlex Vesker 		      MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner_v2) ||
23678310e327SAlex Vesker 		      MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner_v2)))
236825c13324SAriel Levkovich 			return -EOPNOTSUPP;
236925c13324SAriel Levkovich 		break;
237025c13324SAriel Levkovich 	}
237125c13324SAriel Levkovich 
237225c13324SAriel Levkovich 	return 0;
237325c13324SAriel Levkovich }
237425c13324SAriel Levkovich 
23753b113a1eSAriel Levkovich static int handle_alloc_dm_memic(struct ib_ucontext *ctx,
23763b113a1eSAriel Levkovich 				 struct mlx5_ib_dm *dm,
237724da0016SAriel Levkovich 				 struct ib_dm_alloc_attr *attr,
237824da0016SAriel Levkovich 				 struct uverbs_attr_bundle *attrs)
237924da0016SAriel Levkovich {
23803b113a1eSAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
238124da0016SAriel Levkovich 	u64 start_offset;
2382dc2316ebSYishai Hadas 	u16 page_idx;
238324da0016SAriel Levkovich 	int err;
2384dc2316ebSYishai Hadas 	u64 address;
238524da0016SAriel Levkovich 
23863b113a1eSAriel Levkovich 	dm->size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
238724da0016SAriel Levkovich 
23883b113a1eSAriel Levkovich 	err = mlx5_cmd_alloc_memic(dm_db, &dm->dev_addr,
23893b113a1eSAriel Levkovich 				   dm->size, attr->alignment);
239024da0016SAriel Levkovich 	if (err)
23913b113a1eSAriel Levkovich 		return err;
239224da0016SAriel Levkovich 
2393dc2316ebSYishai Hadas 	address = dm->dev_addr & PAGE_MASK;
2394dc2316ebSYishai Hadas 	err = add_dm_mmap_entry(ctx, dm, address);
239524da0016SAriel Levkovich 	if (err)
239624da0016SAriel Levkovich 		goto err_dealloc;
239724da0016SAriel Levkovich 
2398dc2316ebSYishai Hadas 	page_idx = dm->mentry.rdma_entry.start_pgoff & 0xFFFF;
2399dc2316ebSYishai Hadas 	err = uverbs_copy_to(attrs,
2400dc2316ebSYishai Hadas 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2401dc2316ebSYishai Hadas 			     &page_idx,
2402dc2316ebSYishai Hadas 			     sizeof(page_idx));
2403dc2316ebSYishai Hadas 	if (err)
2404dc2316ebSYishai Hadas 		goto err_copy;
2405dc2316ebSYishai Hadas 
24063b113a1eSAriel Levkovich 	start_offset = dm->dev_addr & ~PAGE_MASK;
24073b113a1eSAriel Levkovich 	err = uverbs_copy_to(attrs,
24083b113a1eSAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
24093b113a1eSAriel Levkovich 			     &start_offset, sizeof(start_offset));
24103b113a1eSAriel Levkovich 	if (err)
2411dc2316ebSYishai Hadas 		goto err_copy;
24123b113a1eSAriel Levkovich 
24133b113a1eSAriel Levkovich 	return 0;
24143b113a1eSAriel Levkovich 
2415dc2316ebSYishai Hadas err_copy:
2416dc2316ebSYishai Hadas 	rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
24173b113a1eSAriel Levkovich err_dealloc:
24183b113a1eSAriel Levkovich 	mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
24193b113a1eSAriel Levkovich 
24203b113a1eSAriel Levkovich 	return err;
24213b113a1eSAriel Levkovich }
24223b113a1eSAriel Levkovich 
242325c13324SAriel Levkovich static int handle_alloc_dm_sw_icm(struct ib_ucontext *ctx,
242425c13324SAriel Levkovich 				  struct mlx5_ib_dm *dm,
242525c13324SAriel Levkovich 				  struct ib_dm_alloc_attr *attr,
242625c13324SAriel Levkovich 				  struct uverbs_attr_bundle *attrs,
242725c13324SAriel Levkovich 				  int type)
242825c13324SAriel Levkovich {
2429c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ctx->device)->mdev;
243025c13324SAriel Levkovich 	u64 act_size;
243125c13324SAriel Levkovich 	int err;
243225c13324SAriel Levkovich 
243325c13324SAriel Levkovich 	/* Allocation size must a multiple of the basic block size
243425c13324SAriel Levkovich 	 * and a power of 2.
243525c13324SAriel Levkovich 	 */
2436c9b9dcb4SAriel Levkovich 	act_size = round_up(attr->length, MLX5_SW_ICM_BLOCK_SIZE(dev));
243725c13324SAriel Levkovich 	act_size = roundup_pow_of_two(act_size);
243825c13324SAriel Levkovich 
243925c13324SAriel Levkovich 	dm->size = act_size;
2440dff8e2d1SErez Shitrit 	err = mlx5_dm_sw_icm_alloc(dev, type, act_size, attr->alignment,
244125c13324SAriel Levkovich 				   to_mucontext(ctx)->devx_uid, &dm->dev_addr,
244225c13324SAriel Levkovich 				   &dm->icm_dm.obj_id);
244325c13324SAriel Levkovich 	if (err)
244425c13324SAriel Levkovich 		return err;
244525c13324SAriel Levkovich 
244625c13324SAriel Levkovich 	err = uverbs_copy_to(attrs,
244725c13324SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
244825c13324SAriel Levkovich 			     &dm->dev_addr, sizeof(dm->dev_addr));
244925c13324SAriel Levkovich 	if (err)
2450c9b9dcb4SAriel Levkovich 		mlx5_dm_sw_icm_dealloc(dev, type, dm->size,
2451c9b9dcb4SAriel Levkovich 				       to_mucontext(ctx)->devx_uid, dm->dev_addr,
2452c9b9dcb4SAriel Levkovich 				       dm->icm_dm.obj_id);
245325c13324SAriel Levkovich 
245425c13324SAriel Levkovich 	return err;
245525c13324SAriel Levkovich }
245625c13324SAriel Levkovich 
24573b113a1eSAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
24583b113a1eSAriel Levkovich 			       struct ib_ucontext *context,
24593b113a1eSAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
24603b113a1eSAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
24613b113a1eSAriel Levkovich {
24623b113a1eSAriel Levkovich 	struct mlx5_ib_dm *dm;
24633b113a1eSAriel Levkovich 	enum mlx5_ib_uapi_dm_type type;
24643b113a1eSAriel Levkovich 	int err;
24653b113a1eSAriel Levkovich 
24663b113a1eSAriel Levkovich 	err = uverbs_get_const_default(&type, attrs,
24673b113a1eSAriel Levkovich 				       MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
24683b113a1eSAriel Levkovich 				       MLX5_IB_UAPI_DM_TYPE_MEMIC);
24693b113a1eSAriel Levkovich 	if (err)
24703b113a1eSAriel Levkovich 		return ERR_PTR(err);
24713b113a1eSAriel Levkovich 
24723b113a1eSAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_dm req: dm_type=%d user_length=0x%llx log_alignment=%d\n",
24733b113a1eSAriel Levkovich 		    type, attr->length, attr->alignment);
24743b113a1eSAriel Levkovich 
247525c13324SAriel Levkovich 	err = check_dm_type_support(to_mdev(ibdev), type);
247625c13324SAriel Levkovich 	if (err)
247725c13324SAriel Levkovich 		return ERR_PTR(err);
247825c13324SAriel Levkovich 
24793b113a1eSAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
24803b113a1eSAriel Levkovich 	if (!dm)
24813b113a1eSAriel Levkovich 		return ERR_PTR(-ENOMEM);
24823b113a1eSAriel Levkovich 
24833b113a1eSAriel Levkovich 	dm->type = type;
24843b113a1eSAriel Levkovich 
24853b113a1eSAriel Levkovich 	switch (type) {
24863b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
24873b113a1eSAriel Levkovich 		err = handle_alloc_dm_memic(context, dm,
24883b113a1eSAriel Levkovich 					    attr,
24893b113a1eSAriel Levkovich 					    attrs);
24903b113a1eSAriel Levkovich 		break;
249125c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2492c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2493c9b9dcb4SAriel Levkovich 					     attr, attrs,
2494c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_STEERING);
2495c9b9dcb4SAriel Levkovich 		break;
249625c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2497c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2498c9b9dcb4SAriel Levkovich 					     attr, attrs,
2499c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_HEADER_MODIFY);
250025c13324SAriel Levkovich 		break;
25013b113a1eSAriel Levkovich 	default:
25023b113a1eSAriel Levkovich 		err = -EOPNOTSUPP;
25033b113a1eSAriel Levkovich 	}
25043b113a1eSAriel Levkovich 
25053b113a1eSAriel Levkovich 	if (err)
25063b113a1eSAriel Levkovich 		goto err_free;
250724da0016SAriel Levkovich 
250824da0016SAriel Levkovich 	return &dm->ibdm;
250924da0016SAriel Levkovich 
251024da0016SAriel Levkovich err_free:
251124da0016SAriel Levkovich 	kfree(dm);
251224da0016SAriel Levkovich 	return ERR_PTR(err);
251324da0016SAriel Levkovich }
251424da0016SAriel Levkovich 
2515c4367a26SShamir Rabinovitch int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
251624da0016SAriel Levkovich {
251725c13324SAriel Levkovich 	struct mlx5_ib_ucontext *ctx = rdma_udata_to_drv_context(
251825c13324SAriel Levkovich 		&attrs->driver_udata, struct mlx5_ib_ucontext, ibucontext);
2519c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ibdm->device)->mdev;
252024da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
252124da0016SAriel Levkovich 	int ret;
252224da0016SAriel Levkovich 
25233b113a1eSAriel Levkovich 	switch (dm->type) {
25243b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
2525dc2316ebSYishai Hadas 		rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
2526dc2316ebSYishai Hadas 		return 0;
252725c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2528c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_STEERING,
2529c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
2530c9b9dcb4SAriel Levkovich 					     dm->icm_dm.obj_id);
2531c9b9dcb4SAriel Levkovich 		if (ret)
2532c9b9dcb4SAriel Levkovich 			return ret;
2533c9b9dcb4SAriel Levkovich 		break;
253425c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2535c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_HEADER_MODIFY,
2536c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
253725c13324SAriel Levkovich 					     dm->icm_dm.obj_id);
253825c13324SAriel Levkovich 		if (ret)
253925c13324SAriel Levkovich 			return ret;
25403b113a1eSAriel Levkovich 		break;
25413b113a1eSAriel Levkovich 	default:
25423b113a1eSAriel Levkovich 		return -EOPNOTSUPP;
25433b113a1eSAriel Levkovich 	}
254424da0016SAriel Levkovich 
254524da0016SAriel Levkovich 	kfree(dm);
254624da0016SAriel Levkovich 
254724da0016SAriel Levkovich 	return 0;
254824da0016SAriel Levkovich }
254924da0016SAriel Levkovich 
2550ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2551e126ba97SEli Cohen {
255221a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
255321a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2554e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2555e126ba97SEli Cohen 	int err;
2556a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2557a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2558a1069c1cSYishai Hadas 	u16 uid = 0;
2559ff23dfa1SShamir Rabinovitch 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2560ff23dfa1SShamir Rabinovitch 		udata, struct mlx5_ib_ucontext, ibucontext);
2561e126ba97SEli Cohen 
2562ff23dfa1SShamir Rabinovitch 	uid = context ? context->devx_uid : 0;
2563a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2564a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
256531578defSLeon Romanovsky 	err = mlx5_cmd_exec_inout(to_mdev(ibdev)->mdev, alloc_pd, in, out);
256621a428a0SLeon Romanovsky 	if (err)
256721a428a0SLeon Romanovsky 		return err;
2568e126ba97SEli Cohen 
2569a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2570a1069c1cSYishai Hadas 	pd->uid = uid;
2571ff23dfa1SShamir Rabinovitch 	if (udata) {
2572e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2573e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2574a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
257521a428a0SLeon Romanovsky 			return -EFAULT;
2576e126ba97SEli Cohen 		}
2577e126ba97SEli Cohen 	}
2578e126ba97SEli Cohen 
257921a428a0SLeon Romanovsky 	return 0;
2580e126ba97SEli Cohen }
2581e126ba97SEli Cohen 
258291a7c58fSLeon Romanovsky static int mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2583e126ba97SEli Cohen {
2584e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2585e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2586e126ba97SEli Cohen 
258791a7c58fSLeon Romanovsky 	return mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2588e126ba97SEli Cohen }
2589e126ba97SEli Cohen 
2590e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2591e126ba97SEli Cohen {
2592e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
259381e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
2594e126ba97SEli Cohen 	int err;
2595539ec982SYishai Hadas 	u16 uid;
2596539ec982SYishai Hadas 
2597539ec982SYishai Hadas 	uid = ibqp->pd ?
2598539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
2599e126ba97SEli Cohen 
26002be08c30SLeon Romanovsky 	if (mqp->flags & IB_QP_CREATE_SOURCE_QPN) {
260181e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
260281e30880SYishai Hadas 		return -EOPNOTSUPP;
260381e30880SYishai Hadas 	}
260481e30880SYishai Hadas 
2605539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2606e126ba97SEli Cohen 	if (err)
2607e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2608e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2609e126ba97SEli Cohen 
2610e126ba97SEli Cohen 	return err;
2611e126ba97SEli Cohen }
2612e126ba97SEli Cohen 
2613e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2614e126ba97SEli Cohen {
2615e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2616e126ba97SEli Cohen 	int err;
2617539ec982SYishai Hadas 	u16 uid;
2618e126ba97SEli Cohen 
2619539ec982SYishai Hadas 	uid = ibqp->pd ?
2620539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
2621539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2622e126ba97SEli Cohen 	if (err)
2623e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2624e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2625e126ba97SEli Cohen 
2626e126ba97SEli Cohen 	return err;
2627e126ba97SEli Cohen }
2628e126ba97SEli Cohen 
2629e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2630e126ba97SEli Cohen {
26311b5daf11SMajd Dibbiny 	int err;
2632e126ba97SEli Cohen 
26331b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2634e126ba97SEli Cohen 	if (err)
2635e126ba97SEli Cohen 		return err;
26361b5daf11SMajd Dibbiny 
26371b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
26381b5daf11SMajd Dibbiny 
26391b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2640e126ba97SEli Cohen }
2641e126ba97SEli Cohen 
2642508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
2643508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2644e126ba97SEli Cohen {
2645e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
264654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2647e126ba97SEli Cohen 
26481c7fd726SJoe Perches 	return sysfs_emit(buf, "%d\n", dev->mdev->priv.fw_pages);
2649e126ba97SEli Cohen }
2650508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
2651e126ba97SEli Cohen 
2652508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
2653e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2654e126ba97SEli Cohen {
2655e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
265654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2657e126ba97SEli Cohen 
26581c7fd726SJoe Perches 	return sysfs_emit(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2659e126ba97SEli Cohen }
2660508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
2661e126ba97SEli Cohen 
2662508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
2663508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2664e126ba97SEli Cohen {
2665e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
266654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
266754747231SParav Pandit 
26681c7fd726SJoe Perches 	return sysfs_emit(buf, "MT%d\n", dev->mdev->pdev->device);
2669e126ba97SEli Cohen }
2670508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
2671e126ba97SEli Cohen 
2672508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
2673508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
2674e126ba97SEli Cohen {
2675e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
267654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
267754747231SParav Pandit 
26781c7fd726SJoe Perches 	return sysfs_emit(buf, "%x\n", dev->mdev->rev_id);
2679e126ba97SEli Cohen }
2680508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
2681e126ba97SEli Cohen 
2682508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
2683508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2684e126ba97SEli Cohen {
2685e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
268654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
268754747231SParav Pandit 
26881c7fd726SJoe Perches 	return sysfs_emit(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
26899603b61dSJack Morgenstein 			  dev->mdev->board_id);
2690e126ba97SEli Cohen }
2691508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
2692e126ba97SEli Cohen 
2693508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
2694508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
2695508a523fSParav Pandit 	&dev_attr_hca_type.attr,
2696508a523fSParav Pandit 	&dev_attr_board_id.attr,
2697508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
2698508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
2699508a523fSParav Pandit 	NULL,
2700508a523fSParav Pandit };
2701e126ba97SEli Cohen 
2702508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
2703508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
2704e126ba97SEli Cohen };
2705e126ba97SEli Cohen 
27067722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
27077722f47eSHaggai Eran {
27087722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
27097722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
27107722f47eSHaggai Eran 			     pkey_change_work);
27117722f47eSHaggai Eran 
27127722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
27137722f47eSHaggai Eran }
27147722f47eSHaggai Eran 
271589ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
271689ea94a7SMaor Gottlieb {
271789ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
271889ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
271989ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
272089ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
272189ea94a7SMaor Gottlieb 	unsigned long flags_qp;
272289ea94a7SMaor Gottlieb 	unsigned long flags_cq;
272389ea94a7SMaor Gottlieb 	unsigned long flags;
272489ea94a7SMaor Gottlieb 
272589ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
272689ea94a7SMaor Gottlieb 
272789ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
272889ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
272989ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
273089ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
273189ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
273289ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
273389ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
273489ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
273589ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
273689ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
273789ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
273889ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
273989ea94a7SMaor Gottlieb 						      &cq_armed_list);
274089ea94a7SMaor Gottlieb 				}
274189ea94a7SMaor Gottlieb 			}
274289ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
274389ea94a7SMaor Gottlieb 		}
274489ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
274589ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
274689ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
274789ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
274889ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
274989ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
275089ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
275189ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
275289ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
275389ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
275489ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
275589ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
275689ea94a7SMaor Gottlieb 							      &cq_armed_list);
275789ea94a7SMaor Gottlieb 					}
275889ea94a7SMaor Gottlieb 				}
275989ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
276089ea94a7SMaor Gottlieb 						       flags_cq);
276189ea94a7SMaor Gottlieb 			}
276289ea94a7SMaor Gottlieb 		}
276389ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
276489ea94a7SMaor Gottlieb 	}
276589ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
276689ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
276789ea94a7SMaor Gottlieb 	 */
276889ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
27694e0e2ea1SYishai Hadas 		mcq->comp(mcq, NULL);
277089ea94a7SMaor Gottlieb 	}
277189ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
277289ea94a7SMaor Gottlieb }
277389ea94a7SMaor Gottlieb 
277403404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
277503404e8aSMaor Gottlieb {
277603404e8aSMaor Gottlieb 	int err;
277703404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
277803404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
277903404e8aSMaor Gottlieb 			     delay_drop_work);
278003404e8aSMaor Gottlieb 
2781fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
2782fe248c3aSMaor Gottlieb 
278303404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
2784333fbaa0SLeon Romanovsky 	err = mlx5_core_set_delay_drop(delay_drop->dev, delay_drop->timeout);
278503404e8aSMaor Gottlieb 	if (err) {
278603404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
278703404e8aSMaor Gottlieb 			     delay_drop->timeout);
278803404e8aSMaor Gottlieb 		delay_drop->activate = false;
278903404e8aSMaor Gottlieb 	}
279003404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
279103404e8aSMaor Gottlieb }
279203404e8aSMaor Gottlieb 
279309e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
279409e574faSSaeed Mahameed 				 struct ib_event *ibev)
279509e574faSSaeed Mahameed {
27966cfdc7e4SAya Levin 	u8 port = (eqe->data.port.port >> 4) & 0xf;
27976cfdc7e4SAya Levin 
279809e574faSSaeed Mahameed 	switch (eqe->sub_type) {
279909e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
28006cfdc7e4SAya Levin 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
28016cfdc7e4SAya Levin 					    IB_LINK_LAYER_ETHERNET)
280209e574faSSaeed Mahameed 			schedule_work(&ibdev->delay_drop.delay_drop_work);
280309e574faSSaeed Mahameed 		break;
280409e574faSSaeed Mahameed 	default: /* do nothing */
280509e574faSSaeed Mahameed 		return;
280609e574faSSaeed Mahameed 	}
280709e574faSSaeed Mahameed }
280809e574faSSaeed Mahameed 
2809134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
2810134e9349SSaeed Mahameed 			      struct ib_event *ibev)
2811134e9349SSaeed Mahameed {
2812134e9349SSaeed Mahameed 	u8 port = (eqe->data.port.port >> 4) & 0xf;
2813134e9349SSaeed Mahameed 
2814134e9349SSaeed Mahameed 	ibev->element.port_num = port;
2815134e9349SSaeed Mahameed 
2816134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
2817134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
2818134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
2819134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
2820134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
2821134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
2822134e9349SSaeed Mahameed 		 */
2823134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2824134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
2825134e9349SSaeed Mahameed 			return -EINVAL;
2826134e9349SSaeed Mahameed 
2827134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
2828134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2829134e9349SSaeed Mahameed 		break;
2830134e9349SSaeed Mahameed 
2831134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
2832134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
2833134e9349SSaeed Mahameed 		break;
2834134e9349SSaeed Mahameed 
2835134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
2836134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
2837134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2838134e9349SSaeed Mahameed 		break;
2839134e9349SSaeed Mahameed 
2840134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
2841134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
2842134e9349SSaeed Mahameed 		break;
2843134e9349SSaeed Mahameed 
2844134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
2845134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
2846134e9349SSaeed Mahameed 		break;
2847134e9349SSaeed Mahameed 	default:
2848134e9349SSaeed Mahameed 		return -EINVAL;
2849134e9349SSaeed Mahameed 	}
2850134e9349SSaeed Mahameed 
2851134e9349SSaeed Mahameed 	return 0;
2852134e9349SSaeed Mahameed }
2853134e9349SSaeed Mahameed 
2854d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
2855e126ba97SEli Cohen {
2856d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
2857d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
2858d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
2859e126ba97SEli Cohen 	struct ib_event ibev;
2860dbaaff2aSEli Cohen 	bool fatal = false;
2861e126ba97SEli Cohen 
2862df097a27SSaeed Mahameed 	if (work->is_slave) {
2863df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
2864d69a24e0SDaniel Jurgens 		if (!ibdev)
2865d69a24e0SDaniel Jurgens 			goto out;
2866d69a24e0SDaniel Jurgens 	} else {
2867df097a27SSaeed Mahameed 		ibdev = work->dev;
2868d69a24e0SDaniel Jurgens 	}
2869d69a24e0SDaniel Jurgens 
2870d69a24e0SDaniel Jurgens 	switch (work->event) {
2871e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2872e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
287389ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2874134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
2875dbaaff2aSEli Cohen 		fatal = true;
2876e126ba97SEli Cohen 		break;
2877134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
2878134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
2879d69a24e0SDaniel Jurgens 			goto out;
2880e126ba97SEli Cohen 		break;
288109e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
288209e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
2883df561f66SGustavo A. R. Silva 		fallthrough;
2884bdc37924SSaeed Mahameed 	default:
288503404e8aSMaor Gottlieb 		goto out;
2886e126ba97SEli Cohen 	}
2887e126ba97SEli Cohen 
2888e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
2889e126ba97SEli Cohen 
2890134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
2891134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
289203404e8aSMaor Gottlieb 		goto out;
2893a0c84c32SEli Cohen 	}
2894a0c84c32SEli Cohen 
2895e126ba97SEli Cohen 	if (ibdev->ib_active)
2896e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2897dbaaff2aSEli Cohen 
2898dbaaff2aSEli Cohen 	if (fatal)
2899dbaaff2aSEli Cohen 		ibdev->ib_active = false;
290003404e8aSMaor Gottlieb out:
2901d69a24e0SDaniel Jurgens 	kfree(work);
2902d69a24e0SDaniel Jurgens }
2903d69a24e0SDaniel Jurgens 
2904df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
2905df097a27SSaeed Mahameed 			 unsigned long event, void *param)
2906d69a24e0SDaniel Jurgens {
2907d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
2908d69a24e0SDaniel Jurgens 
2909d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
291010bea9c8SLeon Romanovsky 	if (!work)
2911df097a27SSaeed Mahameed 		return NOTIFY_DONE;
291210bea9c8SLeon Romanovsky 
2913d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
2914df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
2915df097a27SSaeed Mahameed 	work->is_slave = false;
2916d69a24e0SDaniel Jurgens 	work->param = param;
2917d69a24e0SDaniel Jurgens 	work->event = event;
2918d69a24e0SDaniel Jurgens 
2919d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
2920df097a27SSaeed Mahameed 
2921df097a27SSaeed Mahameed 	return NOTIFY_OK;
2922df097a27SSaeed Mahameed }
2923df097a27SSaeed Mahameed 
2924df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
2925df097a27SSaeed Mahameed 				    unsigned long event, void *param)
2926df097a27SSaeed Mahameed {
2927df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
2928df097a27SSaeed Mahameed 
2929df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
2930df097a27SSaeed Mahameed 	if (!work)
2931df097a27SSaeed Mahameed 		return NOTIFY_DONE;
2932df097a27SSaeed Mahameed 
2933df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
2934df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
2935df097a27SSaeed Mahameed 	work->is_slave = true;
2936df097a27SSaeed Mahameed 	work->param = param;
2937df097a27SSaeed Mahameed 	work->event = event;
2938df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
2939df097a27SSaeed Mahameed 
2940df097a27SSaeed Mahameed 	return NOTIFY_OK;
2941d69a24e0SDaniel Jurgens }
2942d69a24e0SDaniel Jurgens 
2943c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2944c43f1112SMaor Gottlieb {
2945c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
2946c43f1112SMaor Gottlieb 	int err;
2947c43f1112SMaor Gottlieb 	int port;
2948c43f1112SMaor Gottlieb 
2949a989ea01SMark Bloch 	for (port = 1; port <= ARRAY_SIZE(dev->mdev->port_caps); port++) {
2950c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
2951c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2952c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
2953c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2954c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
2955c43f1112SMaor Gottlieb 								   port, 0,
2956c43f1112SMaor Gottlieb 								   &vport_ctx);
2957c43f1112SMaor Gottlieb 				if (err) {
2958c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2959c43f1112SMaor Gottlieb 						    port, err);
2960c43f1112SMaor Gottlieb 					return err;
2961c43f1112SMaor Gottlieb 				}
2962c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
2963c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
2964c43f1112SMaor Gottlieb 			} else {
2965c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
2966c43f1112SMaor Gottlieb 			}
2967c43f1112SMaor Gottlieb 		}
2968c43f1112SMaor Gottlieb 	}
2969c43f1112SMaor Gottlieb 	return 0;
2970c43f1112SMaor Gottlieb }
2971c43f1112SMaor Gottlieb 
2972e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2973e126ba97SEli Cohen {
2974e126ba97SEli Cohen 	int port;
2975e126ba97SEli Cohen 
2976508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
2977e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
2978e126ba97SEli Cohen }
2979e126ba97SEli Cohen 
298026628e2dSMark Bloch static int __get_port_caps(struct mlx5_ib_dev *dev, u8 port)
2981e126ba97SEli Cohen {
2982e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
2983e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
2984f614fc15SDan Carpenter 	int err = -ENOMEM;
2985e126ba97SEli Cohen 
298650ba3c18SLeon Romanovsky 	pprops = kzalloc(sizeof(*pprops), GFP_KERNEL);
2987e126ba97SEli Cohen 	if (!pprops)
2988e126ba97SEli Cohen 		goto out;
2989e126ba97SEli Cohen 
2990e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2991e126ba97SEli Cohen 	if (!dprops)
2992e126ba97SEli Cohen 		goto out;
2993e126ba97SEli Cohen 
299448357091SLeon Romanovsky 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, NULL);
2995e126ba97SEli Cohen 	if (err) {
2996e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
2997e126ba97SEli Cohen 		goto out;
2998e126ba97SEli Cohen 	}
2999e126ba97SEli Cohen 
3000e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3001e126ba97SEli Cohen 	if (err) {
3002938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
3003938fe83cSSaeed Mahameed 			     port, err);
300432f69e4bSDaniel Jurgens 		goto out;
3005e126ba97SEli Cohen 	}
300632f69e4bSDaniel Jurgens 
3007938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
3008938fe83cSSaeed Mahameed 					dprops->max_pkeys;
3009938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
3010938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
301132f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
301232f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
3013e126ba97SEli Cohen 
3014e126ba97SEli Cohen out:
3015e126ba97SEli Cohen 	kfree(pprops);
3016e126ba97SEli Cohen 	kfree(dprops);
3017e126ba97SEli Cohen 
3018e126ba97SEli Cohen 	return err;
3019e126ba97SEli Cohen }
3020e126ba97SEli Cohen 
302126628e2dSMark Bloch static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
302226628e2dSMark Bloch {
302326628e2dSMark Bloch 	/* For representors use port 1, is this is the only native
302426628e2dSMark Bloch 	 * port
302526628e2dSMark Bloch 	 */
302626628e2dSMark Bloch 	if (dev->is_rep)
302726628e2dSMark Bloch 		return __get_port_caps(dev, 1);
302826628e2dSMark Bloch 	return __get_port_caps(dev, port);
302926628e2dSMark Bloch }
303026628e2dSMark Bloch 
30316e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
30326e8484c5SMax Gurtovoy {
30336e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
30346e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
30356e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
30366e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
30376e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
30386e8484c5SMax Gurtovoy 	default:
30396e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
30406e8484c5SMax Gurtovoy 	}
30416e8484c5SMax Gurtovoy }
30426e8484c5SMax Gurtovoy 
30431e2b5a90SLeon Romanovsky static int mlx5_ib_dev_res_init(struct mlx5_ib_dev *dev)
3044e126ba97SEli Cohen {
30451e2b5a90SLeon Romanovsky 	struct mlx5_ib_resources *devr = &dev->devr;
3046e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
304721a428a0SLeon Romanovsky 	struct ib_device *ibdev;
3048bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
30497722f47eSHaggai Eran 	int port;
3050e126ba97SEli Cohen 	int ret = 0;
3051e126ba97SEli Cohen 
305221a428a0SLeon Romanovsky 	ibdev = &dev->ib_dev;
3053e126ba97SEli Cohen 
3054f4375443SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, xrc))
3055f4375443SLeon Romanovsky 		return -EOPNOTSUPP;
3056f4375443SLeon Romanovsky 
3057d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
3058d16e91daSHaggai Eran 
305921a428a0SLeon Romanovsky 	devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
306021a428a0SLeon Romanovsky 	if (!devr->p0)
306121a428a0SLeon Romanovsky 		return -ENOMEM;
306221a428a0SLeon Romanovsky 
306321a428a0SLeon Romanovsky 	devr->p0->device  = ibdev;
3064e126ba97SEli Cohen 	devr->p0->uobject = NULL;
3065e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
3066e126ba97SEli Cohen 
3067ff23dfa1SShamir Rabinovitch 	ret = mlx5_ib_alloc_pd(devr->p0, NULL);
306821a428a0SLeon Romanovsky 	if (ret)
306921a428a0SLeon Romanovsky 		goto error0;
307021a428a0SLeon Romanovsky 
3071e39afe3dSLeon Romanovsky 	devr->c0 = rdma_zalloc_drv_obj(ibdev, ib_cq);
3072e39afe3dSLeon Romanovsky 	if (!devr->c0) {
3073e39afe3dSLeon Romanovsky 		ret = -ENOMEM;
3074e126ba97SEli Cohen 		goto error1;
3075e126ba97SEli Cohen 	}
3076e39afe3dSLeon Romanovsky 
3077e126ba97SEli Cohen 	devr->c0->device = &dev->ib_dev;
3078e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
3079e126ba97SEli Cohen 
3080e39afe3dSLeon Romanovsky 	ret = mlx5_ib_create_cq(devr->c0, &cq_attr, NULL);
3081e39afe3dSLeon Romanovsky 	if (ret)
3082e39afe3dSLeon Romanovsky 		goto err_create_cq;
3083e39afe3dSLeon Romanovsky 
3084f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn0, 0);
3085f4375443SLeon Romanovsky 	if (ret)
3086e126ba97SEli Cohen 		goto error2;
3087e126ba97SEli Cohen 
3088f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn1, 0);
3089f4375443SLeon Romanovsky 	if (ret)
3090e126ba97SEli Cohen 		goto error3;
3091e126ba97SEli Cohen 
3092e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
3093e126ba97SEli Cohen 	attr.attr.max_sge = 1;
3094e126ba97SEli Cohen 	attr.attr.max_wr = 1;
3095e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
30961a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
3097e126ba97SEli Cohen 
309868e326deSLeon Romanovsky 	devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
309968e326deSLeon Romanovsky 	if (!devr->s0) {
310068e326deSLeon Romanovsky 		ret = -ENOMEM;
3101e126ba97SEli Cohen 		goto error4;
3102e126ba97SEli Cohen 	}
310368e326deSLeon Romanovsky 
3104e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
3105e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
3106e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
31071a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
310868e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
310968e326deSLeon Romanovsky 	if (ret)
311068e326deSLeon Romanovsky 		goto err_create;
311168e326deSLeon Romanovsky 
31121a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
3113e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
3114e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
3115e126ba97SEli Cohen 
31164aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
31174aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
31184aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
31194aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
312068e326deSLeon Romanovsky 	devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
312168e326deSLeon Romanovsky 	if (!devr->s1) {
312268e326deSLeon Romanovsky 		ret = -ENOMEM;
31234aa17b28SHaggai Abramonvsky 		goto error5;
31244aa17b28SHaggai Abramonvsky 	}
312568e326deSLeon Romanovsky 
31264aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
31274aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
31284aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
31291a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
313068e326deSLeon Romanovsky 
313168e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
313268e326deSLeon Romanovsky 	if (ret)
313368e326deSLeon Romanovsky 		goto error6;
313468e326deSLeon Romanovsky 
31354aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
31361a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
31374aa17b28SHaggai Abramonvsky 
3138f8225e34SLeon Romanovsky 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
31397722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
31407722f47eSHaggai Eran 			  pkey_change_handler);
31417722f47eSHaggai Eran 
3142e126ba97SEli Cohen 	return 0;
3143e126ba97SEli Cohen 
314468e326deSLeon Romanovsky error6:
314568e326deSLeon Romanovsky 	kfree(devr->s1);
31464aa17b28SHaggai Abramonvsky error5:
3147c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
314868e326deSLeon Romanovsky err_create:
314968e326deSLeon Romanovsky 	kfree(devr->s0);
3150e126ba97SEli Cohen error4:
3151f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
3152e126ba97SEli Cohen error3:
3153f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
3154e126ba97SEli Cohen error2:
3155c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
3156e39afe3dSLeon Romanovsky err_create_cq:
3157e39afe3dSLeon Romanovsky 	kfree(devr->c0);
3158e126ba97SEli Cohen error1:
3159c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
3160e126ba97SEli Cohen error0:
316121a428a0SLeon Romanovsky 	kfree(devr->p0);
3162e126ba97SEli Cohen 	return ret;
3163e126ba97SEli Cohen }
3164e126ba97SEli Cohen 
31651e2b5a90SLeon Romanovsky static void mlx5_ib_dev_res_cleanup(struct mlx5_ib_dev *dev)
3166e126ba97SEli Cohen {
31671e2b5a90SLeon Romanovsky 	struct mlx5_ib_resources *devr = &dev->devr;
31687722f47eSHaggai Eran 	int port;
31697722f47eSHaggai Eran 
3170c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s1, NULL);
317168e326deSLeon Romanovsky 	kfree(devr->s1);
3172c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
317368e326deSLeon Romanovsky 	kfree(devr->s0);
3174f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
3175f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
3176c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
3177e39afe3dSLeon Romanovsky 	kfree(devr->c0);
3178c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
317921a428a0SLeon Romanovsky 	kfree(devr->p0);
31807722f47eSHaggai Eran 
31817722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
31825d8f6a0eSMark Bloch 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
31837722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
3184e126ba97SEli Cohen }
3185e126ba97SEli Cohen 
3186b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
3187b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
3188e53505a8SAchiad Shochat {
3189e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3190e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3191e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3192e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
319385c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
3194e53505a8SAchiad Shochat 	u32 ret = 0;
3195e53505a8SAchiad Shochat 
3196b02289b3SArtemy Kovalyov 	if (rep->grh_required)
3197b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
3198b02289b3SArtemy Kovalyov 
3199e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
3200b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
3201e53505a8SAchiad Shochat 
320285c7c014SDaniel Jurgens 	if (raw_support)
3203b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
320472cd5717SOr Gerlitz 
3205e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
320672cd5717SOr Gerlitz 		return ret;
3207e53505a8SAchiad Shochat 
3208e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
320972cd5717SOr Gerlitz 		return ret;
3210e53505a8SAchiad Shochat 
3211e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3212e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
3213e53505a8SAchiad Shochat 
3214e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3215e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3216e53505a8SAchiad Shochat 
3217e53505a8SAchiad Shochat 	return ret;
3218e53505a8SAchiad Shochat }
3219e53505a8SAchiad Shochat 
32207738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
32217738613eSIra Weiny 			       struct ib_port_immutable *immutable)
32227738613eSIra Weiny {
32237738613eSIra Weiny 	struct ib_port_attr attr;
3224ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3225ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
3226b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
32277738613eSIra Weiny 	int err;
32287738613eSIra Weiny 
3229c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
32307738613eSIra Weiny 	if (err)
32317738613eSIra Weiny 		return err;
32327738613eSIra Weiny 
3233b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
3234b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
3235b02289b3SArtemy Kovalyov 						   &rep);
3236b02289b3SArtemy Kovalyov 		if (err)
3237b02289b3SArtemy Kovalyov 			return err;
3238b02289b3SArtemy Kovalyov 	}
3239b02289b3SArtemy Kovalyov 
32407738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
32417738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
3242b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
3243337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
32447738613eSIra Weiny 
32457738613eSIra Weiny 	return 0;
32467738613eSIra Weiny }
32477738613eSIra Weiny 
32488e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
32498e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
32508e6efa3aSMark Bloch {
32518e6efa3aSMark Bloch 	struct ib_port_attr attr;
32528e6efa3aSMark Bloch 	int err;
32538e6efa3aSMark Bloch 
32548e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
32558e6efa3aSMark Bloch 
32568e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
32578e6efa3aSMark Bloch 	if (err)
32588e6efa3aSMark Bloch 		return err;
32598e6efa3aSMark Bloch 
32608e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
32618e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
32628e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
32638e6efa3aSMark Bloch 
32648e6efa3aSMark Bloch 	return 0;
32658e6efa3aSMark Bloch }
32668e6efa3aSMark Bloch 
32679abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
3268c7342823SIra Weiny {
3269c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
3270c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
32719abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
32729abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
32739abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
3274c7342823SIra Weiny }
3275c7342823SIra Weiny 
327645f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
32779ef9c640SAviv Heller {
32789ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
32799ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
32809ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
32819ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
32829ef9c640SAviv Heller 	int err;
32839ef9c640SAviv Heller 
32847c34ec19SAviv Heller 	if (!ns || !mlx5_lag_is_roce(mdev))
32859ef9c640SAviv Heller 		return 0;
32869ef9c640SAviv Heller 
32879ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
32889ef9c640SAviv Heller 	if (err)
32899ef9c640SAviv Heller 		return err;
32909ef9c640SAviv Heller 
32919ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
32929ef9c640SAviv Heller 	if (IS_ERR(ft)) {
32939ef9c640SAviv Heller 		err = PTR_ERR(ft);
32949ef9c640SAviv Heller 		goto err_destroy_vport_lag;
32959ef9c640SAviv Heller 	}
32969ef9c640SAviv Heller 
32979a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
32987c34ec19SAviv Heller 	dev->lag_active = true;
32999ef9c640SAviv Heller 	return 0;
33009ef9c640SAviv Heller 
33019ef9c640SAviv Heller err_destroy_vport_lag:
33029ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
33039ef9c640SAviv Heller 	return err;
33049ef9c640SAviv Heller }
33059ef9c640SAviv Heller 
330645f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
33079ef9c640SAviv Heller {
33089ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
33099ef9c640SAviv Heller 
33107c34ec19SAviv Heller 	if (dev->lag_active) {
33117c34ec19SAviv Heller 		dev->lag_active = false;
33127c34ec19SAviv Heller 
33139a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
33149a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
33159ef9c640SAviv Heller 
33169ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
33179ef9c640SAviv Heller 	}
33189ef9c640SAviv Heller }
33199ef9c640SAviv Heller 
33207fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
3321fc24fc5eSAchiad Shochat {
3322e53505a8SAchiad Shochat 	int err;
3323e53505a8SAchiad Shochat 
332495579e78SMark Bloch 	dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
3325fbdd0049SParav Pandit 	err = register_netdevice_notifier_net(mlx5_core_net(dev->mdev),
3326fbdd0049SParav Pandit 					      &dev->port[port_num].roce.nb);
33275ec8c83eSAviv Heller 	if (err) {
332895579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
3329e53505a8SAchiad Shochat 		return err;
33305ec8c83eSAviv Heller 	}
3331e53505a8SAchiad Shochat 
3332d012f5d6SOr Gerlitz 	return 0;
3333d012f5d6SOr Gerlitz }
3334d012f5d6SOr Gerlitz 
33357fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
3336e126ba97SEli Cohen {
333795579e78SMark Bloch 	if (dev->port[port_num].roce.nb.notifier_call) {
3338fbdd0049SParav Pandit 		unregister_netdevice_notifier_net(mlx5_core_net(dev->mdev),
3339fbdd0049SParav Pandit 						  &dev->port[port_num].roce.nb);
334095579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
3341e126ba97SEli Cohen 	}
3342e126ba97SEli Cohen }
3343e126ba97SEli Cohen 
3344e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3345e126ba97SEli Cohen {
3346e126ba97SEli Cohen 	int err;
3347e126ba97SEli Cohen 
3348e53505a8SAchiad Shochat 	err = mlx5_nic_vport_enable_roce(dev->mdev);
3349e53505a8SAchiad Shochat 	if (err)
33508e6efa3aSMark Bloch 		return err;
3351e53505a8SAchiad Shochat 
335245f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
33539ef9c640SAviv Heller 	if (err)
33549ef9c640SAviv Heller 		goto err_disable_roce;
33559ef9c640SAviv Heller 
3356e53505a8SAchiad Shochat 	return 0;
3357e53505a8SAchiad Shochat 
33589ef9c640SAviv Heller err_disable_roce:
33599ef9c640SAviv Heller 	mlx5_nic_vport_disable_roce(dev->mdev);
33609ef9c640SAviv Heller 
3361e53505a8SAchiad Shochat 	return err;
3362fc24fc5eSAchiad Shochat }
3363fc24fc5eSAchiad Shochat 
336445f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3365fc24fc5eSAchiad Shochat {
336645f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3367e53505a8SAchiad Shochat 	mlx5_nic_vport_disable_roce(dev->mdev);
3368fc24fc5eSAchiad Shochat }
3369fc24fc5eSAchiad Shochat 
3370f6a8a19bSDenis Drozdov static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
3371693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
3372f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
3373693dfd5aSErez Shitrit {
3374693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
3375f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
3376693dfd5aSErez Shitrit 
3377f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
3378693dfd5aSErez Shitrit }
3379693dfd5aSErez Shitrit 
3380fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3381fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
3382fe248c3aSMaor Gottlieb {
3383fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3384fe248c3aSMaor Gottlieb 	char lbuf[20];
3385fe248c3aSMaor Gottlieb 	int len;
3386fe248c3aSMaor Gottlieb 
3387fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3388fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
3389fe248c3aSMaor Gottlieb }
3390fe248c3aSMaor Gottlieb 
3391fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3392fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
3393fe248c3aSMaor Gottlieb {
3394fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3395fe248c3aSMaor Gottlieb 	u32 timeout;
3396fe248c3aSMaor Gottlieb 	u32 var;
3397fe248c3aSMaor Gottlieb 
3398fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
3399fe248c3aSMaor Gottlieb 		return -EFAULT;
3400fe248c3aSMaor Gottlieb 
3401fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3402fe248c3aSMaor Gottlieb 			1000);
3403fe248c3aSMaor Gottlieb 	if (timeout != var)
3404fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3405fe248c3aSMaor Gottlieb 			    timeout);
3406fe248c3aSMaor Gottlieb 
3407fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
3408fe248c3aSMaor Gottlieb 
3409fe248c3aSMaor Gottlieb 	return count;
3410fe248c3aSMaor Gottlieb }
3411fe248c3aSMaor Gottlieb 
3412fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
3413fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
3414fe248c3aSMaor Gottlieb 	.open	= simple_open,
3415fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
3416fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
3417fe248c3aSMaor Gottlieb };
3418fe248c3aSMaor Gottlieb 
341932f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
342032f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
342132f69e4bSDaniel Jurgens {
342232f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
342332f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
342432f69e4bSDaniel Jurgens 	int comps;
342532f69e4bSDaniel Jurgens 	int err;
342632f69e4bSDaniel Jurgens 	int i;
342732f69e4bSDaniel Jurgens 
34289dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
34299dc4cfffSLeon Romanovsky 
3430a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
3431a9e546e7SParav Pandit 
343232f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
343332f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
343432f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
343532f69e4bSDaniel Jurgens 		return;
343632f69e4bSDaniel Jurgens 	}
3437df097a27SSaeed Mahameed 
343832f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
343932f69e4bSDaniel Jurgens 
344032f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
344123eaf3b5SLeon Romanovsky 	if (mpi->mdev_events.notifier_call)
344223eaf3b5SLeon Romanovsky 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
344323eaf3b5SLeon Romanovsky 	mpi->mdev_events.notifier_call = NULL;
344432f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
344532f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
344632f69e4bSDaniel Jurgens 
344732f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
344832f69e4bSDaniel Jurgens 	if (comps) {
344932f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
345032f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
345132f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
345232f69e4bSDaniel Jurgens 
345332f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
345432f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
345532f69e4bSDaniel Jurgens 
345632f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
345732f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
345832f69e4bSDaniel Jurgens 	}
345932f69e4bSDaniel Jurgens 
346032f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
346132f69e4bSDaniel Jurgens 
346232f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
346332f69e4bSDaniel Jurgens 
346432f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
346532f69e4bSDaniel Jurgens 
346632f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
346732f69e4bSDaniel Jurgens 
346832f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
346932f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
347032f69e4bSDaniel Jurgens 	 * it back to the list.
347132f69e4bSDaniel Jurgens 	 */
347232f69e4bSDaniel Jurgens 	if (err)
347332f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
347432f69e4bSDaniel Jurgens 			    port_num + 1);
347532f69e4bSDaniel Jurgens 
347695579e78SMark Bloch 	ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
347732f69e4bSDaniel Jurgens }
347832f69e4bSDaniel Jurgens 
347932f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
348032f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
348132f69e4bSDaniel Jurgens {
348232f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
348332f69e4bSDaniel Jurgens 	int err;
348432f69e4bSDaniel Jurgens 
34859dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
34869dc4cfffSLeon Romanovsky 
348732f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
348832f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
34892577188eSQing Huang 		mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
349032f69e4bSDaniel Jurgens 			    port_num + 1);
349132f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
349232f69e4bSDaniel Jurgens 		return false;
349332f69e4bSDaniel Jurgens 	}
349432f69e4bSDaniel Jurgens 
349532f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
349632f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
3497df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
349832f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
349932f69e4bSDaniel Jurgens 
350032f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
350132f69e4bSDaniel Jurgens 	if (err)
350232f69e4bSDaniel Jurgens 		goto unbind;
350332f69e4bSDaniel Jurgens 
350432f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
350532f69e4bSDaniel Jurgens 	if (err)
350632f69e4bSDaniel Jurgens 		goto unbind;
350732f69e4bSDaniel Jurgens 
350832f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
350932f69e4bSDaniel Jurgens 	if (err) {
351032f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
351132f69e4bSDaniel Jurgens 			    port_num + 1);
351232f69e4bSDaniel Jurgens 		goto unbind;
351332f69e4bSDaniel Jurgens 	}
351432f69e4bSDaniel Jurgens 
3515df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
3516df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
3517df097a27SSaeed Mahameed 
351873eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
3519a9e546e7SParav Pandit 
352032f69e4bSDaniel Jurgens 	return true;
352132f69e4bSDaniel Jurgens 
352232f69e4bSDaniel Jurgens unbind:
352332f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
352432f69e4bSDaniel Jurgens 	return false;
352532f69e4bSDaniel Jurgens }
352632f69e4bSDaniel Jurgens 
352732f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
352832f69e4bSDaniel Jurgens {
352932f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
353032f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
353132f69e4bSDaniel Jurgens 							  port_num + 1);
353232f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
353332f69e4bSDaniel Jurgens 	int err;
353432f69e4bSDaniel Jurgens 	int i;
353532f69e4bSDaniel Jurgens 
353632f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
353732f69e4bSDaniel Jurgens 		return 0;
353832f69e4bSDaniel Jurgens 
353932f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
354032f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
354132f69e4bSDaniel Jurgens 	if (err)
354232f69e4bSDaniel Jurgens 		return err;
354332f69e4bSDaniel Jurgens 
354432f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
354532f69e4bSDaniel Jurgens 	if (err)
354632f69e4bSDaniel Jurgens 		return err;
354732f69e4bSDaniel Jurgens 
354832f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
354932f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
355032f69e4bSDaniel Jurgens 		bool bound = false;
355132f69e4bSDaniel Jurgens 
355232f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
355332f69e4bSDaniel Jurgens 		if (i == port_num) {
355432f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
355532f69e4bSDaniel Jurgens 			if (!mpi) {
355632f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
355732f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
355832f69e4bSDaniel Jurgens 				return -ENOMEM;
355932f69e4bSDaniel Jurgens 			}
356032f69e4bSDaniel Jurgens 
356132f69e4bSDaniel Jurgens 			mpi->is_master = true;
356232f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
356332f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
356432f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
356532f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
356632f69e4bSDaniel Jurgens 			mpi = NULL;
356732f69e4bSDaniel Jurgens 			continue;
356832f69e4bSDaniel Jurgens 		}
356932f69e4bSDaniel Jurgens 
357032f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
357132f69e4bSDaniel Jurgens 				    list) {
357232f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
357332f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
357432f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
357532f69e4bSDaniel Jurgens 			}
357632f69e4bSDaniel Jurgens 
357732f69e4bSDaniel Jurgens 			if (bound) {
3578c42260f1SVu Pham 				dev_dbg(mpi->mdev->device,
3579c42260f1SVu Pham 					"removing port from unaffiliated list.\n");
358032f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
358132f69e4bSDaniel Jurgens 				list_del(&mpi->list);
358232f69e4bSDaniel Jurgens 				break;
358332f69e4bSDaniel Jurgens 			}
358432f69e4bSDaniel Jurgens 		}
358532f69e4bSDaniel Jurgens 		if (!bound) {
358632f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
358732f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
358832f69e4bSDaniel Jurgens 				    i + 1);
358932f69e4bSDaniel Jurgens 		}
359032f69e4bSDaniel Jurgens 	}
359132f69e4bSDaniel Jurgens 
359232f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
359332f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
359432f69e4bSDaniel Jurgens 	return err;
359532f69e4bSDaniel Jurgens }
359632f69e4bSDaniel Jurgens 
359732f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
359832f69e4bSDaniel Jurgens {
359932f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
360032f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
360132f69e4bSDaniel Jurgens 							  port_num + 1);
360232f69e4bSDaniel Jurgens 	int i;
360332f69e4bSDaniel Jurgens 
360432f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
360532f69e4bSDaniel Jurgens 		return;
360632f69e4bSDaniel Jurgens 
360732f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
360832f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
360932f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
361032f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
361132f69e4bSDaniel Jurgens 			if (i == port_num) {
361232f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
361332f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
361432f69e4bSDaniel Jurgens 			} else {
361532f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
361632f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
361732f69e4bSDaniel Jurgens 			}
361832f69e4bSDaniel Jurgens 		}
361932f69e4bSDaniel Jurgens 	}
362032f69e4bSDaniel Jurgens 
362132f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
362232f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
362332f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
362432f69e4bSDaniel Jurgens 
362532f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
362632f69e4bSDaniel Jurgens }
362732f69e4bSDaniel Jurgens 
3628342ee59dSYishai Hadas static int mmap_obj_cleanup(struct ib_uobject *uobject,
36297be76befSYishai Hadas 			    enum rdma_remove_reason why,
36307be76befSYishai Hadas 			    struct uverbs_attr_bundle *attrs)
36317be76befSYishai Hadas {
36327be76befSYishai Hadas 	struct mlx5_user_mmap_entry *obj = uobject->object;
36337be76befSYishai Hadas 
36347be76befSYishai Hadas 	rdma_user_mmap_entry_remove(&obj->rdma_entry);
36357be76befSYishai Hadas 	return 0;
36367be76befSYishai Hadas }
36377be76befSYishai Hadas 
3638342ee59dSYishai Hadas static int mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext *c,
3639342ee59dSYishai Hadas 					    struct mlx5_user_mmap_entry *entry,
3640342ee59dSYishai Hadas 					    size_t length)
3641342ee59dSYishai Hadas {
3642342ee59dSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
3643342ee59dSYishai Hadas 		&c->ibucontext, &entry->rdma_entry, length,
3644342ee59dSYishai Hadas 		(MLX5_IB_MMAP_OFFSET_START << 16),
3645342ee59dSYishai Hadas 		((MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1));
3646342ee59dSYishai Hadas }
3647342ee59dSYishai Hadas 
36487be76befSYishai Hadas static struct mlx5_user_mmap_entry *
36497be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c)
36507be76befSYishai Hadas {
36517be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
36527be76befSYishai Hadas 	struct mlx5_var_table *var_table;
36537be76befSYishai Hadas 	u32 page_idx;
36547be76befSYishai Hadas 	int err;
36557be76befSYishai Hadas 
36567be76befSYishai Hadas 	var_table = &to_mdev(c->ibucontext.device)->var_table;
36577be76befSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
36587be76befSYishai Hadas 	if (!entry)
36597be76befSYishai Hadas 		return ERR_PTR(-ENOMEM);
36607be76befSYishai Hadas 
36617be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
36627be76befSYishai Hadas 	page_idx = find_first_zero_bit(var_table->bitmap,
36637be76befSYishai Hadas 				       var_table->num_var_hw_entries);
36647be76befSYishai Hadas 	if (page_idx >= var_table->num_var_hw_entries) {
36657be76befSYishai Hadas 		err = -ENOSPC;
36667be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
36677be76befSYishai Hadas 		goto end;
36687be76befSYishai Hadas 	}
36697be76befSYishai Hadas 
36707be76befSYishai Hadas 	set_bit(page_idx, var_table->bitmap);
36717be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
36727be76befSYishai Hadas 
36737be76befSYishai Hadas 	entry->address = var_table->hw_start_addr +
36747be76befSYishai Hadas 				(page_idx * var_table->stride_size);
36757be76befSYishai Hadas 	entry->page_idx = page_idx;
36767be76befSYishai Hadas 	entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR;
36777be76befSYishai Hadas 
3678342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry,
3679342ee59dSYishai Hadas 					       var_table->stride_size);
36807be76befSYishai Hadas 	if (err)
36817be76befSYishai Hadas 		goto err_insert;
36827be76befSYishai Hadas 
36837be76befSYishai Hadas 	return entry;
36847be76befSYishai Hadas 
36857be76befSYishai Hadas err_insert:
36867be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
36877be76befSYishai Hadas 	clear_bit(page_idx, var_table->bitmap);
36887be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
36897be76befSYishai Hadas end:
36907be76befSYishai Hadas 	kfree(entry);
36917be76befSYishai Hadas 	return ERR_PTR(err);
36927be76befSYishai Hadas }
36937be76befSYishai Hadas 
36947be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)(
36957be76befSYishai Hadas 	struct uverbs_attr_bundle *attrs)
36967be76befSYishai Hadas {
36977be76befSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
36987be76befSYishai Hadas 		attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
36997be76befSYishai Hadas 	struct mlx5_ib_ucontext *c;
37007be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
37017be76befSYishai Hadas 	u64 mmap_offset;
37027be76befSYishai Hadas 	u32 length;
37037be76befSYishai Hadas 	int err;
37047be76befSYishai Hadas 
37057be76befSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
37067be76befSYishai Hadas 	if (IS_ERR(c))
37077be76befSYishai Hadas 		return PTR_ERR(c);
37087be76befSYishai Hadas 
37097be76befSYishai Hadas 	entry = alloc_var_entry(c);
37107be76befSYishai Hadas 	if (IS_ERR(entry))
37117be76befSYishai Hadas 		return PTR_ERR(entry);
37127be76befSYishai Hadas 
37137be76befSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
37147be76befSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
37157be76befSYishai Hadas 	uobj->object = entry;
37160ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
37177be76befSYishai Hadas 
37187be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
37197be76befSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
37207be76befSYishai Hadas 	if (err)
37210ac8903cSJason Gunthorpe 		return err;
37227be76befSYishai Hadas 
37237be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
37247be76befSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
37257be76befSYishai Hadas 	if (err)
37260ac8903cSJason Gunthorpe 		return err;
37277be76befSYishai Hadas 
37287be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
37297be76befSYishai Hadas 			     &length, sizeof(length));
37307be76befSYishai Hadas 	return err;
37317be76befSYishai Hadas }
37327be76befSYishai Hadas 
37337be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
37347be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_ALLOC,
37357be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE,
37367be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
37377be76befSYishai Hadas 			UVERBS_ACCESS_NEW,
37387be76befSYishai Hadas 			UA_MANDATORY),
37397be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
37407be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
37417be76befSYishai Hadas 			   UA_MANDATORY),
37427be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
37437be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
37447be76befSYishai Hadas 			   UA_MANDATORY),
37457be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
37467be76befSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
37477be76befSYishai Hadas 			    UA_MANDATORY));
37487be76befSYishai Hadas 
37497be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
37507be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_DESTROY,
37517be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE,
37527be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
37537be76befSYishai Hadas 			UVERBS_ACCESS_DESTROY,
37547be76befSYishai Hadas 			UA_MANDATORY));
37557be76befSYishai Hadas 
37567be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR,
3757342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
37587be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC),
37597be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY));
37607be76befSYishai Hadas 
37617be76befSYishai Hadas static bool var_is_supported(struct ib_device *device)
37627be76befSYishai Hadas {
37637be76befSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(device);
37647be76befSYishai Hadas 
37657be76befSYishai Hadas 	return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
37667be76befSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q);
37677be76befSYishai Hadas }
37687be76befSYishai Hadas 
3769342ee59dSYishai Hadas static struct mlx5_user_mmap_entry *
3770342ee59dSYishai Hadas alloc_uar_entry(struct mlx5_ib_ucontext *c,
3771342ee59dSYishai Hadas 		enum mlx5_ib_uapi_uar_alloc_type alloc_type)
3772342ee59dSYishai Hadas {
3773342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
3774342ee59dSYishai Hadas 	struct mlx5_ib_dev *dev;
3775342ee59dSYishai Hadas 	u32 uar_index;
3776342ee59dSYishai Hadas 	int err;
3777342ee59dSYishai Hadas 
3778342ee59dSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
3779342ee59dSYishai Hadas 	if (!entry)
3780342ee59dSYishai Hadas 		return ERR_PTR(-ENOMEM);
3781342ee59dSYishai Hadas 
3782342ee59dSYishai Hadas 	dev = to_mdev(c->ibucontext.device);
3783342ee59dSYishai Hadas 	err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
3784342ee59dSYishai Hadas 	if (err)
3785342ee59dSYishai Hadas 		goto end;
3786342ee59dSYishai Hadas 
3787342ee59dSYishai Hadas 	entry->page_idx = uar_index;
3788342ee59dSYishai Hadas 	entry->address = uar_index2paddress(dev, uar_index);
3789342ee59dSYishai Hadas 	if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3790342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC;
3791342ee59dSYishai Hadas 	else
3792342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_NC;
3793342ee59dSYishai Hadas 
3794342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry, PAGE_SIZE);
3795342ee59dSYishai Hadas 	if (err)
3796342ee59dSYishai Hadas 		goto err_insert;
3797342ee59dSYishai Hadas 
3798342ee59dSYishai Hadas 	return entry;
3799342ee59dSYishai Hadas 
3800342ee59dSYishai Hadas err_insert:
3801342ee59dSYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, uar_index);
3802342ee59dSYishai Hadas end:
3803342ee59dSYishai Hadas 	kfree(entry);
3804342ee59dSYishai Hadas 	return ERR_PTR(err);
3805342ee59dSYishai Hadas }
3806342ee59dSYishai Hadas 
3807342ee59dSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)(
3808342ee59dSYishai Hadas 	struct uverbs_attr_bundle *attrs)
3809342ee59dSYishai Hadas {
3810342ee59dSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
3811342ee59dSYishai Hadas 		attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3812342ee59dSYishai Hadas 	enum mlx5_ib_uapi_uar_alloc_type alloc_type;
3813342ee59dSYishai Hadas 	struct mlx5_ib_ucontext *c;
3814342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
3815342ee59dSYishai Hadas 	u64 mmap_offset;
3816342ee59dSYishai Hadas 	u32 length;
3817342ee59dSYishai Hadas 	int err;
3818342ee59dSYishai Hadas 
3819342ee59dSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
3820342ee59dSYishai Hadas 	if (IS_ERR(c))
3821342ee59dSYishai Hadas 		return PTR_ERR(c);
3822342ee59dSYishai Hadas 
3823342ee59dSYishai Hadas 	err = uverbs_get_const(&alloc_type, attrs,
3824342ee59dSYishai Hadas 			       MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE);
3825342ee59dSYishai Hadas 	if (err)
3826342ee59dSYishai Hadas 		return err;
3827342ee59dSYishai Hadas 
3828342ee59dSYishai Hadas 	if (alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF &&
3829342ee59dSYishai Hadas 	    alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_NC)
3830342ee59dSYishai Hadas 		return -EOPNOTSUPP;
3831342ee59dSYishai Hadas 
3832342ee59dSYishai Hadas 	if (!to_mdev(c->ibucontext.device)->wc_support &&
3833342ee59dSYishai Hadas 	    alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3834342ee59dSYishai Hadas 		return -EOPNOTSUPP;
3835342ee59dSYishai Hadas 
3836342ee59dSYishai Hadas 	entry = alloc_uar_entry(c, alloc_type);
3837342ee59dSYishai Hadas 	if (IS_ERR(entry))
3838342ee59dSYishai Hadas 		return PTR_ERR(entry);
3839342ee59dSYishai Hadas 
3840342ee59dSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
3841342ee59dSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
3842342ee59dSYishai Hadas 	uobj->object = entry;
38430ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3844342ee59dSYishai Hadas 
3845342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3846342ee59dSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
3847342ee59dSYishai Hadas 	if (err)
38480ac8903cSJason Gunthorpe 		return err;
3849342ee59dSYishai Hadas 
3850342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3851342ee59dSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
3852342ee59dSYishai Hadas 	if (err)
38530ac8903cSJason Gunthorpe 		return err;
3854342ee59dSYishai Hadas 
3855342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3856342ee59dSYishai Hadas 			     &length, sizeof(length));
3857342ee59dSYishai Hadas 	return err;
3858342ee59dSYishai Hadas }
3859342ee59dSYishai Hadas 
3860342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
3861342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_ALLOC,
3862342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE,
3863342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
3864342ee59dSYishai Hadas 			UVERBS_ACCESS_NEW,
3865342ee59dSYishai Hadas 			UA_MANDATORY),
3866342ee59dSYishai Hadas 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE,
3867342ee59dSYishai Hadas 			     enum mlx5_ib_uapi_uar_alloc_type,
3868342ee59dSYishai Hadas 			     UA_MANDATORY),
3869342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3870342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
3871342ee59dSYishai Hadas 			   UA_MANDATORY),
3872342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3873342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
3874342ee59dSYishai Hadas 			   UA_MANDATORY),
3875342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3876342ee59dSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
3877342ee59dSYishai Hadas 			    UA_MANDATORY));
3878342ee59dSYishai Hadas 
3879342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
3880342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_DESTROY,
3881342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_DESTROY_HANDLE,
3882342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
3883342ee59dSYishai Hadas 			UVERBS_ACCESS_DESTROY,
3884342ee59dSYishai Hadas 			UA_MANDATORY));
3885342ee59dSYishai Hadas 
3886342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_UAR,
3887342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
3888342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_ALLOC),
3889342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_DESTROY));
3890342ee59dSYishai Hadas 
38919a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
38929a119cd5SJason Gunthorpe 	mlx5_ib_dm,
38939a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
389424da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
38959a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
389624da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
389783bb4442SJason Gunthorpe 			    UA_MANDATORY),
38989a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
389924da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
39003b113a1eSAriel Levkovich 			    UA_OPTIONAL),
39013b113a1eSAriel Levkovich 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
39023b113a1eSAriel Levkovich 			     enum mlx5_ib_uapi_dm_type,
39033b113a1eSAriel Levkovich 			     UA_OPTIONAL));
390424da0016SAriel Levkovich 
39059a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
39069a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
39079a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
3908c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
3909bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
3910bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
3911c6475a0bSAviad Yehezkel 
39120fb556b2SYishai Hadas ADD_UVERBS_ATTRIBUTES_SIMPLE(
39130fb556b2SYishai Hadas 	mlx5_ib_query_context,
39140fb556b2SYishai Hadas 	UVERBS_OBJECT_DEVICE,
39150fb556b2SYishai Hadas 	UVERBS_METHOD_QUERY_CONTEXT,
39160fb556b2SYishai Hadas 	UVERBS_ATTR_PTR_OUT(
39170fb556b2SYishai Hadas 		MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
39180fb556b2SYishai Hadas 		UVERBS_ATTR_STRUCT(struct mlx5_ib_alloc_ucontext_resp,
39190fb556b2SYishai Hadas 				   dump_fill_mkey),
39200fb556b2SYishai Hadas 		UA_MANDATORY));
39210fb556b2SYishai Hadas 
39220cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
392336e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
39240cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
392530f2fe40SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_qos_defs),
392605f71ef9SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_std_types_defs),
39278c84660bSMatan Barak 
39280cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
39290cbf432dSJason Gunthorpe 				&mlx5_ib_flow_action),
39300cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
39310fb556b2SYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DEVICE, &mlx5_ib_query_context),
39327be76befSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR,
39337be76befSYishai Hadas 				UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)),
3934342ee59dSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_UAR),
39350cbf432dSJason Gunthorpe 	{}
39360cbf432dSJason Gunthorpe };
39378c84660bSMatan Barak 
3938fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
3939e126ba97SEli Cohen {
394032f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
3941806b101bSJason Gunthorpe 	WARN_ON(!xa_empty(&dev->odp_mkeys));
3942806b101bSJason Gunthorpe 	cleanup_srcu_struct(&dev->odp_srcu);
3943ab40530aSParav Pandit 	mutex_destroy(&dev->cap_mask_mutex);
394450211ec9SJason Gunthorpe 	WARN_ON(!xa_empty(&dev->sig_mrs));
39454056b12eSAriel Levkovich 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
394616c1975fSMark Bloch }
394716c1975fSMark Bloch 
3948fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
394916c1975fSMark Bloch {
395016c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
3951e126ba97SEli Cohen 	int err;
395232f69e4bSDaniel Jurgens 	int i;
3953e126ba97SEli Cohen 
395432f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
395532f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
395695579e78SMark Bloch 		rwlock_init(&dev->port[i].roce.netdev_lock);
3957d3b5cc1cSMark Bloch 		dev->port[i].roce.dev = dev;
3958d3b5cc1cSMark Bloch 		dev->port[i].roce.native_port_num = i + 1;
3959d3b5cc1cSMark Bloch 		dev->port[i].roce.last_port_state = IB_PORT_DOWN;
396032f69e4bSDaniel Jurgens 	}
396132f69e4bSDaniel Jurgens 
396200815752SMoni Shoua 	mlx5_ib_internal_fill_odp_caps(dev);
396300815752SMoni Shoua 
396432f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
3965e126ba97SEli Cohen 	if (err)
3966da796ccbSMark Bloch 		return err;
3967e126ba97SEli Cohen 
3968a989ea01SMark Bloch 	err = set_has_smi_cap(dev);
3969a989ea01SMark Bloch 	if (err)
39702cb091f6SParav Pandit 		goto err_mp;
3971e126ba97SEli Cohen 
397232f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
397332f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
397432f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
397532f69e4bSDaniel Jurgens 			if (err)
397632f69e4bSDaniel Jurgens 				break;
397732f69e4bSDaniel Jurgens 		}
397832f69e4bSDaniel Jurgens 	} else {
397932f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
398032f69e4bSDaniel Jurgens 	}
398132f69e4bSDaniel Jurgens 	if (err)
398232f69e4bSDaniel Jurgens 		goto err_mp;
398332f69e4bSDaniel Jurgens 
39841b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3985e126ba97SEli Cohen 		get_ext_port_caps(dev);
3986e126ba97SEli Cohen 
3987e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
3988c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
3989e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
3990f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
3991c42260f1SVu Pham 	dev->ib_dev.dev.parent		= mdev->device;
3992cfc1a89eSMaor Gottlieb 	dev->ib_dev.lag_flags		= RDMA_LAG_FLAGS_HASH_ALL_SLAVES;
3993e126ba97SEli Cohen 
3994ab40530aSParav Pandit 	err = init_srcu_struct(&dev->odp_srcu);
3995ab40530aSParav Pandit 	if (err)
3996ab40530aSParav Pandit 		goto err_mp;
3997ab40530aSParav Pandit 
39983cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
39993cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
40003cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
4001806b101bSJason Gunthorpe 	xa_init(&dev->odp_mkeys);
400250211ec9SJason Gunthorpe 	xa_init(&dev->sig_mrs);
4003f743ff3bSSaeed Mahameed 	atomic_set(&dev->mkey_var, 0);
40043cc297dbSMark Bloch 
40053b113a1eSAriel Levkovich 	spin_lock_init(&dev->dm.lock);
40063b113a1eSAriel Levkovich 	dev->dm.dev = mdev;
400716c1975fSMark Bloch 	return 0;
400825c13324SAriel Levkovich 
400932f69e4bSDaniel Jurgens err_mp:
401032f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
4011d286ac1dSParav Pandit 	return err;
401216c1975fSMark Bloch }
401316c1975fSMark Bloch 
4014ac47bf5eSLeon Romanovsky static int mlx5_ib_enable_driver(struct ib_device *dev)
40159a4ca38dSMark Bloch {
4016ac47bf5eSLeon Romanovsky 	struct mlx5_ib_dev *mdev = to_mdev(dev);
4017ac47bf5eSLeon Romanovsky 	int ret;
40189a4ca38dSMark Bloch 
4019ac47bf5eSLeon Romanovsky 	ret = mlx5_ib_test_wc(mdev);
4020ac47bf5eSLeon Romanovsky 	mlx5_ib_dbg(mdev, "Write-Combining %s",
4021ac47bf5eSLeon Romanovsky 		    mdev->wc_support ? "supported" : "not supported");
40229a4ca38dSMark Bloch 
4023ac47bf5eSLeon Romanovsky 	return ret;
40249a4ca38dSMark Bloch }
40259a4ca38dSMark Bloch 
402696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
40277a154142SJason Gunthorpe 	.owner = THIS_MODULE,
4028b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_MLX5,
402972c6ec18SJason Gunthorpe 	.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION,
4030b9560a41SJason Gunthorpe 
403196458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
403296458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
40336c984472SMax Gurtovoy 	.alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
403496458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
403596458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
403696458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
403796458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
403896458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
403996458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
404096458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
404196458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
4042676a80adSJason Gunthorpe 	.create_user_ah = mlx5_ib_create_ah,
404396458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
404496458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
404596458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
404696458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
404796458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
404896458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
404996458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
405096458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
405196458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
405296458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
405396458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
405496458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
405511f552e2SMichael Guralnik 	.enable_driver = mlx5_ib_enable_driver,
405696458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
405796458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
405896458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
405996458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
40606c984472SMax Gurtovoy 	.map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
406196458233SKamal Heib 	.mmap = mlx5_ib_mmap,
4062dc2316ebSYishai Hadas 	.mmap_free = mlx5_ib_mmap_free,
406396458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
406496458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
406596458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
406696458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
406796458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
406896458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
4069029e88fdSLeon Romanovsky 	.post_recv = mlx5_ib_post_recv_nodrain,
4070029e88fdSLeon Romanovsky 	.post_send = mlx5_ib_post_send_nodrain,
407196458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
407296458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
407396458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
407496458233SKamal Heib 	.query_device = mlx5_ib_query_device,
407596458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
407696458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
407796458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
407896458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
40790fb556b2SYishai Hadas 	.query_ucontext = mlx5_ib_query_ucontext,
408096458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
408190da7dc8SJianxin Xiong 	.reg_user_mr_dmabuf = mlx5_ib_reg_user_mr_dmabuf,
408296458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
408396458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
408496458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
4085d3456914SLeon Romanovsky 
4086d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
40873b023e1bSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_counters, mlx5_ib_mcounters, ibcntrs),
4088e39afe3dSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
408921a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
409068e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
4091a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
409296458233SKamal Heib };
409396458233SKamal Heib 
409496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
409596458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
409696458233SKamal Heib };
409796458233SKamal Heib 
409896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
409996458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
41009c0015efSDanit Goldberg 	.get_vf_guid = mlx5_ib_get_vf_guid,
410196458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
410296458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
410396458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
410496458233SKamal Heib };
410596458233SKamal Heib 
410696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
410796458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
410896458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
4109d18bb3e1SLeon Romanovsky 
4110d18bb3e1SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_mw, mlx5_ib_mw, ibmw),
411196458233SKamal Heib };
411296458233SKamal Heib 
411396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
411496458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
411596458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
411628ad5f65SLeon Romanovsky 
411728ad5f65SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_xrcd, mlx5_ib_xrcd, ibxrcd),
411896458233SKamal Heib };
411996458233SKamal Heib 
412096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
412196458233SKamal Heib 	.alloc_dm = mlx5_ib_alloc_dm,
412296458233SKamal Heib 	.dealloc_dm = mlx5_ib_dealloc_dm,
412396458233SKamal Heib 	.reg_dm_mr = mlx5_ib_reg_dm_mr,
412496458233SKamal Heib };
412596458233SKamal Heib 
4126f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev)
4127f164be8cSYishai Hadas {
4128f164be8cSYishai Hadas 	struct mlx5_core_dev *mdev = dev->mdev;
4129f164be8cSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
4130f164be8cSYishai Hadas 	u8 log_doorbell_bar_size;
4131f164be8cSYishai Hadas 	u8 log_doorbell_stride;
4132f164be8cSYishai Hadas 	u64 bar_size;
4133f164be8cSYishai Hadas 
4134f164be8cSYishai Hadas 	log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
4135f164be8cSYishai Hadas 					log_doorbell_bar_size);
4136f164be8cSYishai Hadas 	log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
4137f164be8cSYishai Hadas 					log_doorbell_stride);
4138f164be8cSYishai Hadas 	var_table->hw_start_addr = dev->mdev->bar_addr +
4139f164be8cSYishai Hadas 				MLX5_CAP64_DEV_VDPA_EMULATION(mdev,
4140f164be8cSYishai Hadas 					doorbell_bar_offset);
4141f164be8cSYishai Hadas 	bar_size = (1ULL << log_doorbell_bar_size) * 4096;
4142f164be8cSYishai Hadas 	var_table->stride_size = 1ULL << log_doorbell_stride;
414391b74bf5SAlexander Lobakin 	var_table->num_var_hw_entries = div_u64(bar_size,
414491b74bf5SAlexander Lobakin 						var_table->stride_size);
4145f164be8cSYishai Hadas 	mutex_init(&var_table->bitmap_lock);
4146f164be8cSYishai Hadas 	var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries,
4147f164be8cSYishai Hadas 					  GFP_KERNEL);
4148f164be8cSYishai Hadas 	return (var_table->bitmap) ? 0 : -ENOMEM;
4149f164be8cSYishai Hadas }
4150f164be8cSYishai Hadas 
4151f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev)
4152f164be8cSYishai Hadas {
4153f164be8cSYishai Hadas 	bitmap_free(dev->var_table.bitmap);
4154f164be8cSYishai Hadas }
4155f164be8cSYishai Hadas 
4156fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
415716c1975fSMark Bloch {
415816c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
415916c1975fSMark Bloch 	int err;
416016c1975fSMark Bloch 
4161f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
4162f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
416396458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
416496458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
41658e959601SNiranjana Vishwanathapura 
416696458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
416796458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
41687c2344c3SMaor Gottlieb 
41696e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
41706e8484c5SMax Gurtovoy 
417144ce37bcSJason Gunthorpe 	if (MLX5_CAP_GEN(mdev, imaicl))
417296458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
4173d2370e0aSMatan Barak 
417444ce37bcSJason Gunthorpe 	if (MLX5_CAP_GEN(mdev, xrc))
417596458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
4176e126ba97SEli Cohen 
417725c13324SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic) ||
417825c13324SAriel Levkovich 	    MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
417925c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
418096458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
418124da0016SAriel Levkovich 
418296458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
418381e30880SYishai Hadas 
418436e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
418536e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
418681e30880SYishai Hadas 
4187e126ba97SEli Cohen 	err = init_node_data(dev);
4188e126ba97SEli Cohen 	if (err)
418916c1975fSMark Bloch 		return err;
4190e126ba97SEli Cohen 
4191c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4192e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
4193e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
4194a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
4195c8b89924SMark Bloch 
4196f164be8cSYishai Hadas 	if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
4197f164be8cSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) {
4198f164be8cSYishai Hadas 		err = mlx5_ib_init_var_table(dev);
4199f164be8cSYishai Hadas 		if (err)
4200f164be8cSYishai Hadas 			return err;
4201f164be8cSYishai Hadas 	}
4202f164be8cSYishai Hadas 
420396e2fd73SLeon Romanovsky 	dev->ib_dev.use_cq_dim = true;
420496e2fd73SLeon Romanovsky 
420516c1975fSMark Bloch 	return 0;
420616c1975fSMark Bloch }
420716c1975fSMark Bloch 
420896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
420996458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
421096458233SKamal Heib 	.query_port = mlx5_ib_query_port,
421196458233SKamal Heib };
421296458233SKamal Heib 
42138e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
421416c1975fSMark Bloch {
421596458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
42168e6efa3aSMark Bloch 	return 0;
42178e6efa3aSMark Bloch }
42188e6efa3aSMark Bloch 
421996458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
422096458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
422196458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
4222*d6fd59e1SParav Pandit 	.query_pkey = mlx5_ib_rep_query_pkey,
422396458233SKamal Heib };
422496458233SKamal Heib 
4225b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev)
42268e6efa3aSMark Bloch {
422796458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
42288e6efa3aSMark Bloch 	return 0;
42298e6efa3aSMark Bloch }
42308e6efa3aSMark Bloch 
423196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
423296458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
423396458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
423496458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
423596458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
423696458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
423796458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
4238c0a6b5ecSLeon Romanovsky 
4239c0a6b5ecSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_rwq_ind_table, mlx5_ib_rwq_ind_table,
4240c0a6b5ecSLeon Romanovsky 			   ib_rwq_ind_tbl),
424196458233SKamal Heib };
424296458233SKamal Heib 
42431e2b5a90SLeon Romanovsky static int mlx5_ib_roce_init(struct mlx5_ib_dev *dev)
42448e6efa3aSMark Bloch {
42451e2b5a90SLeon Romanovsky 	struct mlx5_core_dev *mdev = dev->mdev;
42461e2b5a90SLeon Romanovsky 	enum rdma_link_layer ll;
42471e2b5a90SLeon Romanovsky 	int port_type_cap;
42481e2b5a90SLeon Romanovsky 	u8 port_num = 0;
42491e2b5a90SLeon Romanovsky 	int err;
42507fd8aefbSDaniel Jurgens 
42511e2b5a90SLeon Romanovsky 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
42521e2b5a90SLeon Romanovsky 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
42531e2b5a90SLeon Romanovsky 
42541e2b5a90SLeon Romanovsky 	if (ll == IB_LINK_LAYER_ETHERNET) {
425596458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
42568e6efa3aSMark Bloch 
4257e3f1ed1fSLeon Romanovsky 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4258e3f1ed1fSLeon Romanovsky 
425926628e2dSMark Bloch 		/* Register only for native ports */
42601e2b5a90SLeon Romanovsky 		err = mlx5_add_netdev_notifier(dev, port_num);
42611e2b5a90SLeon Romanovsky 		if (err || dev->is_rep || !mlx5_is_roce_enabled(mdev))
42621e2b5a90SLeon Romanovsky 			/*
42631e2b5a90SLeon Romanovsky 			 * We don't enable ETH interface for
42641e2b5a90SLeon Romanovsky 			 * 1. IB representors
42651e2b5a90SLeon Romanovsky 			 * 2. User disabled ROCE through devlink interface
42661e2b5a90SLeon Romanovsky 			 */
42678e6efa3aSMark Bloch 			return err;
42688e6efa3aSMark Bloch 
4269e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
42708e6efa3aSMark Bloch 		if (err)
42718e6efa3aSMark Bloch 			goto cleanup;
42728e6efa3aSMark Bloch 	}
42738e6efa3aSMark Bloch 
427416c1975fSMark Bloch 	return 0;
42758e6efa3aSMark Bloch cleanup:
42761e2b5a90SLeon Romanovsky 	mlx5_remove_netdev_notifier(dev, port_num);
42778e6efa3aSMark Bloch 	return err;
427845bded2cSKamal Heib }
42796aec21f6SHaggai Eran 
42801e2b5a90SLeon Romanovsky static void mlx5_ib_roce_cleanup(struct mlx5_ib_dev *dev)
428116c1975fSMark Bloch {
428216c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
428316c1975fSMark Bloch 	enum rdma_link_layer ll;
428416c1975fSMark Bloch 	int port_type_cap;
42851e2b5a90SLeon Romanovsky 	u8 port_num;
42864a2da0b8SParav Pandit 
428716c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
428816c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
428916c1975fSMark Bloch 
429016c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
42911e2b5a90SLeon Romanovsky 		if (!dev->is_rep)
429216c1975fSMark Bloch 			mlx5_disable_eth(dev);
429316c1975fSMark Bloch 
42941e2b5a90SLeon Romanovsky 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
42951e2b5a90SLeon Romanovsky 		mlx5_remove_netdev_notifier(dev, port_num);
429616c1975fSMark Bloch 	}
429716c1975fSMark Bloch }
429816c1975fSMark Bloch 
429916c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
430016c1975fSMark Bloch {
430173eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
4302a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
430373eb8f03SGreg Kroah-Hartman 	return 0;
430416c1975fSMark Bloch }
430516c1975fSMark Bloch 
430616c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
430716c1975fSMark Bloch {
4308a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
4309a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
431016c1975fSMark Bloch }
431116c1975fSMark Bloch 
431216c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
431316c1975fSMark Bloch {
43145fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
4315444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
431616c1975fSMark Bloch }
431716c1975fSMark Bloch 
431816c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
431916c1975fSMark Bloch {
432016c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
432116c1975fSMark Bloch }
432216c1975fSMark Bloch 
4323fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
432416c1975fSMark Bloch {
432516c1975fSMark Bloch 	int err;
43265fe9dec0SEli Cohen 
43275fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
43285fe9dec0SEli Cohen 	if (err)
432916c1975fSMark Bloch 		return err;
43305fe9dec0SEli Cohen 
43315fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
43325fe9dec0SEli Cohen 	if (err)
43331c3aa6bdSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->bfreg);
43345fe9dec0SEli Cohen 
433516c1975fSMark Bloch 	return err;
433616c1975fSMark Bloch }
43370837e86aSMark Bloch 
4338fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
433916c1975fSMark Bloch {
434016c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
434116c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
434216c1975fSMark Bloch }
4343e126ba97SEli Cohen 
4344fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
434516c1975fSMark Bloch {
4346e349f858SJason Gunthorpe 	const char *name;
4347e349f858SJason Gunthorpe 
4348508a523fSParav Pandit 	rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
43497c34ec19SAviv Heller 	if (!mlx5_lag_is_roce(dev->mdev))
4350e349f858SJason Gunthorpe 		name = "mlx5_%d";
4351e349f858SJason Gunthorpe 	else
4352e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
4353e0477b34SJason Gunthorpe 	return ib_register_device(&dev->ib_dev, name, &dev->mdev->pdev->dev);
435416c1975fSMark Bloch }
435516c1975fSMark Bloch 
4356fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
435742cea83fSMark Bloch {
43581e2b5a90SLeon Romanovsky 	int err;
43591e2b5a90SLeon Romanovsky 
43601e2b5a90SLeon Romanovsky 	err = mlx5_mr_cache_cleanup(dev);
43611e2b5a90SLeon Romanovsky 	if (err)
43621e2b5a90SLeon Romanovsky 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
43631e2b5a90SLeon Romanovsky 
43641e2b5a90SLeon Romanovsky 	if (dev->umrc.qp)
43651e2b5a90SLeon Romanovsky 		mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
43661e2b5a90SLeon Romanovsky 	if (dev->umrc.cq)
43671e2b5a90SLeon Romanovsky 		ib_free_cq(dev->umrc.cq);
43681e2b5a90SLeon Romanovsky 	if (dev->umrc.pd)
43691e2b5a90SLeon Romanovsky 		ib_dealloc_pd(dev->umrc.pd);
437042cea83fSMark Bloch }
437142cea83fSMark Bloch 
4372fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
437316c1975fSMark Bloch {
437416c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
437516c1975fSMark Bloch }
437616c1975fSMark Bloch 
43771e2b5a90SLeon Romanovsky enum {
43781e2b5a90SLeon Romanovsky 	MAX_UMR_WR = 128,
43791e2b5a90SLeon Romanovsky };
43801e2b5a90SLeon Romanovsky 
4381fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
438216c1975fSMark Bloch {
43831e2b5a90SLeon Romanovsky 	struct ib_qp_init_attr *init_attr = NULL;
43841e2b5a90SLeon Romanovsky 	struct ib_qp_attr *attr = NULL;
43851e2b5a90SLeon Romanovsky 	struct ib_pd *pd;
43861e2b5a90SLeon Romanovsky 	struct ib_cq *cq;
43871e2b5a90SLeon Romanovsky 	struct ib_qp *qp;
43881e2b5a90SLeon Romanovsky 	int ret;
43891e2b5a90SLeon Romanovsky 
43901e2b5a90SLeon Romanovsky 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
43911e2b5a90SLeon Romanovsky 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
43921e2b5a90SLeon Romanovsky 	if (!attr || !init_attr) {
43931e2b5a90SLeon Romanovsky 		ret = -ENOMEM;
43941e2b5a90SLeon Romanovsky 		goto error_0;
43951e2b5a90SLeon Romanovsky 	}
43961e2b5a90SLeon Romanovsky 
43971e2b5a90SLeon Romanovsky 	pd = ib_alloc_pd(&dev->ib_dev, 0);
43981e2b5a90SLeon Romanovsky 	if (IS_ERR(pd)) {
43991e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
44001e2b5a90SLeon Romanovsky 		ret = PTR_ERR(pd);
44011e2b5a90SLeon Romanovsky 		goto error_0;
44021e2b5a90SLeon Romanovsky 	}
44031e2b5a90SLeon Romanovsky 
44041e2b5a90SLeon Romanovsky 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
44051e2b5a90SLeon Romanovsky 	if (IS_ERR(cq)) {
44061e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
44071e2b5a90SLeon Romanovsky 		ret = PTR_ERR(cq);
44081e2b5a90SLeon Romanovsky 		goto error_2;
44091e2b5a90SLeon Romanovsky 	}
44101e2b5a90SLeon Romanovsky 
44111e2b5a90SLeon Romanovsky 	init_attr->send_cq = cq;
44121e2b5a90SLeon Romanovsky 	init_attr->recv_cq = cq;
44131e2b5a90SLeon Romanovsky 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
44141e2b5a90SLeon Romanovsky 	init_attr->cap.max_send_wr = MAX_UMR_WR;
44151e2b5a90SLeon Romanovsky 	init_attr->cap.max_send_sge = 1;
44161e2b5a90SLeon Romanovsky 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
44171e2b5a90SLeon Romanovsky 	init_attr->port_num = 1;
44181e2b5a90SLeon Romanovsky 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
44191e2b5a90SLeon Romanovsky 	if (IS_ERR(qp)) {
44201e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
44211e2b5a90SLeon Romanovsky 		ret = PTR_ERR(qp);
44221e2b5a90SLeon Romanovsky 		goto error_3;
44231e2b5a90SLeon Romanovsky 	}
44241e2b5a90SLeon Romanovsky 	qp->device     = &dev->ib_dev;
44251e2b5a90SLeon Romanovsky 	qp->real_qp    = qp;
44261e2b5a90SLeon Romanovsky 	qp->uobject    = NULL;
44271e2b5a90SLeon Romanovsky 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
44281e2b5a90SLeon Romanovsky 	qp->send_cq    = init_attr->send_cq;
44291e2b5a90SLeon Romanovsky 	qp->recv_cq    = init_attr->recv_cq;
44301e2b5a90SLeon Romanovsky 
44311e2b5a90SLeon Romanovsky 	attr->qp_state = IB_QPS_INIT;
44321e2b5a90SLeon Romanovsky 	attr->port_num = 1;
44331e2b5a90SLeon Romanovsky 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
44341e2b5a90SLeon Romanovsky 				IB_QP_PORT, NULL);
44351e2b5a90SLeon Romanovsky 	if (ret) {
44361e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
44371e2b5a90SLeon Romanovsky 		goto error_4;
44381e2b5a90SLeon Romanovsky 	}
44391e2b5a90SLeon Romanovsky 
44401e2b5a90SLeon Romanovsky 	memset(attr, 0, sizeof(*attr));
44411e2b5a90SLeon Romanovsky 	attr->qp_state = IB_QPS_RTR;
44421e2b5a90SLeon Romanovsky 	attr->path_mtu = IB_MTU_256;
44431e2b5a90SLeon Romanovsky 
44441e2b5a90SLeon Romanovsky 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
44451e2b5a90SLeon Romanovsky 	if (ret) {
44461e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
44471e2b5a90SLeon Romanovsky 		goto error_4;
44481e2b5a90SLeon Romanovsky 	}
44491e2b5a90SLeon Romanovsky 
44501e2b5a90SLeon Romanovsky 	memset(attr, 0, sizeof(*attr));
44511e2b5a90SLeon Romanovsky 	attr->qp_state = IB_QPS_RTS;
44521e2b5a90SLeon Romanovsky 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
44531e2b5a90SLeon Romanovsky 	if (ret) {
44541e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
44551e2b5a90SLeon Romanovsky 		goto error_4;
44561e2b5a90SLeon Romanovsky 	}
44571e2b5a90SLeon Romanovsky 
44581e2b5a90SLeon Romanovsky 	dev->umrc.qp = qp;
44591e2b5a90SLeon Romanovsky 	dev->umrc.cq = cq;
44601e2b5a90SLeon Romanovsky 	dev->umrc.pd = pd;
44611e2b5a90SLeon Romanovsky 
44621e2b5a90SLeon Romanovsky 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
44631e2b5a90SLeon Romanovsky 	ret = mlx5_mr_cache_init(dev);
44641e2b5a90SLeon Romanovsky 	if (ret) {
44651e2b5a90SLeon Romanovsky 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
44661e2b5a90SLeon Romanovsky 		goto error_4;
44671e2b5a90SLeon Romanovsky 	}
44681e2b5a90SLeon Romanovsky 
44691e2b5a90SLeon Romanovsky 	kfree(attr);
44701e2b5a90SLeon Romanovsky 	kfree(init_attr);
44711e2b5a90SLeon Romanovsky 
44721e2b5a90SLeon Romanovsky 	return 0;
44731e2b5a90SLeon Romanovsky 
44741e2b5a90SLeon Romanovsky error_4:
44751e2b5a90SLeon Romanovsky 	mlx5_ib_destroy_qp(qp, NULL);
44761e2b5a90SLeon Romanovsky 	dev->umrc.qp = NULL;
44771e2b5a90SLeon Romanovsky 
44781e2b5a90SLeon Romanovsky error_3:
44791e2b5a90SLeon Romanovsky 	ib_free_cq(cq);
44801e2b5a90SLeon Romanovsky 	dev->umrc.cq = NULL;
44811e2b5a90SLeon Romanovsky 
44821e2b5a90SLeon Romanovsky error_2:
44831e2b5a90SLeon Romanovsky 	ib_dealloc_pd(pd);
44841e2b5a90SLeon Romanovsky 	dev->umrc.pd = NULL;
44851e2b5a90SLeon Romanovsky 
44861e2b5a90SLeon Romanovsky error_0:
44871e2b5a90SLeon Romanovsky 	kfree(attr);
44881e2b5a90SLeon Romanovsky 	kfree(init_attr);
44891e2b5a90SLeon Romanovsky 	return ret;
449016c1975fSMark Bloch }
449116c1975fSMark Bloch 
449216c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
449316c1975fSMark Bloch {
44941e2b5a90SLeon Romanovsky 	struct dentry *root;
449503404e8aSMaor Gottlieb 
44961e2b5a90SLeon Romanovsky 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
44971e2b5a90SLeon Romanovsky 		return 0;
44981e2b5a90SLeon Romanovsky 
44991e2b5a90SLeon Romanovsky 	mutex_init(&dev->delay_drop.lock);
45001e2b5a90SLeon Romanovsky 	dev->delay_drop.dev = dev;
45011e2b5a90SLeon Romanovsky 	dev->delay_drop.activate = false;
45021e2b5a90SLeon Romanovsky 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
45031e2b5a90SLeon Romanovsky 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
45041e2b5a90SLeon Romanovsky 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
45051e2b5a90SLeon Romanovsky 	atomic_set(&dev->delay_drop.events_cnt, 0);
45061e2b5a90SLeon Romanovsky 
45071e2b5a90SLeon Romanovsky 	if (!mlx5_debugfs_root)
45081e2b5a90SLeon Romanovsky 		return 0;
45091e2b5a90SLeon Romanovsky 
45101e2b5a90SLeon Romanovsky 	root = debugfs_create_dir("delay_drop", dev->mdev->priv.dbg_root);
45111e2b5a90SLeon Romanovsky 	dev->delay_drop.dir_debugfs = root;
45121e2b5a90SLeon Romanovsky 
45131e2b5a90SLeon Romanovsky 	debugfs_create_atomic_t("num_timeout_events", 0400, root,
45141e2b5a90SLeon Romanovsky 				&dev->delay_drop.events_cnt);
45151e2b5a90SLeon Romanovsky 	debugfs_create_atomic_t("num_rqs", 0400, root,
45161e2b5a90SLeon Romanovsky 				&dev->delay_drop.rqs_cnt);
45171e2b5a90SLeon Romanovsky 	debugfs_create_file("timeout", 0600, root, &dev->delay_drop,
45181e2b5a90SLeon Romanovsky 			    &fops_delay_drop_timeout);
451916c1975fSMark Bloch 	return 0;
452016c1975fSMark Bloch }
452116c1975fSMark Bloch 
452216c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
452316c1975fSMark Bloch {
45241e2b5a90SLeon Romanovsky 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
45251e2b5a90SLeon Romanovsky 		return;
45261e2b5a90SLeon Romanovsky 
45271e2b5a90SLeon Romanovsky 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
45281e2b5a90SLeon Romanovsky 	if (!dev->delay_drop.dir_debugfs)
45291e2b5a90SLeon Romanovsky 		return;
45301e2b5a90SLeon Romanovsky 
45311e2b5a90SLeon Romanovsky 	debugfs_remove_recursive(dev->delay_drop.dir_debugfs);
45321e2b5a90SLeon Romanovsky 	dev->delay_drop.dir_debugfs = NULL;
453316c1975fSMark Bloch }
453416c1975fSMark Bloch 
4535df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
4536df097a27SSaeed Mahameed {
4537df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
4538df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
4539df097a27SSaeed Mahameed 	return 0;
4540df097a27SSaeed Mahameed }
4541df097a27SSaeed Mahameed 
4542df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
4543df097a27SSaeed Mahameed {
4544df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
4545df097a27SSaeed Mahameed }
4546df097a27SSaeed Mahameed 
4547b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
454816c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
454916c1975fSMark Bloch 		      int stage)
455016c1975fSMark Bloch {
45514cca96a8SParav Pandit 	dev->ib_active = false;
45524cca96a8SParav Pandit 
455316c1975fSMark Bloch 	/* Number of stages to cleanup */
455416c1975fSMark Bloch 	while (stage) {
455516c1975fSMark Bloch 		stage--;
455616c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
455716c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
455816c1975fSMark Bloch 	}
45594a6dc855SMark Bloch 
4560da796ccbSMark Bloch 	kfree(dev->port);
45614a6dc855SMark Bloch 	ib_dealloc_device(&dev->ib_dev);
456216c1975fSMark Bloch }
456316c1975fSMark Bloch 
456493f82444SLeon Romanovsky int __mlx5_ib_add(struct mlx5_ib_dev *dev,
456516c1975fSMark Bloch 		  const struct mlx5_ib_profile *profile)
456616c1975fSMark Bloch {
456716c1975fSMark Bloch 	int err;
456816c1975fSMark Bloch 	int i;
456916c1975fSMark Bloch 
45708d93efb8SMark Bloch 	dev->profile = profile;
45718d93efb8SMark Bloch 
457216c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
457316c1975fSMark Bloch 		if (profile->stage[i].init) {
457416c1975fSMark Bloch 			err = profile->stage[i].init(dev);
457516c1975fSMark Bloch 			if (err)
457616c1975fSMark Bloch 				goto err_out;
457716c1975fSMark Bloch 		}
457816c1975fSMark Bloch 	}
457916c1975fSMark Bloch 
4580e126ba97SEli Cohen 	dev->ib_active = true;
458193f82444SLeon Romanovsky 	return 0;
4582e126ba97SEli Cohen 
458316c1975fSMark Bloch err_out:
458493f82444SLeon Romanovsky 	/* Clean up stages which were initialized */
458593f82444SLeon Romanovsky 	while (i) {
458693f82444SLeon Romanovsky 		i--;
458793f82444SLeon Romanovsky 		if (profile->stage[i].cleanup)
458893f82444SLeon Romanovsky 			profile->stage[i].cleanup(dev);
458993f82444SLeon Romanovsky 	}
459093f82444SLeon Romanovsky 	return -ENOMEM;
4591e126ba97SEli Cohen }
4592e126ba97SEli Cohen 
459316c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
459416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
459516c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
459616c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
4597f7c4ffdaSLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_FS,
4598f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_init,
4599f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_cleanup),
460016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
460116c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
4602f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
46038e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
46048e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
46058e6efa3aSMark Bloch 		     NULL),
460616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
46071e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_init,
46081e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_cleanup),
4609333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
4610333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
4611333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
4612f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4613f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
4614f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
461516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
46161e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_init,
46171e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_cleanup),
4618df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4619df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
4620df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
462116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
46221e2b5a90SLeon Romanovsky 		     mlx5_ib_odp_init_one,
46231e2b5a90SLeon Romanovsky 		     mlx5_ib_odp_cleanup_one),
462416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
462564825827SLeon Romanovsky 		     mlx5_ib_counters_init,
462664825827SLeon Romanovsky 		     mlx5_ib_counters_cleanup),
462716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
462816c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
462916c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
463016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
463116c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
463216c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
463316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
463416c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
463516c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
463642cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
463742cea83fSMark Bloch 		     NULL,
463842cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
463981773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4640d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_init,
4641d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_cleanup),
464216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
464316c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
464416c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
464542cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
464642cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
464742cea83fSMark Bloch 		     NULL),
464816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
464916c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
465016c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
4651b572ebe6SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4652b572ebe6SLeon Romanovsky 		     mlx5_ib_restrack_init,
4653b572ebe6SLeon Romanovsky 		     NULL),
465416c1975fSMark Bloch };
465516c1975fSMark Bloch 
4656b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = {
4657b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
4658b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
4659b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
4660f7c4ffdaSLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_FS,
4661f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_init,
4662f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_cleanup),
4663b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
4664b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
4665f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
4666b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
4667b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_non_default_cb,
4668b5ca15adSMark Bloch 		     NULL),
4669b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
46701e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_init,
46711e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_cleanup),
4672333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
4673333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
4674333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
4675f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4676f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
4677f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
4678b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
46791e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_init,
46801e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_cleanup),
4681df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4682df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
4683df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
4684b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
468564825827SLeon Romanovsky 		     mlx5_ib_counters_init,
468664825827SLeon Romanovsky 		     mlx5_ib_counters_cleanup),
468779db784eSParav Pandit 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
468879db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_init,
468979db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_cleanup),
4690b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
4691b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
4692b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
4693b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
4694b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
4695b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
469603fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
469703fe2debSDavid S. Miller 		     NULL,
469803fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
46997f575103SMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4700d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_init,
4701d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_cleanup),
4702b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
4703b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
4704b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
470503fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
470603fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
470703fe2debSDavid S. Miller 		     NULL),
4708b572ebe6SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4709b572ebe6SLeon Romanovsky 		     mlx5_ib_restrack_init,
4710b572ebe6SLeon Romanovsky 		     NULL),
4711b5ca15adSMark Bloch };
4712b5ca15adSMark Bloch 
471393f82444SLeon Romanovsky static int mlx5r_mp_probe(struct auxiliary_device *adev,
471493f82444SLeon Romanovsky 			  const struct auxiliary_device_id *id)
471532f69e4bSDaniel Jurgens {
471693f82444SLeon Romanovsky 	struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
471793f82444SLeon Romanovsky 	struct mlx5_core_dev *mdev = idev->mdev;
471832f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
471932f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
472032f69e4bSDaniel Jurgens 	bool bound = false;
472132f69e4bSDaniel Jurgens 	int err;
472232f69e4bSDaniel Jurgens 
472332f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
472432f69e4bSDaniel Jurgens 	if (!mpi)
472593f82444SLeon Romanovsky 		return -ENOMEM;
472632f69e4bSDaniel Jurgens 
472732f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
472832f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
472932f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
473032f69e4bSDaniel Jurgens 	if (err) {
473132f69e4bSDaniel Jurgens 		kfree(mpi);
473293f82444SLeon Romanovsky 		return err;
473332f69e4bSDaniel Jurgens 	}
473432f69e4bSDaniel Jurgens 
473532f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
473632f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
473732f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
473832f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
473932f69e4bSDaniel Jurgens 
474032f69e4bSDaniel Jurgens 		if (bound) {
474132f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
474232f69e4bSDaniel Jurgens 			break;
474332f69e4bSDaniel Jurgens 		}
474432f69e4bSDaniel Jurgens 	}
474532f69e4bSDaniel Jurgens 
474632f69e4bSDaniel Jurgens 	if (!bound) {
474732f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
4748c42260f1SVu Pham 		dev_dbg(mdev->device,
4749c42260f1SVu Pham 			"no suitable IB device found to bind to, added to unaffiliated list.\n");
475032f69e4bSDaniel Jurgens 	}
475132f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
475232f69e4bSDaniel Jurgens 
475393f82444SLeon Romanovsky 	dev_set_drvdata(&adev->dev, mpi);
475493f82444SLeon Romanovsky 	return 0;
475532f69e4bSDaniel Jurgens }
475632f69e4bSDaniel Jurgens 
475793f82444SLeon Romanovsky static void mlx5r_mp_remove(struct auxiliary_device *adev)
475816c1975fSMark Bloch {
475993f82444SLeon Romanovsky 	struct mlx5_ib_multiport_info *mpi;
476093f82444SLeon Romanovsky 
476193f82444SLeon Romanovsky 	mpi = dev_get_drvdata(&adev->dev);
476293f82444SLeon Romanovsky 	mutex_lock(&mlx5_ib_multiport_mutex);
476393f82444SLeon Romanovsky 	if (mpi->ibdev)
476493f82444SLeon Romanovsky 		mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
476593f82444SLeon Romanovsky 	list_del(&mpi->list);
476693f82444SLeon Romanovsky 	mutex_unlock(&mlx5_ib_multiport_mutex);
476793f82444SLeon Romanovsky 	kfree(mpi);
476893f82444SLeon Romanovsky }
476993f82444SLeon Romanovsky 
477093f82444SLeon Romanovsky static int mlx5r_probe(struct auxiliary_device *adev,
477193f82444SLeon Romanovsky 		       const struct auxiliary_device_id *id)
477293f82444SLeon Romanovsky {
477393f82444SLeon Romanovsky 	struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
477493f82444SLeon Romanovsky 	struct mlx5_core_dev *mdev = idev->mdev;
477594de879cSMichael Guralnik 	const struct mlx5_ib_profile *profile;
477693f82444SLeon Romanovsky 	int port_type_cap, num_ports, ret;
477732f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
4778b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
4779f0666f1fSBodong Wang 
478032f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
478132f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
478232f69e4bSDaniel Jurgens 
4783da796ccbSMark Bloch 	num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
4784da796ccbSMark Bloch 			MLX5_CAP_GEN(mdev, num_vhca_ports));
4785459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
4786b5ca15adSMark Bloch 	if (!dev)
478793f82444SLeon Romanovsky 		return -ENOMEM;
4788da796ccbSMark Bloch 	dev->port = kcalloc(num_ports, sizeof(*dev->port),
4789da796ccbSMark Bloch 			     GFP_KERNEL);
4790da796ccbSMark Bloch 	if (!dev->port) {
4791a5c9c299SParav Pandit 		ib_dealloc_device(&dev->ib_dev);
479293f82444SLeon Romanovsky 		return -ENOMEM;
4793da796ccbSMark Bloch 	}
4794b5ca15adSMark Bloch 
4795b5ca15adSMark Bloch 	dev->mdev = mdev;
4796da796ccbSMark Bloch 	dev->num_ports = num_ports;
4797b5ca15adSMark Bloch 
479894de879cSMichael Guralnik 	if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_is_roce_enabled(mdev))
479994de879cSMichael Guralnik 		profile = &raw_eth_profile;
480094de879cSMichael Guralnik 	else
480194de879cSMichael Guralnik 		profile = &pf_profile;
480294de879cSMichael Guralnik 
480393f82444SLeon Romanovsky 	ret = __mlx5_ib_add(dev, profile);
480493f82444SLeon Romanovsky 	if (ret) {
480593f82444SLeon Romanovsky 		kfree(dev->port);
480693f82444SLeon Romanovsky 		ib_dealloc_device(&dev->ib_dev);
480793f82444SLeon Romanovsky 		return ret;
480816c1975fSMark Bloch 	}
480916c1975fSMark Bloch 
481093f82444SLeon Romanovsky 	dev_set_drvdata(&adev->dev, dev);
481193f82444SLeon Romanovsky 	return 0;
481293f82444SLeon Romanovsky }
481393f82444SLeon Romanovsky 
481493f82444SLeon Romanovsky static void mlx5r_remove(struct auxiliary_device *adev)
4815e126ba97SEli Cohen {
481632f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
48176aec21f6SHaggai Eran 
481893f82444SLeon Romanovsky 	dev = dev_get_drvdata(&adev->dev);
481916c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
4820e126ba97SEli Cohen }
4821e126ba97SEli Cohen 
482293f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_mp_id_table[] = {
482393f82444SLeon Romanovsky 	{ .name = MLX5_ADEV_NAME ".multiport", },
482493f82444SLeon Romanovsky 	{},
482593f82444SLeon Romanovsky };
482693f82444SLeon Romanovsky 
482793f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_id_table[] = {
482893f82444SLeon Romanovsky 	{ .name = MLX5_ADEV_NAME ".rdma", },
482993f82444SLeon Romanovsky 	{},
483093f82444SLeon Romanovsky };
483193f82444SLeon Romanovsky 
483293f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_mp_id_table);
483393f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_id_table);
483493f82444SLeon Romanovsky 
483593f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_mp_driver = {
483693f82444SLeon Romanovsky 	.name = "multiport",
483793f82444SLeon Romanovsky 	.probe = mlx5r_mp_probe,
483893f82444SLeon Romanovsky 	.remove = mlx5r_mp_remove,
483993f82444SLeon Romanovsky 	.id_table = mlx5r_mp_id_table,
484093f82444SLeon Romanovsky };
484193f82444SLeon Romanovsky 
484293f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_driver = {
484393f82444SLeon Romanovsky 	.name = "rdma",
484493f82444SLeon Romanovsky 	.probe = mlx5r_probe,
484593f82444SLeon Romanovsky 	.remove = mlx5r_remove,
484693f82444SLeon Romanovsky 	.id_table = mlx5r_id_table,
4847e126ba97SEli Cohen };
4848e126ba97SEli Cohen 
4849e126ba97SEli Cohen static int __init mlx5_ib_init(void)
4850e126ba97SEli Cohen {
485193f82444SLeon Romanovsky 	int ret;
48526aec21f6SHaggai Eran 
48538010d74bSJason Gunthorpe 	xlt_emergency_page = (void *)__get_free_page(GFP_KERNEL);
4854c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
4855d69a24e0SDaniel Jurgens 		return -ENOMEM;
4856d69a24e0SDaniel Jurgens 
4857c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
4858c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
48598010d74bSJason Gunthorpe 		free_page((unsigned long)xlt_emergency_page);
4860c44ef998SIlya Lesokhin 		return -ENOMEM;
4861c44ef998SIlya Lesokhin 	}
4862c44ef998SIlya Lesokhin 
486381713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
486493f82444SLeon Romanovsky 	ret = mlx5r_rep_init();
486593f82444SLeon Romanovsky 	if (ret)
486693f82444SLeon Romanovsky 		goto rep_err;
486793f82444SLeon Romanovsky 	ret = auxiliary_driver_register(&mlx5r_mp_driver);
486893f82444SLeon Romanovsky 	if (ret)
486993f82444SLeon Romanovsky 		goto mp_err;
487093f82444SLeon Romanovsky 	ret = auxiliary_driver_register(&mlx5r_driver);
487193f82444SLeon Romanovsky 	if (ret)
487293f82444SLeon Romanovsky 		goto drv_err;
487393f82444SLeon Romanovsky 	return 0;
48749603b61dSJack Morgenstein 
487593f82444SLeon Romanovsky drv_err:
487693f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_mp_driver);
487793f82444SLeon Romanovsky mp_err:
487893f82444SLeon Romanovsky 	mlx5r_rep_cleanup();
487993f82444SLeon Romanovsky rep_err:
488093f82444SLeon Romanovsky 	destroy_workqueue(mlx5_ib_event_wq);
488193f82444SLeon Romanovsky 	free_page((unsigned long)xlt_emergency_page);
488293f82444SLeon Romanovsky 	return ret;
4883e126ba97SEli Cohen }
4884e126ba97SEli Cohen 
4885e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
4886e126ba97SEli Cohen {
488793f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_driver);
488893f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_mp_driver);
488993f82444SLeon Romanovsky 	mlx5r_rep_cleanup();
489093f82444SLeon Romanovsky 
4891d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
48928010d74bSJason Gunthorpe 	free_page((unsigned long)xlt_emergency_page);
4893e126ba97SEli Cohen }
4894e126ba97SEli Cohen 
4895e126ba97SEli Cohen module_init(mlx5_ib_init);
4896e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
4897