xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision c44ef998f25eaddcd78924f98e5baed602d933e6)
1e126ba97SEli Cohen /*
26cf0a15fSSaeed Mahameed  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3e126ba97SEli Cohen  *
4e126ba97SEli Cohen  * This software is available to you under a choice of one of two
5e126ba97SEli Cohen  * licenses.  You may choose to be licensed under the terms of the GNU
6e126ba97SEli Cohen  * General Public License (GPL) Version 2, available from the file
7e126ba97SEli Cohen  * COPYING in the main directory of this source tree, or the
8e126ba97SEli Cohen  * OpenIB.org BSD license below:
9e126ba97SEli Cohen  *
10e126ba97SEli Cohen  *     Redistribution and use in source and binary forms, with or
11e126ba97SEli Cohen  *     without modification, are permitted provided that the following
12e126ba97SEli Cohen  *     conditions are met:
13e126ba97SEli Cohen  *
14e126ba97SEli Cohen  *      - Redistributions of source code must retain the above
15e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
16e126ba97SEli Cohen  *        disclaimer.
17e126ba97SEli Cohen  *
18e126ba97SEli Cohen  *      - Redistributions in binary form must reproduce the above
19e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
20e126ba97SEli Cohen  *        disclaimer in the documentation and/or other materials
21e126ba97SEli Cohen  *        provided with the distribution.
22e126ba97SEli Cohen  *
23e126ba97SEli Cohen  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e126ba97SEli Cohen  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e126ba97SEli Cohen  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e126ba97SEli Cohen  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e126ba97SEli Cohen  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e126ba97SEli Cohen  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e126ba97SEli Cohen  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e126ba97SEli Cohen  * SOFTWARE.
31e126ba97SEli Cohen  */
32e126ba97SEli Cohen 
33fe248c3aSMaor Gottlieb #include <linux/debugfs.h>
34adec640eSChristoph Hellwig #include <linux/highmem.h>
35e126ba97SEli Cohen #include <linux/module.h>
36e126ba97SEli Cohen #include <linux/init.h>
37e126ba97SEli Cohen #include <linux/errno.h>
38e126ba97SEli Cohen #include <linux/pci.h>
39e126ba97SEli Cohen #include <linux/dma-mapping.h>
40e126ba97SEli Cohen #include <linux/slab.h>
4137aa5c36SGuy Levi #if defined(CONFIG_X86)
4237aa5c36SGuy Levi #include <asm/pat.h>
4337aa5c36SGuy Levi #endif
44e126ba97SEli Cohen #include <linux/sched.h>
456e84f315SIngo Molnar #include <linux/sched/mm.h>
460881e7bdSIngo Molnar #include <linux/sched/task.h>
477c2344c3SMaor Gottlieb #include <linux/delay.h>
48e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
493f89a643SAchiad Shochat #include <rdma/ib_addr.h>
502811ba51SAchiad Shochat #include <rdma/ib_cache.h>
51ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
521b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
5372c7fe90SPravin Shedge #include <linux/mlx5/fs.h>
547c2344c3SMaor Gottlieb #include <linux/list.h>
55e126ba97SEli Cohen #include <rdma/ib_smi.h>
56e126ba97SEli Cohen #include <rdma/ib_umem.h>
57038d2ef8SMaor Gottlieb #include <linux/in.h>
58038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
59e126ba97SEli Cohen #include "mlx5_ib.h"
60fc385b7aSMark Bloch #include "ib_rep.h"
61e1f24a79SParav Pandit #include "cmd.h"
623346c487SBoris Pismenny #include <linux/mlx5/fs_helpers.h>
63e126ba97SEli Cohen 
64e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
65b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0"
66e126ba97SEli Cohen 
67e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
68e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
69e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
70e126ba97SEli Cohen 
71e126ba97SEli Cohen static char mlx5_version[] =
72e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
73b359911dSTariq Toukan 	DRIVER_VERSION "\n";
74e126ba97SEli Cohen 
75d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
76d69a24e0SDaniel Jurgens 	struct work_struct	work;
77d69a24e0SDaniel Jurgens 	struct mlx5_core_dev	*dev;
78d69a24e0SDaniel Jurgens 	void			*context;
79d69a24e0SDaniel Jurgens 	enum mlx5_dev_event	event;
80d69a24e0SDaniel Jurgens 	unsigned long		param;
81d69a24e0SDaniel Jurgens };
82d69a24e0SDaniel Jurgens 
83da7525d2SEran Ben Elisha enum {
84da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
85da7525d2SEran Ben Elisha };
861b5daf11SMajd Dibbiny 
87d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
8832f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
8932f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
9032f69e4bSDaniel Jurgens /*
9132f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
9232f69e4bSDaniel Jurgens  */
9332f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
9432f69e4bSDaniel Jurgens 
95*c44ef998SIlya Lesokhin /* We can't use an array for xlt_emergency_page because dma_map_single
96*c44ef998SIlya Lesokhin  * doesn't work on kernel modules memory
97*c44ef998SIlya Lesokhin  */
98*c44ef998SIlya Lesokhin static unsigned long xlt_emergency_page;
99*c44ef998SIlya Lesokhin static struct mutex xlt_emergency_page_mutex;
100*c44ef998SIlya Lesokhin 
10132f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
10232f69e4bSDaniel Jurgens {
10332f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
10432f69e4bSDaniel Jurgens 
10532f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
10632f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
10732f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
10832f69e4bSDaniel Jurgens 	return dev;
10932f69e4bSDaniel Jurgens }
11032f69e4bSDaniel Jurgens 
1111b5daf11SMajd Dibbiny static enum rdma_link_layer
112ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
1131b5daf11SMajd Dibbiny {
114ebd61f68SAchiad Shochat 	switch (port_type_cap) {
1151b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
1161b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
1171b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
1181b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
1191b5daf11SMajd Dibbiny 	default:
1201b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1211b5daf11SMajd Dibbiny 	}
1221b5daf11SMajd Dibbiny }
1231b5daf11SMajd Dibbiny 
124ebd61f68SAchiad Shochat static enum rdma_link_layer
125ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
126ebd61f68SAchiad Shochat {
127ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
128ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
129ebd61f68SAchiad Shochat 
130ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
131ebd61f68SAchiad Shochat }
132ebd61f68SAchiad Shochat 
133fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
134fd65f1b8SMoni Shoua 			  u8 port_num,
135fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
136fd65f1b8SMoni Shoua {
137fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
138fd65f1b8SMoni Shoua 	int ret;
139fd65f1b8SMoni Shoua 
140fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1418e6efa3aSMark Bloch 	ret = ibdev->query_port(ibdev, port_num, &attr);
142fd65f1b8SMoni Shoua 	if (!ret)
143fd65f1b8SMoni Shoua 		*state = attr.state;
144fd65f1b8SMoni Shoua 	return ret;
145fd65f1b8SMoni Shoua }
146fd65f1b8SMoni Shoua 
147fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
148fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
149fc24fc5eSAchiad Shochat {
1507fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
151fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1527fd8aefbSDaniel Jurgens 	u8 port_num = roce->native_port_num;
1537fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1547fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1557fd8aefbSDaniel Jurgens 
1567fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
15732f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
15832f69e4bSDaniel Jurgens 	if (!mdev)
15932f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
160fc24fc5eSAchiad Shochat 
1615ec8c83eSAviv Heller 	switch (event) {
1625ec8c83eSAviv Heller 	case NETDEV_REGISTER:
1635ec8c83eSAviv Heller 	case NETDEV_UNREGISTER:
1647fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
165bcf87f1dSMark Bloch 		if (ibdev->rep) {
166bcf87f1dSMark Bloch 			struct mlx5_eswitch *esw = ibdev->mdev->priv.eswitch;
167bcf87f1dSMark Bloch 			struct net_device *rep_ndev;
1687fd8aefbSDaniel Jurgens 
169bcf87f1dSMark Bloch 			rep_ndev = mlx5_ib_get_rep_netdev(esw,
170bcf87f1dSMark Bloch 							  ibdev->rep->vport);
171bcf87f1dSMark Bloch 			if (rep_ndev == ndev)
1727fd8aefbSDaniel Jurgens 				roce->netdev = (event == NETDEV_UNREGISTER) ?
1735ec8c83eSAviv Heller 					NULL : ndev;
174bcf87f1dSMark Bloch 		} else if (ndev->dev.parent == &ibdev->mdev->pdev->dev) {
175bcf87f1dSMark Bloch 			roce->netdev = (event == NETDEV_UNREGISTER) ?
176bcf87f1dSMark Bloch 				NULL : ndev;
177bcf87f1dSMark Bloch 		}
1787fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
1795ec8c83eSAviv Heller 		break;
1805ec8c83eSAviv Heller 
181fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
1825ec8c83eSAviv Heller 	case NETDEV_UP:
18388621dfeSAviv Heller 	case NETDEV_DOWN: {
1847fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
18588621dfeSAviv Heller 		struct net_device *upper = NULL;
18688621dfeSAviv Heller 
18788621dfeSAviv Heller 		if (lag_ndev) {
18888621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
18988621dfeSAviv Heller 			dev_put(lag_ndev);
19088621dfeSAviv Heller 		}
19188621dfeSAviv Heller 
1927fd8aefbSDaniel Jurgens 		if ((upper == ndev || (!upper && ndev == roce->netdev))
19388621dfeSAviv Heller 		    && ibdev->ib_active) {
194626bc02dSBart Van Assche 			struct ib_event ibev = { };
195fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
1965ec8c83eSAviv Heller 
1977fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
1987fd8aefbSDaniel Jurgens 					   &port_state))
1997fd8aefbSDaniel Jurgens 				goto done;
200fd65f1b8SMoni Shoua 
2017fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2027fd8aefbSDaniel Jurgens 				goto done;
203fd65f1b8SMoni Shoua 
2047fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2055ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
206fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
207fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
208fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
209fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
210fd65f1b8SMoni Shoua 			else
2117fd8aefbSDaniel Jurgens 				goto done;
212fd65f1b8SMoni Shoua 
2137fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2145ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2155ec8c83eSAviv Heller 		}
2165ec8c83eSAviv Heller 		break;
21788621dfeSAviv Heller 	}
2185ec8c83eSAviv Heller 
2195ec8c83eSAviv Heller 	default:
2205ec8c83eSAviv Heller 		break;
2215ec8c83eSAviv Heller 	}
2227fd8aefbSDaniel Jurgens done:
22332f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
224fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
225fc24fc5eSAchiad Shochat }
226fc24fc5eSAchiad Shochat 
227fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
228fc24fc5eSAchiad Shochat 					     u8 port_num)
229fc24fc5eSAchiad Shochat {
230fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
231fc24fc5eSAchiad Shochat 	struct net_device *ndev;
23232f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
233fc24fc5eSAchiad Shochat 
23432f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
23532f69e4bSDaniel Jurgens 	if (!mdev)
23632f69e4bSDaniel Jurgens 		return NULL;
23732f69e4bSDaniel Jurgens 
23832f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
23988621dfeSAviv Heller 	if (ndev)
24032f69e4bSDaniel Jurgens 		goto out;
24188621dfeSAviv Heller 
242fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
243fc24fc5eSAchiad Shochat 	 */
2447fd8aefbSDaniel Jurgens 	read_lock(&ibdev->roce[port_num - 1].netdev_lock);
2457fd8aefbSDaniel Jurgens 	ndev = ibdev->roce[port_num - 1].netdev;
246fc24fc5eSAchiad Shochat 	if (ndev)
247fc24fc5eSAchiad Shochat 		dev_hold(ndev);
2487fd8aefbSDaniel Jurgens 	read_unlock(&ibdev->roce[port_num - 1].netdev_lock);
249fc24fc5eSAchiad Shochat 
25032f69e4bSDaniel Jurgens out:
25132f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
252fc24fc5eSAchiad Shochat 	return ndev;
253fc24fc5eSAchiad Shochat }
254fc24fc5eSAchiad Shochat 
25532f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
25632f69e4bSDaniel Jurgens 						   u8 ib_port_num,
25732f69e4bSDaniel Jurgens 						   u8 *native_port_num)
25832f69e4bSDaniel Jurgens {
25932f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
26032f69e4bSDaniel Jurgens 							  ib_port_num);
26132f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
26232f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
26332f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
26432f69e4bSDaniel Jurgens 
26532f69e4bSDaniel Jurgens 	if (native_port_num)
26632f69e4bSDaniel Jurgens 		*native_port_num = 1;
26732f69e4bSDaniel Jurgens 
26832f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
26932f69e4bSDaniel Jurgens 		return ibdev->mdev;
27032f69e4bSDaniel Jurgens 
27132f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
27232f69e4bSDaniel Jurgens 	if (!port)
27332f69e4bSDaniel Jurgens 		return NULL;
27432f69e4bSDaniel Jurgens 
27532f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
27632f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
27732f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
27832f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
27932f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
28032f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
28132f69e4bSDaniel Jurgens 		 */
28232f69e4bSDaniel Jurgens 		if (!mpi->is_master)
28332f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
28432f69e4bSDaniel Jurgens 	}
28532f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
28632f69e4bSDaniel Jurgens 
28732f69e4bSDaniel Jurgens 	return mdev;
28832f69e4bSDaniel Jurgens }
28932f69e4bSDaniel Jurgens 
29032f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
29132f69e4bSDaniel Jurgens {
29232f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
29332f69e4bSDaniel Jurgens 							  port_num);
29432f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
29532f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
29632f69e4bSDaniel Jurgens 
29732f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
29832f69e4bSDaniel Jurgens 		return;
29932f69e4bSDaniel Jurgens 
30032f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
30132f69e4bSDaniel Jurgens 
30232f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
30332f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
30432f69e4bSDaniel Jurgens 	if (mpi->is_master)
30532f69e4bSDaniel Jurgens 		goto out;
30632f69e4bSDaniel Jurgens 
30732f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
30832f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
30932f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
31032f69e4bSDaniel Jurgens out:
31132f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
31232f69e4bSDaniel Jurgens }
31332f69e4bSDaniel Jurgens 
314f1b65df5SNoa Osherovich static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
315f1b65df5SNoa Osherovich 				    u8 *active_width)
316f1b65df5SNoa Osherovich {
317f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
318f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
319f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
320f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
321f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
322f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
323f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
324f1b65df5SNoa Osherovich 		break;
325f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
326f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
327f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
328f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
329f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
330f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
331f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
332f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
333f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
334f1b65df5SNoa Osherovich 		break;
335f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
336f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
337f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
338f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
339f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
340f1b65df5SNoa Osherovich 		break;
341f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
342f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
343f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
344f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
345f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
346f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
347f1b65df5SNoa Osherovich 		break;
348f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
349f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
350f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
351f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
352f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
353f1b65df5SNoa Osherovich 		break;
354f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
355f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
356f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
357f1b65df5SNoa Osherovich 		break;
358f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
359f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
360f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
361f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
362f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
363f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
364f1b65df5SNoa Osherovich 		break;
365f1b65df5SNoa Osherovich 	default:
366f1b65df5SNoa Osherovich 		return -EINVAL;
367f1b65df5SNoa Osherovich 	}
368f1b65df5SNoa Osherovich 
369f1b65df5SNoa Osherovich 	return 0;
370f1b65df5SNoa Osherovich }
371f1b65df5SNoa Osherovich 
372095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
3733f89a643SAchiad Shochat 				struct ib_port_attr *props)
3743f89a643SAchiad Shochat {
3753f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
376da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
37788621dfeSAviv Heller 	struct net_device *ndev, *upper;
3783f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
379b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
380c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
381f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
382b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
383095b0927SIlan Tayari 	int err;
3843f89a643SAchiad Shochat 
385b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
386b3cbd6f0SDaniel Jurgens 	if (!mdev) {
387b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
388b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
389b3cbd6f0SDaniel Jurgens 		 */
390b3cbd6f0SDaniel Jurgens 		put_mdev = false;
391b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
392b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
393b3cbd6f0SDaniel Jurgens 		port_num = 1;
394b3cbd6f0SDaniel Jurgens 	}
395b3cbd6f0SDaniel Jurgens 
396f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
397f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
39850f22fd8SNoa Osherovich 	 */
399b3cbd6f0SDaniel Jurgens 	err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper,
400b3cbd6f0SDaniel Jurgens 					     mdev_port_num);
401095b0927SIlan Tayari 	if (err)
402b3cbd6f0SDaniel Jurgens 		goto out;
403f1b65df5SNoa Osherovich 
404f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
405f1b65df5SNoa Osherovich 				 &props->active_width);
4063f89a643SAchiad Shochat 
4073f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
4083f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
4093f89a643SAchiad Shochat 
4103f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
4113f89a643SAchiad Shochat 						roce_address_table_size);
4123f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
4133f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
4143f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
4153f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
4163f89a643SAchiad Shochat 	props->phys_state       = 3;
4173f89a643SAchiad Shochat 
418b3cbd6f0SDaniel Jurgens 	mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
419c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
4203f89a643SAchiad Shochat 
421b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
422b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
423b3cbd6f0SDaniel Jurgens 		goto out;
424b3cbd6f0SDaniel Jurgens 
4253f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
4263f89a643SAchiad Shochat 	if (!ndev)
427b3cbd6f0SDaniel Jurgens 		goto out;
4283f89a643SAchiad Shochat 
42988621dfeSAviv Heller 	if (mlx5_lag_is_active(dev->mdev)) {
43088621dfeSAviv Heller 		rcu_read_lock();
43188621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
43288621dfeSAviv Heller 		if (upper) {
43388621dfeSAviv Heller 			dev_put(ndev);
43488621dfeSAviv Heller 			ndev = upper;
43588621dfeSAviv Heller 			dev_hold(ndev);
43688621dfeSAviv Heller 		}
43788621dfeSAviv Heller 		rcu_read_unlock();
43888621dfeSAviv Heller 	}
43988621dfeSAviv Heller 
4403f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
4413f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
4423f89a643SAchiad Shochat 		props->phys_state = 5;
4433f89a643SAchiad Shochat 	}
4443f89a643SAchiad Shochat 
4453f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
4463f89a643SAchiad Shochat 
4473f89a643SAchiad Shochat 	dev_put(ndev);
4483f89a643SAchiad Shochat 
4493f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
450b3cbd6f0SDaniel Jurgens out:
451b3cbd6f0SDaniel Jurgens 	if (put_mdev)
452b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
453b3cbd6f0SDaniel Jurgens 	return err;
4543f89a643SAchiad Shochat }
4553f89a643SAchiad Shochat 
456095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
457095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
458095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
4593cca2606SAchiad Shochat {
460095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
461095b0927SIlan Tayari 	u8 roce_version = 0;
462095b0927SIlan Tayari 	u8 roce_l3_type = 0;
463095b0927SIlan Tayari 	bool vlan = false;
464095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
465095b0927SIlan Tayari 	u16 vlan_id = 0;
4663cca2606SAchiad Shochat 
467095b0927SIlan Tayari 	if (gid) {
468095b0927SIlan Tayari 		gid_type = attr->gid_type;
469095b0927SIlan Tayari 		ether_addr_copy(mac, attr->ndev->dev_addr);
4703cca2606SAchiad Shochat 
4713cca2606SAchiad Shochat 		if (is_vlan_dev(attr->ndev)) {
472095b0927SIlan Tayari 			vlan = true;
473095b0927SIlan Tayari 			vlan_id = vlan_dev_vlan_id(attr->ndev);
474095b0927SIlan Tayari 		}
4753cca2606SAchiad Shochat 	}
4763cca2606SAchiad Shochat 
477095b0927SIlan Tayari 	switch (gid_type) {
4783cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
479095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
4803cca2606SAchiad Shochat 		break;
4813cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
482095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
483095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
484095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
485095b0927SIlan Tayari 		else
486095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
4873cca2606SAchiad Shochat 		break;
4883cca2606SAchiad Shochat 
4893cca2606SAchiad Shochat 	default:
490095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
4913cca2606SAchiad Shochat 	}
4923cca2606SAchiad Shochat 
493095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
494095b0927SIlan Tayari 				      roce_l3_type, gid->raw, mac, vlan,
495cfe4e37fSDaniel Jurgens 				      vlan_id, port_num);
4963cca2606SAchiad Shochat }
4973cca2606SAchiad Shochat 
4983cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
4993cca2606SAchiad Shochat 			   unsigned int index, const union ib_gid *gid,
5003cca2606SAchiad Shochat 			   const struct ib_gid_attr *attr,
5013cca2606SAchiad Shochat 			   __always_unused void **context)
5023cca2606SAchiad Shochat {
503095b0927SIlan Tayari 	return set_roce_addr(to_mdev(device), port_num, index, gid, attr);
5043cca2606SAchiad Shochat }
5053cca2606SAchiad Shochat 
5063cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
5073cca2606SAchiad Shochat 			   unsigned int index, __always_unused void **context)
5083cca2606SAchiad Shochat {
509095b0927SIlan Tayari 	return set_roce_addr(to_mdev(device), port_num, index, NULL, NULL);
5103cca2606SAchiad Shochat }
5113cca2606SAchiad Shochat 
5122811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
5132811ba51SAchiad Shochat 			       int index)
5142811ba51SAchiad Shochat {
5152811ba51SAchiad Shochat 	struct ib_gid_attr attr;
5162811ba51SAchiad Shochat 	union ib_gid gid;
5172811ba51SAchiad Shochat 
5182811ba51SAchiad Shochat 	if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
5192811ba51SAchiad Shochat 		return 0;
5202811ba51SAchiad Shochat 
5212811ba51SAchiad Shochat 	if (!attr.ndev)
5222811ba51SAchiad Shochat 		return 0;
5232811ba51SAchiad Shochat 
5242811ba51SAchiad Shochat 	dev_put(attr.ndev);
5252811ba51SAchiad Shochat 
5262811ba51SAchiad Shochat 	if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
5272811ba51SAchiad Shochat 		return 0;
5282811ba51SAchiad Shochat 
5292811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
5302811ba51SAchiad Shochat }
5312811ba51SAchiad Shochat 
532ed88451eSMajd Dibbiny int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
533ed88451eSMajd Dibbiny 			   int index, enum ib_gid_type *gid_type)
534ed88451eSMajd Dibbiny {
535ed88451eSMajd Dibbiny 	struct ib_gid_attr attr;
536ed88451eSMajd Dibbiny 	union ib_gid gid;
537ed88451eSMajd Dibbiny 	int ret;
538ed88451eSMajd Dibbiny 
539ed88451eSMajd Dibbiny 	ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
540ed88451eSMajd Dibbiny 	if (ret)
541ed88451eSMajd Dibbiny 		return ret;
542ed88451eSMajd Dibbiny 
543ed88451eSMajd Dibbiny 	if (!attr.ndev)
544ed88451eSMajd Dibbiny 		return -ENODEV;
545ed88451eSMajd Dibbiny 
546ed88451eSMajd Dibbiny 	dev_put(attr.ndev);
547ed88451eSMajd Dibbiny 
548ed88451eSMajd Dibbiny 	*gid_type = attr.gid_type;
549ed88451eSMajd Dibbiny 
550ed88451eSMajd Dibbiny 	return 0;
551ed88451eSMajd Dibbiny }
552ed88451eSMajd Dibbiny 
5531b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
5541b5daf11SMajd Dibbiny {
5557fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
556d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
5577fae6655SNoa Osherovich 	return 0;
5581b5daf11SMajd Dibbiny }
5591b5daf11SMajd Dibbiny 
5601b5daf11SMajd Dibbiny enum {
5611b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
5621b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
5631b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
5641b5daf11SMajd Dibbiny };
5651b5daf11SMajd Dibbiny 
5661b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
5671b5daf11SMajd Dibbiny {
5681b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
5691b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
5701b5daf11SMajd Dibbiny 
571ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
5721b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
5731b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
5741b5daf11SMajd Dibbiny 
5751b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
5761b5daf11SMajd Dibbiny }
5771b5daf11SMajd Dibbiny 
578da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
579776a3906SMoni Shoua 			    u8 atomic_size_qp,
580da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
581da7525d2SEran Ben Elisha {
582da7525d2SEran Ben Elisha 	u8 tmp;
583da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
584da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
585bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
586da7525d2SEran Ben Elisha 
587da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
588da7525d2SEran Ben Elisha 	 * of host endianness respond
589da7525d2SEran Ben Elisha 	 */
590da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
591da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
592da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
593da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
594da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
595da7525d2SEran Ben Elisha 	} else {
596da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
597da7525d2SEran Ben Elisha 	}
598da7525d2SEran Ben Elisha }
599da7525d2SEran Ben Elisha 
600776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
601776a3906SMoni Shoua 			       struct ib_device_attr *props)
602776a3906SMoni Shoua {
603776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
604776a3906SMoni Shoua 
605776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
606776a3906SMoni Shoua }
607776a3906SMoni Shoua 
608776a3906SMoni Shoua static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
609776a3906SMoni Shoua 			       struct ib_device_attr *props)
610776a3906SMoni Shoua {
611776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
612776a3906SMoni Shoua 
613776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
614776a3906SMoni Shoua }
615776a3906SMoni Shoua 
616776a3906SMoni Shoua bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
617776a3906SMoni Shoua {
618776a3906SMoni Shoua 	struct ib_device_attr props = {};
619776a3906SMoni Shoua 
620776a3906SMoni Shoua 	get_atomic_caps_dc(dev, &props);
621776a3906SMoni Shoua 	return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
622776a3906SMoni Shoua }
6231b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
6241b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
6251b5daf11SMajd Dibbiny {
6261b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6271b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6281b5daf11SMajd Dibbiny 	u64 tmp;
6291b5daf11SMajd Dibbiny 	int err;
6301b5daf11SMajd Dibbiny 
6311b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6321b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6331b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
6341b5daf11SMajd Dibbiny 							    sys_image_guid);
6351b5daf11SMajd Dibbiny 
6361b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6371b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
6383f89a643SAchiad Shochat 		break;
6393f89a643SAchiad Shochat 
6403f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6413f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
6423f89a643SAchiad Shochat 		break;
6431b5daf11SMajd Dibbiny 
6441b5daf11SMajd Dibbiny 	default:
6451b5daf11SMajd Dibbiny 		return -EINVAL;
6461b5daf11SMajd Dibbiny 	}
6473f89a643SAchiad Shochat 
6483f89a643SAchiad Shochat 	if (!err)
6493f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
6503f89a643SAchiad Shochat 
6513f89a643SAchiad Shochat 	return err;
6523f89a643SAchiad Shochat 
6531b5daf11SMajd Dibbiny }
6541b5daf11SMajd Dibbiny 
6551b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
6561b5daf11SMajd Dibbiny 				u16 *max_pkeys)
6571b5daf11SMajd Dibbiny {
6581b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6591b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6601b5daf11SMajd Dibbiny 
6611b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6621b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6631b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
6641b5daf11SMajd Dibbiny 
6651b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6661b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6671b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
6681b5daf11SMajd Dibbiny 						pkey_table_size));
6691b5daf11SMajd Dibbiny 		return 0;
6701b5daf11SMajd Dibbiny 
6711b5daf11SMajd Dibbiny 	default:
6721b5daf11SMajd Dibbiny 		return -EINVAL;
6731b5daf11SMajd Dibbiny 	}
6741b5daf11SMajd Dibbiny }
6751b5daf11SMajd Dibbiny 
6761b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
6771b5daf11SMajd Dibbiny 				u32 *vendor_id)
6781b5daf11SMajd Dibbiny {
6791b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6801b5daf11SMajd Dibbiny 
6811b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6821b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6831b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
6841b5daf11SMajd Dibbiny 
6851b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6861b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6871b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
6881b5daf11SMajd Dibbiny 
6891b5daf11SMajd Dibbiny 	default:
6901b5daf11SMajd Dibbiny 		return -EINVAL;
6911b5daf11SMajd Dibbiny 	}
6921b5daf11SMajd Dibbiny }
6931b5daf11SMajd Dibbiny 
6941b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
6951b5daf11SMajd Dibbiny 				__be64 *node_guid)
6961b5daf11SMajd Dibbiny {
6971b5daf11SMajd Dibbiny 	u64 tmp;
6981b5daf11SMajd Dibbiny 	int err;
6991b5daf11SMajd Dibbiny 
7001b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7011b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7021b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7031b5daf11SMajd Dibbiny 
7041b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7051b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7063f89a643SAchiad Shochat 		break;
7073f89a643SAchiad Shochat 
7083f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7093f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7103f89a643SAchiad Shochat 		break;
7111b5daf11SMajd Dibbiny 
7121b5daf11SMajd Dibbiny 	default:
7131b5daf11SMajd Dibbiny 		return -EINVAL;
7141b5daf11SMajd Dibbiny 	}
7153f89a643SAchiad Shochat 
7163f89a643SAchiad Shochat 	if (!err)
7173f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7183f89a643SAchiad Shochat 
7193f89a643SAchiad Shochat 	return err;
7201b5daf11SMajd Dibbiny }
7211b5daf11SMajd Dibbiny 
7221b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
723bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
7241b5daf11SMajd Dibbiny };
7251b5daf11SMajd Dibbiny 
7261b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
7271b5daf11SMajd Dibbiny {
7281b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
7291b5daf11SMajd Dibbiny 
7301b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
7311b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
7321b5daf11SMajd Dibbiny 
7331b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
7341b5daf11SMajd Dibbiny 
7351b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
7361b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
7371b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
7381b5daf11SMajd Dibbiny }
7391b5daf11SMajd Dibbiny 
740e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
7412528e33eSMatan Barak 				struct ib_device_attr *props,
7422528e33eSMatan Barak 				struct ib_udata *uhw)
743e126ba97SEli Cohen {
744e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
745938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
746e126ba97SEli Cohen 	int err = -ENOMEM;
747288c01b7SEli Cohen 	int max_sq_desc;
748e126ba97SEli Cohen 	int max_rq_sg;
749e126ba97SEli Cohen 	int max_sq_sg;
750e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
75185c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
752402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
753402ca536SBodong Wang 	size_t resp_len;
754402ca536SBodong Wang 	u64 max_tso;
755e126ba97SEli Cohen 
756402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
757402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
758402ca536SBodong Wang 		return -EINVAL;
759402ca536SBodong Wang 	else
760402ca536SBodong Wang 		resp.response_length = resp_len;
761402ca536SBodong Wang 
762402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
7632528e33eSMatan Barak 		return -EINVAL;
7642528e33eSMatan Barak 
765e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
7661b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
7671b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
7681b5daf11SMajd Dibbiny 	if (err)
7691b5daf11SMajd Dibbiny 		return err;
7701b5daf11SMajd Dibbiny 
7711b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
7721b5daf11SMajd Dibbiny 	if (err)
7731b5daf11SMajd Dibbiny 		return err;
7741b5daf11SMajd Dibbiny 
7751b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
7761b5daf11SMajd Dibbiny 	if (err)
7771b5daf11SMajd Dibbiny 		return err;
778e126ba97SEli Cohen 
7799603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
7809603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
7819603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
782e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
783e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
784e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
7851a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
786938fe83cSSaeed Mahameed 
787938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
788e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
789938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
790e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
791938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
792e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
793938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
794e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
795d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
796d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
797d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
798d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
799b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
800b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
801d2370e0aSMatan Barak 	}
802e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
803938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
8042dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
8052dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8062dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8072dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8082dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8092dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8102dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8112dea9094SSagi Grimberg 	}
812938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
813f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
814e126ba97SEli Cohen 
81585c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
816e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
817e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
81888115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
819e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
820e8161334SNoa Osherovich 		}
821e8161334SNoa Osherovich 
822e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
823e8161334SNoa Osherovich 			props->raw_packet_caps |=
824e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
82588115fe7SBodong Wang 
826402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
827402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
828402ca536SBodong Wang 			if (max_tso) {
829402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
830402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
831402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
832402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
833402ca536SBodong Wang 			}
834402ca536SBodong Wang 		}
83531f69a82SYishai Hadas 
83631f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
83731f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
83831f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
83931f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
84031f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
84131f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
84231f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
84331f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
84431f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
84531f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
84631f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
8474e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
8484e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
84931f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
85031f69a82SYishai Hadas 		}
85131f69a82SYishai Hadas 	} else {
85231f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
85331f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
85431f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
85531f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
856402ca536SBodong Wang 	}
857402ca536SBodong Wang 
858f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
859f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
860f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
861f0313965SErez Shitrit 	}
862f0313965SErez Shitrit 
86303404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
86485c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
86585c7c014SDaniel Jurgens 	    raw_support)
86603404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
86703404e8aSMaor Gottlieb 
8681d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
8691d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
8701d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
8711d54f890SYishai Hadas 
872cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
87385c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
87485c7c014SDaniel Jurgens 	    raw_support) {
875e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
876cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
877e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
878e8161334SNoa Osherovich 	}
879cff5a0f3SMajd Dibbiny 
880da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
881da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
882da6d6ba3SMaor Gottlieb 
883b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
884b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
885b1383aa6SNoa Osherovich 
8861b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
8871b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
888e126ba97SEli Cohen 
889e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
890e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
891938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
892938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
893938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
894938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
895288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
896288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
897288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
898e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
899e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
900986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
901938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9029f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
903938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
904938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
905938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
906938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
907938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
908938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
909938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
910e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
911e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
912911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
913911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
914776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
91581bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
916938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
917938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
918e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
919e126ba97SEli Cohen 					   props->max_mcast_grp;
920e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
92186695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
9227c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
9237c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
924e126ba97SEli Cohen 
9258cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
926938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
9278cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
9288cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
9298cdd312cSHaggai Eran #endif
9308cdd312cSHaggai Eran 
931051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
932051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
933051f2630SLeon Romanovsky 
934eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
935eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
936eff901d3SEli Cohen 
93731f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
93885c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
93931f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
94031f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
94131f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
94231f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
94331f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
94431f69a82SYishai Hadas 		props->max_wq_type_rq =
94531f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
94631f69a82SYishai Hadas 	}
94731f69a82SYishai Hadas 
948eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
94978b1beb0SLeon Romanovsky 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
95078b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
951eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
95278b1beb0SLeon Romanovsky 		props->tm_caps.flags = IB_TM_CAP_RC;
95378b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
954eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
95578b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
956eb761894SArtemy Kovalyov 	}
957eb761894SArtemy Kovalyov 
95887ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
95987ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
96087ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
96187ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
96287ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
96387ab3f52SYonatan Cohen 	}
96487ab3f52SYonatan Cohen 
9657e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
9667e43a2a5SBodong Wang 		resp.cqe_comp_caps.max_num =
9677e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
9687e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
9697e43a2a5SBodong Wang 		resp.cqe_comp_caps.supported_format =
9707e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_HASH |
9717e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_CSUM;
9727e43a2a5SBodong Wang 		resp.response_length += sizeof(resp.cqe_comp_caps);
9737e43a2a5SBodong Wang 	}
9747e43a2a5SBodong Wang 
97585c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
97685c7c014SDaniel Jurgens 	    raw_support) {
977d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
978d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
979d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
980d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
981d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
982d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
983d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
984d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
985d949167dSBodong Wang 		}
986d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
987d949167dSBodong Wang 	}
988d949167dSBodong Wang 
9899f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
9909f885201SLeon Romanovsky 			uhw->outlen)) {
991795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
9929f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
993795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
994050da902SBodong Wang 
995050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
996050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
997050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
998050da902SBodong Wang 
9999f885201SLeon Romanovsky 		resp.response_length +=
10009f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
10019f885201SLeon Romanovsky 	}
10029f885201SLeon Romanovsky 
1003de57f2adSGuy Levi 	if (field_avail(typeof(resp), flags, uhw->outlen)) {
1004de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
10057a0c8f42SGuy Levi 
1006de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1007de57f2adSGuy Levi 			resp.flags |=
1008de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
10097a0c8f42SGuy Levi 
10107a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
10117a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
1012de57f2adSGuy Levi 	}
10139f885201SLeon Romanovsky 
101496dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
101596dc3fc5SNoa Osherovich 			uhw->outlen)) {
101696dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
101796dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
101896dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
101996dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
102096dc3fc5SNoa Osherovich 
102196dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
102296dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
102396dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
102496dc3fc5SNoa Osherovich 
102596dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
102696dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
102796dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
102896dc3fc5SNoa Osherovich 
102996dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
103096dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
103196dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
103296dc3fc5SNoa Osherovich 		}
103396dc3fc5SNoa Osherovich 	}
103496dc3fc5SNoa Osherovich 
103585c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
103685c7c014SDaniel Jurgens 	    raw_support) {
1037b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1038b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1039b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1040b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1041b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1042b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1043b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1044b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1045b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1046b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1047b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1048b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1049b4f34597SNoa Osherovich 		}
1050b4f34597SNoa Osherovich 	}
1051b4f34597SNoa Osherovich 
1052f95ef6cbSMaor Gottlieb 	if (field_avail(typeof(resp), tunnel_offloads_caps,
1053f95ef6cbSMaor Gottlieb 			uhw->outlen)) {
1054f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1055f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1056f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1057f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1058f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1059f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1060f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1061f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1062f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1063f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
1064f95ef6cbSMaor Gottlieb 	}
1065f95ef6cbSMaor Gottlieb 
1066402ca536SBodong Wang 	if (uhw->outlen) {
1067402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1068402ca536SBodong Wang 
1069402ca536SBodong Wang 		if (err)
1070402ca536SBodong Wang 			return err;
1071402ca536SBodong Wang 	}
1072402ca536SBodong Wang 
10731b5daf11SMajd Dibbiny 	return 0;
10741b5daf11SMajd Dibbiny }
1075e126ba97SEli Cohen 
10761b5daf11SMajd Dibbiny enum mlx5_ib_width {
10771b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
10781b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
10791b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
10801b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
10811b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
10821b5daf11SMajd Dibbiny };
10831b5daf11SMajd Dibbiny 
10841b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
10851b5daf11SMajd Dibbiny 				  u8 *ib_width)
10861b5daf11SMajd Dibbiny {
10871b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
10881b5daf11SMajd Dibbiny 	int err = 0;
10891b5daf11SMajd Dibbiny 
10901b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
10911b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
10921b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
10931b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
10941b5daf11SMajd Dibbiny 			    (int)active_width);
10951b5daf11SMajd Dibbiny 		err = -EINVAL;
10961b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
10971b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
10981b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
10991b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
11001b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
11011b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
11021b5daf11SMajd Dibbiny 	} else {
11031b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
11041b5daf11SMajd Dibbiny 			    (int)active_width);
11051b5daf11SMajd Dibbiny 		err = -EINVAL;
11061b5daf11SMajd Dibbiny 	}
11071b5daf11SMajd Dibbiny 
11081b5daf11SMajd Dibbiny 	return err;
11091b5daf11SMajd Dibbiny }
11101b5daf11SMajd Dibbiny 
11111b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
11121b5daf11SMajd Dibbiny {
11131b5daf11SMajd Dibbiny 	switch (mtu) {
11141b5daf11SMajd Dibbiny 	case 256: return 1;
11151b5daf11SMajd Dibbiny 	case 512: return 2;
11161b5daf11SMajd Dibbiny 	case 1024: return 3;
11171b5daf11SMajd Dibbiny 	case 2048: return 4;
11181b5daf11SMajd Dibbiny 	case 4096: return 5;
11191b5daf11SMajd Dibbiny 	default:
11201b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
11211b5daf11SMajd Dibbiny 		return -1;
11221b5daf11SMajd Dibbiny 	}
11231b5daf11SMajd Dibbiny }
11241b5daf11SMajd Dibbiny 
11251b5daf11SMajd Dibbiny enum ib_max_vl_num {
11261b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
11271b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
11281b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
11291b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
11301b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
11311b5daf11SMajd Dibbiny };
11321b5daf11SMajd Dibbiny 
11331b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
11341b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
11351b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
11361b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
11371b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
11381b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
11391b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
11401b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
11411b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
11421b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
11431b5daf11SMajd Dibbiny };
11441b5daf11SMajd Dibbiny 
11451b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
11461b5daf11SMajd Dibbiny 				u8 *max_vl_num)
11471b5daf11SMajd Dibbiny {
11481b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
11491b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
11501b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
11511b5daf11SMajd Dibbiny 		break;
11521b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
11531b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
11541b5daf11SMajd Dibbiny 		break;
11551b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
11561b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
11571b5daf11SMajd Dibbiny 		break;
11581b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
11591b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
11601b5daf11SMajd Dibbiny 		break;
11611b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
11621b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
11631b5daf11SMajd Dibbiny 		break;
11641b5daf11SMajd Dibbiny 
11651b5daf11SMajd Dibbiny 	default:
11661b5daf11SMajd Dibbiny 		return -EINVAL;
11671b5daf11SMajd Dibbiny 	}
11681b5daf11SMajd Dibbiny 
11691b5daf11SMajd Dibbiny 	return 0;
11701b5daf11SMajd Dibbiny }
11711b5daf11SMajd Dibbiny 
11721b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
11731b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
11741b5daf11SMajd Dibbiny {
11751b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11761b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
11771b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1178046339eaSSaeed Mahameed 	u16 max_mtu;
1179046339eaSSaeed Mahameed 	u16 oper_mtu;
11801b5daf11SMajd Dibbiny 	int err;
11811b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
11821b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
11831b5daf11SMajd Dibbiny 
11841b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
11851b5daf11SMajd Dibbiny 	if (!rep) {
11861b5daf11SMajd Dibbiny 		err = -ENOMEM;
11871b5daf11SMajd Dibbiny 		goto out;
11881b5daf11SMajd Dibbiny 	}
11891b5daf11SMajd Dibbiny 
1190c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
11911b5daf11SMajd Dibbiny 
11921b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
11931b5daf11SMajd Dibbiny 	if (err)
11941b5daf11SMajd Dibbiny 		goto out;
11951b5daf11SMajd Dibbiny 
11961b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
11971b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
11981b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
11991b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
12001b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
12011b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
12021b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
12031b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
12041b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
12051b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
12061b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
12071b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
12081b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
12091b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
1210eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
12111b5daf11SMajd Dibbiny 
12121b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
12131b5daf11SMajd Dibbiny 	if (err)
12141b5daf11SMajd Dibbiny 		goto out;
12151b5daf11SMajd Dibbiny 
12161b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
12171b5daf11SMajd Dibbiny 				     &props->active_width);
12181b5daf11SMajd Dibbiny 	if (err)
12191b5daf11SMajd Dibbiny 		goto out;
1220d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
12211b5daf11SMajd Dibbiny 	if (err)
12221b5daf11SMajd Dibbiny 		goto out;
12231b5daf11SMajd Dibbiny 
1224facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
12251b5daf11SMajd Dibbiny 
12261b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
12271b5daf11SMajd Dibbiny 
1228facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
12291b5daf11SMajd Dibbiny 
12301b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
12311b5daf11SMajd Dibbiny 
12321b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
12331b5daf11SMajd Dibbiny 	if (err)
12341b5daf11SMajd Dibbiny 		goto out;
12351b5daf11SMajd Dibbiny 
12361b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
12371b5daf11SMajd Dibbiny 				   &props->max_vl_num);
12381b5daf11SMajd Dibbiny out:
12391b5daf11SMajd Dibbiny 	kfree(rep);
1240e126ba97SEli Cohen 	return err;
1241e126ba97SEli Cohen }
1242e126ba97SEli Cohen 
1243e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1244e126ba97SEli Cohen 		       struct ib_port_attr *props)
1245e126ba97SEli Cohen {
1246095b0927SIlan Tayari 	unsigned int count;
1247095b0927SIlan Tayari 	int ret;
1248095b0927SIlan Tayari 
12491b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
12501b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1251095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1252095b0927SIlan Tayari 		break;
1253e126ba97SEli Cohen 
12541b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1255095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1256095b0927SIlan Tayari 		break;
12571b5daf11SMajd Dibbiny 
12583f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1259095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1260095b0927SIlan Tayari 		break;
12613f89a643SAchiad Shochat 
12621b5daf11SMajd Dibbiny 	default:
1263095b0927SIlan Tayari 		ret = -EINVAL;
1264e126ba97SEli Cohen 	}
1265095b0927SIlan Tayari 
1266095b0927SIlan Tayari 	if (!ret && props) {
1267b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1268b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1269b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1270b3cbd6f0SDaniel Jurgens 
1271b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1272b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1273b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1274b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1275b3cbd6f0SDaniel Jurgens 			 */
1276b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1277b3cbd6f0SDaniel Jurgens 			port = 1;
1278b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1279b3cbd6f0SDaniel Jurgens 		}
1280b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1281b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1282b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1283095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1284095b0927SIlan Tayari 	}
1285095b0927SIlan Tayari 	return ret;
1286e126ba97SEli Cohen }
1287e126ba97SEli Cohen 
12888e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
12898e6efa3aSMark Bloch 				  struct ib_port_attr *props)
12908e6efa3aSMark Bloch {
12918e6efa3aSMark Bloch 	int ret;
12928e6efa3aSMark Bloch 
12938e6efa3aSMark Bloch 	/* Only link layer == ethernet is valid for representors */
12948e6efa3aSMark Bloch 	ret = mlx5_query_port_roce(ibdev, port, props);
12958e6efa3aSMark Bloch 	if (ret || !props)
12968e6efa3aSMark Bloch 		return ret;
12978e6efa3aSMark Bloch 
12988e6efa3aSMark Bloch 	/* We don't support GIDS */
12998e6efa3aSMark Bloch 	props->gid_tbl_len = 0;
13008e6efa3aSMark Bloch 
13018e6efa3aSMark Bloch 	return ret;
13028e6efa3aSMark Bloch }
13038e6efa3aSMark Bloch 
1304e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1305e126ba97SEli Cohen 			     union ib_gid *gid)
1306e126ba97SEli Cohen {
13071b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
13081b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1309e126ba97SEli Cohen 
13101b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13111b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
13121b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1313e126ba97SEli Cohen 
13141b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
13151b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1316e126ba97SEli Cohen 
13171b5daf11SMajd Dibbiny 	default:
13181b5daf11SMajd Dibbiny 		return -EINVAL;
13191b5daf11SMajd Dibbiny 	}
1320e126ba97SEli Cohen 
1321e126ba97SEli Cohen }
1322e126ba97SEli Cohen 
1323b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1324b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1325b3cbd6f0SDaniel Jurgens {
1326b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1327b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1328b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1329b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1330b3cbd6f0SDaniel Jurgens 	int err;
1331b3cbd6f0SDaniel Jurgens 
1332b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1333b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1334b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1335b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1336b3cbd6f0SDaniel Jurgens 		 */
1337b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1338b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1339b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1340b3cbd6f0SDaniel Jurgens 	}
1341b3cbd6f0SDaniel Jurgens 
1342b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1343b3cbd6f0SDaniel Jurgens 					index, pkey);
1344b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1345b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1346b3cbd6f0SDaniel Jurgens 
1347b3cbd6f0SDaniel Jurgens 	return err;
1348b3cbd6f0SDaniel Jurgens }
1349b3cbd6f0SDaniel Jurgens 
1350e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1351e126ba97SEli Cohen 			      u16 *pkey)
1352e126ba97SEli Cohen {
13531b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13541b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
13551b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1356e126ba97SEli Cohen 
13571b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
13581b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1359b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
13601b5daf11SMajd Dibbiny 	default:
13611b5daf11SMajd Dibbiny 		return -EINVAL;
1362e126ba97SEli Cohen 	}
13631b5daf11SMajd Dibbiny }
1364e126ba97SEli Cohen 
1365e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1366e126ba97SEli Cohen 				 struct ib_device_modify *props)
1367e126ba97SEli Cohen {
1368e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1369e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1370e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1371e126ba97SEli Cohen 	int err;
1372e126ba97SEli Cohen 
1373e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1374e126ba97SEli Cohen 		return -EOPNOTSUPP;
1375e126ba97SEli Cohen 
1376e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1377e126ba97SEli Cohen 		return 0;
1378e126ba97SEli Cohen 
1379e126ba97SEli Cohen 	/*
1380e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1381e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1382e126ba97SEli Cohen 	 */
1383bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
13849603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1385e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1386e126ba97SEli Cohen 	if (err)
1387e126ba97SEli Cohen 		return err;
1388e126ba97SEli Cohen 
1389bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1390e126ba97SEli Cohen 
1391e126ba97SEli Cohen 	return err;
1392e126ba97SEli Cohen }
1393e126ba97SEli Cohen 
1394cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1395cdbe33d0SEli Cohen 				u32 value)
1396cdbe33d0SEli Cohen {
1397cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1398b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1399b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1400cdbe33d0SEli Cohen 	int err;
1401cdbe33d0SEli Cohen 
1402b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1403b3cbd6f0SDaniel Jurgens 	if (!mdev)
1404b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1405b3cbd6f0SDaniel Jurgens 
1406b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1407cdbe33d0SEli Cohen 	if (err)
1408b3cbd6f0SDaniel Jurgens 		goto out;
1409cdbe33d0SEli Cohen 
1410cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1411cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1412cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1413b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1414b3cbd6f0SDaniel Jurgens 		goto out;
1415cdbe33d0SEli Cohen 	}
1416cdbe33d0SEli Cohen 
1417cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1418cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1419b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1420b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1421b3cbd6f0SDaniel Jurgens 
1422b3cbd6f0SDaniel Jurgens out:
1423b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1424cdbe33d0SEli Cohen 
1425cdbe33d0SEli Cohen 	return err;
1426cdbe33d0SEli Cohen }
1427cdbe33d0SEli Cohen 
1428e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1429e126ba97SEli Cohen 			       struct ib_port_modify *props)
1430e126ba97SEli Cohen {
1431e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1432e126ba97SEli Cohen 	struct ib_port_attr attr;
1433e126ba97SEli Cohen 	u32 tmp;
1434e126ba97SEli Cohen 	int err;
1435cdbe33d0SEli Cohen 	u32 change_mask;
1436cdbe33d0SEli Cohen 	u32 value;
1437cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1438cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1439cdbe33d0SEli Cohen 
1440ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1441ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1442ec255879SMajd Dibbiny 	 */
1443ec255879SMajd Dibbiny 	if (!is_ib)
1444ec255879SMajd Dibbiny 		return 0;
1445ec255879SMajd Dibbiny 
1446cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1447cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1448cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1449cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1450cdbe33d0SEli Cohen 	}
1451e126ba97SEli Cohen 
1452e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1453e126ba97SEli Cohen 
1454c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1455e126ba97SEli Cohen 	if (err)
1456e126ba97SEli Cohen 		goto out;
1457e126ba97SEli Cohen 
1458e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1459e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1460e126ba97SEli Cohen 
14619603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1462e126ba97SEli Cohen 
1463e126ba97SEli Cohen out:
1464e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1465e126ba97SEli Cohen 	return err;
1466e126ba97SEli Cohen }
1467e126ba97SEli Cohen 
146830aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
146930aa60b3SEli Cohen {
147030aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
147130aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
147230aa60b3SEli Cohen }
147330aa60b3SEli Cohen 
147431a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
147531a78a5aSYishai Hadas {
147631a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
147731a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
147831a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
147931a78a5aSYishai Hadas 
148031a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
148131a78a5aSYishai Hadas }
148231a78a5aSYishai Hadas 
1483b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1484b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
148531a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1486b037c29aSEli Cohen {
1487b037c29aSEli Cohen 	int uars_per_sys_page;
1488b037c29aSEli Cohen 	int bfregs_per_sys_page;
1489b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1490b037c29aSEli Cohen 
1491b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1492b037c29aSEli Cohen 		return -EINVAL;
1493b037c29aSEli Cohen 
1494b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1495b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1496b037c29aSEli Cohen 
1497b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1498b037c29aSEli Cohen 		return -ENOMEM;
1499b037c29aSEli Cohen 
1500b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1501b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
150231a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1503b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1504b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1505b037c29aSEli Cohen 		return -EINVAL;
1506b037c29aSEli Cohen 
150731a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
150831a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
150931a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
151031a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
151131a78a5aSYishai Hadas 
151231a78a5aSYishai 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",
1513b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1514b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
151531a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
151631a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1517b037c29aSEli Cohen 
1518b037c29aSEli Cohen 	return 0;
1519b037c29aSEli Cohen }
1520b037c29aSEli Cohen 
1521b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1522b037c29aSEli Cohen {
1523b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1524b037c29aSEli Cohen 	int err;
1525b037c29aSEli Cohen 	int i;
1526b037c29aSEli Cohen 
1527b037c29aSEli Cohen 	bfregi = &context->bfregi;
152831a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1529b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1530b037c29aSEli Cohen 		if (err)
1531b037c29aSEli Cohen 			goto error;
1532b037c29aSEli Cohen 
1533b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1534b037c29aSEli Cohen 	}
15354ed131d0SYishai Hadas 
15364ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
15374ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
15384ed131d0SYishai Hadas 
1539b037c29aSEli Cohen 	return 0;
1540b037c29aSEli Cohen 
1541b037c29aSEli Cohen error:
1542b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1543b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1544b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1545b037c29aSEli Cohen 
1546b037c29aSEli Cohen 	return err;
1547b037c29aSEli Cohen }
1548b037c29aSEli Cohen 
1549b037c29aSEli Cohen static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1550b037c29aSEli Cohen {
1551b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1552b037c29aSEli Cohen 	int err;
1553b037c29aSEli Cohen 	int i;
1554b037c29aSEli Cohen 
1555b037c29aSEli Cohen 	bfregi = &context->bfregi;
15564ed131d0SYishai Hadas 	for (i = 0; i < bfregi->num_sys_pages; i++) {
15574ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
15584ed131d0SYishai Hadas 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX) {
1559b037c29aSEli Cohen 			err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1560b037c29aSEli Cohen 			if (err) {
15614ed131d0SYishai Hadas 				mlx5_ib_warn(dev, "failed to free uar %d, err=%d\n", i, err);
1562b037c29aSEli Cohen 				return err;
1563b037c29aSEli Cohen 			}
1564b037c29aSEli Cohen 		}
15654ed131d0SYishai Hadas 	}
15664ed131d0SYishai Hadas 
1567b037c29aSEli Cohen 	return 0;
1568b037c29aSEli Cohen }
1569b037c29aSEli Cohen 
1570c85023e1SHuy Nguyen static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1571c85023e1SHuy Nguyen {
1572c85023e1SHuy Nguyen 	int err;
1573c85023e1SHuy Nguyen 
1574c85023e1SHuy Nguyen 	err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1575c85023e1SHuy Nguyen 	if (err)
1576c85023e1SHuy Nguyen 		return err;
1577c85023e1SHuy Nguyen 
1578c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
15798978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
15808978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1581c85023e1SHuy Nguyen 		return err;
1582c85023e1SHuy Nguyen 
1583c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1584c85023e1SHuy Nguyen 	dev->user_td++;
1585c85023e1SHuy Nguyen 
1586c85023e1SHuy Nguyen 	if (dev->user_td == 2)
1587c85023e1SHuy Nguyen 		err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1588c85023e1SHuy Nguyen 
1589c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1590c85023e1SHuy Nguyen 	return err;
1591c85023e1SHuy Nguyen }
1592c85023e1SHuy Nguyen 
1593c85023e1SHuy Nguyen static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1594c85023e1SHuy Nguyen {
1595c85023e1SHuy Nguyen 	mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1596c85023e1SHuy Nguyen 
1597c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
15988978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
15998978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1600c85023e1SHuy Nguyen 		return;
1601c85023e1SHuy Nguyen 
1602c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1603c85023e1SHuy Nguyen 	dev->user_td--;
1604c85023e1SHuy Nguyen 
1605c85023e1SHuy Nguyen 	if (dev->user_td < 2)
1606c85023e1SHuy Nguyen 		mlx5_nic_vport_update_local_lb(dev->mdev, false);
1607c85023e1SHuy Nguyen 
1608c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1609c85023e1SHuy Nguyen }
1610c85023e1SHuy Nguyen 
1611e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1612e126ba97SEli Cohen 						  struct ib_udata *udata)
1613e126ba97SEli Cohen {
1614e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1615b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1616b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
16175c99eaecSFeras Daoud 	struct mlx5_core_dev *mdev = dev->mdev;
1618e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
16192f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
162078c0f98cSEli Cohen 	int ver;
1621e126ba97SEli Cohen 	int err;
1622a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1623a168a41cSMajd Dibbiny 				     max_cqe_version);
1624b037c29aSEli Cohen 	bool lib_uar_4k;
1625e126ba97SEli Cohen 
1626e126ba97SEli Cohen 	if (!dev->ib_active)
1627e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1628e126ba97SEli Cohen 
1629e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
163078c0f98cSEli Cohen 		ver = 0;
1631e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
163278c0f98cSEli Cohen 		ver = 2;
163378c0f98cSEli Cohen 	else
163478c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
163578c0f98cSEli Cohen 
1636e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1637e126ba97SEli Cohen 	if (err)
1638e126ba97SEli Cohen 		return ERR_PTR(err);
1639e126ba97SEli Cohen 
1640b368d7cbSMatan Barak 	if (req.flags)
164178c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
164278c0f98cSEli Cohen 
1643f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1644b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1645b368d7cbSMatan Barak 
16462f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
16472f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
16482f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1649e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1650e126ba97SEli Cohen 
1651938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
16522cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1653938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1654b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1655938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1656938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1657938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1658938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1659938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1660f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1661f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1662f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
166330aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
166430aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
166530aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
166630aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1667b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1668b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1669e126ba97SEli Cohen 
1670e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1671e126ba97SEli Cohen 	if (!context)
1672e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1673e126ba97SEli Cohen 
167430aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
16752f5ff264SEli Cohen 	bfregi = &context->bfregi;
1676b037c29aSEli Cohen 
1677b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
167831a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1679b037c29aSEli Cohen 	if (err)
1680b037c29aSEli Cohen 		goto out_ctx;
1681b037c29aSEli Cohen 
16822f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1683b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
168431a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1685b037c29aSEli Cohen 				GFP_KERNEL);
1686b037c29aSEli Cohen 	if (!bfregi->count) {
1687e126ba97SEli Cohen 		err = -ENOMEM;
1688e126ba97SEli Cohen 		goto out_ctx;
1689e126ba97SEli Cohen 	}
1690e126ba97SEli Cohen 
1691b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1692b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1693e126ba97SEli Cohen 				    GFP_KERNEL);
1694b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1695e126ba97SEli Cohen 		err = -ENOMEM;
1696e126ba97SEli Cohen 		goto out_count;
1697e126ba97SEli Cohen 	}
1698e126ba97SEli Cohen 
1699b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1700b037c29aSEli Cohen 	if (err)
1701b037c29aSEli Cohen 		goto out_sys_pages;
1702b037c29aSEli Cohen 
1703b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1704b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1705b4cfe447SHaggai Eran #endif
1706b4cfe447SHaggai Eran 
1707146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1708c85023e1SHuy Nguyen 		err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
1709146d2f1aSmajd@mellanox.com 		if (err)
1710*c44ef998SIlya Lesokhin 			goto out_uars;
1711146d2f1aSmajd@mellanox.com 	}
1712146d2f1aSmajd@mellanox.com 
17137c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
1714ad9a3668SMajd Dibbiny 	mutex_init(&context->vma_private_list_mutex);
1715e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1716e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1717e126ba97SEli Cohen 
17182f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1719508562d6SDaniel Jurgens 	resp.num_ports = dev->num_ports;
1720b368d7cbSMatan Barak 
1721f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1722f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1723b368d7cbSMatan Barak 
1724402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
17256ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
17266ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1727402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1728402ca536SBodong Wang 	}
1729402ca536SBodong Wang 
173078984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
173178984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
173278984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
173378984898SOr Gerlitz 			resp.eth_min_inline++;
173478984898SOr Gerlitz 		}
173578984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
173678984898SOr Gerlitz 	}
173778984898SOr Gerlitz 
17385c99eaecSFeras Daoud 	if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
17395c99eaecSFeras Daoud 		if (mdev->clock_info)
17405c99eaecSFeras Daoud 			resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
17415c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.clock_info_versions);
17425c99eaecSFeras Daoud 	}
17435c99eaecSFeras Daoud 
1744bc5c6eedSNoa Osherovich 	/*
1745bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1746bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1747bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1748bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1749bc5c6eedSNoa Osherovich 	 */
1750de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1751de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1752b368d7cbSMatan Barak 			resp.comp_mask |=
1753b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1754b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1755de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1756de8d6e02SEli Cohen 		}
17575c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.hca_core_clock_offset);
1758b368d7cbSMatan Barak 	}
1759b368d7cbSMatan Barak 
176030aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
176130aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
176230aa60b3SEli Cohen 
176330aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
176430aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
176530aa60b3SEli Cohen 
176631a78a5aSYishai Hadas 	if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
176731a78a5aSYishai Hadas 		resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
176831a78a5aSYishai Hadas 		resp.response_length += sizeof(resp.num_dyn_bfregs);
176931a78a5aSYishai Hadas 	}
177031a78a5aSYishai Hadas 
1771b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1772e126ba97SEli Cohen 	if (err)
1773146d2f1aSmajd@mellanox.com 		goto out_td;
1774e126ba97SEli Cohen 
17752f5ff264SEli Cohen 	bfregi->ver = ver;
17762f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1777f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
177830aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
177930aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1780f72300c5SHaggai Abramovsky 
1781e126ba97SEli Cohen 	return &context->ibucontext;
1782e126ba97SEli Cohen 
1783146d2f1aSmajd@mellanox.com out_td:
1784146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1785c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1786146d2f1aSmajd@mellanox.com 
1787e126ba97SEli Cohen out_uars:
1788b037c29aSEli Cohen 	deallocate_uars(dev, context);
1789b037c29aSEli Cohen 
1790b037c29aSEli Cohen out_sys_pages:
1791b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1792b037c29aSEli Cohen 
1793e126ba97SEli Cohen out_count:
17942f5ff264SEli Cohen 	kfree(bfregi->count);
1795e126ba97SEli Cohen 
1796e126ba97SEli Cohen out_ctx:
1797e126ba97SEli Cohen 	kfree(context);
1798b037c29aSEli Cohen 
1799e126ba97SEli Cohen 	return ERR_PTR(err);
1800e126ba97SEli Cohen }
1801e126ba97SEli Cohen 
1802e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1803e126ba97SEli Cohen {
1804e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1805e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1806b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1807e126ba97SEli Cohen 
1808b037c29aSEli Cohen 	bfregi = &context->bfregi;
1809146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1810c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1811146d2f1aSmajd@mellanox.com 
1812b037c29aSEli Cohen 	deallocate_uars(dev, context);
1813b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
18142f5ff264SEli Cohen 	kfree(bfregi->count);
1815e126ba97SEli Cohen 	kfree(context);
1816e126ba97SEli Cohen 
1817e126ba97SEli Cohen 	return 0;
1818e126ba97SEli Cohen }
1819e126ba97SEli Cohen 
1820b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
18214ed131d0SYishai Hadas 				 int uar_idx)
1822e126ba97SEli Cohen {
1823b037c29aSEli Cohen 	int fw_uars_per_page;
1824b037c29aSEli Cohen 
1825b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1826b037c29aSEli Cohen 
18274ed131d0SYishai Hadas 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
1828e126ba97SEli Cohen }
1829e126ba97SEli Cohen 
1830e126ba97SEli Cohen static int get_command(unsigned long offset)
1831e126ba97SEli Cohen {
1832e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1833e126ba97SEli Cohen }
1834e126ba97SEli Cohen 
1835e126ba97SEli Cohen static int get_arg(unsigned long offset)
1836e126ba97SEli Cohen {
1837e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1838e126ba97SEli Cohen }
1839e126ba97SEli Cohen 
1840e126ba97SEli Cohen static int get_index(unsigned long offset)
1841e126ba97SEli Cohen {
1842e126ba97SEli Cohen 	return get_arg(offset);
1843e126ba97SEli Cohen }
1844e126ba97SEli Cohen 
18454ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
18464ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
18474ed131d0SYishai Hadas {
18484ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
18494ed131d0SYishai Hadas }
18504ed131d0SYishai Hadas 
18517c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
18527c2344c3SMaor Gottlieb {
18537c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
18547c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
18557c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
18567c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
18577c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
18587c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
18597c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
18607c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
18617c2344c3SMaor Gottlieb 	 */
18627c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
18637c2344c3SMaor Gottlieb }
18647c2344c3SMaor Gottlieb 
18657c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
18667c2344c3SMaor Gottlieb {
18677c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
18687c2344c3SMaor Gottlieb 
18697c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
18707c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
18717c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
18727c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
18737c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
18747c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
18757c2344c3SMaor Gottlieb 	 * process is exiting.
18767c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
18777c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
18787c2344c3SMaor Gottlieb 	 */
18797c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
18807c2344c3SMaor Gottlieb 
18817c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
18827c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
18837c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
18847c2344c3SMaor Gottlieb 	 */
18857c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
1886ad9a3668SMajd Dibbiny 	mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
18877c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
1888ad9a3668SMajd Dibbiny 	mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
18897c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
18907c2344c3SMaor Gottlieb }
18917c2344c3SMaor Gottlieb 
18927c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
18937c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
18947c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
18957c2344c3SMaor Gottlieb };
18967c2344c3SMaor Gottlieb 
18977c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
18987c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
18997c2344c3SMaor Gottlieb {
19007c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
19017c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
19027c2344c3SMaor Gottlieb 
19037c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
19047c2344c3SMaor Gottlieb 	if (!vma_prv)
19057c2344c3SMaor Gottlieb 		return -ENOMEM;
19067c2344c3SMaor Gottlieb 
19077c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
1908ad9a3668SMajd Dibbiny 	vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
19097c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
19107c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
19117c2344c3SMaor Gottlieb 
1912ad9a3668SMajd Dibbiny 	mutex_lock(&ctx->vma_private_list_mutex);
19137c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
1914ad9a3668SMajd Dibbiny 	mutex_unlock(&ctx->vma_private_list_mutex);
19157c2344c3SMaor Gottlieb 
19167c2344c3SMaor Gottlieb 	return 0;
19177c2344c3SMaor Gottlieb }
19187c2344c3SMaor Gottlieb 
19197c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
19207c2344c3SMaor Gottlieb {
19217c2344c3SMaor Gottlieb 	int ret;
19227c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
19237c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
19247c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
19257c2344c3SMaor Gottlieb 	struct task_struct *owning_process  = NULL;
19267c2344c3SMaor Gottlieb 	struct mm_struct   *owning_mm       = NULL;
19277c2344c3SMaor Gottlieb 
19287c2344c3SMaor Gottlieb 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
19297c2344c3SMaor Gottlieb 	if (!owning_process)
19307c2344c3SMaor Gottlieb 		return;
19317c2344c3SMaor Gottlieb 
19327c2344c3SMaor Gottlieb 	owning_mm = get_task_mm(owning_process);
19337c2344c3SMaor Gottlieb 	if (!owning_mm) {
19347c2344c3SMaor Gottlieb 		pr_info("no mm, disassociate ucontext is pending task termination\n");
19357c2344c3SMaor Gottlieb 		while (1) {
19367c2344c3SMaor Gottlieb 			put_task_struct(owning_process);
19377c2344c3SMaor Gottlieb 			usleep_range(1000, 2000);
19387c2344c3SMaor Gottlieb 			owning_process = get_pid_task(ibcontext->tgid,
19397c2344c3SMaor Gottlieb 						      PIDTYPE_PID);
19407c2344c3SMaor Gottlieb 			if (!owning_process ||
19417c2344c3SMaor Gottlieb 			    owning_process->state == TASK_DEAD) {
19427c2344c3SMaor Gottlieb 				pr_info("disassociate ucontext done, task was terminated\n");
19437c2344c3SMaor Gottlieb 				/* in case task was dead need to release the
19447c2344c3SMaor Gottlieb 				 * task struct.
19457c2344c3SMaor Gottlieb 				 */
19467c2344c3SMaor Gottlieb 				if (owning_process)
19477c2344c3SMaor Gottlieb 					put_task_struct(owning_process);
19487c2344c3SMaor Gottlieb 				return;
19497c2344c3SMaor Gottlieb 			}
19507c2344c3SMaor Gottlieb 		}
19517c2344c3SMaor Gottlieb 	}
19527c2344c3SMaor Gottlieb 
19537c2344c3SMaor Gottlieb 	/* need to protect from a race on closing the vma as part of
19547c2344c3SMaor Gottlieb 	 * mlx5_ib_vma_close.
19557c2344c3SMaor Gottlieb 	 */
1956ecc7d83bSMaor Gottlieb 	down_write(&owning_mm->mmap_sem);
1957ad9a3668SMajd Dibbiny 	mutex_lock(&context->vma_private_list_mutex);
19587c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
19597c2344c3SMaor Gottlieb 				 list) {
19607c2344c3SMaor Gottlieb 		vma = vma_private->vma;
19617c2344c3SMaor Gottlieb 		ret = zap_vma_ptes(vma, vma->vm_start,
19627c2344c3SMaor Gottlieb 				   PAGE_SIZE);
19637c2344c3SMaor Gottlieb 		WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
19647c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
19657c2344c3SMaor Gottlieb 		 * not access ops any more.
19667c2344c3SMaor Gottlieb 		 */
196713776612SMaor Gottlieb 		vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
19687c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
19697c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
19707c2344c3SMaor Gottlieb 		kfree(vma_private);
19717c2344c3SMaor Gottlieb 	}
1972ad9a3668SMajd Dibbiny 	mutex_unlock(&context->vma_private_list_mutex);
1973ecc7d83bSMaor Gottlieb 	up_write(&owning_mm->mmap_sem);
19747c2344c3SMaor Gottlieb 	mmput(owning_mm);
19757c2344c3SMaor Gottlieb 	put_task_struct(owning_process);
19767c2344c3SMaor Gottlieb }
19777c2344c3SMaor Gottlieb 
197837aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1979e126ba97SEli Cohen {
198037aa5c36SGuy Levi 	switch (cmd) {
198137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
198237aa5c36SGuy Levi 		return "WC";
1983e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
198437aa5c36SGuy Levi 		return "best effort WC";
198537aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
198637aa5c36SGuy Levi 		return "NC";
198737aa5c36SGuy Levi 	default:
198837aa5c36SGuy Levi 		return NULL;
198937aa5c36SGuy Levi 	}
199037aa5c36SGuy Levi }
199137aa5c36SGuy Levi 
19925c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
19935c99eaecSFeras Daoud 					struct vm_area_struct *vma,
19945c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
19955c99eaecSFeras Daoud {
19965c99eaecSFeras Daoud 	phys_addr_t pfn;
19975c99eaecSFeras Daoud 	int err;
19985c99eaecSFeras Daoud 
19995c99eaecSFeras Daoud 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
20005c99eaecSFeras Daoud 		return -EINVAL;
20015c99eaecSFeras Daoud 
20025c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20035c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20045c99eaecSFeras Daoud 
20055c99eaecSFeras Daoud 	if (vma->vm_flags & VM_WRITE)
20065c99eaecSFeras Daoud 		return -EPERM;
20075c99eaecSFeras Daoud 
20085c99eaecSFeras Daoud 	if (!dev->mdev->clock_info_page)
20095c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20105c99eaecSFeras Daoud 
20115c99eaecSFeras Daoud 	pfn = page_to_pfn(dev->mdev->clock_info_page);
20125c99eaecSFeras Daoud 	err = remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE,
20135c99eaecSFeras Daoud 			      vma->vm_page_prot);
20145c99eaecSFeras Daoud 	if (err)
20155c99eaecSFeras Daoud 		return err;
20165c99eaecSFeras Daoud 
20175c99eaecSFeras Daoud 	mlx5_ib_dbg(dev, "mapped clock info at 0x%lx, PA 0x%llx\n",
20185c99eaecSFeras Daoud 		    vma->vm_start,
20195c99eaecSFeras Daoud 		    (unsigned long long)pfn << PAGE_SHIFT);
20205c99eaecSFeras Daoud 
20215c99eaecSFeras Daoud 	return mlx5_ib_set_vma_data(vma, context);
20225c99eaecSFeras Daoud }
20235c99eaecSFeras Daoud 
202437aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
20257c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
20267c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
202737aa5c36SGuy Levi {
20282f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
202937aa5c36SGuy Levi 	int err;
203037aa5c36SGuy Levi 	unsigned long idx;
203137aa5c36SGuy Levi 	phys_addr_t pfn, pa;
203237aa5c36SGuy Levi 	pgprot_t prot;
20334ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
20344ed131d0SYishai Hadas 	u32 uar_index;
20354ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
20364ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
20374ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2038b037c29aSEli Cohen 
2039b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2040b037c29aSEli Cohen 		return -EINVAL;
2041b037c29aSEli Cohen 
20424ed131d0SYishai Hadas 	if (dyn_uar)
20434ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
20444ed131d0SYishai Hadas 	else
2045b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
20464ed131d0SYishai Hadas 
20474ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
20484ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
20494ed131d0SYishai Hadas 			     idx, max_valid_idx);
2050b037c29aSEli Cohen 		return -EINVAL;
2051b037c29aSEli Cohen 	}
205237aa5c36SGuy Levi 
205337aa5c36SGuy Levi 	switch (cmd) {
205437aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
20554ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
205637aa5c36SGuy Levi /* Some architectures don't support WC memory */
205737aa5c36SGuy Levi #if defined(CONFIG_X86)
205837aa5c36SGuy Levi 		if (!pat_enabled())
205937aa5c36SGuy Levi 			return -EPERM;
206037aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
206137aa5c36SGuy Levi 			return -EPERM;
206237aa5c36SGuy Levi #endif
206337aa5c36SGuy Levi 	/* fall through */
206437aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
206537aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
206637aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
206737aa5c36SGuy Levi 		break;
206837aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
206937aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
207037aa5c36SGuy Levi 		break;
207137aa5c36SGuy Levi 	default:
207237aa5c36SGuy Levi 		return -EINVAL;
207337aa5c36SGuy Levi 	}
207437aa5c36SGuy Levi 
20754ed131d0SYishai Hadas 	if (dyn_uar) {
20764ed131d0SYishai Hadas 		int uars_per_page;
20774ed131d0SYishai Hadas 
20784ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
20794ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
20804ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
20814ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
20824ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
20834ed131d0SYishai Hadas 			return -EINVAL;
20844ed131d0SYishai Hadas 		}
20854ed131d0SYishai Hadas 
20864ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
20874ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
20884ed131d0SYishai Hadas 		 * page holds its count.
20894ed131d0SYishai Hadas 		 */
20904ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
20914ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
20924ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
20934ed131d0SYishai Hadas 			return -EINVAL;
20944ed131d0SYishai Hadas 		}
20954ed131d0SYishai Hadas 
20964ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
20974ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
20984ed131d0SYishai Hadas 
20994ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
21004ed131d0SYishai Hadas 		if (err) {
21014ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
21024ed131d0SYishai Hadas 			goto free_bfreg;
21034ed131d0SYishai Hadas 		}
21044ed131d0SYishai Hadas 	} else {
21054ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
21064ed131d0SYishai Hadas 	}
21074ed131d0SYishai Hadas 
21084ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
210937aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2110e126ba97SEli Cohen 
211137aa5c36SGuy Levi 	vma->vm_page_prot = prot;
211237aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
211337aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
211437aa5c36SGuy Levi 	if (err) {
211537aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
211637aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
21174ed131d0SYishai Hadas 		err = -EAGAIN;
21184ed131d0SYishai Hadas 		goto err;
211937aa5c36SGuy Levi 	}
2120e126ba97SEli Cohen 
212137aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
212237aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
212337aa5c36SGuy Levi 		    vma->vm_start, &pa);
212437aa5c36SGuy Levi 
21254ed131d0SYishai Hadas 	err = mlx5_ib_set_vma_data(vma, context);
21264ed131d0SYishai Hadas 	if (err)
21274ed131d0SYishai Hadas 		goto err;
21284ed131d0SYishai Hadas 
21294ed131d0SYishai Hadas 	if (dyn_uar)
21304ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
21314ed131d0SYishai Hadas 	return 0;
21324ed131d0SYishai Hadas 
21334ed131d0SYishai Hadas err:
21344ed131d0SYishai Hadas 	if (!dyn_uar)
21354ed131d0SYishai Hadas 		return err;
21364ed131d0SYishai Hadas 
21374ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
21384ed131d0SYishai Hadas 
21394ed131d0SYishai Hadas free_bfreg:
21404ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
21414ed131d0SYishai Hadas 
21424ed131d0SYishai Hadas 	return err;
214337aa5c36SGuy Levi }
214437aa5c36SGuy Levi 
214537aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
214637aa5c36SGuy Levi {
214737aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
214837aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
214937aa5c36SGuy Levi 	unsigned long command;
215037aa5c36SGuy Levi 	phys_addr_t pfn;
215137aa5c36SGuy Levi 
215237aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
215337aa5c36SGuy Levi 	switch (command) {
215437aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
215537aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
215637aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
21574ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
21587c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2159e126ba97SEli Cohen 
2160e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2161e126ba97SEli Cohen 		return -ENOSYS;
2162e126ba97SEli Cohen 
2163d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2164d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2165d69e3bcfSMatan Barak 			return -EINVAL;
2166d69e3bcfSMatan Barak 
21676cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2168d69e3bcfSMatan Barak 			return -EPERM;
2169d69e3bcfSMatan Barak 
2170d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2171d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2172d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2173d69e3bcfSMatan Barak 
2174d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2175d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2176d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2177d69e3bcfSMatan Barak 			PAGE_SHIFT;
2178d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2179d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
2180d69e3bcfSMatan Barak 			return -EAGAIN;
2181d69e3bcfSMatan Barak 
2182d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
2183d69e3bcfSMatan Barak 			    vma->vm_start,
2184d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
2185d69e3bcfSMatan Barak 		break;
21865c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
21875c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2188d69e3bcfSMatan Barak 
2189e126ba97SEli Cohen 	default:
2190e126ba97SEli Cohen 		return -EINVAL;
2191e126ba97SEli Cohen 	}
2192e126ba97SEli Cohen 
2193e126ba97SEli Cohen 	return 0;
2194e126ba97SEli Cohen }
2195e126ba97SEli Cohen 
2196e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
2197e126ba97SEli Cohen 				      struct ib_ucontext *context,
2198e126ba97SEli Cohen 				      struct ib_udata *udata)
2199e126ba97SEli Cohen {
2200e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2201e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
2202e126ba97SEli Cohen 	int err;
2203e126ba97SEli Cohen 
2204e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
2205e126ba97SEli Cohen 	if (!pd)
2206e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
2207e126ba97SEli Cohen 
22089603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
2209e126ba97SEli Cohen 	if (err) {
2210e126ba97SEli Cohen 		kfree(pd);
2211e126ba97SEli Cohen 		return ERR_PTR(err);
2212e126ba97SEli Cohen 	}
2213e126ba97SEli Cohen 
2214e126ba97SEli Cohen 	if (context) {
2215e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2216e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
22179603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
2218e126ba97SEli Cohen 			kfree(pd);
2219e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
2220e126ba97SEli Cohen 		}
2221e126ba97SEli Cohen 	}
2222e126ba97SEli Cohen 
2223e126ba97SEli Cohen 	return &pd->ibpd;
2224e126ba97SEli Cohen }
2225e126ba97SEli Cohen 
2226e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
2227e126ba97SEli Cohen {
2228e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2229e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2230e126ba97SEli Cohen 
22319603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
2232e126ba97SEli Cohen 	kfree(mpd);
2233e126ba97SEli Cohen 
2234e126ba97SEli Cohen 	return 0;
2235e126ba97SEli Cohen }
2236e126ba97SEli Cohen 
2237466fa6d2SMaor Gottlieb enum {
2238466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2239466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
2240466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_INNER_BIT
2241466fa6d2SMaor Gottlieb };
2242038d2ef8SMaor Gottlieb 
2243466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2244466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2245466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2246466fa6d2SMaor Gottlieb 
2247466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2248466fa6d2SMaor Gottlieb {
2249466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2250466fa6d2SMaor Gottlieb 
2251466fa6d2SMaor Gottlieb 	match_criteria_enable =
2252466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2253466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2254466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2255466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2256466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2257466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2258466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2259466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
2260466fa6d2SMaor Gottlieb 
2261466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2262038d2ef8SMaor Gottlieb }
2263038d2ef8SMaor Gottlieb 
2264ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2265ca0d4753SMaor Gottlieb {
2266ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2267ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2268ca0d4753SMaor Gottlieb }
2269ca0d4753SMaor Gottlieb 
22702d1e697eSMoses Reuben static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
22712d1e697eSMoses Reuben 			   bool inner)
22722d1e697eSMoses Reuben {
22732d1e697eSMoses Reuben 	if (inner) {
22742d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
22752d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
22762d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
22772d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
22782d1e697eSMoses Reuben 	} else {
22792d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
22802d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
22812d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
22822d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
22832d1e697eSMoses Reuben 	}
22842d1e697eSMoses Reuben }
22852d1e697eSMoses Reuben 
2286ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2287ca0d4753SMaor Gottlieb {
2288ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2289ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2290ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2291ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2292ca0d4753SMaor Gottlieb }
2293ca0d4753SMaor Gottlieb 
2294c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2295c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2296ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2297466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2298c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2299ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
23002ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2301a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
2302c47ac6aeSMaor Gottlieb 
2303c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2304c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2305c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2306c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2307c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2308c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2309c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2310c47ac6aeSMaor Gottlieb 
231119cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
231219cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
2313075572d4SBoris Pismenny 			   struct mlx5_flow_act *action)
2314038d2ef8SMaor Gottlieb {
2315466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2316466fa6d2SMaor Gottlieb 					   misc_parameters);
2317466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2318466fa6d2SMaor Gottlieb 					   misc_parameters);
23192d1e697eSMoses Reuben 	void *headers_c;
23202d1e697eSMoses Reuben 	void *headers_v;
232119cc7524SAriel Levkovich 	int match_ipv;
2322466fa6d2SMaor Gottlieb 
23232d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
23242d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
23252d1e697eSMoses Reuben 					 inner_headers);
23262d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
23272d1e697eSMoses Reuben 					 inner_headers);
232819cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
232919cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
23302d1e697eSMoses Reuben 	} else {
23312d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
23322d1e697eSMoses Reuben 					 outer_headers);
23332d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
23342d1e697eSMoses Reuben 					 outer_headers);
233519cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
233619cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
23372d1e697eSMoses Reuben 	}
23382d1e697eSMoses Reuben 
23392d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2340038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2341c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
23421ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2343038d2ef8SMaor Gottlieb 
23442d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2345038d2ef8SMaor Gottlieb 					     dmac_47_16),
2346038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
23472d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2348038d2ef8SMaor Gottlieb 					     dmac_47_16),
2349038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2350038d2ef8SMaor Gottlieb 
23512d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2352ee3da804SMaor Gottlieb 					     smac_47_16),
2353ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
23542d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2355ee3da804SMaor Gottlieb 					     smac_47_16),
2356ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2357ee3da804SMaor Gottlieb 
2358038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
23592d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
236010543365SMohamad Haj Yahia 				 cvlan_tag, 1);
23612d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
236210543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2363038d2ef8SMaor Gottlieb 
23642d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2365038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
23662d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2367038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2368038d2ef8SMaor Gottlieb 
23692d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2370038d2ef8SMaor Gottlieb 				 first_cfi,
2371038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
23722d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2373038d2ef8SMaor Gottlieb 				 first_cfi,
2374038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2375038d2ef8SMaor Gottlieb 
23762d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2377038d2ef8SMaor Gottlieb 				 first_prio,
2378038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
23792d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2380038d2ef8SMaor Gottlieb 				 first_prio,
2381038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2382038d2ef8SMaor Gottlieb 		}
23832d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2384038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
23852d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2386038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2387038d2ef8SMaor Gottlieb 		break;
2388038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2389c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
23901ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2391038d2ef8SMaor Gottlieb 
239219cc7524SAriel Levkovich 		if (match_ipv) {
239319cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
239419cc7524SAriel Levkovich 				 ip_version, 0xf);
239519cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
23963346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV4_VERSION);
239719cc7524SAriel Levkovich 		} else {
23982d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2399038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
24002d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2401038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
240219cc7524SAriel Levkovich 		}
2403038d2ef8SMaor Gottlieb 
24042d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2405038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2406038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2407038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
24082d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2409038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2410038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2411038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
24122d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2413038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2414038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2415038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
24162d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2417038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2418038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2419038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2420ca0d4753SMaor Gottlieb 
24212d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2422ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2423ca0d4753SMaor Gottlieb 
24242d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2425ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
2426038d2ef8SMaor Gottlieb 		break;
2427026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2428c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
24291ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2430026bae0cSMaor Gottlieb 
243119cc7524SAriel Levkovich 		if (match_ipv) {
243219cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
243319cc7524SAriel Levkovich 				 ip_version, 0xf);
243419cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
24353346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV6_VERSION);
243619cc7524SAriel Levkovich 		} else {
24372d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2438026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
24392d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2440026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
244119cc7524SAriel Levkovich 		}
2442026bae0cSMaor Gottlieb 
24432d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2444026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2445026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2446026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
24472d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2448026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2449026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2450026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
24512d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2452026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2453026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2454026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
24552d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2456026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2457026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2458026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2459466fa6d2SMaor Gottlieb 
24602d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2461466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2462466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2463466fa6d2SMaor Gottlieb 
24642d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2465466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
2466466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
2467466fa6d2SMaor Gottlieb 
24682d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
24692d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
24702d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
24712d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
24722d1e697eSMoses Reuben 
2473026bae0cSMaor Gottlieb 		break;
2474038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2475c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2476c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
24771ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2478038d2ef8SMaor Gottlieb 
24792d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2480038d2ef8SMaor Gottlieb 			 0xff);
24812d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2482038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
2483038d2ef8SMaor Gottlieb 
24842d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2485038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
24862d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2487038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2488038d2ef8SMaor Gottlieb 
24892d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2490038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
24912d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2492038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2493038d2ef8SMaor Gottlieb 		break;
2494038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2495c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2496c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
24971ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2498038d2ef8SMaor Gottlieb 
24992d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2500038d2ef8SMaor Gottlieb 			 0xff);
25012d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2502038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
2503038d2ef8SMaor Gottlieb 
25042d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2505038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
25062d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2507038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2508038d2ef8SMaor Gottlieb 
25092d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2510038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
25112d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2512038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2513038d2ef8SMaor Gottlieb 		break;
2514ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2515ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2516ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
25171ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2518ffb30d8fSMoses Reuben 
2519ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2520ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2521ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2522ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2523ffb30d8fSMoses Reuben 		break;
25242ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
25252ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
25262ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
25272ac693f9SMoses Reuben 			return -EOPNOTSUPP;
25282ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
25292ac693f9SMoses Reuben 			return -EINVAL;
25302ac693f9SMoses Reuben 
2531075572d4SBoris Pismenny 		action->flow_tag = ib_spec->flow_tag.tag_id;
2532a9db0ecfSMatan Barak 		action->has_flow_tag = true;
25332ac693f9SMoses Reuben 		break;
2534a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2535a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2536a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2537a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2538075572d4SBoris Pismenny 		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
2539a22ed86cSSlava Shwartsman 		break;
2540038d2ef8SMaor Gottlieb 	default:
2541038d2ef8SMaor Gottlieb 		return -EINVAL;
2542038d2ef8SMaor Gottlieb 	}
2543038d2ef8SMaor Gottlieb 
2544038d2ef8SMaor Gottlieb 	return 0;
2545038d2ef8SMaor Gottlieb }
2546038d2ef8SMaor Gottlieb 
2547038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
2548038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
2549038d2ef8SMaor Gottlieb  * could steal other multicast packets.
2550038d2ef8SMaor Gottlieb  */
2551a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2552038d2ef8SMaor Gottlieb {
255381e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
2554038d2ef8SMaor Gottlieb 
2555038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2556038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
2557038d2ef8SMaor Gottlieb 		return false;
2558038d2ef8SMaor Gottlieb 
255981e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
256081e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
256181e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
2562038d2ef8SMaor Gottlieb 
256381e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
256481e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
256581e30880SYishai Hadas 			return true;
256681e30880SYishai Hadas 
256781e30880SYishai Hadas 		return false;
256881e30880SYishai Hadas 	}
256981e30880SYishai Hadas 
257081e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
257181e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
257281e30880SYishai Hadas 
257381e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2574038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2575038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
2576038d2ef8SMaor Gottlieb 	}
2577038d2ef8SMaor Gottlieb 
257881e30880SYishai Hadas 	return false;
257981e30880SYishai Hadas }
258081e30880SYishai Hadas 
258119cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
258219cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
25830f750966SAriel Levkovich 			       bool check_inner)
2584038d2ef8SMaor Gottlieb {
2585038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
258619cc7524SAriel Levkovich 	int match_ipv = check_inner ?
258719cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
258819cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
258919cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
259019cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
25910f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
25920f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
25930f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
25940f750966SAriel Levkovich 	bool has_ethertype = false;
2595038d2ef8SMaor Gottlieb 	unsigned int spec_index;
25960f750966SAriel Levkovich 	bool mask_valid = true;
25970f750966SAriel Levkovich 	u16 eth_type = 0;
25980f750966SAriel Levkovich 	bool type_valid;
2599038d2ef8SMaor Gottlieb 
2600038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
2601038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
26020f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2603038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
26040f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
26050f750966SAriel Levkovich 				      htons(0xffff));
26060f750966SAriel Levkovich 			has_ethertype = true;
26070f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
26080f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
26090f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
26100f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
2611038d2ef8SMaor Gottlieb 		}
2612038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
2613038d2ef8SMaor Gottlieb 	}
26140f750966SAriel Levkovich 
26150f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
26160f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
26170f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
26180f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
26190f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
26200f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
262119cc7524SAriel Levkovich 
262219cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
262319cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
262419cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
26250f750966SAriel Levkovich 	}
26260f750966SAriel Levkovich 
26270f750966SAriel Levkovich 	return type_valid;
26280f750966SAriel Levkovich }
26290f750966SAriel Levkovich 
263019cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
263119cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
26320f750966SAriel Levkovich {
263319cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
263419cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
2635038d2ef8SMaor Gottlieb }
2636038d2ef8SMaor Gottlieb 
2637038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
2638038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
2639038d2ef8SMaor Gottlieb {
2640038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
2641038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
2642038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
2643038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
2644038d2ef8SMaor Gottlieb 	}
2645038d2ef8SMaor Gottlieb }
2646038d2ef8SMaor Gottlieb 
2647038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2648038d2ef8SMaor Gottlieb {
2649038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2650038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2651038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2652038d2ef8SMaor Gottlieb 							  ibflow);
2653038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2654038d2ef8SMaor Gottlieb 
26559a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
2656038d2ef8SMaor Gottlieb 
2657038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
265874491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2659cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
2660038d2ef8SMaor Gottlieb 		list_del(&iter->list);
2661038d2ef8SMaor Gottlieb 		kfree(iter);
2662038d2ef8SMaor Gottlieb 	}
2663038d2ef8SMaor Gottlieb 
266474491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
26655497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
26669a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
2667038d2ef8SMaor Gottlieb 
2668038d2ef8SMaor Gottlieb 	kfree(handler);
2669038d2ef8SMaor Gottlieb 
2670038d2ef8SMaor Gottlieb 	return 0;
2671038d2ef8SMaor Gottlieb }
2672038d2ef8SMaor Gottlieb 
267335d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
267435d19011SMaor Gottlieb {
267535d19011SMaor Gottlieb 	priority *= 2;
267635d19011SMaor Gottlieb 	if (!dont_trap)
267735d19011SMaor Gottlieb 		priority++;
267835d19011SMaor Gottlieb 	return priority;
267935d19011SMaor Gottlieb }
268035d19011SMaor Gottlieb 
2681cc0e5d42SMaor Gottlieb enum flow_table_type {
2682cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
2683cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
2684cc0e5d42SMaor Gottlieb };
2685cc0e5d42SMaor Gottlieb 
268600b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
268700b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
2688038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
2689cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
2690cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
2691038d2ef8SMaor Gottlieb {
269235d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
2693038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
2694038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
2695038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
2696dac388efSMaor Gottlieb 	int max_table_size;
2697038d2ef8SMaor Gottlieb 	int num_entries;
2698038d2ef8SMaor Gottlieb 	int num_groups;
2699038d2ef8SMaor Gottlieb 	int priority;
2700038d2ef8SMaor Gottlieb 	int err = 0;
2701038d2ef8SMaor Gottlieb 
2702dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2703dac388efSMaor Gottlieb 						       log_max_ft_size));
2704038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
270535d19011SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr) &&
270635d19011SMaor Gottlieb 		    !dont_trap)
2707038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
2708038d2ef8SMaor Gottlieb 		else
270935d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
271035d19011SMaor Gottlieb 							dont_trap);
2711038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2712038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
2713038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
2714038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
27159a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[priority];
2716038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2717038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2718038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2719038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
2720038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
2721038d2ef8SMaor Gottlieb 					 &num_entries,
2722038d2ef8SMaor Gottlieb 					 &num_groups);
27239a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
2724cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2725cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2726cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
2727cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
2728cc0e5d42SMaor Gottlieb 
2729cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2730cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2731cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2732cc0e5d42SMaor Gottlieb 
27339a4ca38dSMark Bloch 		prio = &dev->flow_db->sniffer[ft_type];
2734cc0e5d42SMaor Gottlieb 		priority = 0;
2735cc0e5d42SMaor Gottlieb 		num_entries = 1;
2736cc0e5d42SMaor Gottlieb 		num_groups = 1;
2737038d2ef8SMaor Gottlieb 	}
2738038d2ef8SMaor Gottlieb 
2739038d2ef8SMaor Gottlieb 	if (!ns)
2740038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
2741038d2ef8SMaor Gottlieb 
2742dac388efSMaor Gottlieb 	if (num_entries > max_table_size)
2743dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2744dac388efSMaor Gottlieb 
2745038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
2746038d2ef8SMaor Gottlieb 	if (!ft) {
2747038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2748038d2ef8SMaor Gottlieb 							 num_entries,
2749d63cd286SMaor Gottlieb 							 num_groups,
2750c9f1b073SHadar Hen Zion 							 0, 0);
2751038d2ef8SMaor Gottlieb 
2752038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
2753038d2ef8SMaor Gottlieb 			prio->refcount = 0;
2754038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
2755038d2ef8SMaor Gottlieb 		} else {
2756038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
2757038d2ef8SMaor Gottlieb 		}
2758038d2ef8SMaor Gottlieb 	}
2759038d2ef8SMaor Gottlieb 
2760038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
2761038d2ef8SMaor Gottlieb }
2762038d2ef8SMaor Gottlieb 
2763a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
2764a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
2765a550ddfcSYishai Hadas 			    u32 underlay_qpn)
2766a550ddfcSYishai Hadas {
2767a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2768a550ddfcSYishai Hadas 					   spec->match_criteria,
2769a550ddfcSYishai Hadas 					   misc_parameters);
2770a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2771a550ddfcSYishai Hadas 					   misc_parameters);
2772a550ddfcSYishai Hadas 
2773a550ddfcSYishai Hadas 	if (underlay_qpn &&
2774a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2775a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
2776a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
2777a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
2778a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
2779a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
2780a550ddfcSYishai Hadas 	}
2781a550ddfcSYishai Hadas }
2782a550ddfcSYishai Hadas 
2783a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2784038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
2785dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
2786a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
2787a550ddfcSYishai Hadas 						      u32 underlay_qpn)
2788038d2ef8SMaor Gottlieb {
2789038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
2790038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
2791075572d4SBoris Pismenny 	struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
2792c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
2793a22ed86cSSlava Shwartsman 	struct mlx5_flow_destination *rule_dst = dst;
2794dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
2795038d2ef8SMaor Gottlieb 	unsigned int spec_index;
2796038d2ef8SMaor Gottlieb 	int err = 0;
2797a22ed86cSSlava Shwartsman 	int dest_num = 1;
2798038d2ef8SMaor Gottlieb 
279919cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
2800038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2801038d2ef8SMaor Gottlieb 
28021b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
2803038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
2804c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
2805038d2ef8SMaor Gottlieb 		err = -ENOMEM;
2806038d2ef8SMaor Gottlieb 		goto free;
2807038d2ef8SMaor Gottlieb 	}
2808038d2ef8SMaor Gottlieb 
2809038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
2810038d2ef8SMaor Gottlieb 
2811038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
281219cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
2813a22ed86cSSlava Shwartsman 				      spec->match_value,
2814075572d4SBoris Pismenny 				      ib_flow, &flow_act);
2815038d2ef8SMaor Gottlieb 		if (err < 0)
2816038d2ef8SMaor Gottlieb 			goto free;
2817038d2ef8SMaor Gottlieb 
2818038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2819038d2ef8SMaor Gottlieb 	}
2820038d2ef8SMaor Gottlieb 
2821a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
2822a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
2823a550ddfcSYishai Hadas 
2824018a94eeSMark Bloch 	if (dev->rep) {
2825018a94eeSMark Bloch 		void *misc;
2826018a94eeSMark Bloch 
2827018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2828018a94eeSMark Bloch 				    misc_parameters);
2829018a94eeSMark Bloch 		MLX5_SET(fte_match_set_misc, misc, source_port,
2830018a94eeSMark Bloch 			 dev->rep->vport);
2831018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
2832018a94eeSMark Bloch 				    misc_parameters);
2833018a94eeSMark Bloch 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
2834018a94eeSMark Bloch 	}
2835018a94eeSMark Bloch 
2836466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
2837075572d4SBoris Pismenny 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
2838a22ed86cSSlava Shwartsman 		rule_dst = NULL;
2839a22ed86cSSlava Shwartsman 		dest_num = 0;
2840a22ed86cSSlava Shwartsman 	} else {
284166958ed9SHadar Hen Zion 		flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
284235d19011SMaor Gottlieb 		    MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2843a22ed86cSSlava Shwartsman 	}
28442ac693f9SMoses Reuben 
2845a9db0ecfSMatan Barak 	if (flow_act.has_flow_tag &&
28462ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
28472ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
28482ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
2849075572d4SBoris Pismenny 			     flow_act.flow_tag, flow_attr->type);
28502ac693f9SMoses Reuben 		err = -EINVAL;
28512ac693f9SMoses Reuben 		goto free;
28522ac693f9SMoses Reuben 	}
285374491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
285466958ed9SHadar Hen Zion 					    &flow_act,
2855a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
2856038d2ef8SMaor Gottlieb 
2857038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
2858038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
2859038d2ef8SMaor Gottlieb 		goto free;
2860038d2ef8SMaor Gottlieb 	}
2861038d2ef8SMaor Gottlieb 
2862d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
28635497adc6SMaor Gottlieb 	handler->prio = ft_prio;
2864038d2ef8SMaor Gottlieb 
2865038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
2866038d2ef8SMaor Gottlieb free:
2867038d2ef8SMaor Gottlieb 	if (err)
2868038d2ef8SMaor Gottlieb 		kfree(handler);
2869c5bb1730SMaor Gottlieb 	kvfree(spec);
2870038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
2871038d2ef8SMaor Gottlieb }
2872038d2ef8SMaor Gottlieb 
2873a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2874a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
2875a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
2876a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
2877a550ddfcSYishai Hadas {
2878a550ddfcSYishai Hadas 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2879a550ddfcSYishai Hadas }
2880a550ddfcSYishai Hadas 
288135d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
288235d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
288335d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
288435d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
288535d19011SMaor Gottlieb {
288635d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
288735d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
288835d19011SMaor Gottlieb 
288935d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
289035d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
289135d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
289235d19011SMaor Gottlieb 					       flow_attr, dst);
289335d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
289474491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2895d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
289635d19011SMaor Gottlieb 			kfree(handler);
289735d19011SMaor Gottlieb 			handler = handler_dst;
289835d19011SMaor Gottlieb 		} else {
289935d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
290035d19011SMaor Gottlieb 		}
290135d19011SMaor Gottlieb 	}
290235d19011SMaor Gottlieb 
290335d19011SMaor Gottlieb 	return handler;
290435d19011SMaor Gottlieb }
2905038d2ef8SMaor Gottlieb enum {
2906038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
2907038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
2908038d2ef8SMaor Gottlieb };
2909038d2ef8SMaor Gottlieb 
2910038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2911038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
2912038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
2913038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
2914038d2ef8SMaor Gottlieb {
2915038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
2916038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2917038d2ef8SMaor Gottlieb 
2918038d2ef8SMaor Gottlieb 	static struct {
2919038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
2920038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
2921038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
2922038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
2923038d2ef8SMaor Gottlieb 			.flow_attr = {
2924038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2925038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2926038d2ef8SMaor Gottlieb 			},
2927038d2ef8SMaor Gottlieb 			.eth_flow = {
2928038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2929038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2930038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2931038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
2932038d2ef8SMaor Gottlieb 			}
2933038d2ef8SMaor Gottlieb 		},
2934038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
2935038d2ef8SMaor Gottlieb 			.flow_attr = {
2936038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2937038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2938038d2ef8SMaor Gottlieb 			},
2939038d2ef8SMaor Gottlieb 			.eth_flow = {
2940038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2941038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2942038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2943038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
2944038d2ef8SMaor Gottlieb 			}
2945038d2ef8SMaor Gottlieb 		}
2946038d2ef8SMaor Gottlieb 	};
2947038d2ef8SMaor Gottlieb 
2948038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
2949038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
2950038d2ef8SMaor Gottlieb 				   dst);
2951038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
2952038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2953038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
2954038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2955038d2ef8SMaor Gottlieb 						 dst);
2956038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
295774491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2958d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
2959038d2ef8SMaor Gottlieb 			kfree(handler);
2960038d2ef8SMaor Gottlieb 			handler = handler_ucast;
2961038d2ef8SMaor Gottlieb 		} else {
2962038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
2963038d2ef8SMaor Gottlieb 		}
2964038d2ef8SMaor Gottlieb 	}
2965038d2ef8SMaor Gottlieb 
2966038d2ef8SMaor Gottlieb 	return handler;
2967038d2ef8SMaor Gottlieb }
2968038d2ef8SMaor Gottlieb 
2969cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2970cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
2971cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
2972cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
2973cc0e5d42SMaor Gottlieb {
2974cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
2975cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
2976cc0e5d42SMaor Gottlieb 	int err;
2977cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
2978cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
2979cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
2980cc0e5d42SMaor Gottlieb 	};
2981cc0e5d42SMaor Gottlieb 
2982cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2983cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
2984cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
2985cc0e5d42SMaor Gottlieb 		goto err;
2986cc0e5d42SMaor Gottlieb 	}
2987cc0e5d42SMaor Gottlieb 
2988cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2989cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
2990cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
2991cc0e5d42SMaor Gottlieb 		goto err_tx;
2992cc0e5d42SMaor Gottlieb 	}
2993cc0e5d42SMaor Gottlieb 
2994cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
2995cc0e5d42SMaor Gottlieb 
2996cc0e5d42SMaor Gottlieb 	return handler_rx;
2997cc0e5d42SMaor Gottlieb 
2998cc0e5d42SMaor Gottlieb err_tx:
299974491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
3000cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
3001cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
3002cc0e5d42SMaor Gottlieb err:
3003cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
3004cc0e5d42SMaor Gottlieb }
3005cc0e5d42SMaor Gottlieb 
3006038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3007038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
3008038d2ef8SMaor Gottlieb 					   int domain)
3009038d2ef8SMaor Gottlieb {
3010038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3011d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
3012038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3013038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
3014cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3015038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
3016038d2ef8SMaor Gottlieb 	int err;
3017a550ddfcSYishai Hadas 	int underlay_qpn;
3018038d2ef8SMaor Gottlieb 
3019038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
3020dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
3021038d2ef8SMaor Gottlieb 
3022038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
3023508562d6SDaniel Jurgens 	    flow_attr->port > dev->num_ports ||
302435d19011SMaor Gottlieb 	    (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
3025038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3026038d2ef8SMaor Gottlieb 
3027038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3028038d2ef8SMaor Gottlieb 	if (!dst)
3029038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
3030038d2ef8SMaor Gottlieb 
30319a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3032038d2ef8SMaor Gottlieb 
3033cc0e5d42SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
3034038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
3035038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
3036038d2ef8SMaor Gottlieb 		goto unlock;
3037038d2ef8SMaor Gottlieb 	}
3038cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3039cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3040cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
3041cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
3042cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
3043cc0e5d42SMaor Gottlieb 			goto destroy_ft;
3044cc0e5d42SMaor Gottlieb 		}
3045cc0e5d42SMaor Gottlieb 	}
3046038d2ef8SMaor Gottlieb 
3047038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3048d9f88e5aSYishai Hadas 	if (mqp->flags & MLX5_IB_QP_RSS)
3049d9f88e5aSYishai Hadas 		dst->tir_num = mqp->rss_qp.tirn;
3050d9f88e5aSYishai Hadas 	else
3051d9f88e5aSYishai Hadas 		dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3052038d2ef8SMaor Gottlieb 
3053038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
305435d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
305535d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
305635d19011SMaor Gottlieb 							flow_attr, dst);
305735d19011SMaor Gottlieb 		} else {
3058a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3059a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
3060a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
3061a550ddfcSYishai Hadas 						    dst, underlay_qpn);
306235d19011SMaor Gottlieb 		}
3063038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3064038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3065038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3066038d2ef8SMaor Gottlieb 						dst);
3067cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3068cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3069038d2ef8SMaor Gottlieb 	} else {
3070038d2ef8SMaor Gottlieb 		err = -EINVAL;
3071038d2ef8SMaor Gottlieb 		goto destroy_ft;
3072038d2ef8SMaor Gottlieb 	}
3073038d2ef8SMaor Gottlieb 
3074038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
3075038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
3076038d2ef8SMaor Gottlieb 		handler = NULL;
3077038d2ef8SMaor Gottlieb 		goto destroy_ft;
3078038d2ef8SMaor Gottlieb 	}
3079038d2ef8SMaor Gottlieb 
30809a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3081038d2ef8SMaor Gottlieb 	kfree(dst);
3082038d2ef8SMaor Gottlieb 
3083038d2ef8SMaor Gottlieb 	return &handler->ibflow;
3084038d2ef8SMaor Gottlieb 
3085038d2ef8SMaor Gottlieb destroy_ft:
3086038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
3087cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
3088cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
3089038d2ef8SMaor Gottlieb unlock:
30909a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3091038d2ef8SMaor Gottlieb 	kfree(dst);
3092038d2ef8SMaor Gottlieb 	kfree(handler);
3093038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
3094038d2ef8SMaor Gottlieb }
3095038d2ef8SMaor Gottlieb 
3096e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3097e126ba97SEli Cohen {
3098e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
309981e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
3100e126ba97SEli Cohen 	int err;
3101e126ba97SEli Cohen 
310281e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
310381e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
310481e30880SYishai Hadas 		return -EOPNOTSUPP;
310581e30880SYishai Hadas 	}
310681e30880SYishai Hadas 
31079603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
3108e126ba97SEli Cohen 	if (err)
3109e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
3110e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
3111e126ba97SEli Cohen 
3112e126ba97SEli Cohen 	return err;
3113e126ba97SEli Cohen }
3114e126ba97SEli Cohen 
3115e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3116e126ba97SEli Cohen {
3117e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3118e126ba97SEli Cohen 	int err;
3119e126ba97SEli Cohen 
31209603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
3121e126ba97SEli Cohen 	if (err)
3122e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
3123e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
3124e126ba97SEli Cohen 
3125e126ba97SEli Cohen 	return err;
3126e126ba97SEli Cohen }
3127e126ba97SEli Cohen 
3128e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
3129e126ba97SEli Cohen {
31301b5daf11SMajd Dibbiny 	int err;
3131e126ba97SEli Cohen 
31321b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
3133e126ba97SEli Cohen 	if (err)
3134e126ba97SEli Cohen 		return err;
31351b5daf11SMajd Dibbiny 
31361b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
31371b5daf11SMajd Dibbiny 
31381b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
3139e126ba97SEli Cohen }
3140e126ba97SEli Cohen 
3141e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
3142e126ba97SEli Cohen 			     char *buf)
3143e126ba97SEli Cohen {
3144e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3145e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3146e126ba97SEli Cohen 
31479603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
3148e126ba97SEli Cohen }
3149e126ba97SEli Cohen 
3150e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
3151e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
3152e126ba97SEli Cohen {
3153e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3154e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3155e126ba97SEli Cohen 
31566aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
3157e126ba97SEli Cohen }
3158e126ba97SEli Cohen 
3159e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
3160e126ba97SEli Cohen 			char *buf)
3161e126ba97SEli Cohen {
3162e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3163e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
31649603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
3165e126ba97SEli Cohen }
3166e126ba97SEli Cohen 
3167e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
3168e126ba97SEli Cohen 			char *buf)
3169e126ba97SEli Cohen {
3170e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3171e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
31729603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
3173e126ba97SEli Cohen }
3174e126ba97SEli Cohen 
3175e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
3176e126ba97SEli Cohen 			  char *buf)
3177e126ba97SEli Cohen {
3178e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3179e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3180e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
31819603b61dSJack Morgenstein 		       dev->mdev->board_id);
3182e126ba97SEli Cohen }
3183e126ba97SEli Cohen 
3184e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
3185e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
3186e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
3187e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
3188e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
3189e126ba97SEli Cohen 
3190e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
3191e126ba97SEli Cohen 	&dev_attr_hw_rev,
3192e126ba97SEli Cohen 	&dev_attr_hca_type,
3193e126ba97SEli Cohen 	&dev_attr_board_id,
3194e126ba97SEli Cohen 	&dev_attr_fw_pages,
3195e126ba97SEli Cohen 	&dev_attr_reg_pages,
3196e126ba97SEli Cohen };
3197e126ba97SEli Cohen 
31987722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
31997722f47eSHaggai Eran {
32007722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
32017722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
32027722f47eSHaggai Eran 			     pkey_change_work);
32037722f47eSHaggai Eran 
32047722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
32057722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
32067722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
32077722f47eSHaggai Eran }
32087722f47eSHaggai Eran 
320989ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
321089ea94a7SMaor Gottlieb {
321189ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
321289ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
321389ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
321489ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
321589ea94a7SMaor Gottlieb 	unsigned long flags_qp;
321689ea94a7SMaor Gottlieb 	unsigned long flags_cq;
321789ea94a7SMaor Gottlieb 	unsigned long flags;
321889ea94a7SMaor Gottlieb 
321989ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
322089ea94a7SMaor Gottlieb 
322189ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
322289ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
322389ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
322489ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
322589ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
322689ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
322789ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
322889ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
322989ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
323089ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
323189ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
323289ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
323389ea94a7SMaor Gottlieb 						      &cq_armed_list);
323489ea94a7SMaor Gottlieb 				}
323589ea94a7SMaor Gottlieb 			}
323689ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
323789ea94a7SMaor Gottlieb 		}
323889ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
323989ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
324089ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
324189ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
324289ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
324389ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
324489ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
324589ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
324689ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
324789ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
324889ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
324989ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
325089ea94a7SMaor Gottlieb 							      &cq_armed_list);
325189ea94a7SMaor Gottlieb 					}
325289ea94a7SMaor Gottlieb 				}
325389ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
325489ea94a7SMaor Gottlieb 						       flags_cq);
325589ea94a7SMaor Gottlieb 			}
325689ea94a7SMaor Gottlieb 		}
325789ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
325889ea94a7SMaor Gottlieb 	}
325989ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
326089ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
326189ea94a7SMaor Gottlieb 	 */
326289ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
326389ea94a7SMaor Gottlieb 		mcq->comp(mcq);
326489ea94a7SMaor Gottlieb 	}
326589ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
326689ea94a7SMaor Gottlieb }
326789ea94a7SMaor Gottlieb 
326803404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
326903404e8aSMaor Gottlieb {
327003404e8aSMaor Gottlieb 	int err;
327103404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
327203404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
327303404e8aSMaor Gottlieb 			     delay_drop_work);
327403404e8aSMaor Gottlieb 
3275fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
3276fe248c3aSMaor Gottlieb 
327703404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
327803404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
327903404e8aSMaor Gottlieb 				       delay_drop->timeout);
328003404e8aSMaor Gottlieb 	if (err) {
328103404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
328203404e8aSMaor Gottlieb 			     delay_drop->timeout);
328303404e8aSMaor Gottlieb 		delay_drop->activate = false;
328403404e8aSMaor Gottlieb 	}
328503404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
328603404e8aSMaor Gottlieb }
328703404e8aSMaor Gottlieb 
3288d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
3289e126ba97SEli Cohen {
3290d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
3291d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
3292d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
3293e126ba97SEli Cohen 	struct ib_event ibev;
3294dbaaff2aSEli Cohen 	bool fatal = false;
3295e126ba97SEli Cohen 	u8 port = 0;
3296e126ba97SEli Cohen 
3297d69a24e0SDaniel Jurgens 	if (mlx5_core_is_mp_slave(work->dev)) {
3298d69a24e0SDaniel Jurgens 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->context);
3299d69a24e0SDaniel Jurgens 		if (!ibdev)
3300d69a24e0SDaniel Jurgens 			goto out;
3301d69a24e0SDaniel Jurgens 	} else {
3302d69a24e0SDaniel Jurgens 		ibdev = work->context;
3303d69a24e0SDaniel Jurgens 	}
3304d69a24e0SDaniel Jurgens 
3305d69a24e0SDaniel Jurgens 	switch (work->event) {
3306e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
3307e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
330889ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
3309dbaaff2aSEli Cohen 		fatal = true;
3310e126ba97SEli Cohen 		break;
3311e126ba97SEli Cohen 
3312e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
3313e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
33142788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
3315d69a24e0SDaniel Jurgens 		port = (u8)work->param;
33165ec8c83eSAviv Heller 
33175ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
33185ec8c83eSAviv Heller 		 * mlx5_netdev_event().
33195ec8c83eSAviv Heller 		 */
33205ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
33215ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
3322d69a24e0SDaniel Jurgens 			goto out;
33235ec8c83eSAviv Heller 
3324d69a24e0SDaniel Jurgens 		ibev.event = (work->event == MLX5_DEV_EVENT_PORT_UP) ?
33255ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
3326e126ba97SEli Cohen 		break;
3327e126ba97SEli Cohen 
3328e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
3329e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
3330d69a24e0SDaniel Jurgens 		port = (u8)work->param;
3331e126ba97SEli Cohen 		break;
3332e126ba97SEli Cohen 
3333e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
3334e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
3335d69a24e0SDaniel Jurgens 		port = (u8)work->param;
33367722f47eSHaggai Eran 
33377722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
3338e126ba97SEli Cohen 		break;
3339e126ba97SEli Cohen 
3340e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
3341e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
3342d69a24e0SDaniel Jurgens 		port = (u8)work->param;
3343e126ba97SEli Cohen 		break;
3344e126ba97SEli Cohen 
3345e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
3346e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
3347d69a24e0SDaniel Jurgens 		port = (u8)work->param;
3348e126ba97SEli Cohen 		break;
334903404e8aSMaor Gottlieb 	case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
335003404e8aSMaor Gottlieb 		schedule_work(&ibdev->delay_drop.delay_drop_work);
335103404e8aSMaor Gottlieb 		goto out;
3352bdc37924SSaeed Mahameed 	default:
335303404e8aSMaor Gottlieb 		goto out;
3354e126ba97SEli Cohen 	}
3355e126ba97SEli Cohen 
3356e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
3357e126ba97SEli Cohen 	ibev.element.port_num = port;
3358e126ba97SEli Cohen 
3359a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
3360a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
336103404e8aSMaor Gottlieb 		goto out;
3362a0c84c32SEli Cohen 	}
3363a0c84c32SEli Cohen 
3364e126ba97SEli Cohen 	if (ibdev->ib_active)
3365e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
3366dbaaff2aSEli Cohen 
3367dbaaff2aSEli Cohen 	if (fatal)
3368dbaaff2aSEli Cohen 		ibdev->ib_active = false;
336903404e8aSMaor Gottlieb out:
3370d69a24e0SDaniel Jurgens 	kfree(work);
3371d69a24e0SDaniel Jurgens }
3372d69a24e0SDaniel Jurgens 
3373d69a24e0SDaniel Jurgens static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
3374d69a24e0SDaniel Jurgens 			  enum mlx5_dev_event event, unsigned long param)
3375d69a24e0SDaniel Jurgens {
3376d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
3377d69a24e0SDaniel Jurgens 
3378d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
337910bea9c8SLeon Romanovsky 	if (!work)
338010bea9c8SLeon Romanovsky 		return;
338110bea9c8SLeon Romanovsky 
3382d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
3383d69a24e0SDaniel Jurgens 	work->dev = dev;
3384d69a24e0SDaniel Jurgens 	work->param = param;
3385d69a24e0SDaniel Jurgens 	work->context = context;
3386d69a24e0SDaniel Jurgens 	work->event = event;
3387d69a24e0SDaniel Jurgens 
3388d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
3389d69a24e0SDaniel Jurgens }
3390d69a24e0SDaniel Jurgens 
3391c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
3392c43f1112SMaor Gottlieb {
3393c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
3394c43f1112SMaor Gottlieb 	int err;
3395c43f1112SMaor Gottlieb 	int port;
3396c43f1112SMaor Gottlieb 
3397508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++) {
3398c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
3399c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3400c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
3401c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3402c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
3403c43f1112SMaor Gottlieb 								   port, 0,
3404c43f1112SMaor Gottlieb 								   &vport_ctx);
3405c43f1112SMaor Gottlieb 				if (err) {
3406c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3407c43f1112SMaor Gottlieb 						    port, err);
3408c43f1112SMaor Gottlieb 					return err;
3409c43f1112SMaor Gottlieb 				}
3410c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
3411c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
3412c43f1112SMaor Gottlieb 			} else {
3413c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
3414c43f1112SMaor Gottlieb 			}
3415c43f1112SMaor Gottlieb 		}
3416c43f1112SMaor Gottlieb 	}
3417c43f1112SMaor Gottlieb 	return 0;
3418c43f1112SMaor Gottlieb }
3419c43f1112SMaor Gottlieb 
3420e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3421e126ba97SEli Cohen {
3422e126ba97SEli Cohen 	int port;
3423e126ba97SEli Cohen 
3424508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
3425e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
3426e126ba97SEli Cohen }
3427e126ba97SEli Cohen 
342832f69e4bSDaniel Jurgens static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
3429e126ba97SEli Cohen {
3430e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
3431e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
3432f614fc15SDan Carpenter 	int err = -ENOMEM;
34332528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
3434e126ba97SEli Cohen 
3435e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3436e126ba97SEli Cohen 	if (!pprops)
3437e126ba97SEli Cohen 		goto out;
3438e126ba97SEli Cohen 
3439e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3440e126ba97SEli Cohen 	if (!dprops)
3441e126ba97SEli Cohen 		goto out;
3442e126ba97SEli Cohen 
3443c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
3444c43f1112SMaor Gottlieb 	if (err)
3445c43f1112SMaor Gottlieb 		goto out;
3446c43f1112SMaor Gottlieb 
34472528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
3448e126ba97SEli Cohen 	if (err) {
3449e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
3450e126ba97SEli Cohen 		goto out;
3451e126ba97SEli Cohen 	}
3452e126ba97SEli Cohen 
3453c4550c63SOr Gerlitz 	memset(pprops, 0, sizeof(*pprops));
3454e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3455e126ba97SEli Cohen 	if (err) {
3456938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
3457938fe83cSSaeed Mahameed 			     port, err);
345832f69e4bSDaniel Jurgens 		goto out;
3459e126ba97SEli Cohen 	}
346032f69e4bSDaniel Jurgens 
3461938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
3462938fe83cSSaeed Mahameed 					dprops->max_pkeys;
3463938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
3464938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
346532f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
346632f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
3467e126ba97SEli Cohen 
3468e126ba97SEli Cohen out:
3469e126ba97SEli Cohen 	kfree(pprops);
3470e126ba97SEli Cohen 	kfree(dprops);
3471e126ba97SEli Cohen 
3472e126ba97SEli Cohen 	return err;
3473e126ba97SEli Cohen }
3474e126ba97SEli Cohen 
3475e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3476e126ba97SEli Cohen {
3477e126ba97SEli Cohen 	int err;
3478e126ba97SEli Cohen 
3479e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
3480e126ba97SEli Cohen 	if (err)
3481e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3482e126ba97SEli Cohen 
3483e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
3484add08d76SChristoph Hellwig 	ib_free_cq(dev->umrc.cq);
3485e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
3486e126ba97SEli Cohen }
3487e126ba97SEli Cohen 
3488e126ba97SEli Cohen enum {
3489e126ba97SEli Cohen 	MAX_UMR_WR = 128,
3490e126ba97SEli Cohen };
3491e126ba97SEli Cohen 
3492e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
3493e126ba97SEli Cohen {
3494e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
3495e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
3496e126ba97SEli Cohen 	struct ib_pd *pd;
3497e126ba97SEli Cohen 	struct ib_cq *cq;
3498e126ba97SEli Cohen 	struct ib_qp *qp;
3499e126ba97SEli Cohen 	int ret;
3500e126ba97SEli Cohen 
3501e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3502e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3503e126ba97SEli Cohen 	if (!attr || !init_attr) {
3504e126ba97SEli Cohen 		ret = -ENOMEM;
3505e126ba97SEli Cohen 		goto error_0;
3506e126ba97SEli Cohen 	}
3507e126ba97SEli Cohen 
3508ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
3509e126ba97SEli Cohen 	if (IS_ERR(pd)) {
3510e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3511e126ba97SEli Cohen 		ret = PTR_ERR(pd);
3512e126ba97SEli Cohen 		goto error_0;
3513e126ba97SEli Cohen 	}
3514e126ba97SEli Cohen 
3515add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
3516e126ba97SEli Cohen 	if (IS_ERR(cq)) {
3517e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3518e126ba97SEli Cohen 		ret = PTR_ERR(cq);
3519e126ba97SEli Cohen 		goto error_2;
3520e126ba97SEli Cohen 	}
3521e126ba97SEli Cohen 
3522e126ba97SEli Cohen 	init_attr->send_cq = cq;
3523e126ba97SEli Cohen 	init_attr->recv_cq = cq;
3524e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3525e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
3526e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
3527e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3528e126ba97SEli Cohen 	init_attr->port_num = 1;
3529e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3530e126ba97SEli Cohen 	if (IS_ERR(qp)) {
3531e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3532e126ba97SEli Cohen 		ret = PTR_ERR(qp);
3533e126ba97SEli Cohen 		goto error_3;
3534e126ba97SEli Cohen 	}
3535e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
3536e126ba97SEli Cohen 	qp->real_qp    = qp;
3537e126ba97SEli Cohen 	qp->uobject    = NULL;
3538e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
353931fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
354031fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
3541e126ba97SEli Cohen 
3542e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
3543e126ba97SEli Cohen 	attr->port_num = 1;
3544e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3545e126ba97SEli Cohen 				IB_QP_PORT, NULL);
3546e126ba97SEli Cohen 	if (ret) {
3547e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3548e126ba97SEli Cohen 		goto error_4;
3549e126ba97SEli Cohen 	}
3550e126ba97SEli Cohen 
3551e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3552e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
3553e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
3554e126ba97SEli Cohen 
3555e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3556e126ba97SEli Cohen 	if (ret) {
3557e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3558e126ba97SEli Cohen 		goto error_4;
3559e126ba97SEli Cohen 	}
3560e126ba97SEli Cohen 
3561e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3562e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
3563e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3564e126ba97SEli Cohen 	if (ret) {
3565e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3566e126ba97SEli Cohen 		goto error_4;
3567e126ba97SEli Cohen 	}
3568e126ba97SEli Cohen 
3569e126ba97SEli Cohen 	dev->umrc.qp = qp;
3570e126ba97SEli Cohen 	dev->umrc.cq = cq;
3571e126ba97SEli Cohen 	dev->umrc.pd = pd;
3572e126ba97SEli Cohen 
3573e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
3574e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
3575e126ba97SEli Cohen 	if (ret) {
3576e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3577e126ba97SEli Cohen 		goto error_4;
3578e126ba97SEli Cohen 	}
3579e126ba97SEli Cohen 
3580e126ba97SEli Cohen 	kfree(attr);
3581e126ba97SEli Cohen 	kfree(init_attr);
3582e126ba97SEli Cohen 
3583e126ba97SEli Cohen 	return 0;
3584e126ba97SEli Cohen 
3585e126ba97SEli Cohen error_4:
3586e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
3587e126ba97SEli Cohen 
3588e126ba97SEli Cohen error_3:
3589add08d76SChristoph Hellwig 	ib_free_cq(cq);
3590e126ba97SEli Cohen 
3591e126ba97SEli Cohen error_2:
3592e126ba97SEli Cohen 	ib_dealloc_pd(pd);
3593e126ba97SEli Cohen 
3594e126ba97SEli Cohen error_0:
3595e126ba97SEli Cohen 	kfree(attr);
3596e126ba97SEli Cohen 	kfree(init_attr);
3597e126ba97SEli Cohen 	return ret;
3598e126ba97SEli Cohen }
3599e126ba97SEli Cohen 
36006e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
36016e8484c5SMax Gurtovoy {
36026e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
36036e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
36046e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
36056e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
36066e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
36076e8484c5SMax Gurtovoy 	default:
36086e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
36096e8484c5SMax Gurtovoy 	}
36106e8484c5SMax Gurtovoy }
36116e8484c5SMax Gurtovoy 
3612e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
3613e126ba97SEli Cohen {
3614e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
3615e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3616bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
36177722f47eSHaggai Eran 	int port;
3618e126ba97SEli Cohen 	int ret = 0;
3619e126ba97SEli Cohen 
3620e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
3621e126ba97SEli Cohen 
3622d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
3623d16e91daSHaggai Eran 
3624e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3625e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
3626e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
3627e126ba97SEli Cohen 		goto error0;
3628e126ba97SEli Cohen 	}
3629e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
3630e126ba97SEli Cohen 	devr->p0->uobject = NULL;
3631e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
3632e126ba97SEli Cohen 
3633bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
3634e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
3635e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
3636e126ba97SEli Cohen 		goto error1;
3637e126ba97SEli Cohen 	}
3638e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
3639e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
3640e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
3641e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
3642e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
3643e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
3644e126ba97SEli Cohen 
3645e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3646e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
3647e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
3648e126ba97SEli Cohen 		goto error2;
3649e126ba97SEli Cohen 	}
3650e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
3651e126ba97SEli Cohen 	devr->x0->inode = NULL;
3652e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
3653e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
3654e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3655e126ba97SEli Cohen 
3656e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3657e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
3658e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
3659e126ba97SEli Cohen 		goto error3;
3660e126ba97SEli Cohen 	}
3661e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
3662e126ba97SEli Cohen 	devr->x1->inode = NULL;
3663e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
3664e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
3665e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3666e126ba97SEli Cohen 
3667e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
3668e126ba97SEli Cohen 	attr.attr.max_sge = 1;
3669e126ba97SEli Cohen 	attr.attr.max_wr = 1;
3670e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
36711a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
3672e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
3673e126ba97SEli Cohen 
3674e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3675e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
3676e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
3677e126ba97SEli Cohen 		goto error4;
3678e126ba97SEli Cohen 	}
3679e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
3680e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
3681e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
3682e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
3683e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
3684e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
3685e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
36861a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
3687e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
36881a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
3689e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
3690e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
3691e126ba97SEli Cohen 
36924aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
36934aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
36944aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
36954aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
36964aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
36974aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
36984aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
36994aa17b28SHaggai Abramonvsky 		goto error5;
37004aa17b28SHaggai Abramonvsky 	}
37014aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
37024aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
37034aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
37044aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
37054aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
37064aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
37071a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
37084aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
37091a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
37104aa17b28SHaggai Abramonvsky 
37117722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
37127722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
37137722f47eSHaggai Eran 			  pkey_change_handler);
37147722f47eSHaggai Eran 		devr->ports[port].devr = devr;
37157722f47eSHaggai Eran 	}
37167722f47eSHaggai Eran 
3717e126ba97SEli Cohen 	return 0;
3718e126ba97SEli Cohen 
37194aa17b28SHaggai Abramonvsky error5:
37204aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
3721e126ba97SEli Cohen error4:
3722e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3723e126ba97SEli Cohen error3:
3724e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3725e126ba97SEli Cohen error2:
3726e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3727e126ba97SEli Cohen error1:
3728e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
3729e126ba97SEli Cohen error0:
3730e126ba97SEli Cohen 	return ret;
3731e126ba97SEli Cohen }
3732e126ba97SEli Cohen 
3733e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3734e126ba97SEli Cohen {
37357722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
37367722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
37377722f47eSHaggai Eran 	int port;
37387722f47eSHaggai Eran 
37394aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
3740e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
3741e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3742e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3743e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3744e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
37457722f47eSHaggai Eran 
37467722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
37477722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
37487722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
3749e126ba97SEli Cohen }
3750e126ba97SEli Cohen 
3751e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
3752e53505a8SAchiad Shochat {
3753e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3754e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3755e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3756e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
375785c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
3758e53505a8SAchiad Shochat 	u32 ret = 0;
3759e53505a8SAchiad Shochat 
3760e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
3761e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
3762e53505a8SAchiad Shochat 
376385c7c014SDaniel Jurgens 	if (raw_support)
376472cd5717SOr Gerlitz 		ret = RDMA_CORE_PORT_RAW_PACKET;
376572cd5717SOr Gerlitz 
3766e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
376772cd5717SOr Gerlitz 		return ret;
3768e53505a8SAchiad Shochat 
3769e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
377072cd5717SOr Gerlitz 		return ret;
3771e53505a8SAchiad Shochat 
3772e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3773e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
3774e53505a8SAchiad Shochat 
3775e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3776e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3777e53505a8SAchiad Shochat 
3778e53505a8SAchiad Shochat 	return ret;
3779e53505a8SAchiad Shochat }
3780e53505a8SAchiad Shochat 
37817738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
37827738613eSIra Weiny 			       struct ib_port_immutable *immutable)
37837738613eSIra Weiny {
37847738613eSIra Weiny 	struct ib_port_attr attr;
3785ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3786ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
37877738613eSIra Weiny 	int err;
37887738613eSIra Weiny 
3789c4550c63SOr Gerlitz 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3790c4550c63SOr Gerlitz 
3791c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
37927738613eSIra Weiny 	if (err)
37937738613eSIra Weiny 		return err;
37947738613eSIra Weiny 
37957738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
37967738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
3797e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3798ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3799337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
38007738613eSIra Weiny 
38017738613eSIra Weiny 	return 0;
38027738613eSIra Weiny }
38037738613eSIra Weiny 
38048e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
38058e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
38068e6efa3aSMark Bloch {
38078e6efa3aSMark Bloch 	struct ib_port_attr attr;
38088e6efa3aSMark Bloch 	int err;
38098e6efa3aSMark Bloch 
38108e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
38118e6efa3aSMark Bloch 
38128e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
38138e6efa3aSMark Bloch 	if (err)
38148e6efa3aSMark Bloch 		return err;
38158e6efa3aSMark Bloch 
38168e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
38178e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
38188e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
38198e6efa3aSMark Bloch 
38208e6efa3aSMark Bloch 	return 0;
38218e6efa3aSMark Bloch }
38228e6efa3aSMark Bloch 
38239abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
3824c7342823SIra Weiny {
3825c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
3826c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
38279abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
38289abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
38299abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
3830c7342823SIra Weiny }
3831c7342823SIra Weiny 
383245f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
38339ef9c640SAviv Heller {
38349ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
38359ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
38369ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
38379ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
38389ef9c640SAviv Heller 	int err;
38399ef9c640SAviv Heller 
38409ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
38419ef9c640SAviv Heller 		return 0;
38429ef9c640SAviv Heller 
38439ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
38449ef9c640SAviv Heller 	if (err)
38459ef9c640SAviv Heller 		return err;
38469ef9c640SAviv Heller 
38479ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
38489ef9c640SAviv Heller 	if (IS_ERR(ft)) {
38499ef9c640SAviv Heller 		err = PTR_ERR(ft);
38509ef9c640SAviv Heller 		goto err_destroy_vport_lag;
38519ef9c640SAviv Heller 	}
38529ef9c640SAviv Heller 
38539a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
38549ef9c640SAviv Heller 	return 0;
38559ef9c640SAviv Heller 
38569ef9c640SAviv Heller err_destroy_vport_lag:
38579ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
38589ef9c640SAviv Heller 	return err;
38599ef9c640SAviv Heller }
38609ef9c640SAviv Heller 
386145f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
38629ef9c640SAviv Heller {
38639ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
38649ef9c640SAviv Heller 
38659a4ca38dSMark Bloch 	if (dev->flow_db->lag_demux_ft) {
38669a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
38679a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
38689ef9c640SAviv Heller 
38699ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
38709ef9c640SAviv Heller 	}
38719ef9c640SAviv Heller }
38729ef9c640SAviv Heller 
38737fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
3874fc24fc5eSAchiad Shochat {
3875e53505a8SAchiad Shochat 	int err;
3876e53505a8SAchiad Shochat 
38777fd8aefbSDaniel Jurgens 	dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
38787fd8aefbSDaniel Jurgens 	err = register_netdevice_notifier(&dev->roce[port_num].nb);
38795ec8c83eSAviv Heller 	if (err) {
38807fd8aefbSDaniel Jurgens 		dev->roce[port_num].nb.notifier_call = NULL;
3881e53505a8SAchiad Shochat 		return err;
38825ec8c83eSAviv Heller 	}
3883e53505a8SAchiad Shochat 
3884d012f5d6SOr Gerlitz 	return 0;
3885d012f5d6SOr Gerlitz }
3886d012f5d6SOr Gerlitz 
38877fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
3888e126ba97SEli Cohen {
38897fd8aefbSDaniel Jurgens 	if (dev->roce[port_num].nb.notifier_call) {
38907fd8aefbSDaniel Jurgens 		unregister_netdevice_notifier(&dev->roce[port_num].nb);
38917fd8aefbSDaniel Jurgens 		dev->roce[port_num].nb.notifier_call = NULL;
3892e126ba97SEli Cohen 	}
3893e126ba97SEli Cohen }
3894e126ba97SEli Cohen 
38957fd8aefbSDaniel Jurgens static int mlx5_enable_eth(struct mlx5_ib_dev *dev, u8 port_num)
3896e126ba97SEli Cohen {
3897e126ba97SEli Cohen 	int err;
3898e126ba97SEli Cohen 
3899ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
3900e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3901e53505a8SAchiad Shochat 		if (err)
39028e6efa3aSMark Bloch 			return err;
3903ca5b91d6SOr Gerlitz 	}
3904e53505a8SAchiad Shochat 
390545f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
39069ef9c640SAviv Heller 	if (err)
39079ef9c640SAviv Heller 		goto err_disable_roce;
39089ef9c640SAviv Heller 
3909e53505a8SAchiad Shochat 	return 0;
3910e53505a8SAchiad Shochat 
39119ef9c640SAviv Heller err_disable_roce:
3912ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
39139ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
39149ef9c640SAviv Heller 
3915e53505a8SAchiad Shochat 	return err;
3916fc24fc5eSAchiad Shochat }
3917fc24fc5eSAchiad Shochat 
391845f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3919fc24fc5eSAchiad Shochat {
392045f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3921ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
3922e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
3923fc24fc5eSAchiad Shochat }
3924fc24fc5eSAchiad Shochat 
3925e1f24a79SParav Pandit struct mlx5_ib_counter {
39267c16f477SKamal Heib 	const char *name;
39277c16f477SKamal Heib 	size_t offset;
39287c16f477SKamal Heib };
39297c16f477SKamal Heib 
39307c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
39317c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
39327c16f477SKamal Heib 
3933e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
39347c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
39357c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
39367c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
39377c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
39387c16f477SKamal Heib };
39397c16f477SKamal Heib 
3940e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
39417c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
39427c16f477SKamal Heib };
39437c16f477SKamal Heib 
3944e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
39457c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
39467c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
39477c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
39487c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
39497c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
39507c16f477SKamal Heib };
39517c16f477SKamal Heib 
3952e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
3953e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
3954e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3955e1f24a79SParav Pandit 
3956e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
3957e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
3958e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
3959e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3960e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
3961e1f24a79SParav Pandit };
3962e1f24a79SParav Pandit 
396358dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
396458dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
396558dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
396658dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
396758dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
396858dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
396958dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
397058dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
397158dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
397258dcb60aSParav Pandit };
397358dcb60aSParav Pandit 
3974e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
39750837e86aSMark Bloch {
3976aac4492eSDaniel Jurgens 	int i;
39770837e86aSMark Bloch 
39787c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
3979aac4492eSDaniel Jurgens 		if (dev->port[i].cnts.set_id)
39800837e86aSMark Bloch 			mlx5_core_dealloc_q_counter(dev->mdev,
3981e1f24a79SParav Pandit 						    dev->port[i].cnts.set_id);
3982e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
3983e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
39847c16f477SKamal Heib 	}
39857c16f477SKamal Heib }
39867c16f477SKamal Heib 
3987e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3988e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
39897c16f477SKamal Heib {
39907c16f477SKamal Heib 	u32 num_counters;
39917c16f477SKamal Heib 
39927c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
39937c16f477SKamal Heib 
39947c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
39957c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
39967c16f477SKamal Heib 
39977c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
39987c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
399958dcb60aSParav Pandit 
400058dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
400158dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
400258dcb60aSParav Pandit 
4003e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
40047c16f477SKamal Heib 
4005e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4006e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
4007e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
4008e1f24a79SParav Pandit 	}
4009e1f24a79SParav Pandit 
4010e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
4011e1f24a79SParav Pandit 	if (!cnts->names)
40127c16f477SKamal Heib 		return -ENOMEM;
40137c16f477SKamal Heib 
4014e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
4015e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
4016e1f24a79SParav Pandit 	if (!cnts->offsets)
40177c16f477SKamal Heib 		goto err_names;
40187c16f477SKamal Heib 
40197c16f477SKamal Heib 	return 0;
40207c16f477SKamal Heib 
40217c16f477SKamal Heib err_names:
4022e1f24a79SParav Pandit 	kfree(cnts->names);
4023aac4492eSDaniel Jurgens 	cnts->names = NULL;
40247c16f477SKamal Heib 	return -ENOMEM;
40257c16f477SKamal Heib }
40267c16f477SKamal Heib 
4027e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
40287c16f477SKamal Heib 				  const char **names,
40297c16f477SKamal Heib 				  size_t *offsets)
40307c16f477SKamal Heib {
40317c16f477SKamal Heib 	int i;
40327c16f477SKamal Heib 	int j = 0;
40337c16f477SKamal Heib 
40347c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
40357c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
40367c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
40377c16f477SKamal Heib 	}
40387c16f477SKamal Heib 
40397c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
40407c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
40417c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
40427c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
40437c16f477SKamal Heib 		}
40447c16f477SKamal Heib 	}
40457c16f477SKamal Heib 
40467c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
40477c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
40487c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
40497c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
40507c16f477SKamal Heib 		}
40517c16f477SKamal Heib 	}
4052e1f24a79SParav Pandit 
405358dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
405458dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
405558dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
405658dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
405758dcb60aSParav Pandit 		}
405858dcb60aSParav Pandit 	}
405958dcb60aSParav Pandit 
4060e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4061e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
4062e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
4063e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
4064e1f24a79SParav Pandit 		}
4065e1f24a79SParav Pandit 	}
40660837e86aSMark Bloch }
40670837e86aSMark Bloch 
4068e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
40690837e86aSMark Bloch {
4070aac4492eSDaniel Jurgens 	int err = 0;
40710837e86aSMark Bloch 	int i;
40720837e86aSMark Bloch 
40730837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
4074aac4492eSDaniel Jurgens 		err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
4075aac4492eSDaniel Jurgens 		if (err)
4076aac4492eSDaniel Jurgens 			goto err_alloc;
40777c16f477SKamal Heib 
4078aac4492eSDaniel Jurgens 		mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
4079aac4492eSDaniel Jurgens 				      dev->port[i].cnts.offsets);
4080aac4492eSDaniel Jurgens 
4081aac4492eSDaniel Jurgens 		err = mlx5_core_alloc_q_counter(dev->mdev,
4082aac4492eSDaniel Jurgens 						&dev->port[i].cnts.set_id);
4083aac4492eSDaniel Jurgens 		if (err) {
40840837e86aSMark Bloch 			mlx5_ib_warn(dev,
40850837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
4086aac4492eSDaniel Jurgens 				     i + 1, err);
4087aac4492eSDaniel Jurgens 			goto err_alloc;
40880837e86aSMark Bloch 		}
4089aac4492eSDaniel Jurgens 		dev->port[i].cnts.set_id_valid = true;
40900837e86aSMark Bloch 	}
40910837e86aSMark Bloch 
40920837e86aSMark Bloch 	return 0;
40930837e86aSMark Bloch 
4094aac4492eSDaniel Jurgens err_alloc:
4095aac4492eSDaniel Jurgens 	mlx5_ib_dealloc_counters(dev);
4096aac4492eSDaniel Jurgens 	return err;
40970837e86aSMark Bloch }
40980837e86aSMark Bloch 
40990ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
41000ad17a8fSMark Bloch 						    u8 port_num)
41010ad17a8fSMark Bloch {
41027c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
41037c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
41040ad17a8fSMark Bloch 
41050ad17a8fSMark Bloch 	/* We support only per port stats */
41060ad17a8fSMark Bloch 	if (port_num == 0)
41070ad17a8fSMark Bloch 		return NULL;
41080ad17a8fSMark Bloch 
4109e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
4110e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
4111e1f24a79SParav Pandit 					  port->cnts.num_cong_counters,
41120ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
41130ad17a8fSMark Bloch }
41140ad17a8fSMark Bloch 
4115aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
4116e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
4117e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
4118e1f24a79SParav Pandit {
4119e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
4120e1f24a79SParav Pandit 	void *out;
4121e1f24a79SParav Pandit 	__be32 val;
4122e1f24a79SParav Pandit 	int ret, i;
4123e1f24a79SParav Pandit 
41241b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
4125e1f24a79SParav Pandit 	if (!out)
4126e1f24a79SParav Pandit 		return -ENOMEM;
4127e1f24a79SParav Pandit 
4128aac4492eSDaniel Jurgens 	ret = mlx5_core_query_q_counter(mdev,
4129e1f24a79SParav Pandit 					port->cnts.set_id, 0,
4130e1f24a79SParav Pandit 					out, outlen);
4131e1f24a79SParav Pandit 	if (ret)
4132e1f24a79SParav Pandit 		goto free;
4133e1f24a79SParav Pandit 
4134e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
4135e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
4136e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
4137e1f24a79SParav Pandit 	}
4138e1f24a79SParav Pandit 
4139e1f24a79SParav Pandit free:
4140e1f24a79SParav Pandit 	kvfree(out);
4141e1f24a79SParav Pandit 	return ret;
4142e1f24a79SParav Pandit }
4143e1f24a79SParav Pandit 
41440ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
41450ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
41467c16f477SKamal Heib 				u8 port_num, int index)
41470ad17a8fSMark Bloch {
41480ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
41497c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
4150aac4492eSDaniel Jurgens 	struct mlx5_core_dev *mdev;
4151e1f24a79SParav Pandit 	int ret, num_counters;
4152aac4492eSDaniel Jurgens 	u8 mdev_port_num;
41530ad17a8fSMark Bloch 
41547c16f477SKamal Heib 	if (!stats)
4155e1f24a79SParav Pandit 		return -EINVAL;
41560ad17a8fSMark Bloch 
4157aac4492eSDaniel Jurgens 	num_counters = port->cnts.num_q_counters + port->cnts.num_cong_counters;
4158aac4492eSDaniel Jurgens 
4159aac4492eSDaniel Jurgens 	/* q_counters are per IB device, query the master mdev */
4160aac4492eSDaniel Jurgens 	ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
41610ad17a8fSMark Bloch 	if (ret)
4162e1f24a79SParav Pandit 		return ret;
41630ad17a8fSMark Bloch 
4164e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4165aac4492eSDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
4166aac4492eSDaniel Jurgens 						    &mdev_port_num);
4167aac4492eSDaniel Jurgens 		if (!mdev) {
4168aac4492eSDaniel Jurgens 			/* If port is not affiliated yet, its in down state
4169aac4492eSDaniel Jurgens 			 * which doesn't have any counters yet, so it would be
4170aac4492eSDaniel Jurgens 			 * zero. So no need to read from the HCA.
4171aac4492eSDaniel Jurgens 			 */
4172aac4492eSDaniel Jurgens 			goto done;
4173aac4492eSDaniel Jurgens 		}
417471a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
417571a0ff65SMajd Dibbiny 						   stats->value +
417671a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters,
417771a0ff65SMajd Dibbiny 						   port->cnts.num_cong_counters,
417871a0ff65SMajd Dibbiny 						   port->cnts.offsets +
417971a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters);
4180aac4492eSDaniel Jurgens 
4181aac4492eSDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
4182e1f24a79SParav Pandit 		if (ret)
4183e1f24a79SParav Pandit 			return ret;
41840ad17a8fSMark Bloch 	}
41857c16f477SKamal Heib 
4186aac4492eSDaniel Jurgens done:
4187e1f24a79SParav Pandit 	return num_counters;
41880ad17a8fSMark Bloch }
41890ad17a8fSMark Bloch 
41908e959601SNiranjana Vishwanathapura static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
41918e959601SNiranjana Vishwanathapura {
41928e959601SNiranjana Vishwanathapura 	return mlx5_rdma_netdev_free(netdev);
41938e959601SNiranjana Vishwanathapura }
41948e959601SNiranjana Vishwanathapura 
4195693dfd5aSErez Shitrit static struct net_device*
4196693dfd5aSErez Shitrit mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
4197693dfd5aSErez Shitrit 			  u8 port_num,
4198693dfd5aSErez Shitrit 			  enum rdma_netdev_t type,
4199693dfd5aSErez Shitrit 			  const char *name,
4200693dfd5aSErez Shitrit 			  unsigned char name_assign_type,
4201693dfd5aSErez Shitrit 			  void (*setup)(struct net_device *))
4202693dfd5aSErez Shitrit {
42038e959601SNiranjana Vishwanathapura 	struct net_device *netdev;
42048e959601SNiranjana Vishwanathapura 	struct rdma_netdev *rn;
42058e959601SNiranjana Vishwanathapura 
4206693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
4207693dfd5aSErez Shitrit 		return ERR_PTR(-EOPNOTSUPP);
4208693dfd5aSErez Shitrit 
42098e959601SNiranjana Vishwanathapura 	netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
4210693dfd5aSErez Shitrit 					name, setup);
42118e959601SNiranjana Vishwanathapura 	if (likely(!IS_ERR_OR_NULL(netdev))) {
42128e959601SNiranjana Vishwanathapura 		rn = netdev_priv(netdev);
42138e959601SNiranjana Vishwanathapura 		rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
4214693dfd5aSErez Shitrit 	}
42158e959601SNiranjana Vishwanathapura 	return netdev;
4216693dfd5aSErez Shitrit }
4217693dfd5aSErez Shitrit 
4218fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
4219fe248c3aSMaor Gottlieb {
4220fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
4221fe248c3aSMaor Gottlieb 		return;
4222fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
4223fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
4224fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
4225fe248c3aSMaor Gottlieb }
4226fe248c3aSMaor Gottlieb 
422703404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
422803404e8aSMaor Gottlieb {
422903404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
423003404e8aSMaor Gottlieb 		return;
423103404e8aSMaor Gottlieb 
423203404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
4233fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
4234fe248c3aSMaor Gottlieb }
4235fe248c3aSMaor Gottlieb 
4236fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
4237fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
4238fe248c3aSMaor Gottlieb {
4239fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
4240fe248c3aSMaor Gottlieb 	char lbuf[20];
4241fe248c3aSMaor Gottlieb 	int len;
4242fe248c3aSMaor Gottlieb 
4243fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
4244fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
4245fe248c3aSMaor Gottlieb }
4246fe248c3aSMaor Gottlieb 
4247fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
4248fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
4249fe248c3aSMaor Gottlieb {
4250fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
4251fe248c3aSMaor Gottlieb 	u32 timeout;
4252fe248c3aSMaor Gottlieb 	u32 var;
4253fe248c3aSMaor Gottlieb 
4254fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
4255fe248c3aSMaor Gottlieb 		return -EFAULT;
4256fe248c3aSMaor Gottlieb 
4257fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
4258fe248c3aSMaor Gottlieb 			1000);
4259fe248c3aSMaor Gottlieb 	if (timeout != var)
4260fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
4261fe248c3aSMaor Gottlieb 			    timeout);
4262fe248c3aSMaor Gottlieb 
4263fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
4264fe248c3aSMaor Gottlieb 
4265fe248c3aSMaor Gottlieb 	return count;
4266fe248c3aSMaor Gottlieb }
4267fe248c3aSMaor Gottlieb 
4268fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
4269fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
4270fe248c3aSMaor Gottlieb 	.open	= simple_open,
4271fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
4272fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
4273fe248c3aSMaor Gottlieb };
4274fe248c3aSMaor Gottlieb 
4275fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
4276fe248c3aSMaor Gottlieb {
4277fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
4278fe248c3aSMaor Gottlieb 
4279fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
4280fe248c3aSMaor Gottlieb 		return 0;
4281fe248c3aSMaor Gottlieb 
4282fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
4283fe248c3aSMaor Gottlieb 	if (!dbg)
4284fe248c3aSMaor Gottlieb 		return -ENOMEM;
4285fe248c3aSMaor Gottlieb 
4286cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
4287cbafad87SSudip Mukherjee 
4288fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
4289fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
4290fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
4291fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
4292cbafad87SSudip Mukherjee 		goto out_debugfs;
4293fe248c3aSMaor Gottlieb 
4294fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
4295fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
4296fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
4297fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
4298fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
4299fe248c3aSMaor Gottlieb 		goto out_debugfs;
4300fe248c3aSMaor Gottlieb 
4301fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
4302fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
4303fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
4304fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
4305fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
4306fe248c3aSMaor Gottlieb 		goto out_debugfs;
4307fe248c3aSMaor Gottlieb 
4308fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
4309fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
4310fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
4311fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
4312fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
4313fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
4314fe248c3aSMaor Gottlieb 		goto out_debugfs;
4315fe248c3aSMaor Gottlieb 
4316fe248c3aSMaor Gottlieb 	return 0;
4317fe248c3aSMaor Gottlieb 
4318fe248c3aSMaor Gottlieb out_debugfs:
4319fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
4320fe248c3aSMaor Gottlieb 	return -ENOMEM;
432103404e8aSMaor Gottlieb }
432203404e8aSMaor Gottlieb 
432303404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
432403404e8aSMaor Gottlieb {
432503404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
432603404e8aSMaor Gottlieb 		return;
432703404e8aSMaor Gottlieb 
432803404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
432903404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
433003404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
433103404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
433203404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
4333fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
4334fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
4335fe248c3aSMaor Gottlieb 
4336fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
4337fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
433803404e8aSMaor Gottlieb }
433903404e8aSMaor Gottlieb 
434084305d71SLeon Romanovsky static const struct cpumask *
434184305d71SLeon Romanovsky mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
434240b24403SSagi Grimberg {
434340b24403SSagi Grimberg 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
434440b24403SSagi Grimberg 
434540b24403SSagi Grimberg 	return mlx5_get_vector_affinity(dev->mdev, comp_vector);
434640b24403SSagi Grimberg }
434740b24403SSagi Grimberg 
434832f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
434932f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
435032f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
435132f69e4bSDaniel Jurgens {
435232f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
435332f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
435432f69e4bSDaniel Jurgens 	int comps;
435532f69e4bSDaniel Jurgens 	int err;
435632f69e4bSDaniel Jurgens 	int i;
435732f69e4bSDaniel Jurgens 
4358a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
4359a9e546e7SParav Pandit 
436032f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
436132f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
436232f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
436332f69e4bSDaniel Jurgens 		return;
436432f69e4bSDaniel Jurgens 	}
436532f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
436632f69e4bSDaniel Jurgens 
436732f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
436832f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
436932f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
437032f69e4bSDaniel Jurgens 
437132f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
437232f69e4bSDaniel Jurgens 	if (comps) {
437332f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
437432f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
437532f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
437632f69e4bSDaniel Jurgens 
437732f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
437832f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
437932f69e4bSDaniel Jurgens 
438032f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
438132f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
438232f69e4bSDaniel Jurgens 	}
438332f69e4bSDaniel Jurgens 
438432f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
438532f69e4bSDaniel Jurgens 
438632f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
438732f69e4bSDaniel Jurgens 
438832f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
438932f69e4bSDaniel Jurgens 
439032f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
439132f69e4bSDaniel Jurgens 
439232f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
439332f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
439432f69e4bSDaniel Jurgens 	 * it back to the list.
439532f69e4bSDaniel Jurgens 	 */
439632f69e4bSDaniel Jurgens 	if (err)
439732f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
439832f69e4bSDaniel Jurgens 			    port_num + 1);
439932f69e4bSDaniel Jurgens 
440032f69e4bSDaniel Jurgens 	ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
440132f69e4bSDaniel Jurgens }
440232f69e4bSDaniel Jurgens 
440332f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
440432f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
440532f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
440632f69e4bSDaniel Jurgens {
440732f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
440832f69e4bSDaniel Jurgens 	int err;
440932f69e4bSDaniel Jurgens 
441032f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
441132f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
441232f69e4bSDaniel Jurgens 		mlx5_ib_warn(ibdev, "port %d already affiliated.\n",
441332f69e4bSDaniel Jurgens 			     port_num + 1);
441432f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
441532f69e4bSDaniel Jurgens 		return false;
441632f69e4bSDaniel Jurgens 	}
441732f69e4bSDaniel Jurgens 
441832f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
441932f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
442032f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
442132f69e4bSDaniel Jurgens 
442232f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
442332f69e4bSDaniel Jurgens 	if (err)
442432f69e4bSDaniel Jurgens 		goto unbind;
442532f69e4bSDaniel Jurgens 
442632f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
442732f69e4bSDaniel Jurgens 	if (err)
442832f69e4bSDaniel Jurgens 		goto unbind;
442932f69e4bSDaniel Jurgens 
443032f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
443132f69e4bSDaniel Jurgens 	if (err) {
443232f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
443332f69e4bSDaniel Jurgens 			    port_num + 1);
443432f69e4bSDaniel Jurgens 		goto unbind;
443532f69e4bSDaniel Jurgens 	}
443632f69e4bSDaniel Jurgens 
4437a9e546e7SParav Pandit 	err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
4438a9e546e7SParav Pandit 	if (err)
4439a9e546e7SParav Pandit 		goto unbind;
4440a9e546e7SParav Pandit 
444132f69e4bSDaniel Jurgens 	return true;
444232f69e4bSDaniel Jurgens 
444332f69e4bSDaniel Jurgens unbind:
444432f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
444532f69e4bSDaniel Jurgens 	return false;
444632f69e4bSDaniel Jurgens }
444732f69e4bSDaniel Jurgens 
444832f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
444932f69e4bSDaniel Jurgens {
445032f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
445132f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
445232f69e4bSDaniel Jurgens 							  port_num + 1);
445332f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
445432f69e4bSDaniel Jurgens 	int err;
445532f69e4bSDaniel Jurgens 	int i;
445632f69e4bSDaniel Jurgens 
445732f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
445832f69e4bSDaniel Jurgens 		return 0;
445932f69e4bSDaniel Jurgens 
446032f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
446132f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
446232f69e4bSDaniel Jurgens 	if (err)
446332f69e4bSDaniel Jurgens 		return err;
446432f69e4bSDaniel Jurgens 
446532f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
446632f69e4bSDaniel Jurgens 	if (err)
446732f69e4bSDaniel Jurgens 		return err;
446832f69e4bSDaniel Jurgens 
446932f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
447032f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
447132f69e4bSDaniel Jurgens 		bool bound = false;
447232f69e4bSDaniel Jurgens 
447332f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
447432f69e4bSDaniel Jurgens 		if (i == port_num) {
447532f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
447632f69e4bSDaniel Jurgens 			if (!mpi) {
447732f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
447832f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
447932f69e4bSDaniel Jurgens 				return -ENOMEM;
448032f69e4bSDaniel Jurgens 			}
448132f69e4bSDaniel Jurgens 
448232f69e4bSDaniel Jurgens 			mpi->is_master = true;
448332f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
448432f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
448532f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
448632f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
448732f69e4bSDaniel Jurgens 			mpi = NULL;
448832f69e4bSDaniel Jurgens 			continue;
448932f69e4bSDaniel Jurgens 		}
449032f69e4bSDaniel Jurgens 
449132f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
449232f69e4bSDaniel Jurgens 				    list) {
449332f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
449432f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
449532f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
449632f69e4bSDaniel Jurgens 			}
449732f69e4bSDaniel Jurgens 
449832f69e4bSDaniel Jurgens 			if (bound) {
449932f69e4bSDaniel Jurgens 				dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
450032f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
450132f69e4bSDaniel Jurgens 				list_del(&mpi->list);
450232f69e4bSDaniel Jurgens 				break;
450332f69e4bSDaniel Jurgens 			}
450432f69e4bSDaniel Jurgens 		}
450532f69e4bSDaniel Jurgens 		if (!bound) {
450632f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
450732f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
450832f69e4bSDaniel Jurgens 				    i + 1);
450932f69e4bSDaniel Jurgens 		}
451032f69e4bSDaniel Jurgens 	}
451132f69e4bSDaniel Jurgens 
451232f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
451332f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
451432f69e4bSDaniel Jurgens 	return err;
451532f69e4bSDaniel Jurgens }
451632f69e4bSDaniel Jurgens 
451732f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
451832f69e4bSDaniel Jurgens {
451932f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
452032f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
452132f69e4bSDaniel Jurgens 							  port_num + 1);
452232f69e4bSDaniel Jurgens 	int i;
452332f69e4bSDaniel Jurgens 
452432f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
452532f69e4bSDaniel Jurgens 		return;
452632f69e4bSDaniel Jurgens 
452732f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
452832f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
452932f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
453032f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
453132f69e4bSDaniel Jurgens 			if (i == port_num) {
453232f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
453332f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
453432f69e4bSDaniel Jurgens 			} else {
453532f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
453632f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
453732f69e4bSDaniel Jurgens 			}
453832f69e4bSDaniel Jurgens 		}
453932f69e4bSDaniel Jurgens 	}
454032f69e4bSDaniel Jurgens 
454132f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
454232f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
454332f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
454432f69e4bSDaniel Jurgens 
454532f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
454632f69e4bSDaniel Jurgens }
454732f69e4bSDaniel Jurgens 
4548b5ca15adSMark Bloch void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
4549e126ba97SEli Cohen {
455032f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
45513cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
45523cc297dbSMark Bloch 	cleanup_srcu_struct(&dev->mr_srcu);
45533cc297dbSMark Bloch #endif
455416c1975fSMark Bloch 	kfree(dev->port);
455516c1975fSMark Bloch }
455616c1975fSMark Bloch 
4557b5ca15adSMark Bloch int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
455816c1975fSMark Bloch {
455916c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
45604babcf97SAviv Heller 	const char *name;
4561e126ba97SEli Cohen 	int err;
456232f69e4bSDaniel Jurgens 	int i;
4563e126ba97SEli Cohen 
4564508562d6SDaniel Jurgens 	dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
45650837e86aSMark Bloch 			    GFP_KERNEL);
45660837e86aSMark Bloch 	if (!dev->port)
456716c1975fSMark Bloch 		return -ENOMEM;
45680837e86aSMark Bloch 
456932f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
457032f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
457132f69e4bSDaniel Jurgens 		rwlock_init(&dev->roce[i].netdev_lock);
457232f69e4bSDaniel Jurgens 	}
457332f69e4bSDaniel Jurgens 
457432f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
4575e126ba97SEli Cohen 	if (err)
45760837e86aSMark Bloch 		goto err_free_port;
4577e126ba97SEli Cohen 
457832f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
457932f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
458032f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
458132f69e4bSDaniel Jurgens 			if (err)
458232f69e4bSDaniel Jurgens 				break;
458332f69e4bSDaniel Jurgens 		}
458432f69e4bSDaniel Jurgens 	} else {
458532f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
458632f69e4bSDaniel Jurgens 	}
458732f69e4bSDaniel Jurgens 	if (err)
458832f69e4bSDaniel Jurgens 		goto err_mp;
458932f69e4bSDaniel Jurgens 
45901b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
4591e126ba97SEli Cohen 		get_ext_port_caps(dev);
4592e126ba97SEli Cohen 
45934babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
45944babcf97SAviv Heller 		name = "mlx5_%d";
45954babcf97SAviv Heller 	else
45964babcf97SAviv Heller 		name = "mlx5_bond_%d";
45974babcf97SAviv Heller 
45984babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
4599e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
4600e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
4601c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
4602e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
4603233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
4604233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
46059b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
4606e126ba97SEli Cohen 
46073cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
46083cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
46093cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
46103cc297dbSMark Bloch 
46113cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
46123cc297dbSMark Bloch 	err = init_srcu_struct(&dev->mr_srcu);
46133cc297dbSMark Bloch 	if (err)
46143cc297dbSMark Bloch 		goto err_free_port;
46153cc297dbSMark Bloch #endif
46163cc297dbSMark Bloch 
461716c1975fSMark Bloch 	return 0;
461832f69e4bSDaniel Jurgens err_mp:
461932f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
462016c1975fSMark Bloch 
462116c1975fSMark Bloch err_free_port:
462216c1975fSMark Bloch 	kfree(dev->port);
462316c1975fSMark Bloch 
462416c1975fSMark Bloch 	return -ENOMEM;
462516c1975fSMark Bloch }
462616c1975fSMark Bloch 
46279a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
46289a4ca38dSMark Bloch {
46299a4ca38dSMark Bloch 	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
46309a4ca38dSMark Bloch 
46319a4ca38dSMark Bloch 	if (!dev->flow_db)
46329a4ca38dSMark Bloch 		return -ENOMEM;
46339a4ca38dSMark Bloch 
46349a4ca38dSMark Bloch 	mutex_init(&dev->flow_db->lock);
46359a4ca38dSMark Bloch 
46369a4ca38dSMark Bloch 	return 0;
46379a4ca38dSMark Bloch }
46389a4ca38dSMark Bloch 
4639b5ca15adSMark Bloch int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
4640b5ca15adSMark Bloch {
4641b5ca15adSMark Bloch 	struct mlx5_ib_dev *nic_dev;
4642b5ca15adSMark Bloch 
4643b5ca15adSMark Bloch 	nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
4644b5ca15adSMark Bloch 
4645b5ca15adSMark Bloch 	if (!nic_dev)
4646b5ca15adSMark Bloch 		return -EINVAL;
4647b5ca15adSMark Bloch 
4648b5ca15adSMark Bloch 	dev->flow_db = nic_dev->flow_db;
4649b5ca15adSMark Bloch 
4650b5ca15adSMark Bloch 	return 0;
4651b5ca15adSMark Bloch }
4652b5ca15adSMark Bloch 
46539a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
46549a4ca38dSMark Bloch {
46559a4ca38dSMark Bloch 	kfree(dev->flow_db);
46569a4ca38dSMark Bloch }
46579a4ca38dSMark Bloch 
4658b5ca15adSMark Bloch int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
465916c1975fSMark Bloch {
466016c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
466116c1975fSMark Bloch 	int err;
466216c1975fSMark Bloch 
4663e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
4664e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
4665e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
4666e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
4667e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
4668e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
4669e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
467041c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
467141c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
4672e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
467356e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
4674e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
4675e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
4676e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
4677e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
4678e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
4679e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
4680e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
4681e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
4682e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
4683e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
4684e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
4685e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
4686e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
4687e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
4688e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
4689e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
4690e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
46911707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
4692d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
4693d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
46947d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
4695b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
4696b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
4697e126ba97SEli Cohen 
4698e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
4699ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
4700e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
47013cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
47023cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
4703e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
4704e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
4705e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
4706e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
4707e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
4708e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
4709e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
4710e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
4711e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
4712e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
4713e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
4714e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
4715e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
4716e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
4717e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
4718e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
4719e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
4720e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
4721e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
4722e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
4723e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
4724e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
4725e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
4726e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
4727e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
4728e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
4729e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
4730e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
4731e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
4732e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
473356e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
4734e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
4735e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
4736e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
4737e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
47389bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
47398a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
4740d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
4741c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
474240b24403SSagi Grimberg 	dev->ib_dev.get_vector_affinity	= mlx5_ib_get_vector_affinity;
47438e959601SNiranjana Vishwanathapura 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
4744693dfd5aSErez Shitrit 		dev->ib_dev.alloc_rdma_netdev	= mlx5_ib_alloc_rdma_netdev;
47458e959601SNiranjana Vishwanathapura 
4746eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
4747eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
4748eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
4749eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
4750eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
4751eff901d3SEli Cohen 	}
4752e126ba97SEli Cohen 
47537c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
47547c2344c3SMaor Gottlieb 
47556e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
47566e8484c5SMax Gurtovoy 
4757d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
4758d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
4759d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
4760d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
4761d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
4762d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4763d2370e0aSMatan Barak 	}
4764d2370e0aSMatan Barak 
4765938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
4766e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4767e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4768e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
4769e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4770e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4771e126ba97SEli Cohen 	}
4772e126ba97SEli Cohen 
4773038d2ef8SMaor Gottlieb 	dev->ib_dev.create_flow	= mlx5_ib_create_flow;
4774038d2ef8SMaor Gottlieb 	dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
477581e30880SYishai Hadas 	dev->ib_dev.uverbs_ex_cmd_mask |=
477681e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
477781e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
477881e30880SYishai Hadas 
4779e126ba97SEli Cohen 	err = init_node_data(dev);
4780e126ba97SEli Cohen 	if (err)
478116c1975fSMark Bloch 		return err;
4782e126ba97SEli Cohen 
4783c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4784e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
4785e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
4786c8b89924SMark Bloch 		mutex_init(&dev->lb_mutex);
4787c8b89924SMark Bloch 
478816c1975fSMark Bloch 	return 0;
478916c1975fSMark Bloch }
479016c1975fSMark Bloch 
47918e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
479216c1975fSMark Bloch {
47938e6efa3aSMark Bloch 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
47948e6efa3aSMark Bloch 	dev->ib_dev.query_port		= mlx5_ib_query_port;
47958e6efa3aSMark Bloch 
47968e6efa3aSMark Bloch 	return 0;
47978e6efa3aSMark Bloch }
47988e6efa3aSMark Bloch 
4799b5ca15adSMark Bloch int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
48008e6efa3aSMark Bloch {
48018e6efa3aSMark Bloch 	dev->ib_dev.get_port_immutable  = mlx5_port_rep_immutable;
48028e6efa3aSMark Bloch 	dev->ib_dev.query_port		= mlx5_ib_rep_query_port;
48038e6efa3aSMark Bloch 
48048e6efa3aSMark Bloch 	return 0;
48058e6efa3aSMark Bloch }
48068e6efa3aSMark Bloch 
48078e6efa3aSMark Bloch static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev,
48088e6efa3aSMark Bloch 					  u8 port_num)
48098e6efa3aSMark Bloch {
48107fd8aefbSDaniel Jurgens 	int i;
481116c1975fSMark Bloch 
48127fd8aefbSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
48137fd8aefbSDaniel Jurgens 		dev->roce[i].dev = dev;
48147fd8aefbSDaniel Jurgens 		dev->roce[i].native_port_num = i + 1;
48157fd8aefbSDaniel Jurgens 		dev->roce[i].last_port_state = IB_PORT_DOWN;
48167fd8aefbSDaniel Jurgens 	}
48177fd8aefbSDaniel Jurgens 
4818c11a226aSMark Bloch 	dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
4819c11a226aSMark Bloch 	dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
4820c11a226aSMark Bloch 	dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
4821c11a226aSMark Bloch 	dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
4822c11a226aSMark Bloch 	dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4823c11a226aSMark Bloch 	dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
48248e6efa3aSMark Bloch 
4825c11a226aSMark Bloch 	dev->ib_dev.uverbs_ex_cmd_mask |=
4826c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
4827c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
4828c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4829c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4830c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
48318e6efa3aSMark Bloch 
48328e6efa3aSMark Bloch 	return mlx5_add_netdev_notifier(dev, port_num);
48338e6efa3aSMark Bloch }
48348e6efa3aSMark Bloch 
48358e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
48368e6efa3aSMark Bloch {
48378e6efa3aSMark Bloch 	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
48388e6efa3aSMark Bloch 
48398e6efa3aSMark Bloch 	mlx5_remove_netdev_notifier(dev, port_num);
48408e6efa3aSMark Bloch }
48418e6efa3aSMark Bloch 
48428e6efa3aSMark Bloch int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
48438e6efa3aSMark Bloch {
48448e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
48458e6efa3aSMark Bloch 	enum rdma_link_layer ll;
48468e6efa3aSMark Bloch 	int port_type_cap;
48478e6efa3aSMark Bloch 	int err = 0;
48488e6efa3aSMark Bloch 	u8 port_num;
48498e6efa3aSMark Bloch 
48508e6efa3aSMark Bloch 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
48518e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
48528e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
48538e6efa3aSMark Bloch 
48548e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET)
48558e6efa3aSMark Bloch 		err = mlx5_ib_stage_common_roce_init(dev, port_num);
48568e6efa3aSMark Bloch 
485716c1975fSMark Bloch 	return err;
4858fc24fc5eSAchiad Shochat }
4859fc24fc5eSAchiad Shochat 
48608e6efa3aSMark Bloch void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
48618e6efa3aSMark Bloch {
48628e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
48638e6efa3aSMark Bloch }
48648e6efa3aSMark Bloch 
48658e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
48668e6efa3aSMark Bloch {
48678e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
48688e6efa3aSMark Bloch 	enum rdma_link_layer ll;
48698e6efa3aSMark Bloch 	int port_type_cap;
48708e6efa3aSMark Bloch 	u8 port_num;
48718e6efa3aSMark Bloch 	int err;
48728e6efa3aSMark Bloch 
48738e6efa3aSMark Bloch 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
48748e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
48758e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
48768e6efa3aSMark Bloch 
48778e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
48788e6efa3aSMark Bloch 		err = mlx5_ib_stage_common_roce_init(dev, port_num);
48798e6efa3aSMark Bloch 		if (err)
48808e6efa3aSMark Bloch 			return err;
48818e6efa3aSMark Bloch 
48828e6efa3aSMark Bloch 		err = mlx5_enable_eth(dev, port_num);
48838e6efa3aSMark Bloch 		if (err)
48848e6efa3aSMark Bloch 			goto cleanup;
48858e6efa3aSMark Bloch 	}
48868e6efa3aSMark Bloch 
488716c1975fSMark Bloch 	return 0;
48888e6efa3aSMark Bloch cleanup:
48898e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
48908e6efa3aSMark Bloch 
48918e6efa3aSMark Bloch 	return err;
489245bded2cSKamal Heib }
48936aec21f6SHaggai Eran 
489416c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
489516c1975fSMark Bloch {
489616c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
489716c1975fSMark Bloch 	enum rdma_link_layer ll;
489816c1975fSMark Bloch 	int port_type_cap;
489932f69e4bSDaniel Jurgens 	u8 port_num;
49004a2da0b8SParav Pandit 
490132f69e4bSDaniel Jurgens 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
490216c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
490316c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
490416c1975fSMark Bloch 
490516c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
490616c1975fSMark Bloch 		mlx5_disable_eth(dev);
49078e6efa3aSMark Bloch 		mlx5_ib_stage_common_roce_cleanup(dev);
490816c1975fSMark Bloch 	}
490916c1975fSMark Bloch }
491016c1975fSMark Bloch 
4911b5ca15adSMark Bloch int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
491216c1975fSMark Bloch {
491316c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
491416c1975fSMark Bloch }
491516c1975fSMark Bloch 
4916b5ca15adSMark Bloch void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
491716c1975fSMark Bloch {
491816c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
491916c1975fSMark Bloch }
492016c1975fSMark Bloch 
492116c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
492216c1975fSMark Bloch {
492307321b3cSMark Bloch 	mlx5_ib_internal_fill_odp_caps(dev);
492407321b3cSMark Bloch 
492516c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
492616c1975fSMark Bloch }
492716c1975fSMark Bloch 
4928b5ca15adSMark Bloch int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
492916c1975fSMark Bloch {
49305e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
49315e1e7612SMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
49325e1e7612SMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
49335e1e7612SMark Bloch 
493416c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
49355e1e7612SMark Bloch 	}
493616c1975fSMark Bloch 
493716c1975fSMark Bloch 	return 0;
493816c1975fSMark Bloch }
493916c1975fSMark Bloch 
4940b5ca15adSMark Bloch void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
494116c1975fSMark Bloch {
494216c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
494316c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
494416c1975fSMark Bloch }
494516c1975fSMark Bloch 
494616c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
494716c1975fSMark Bloch {
4948a9e546e7SParav Pandit 	return mlx5_ib_init_cong_debugfs(dev,
4949a9e546e7SParav Pandit 					 mlx5_core_native_port_num(dev->mdev) - 1);
495016c1975fSMark Bloch }
495116c1975fSMark Bloch 
495216c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
495316c1975fSMark Bloch {
4954a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
4955a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
495616c1975fSMark Bloch }
495716c1975fSMark Bloch 
495816c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
495916c1975fSMark Bloch {
49605fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
49615fe9dec0SEli Cohen 	if (!dev->mdev->priv.uar)
496216c1975fSMark Bloch 		return -ENOMEM;
496316c1975fSMark Bloch 	return 0;
496416c1975fSMark Bloch }
496516c1975fSMark Bloch 
496616c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
496716c1975fSMark Bloch {
496816c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
496916c1975fSMark Bloch }
497016c1975fSMark Bloch 
4971b5ca15adSMark Bloch int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
497216c1975fSMark Bloch {
497316c1975fSMark Bloch 	int err;
49745fe9dec0SEli Cohen 
49755fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
49765fe9dec0SEli Cohen 	if (err)
497716c1975fSMark Bloch 		return err;
49785fe9dec0SEli Cohen 
49795fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
49805fe9dec0SEli Cohen 	if (err)
498116c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
49825fe9dec0SEli Cohen 
498316c1975fSMark Bloch 	return err;
498416c1975fSMark Bloch }
49850837e86aSMark Bloch 
4986b5ca15adSMark Bloch void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
498716c1975fSMark Bloch {
498816c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
498916c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
499016c1975fSMark Bloch }
4991e126ba97SEli Cohen 
4992b5ca15adSMark Bloch int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
499316c1975fSMark Bloch {
499416c1975fSMark Bloch 	return ib_register_device(&dev->ib_dev, NULL);
499516c1975fSMark Bloch }
499616c1975fSMark Bloch 
4997b5ca15adSMark Bloch void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
499816c1975fSMark Bloch {
499916c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
500016c1975fSMark Bloch }
500116c1975fSMark Bloch 
5002b5ca15adSMark Bloch int mlx5_ib_stage_umr_res_init(struct mlx5_ib_dev *dev)
500316c1975fSMark Bloch {
500416c1975fSMark Bloch 	return create_umr_res(dev);
500516c1975fSMark Bloch }
500616c1975fSMark Bloch 
5007b5ca15adSMark Bloch void mlx5_ib_stage_umr_res_cleanup(struct mlx5_ib_dev *dev)
500816c1975fSMark Bloch {
500916c1975fSMark Bloch 	destroy_umrc_res(dev);
501016c1975fSMark Bloch }
501116c1975fSMark Bloch 
501216c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
501316c1975fSMark Bloch {
501403404e8aSMaor Gottlieb 	init_delay_drop(dev);
501503404e8aSMaor Gottlieb 
501616c1975fSMark Bloch 	return 0;
501716c1975fSMark Bloch }
501816c1975fSMark Bloch 
501916c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
502016c1975fSMark Bloch {
502116c1975fSMark Bloch 	cancel_delay_drop(dev);
502216c1975fSMark Bloch }
502316c1975fSMark Bloch 
5024b5ca15adSMark Bloch int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev)
502516c1975fSMark Bloch {
502616c1975fSMark Bloch 	int err;
502716c1975fSMark Bloch 	int i;
502816c1975fSMark Bloch 
5029e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
5030281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
5031281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
5032281d1a92SWei Yongjun 		if (err)
503316c1975fSMark Bloch 			return err;
5034e126ba97SEli Cohen 	}
5035e126ba97SEli Cohen 
503616c1975fSMark Bloch 	return 0;
503716c1975fSMark Bloch }
503816c1975fSMark Bloch 
5039fc385b7aSMark Bloch static int mlx5_ib_stage_rep_reg_init(struct mlx5_ib_dev *dev)
5040fc385b7aSMark Bloch {
5041fc385b7aSMark Bloch 	mlx5_ib_register_vport_reps(dev);
5042fc385b7aSMark Bloch 
5043fc385b7aSMark Bloch 	return 0;
5044fc385b7aSMark Bloch }
5045fc385b7aSMark Bloch 
5046fc385b7aSMark Bloch static void mlx5_ib_stage_rep_reg_cleanup(struct mlx5_ib_dev *dev)
5047fc385b7aSMark Bloch {
5048fc385b7aSMark Bloch 	mlx5_ib_unregister_vport_reps(dev);
5049fc385b7aSMark Bloch }
5050fc385b7aSMark Bloch 
5051b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
505216c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
505316c1975fSMark Bloch 		      int stage)
505416c1975fSMark Bloch {
505516c1975fSMark Bloch 	/* Number of stages to cleanup */
505616c1975fSMark Bloch 	while (stage) {
505716c1975fSMark Bloch 		stage--;
505816c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
505916c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
506016c1975fSMark Bloch 	}
506116c1975fSMark Bloch 
506216c1975fSMark Bloch 	ib_dealloc_device((struct ib_device *)dev);
506316c1975fSMark Bloch }
506416c1975fSMark Bloch 
506532f69e4bSDaniel Jurgens static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num);
506632f69e4bSDaniel Jurgens 
5067b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
506816c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
506916c1975fSMark Bloch {
507016c1975fSMark Bloch 	int err;
507116c1975fSMark Bloch 	int i;
507216c1975fSMark Bloch 
507316c1975fSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
507416c1975fSMark Bloch 
507516c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
507616c1975fSMark Bloch 		if (profile->stage[i].init) {
507716c1975fSMark Bloch 			err = profile->stage[i].init(dev);
507816c1975fSMark Bloch 			if (err)
507916c1975fSMark Bloch 				goto err_out;
508016c1975fSMark Bloch 		}
508116c1975fSMark Bloch 	}
508216c1975fSMark Bloch 
508316c1975fSMark Bloch 	dev->profile = profile;
5084e126ba97SEli Cohen 	dev->ib_active = true;
5085e126ba97SEli Cohen 
50869603b61dSJack Morgenstein 	return dev;
5087e126ba97SEli Cohen 
508816c1975fSMark Bloch err_out:
508916c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
5090e126ba97SEli Cohen 
50919603b61dSJack Morgenstein 	return NULL;
5092e126ba97SEli Cohen }
5093e126ba97SEli Cohen 
509416c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
509516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
509616c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
509716c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
50989a4ca38dSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
50999a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_init,
51009a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
510116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
510216c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
510316c1975fSMark Bloch 		     NULL),
51048e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
51058e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
51068e6efa3aSMark Bloch 		     NULL),
510716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
510816c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
510916c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
511016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
511116c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
511216c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
511316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
511416c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
51153cc297dbSMark Bloch 		     NULL),
511616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
511716c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
511816c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
511916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
512016c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
512116c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
512216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
512316c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
512416c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
512516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
512616c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
512716c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
512816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
512916c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
513016c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
513116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UMR_RESOURCES,
513216c1975fSMark Bloch 		     mlx5_ib_stage_umr_res_init,
513316c1975fSMark Bloch 		     mlx5_ib_stage_umr_res_cleanup),
513416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
513516c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
513616c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
513716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
513816c1975fSMark Bloch 		     mlx5_ib_stage_class_attr_init,
513916c1975fSMark Bloch 		     NULL),
514016c1975fSMark Bloch };
514116c1975fSMark Bloch 
5142b5ca15adSMark Bloch static const struct mlx5_ib_profile nic_rep_profile = {
5143b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
5144b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
5145b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
5146b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
5147b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_init,
5148b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
5149b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
5150b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
5151b5ca15adSMark Bloch 		     NULL),
5152b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
5153b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_non_default_cb,
5154b5ca15adSMark Bloch 		     NULL),
5155b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
5156b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_init,
5157b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_cleanup),
5158b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
5159b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_init,
5160b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
5161b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
5162b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_init,
5163b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
5164b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
5165b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
5166b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
5167b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
5168b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
5169b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
5170b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
5171b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
5172b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
5173b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UMR_RESOURCES,
5174b5ca15adSMark Bloch 		     mlx5_ib_stage_umr_res_init,
5175b5ca15adSMark Bloch 		     mlx5_ib_stage_umr_res_cleanup),
5176b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
5177b5ca15adSMark Bloch 		     mlx5_ib_stage_class_attr_init,
5178b5ca15adSMark Bloch 		     NULL),
5179b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_REP_REG,
5180b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_reg_init,
5181b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_reg_cleanup),
5182b5ca15adSMark Bloch };
5183b5ca15adSMark Bloch 
518432f69e4bSDaniel Jurgens static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num)
518532f69e4bSDaniel Jurgens {
518632f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
518732f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
518832f69e4bSDaniel Jurgens 	bool bound = false;
518932f69e4bSDaniel Jurgens 	int err;
519032f69e4bSDaniel Jurgens 
519132f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
519232f69e4bSDaniel Jurgens 	if (!mpi)
519332f69e4bSDaniel Jurgens 		return NULL;
519432f69e4bSDaniel Jurgens 
519532f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
519632f69e4bSDaniel Jurgens 
519732f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
519832f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
519932f69e4bSDaniel Jurgens 	if (err) {
520032f69e4bSDaniel Jurgens 		kfree(mpi);
520132f69e4bSDaniel Jurgens 		return NULL;
520232f69e4bSDaniel Jurgens 	}
520332f69e4bSDaniel Jurgens 
520432f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
520532f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
520632f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
520732f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
520832f69e4bSDaniel Jurgens 
520932f69e4bSDaniel Jurgens 		if (bound) {
521032f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
521132f69e4bSDaniel Jurgens 			break;
521232f69e4bSDaniel Jurgens 		}
521332f69e4bSDaniel Jurgens 	}
521432f69e4bSDaniel Jurgens 
521532f69e4bSDaniel Jurgens 	if (!bound) {
521632f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
521732f69e4bSDaniel Jurgens 		dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
521832f69e4bSDaniel Jurgens 	} else {
521932f69e4bSDaniel Jurgens 		mlx5_ib_dbg(dev, "bound port %u\n", port_num + 1);
522032f69e4bSDaniel Jurgens 	}
522132f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
522232f69e4bSDaniel Jurgens 
522332f69e4bSDaniel Jurgens 	return mpi;
522432f69e4bSDaniel Jurgens }
522532f69e4bSDaniel Jurgens 
522616c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
522716c1975fSMark Bloch {
522832f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
5229b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
523032f69e4bSDaniel Jurgens 	int port_type_cap;
523132f69e4bSDaniel Jurgens 
5232b5ca15adSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
5233b5ca15adSMark Bloch 
523432f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
523532f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
523632f69e4bSDaniel Jurgens 
523732f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET) {
523832f69e4bSDaniel Jurgens 		u8 port_num = mlx5_core_native_port_num(mdev) - 1;
523932f69e4bSDaniel Jurgens 
524032f69e4bSDaniel Jurgens 		return mlx5_ib_add_slave_port(mdev, port_num);
524132f69e4bSDaniel Jurgens 	}
524232f69e4bSDaniel Jurgens 
5243b5ca15adSMark Bloch 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
5244b5ca15adSMark Bloch 	if (!dev)
5245b5ca15adSMark Bloch 		return NULL;
5246b5ca15adSMark Bloch 
5247b5ca15adSMark Bloch 	dev->mdev = mdev;
5248b5ca15adSMark Bloch 	dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
5249b5ca15adSMark Bloch 			     MLX5_CAP_GEN(mdev, num_vhca_ports));
5250b5ca15adSMark Bloch 
5251b5ca15adSMark Bloch 	if (MLX5_VPORT_MANAGER(mdev) &&
5252b5ca15adSMark Bloch 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
5253b5ca15adSMark Bloch 		dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0);
5254b5ca15adSMark Bloch 
5255b5ca15adSMark Bloch 		return __mlx5_ib_add(dev, &nic_rep_profile);
5256b5ca15adSMark Bloch 	}
5257b5ca15adSMark Bloch 
5258b5ca15adSMark Bloch 	return __mlx5_ib_add(dev, &pf_profile);
525916c1975fSMark Bloch }
526016c1975fSMark Bloch 
52619603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
5262e126ba97SEli Cohen {
526332f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
526432f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
52656aec21f6SHaggai Eran 
526632f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
526732f69e4bSDaniel Jurgens 		mpi = context;
526832f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
526932f69e4bSDaniel Jurgens 		if (mpi->ibdev)
527032f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
527132f69e4bSDaniel Jurgens 		list_del(&mpi->list);
527232f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
527332f69e4bSDaniel Jurgens 		return;
527432f69e4bSDaniel Jurgens 	}
527532f69e4bSDaniel Jurgens 
527632f69e4bSDaniel Jurgens 	dev = context;
527716c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
5278e126ba97SEli Cohen }
5279e126ba97SEli Cohen 
52809603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
52819603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
52829603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
52839603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
5284d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5285d9aaed83SArtemy Kovalyov 	.pfault		= mlx5_ib_pfault,
5286d9aaed83SArtemy Kovalyov #endif
528764613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
5288e126ba97SEli Cohen };
5289e126ba97SEli Cohen 
5290*c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
5291*c44ef998SIlya Lesokhin {
5292*c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
5293*c44ef998SIlya Lesokhin 	return xlt_emergency_page;
5294*c44ef998SIlya Lesokhin }
5295*c44ef998SIlya Lesokhin 
5296*c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
5297*c44ef998SIlya Lesokhin {
5298*c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
5299*c44ef998SIlya Lesokhin }
5300*c44ef998SIlya Lesokhin 
5301e126ba97SEli Cohen static int __init mlx5_ib_init(void)
5302e126ba97SEli Cohen {
53036aec21f6SHaggai Eran 	int err;
53046aec21f6SHaggai Eran 
5305*c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
5306*c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
5307d69a24e0SDaniel Jurgens 		return -ENOMEM;
5308d69a24e0SDaniel Jurgens 
5309*c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
5310*c44ef998SIlya Lesokhin 
5311*c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
5312*c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
5313*c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
5314*c44ef998SIlya Lesokhin 		return -ENOMEM;
5315*c44ef998SIlya Lesokhin 	}
5316*c44ef998SIlya Lesokhin 
531781713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
53189603b61dSJack Morgenstein 
53196aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
53206aec21f6SHaggai Eran 
53216aec21f6SHaggai Eran 	return err;
5322e126ba97SEli Cohen }
5323e126ba97SEli Cohen 
5324e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
5325e126ba97SEli Cohen {
53269603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
5327d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
5328*c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
5329*c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
5330e126ba97SEli Cohen }
5331e126ba97SEli Cohen 
5332e126ba97SEli Cohen module_init(mlx5_ib_init);
5333e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
5334