xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 37da2a03c036538a5a79766d74bfcf5b873e5cad)
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>
4124da0016SAriel Levkovich #include <linux/bitmap.h>
4237aa5c36SGuy Levi #if defined(CONFIG_X86)
4337aa5c36SGuy Levi #include <asm/pat.h>
4437aa5c36SGuy Levi #endif
45e126ba97SEli Cohen #include <linux/sched.h>
466e84f315SIngo Molnar #include <linux/sched/mm.h>
470881e7bdSIngo Molnar #include <linux/sched/task.h>
487c2344c3SMaor Gottlieb #include <linux/delay.h>
49e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
503f89a643SAchiad Shochat #include <rdma/ib_addr.h>
512811ba51SAchiad Shochat #include <rdma/ib_cache.h>
52ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
531b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
5472c7fe90SPravin Shedge #include <linux/mlx5/fs.h>
557c2344c3SMaor Gottlieb #include <linux/list.h>
56e126ba97SEli Cohen #include <rdma/ib_smi.h>
57e126ba97SEli Cohen #include <rdma/ib_umem.h>
58038d2ef8SMaor Gottlieb #include <linux/in.h>
59038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
60e126ba97SEli Cohen #include "mlx5_ib.h"
61fc385b7aSMark Bloch #include "ib_rep.h"
62e1f24a79SParav Pandit #include "cmd.h"
633346c487SBoris Pismenny #include <linux/mlx5/fs_helpers.h>
64c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h>
658c84660bSMatan Barak #include <rdma/uverbs_std_types.h>
66c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h>
67c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h>
688c84660bSMatan Barak 
698c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
708c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
71e126ba97SEli Cohen 
72e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
73b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0"
74e126ba97SEli Cohen 
75e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
76e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
77e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
78e126ba97SEli Cohen 
79e126ba97SEli Cohen static char mlx5_version[] =
80e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
81b359911dSTariq Toukan 	DRIVER_VERSION "\n";
82e126ba97SEli Cohen 
83d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
84d69a24e0SDaniel Jurgens 	struct work_struct	work;
85d69a24e0SDaniel Jurgens 	struct mlx5_core_dev	*dev;
86d69a24e0SDaniel Jurgens 	void			*context;
87d69a24e0SDaniel Jurgens 	enum mlx5_dev_event	event;
88d69a24e0SDaniel Jurgens 	unsigned long		param;
89d69a24e0SDaniel Jurgens };
90d69a24e0SDaniel Jurgens 
91da7525d2SEran Ben Elisha enum {
92da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
93da7525d2SEran Ben Elisha };
941b5daf11SMajd Dibbiny 
95d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
9632f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
9732f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
9832f69e4bSDaniel Jurgens /*
9932f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
10032f69e4bSDaniel Jurgens  */
10132f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
10232f69e4bSDaniel Jurgens 
103c44ef998SIlya Lesokhin /* We can't use an array for xlt_emergency_page because dma_map_single
104c44ef998SIlya Lesokhin  * doesn't work on kernel modules memory
105c44ef998SIlya Lesokhin  */
106c44ef998SIlya Lesokhin static unsigned long xlt_emergency_page;
107c44ef998SIlya Lesokhin static struct mutex xlt_emergency_page_mutex;
108c44ef998SIlya Lesokhin 
10932f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
11032f69e4bSDaniel Jurgens {
11132f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
11232f69e4bSDaniel Jurgens 
11332f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
11432f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
11532f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
11632f69e4bSDaniel Jurgens 	return dev;
11732f69e4bSDaniel Jurgens }
11832f69e4bSDaniel Jurgens 
1191b5daf11SMajd Dibbiny static enum rdma_link_layer
120ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
1211b5daf11SMajd Dibbiny {
122ebd61f68SAchiad Shochat 	switch (port_type_cap) {
1231b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
1241b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
1251b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
1261b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
1271b5daf11SMajd Dibbiny 	default:
1281b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1291b5daf11SMajd Dibbiny 	}
1301b5daf11SMajd Dibbiny }
1311b5daf11SMajd Dibbiny 
132ebd61f68SAchiad Shochat static enum rdma_link_layer
133ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
134ebd61f68SAchiad Shochat {
135ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
136ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
137ebd61f68SAchiad Shochat 
138ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
139ebd61f68SAchiad Shochat }
140ebd61f68SAchiad Shochat 
141fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
142fd65f1b8SMoni Shoua 			  u8 port_num,
143fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
144fd65f1b8SMoni Shoua {
145fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
146fd65f1b8SMoni Shoua 	int ret;
147fd65f1b8SMoni Shoua 
148fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1498e6efa3aSMark Bloch 	ret = ibdev->query_port(ibdev, port_num, &attr);
150fd65f1b8SMoni Shoua 	if (!ret)
151fd65f1b8SMoni Shoua 		*state = attr.state;
152fd65f1b8SMoni Shoua 	return ret;
153fd65f1b8SMoni Shoua }
154fd65f1b8SMoni Shoua 
155fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
156fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
157fc24fc5eSAchiad Shochat {
1587fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
159fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1607fd8aefbSDaniel Jurgens 	u8 port_num = roce->native_port_num;
1617fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1627fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1637fd8aefbSDaniel Jurgens 
1647fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
16532f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
16632f69e4bSDaniel Jurgens 	if (!mdev)
16732f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
168fc24fc5eSAchiad Shochat 
1695ec8c83eSAviv Heller 	switch (event) {
1705ec8c83eSAviv Heller 	case NETDEV_REGISTER:
1715ec8c83eSAviv Heller 	case NETDEV_UNREGISTER:
1727fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
173bcf87f1dSMark Bloch 		if (ibdev->rep) {
174bcf87f1dSMark Bloch 			struct mlx5_eswitch *esw = ibdev->mdev->priv.eswitch;
175bcf87f1dSMark Bloch 			struct net_device *rep_ndev;
1767fd8aefbSDaniel Jurgens 
177bcf87f1dSMark Bloch 			rep_ndev = mlx5_ib_get_rep_netdev(esw,
178bcf87f1dSMark Bloch 							  ibdev->rep->vport);
179bcf87f1dSMark Bloch 			if (rep_ndev == ndev)
1807fd8aefbSDaniel Jurgens 				roce->netdev = (event == NETDEV_UNREGISTER) ?
1815ec8c83eSAviv Heller 					NULL : ndev;
18284a6a7a9SParav Pandit 		} else if (ndev->dev.parent == &mdev->pdev->dev) {
183bcf87f1dSMark Bloch 			roce->netdev = (event == NETDEV_UNREGISTER) ?
184bcf87f1dSMark Bloch 				NULL : ndev;
185bcf87f1dSMark Bloch 		}
1867fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
1875ec8c83eSAviv Heller 		break;
1885ec8c83eSAviv Heller 
189fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
1905ec8c83eSAviv Heller 	case NETDEV_UP:
19188621dfeSAviv Heller 	case NETDEV_DOWN: {
1927fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
19388621dfeSAviv Heller 		struct net_device *upper = NULL;
19488621dfeSAviv Heller 
19588621dfeSAviv Heller 		if (lag_ndev) {
19688621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
19788621dfeSAviv Heller 			dev_put(lag_ndev);
19888621dfeSAviv Heller 		}
19988621dfeSAviv Heller 
2007fd8aefbSDaniel Jurgens 		if ((upper == ndev || (!upper && ndev == roce->netdev))
20188621dfeSAviv Heller 		    && ibdev->ib_active) {
202626bc02dSBart Van Assche 			struct ib_event ibev = { };
203fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2045ec8c83eSAviv Heller 
2057fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2067fd8aefbSDaniel Jurgens 					   &port_state))
2077fd8aefbSDaniel Jurgens 				goto done;
208fd65f1b8SMoni Shoua 
2097fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2107fd8aefbSDaniel Jurgens 				goto done;
211fd65f1b8SMoni Shoua 
2127fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2135ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
214fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
215fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
216fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
217fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
218fd65f1b8SMoni Shoua 			else
2197fd8aefbSDaniel Jurgens 				goto done;
220fd65f1b8SMoni Shoua 
2217fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2225ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2235ec8c83eSAviv Heller 		}
2245ec8c83eSAviv Heller 		break;
22588621dfeSAviv Heller 	}
2265ec8c83eSAviv Heller 
2275ec8c83eSAviv Heller 	default:
2285ec8c83eSAviv Heller 		break;
2295ec8c83eSAviv Heller 	}
2307fd8aefbSDaniel Jurgens done:
23132f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
232fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
233fc24fc5eSAchiad Shochat }
234fc24fc5eSAchiad Shochat 
235fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
236fc24fc5eSAchiad Shochat 					     u8 port_num)
237fc24fc5eSAchiad Shochat {
238fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
239fc24fc5eSAchiad Shochat 	struct net_device *ndev;
24032f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
241fc24fc5eSAchiad Shochat 
24232f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
24332f69e4bSDaniel Jurgens 	if (!mdev)
24432f69e4bSDaniel Jurgens 		return NULL;
24532f69e4bSDaniel Jurgens 
24632f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
24788621dfeSAviv Heller 	if (ndev)
24832f69e4bSDaniel Jurgens 		goto out;
24988621dfeSAviv Heller 
250fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
251fc24fc5eSAchiad Shochat 	 */
2527fd8aefbSDaniel Jurgens 	read_lock(&ibdev->roce[port_num - 1].netdev_lock);
2537fd8aefbSDaniel Jurgens 	ndev = ibdev->roce[port_num - 1].netdev;
254fc24fc5eSAchiad Shochat 	if (ndev)
255fc24fc5eSAchiad Shochat 		dev_hold(ndev);
2567fd8aefbSDaniel Jurgens 	read_unlock(&ibdev->roce[port_num - 1].netdev_lock);
257fc24fc5eSAchiad Shochat 
25832f69e4bSDaniel Jurgens out:
25932f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
260fc24fc5eSAchiad Shochat 	return ndev;
261fc24fc5eSAchiad Shochat }
262fc24fc5eSAchiad Shochat 
26332f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
26432f69e4bSDaniel Jurgens 						   u8 ib_port_num,
26532f69e4bSDaniel Jurgens 						   u8 *native_port_num)
26632f69e4bSDaniel Jurgens {
26732f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
26832f69e4bSDaniel Jurgens 							  ib_port_num);
26932f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
27032f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
27132f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
27232f69e4bSDaniel Jurgens 
273210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
274210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
275210b1f78SMark Bloch 		if (native_port_num)
276210b1f78SMark Bloch 			*native_port_num = ib_port_num;
277210b1f78SMark Bloch 		return ibdev->mdev;
278210b1f78SMark Bloch 	}
279210b1f78SMark Bloch 
28032f69e4bSDaniel Jurgens 	if (native_port_num)
28132f69e4bSDaniel Jurgens 		*native_port_num = 1;
28232f69e4bSDaniel Jurgens 
28332f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
28432f69e4bSDaniel Jurgens 	if (!port)
28532f69e4bSDaniel Jurgens 		return NULL;
28632f69e4bSDaniel Jurgens 
28732f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
28832f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
28932f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
29032f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
29132f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
29232f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
29332f69e4bSDaniel Jurgens 		 */
29432f69e4bSDaniel Jurgens 		if (!mpi->is_master)
29532f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
29632f69e4bSDaniel Jurgens 	}
29732f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
29832f69e4bSDaniel Jurgens 
29932f69e4bSDaniel Jurgens 	return mdev;
30032f69e4bSDaniel Jurgens }
30132f69e4bSDaniel Jurgens 
30232f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
30332f69e4bSDaniel Jurgens {
30432f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
30532f69e4bSDaniel Jurgens 							  port_num);
30632f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
30732f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
30832f69e4bSDaniel Jurgens 
30932f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
31032f69e4bSDaniel Jurgens 		return;
31132f69e4bSDaniel Jurgens 
31232f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
31332f69e4bSDaniel Jurgens 
31432f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
31532f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
31632f69e4bSDaniel Jurgens 	if (mpi->is_master)
31732f69e4bSDaniel Jurgens 		goto out;
31832f69e4bSDaniel Jurgens 
31932f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
32032f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
32132f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
32232f69e4bSDaniel Jurgens out:
32332f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
32432f69e4bSDaniel Jurgens }
32532f69e4bSDaniel Jurgens 
326f1b65df5SNoa Osherovich static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
327f1b65df5SNoa Osherovich 				    u8 *active_width)
328f1b65df5SNoa Osherovich {
329f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
330f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
331f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
332f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
333f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
334f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
335f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
336f1b65df5SNoa Osherovich 		break;
337f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
338f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
339f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
340f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
341f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
342f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
343f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
344f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
345f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
346f1b65df5SNoa Osherovich 		break;
347f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
348f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
349f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
350f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
351f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
352f1b65df5SNoa Osherovich 		break;
353f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
354f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
355f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
356f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
357f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
358f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
359f1b65df5SNoa Osherovich 		break;
360f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
361f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
362f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
363f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
364f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
365f1b65df5SNoa Osherovich 		break;
366f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
367f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
368f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
369f1b65df5SNoa Osherovich 		break;
370f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
371f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
372f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
373f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
374f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
375f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
376f1b65df5SNoa Osherovich 		break;
377f1b65df5SNoa Osherovich 	default:
378f1b65df5SNoa Osherovich 		return -EINVAL;
379f1b65df5SNoa Osherovich 	}
380f1b65df5SNoa Osherovich 
381f1b65df5SNoa Osherovich 	return 0;
382f1b65df5SNoa Osherovich }
383f1b65df5SNoa Osherovich 
384095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
3853f89a643SAchiad Shochat 				struct ib_port_attr *props)
3863f89a643SAchiad Shochat {
3873f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
388da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
38988621dfeSAviv Heller 	struct net_device *ndev, *upper;
3903f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
391b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
392c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
393f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
394b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
395095b0927SIlan Tayari 	int err;
3963f89a643SAchiad Shochat 
397b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
398b3cbd6f0SDaniel Jurgens 	if (!mdev) {
399b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
400b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
401b3cbd6f0SDaniel Jurgens 		 */
402b3cbd6f0SDaniel Jurgens 		put_mdev = false;
403b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
404b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
405b3cbd6f0SDaniel Jurgens 		port_num = 1;
406b3cbd6f0SDaniel Jurgens 	}
407b3cbd6f0SDaniel Jurgens 
408f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
409f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
41050f22fd8SNoa Osherovich 	 */
411b3cbd6f0SDaniel Jurgens 	err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper,
412b3cbd6f0SDaniel Jurgens 					     mdev_port_num);
413095b0927SIlan Tayari 	if (err)
414b3cbd6f0SDaniel Jurgens 		goto out;
415f1b65df5SNoa Osherovich 
4167672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
4177672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
4187672ed33SHonggang Li 
419f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
420f1b65df5SNoa Osherovich 				 &props->active_width);
4213f89a643SAchiad Shochat 
4223f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
4233f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
4243f89a643SAchiad Shochat 
4253f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
4263f89a643SAchiad Shochat 						roce_address_table_size);
4273f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
4283f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
4293f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
4303f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
4313f89a643SAchiad Shochat 	props->phys_state       = 3;
4323f89a643SAchiad Shochat 
433b3cbd6f0SDaniel Jurgens 	mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
434c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
4353f89a643SAchiad Shochat 
436b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
437b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
438b3cbd6f0SDaniel Jurgens 		goto out;
439b3cbd6f0SDaniel Jurgens 
4403f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
4413f89a643SAchiad Shochat 	if (!ndev)
442b3cbd6f0SDaniel Jurgens 		goto out;
4433f89a643SAchiad Shochat 
44488621dfeSAviv Heller 	if (mlx5_lag_is_active(dev->mdev)) {
44588621dfeSAviv Heller 		rcu_read_lock();
44688621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
44788621dfeSAviv Heller 		if (upper) {
44888621dfeSAviv Heller 			dev_put(ndev);
44988621dfeSAviv Heller 			ndev = upper;
45088621dfeSAviv Heller 			dev_hold(ndev);
45188621dfeSAviv Heller 		}
45288621dfeSAviv Heller 		rcu_read_unlock();
45388621dfeSAviv Heller 	}
45488621dfeSAviv Heller 
4553f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
4563f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
4573f89a643SAchiad Shochat 		props->phys_state = 5;
4583f89a643SAchiad Shochat 	}
4593f89a643SAchiad Shochat 
4603f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
4613f89a643SAchiad Shochat 
4623f89a643SAchiad Shochat 	dev_put(ndev);
4633f89a643SAchiad Shochat 
4643f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
465b3cbd6f0SDaniel Jurgens out:
466b3cbd6f0SDaniel Jurgens 	if (put_mdev)
467b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
468b3cbd6f0SDaniel Jurgens 	return err;
4693f89a643SAchiad Shochat }
4703f89a643SAchiad Shochat 
471095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
472095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
473095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
4743cca2606SAchiad Shochat {
475095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
476095b0927SIlan Tayari 	u8 roce_version = 0;
477095b0927SIlan Tayari 	u8 roce_l3_type = 0;
478095b0927SIlan Tayari 	bool vlan = false;
479095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
480095b0927SIlan Tayari 	u16 vlan_id = 0;
4813cca2606SAchiad Shochat 
482095b0927SIlan Tayari 	if (gid) {
483095b0927SIlan Tayari 		gid_type = attr->gid_type;
484095b0927SIlan Tayari 		ether_addr_copy(mac, attr->ndev->dev_addr);
4853cca2606SAchiad Shochat 
4863cca2606SAchiad Shochat 		if (is_vlan_dev(attr->ndev)) {
487095b0927SIlan Tayari 			vlan = true;
488095b0927SIlan Tayari 			vlan_id = vlan_dev_vlan_id(attr->ndev);
489095b0927SIlan Tayari 		}
4903cca2606SAchiad Shochat 	}
4913cca2606SAchiad Shochat 
492095b0927SIlan Tayari 	switch (gid_type) {
4933cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
494095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
4953cca2606SAchiad Shochat 		break;
4963cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
497095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
498095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
499095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
500095b0927SIlan Tayari 		else
501095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
5023cca2606SAchiad Shochat 		break;
5033cca2606SAchiad Shochat 
5043cca2606SAchiad Shochat 	default:
505095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
5063cca2606SAchiad Shochat 	}
5073cca2606SAchiad Shochat 
508095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
509095b0927SIlan Tayari 				      roce_l3_type, gid->raw, mac, vlan,
510cfe4e37fSDaniel Jurgens 				      vlan_id, port_num);
5113cca2606SAchiad Shochat }
5123cca2606SAchiad Shochat 
513414448d2SParav Pandit static int mlx5_ib_add_gid(const union ib_gid *gid,
5143cca2606SAchiad Shochat 			   const struct ib_gid_attr *attr,
5153cca2606SAchiad Shochat 			   __always_unused void **context)
5163cca2606SAchiad Shochat {
517414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
518414448d2SParav Pandit 			     attr->index, gid, attr);
5193cca2606SAchiad Shochat }
5203cca2606SAchiad Shochat 
521414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
522414448d2SParav Pandit 			   __always_unused void **context)
5233cca2606SAchiad Shochat {
524414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
525414448d2SParav Pandit 			     attr->index, NULL, NULL);
5263cca2606SAchiad Shochat }
5273cca2606SAchiad Shochat 
5282811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
5292811ba51SAchiad Shochat 			       int index)
5302811ba51SAchiad Shochat {
5312811ba51SAchiad Shochat 	struct ib_gid_attr attr;
5322811ba51SAchiad Shochat 	union ib_gid gid;
5332811ba51SAchiad Shochat 
5342811ba51SAchiad Shochat 	if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
5352811ba51SAchiad Shochat 		return 0;
5362811ba51SAchiad Shochat 
5372811ba51SAchiad Shochat 	dev_put(attr.ndev);
5382811ba51SAchiad Shochat 
5392811ba51SAchiad Shochat 	if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
5402811ba51SAchiad Shochat 		return 0;
5412811ba51SAchiad Shochat 
5422811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
5432811ba51SAchiad Shochat }
5442811ba51SAchiad Shochat 
545ed88451eSMajd Dibbiny int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
546ed88451eSMajd Dibbiny 			   int index, enum ib_gid_type *gid_type)
547ed88451eSMajd Dibbiny {
548ed88451eSMajd Dibbiny 	struct ib_gid_attr attr;
549ed88451eSMajd Dibbiny 	union ib_gid gid;
550ed88451eSMajd Dibbiny 	int ret;
551ed88451eSMajd Dibbiny 
552ed88451eSMajd Dibbiny 	ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
553ed88451eSMajd Dibbiny 	if (ret)
554ed88451eSMajd Dibbiny 		return ret;
555ed88451eSMajd Dibbiny 
556ed88451eSMajd Dibbiny 	dev_put(attr.ndev);
557ed88451eSMajd Dibbiny 
558ed88451eSMajd Dibbiny 	*gid_type = attr.gid_type;
559ed88451eSMajd Dibbiny 
560ed88451eSMajd Dibbiny 	return 0;
561ed88451eSMajd Dibbiny }
562ed88451eSMajd Dibbiny 
5631b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
5641b5daf11SMajd Dibbiny {
5657fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
566d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
5677fae6655SNoa Osherovich 	return 0;
5681b5daf11SMajd Dibbiny }
5691b5daf11SMajd Dibbiny 
5701b5daf11SMajd Dibbiny enum {
5711b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
5721b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
5731b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
5741b5daf11SMajd Dibbiny };
5751b5daf11SMajd Dibbiny 
5761b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
5771b5daf11SMajd Dibbiny {
5781b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
5791b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
5801b5daf11SMajd Dibbiny 
581ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
5821b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
5831b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
5841b5daf11SMajd Dibbiny 
5851b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
5861b5daf11SMajd Dibbiny }
5871b5daf11SMajd Dibbiny 
588da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
589776a3906SMoni Shoua 			    u8 atomic_size_qp,
590da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
591da7525d2SEran Ben Elisha {
592da7525d2SEran Ben Elisha 	u8 tmp;
593da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
594da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
595bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
596da7525d2SEran Ben Elisha 
597da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
598da7525d2SEran Ben Elisha 	 * of host endianness respond
599da7525d2SEran Ben Elisha 	 */
600da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
601da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
602da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
603da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
604da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
605da7525d2SEran Ben Elisha 	} else {
606da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
607da7525d2SEran Ben Elisha 	}
608da7525d2SEran Ben Elisha }
609da7525d2SEran Ben Elisha 
610776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
611776a3906SMoni Shoua 			       struct ib_device_attr *props)
612776a3906SMoni Shoua {
613776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
614776a3906SMoni Shoua 
615776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
616776a3906SMoni Shoua }
617776a3906SMoni Shoua 
618776a3906SMoni Shoua static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
619776a3906SMoni Shoua 			       struct ib_device_attr *props)
620776a3906SMoni Shoua {
621776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
622776a3906SMoni Shoua 
623776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
624776a3906SMoni Shoua }
625776a3906SMoni Shoua 
626776a3906SMoni Shoua bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
627776a3906SMoni Shoua {
628776a3906SMoni Shoua 	struct ib_device_attr props = {};
629776a3906SMoni Shoua 
630776a3906SMoni Shoua 	get_atomic_caps_dc(dev, &props);
631776a3906SMoni Shoua 	return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
632776a3906SMoni Shoua }
6331b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
6341b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
6351b5daf11SMajd Dibbiny {
6361b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6371b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6381b5daf11SMajd Dibbiny 	u64 tmp;
6391b5daf11SMajd Dibbiny 	int err;
6401b5daf11SMajd Dibbiny 
6411b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6421b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6431b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
6441b5daf11SMajd Dibbiny 							    sys_image_guid);
6451b5daf11SMajd Dibbiny 
6461b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6471b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
6483f89a643SAchiad Shochat 		break;
6493f89a643SAchiad Shochat 
6503f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6513f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
6523f89a643SAchiad Shochat 		break;
6531b5daf11SMajd Dibbiny 
6541b5daf11SMajd Dibbiny 	default:
6551b5daf11SMajd Dibbiny 		return -EINVAL;
6561b5daf11SMajd Dibbiny 	}
6573f89a643SAchiad Shochat 
6583f89a643SAchiad Shochat 	if (!err)
6593f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
6603f89a643SAchiad Shochat 
6613f89a643SAchiad Shochat 	return err;
6623f89a643SAchiad Shochat 
6631b5daf11SMajd Dibbiny }
6641b5daf11SMajd Dibbiny 
6651b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
6661b5daf11SMajd Dibbiny 				u16 *max_pkeys)
6671b5daf11SMajd Dibbiny {
6681b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6691b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6701b5daf11SMajd Dibbiny 
6711b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6721b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6731b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
6741b5daf11SMajd Dibbiny 
6751b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6761b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6771b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
6781b5daf11SMajd Dibbiny 						pkey_table_size));
6791b5daf11SMajd Dibbiny 		return 0;
6801b5daf11SMajd Dibbiny 
6811b5daf11SMajd Dibbiny 	default:
6821b5daf11SMajd Dibbiny 		return -EINVAL;
6831b5daf11SMajd Dibbiny 	}
6841b5daf11SMajd Dibbiny }
6851b5daf11SMajd Dibbiny 
6861b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
6871b5daf11SMajd Dibbiny 				u32 *vendor_id)
6881b5daf11SMajd Dibbiny {
6891b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6901b5daf11SMajd Dibbiny 
6911b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6921b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6931b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
6941b5daf11SMajd Dibbiny 
6951b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6961b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6971b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
6981b5daf11SMajd Dibbiny 
6991b5daf11SMajd Dibbiny 	default:
7001b5daf11SMajd Dibbiny 		return -EINVAL;
7011b5daf11SMajd Dibbiny 	}
7021b5daf11SMajd Dibbiny }
7031b5daf11SMajd Dibbiny 
7041b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
7051b5daf11SMajd Dibbiny 				__be64 *node_guid)
7061b5daf11SMajd Dibbiny {
7071b5daf11SMajd Dibbiny 	u64 tmp;
7081b5daf11SMajd Dibbiny 	int err;
7091b5daf11SMajd Dibbiny 
7101b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7111b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7121b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7131b5daf11SMajd Dibbiny 
7141b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7151b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7163f89a643SAchiad Shochat 		break;
7173f89a643SAchiad Shochat 
7183f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7193f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7203f89a643SAchiad Shochat 		break;
7211b5daf11SMajd Dibbiny 
7221b5daf11SMajd Dibbiny 	default:
7231b5daf11SMajd Dibbiny 		return -EINVAL;
7241b5daf11SMajd Dibbiny 	}
7253f89a643SAchiad Shochat 
7263f89a643SAchiad Shochat 	if (!err)
7273f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7283f89a643SAchiad Shochat 
7293f89a643SAchiad Shochat 	return err;
7301b5daf11SMajd Dibbiny }
7311b5daf11SMajd Dibbiny 
7321b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
733bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
7341b5daf11SMajd Dibbiny };
7351b5daf11SMajd Dibbiny 
7361b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
7371b5daf11SMajd Dibbiny {
7381b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
7391b5daf11SMajd Dibbiny 
7401b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
7411b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
7421b5daf11SMajd Dibbiny 
7431b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
7441b5daf11SMajd Dibbiny 
7451b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
7461b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
7471b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
7481b5daf11SMajd Dibbiny }
7491b5daf11SMajd Dibbiny 
750e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
7512528e33eSMatan Barak 				struct ib_device_attr *props,
7522528e33eSMatan Barak 				struct ib_udata *uhw)
753e126ba97SEli Cohen {
754e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
755938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
756e126ba97SEli Cohen 	int err = -ENOMEM;
757288c01b7SEli Cohen 	int max_sq_desc;
758e126ba97SEli Cohen 	int max_rq_sg;
759e126ba97SEli Cohen 	int max_sq_sg;
760e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
76185c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
762402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
763402ca536SBodong Wang 	size_t resp_len;
764402ca536SBodong Wang 	u64 max_tso;
765e126ba97SEli Cohen 
766402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
767402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
768402ca536SBodong Wang 		return -EINVAL;
769402ca536SBodong Wang 	else
770402ca536SBodong Wang 		resp.response_length = resp_len;
771402ca536SBodong Wang 
772402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
7732528e33eSMatan Barak 		return -EINVAL;
7742528e33eSMatan Barak 
775e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
7761b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
7771b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
7781b5daf11SMajd Dibbiny 	if (err)
7791b5daf11SMajd Dibbiny 		return err;
7801b5daf11SMajd Dibbiny 
7811b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
7821b5daf11SMajd Dibbiny 	if (err)
7831b5daf11SMajd Dibbiny 		return err;
7841b5daf11SMajd Dibbiny 
7851b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
7861b5daf11SMajd Dibbiny 	if (err)
7871b5daf11SMajd Dibbiny 		return err;
788e126ba97SEli Cohen 
7899603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
7909603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
7919603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
792e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
793e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
794e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
7951a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
796938fe83cSSaeed Mahameed 
797938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
798e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
799938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
800e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
801938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
802e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
803938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
804e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
805d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
806d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
807d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
808d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
809b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
810b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
811d2370e0aSMatan Barak 	}
812e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
813938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
8142dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
8152dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8162dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8172dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8182dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8192dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8202dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8212dea9094SSagi Grimberg 	}
822938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
823f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
824e126ba97SEli Cohen 
82585c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
826e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
827e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
82888115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
829e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
830e8161334SNoa Osherovich 		}
831e8161334SNoa Osherovich 
832e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
833e8161334SNoa Osherovich 			props->raw_packet_caps |=
834e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
83588115fe7SBodong Wang 
836402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
837402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
838402ca536SBodong Wang 			if (max_tso) {
839402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
840402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
841402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
842402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
843402ca536SBodong Wang 			}
844402ca536SBodong Wang 		}
84531f69a82SYishai Hadas 
84631f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
84731f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
84831f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
84931f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
85031f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
85131f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
85231f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
85331f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
85431f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
85531f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
85631f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
8574e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
8584e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
8592d93fc85SMatan Barak 			if (mlx5_accel_ipsec_device_caps(dev->mdev) &
8602d93fc85SMatan Barak 			    MLX5_ACCEL_IPSEC_CAP_DEVICE)
8612d93fc85SMatan Barak 				resp.rss_caps.rx_hash_fields_mask |=
8622d93fc85SMatan Barak 					MLX5_RX_HASH_IPSEC_SPI;
86331f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
86431f69a82SYishai Hadas 		}
86531f69a82SYishai Hadas 	} else {
86631f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
86731f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
86831f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
86931f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
870402ca536SBodong Wang 	}
871402ca536SBodong Wang 
872f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
873f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
874f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
875f0313965SErez Shitrit 	}
876f0313965SErez Shitrit 
87703404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
87885c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
87985c7c014SDaniel Jurgens 	    raw_support)
88003404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
88103404e8aSMaor Gottlieb 
8821d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
8831d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
8841d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
8851d54f890SYishai Hadas 
886cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
88785c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
88885c7c014SDaniel Jurgens 	    raw_support) {
889e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
890cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
891e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
892e8161334SNoa Osherovich 	}
893cff5a0f3SMajd Dibbiny 
89424da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
89524da0016SAriel Levkovich 		props->max_dm_size =
89624da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
89724da0016SAriel Levkovich 	}
89824da0016SAriel Levkovich 
899da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
900da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
901da6d6ba3SMaor Gottlieb 
902b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
903b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
904b1383aa6SNoa Osherovich 
9051b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
9061b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
907e126ba97SEli Cohen 
908e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
909e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
910938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
911938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
912938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
913938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
914288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
915288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
916288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
917e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
918e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
919986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
920938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9219f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
922938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
923938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
924938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
925938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
926938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
927938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
928938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
929e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
930e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
931911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
932911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
933776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
93481bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
935938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
936938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
937e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
938e126ba97SEli Cohen 					   props->max_mcast_grp;
939e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
94086695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
9417c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
9427c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
943e126ba97SEli Cohen 
9448cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
945938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
9468cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
9478cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
9488cdd312cSHaggai Eran #endif
9498cdd312cSHaggai Eran 
950051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
951051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
952051f2630SLeon Romanovsky 
953eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
954eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
955eff901d3SEli Cohen 
95631f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
95785c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
95831f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
95931f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
96031f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
96131f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
96231f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
96331f69a82SYishai Hadas 		props->max_wq_type_rq =
96431f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
96531f69a82SYishai Hadas 	}
96631f69a82SYishai Hadas 
967eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
96878b1beb0SLeon Romanovsky 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
96978b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
970eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
97178b1beb0SLeon Romanovsky 		props->tm_caps.flags = IB_TM_CAP_RC;
97278b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
973eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
97478b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
975eb761894SArtemy Kovalyov 	}
976eb761894SArtemy Kovalyov 
97787ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
97887ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
97987ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
98087ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
98187ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
98287ab3f52SYonatan Cohen 	}
98387ab3f52SYonatan Cohen 
9847e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
9857e43a2a5SBodong Wang 		resp.cqe_comp_caps.max_num =
9867e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
9877e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
9887e43a2a5SBodong Wang 		resp.cqe_comp_caps.supported_format =
9897e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_HASH |
9907e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_CSUM;
9917e43a2a5SBodong Wang 		resp.response_length += sizeof(resp.cqe_comp_caps);
9927e43a2a5SBodong Wang 	}
9937e43a2a5SBodong Wang 
99485c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
99585c7c014SDaniel Jurgens 	    raw_support) {
996d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
997d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
998d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
999d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1000d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
1001d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1002d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
1003d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
100461147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
100561147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
100661147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
100761147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
1008d949167dSBodong Wang 		}
1009d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1010d949167dSBodong Wang 	}
1011d949167dSBodong Wang 
10129f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
10139f885201SLeon Romanovsky 			uhw->outlen)) {
1014795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
10159f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1016795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1017050da902SBodong Wang 
1018050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1019050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1020050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1021050da902SBodong Wang 
10229f885201SLeon Romanovsky 		resp.response_length +=
10239f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
10249f885201SLeon Romanovsky 	}
10259f885201SLeon Romanovsky 
1026de57f2adSGuy Levi 	if (field_avail(typeof(resp), flags, uhw->outlen)) {
1027de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
10287a0c8f42SGuy Levi 
1029de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1030de57f2adSGuy Levi 			resp.flags |=
1031de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
10327a0c8f42SGuy Levi 
10337a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
10347a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
1035de57f2adSGuy Levi 	}
10369f885201SLeon Romanovsky 
103796dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
103896dc3fc5SNoa Osherovich 			uhw->outlen)) {
103996dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
104096dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
104196dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
104296dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
104396dc3fc5SNoa Osherovich 
104496dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
104596dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
104696dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
104796dc3fc5SNoa Osherovich 
104896dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
104996dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
105096dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
105196dc3fc5SNoa Osherovich 
105296dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
105396dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
105496dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
105596dc3fc5SNoa Osherovich 		}
105696dc3fc5SNoa Osherovich 	}
105796dc3fc5SNoa Osherovich 
105885c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
105985c7c014SDaniel Jurgens 	    raw_support) {
1060b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1061b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1062b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1063b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1064b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1065b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1066b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1067b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1068b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1069b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1070b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1071b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1072b4f34597SNoa Osherovich 		}
1073b4f34597SNoa Osherovich 	}
1074b4f34597SNoa Osherovich 
1075f95ef6cbSMaor Gottlieb 	if (field_avail(typeof(resp), tunnel_offloads_caps,
1076f95ef6cbSMaor Gottlieb 			uhw->outlen)) {
1077f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1078f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1079f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1080f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1081f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1082f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1083f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1084f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1085f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1086f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
1087f95ef6cbSMaor Gottlieb 	}
1088f95ef6cbSMaor Gottlieb 
1089402ca536SBodong Wang 	if (uhw->outlen) {
1090402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1091402ca536SBodong Wang 
1092402ca536SBodong Wang 		if (err)
1093402ca536SBodong Wang 			return err;
1094402ca536SBodong Wang 	}
1095402ca536SBodong Wang 
10961b5daf11SMajd Dibbiny 	return 0;
10971b5daf11SMajd Dibbiny }
1098e126ba97SEli Cohen 
10991b5daf11SMajd Dibbiny enum mlx5_ib_width {
11001b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
11011b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
11021b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
11031b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
11041b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
11051b5daf11SMajd Dibbiny };
11061b5daf11SMajd Dibbiny 
11071b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
11081b5daf11SMajd Dibbiny 				  u8 *ib_width)
11091b5daf11SMajd Dibbiny {
11101b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11111b5daf11SMajd Dibbiny 	int err = 0;
11121b5daf11SMajd Dibbiny 
11131b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
11141b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
11151b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
11161b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
11171b5daf11SMajd Dibbiny 			    (int)active_width);
11181b5daf11SMajd Dibbiny 		err = -EINVAL;
11191b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
11201b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
11211b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
11221b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
11231b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
11241b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
11251b5daf11SMajd Dibbiny 	} else {
11261b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
11271b5daf11SMajd Dibbiny 			    (int)active_width);
11281b5daf11SMajd Dibbiny 		err = -EINVAL;
11291b5daf11SMajd Dibbiny 	}
11301b5daf11SMajd Dibbiny 
11311b5daf11SMajd Dibbiny 	return err;
11321b5daf11SMajd Dibbiny }
11331b5daf11SMajd Dibbiny 
11341b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
11351b5daf11SMajd Dibbiny {
11361b5daf11SMajd Dibbiny 	switch (mtu) {
11371b5daf11SMajd Dibbiny 	case 256: return 1;
11381b5daf11SMajd Dibbiny 	case 512: return 2;
11391b5daf11SMajd Dibbiny 	case 1024: return 3;
11401b5daf11SMajd Dibbiny 	case 2048: return 4;
11411b5daf11SMajd Dibbiny 	case 4096: return 5;
11421b5daf11SMajd Dibbiny 	default:
11431b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
11441b5daf11SMajd Dibbiny 		return -1;
11451b5daf11SMajd Dibbiny 	}
11461b5daf11SMajd Dibbiny }
11471b5daf11SMajd Dibbiny 
11481b5daf11SMajd Dibbiny enum ib_max_vl_num {
11491b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
11501b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
11511b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
11521b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
11531b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
11541b5daf11SMajd Dibbiny };
11551b5daf11SMajd Dibbiny 
11561b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
11571b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
11581b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
11591b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
11601b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
11611b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
11621b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
11631b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
11641b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
11651b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
11661b5daf11SMajd Dibbiny };
11671b5daf11SMajd Dibbiny 
11681b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
11691b5daf11SMajd Dibbiny 				u8 *max_vl_num)
11701b5daf11SMajd Dibbiny {
11711b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
11721b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
11731b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
11741b5daf11SMajd Dibbiny 		break;
11751b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
11761b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
11771b5daf11SMajd Dibbiny 		break;
11781b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
11791b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
11801b5daf11SMajd Dibbiny 		break;
11811b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
11821b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
11831b5daf11SMajd Dibbiny 		break;
11841b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
11851b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
11861b5daf11SMajd Dibbiny 		break;
11871b5daf11SMajd Dibbiny 
11881b5daf11SMajd Dibbiny 	default:
11891b5daf11SMajd Dibbiny 		return -EINVAL;
11901b5daf11SMajd Dibbiny 	}
11911b5daf11SMajd Dibbiny 
11921b5daf11SMajd Dibbiny 	return 0;
11931b5daf11SMajd Dibbiny }
11941b5daf11SMajd Dibbiny 
11951b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
11961b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
11971b5daf11SMajd Dibbiny {
11981b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11991b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
12001b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1201046339eaSSaeed Mahameed 	u16 max_mtu;
1202046339eaSSaeed Mahameed 	u16 oper_mtu;
12031b5daf11SMajd Dibbiny 	int err;
12041b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
12051b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
12061b5daf11SMajd Dibbiny 
12071b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
12081b5daf11SMajd Dibbiny 	if (!rep) {
12091b5daf11SMajd Dibbiny 		err = -ENOMEM;
12101b5daf11SMajd Dibbiny 		goto out;
12111b5daf11SMajd Dibbiny 	}
12121b5daf11SMajd Dibbiny 
1213c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
12141b5daf11SMajd Dibbiny 
12151b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
12161b5daf11SMajd Dibbiny 	if (err)
12171b5daf11SMajd Dibbiny 		goto out;
12181b5daf11SMajd Dibbiny 
12191b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
12201b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
12211b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
12221b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
12231b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
12241b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
12251b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
12261b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
12271b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
12281b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
12291b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
12301b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
12311b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
12321b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
1233eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
12341b5daf11SMajd Dibbiny 
12351b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
12361b5daf11SMajd Dibbiny 	if (err)
12371b5daf11SMajd Dibbiny 		goto out;
12381b5daf11SMajd Dibbiny 
12391b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
12401b5daf11SMajd Dibbiny 				     &props->active_width);
12411b5daf11SMajd Dibbiny 	if (err)
12421b5daf11SMajd Dibbiny 		goto out;
1243d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
12441b5daf11SMajd Dibbiny 	if (err)
12451b5daf11SMajd Dibbiny 		goto out;
12461b5daf11SMajd Dibbiny 
1247facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
12481b5daf11SMajd Dibbiny 
12491b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
12501b5daf11SMajd Dibbiny 
1251facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
12521b5daf11SMajd Dibbiny 
12531b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
12541b5daf11SMajd Dibbiny 
12551b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
12561b5daf11SMajd Dibbiny 	if (err)
12571b5daf11SMajd Dibbiny 		goto out;
12581b5daf11SMajd Dibbiny 
12591b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
12601b5daf11SMajd Dibbiny 				   &props->max_vl_num);
12611b5daf11SMajd Dibbiny out:
12621b5daf11SMajd Dibbiny 	kfree(rep);
1263e126ba97SEli Cohen 	return err;
1264e126ba97SEli Cohen }
1265e126ba97SEli Cohen 
1266e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1267e126ba97SEli Cohen 		       struct ib_port_attr *props)
1268e126ba97SEli Cohen {
1269095b0927SIlan Tayari 	unsigned int count;
1270095b0927SIlan Tayari 	int ret;
1271095b0927SIlan Tayari 
12721b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
12731b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1274095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1275095b0927SIlan Tayari 		break;
1276e126ba97SEli Cohen 
12771b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1278095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1279095b0927SIlan Tayari 		break;
12801b5daf11SMajd Dibbiny 
12813f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1282095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1283095b0927SIlan Tayari 		break;
12843f89a643SAchiad Shochat 
12851b5daf11SMajd Dibbiny 	default:
1286095b0927SIlan Tayari 		ret = -EINVAL;
1287e126ba97SEli Cohen 	}
1288095b0927SIlan Tayari 
1289095b0927SIlan Tayari 	if (!ret && props) {
1290b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1291b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1292b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1293b3cbd6f0SDaniel Jurgens 
1294b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1295b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1296b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1297b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1298b3cbd6f0SDaniel Jurgens 			 */
1299b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1300b3cbd6f0SDaniel Jurgens 			port = 1;
1301b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1302b3cbd6f0SDaniel Jurgens 		}
1303b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1304b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1305b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1306095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1307095b0927SIlan Tayari 	}
1308095b0927SIlan Tayari 	return ret;
1309e126ba97SEli Cohen }
1310e126ba97SEli Cohen 
13118e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
13128e6efa3aSMark Bloch 				  struct ib_port_attr *props)
13138e6efa3aSMark Bloch {
13148e6efa3aSMark Bloch 	int ret;
13158e6efa3aSMark Bloch 
13168e6efa3aSMark Bloch 	/* Only link layer == ethernet is valid for representors */
13178e6efa3aSMark Bloch 	ret = mlx5_query_port_roce(ibdev, port, props);
13188e6efa3aSMark Bloch 	if (ret || !props)
13198e6efa3aSMark Bloch 		return ret;
13208e6efa3aSMark Bloch 
13218e6efa3aSMark Bloch 	/* We don't support GIDS */
13228e6efa3aSMark Bloch 	props->gid_tbl_len = 0;
13238e6efa3aSMark Bloch 
13248e6efa3aSMark Bloch 	return ret;
13258e6efa3aSMark Bloch }
13268e6efa3aSMark Bloch 
1327e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1328e126ba97SEli Cohen 			     union ib_gid *gid)
1329e126ba97SEli Cohen {
13301b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
13311b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1332e126ba97SEli Cohen 
13331b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13341b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
13351b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1336e126ba97SEli Cohen 
13371b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
13381b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1339e126ba97SEli Cohen 
13401b5daf11SMajd Dibbiny 	default:
13411b5daf11SMajd Dibbiny 		return -EINVAL;
13421b5daf11SMajd Dibbiny 	}
1343e126ba97SEli Cohen 
1344e126ba97SEli Cohen }
1345e126ba97SEli Cohen 
1346b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1347b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1348b3cbd6f0SDaniel Jurgens {
1349b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1350b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1351b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1352b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1353b3cbd6f0SDaniel Jurgens 	int err;
1354b3cbd6f0SDaniel Jurgens 
1355b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1356b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1357b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1358b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1359b3cbd6f0SDaniel Jurgens 		 */
1360b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1361b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1362b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1363b3cbd6f0SDaniel Jurgens 	}
1364b3cbd6f0SDaniel Jurgens 
1365b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1366b3cbd6f0SDaniel Jurgens 					index, pkey);
1367b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1368b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1369b3cbd6f0SDaniel Jurgens 
1370b3cbd6f0SDaniel Jurgens 	return err;
1371b3cbd6f0SDaniel Jurgens }
1372b3cbd6f0SDaniel Jurgens 
1373e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1374e126ba97SEli Cohen 			      u16 *pkey)
1375e126ba97SEli Cohen {
13761b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13771b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
13781b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1379e126ba97SEli Cohen 
13801b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
13811b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1382b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
13831b5daf11SMajd Dibbiny 	default:
13841b5daf11SMajd Dibbiny 		return -EINVAL;
1385e126ba97SEli Cohen 	}
13861b5daf11SMajd Dibbiny }
1387e126ba97SEli Cohen 
1388e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1389e126ba97SEli Cohen 				 struct ib_device_modify *props)
1390e126ba97SEli Cohen {
1391e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1392e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1393e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1394e126ba97SEli Cohen 	int err;
1395e126ba97SEli Cohen 
1396e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1397e126ba97SEli Cohen 		return -EOPNOTSUPP;
1398e126ba97SEli Cohen 
1399e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1400e126ba97SEli Cohen 		return 0;
1401e126ba97SEli Cohen 
1402e126ba97SEli Cohen 	/*
1403e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1404e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1405e126ba97SEli Cohen 	 */
1406bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
14079603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1408e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1409e126ba97SEli Cohen 	if (err)
1410e126ba97SEli Cohen 		return err;
1411e126ba97SEli Cohen 
1412bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1413e126ba97SEli Cohen 
1414e126ba97SEli Cohen 	return err;
1415e126ba97SEli Cohen }
1416e126ba97SEli Cohen 
1417cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1418cdbe33d0SEli Cohen 				u32 value)
1419cdbe33d0SEli Cohen {
1420cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1421b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1422b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1423cdbe33d0SEli Cohen 	int err;
1424cdbe33d0SEli Cohen 
1425b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1426b3cbd6f0SDaniel Jurgens 	if (!mdev)
1427b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1428b3cbd6f0SDaniel Jurgens 
1429b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1430cdbe33d0SEli Cohen 	if (err)
1431b3cbd6f0SDaniel Jurgens 		goto out;
1432cdbe33d0SEli Cohen 
1433cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1434cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1435cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1436b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1437b3cbd6f0SDaniel Jurgens 		goto out;
1438cdbe33d0SEli Cohen 	}
1439cdbe33d0SEli Cohen 
1440cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1441cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1442b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1443b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1444b3cbd6f0SDaniel Jurgens 
1445b3cbd6f0SDaniel Jurgens out:
1446b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1447cdbe33d0SEli Cohen 
1448cdbe33d0SEli Cohen 	return err;
1449cdbe33d0SEli Cohen }
1450cdbe33d0SEli Cohen 
1451e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1452e126ba97SEli Cohen 			       struct ib_port_modify *props)
1453e126ba97SEli Cohen {
1454e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1455e126ba97SEli Cohen 	struct ib_port_attr attr;
1456e126ba97SEli Cohen 	u32 tmp;
1457e126ba97SEli Cohen 	int err;
1458cdbe33d0SEli Cohen 	u32 change_mask;
1459cdbe33d0SEli Cohen 	u32 value;
1460cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1461cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1462cdbe33d0SEli Cohen 
1463ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1464ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1465ec255879SMajd Dibbiny 	 */
1466ec255879SMajd Dibbiny 	if (!is_ib)
1467ec255879SMajd Dibbiny 		return 0;
1468ec255879SMajd Dibbiny 
1469cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1470cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1471cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1472cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1473cdbe33d0SEli Cohen 	}
1474e126ba97SEli Cohen 
1475e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1476e126ba97SEli Cohen 
1477c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1478e126ba97SEli Cohen 	if (err)
1479e126ba97SEli Cohen 		goto out;
1480e126ba97SEli Cohen 
1481e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1482e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1483e126ba97SEli Cohen 
14849603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1485e126ba97SEli Cohen 
1486e126ba97SEli Cohen out:
1487e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1488e126ba97SEli Cohen 	return err;
1489e126ba97SEli Cohen }
1490e126ba97SEli Cohen 
149130aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
149230aa60b3SEli Cohen {
149330aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
149430aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
149530aa60b3SEli Cohen }
149630aa60b3SEli Cohen 
149731a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
149831a78a5aSYishai Hadas {
149931a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
150031a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
150131a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
150231a78a5aSYishai Hadas 
150331a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
150431a78a5aSYishai Hadas }
150531a78a5aSYishai Hadas 
1506b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1507b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
150831a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1509b037c29aSEli Cohen {
1510b037c29aSEli Cohen 	int uars_per_sys_page;
1511b037c29aSEli Cohen 	int bfregs_per_sys_page;
1512b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1513b037c29aSEli Cohen 
1514b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1515b037c29aSEli Cohen 		return -EINVAL;
1516b037c29aSEli Cohen 
1517b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1518b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1519b037c29aSEli Cohen 
1520b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1521b037c29aSEli Cohen 		return -ENOMEM;
1522b037c29aSEli Cohen 
1523b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1524b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
152531a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1526b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1527b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1528b037c29aSEli Cohen 		return -EINVAL;
1529b037c29aSEli Cohen 
153031a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
153131a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
153231a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
153331a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
153431a78a5aSYishai Hadas 
153531a78a5aSYishai 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",
1536b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1537b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
153831a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
153931a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1540b037c29aSEli Cohen 
1541b037c29aSEli Cohen 	return 0;
1542b037c29aSEli Cohen }
1543b037c29aSEli Cohen 
1544b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1545b037c29aSEli Cohen {
1546b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1547b037c29aSEli Cohen 	int err;
1548b037c29aSEli Cohen 	int i;
1549b037c29aSEli Cohen 
1550b037c29aSEli Cohen 	bfregi = &context->bfregi;
155131a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1552b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1553b037c29aSEli Cohen 		if (err)
1554b037c29aSEli Cohen 			goto error;
1555b037c29aSEli Cohen 
1556b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1557b037c29aSEli Cohen 	}
15584ed131d0SYishai Hadas 
15594ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
15604ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
15614ed131d0SYishai Hadas 
1562b037c29aSEli Cohen 	return 0;
1563b037c29aSEli Cohen 
1564b037c29aSEli Cohen error:
1565b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1566b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1567b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1568b037c29aSEli Cohen 
1569b037c29aSEli Cohen 	return err;
1570b037c29aSEli Cohen }
1571b037c29aSEli Cohen 
1572b037c29aSEli Cohen static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1573b037c29aSEli Cohen {
1574b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1575b037c29aSEli Cohen 	int err;
1576b037c29aSEli Cohen 	int i;
1577b037c29aSEli Cohen 
1578b037c29aSEli Cohen 	bfregi = &context->bfregi;
15794ed131d0SYishai Hadas 	for (i = 0; i < bfregi->num_sys_pages; i++) {
15804ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
15814ed131d0SYishai Hadas 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX) {
1582b037c29aSEli Cohen 			err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1583b037c29aSEli Cohen 			if (err) {
15844ed131d0SYishai Hadas 				mlx5_ib_warn(dev, "failed to free uar %d, err=%d\n", i, err);
1585b037c29aSEli Cohen 				return err;
1586b037c29aSEli Cohen 			}
1587b037c29aSEli Cohen 		}
15884ed131d0SYishai Hadas 	}
15894ed131d0SYishai Hadas 
1590b037c29aSEli Cohen 	return 0;
1591b037c29aSEli Cohen }
1592b037c29aSEli Cohen 
1593c85023e1SHuy Nguyen static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1594c85023e1SHuy Nguyen {
1595c85023e1SHuy Nguyen 	int err;
1596c85023e1SHuy Nguyen 
1597c85023e1SHuy Nguyen 	err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1598c85023e1SHuy Nguyen 	if (err)
1599c85023e1SHuy Nguyen 		return err;
1600c85023e1SHuy Nguyen 
1601c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
16028978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
16038978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1604c85023e1SHuy Nguyen 		return err;
1605c85023e1SHuy Nguyen 
1606c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1607c85023e1SHuy Nguyen 	dev->user_td++;
1608c85023e1SHuy Nguyen 
1609c85023e1SHuy Nguyen 	if (dev->user_td == 2)
1610c85023e1SHuy Nguyen 		err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1611c85023e1SHuy Nguyen 
1612c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1613c85023e1SHuy Nguyen 	return err;
1614c85023e1SHuy Nguyen }
1615c85023e1SHuy Nguyen 
1616c85023e1SHuy Nguyen static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1617c85023e1SHuy Nguyen {
1618c85023e1SHuy Nguyen 	mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1619c85023e1SHuy Nguyen 
1620c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
16218978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
16228978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1623c85023e1SHuy Nguyen 		return;
1624c85023e1SHuy Nguyen 
1625c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1626c85023e1SHuy Nguyen 	dev->user_td--;
1627c85023e1SHuy Nguyen 
1628c85023e1SHuy Nguyen 	if (dev->user_td < 2)
1629c85023e1SHuy Nguyen 		mlx5_nic_vport_update_local_lb(dev->mdev, false);
1630c85023e1SHuy Nguyen 
1631c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1632c85023e1SHuy Nguyen }
1633c85023e1SHuy Nguyen 
1634e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1635e126ba97SEli Cohen 						  struct ib_udata *udata)
1636e126ba97SEli Cohen {
1637e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1638b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1639b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
16405c99eaecSFeras Daoud 	struct mlx5_core_dev *mdev = dev->mdev;
1641e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
16422f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
164378c0f98cSEli Cohen 	int ver;
1644e126ba97SEli Cohen 	int err;
1645a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1646a168a41cSMajd Dibbiny 				     max_cqe_version);
1647b037c29aSEli Cohen 	bool lib_uar_4k;
1648e126ba97SEli Cohen 
1649e126ba97SEli Cohen 	if (!dev->ib_active)
1650e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1651e126ba97SEli Cohen 
1652e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
165378c0f98cSEli Cohen 		ver = 0;
1654e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
165578c0f98cSEli Cohen 		ver = 2;
165678c0f98cSEli Cohen 	else
165778c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
165878c0f98cSEli Cohen 
1659e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1660e126ba97SEli Cohen 	if (err)
1661e126ba97SEli Cohen 		return ERR_PTR(err);
1662e126ba97SEli Cohen 
1663b368d7cbSMatan Barak 	if (req.flags)
166478c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
166578c0f98cSEli Cohen 
1666f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1667b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1668b368d7cbSMatan Barak 
16692f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
16702f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
16712f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1672e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1673e126ba97SEli Cohen 
1674938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
16752cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1676938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1677b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1678938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1679938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1680938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1681938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1682938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1683f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1684f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1685f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
168630aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
168730aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
168830aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
168930aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1690b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1691b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1692e126ba97SEli Cohen 
1693c03faa56SMatan Barak 	if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1694c03faa56SMatan Barak 		if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1695c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1696c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1697c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1698c03faa56SMatan Barak 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1699c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1700c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1701c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1702c03faa56SMatan Barak 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1703c03faa56SMatan Barak 	}
1704c03faa56SMatan Barak 
1705e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1706e126ba97SEli Cohen 	if (!context)
1707e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1708e126ba97SEli Cohen 
170930aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
17102f5ff264SEli Cohen 	bfregi = &context->bfregi;
1711b037c29aSEli Cohen 
1712b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
171331a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1714b037c29aSEli Cohen 	if (err)
1715b037c29aSEli Cohen 		goto out_ctx;
1716b037c29aSEli Cohen 
17172f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1718b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
171931a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1720b037c29aSEli Cohen 				GFP_KERNEL);
1721b037c29aSEli Cohen 	if (!bfregi->count) {
1722e126ba97SEli Cohen 		err = -ENOMEM;
1723e126ba97SEli Cohen 		goto out_ctx;
1724e126ba97SEli Cohen 	}
1725e126ba97SEli Cohen 
1726b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1727b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1728e126ba97SEli Cohen 				    GFP_KERNEL);
1729b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1730e126ba97SEli Cohen 		err = -ENOMEM;
1731e126ba97SEli Cohen 		goto out_count;
1732e126ba97SEli Cohen 	}
1733e126ba97SEli Cohen 
1734b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1735b037c29aSEli Cohen 	if (err)
1736b037c29aSEli Cohen 		goto out_sys_pages;
1737b037c29aSEli Cohen 
1738b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1739b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1740b4cfe447SHaggai Eran #endif
1741b4cfe447SHaggai Eran 
1742146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1743c85023e1SHuy Nguyen 		err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
1744146d2f1aSmajd@mellanox.com 		if (err)
1745c44ef998SIlya Lesokhin 			goto out_uars;
1746146d2f1aSmajd@mellanox.com 	}
1747146d2f1aSmajd@mellanox.com 
17487c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
1749ad9a3668SMajd Dibbiny 	mutex_init(&context->vma_private_list_mutex);
1750e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1751e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1752e126ba97SEli Cohen 
17532f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1754508562d6SDaniel Jurgens 	resp.num_ports = dev->num_ports;
1755b368d7cbSMatan Barak 
1756f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1757f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1758b368d7cbSMatan Barak 
1759402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
17606ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
17616ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1762402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1763402ca536SBodong Wang 	}
1764402ca536SBodong Wang 
176578984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
176678984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
176778984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
176878984898SOr Gerlitz 			resp.eth_min_inline++;
176978984898SOr Gerlitz 		}
177078984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
177178984898SOr Gerlitz 	}
177278984898SOr Gerlitz 
17735c99eaecSFeras Daoud 	if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
17745c99eaecSFeras Daoud 		if (mdev->clock_info)
17755c99eaecSFeras Daoud 			resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
17765c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.clock_info_versions);
17775c99eaecSFeras Daoud 	}
17785c99eaecSFeras Daoud 
1779bc5c6eedSNoa Osherovich 	/*
1780bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1781bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1782bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1783bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1784bc5c6eedSNoa Osherovich 	 */
1785de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1786de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1787b368d7cbSMatan Barak 			resp.comp_mask |=
1788b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1789b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1790de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1791de8d6e02SEli Cohen 		}
17925c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.hca_core_clock_offset);
1793b368d7cbSMatan Barak 	}
1794b368d7cbSMatan Barak 
179530aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
179630aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
179730aa60b3SEli Cohen 
179830aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
179930aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
180030aa60b3SEli Cohen 
180131a78a5aSYishai Hadas 	if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
180231a78a5aSYishai Hadas 		resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
180331a78a5aSYishai Hadas 		resp.response_length += sizeof(resp.num_dyn_bfregs);
180431a78a5aSYishai Hadas 	}
180531a78a5aSYishai Hadas 
1806b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1807e126ba97SEli Cohen 	if (err)
1808146d2f1aSmajd@mellanox.com 		goto out_td;
1809e126ba97SEli Cohen 
18102f5ff264SEli Cohen 	bfregi->ver = ver;
18112f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1812f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
181330aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
181430aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1815f72300c5SHaggai Abramovsky 
1816e126ba97SEli Cohen 	return &context->ibucontext;
1817e126ba97SEli Cohen 
1818146d2f1aSmajd@mellanox.com out_td:
1819146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1820c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1821146d2f1aSmajd@mellanox.com 
1822e126ba97SEli Cohen out_uars:
1823b037c29aSEli Cohen 	deallocate_uars(dev, context);
1824b037c29aSEli Cohen 
1825b037c29aSEli Cohen out_sys_pages:
1826b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1827b037c29aSEli Cohen 
1828e126ba97SEli Cohen out_count:
18292f5ff264SEli Cohen 	kfree(bfregi->count);
1830e126ba97SEli Cohen 
1831e126ba97SEli Cohen out_ctx:
1832e126ba97SEli Cohen 	kfree(context);
1833b037c29aSEli Cohen 
1834e126ba97SEli Cohen 	return ERR_PTR(err);
1835e126ba97SEli Cohen }
1836e126ba97SEli Cohen 
1837e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1838e126ba97SEli Cohen {
1839e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1840e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1841b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1842e126ba97SEli Cohen 
1843b037c29aSEli Cohen 	bfregi = &context->bfregi;
1844146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1845c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1846146d2f1aSmajd@mellanox.com 
1847b037c29aSEli Cohen 	deallocate_uars(dev, context);
1848b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
18492f5ff264SEli Cohen 	kfree(bfregi->count);
1850e126ba97SEli Cohen 	kfree(context);
1851e126ba97SEli Cohen 
1852e126ba97SEli Cohen 	return 0;
1853e126ba97SEli Cohen }
1854e126ba97SEli Cohen 
1855b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
18564ed131d0SYishai Hadas 				 int uar_idx)
1857e126ba97SEli Cohen {
1858b037c29aSEli Cohen 	int fw_uars_per_page;
1859b037c29aSEli Cohen 
1860b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1861b037c29aSEli Cohen 
18624ed131d0SYishai Hadas 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
1863e126ba97SEli Cohen }
1864e126ba97SEli Cohen 
1865e126ba97SEli Cohen static int get_command(unsigned long offset)
1866e126ba97SEli Cohen {
1867e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1868e126ba97SEli Cohen }
1869e126ba97SEli Cohen 
1870e126ba97SEli Cohen static int get_arg(unsigned long offset)
1871e126ba97SEli Cohen {
1872e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1873e126ba97SEli Cohen }
1874e126ba97SEli Cohen 
1875e126ba97SEli Cohen static int get_index(unsigned long offset)
1876e126ba97SEli Cohen {
1877e126ba97SEli Cohen 	return get_arg(offset);
1878e126ba97SEli Cohen }
1879e126ba97SEli Cohen 
18804ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
18814ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
18824ed131d0SYishai Hadas {
18834ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
18844ed131d0SYishai Hadas }
18854ed131d0SYishai Hadas 
18867c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
18877c2344c3SMaor Gottlieb {
18887c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
18897c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
18907c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
18917c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
18927c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
18937c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
18947c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
18957c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
18967c2344c3SMaor Gottlieb 	 */
18977c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
18987c2344c3SMaor Gottlieb }
18997c2344c3SMaor Gottlieb 
19007c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
19017c2344c3SMaor Gottlieb {
19027c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
19037c2344c3SMaor Gottlieb 
19047c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
19057c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
19067c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
19077c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
19087c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
19097c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
19107c2344c3SMaor Gottlieb 	 * process is exiting.
19117c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
19127c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
19137c2344c3SMaor Gottlieb 	 */
19147c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
19157c2344c3SMaor Gottlieb 
19167c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
19177c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
19187c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
19197c2344c3SMaor Gottlieb 	 */
19207c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
1921ad9a3668SMajd Dibbiny 	mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
19227c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
1923ad9a3668SMajd Dibbiny 	mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
19247c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
19257c2344c3SMaor Gottlieb }
19267c2344c3SMaor Gottlieb 
19277c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
19287c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
19297c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
19307c2344c3SMaor Gottlieb };
19317c2344c3SMaor Gottlieb 
19327c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
19337c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
19347c2344c3SMaor Gottlieb {
19357c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
19367c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
19377c2344c3SMaor Gottlieb 
19387c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
19397c2344c3SMaor Gottlieb 	if (!vma_prv)
19407c2344c3SMaor Gottlieb 		return -ENOMEM;
19417c2344c3SMaor Gottlieb 
19427c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
1943ad9a3668SMajd Dibbiny 	vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
19447c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
19457c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
19467c2344c3SMaor Gottlieb 
1947ad9a3668SMajd Dibbiny 	mutex_lock(&ctx->vma_private_list_mutex);
19487c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
1949ad9a3668SMajd Dibbiny 	mutex_unlock(&ctx->vma_private_list_mutex);
19507c2344c3SMaor Gottlieb 
19517c2344c3SMaor Gottlieb 	return 0;
19527c2344c3SMaor Gottlieb }
19537c2344c3SMaor Gottlieb 
19547c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
19557c2344c3SMaor Gottlieb {
19567c2344c3SMaor Gottlieb 	int ret;
19577c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
19587c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
19597c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
19607c2344c3SMaor Gottlieb 	struct task_struct *owning_process  = NULL;
19617c2344c3SMaor Gottlieb 	struct mm_struct   *owning_mm       = NULL;
19627c2344c3SMaor Gottlieb 
19637c2344c3SMaor Gottlieb 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
19647c2344c3SMaor Gottlieb 	if (!owning_process)
19657c2344c3SMaor Gottlieb 		return;
19667c2344c3SMaor Gottlieb 
19677c2344c3SMaor Gottlieb 	owning_mm = get_task_mm(owning_process);
19687c2344c3SMaor Gottlieb 	if (!owning_mm) {
19697c2344c3SMaor Gottlieb 		pr_info("no mm, disassociate ucontext is pending task termination\n");
19707c2344c3SMaor Gottlieb 		while (1) {
19717c2344c3SMaor Gottlieb 			put_task_struct(owning_process);
19727c2344c3SMaor Gottlieb 			usleep_range(1000, 2000);
19737c2344c3SMaor Gottlieb 			owning_process = get_pid_task(ibcontext->tgid,
19747c2344c3SMaor Gottlieb 						      PIDTYPE_PID);
19757c2344c3SMaor Gottlieb 			if (!owning_process ||
19767c2344c3SMaor Gottlieb 			    owning_process->state == TASK_DEAD) {
19777c2344c3SMaor Gottlieb 				pr_info("disassociate ucontext done, task was terminated\n");
19787c2344c3SMaor Gottlieb 				/* in case task was dead need to release the
19797c2344c3SMaor Gottlieb 				 * task struct.
19807c2344c3SMaor Gottlieb 				 */
19817c2344c3SMaor Gottlieb 				if (owning_process)
19827c2344c3SMaor Gottlieb 					put_task_struct(owning_process);
19837c2344c3SMaor Gottlieb 				return;
19847c2344c3SMaor Gottlieb 			}
19857c2344c3SMaor Gottlieb 		}
19867c2344c3SMaor Gottlieb 	}
19877c2344c3SMaor Gottlieb 
19887c2344c3SMaor Gottlieb 	/* need to protect from a race on closing the vma as part of
19897c2344c3SMaor Gottlieb 	 * mlx5_ib_vma_close.
19907c2344c3SMaor Gottlieb 	 */
1991ecc7d83bSMaor Gottlieb 	down_write(&owning_mm->mmap_sem);
1992ad9a3668SMajd Dibbiny 	mutex_lock(&context->vma_private_list_mutex);
19937c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
19947c2344c3SMaor Gottlieb 				 list) {
19957c2344c3SMaor Gottlieb 		vma = vma_private->vma;
19967c2344c3SMaor Gottlieb 		ret = zap_vma_ptes(vma, vma->vm_start,
19977c2344c3SMaor Gottlieb 				   PAGE_SIZE);
19987c2344c3SMaor Gottlieb 		WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
19997c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
20007c2344c3SMaor Gottlieb 		 * not access ops any more.
20017c2344c3SMaor Gottlieb 		 */
200213776612SMaor Gottlieb 		vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
20037c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
20047c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
20057c2344c3SMaor Gottlieb 		kfree(vma_private);
20067c2344c3SMaor Gottlieb 	}
2007ad9a3668SMajd Dibbiny 	mutex_unlock(&context->vma_private_list_mutex);
2008ecc7d83bSMaor Gottlieb 	up_write(&owning_mm->mmap_sem);
20097c2344c3SMaor Gottlieb 	mmput(owning_mm);
20107c2344c3SMaor Gottlieb 	put_task_struct(owning_process);
20117c2344c3SMaor Gottlieb }
20127c2344c3SMaor Gottlieb 
201337aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2014e126ba97SEli Cohen {
201537aa5c36SGuy Levi 	switch (cmd) {
201637aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
201737aa5c36SGuy Levi 		return "WC";
2018e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
201937aa5c36SGuy Levi 		return "best effort WC";
202037aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
202137aa5c36SGuy Levi 		return "NC";
202224da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
202324da0016SAriel Levkovich 		return "Device Memory";
202437aa5c36SGuy Levi 	default:
202537aa5c36SGuy Levi 		return NULL;
202637aa5c36SGuy Levi 	}
202737aa5c36SGuy Levi }
202837aa5c36SGuy Levi 
20295c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20305c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20315c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20325c99eaecSFeras Daoud {
20335c99eaecSFeras Daoud 	phys_addr_t pfn;
20345c99eaecSFeras Daoud 	int err;
20355c99eaecSFeras Daoud 
20365c99eaecSFeras Daoud 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
20375c99eaecSFeras Daoud 		return -EINVAL;
20385c99eaecSFeras Daoud 
20395c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20405c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20415c99eaecSFeras Daoud 
20425c99eaecSFeras Daoud 	if (vma->vm_flags & VM_WRITE)
20435c99eaecSFeras Daoud 		return -EPERM;
20445c99eaecSFeras Daoud 
20455c99eaecSFeras Daoud 	if (!dev->mdev->clock_info_page)
20465c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20475c99eaecSFeras Daoud 
20485c99eaecSFeras Daoud 	pfn = page_to_pfn(dev->mdev->clock_info_page);
20495c99eaecSFeras Daoud 	err = remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE,
20505c99eaecSFeras Daoud 			      vma->vm_page_prot);
20515c99eaecSFeras Daoud 	if (err)
20525c99eaecSFeras Daoud 		return err;
20535c99eaecSFeras Daoud 
20545c99eaecSFeras Daoud 	mlx5_ib_dbg(dev, "mapped clock info at 0x%lx, PA 0x%llx\n",
20555c99eaecSFeras Daoud 		    vma->vm_start,
20565c99eaecSFeras Daoud 		    (unsigned long long)pfn << PAGE_SHIFT);
20575c99eaecSFeras Daoud 
20585c99eaecSFeras Daoud 	return mlx5_ib_set_vma_data(vma, context);
20595c99eaecSFeras Daoud }
20605c99eaecSFeras Daoud 
206137aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
20627c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
20637c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
206437aa5c36SGuy Levi {
20652f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
206637aa5c36SGuy Levi 	int err;
206737aa5c36SGuy Levi 	unsigned long idx;
206837aa5c36SGuy Levi 	phys_addr_t pfn, pa;
206937aa5c36SGuy Levi 	pgprot_t prot;
20704ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
20714ed131d0SYishai Hadas 	u32 uar_index;
20724ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
20734ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
20744ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2075b037c29aSEli Cohen 
2076b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2077b037c29aSEli Cohen 		return -EINVAL;
2078b037c29aSEli Cohen 
20794ed131d0SYishai Hadas 	if (dyn_uar)
20804ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
20814ed131d0SYishai Hadas 	else
2082b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
20834ed131d0SYishai Hadas 
20844ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
20854ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
20864ed131d0SYishai Hadas 			     idx, max_valid_idx);
2087b037c29aSEli Cohen 		return -EINVAL;
2088b037c29aSEli Cohen 	}
208937aa5c36SGuy Levi 
209037aa5c36SGuy Levi 	switch (cmd) {
209137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
20924ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
209337aa5c36SGuy Levi /* Some architectures don't support WC memory */
209437aa5c36SGuy Levi #if defined(CONFIG_X86)
209537aa5c36SGuy Levi 		if (!pat_enabled())
209637aa5c36SGuy Levi 			return -EPERM;
209737aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
209837aa5c36SGuy Levi 			return -EPERM;
209937aa5c36SGuy Levi #endif
210037aa5c36SGuy Levi 	/* fall through */
210137aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
210237aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
210337aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
210437aa5c36SGuy Levi 		break;
210537aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
210637aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
210737aa5c36SGuy Levi 		break;
210837aa5c36SGuy Levi 	default:
210937aa5c36SGuy Levi 		return -EINVAL;
211037aa5c36SGuy Levi 	}
211137aa5c36SGuy Levi 
21124ed131d0SYishai Hadas 	if (dyn_uar) {
21134ed131d0SYishai Hadas 		int uars_per_page;
21144ed131d0SYishai Hadas 
21154ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21164ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21174ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21184ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21194ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21204ed131d0SYishai Hadas 			return -EINVAL;
21214ed131d0SYishai Hadas 		}
21224ed131d0SYishai Hadas 
21234ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21244ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21254ed131d0SYishai Hadas 		 * page holds its count.
21264ed131d0SYishai Hadas 		 */
21274ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
21284ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
21294ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
21304ed131d0SYishai Hadas 			return -EINVAL;
21314ed131d0SYishai Hadas 		}
21324ed131d0SYishai Hadas 
21334ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
21344ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
21354ed131d0SYishai Hadas 
21364ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
21374ed131d0SYishai Hadas 		if (err) {
21384ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
21394ed131d0SYishai Hadas 			goto free_bfreg;
21404ed131d0SYishai Hadas 		}
21414ed131d0SYishai Hadas 	} else {
21424ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
21434ed131d0SYishai Hadas 	}
21444ed131d0SYishai Hadas 
21454ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
214637aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2147e126ba97SEli Cohen 
214837aa5c36SGuy Levi 	vma->vm_page_prot = prot;
214937aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
215037aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
215137aa5c36SGuy Levi 	if (err) {
215237aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
215337aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
21544ed131d0SYishai Hadas 		err = -EAGAIN;
21554ed131d0SYishai Hadas 		goto err;
215637aa5c36SGuy Levi 	}
2157e126ba97SEli Cohen 
215837aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
215937aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
216037aa5c36SGuy Levi 		    vma->vm_start, &pa);
216137aa5c36SGuy Levi 
21624ed131d0SYishai Hadas 	err = mlx5_ib_set_vma_data(vma, context);
21634ed131d0SYishai Hadas 	if (err)
21644ed131d0SYishai Hadas 		goto err;
21654ed131d0SYishai Hadas 
21664ed131d0SYishai Hadas 	if (dyn_uar)
21674ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
21684ed131d0SYishai Hadas 	return 0;
21694ed131d0SYishai Hadas 
21704ed131d0SYishai Hadas err:
21714ed131d0SYishai Hadas 	if (!dyn_uar)
21724ed131d0SYishai Hadas 		return err;
21734ed131d0SYishai Hadas 
21744ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
21754ed131d0SYishai Hadas 
21764ed131d0SYishai Hadas free_bfreg:
21774ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
21784ed131d0SYishai Hadas 
21794ed131d0SYishai Hadas 	return err;
218037aa5c36SGuy Levi }
218137aa5c36SGuy Levi 
218224da0016SAriel Levkovich static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
218324da0016SAriel Levkovich {
218424da0016SAriel Levkovich 	struct mlx5_ib_ucontext *mctx = to_mucontext(context);
218524da0016SAriel Levkovich 	struct mlx5_ib_dev *dev = to_mdev(context->device);
218624da0016SAriel Levkovich 	u16 page_idx = get_extended_index(vma->vm_pgoff);
218724da0016SAriel Levkovich 	size_t map_size = vma->vm_end - vma->vm_start;
218824da0016SAriel Levkovich 	u32 npages = map_size >> PAGE_SHIFT;
218924da0016SAriel Levkovich 	phys_addr_t pfn;
219024da0016SAriel Levkovich 	pgprot_t prot;
219124da0016SAriel Levkovich 
219224da0016SAriel Levkovich 	if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
219324da0016SAriel Levkovich 	    page_idx + npages)
219424da0016SAriel Levkovich 		return -EINVAL;
219524da0016SAriel Levkovich 
219624da0016SAriel Levkovich 	pfn = ((pci_resource_start(dev->mdev->pdev, 0) +
219724da0016SAriel Levkovich 	      MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
219824da0016SAriel Levkovich 	      PAGE_SHIFT) +
219924da0016SAriel Levkovich 	      page_idx;
220024da0016SAriel Levkovich 	prot = pgprot_writecombine(vma->vm_page_prot);
220124da0016SAriel Levkovich 	vma->vm_page_prot = prot;
220224da0016SAriel Levkovich 
220324da0016SAriel Levkovich 	if (io_remap_pfn_range(vma, vma->vm_start, pfn, map_size,
220424da0016SAriel Levkovich 			       vma->vm_page_prot))
220524da0016SAriel Levkovich 		return -EAGAIN;
220624da0016SAriel Levkovich 
220724da0016SAriel Levkovich 	return mlx5_ib_set_vma_data(vma, mctx);
220824da0016SAriel Levkovich }
220924da0016SAriel Levkovich 
221037aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
221137aa5c36SGuy Levi {
221237aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
221337aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
221437aa5c36SGuy Levi 	unsigned long command;
221537aa5c36SGuy Levi 	phys_addr_t pfn;
221637aa5c36SGuy Levi 
221737aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
221837aa5c36SGuy Levi 	switch (command) {
221937aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
222037aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
222137aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
22224ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
22237c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2224e126ba97SEli Cohen 
2225e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2226e126ba97SEli Cohen 		return -ENOSYS;
2227e126ba97SEli Cohen 
2228d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2229d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2230d69e3bcfSMatan Barak 			return -EINVAL;
2231d69e3bcfSMatan Barak 
22326cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2233d69e3bcfSMatan Barak 			return -EPERM;
2234d69e3bcfSMatan Barak 
2235d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2236d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2237d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2238d69e3bcfSMatan Barak 
2239d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2240d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2241d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2242d69e3bcfSMatan Barak 			PAGE_SHIFT;
2243d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2244d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
2245d69e3bcfSMatan Barak 			return -EAGAIN;
2246d69e3bcfSMatan Barak 
2247d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
2248d69e3bcfSMatan Barak 			    vma->vm_start,
2249d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
2250d69e3bcfSMatan Barak 		break;
22515c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
22525c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2253d69e3bcfSMatan Barak 
225424da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
225524da0016SAriel Levkovich 		return dm_mmap(ibcontext, vma);
225624da0016SAriel Levkovich 
2257e126ba97SEli Cohen 	default:
2258e126ba97SEli Cohen 		return -EINVAL;
2259e126ba97SEli Cohen 	}
2260e126ba97SEli Cohen 
2261e126ba97SEli Cohen 	return 0;
2262e126ba97SEli Cohen }
2263e126ba97SEli Cohen 
226424da0016SAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
226524da0016SAriel Levkovich 			       struct ib_ucontext *context,
226624da0016SAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
226724da0016SAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
226824da0016SAriel Levkovich {
226924da0016SAriel Levkovich 	u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
227024da0016SAriel Levkovich 	struct mlx5_memic *memic = &to_mdev(ibdev)->memic;
227124da0016SAriel Levkovich 	phys_addr_t memic_addr;
227224da0016SAriel Levkovich 	struct mlx5_ib_dm *dm;
227324da0016SAriel Levkovich 	u64 start_offset;
227424da0016SAriel Levkovich 	u32 page_idx;
227524da0016SAriel Levkovich 	int err;
227624da0016SAriel Levkovich 
227724da0016SAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
227824da0016SAriel Levkovich 	if (!dm)
227924da0016SAriel Levkovich 		return ERR_PTR(-ENOMEM);
228024da0016SAriel Levkovich 
228124da0016SAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
228224da0016SAriel Levkovich 		    attr->length, act_size, attr->alignment);
228324da0016SAriel Levkovich 
228424da0016SAriel Levkovich 	err = mlx5_cmd_alloc_memic(memic, &memic_addr,
228524da0016SAriel Levkovich 				   act_size, attr->alignment);
228624da0016SAriel Levkovich 	if (err)
228724da0016SAriel Levkovich 		goto err_free;
228824da0016SAriel Levkovich 
228924da0016SAriel Levkovich 	start_offset = memic_addr & ~PAGE_MASK;
229024da0016SAriel Levkovich 	page_idx = (memic_addr - pci_resource_start(memic->dev->pdev, 0) -
229124da0016SAriel Levkovich 		    MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
229224da0016SAriel Levkovich 		    PAGE_SHIFT;
229324da0016SAriel Levkovich 
229424da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
229524da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
229624da0016SAriel Levkovich 			     &start_offset, sizeof(start_offset));
229724da0016SAriel Levkovich 	if (err)
229824da0016SAriel Levkovich 		goto err_dealloc;
229924da0016SAriel Levkovich 
230024da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
230124da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
230224da0016SAriel Levkovich 			     &page_idx, sizeof(page_idx));
230324da0016SAriel Levkovich 	if (err)
230424da0016SAriel Levkovich 		goto err_dealloc;
230524da0016SAriel Levkovich 
230624da0016SAriel Levkovich 	bitmap_set(to_mucontext(context)->dm_pages, page_idx,
230724da0016SAriel Levkovich 		   DIV_ROUND_UP(act_size, PAGE_SIZE));
230824da0016SAriel Levkovich 
230924da0016SAriel Levkovich 	dm->dev_addr = memic_addr;
231024da0016SAriel Levkovich 
231124da0016SAriel Levkovich 	return &dm->ibdm;
231224da0016SAriel Levkovich 
231324da0016SAriel Levkovich err_dealloc:
231424da0016SAriel Levkovich 	mlx5_cmd_dealloc_memic(memic, memic_addr,
231524da0016SAriel Levkovich 			       act_size);
231624da0016SAriel Levkovich err_free:
231724da0016SAriel Levkovich 	kfree(dm);
231824da0016SAriel Levkovich 	return ERR_PTR(err);
231924da0016SAriel Levkovich }
232024da0016SAriel Levkovich 
232124da0016SAriel Levkovich int mlx5_ib_dealloc_dm(struct ib_dm *ibdm)
232224da0016SAriel Levkovich {
232324da0016SAriel Levkovich 	struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic;
232424da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
232524da0016SAriel Levkovich 	u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE);
232624da0016SAriel Levkovich 	u32 page_idx;
232724da0016SAriel Levkovich 	int ret;
232824da0016SAriel Levkovich 
232924da0016SAriel Levkovich 	ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size);
233024da0016SAriel Levkovich 	if (ret)
233124da0016SAriel Levkovich 		return ret;
233224da0016SAriel Levkovich 
233324da0016SAriel Levkovich 	page_idx = (dm->dev_addr - pci_resource_start(memic->dev->pdev, 0) -
233424da0016SAriel Levkovich 		    MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
233524da0016SAriel Levkovich 		    PAGE_SHIFT;
233624da0016SAriel Levkovich 	bitmap_clear(to_mucontext(ibdm->uobject->context)->dm_pages,
233724da0016SAriel Levkovich 		     page_idx,
233824da0016SAriel Levkovich 		     DIV_ROUND_UP(act_size, PAGE_SIZE));
233924da0016SAriel Levkovich 
234024da0016SAriel Levkovich 	kfree(dm);
234124da0016SAriel Levkovich 
234224da0016SAriel Levkovich 	return 0;
234324da0016SAriel Levkovich }
234424da0016SAriel Levkovich 
2345e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
2346e126ba97SEli Cohen 				      struct ib_ucontext *context,
2347e126ba97SEli Cohen 				      struct ib_udata *udata)
2348e126ba97SEli Cohen {
2349e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2350e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
2351e126ba97SEli Cohen 	int err;
2352e126ba97SEli Cohen 
2353e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
2354e126ba97SEli Cohen 	if (!pd)
2355e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
2356e126ba97SEli Cohen 
23579603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
2358e126ba97SEli Cohen 	if (err) {
2359e126ba97SEli Cohen 		kfree(pd);
2360e126ba97SEli Cohen 		return ERR_PTR(err);
2361e126ba97SEli Cohen 	}
2362e126ba97SEli Cohen 
2363e126ba97SEli Cohen 	if (context) {
2364e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2365e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
23669603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
2367e126ba97SEli Cohen 			kfree(pd);
2368e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
2369e126ba97SEli Cohen 		}
2370e126ba97SEli Cohen 	}
2371e126ba97SEli Cohen 
2372e126ba97SEli Cohen 	return &pd->ibpd;
2373e126ba97SEli Cohen }
2374e126ba97SEli Cohen 
2375e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
2376e126ba97SEli Cohen {
2377e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2378e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2379e126ba97SEli Cohen 
23809603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
2381e126ba97SEli Cohen 	kfree(mpd);
2382e126ba97SEli Cohen 
2383e126ba97SEli Cohen 	return 0;
2384e126ba97SEli Cohen }
2385e126ba97SEli Cohen 
2386466fa6d2SMaor Gottlieb enum {
2387466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2388466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
2389466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_INNER_BIT
2390466fa6d2SMaor Gottlieb };
2391038d2ef8SMaor Gottlieb 
2392466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2393466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2394466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2395466fa6d2SMaor Gottlieb 
2396466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2397466fa6d2SMaor Gottlieb {
2398466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2399466fa6d2SMaor Gottlieb 
2400466fa6d2SMaor Gottlieb 	match_criteria_enable =
2401466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2402466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2403466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2404466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2405466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2406466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2407466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2408466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
2409466fa6d2SMaor Gottlieb 
2410466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2411038d2ef8SMaor Gottlieb }
2412038d2ef8SMaor Gottlieb 
2413ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2414ca0d4753SMaor Gottlieb {
2415ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2416ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2417ca0d4753SMaor Gottlieb }
2418ca0d4753SMaor Gottlieb 
2419*37da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
24202d1e697eSMoses Reuben 			   bool inner)
24212d1e697eSMoses Reuben {
24222d1e697eSMoses Reuben 	if (inner) {
24232d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24242d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
24252d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24262d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
24272d1e697eSMoses Reuben 	} else {
24282d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24292d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
24302d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24312d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
24322d1e697eSMoses Reuben 	}
24332d1e697eSMoses Reuben }
24342d1e697eSMoses Reuben 
2435ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2436ca0d4753SMaor Gottlieb {
2437ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2438ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2439ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2440ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2441ca0d4753SMaor Gottlieb }
2442ca0d4753SMaor Gottlieb 
2443c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2444c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2445ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2446466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2447c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2448ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
24492ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2450a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
2451c47ac6aeSMaor Gottlieb 
2452c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2453c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2454c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2455c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2456c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2457c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2458c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2459c47ac6aeSMaor Gottlieb 
2460802c2125SAviad Yehezkel static int parse_flow_flow_action(const union ib_flow_spec *ib_spec,
2461802c2125SAviad Yehezkel 				  const struct ib_flow_attr *flow_attr,
2462802c2125SAviad Yehezkel 				  struct mlx5_flow_act *action)
2463802c2125SAviad Yehezkel {
2464802c2125SAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(ib_spec->action.act);
2465802c2125SAviad Yehezkel 
2466802c2125SAviad Yehezkel 	switch (maction->ib_action.type) {
2467802c2125SAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
2468802c2125SAviad Yehezkel 		/* Currently only AES_GCM keymat is supported by the driver */
2469802c2125SAviad Yehezkel 		action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
2470802c2125SAviad Yehezkel 		action->action |= flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS ?
2471802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2472802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2473802c2125SAviad Yehezkel 		return 0;
2474802c2125SAviad Yehezkel 	default:
2475802c2125SAviad Yehezkel 		return -EOPNOTSUPP;
2476802c2125SAviad Yehezkel 	}
2477802c2125SAviad Yehezkel }
2478802c2125SAviad Yehezkel 
247919cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
248019cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
2481802c2125SAviad Yehezkel 			   const struct ib_flow_attr *flow_attr,
2482075572d4SBoris Pismenny 			   struct mlx5_flow_act *action)
2483038d2ef8SMaor Gottlieb {
2484466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2485466fa6d2SMaor Gottlieb 					   misc_parameters);
2486466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2487466fa6d2SMaor Gottlieb 					   misc_parameters);
24882d1e697eSMoses Reuben 	void *headers_c;
24892d1e697eSMoses Reuben 	void *headers_v;
249019cc7524SAriel Levkovich 	int match_ipv;
2491802c2125SAviad Yehezkel 	int ret;
2492466fa6d2SMaor Gottlieb 
24932d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
24942d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
24952d1e697eSMoses Reuben 					 inner_headers);
24962d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
24972d1e697eSMoses Reuben 					 inner_headers);
249819cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
249919cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
25002d1e697eSMoses Reuben 	} else {
25012d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
25022d1e697eSMoses Reuben 					 outer_headers);
25032d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
25042d1e697eSMoses Reuben 					 outer_headers);
250519cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
250619cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
25072d1e697eSMoses Reuben 	}
25082d1e697eSMoses Reuben 
25092d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2510038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2511c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
25121ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2513038d2ef8SMaor Gottlieb 
25142d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2515038d2ef8SMaor Gottlieb 					     dmac_47_16),
2516038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
25172d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2518038d2ef8SMaor Gottlieb 					     dmac_47_16),
2519038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2520038d2ef8SMaor Gottlieb 
25212d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2522ee3da804SMaor Gottlieb 					     smac_47_16),
2523ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
25242d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2525ee3da804SMaor Gottlieb 					     smac_47_16),
2526ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2527ee3da804SMaor Gottlieb 
2528038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
25292d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
253010543365SMohamad Haj Yahia 				 cvlan_tag, 1);
25312d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
253210543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2533038d2ef8SMaor Gottlieb 
25342d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2535038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
25362d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2537038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2538038d2ef8SMaor Gottlieb 
25392d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2540038d2ef8SMaor Gottlieb 				 first_cfi,
2541038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
25422d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2543038d2ef8SMaor Gottlieb 				 first_cfi,
2544038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2545038d2ef8SMaor Gottlieb 
25462d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2547038d2ef8SMaor Gottlieb 				 first_prio,
2548038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
25492d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2550038d2ef8SMaor Gottlieb 				 first_prio,
2551038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2552038d2ef8SMaor Gottlieb 		}
25532d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2554038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
25552d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2556038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2557038d2ef8SMaor Gottlieb 		break;
2558038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2559c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
25601ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2561038d2ef8SMaor Gottlieb 
256219cc7524SAriel Levkovich 		if (match_ipv) {
256319cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
256419cc7524SAriel Levkovich 				 ip_version, 0xf);
256519cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
25663346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV4_VERSION);
256719cc7524SAriel Levkovich 		} else {
25682d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2569038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
25702d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2571038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
257219cc7524SAriel Levkovich 		}
2573038d2ef8SMaor Gottlieb 
25742d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2575038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2576038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2577038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
25782d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2579038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2580038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2581038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
25822d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2583038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2584038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2585038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
25862d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2587038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2588038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2589038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2590ca0d4753SMaor Gottlieb 
25912d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2592ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2593ca0d4753SMaor Gottlieb 
25942d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2595ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
2596038d2ef8SMaor Gottlieb 		break;
2597026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2598c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
25991ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2600026bae0cSMaor Gottlieb 
260119cc7524SAriel Levkovich 		if (match_ipv) {
260219cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
260319cc7524SAriel Levkovich 				 ip_version, 0xf);
260419cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
26053346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV6_VERSION);
260619cc7524SAriel Levkovich 		} else {
26072d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2608026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
26092d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2610026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
261119cc7524SAriel Levkovich 		}
2612026bae0cSMaor Gottlieb 
26132d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2614026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2615026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2616026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
26172d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2618026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2619026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2620026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
26212d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2622026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2623026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2624026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
26252d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2626026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2627026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2628026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2629466fa6d2SMaor Gottlieb 
26302d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2631466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2632466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2633466fa6d2SMaor Gottlieb 
26342d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2635466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
2636466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
2637466fa6d2SMaor Gottlieb 
26382d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
26392d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
26402d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
26412d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
2642802c2125SAviad Yehezkel 		break;
2643802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ESP:
2644802c2125SAviad Yehezkel 		if (ib_spec->esp.mask.seq)
2645802c2125SAviad Yehezkel 			return -EOPNOTSUPP;
26462d1e697eSMoses Reuben 
2647802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2648802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.mask.spi));
2649802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2650802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.val.spi));
2651026bae0cSMaor Gottlieb 		break;
2652038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2653c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2654c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
26551ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2656038d2ef8SMaor Gottlieb 
26572d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2658038d2ef8SMaor Gottlieb 			 0xff);
26592d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2660038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
2661038d2ef8SMaor Gottlieb 
26622d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2663038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
26642d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2665038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2666038d2ef8SMaor Gottlieb 
26672d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2668038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
26692d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2670038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2671038d2ef8SMaor Gottlieb 		break;
2672038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2673c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2674c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
26751ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2676038d2ef8SMaor Gottlieb 
26772d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2678038d2ef8SMaor Gottlieb 			 0xff);
26792d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2680038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
2681038d2ef8SMaor Gottlieb 
26822d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2683038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
26842d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2685038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2686038d2ef8SMaor Gottlieb 
26872d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2688038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
26892d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2690038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2691038d2ef8SMaor Gottlieb 		break;
2692ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2693ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2694ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
26951ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2696ffb30d8fSMoses Reuben 
2697ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2698ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2699ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2700ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2701ffb30d8fSMoses Reuben 		break;
27022ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
27032ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
27042ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
27052ac693f9SMoses Reuben 			return -EOPNOTSUPP;
27062ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
27072ac693f9SMoses Reuben 			return -EINVAL;
27082ac693f9SMoses Reuben 
2709075572d4SBoris Pismenny 		action->flow_tag = ib_spec->flow_tag.tag_id;
2710a9db0ecfSMatan Barak 		action->has_flow_tag = true;
27112ac693f9SMoses Reuben 		break;
2712a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2713a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2714a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2715a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2716075572d4SBoris Pismenny 		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
2717a22ed86cSSlava Shwartsman 		break;
2718802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ACTION_HANDLE:
2719802c2125SAviad Yehezkel 		ret = parse_flow_flow_action(ib_spec, flow_attr, action);
2720802c2125SAviad Yehezkel 		if (ret)
2721802c2125SAviad Yehezkel 			return ret;
2722802c2125SAviad Yehezkel 		break;
2723038d2ef8SMaor Gottlieb 	default:
2724038d2ef8SMaor Gottlieb 		return -EINVAL;
2725038d2ef8SMaor Gottlieb 	}
2726038d2ef8SMaor Gottlieb 
2727038d2ef8SMaor Gottlieb 	return 0;
2728038d2ef8SMaor Gottlieb }
2729038d2ef8SMaor Gottlieb 
2730038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
2731038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
2732038d2ef8SMaor Gottlieb  * could steal other multicast packets.
2733038d2ef8SMaor Gottlieb  */
2734a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2735038d2ef8SMaor Gottlieb {
273681e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
2737038d2ef8SMaor Gottlieb 
2738038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2739038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
2740038d2ef8SMaor Gottlieb 		return false;
2741038d2ef8SMaor Gottlieb 
274281e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
274381e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
274481e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
2745038d2ef8SMaor Gottlieb 
274681e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
274781e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
274881e30880SYishai Hadas 			return true;
274981e30880SYishai Hadas 
275081e30880SYishai Hadas 		return false;
275181e30880SYishai Hadas 	}
275281e30880SYishai Hadas 
275381e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
275481e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
275581e30880SYishai Hadas 
275681e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2757038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2758038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
2759038d2ef8SMaor Gottlieb 	}
2760038d2ef8SMaor Gottlieb 
276181e30880SYishai Hadas 	return false;
276281e30880SYishai Hadas }
276381e30880SYishai Hadas 
2764802c2125SAviad Yehezkel enum valid_spec {
2765802c2125SAviad Yehezkel 	VALID_SPEC_INVALID,
2766802c2125SAviad Yehezkel 	VALID_SPEC_VALID,
2767802c2125SAviad Yehezkel 	VALID_SPEC_NA,
2768802c2125SAviad Yehezkel };
2769802c2125SAviad Yehezkel 
2770802c2125SAviad Yehezkel static enum valid_spec
2771802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
2772802c2125SAviad Yehezkel 		     const struct mlx5_flow_spec *spec,
2773802c2125SAviad Yehezkel 		     const struct mlx5_flow_act *flow_act,
2774802c2125SAviad Yehezkel 		     bool egress)
2775802c2125SAviad Yehezkel {
2776802c2125SAviad Yehezkel 	const u32 *match_c = spec->match_criteria;
2777802c2125SAviad Yehezkel 	bool is_crypto =
2778802c2125SAviad Yehezkel 		(flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2779802c2125SAviad Yehezkel 				     MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
2780802c2125SAviad Yehezkel 	bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
2781802c2125SAviad Yehezkel 	bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
2782802c2125SAviad Yehezkel 
2783802c2125SAviad Yehezkel 	/*
2784802c2125SAviad Yehezkel 	 * Currently only crypto is supported in egress, when regular egress
2785802c2125SAviad Yehezkel 	 * rules would be supported, always return VALID_SPEC_NA.
2786802c2125SAviad Yehezkel 	 */
2787802c2125SAviad Yehezkel 	if (!is_crypto)
2788802c2125SAviad Yehezkel 		return egress ? VALID_SPEC_INVALID : VALID_SPEC_NA;
2789802c2125SAviad Yehezkel 
2790802c2125SAviad Yehezkel 	return is_crypto && is_ipsec &&
2791802c2125SAviad Yehezkel 		(!egress || (!is_drop && !flow_act->has_flow_tag)) ?
2792802c2125SAviad Yehezkel 		VALID_SPEC_VALID : VALID_SPEC_INVALID;
2793802c2125SAviad Yehezkel }
2794802c2125SAviad Yehezkel 
2795802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev,
2796802c2125SAviad Yehezkel 			  const struct mlx5_flow_spec *spec,
2797802c2125SAviad Yehezkel 			  const struct mlx5_flow_act *flow_act,
2798802c2125SAviad Yehezkel 			  bool egress)
2799802c2125SAviad Yehezkel {
2800802c2125SAviad Yehezkel 	/* We curretly only support ipsec egress flow */
2801802c2125SAviad Yehezkel 	return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
2802802c2125SAviad Yehezkel }
2803802c2125SAviad Yehezkel 
280419cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
280519cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
28060f750966SAriel Levkovich 			       bool check_inner)
2807038d2ef8SMaor Gottlieb {
2808038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
280919cc7524SAriel Levkovich 	int match_ipv = check_inner ?
281019cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
281119cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
281219cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
281319cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
28140f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
28150f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
28160f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
28170f750966SAriel Levkovich 	bool has_ethertype = false;
2818038d2ef8SMaor Gottlieb 	unsigned int spec_index;
28190f750966SAriel Levkovich 	bool mask_valid = true;
28200f750966SAriel Levkovich 	u16 eth_type = 0;
28210f750966SAriel Levkovich 	bool type_valid;
2822038d2ef8SMaor Gottlieb 
2823038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
2824038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
28250f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2826038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
28270f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
28280f750966SAriel Levkovich 				      htons(0xffff));
28290f750966SAriel Levkovich 			has_ethertype = true;
28300f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
28310f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
28320f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
28330f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
2834038d2ef8SMaor Gottlieb 		}
2835038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
2836038d2ef8SMaor Gottlieb 	}
28370f750966SAriel Levkovich 
28380f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
28390f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
28400f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
28410f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
28420f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
28430f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
284419cc7524SAriel Levkovich 
284519cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
284619cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
284719cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
28480f750966SAriel Levkovich 	}
28490f750966SAriel Levkovich 
28500f750966SAriel Levkovich 	return type_valid;
28510f750966SAriel Levkovich }
28520f750966SAriel Levkovich 
285319cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
285419cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
28550f750966SAriel Levkovich {
285619cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
285719cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
2858038d2ef8SMaor Gottlieb }
2859038d2ef8SMaor Gottlieb 
2860038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
2861038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
2862038d2ef8SMaor Gottlieb {
2863038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
2864038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
2865038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
2866038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
2867038d2ef8SMaor Gottlieb 	}
2868038d2ef8SMaor Gottlieb }
2869038d2ef8SMaor Gottlieb 
2870038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2871038d2ef8SMaor Gottlieb {
2872038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2873038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2874038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2875038d2ef8SMaor Gottlieb 							  ibflow);
2876038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2877038d2ef8SMaor Gottlieb 
28789a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
2879038d2ef8SMaor Gottlieb 
2880038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
288174491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2882cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
2883038d2ef8SMaor Gottlieb 		list_del(&iter->list);
2884038d2ef8SMaor Gottlieb 		kfree(iter);
2885038d2ef8SMaor Gottlieb 	}
2886038d2ef8SMaor Gottlieb 
288774491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
28885497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
28899a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
2890038d2ef8SMaor Gottlieb 
2891038d2ef8SMaor Gottlieb 	kfree(handler);
2892038d2ef8SMaor Gottlieb 
2893038d2ef8SMaor Gottlieb 	return 0;
2894038d2ef8SMaor Gottlieb }
2895038d2ef8SMaor Gottlieb 
289635d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
289735d19011SMaor Gottlieb {
289835d19011SMaor Gottlieb 	priority *= 2;
289935d19011SMaor Gottlieb 	if (!dont_trap)
290035d19011SMaor Gottlieb 		priority++;
290135d19011SMaor Gottlieb 	return priority;
290235d19011SMaor Gottlieb }
290335d19011SMaor Gottlieb 
2904cc0e5d42SMaor Gottlieb enum flow_table_type {
2905cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
2906cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
2907cc0e5d42SMaor Gottlieb };
2908cc0e5d42SMaor Gottlieb 
290900b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
291000b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
2911038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
2912cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
2913cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
2914038d2ef8SMaor Gottlieb {
291535d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
2916038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
2917038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
2918038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
2919dac388efSMaor Gottlieb 	int max_table_size;
2920038d2ef8SMaor Gottlieb 	int num_entries;
2921038d2ef8SMaor Gottlieb 	int num_groups;
2922038d2ef8SMaor Gottlieb 	int priority;
2923038d2ef8SMaor Gottlieb 	int err = 0;
2924038d2ef8SMaor Gottlieb 
2925dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2926dac388efSMaor Gottlieb 						       log_max_ft_size));
2927038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
2928802c2125SAviad Yehezkel 		if (ft_type == MLX5_IB_FT_TX)
2929802c2125SAviad Yehezkel 			priority = 0;
2930802c2125SAviad Yehezkel 		else if (flow_is_multicast_only(flow_attr) &&
293135d19011SMaor Gottlieb 			 !dont_trap)
2932038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
2933038d2ef8SMaor Gottlieb 		else
293435d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
293535d19011SMaor Gottlieb 							dont_trap);
2936038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2937802c2125SAviad Yehezkel 					     ft_type == MLX5_IB_FT_TX ?
2938802c2125SAviad Yehezkel 					     MLX5_FLOW_NAMESPACE_EGRESS :
2939038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
2940038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
2941038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
29429a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[priority];
2943038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2944038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2945038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2946038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
2947038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
2948038d2ef8SMaor Gottlieb 					 &num_entries,
2949038d2ef8SMaor Gottlieb 					 &num_groups);
29509a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
2951cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2952cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2953cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
2954cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
2955cc0e5d42SMaor Gottlieb 
2956cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2957cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2958cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2959cc0e5d42SMaor Gottlieb 
29609a4ca38dSMark Bloch 		prio = &dev->flow_db->sniffer[ft_type];
2961cc0e5d42SMaor Gottlieb 		priority = 0;
2962cc0e5d42SMaor Gottlieb 		num_entries = 1;
2963cc0e5d42SMaor Gottlieb 		num_groups = 1;
2964038d2ef8SMaor Gottlieb 	}
2965038d2ef8SMaor Gottlieb 
2966038d2ef8SMaor Gottlieb 	if (!ns)
2967038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
2968038d2ef8SMaor Gottlieb 
2969dac388efSMaor Gottlieb 	if (num_entries > max_table_size)
2970dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2971dac388efSMaor Gottlieb 
2972038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
2973038d2ef8SMaor Gottlieb 	if (!ft) {
2974038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2975038d2ef8SMaor Gottlieb 							 num_entries,
2976d63cd286SMaor Gottlieb 							 num_groups,
2977c9f1b073SHadar Hen Zion 							 0, 0);
2978038d2ef8SMaor Gottlieb 
2979038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
2980038d2ef8SMaor Gottlieb 			prio->refcount = 0;
2981038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
2982038d2ef8SMaor Gottlieb 		} else {
2983038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
2984038d2ef8SMaor Gottlieb 		}
2985038d2ef8SMaor Gottlieb 	}
2986038d2ef8SMaor Gottlieb 
2987038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
2988038d2ef8SMaor Gottlieb }
2989038d2ef8SMaor Gottlieb 
2990a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
2991a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
2992a550ddfcSYishai Hadas 			    u32 underlay_qpn)
2993a550ddfcSYishai Hadas {
2994a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2995a550ddfcSYishai Hadas 					   spec->match_criteria,
2996a550ddfcSYishai Hadas 					   misc_parameters);
2997a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2998a550ddfcSYishai Hadas 					   misc_parameters);
2999a550ddfcSYishai Hadas 
3000a550ddfcSYishai Hadas 	if (underlay_qpn &&
3001a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3002a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
3003a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3004a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
3005a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3006a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
3007a550ddfcSYishai Hadas 	}
3008a550ddfcSYishai Hadas }
3009a550ddfcSYishai Hadas 
3010a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3011038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
3012dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
3013a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
3014a550ddfcSYishai Hadas 						      u32 underlay_qpn)
3015038d2ef8SMaor Gottlieb {
3016038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
3017038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
3018075572d4SBoris Pismenny 	struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
3019c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
3020a22ed86cSSlava Shwartsman 	struct mlx5_flow_destination *rule_dst = dst;
3021dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3022038d2ef8SMaor Gottlieb 	unsigned int spec_index;
3023038d2ef8SMaor Gottlieb 	int err = 0;
3024a22ed86cSSlava Shwartsman 	int dest_num = 1;
3025802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3026038d2ef8SMaor Gottlieb 
302719cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
3028038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3029038d2ef8SMaor Gottlieb 
30301b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3031038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3032c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
3033038d2ef8SMaor Gottlieb 		err = -ENOMEM;
3034038d2ef8SMaor Gottlieb 		goto free;
3035038d2ef8SMaor Gottlieb 	}
3036038d2ef8SMaor Gottlieb 
3037038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
3038038d2ef8SMaor Gottlieb 
3039038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
304019cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
3041a22ed86cSSlava Shwartsman 				      spec->match_value,
3042802c2125SAviad Yehezkel 				      ib_flow, flow_attr, &flow_act);
3043038d2ef8SMaor Gottlieb 		if (err < 0)
3044038d2ef8SMaor Gottlieb 			goto free;
3045038d2ef8SMaor Gottlieb 
3046038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3047038d2ef8SMaor Gottlieb 	}
3048038d2ef8SMaor Gottlieb 
3049a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
3050a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
3051a550ddfcSYishai Hadas 
3052018a94eeSMark Bloch 	if (dev->rep) {
3053018a94eeSMark Bloch 		void *misc;
3054018a94eeSMark Bloch 
3055018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3056018a94eeSMark Bloch 				    misc_parameters);
3057018a94eeSMark Bloch 		MLX5_SET(fte_match_set_misc, misc, source_port,
3058018a94eeSMark Bloch 			 dev->rep->vport);
3059018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3060018a94eeSMark Bloch 				    misc_parameters);
3061018a94eeSMark Bloch 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3062018a94eeSMark Bloch 	}
3063018a94eeSMark Bloch 
3064466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3065802c2125SAviad Yehezkel 
3066802c2125SAviad Yehezkel 	if (is_egress &&
3067802c2125SAviad Yehezkel 	    !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3068802c2125SAviad Yehezkel 		err = -EINVAL;
3069802c2125SAviad Yehezkel 		goto free;
3070802c2125SAviad Yehezkel 	}
3071802c2125SAviad Yehezkel 
3072075572d4SBoris Pismenny 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
3073a22ed86cSSlava Shwartsman 		rule_dst = NULL;
3074a22ed86cSSlava Shwartsman 		dest_num = 0;
3075a22ed86cSSlava Shwartsman 	} else {
3076802c2125SAviad Yehezkel 		if (is_egress)
3077802c2125SAviad Yehezkel 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3078802c2125SAviad Yehezkel 		else
3079802c2125SAviad Yehezkel 			flow_act.action |=
3080802c2125SAviad Yehezkel 				dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
308135d19011SMaor Gottlieb 					MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3082a22ed86cSSlava Shwartsman 	}
30832ac693f9SMoses Reuben 
3084a9db0ecfSMatan Barak 	if (flow_act.has_flow_tag &&
30852ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
30862ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
30872ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3088075572d4SBoris Pismenny 			     flow_act.flow_tag, flow_attr->type);
30892ac693f9SMoses Reuben 		err = -EINVAL;
30902ac693f9SMoses Reuben 		goto free;
30912ac693f9SMoses Reuben 	}
309274491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
309366958ed9SHadar Hen Zion 					    &flow_act,
3094a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
3095038d2ef8SMaor Gottlieb 
3096038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
3097038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
3098038d2ef8SMaor Gottlieb 		goto free;
3099038d2ef8SMaor Gottlieb 	}
3100038d2ef8SMaor Gottlieb 
3101d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
31025497adc6SMaor Gottlieb 	handler->prio = ft_prio;
3103038d2ef8SMaor Gottlieb 
3104038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
3105038d2ef8SMaor Gottlieb free:
3106038d2ef8SMaor Gottlieb 	if (err)
3107038d2ef8SMaor Gottlieb 		kfree(handler);
3108c5bb1730SMaor Gottlieb 	kvfree(spec);
3109038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
3110038d2ef8SMaor Gottlieb }
3111038d2ef8SMaor Gottlieb 
3112a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3113a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
3114a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
3115a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
3116a550ddfcSYishai Hadas {
3117a550ddfcSYishai Hadas 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
3118a550ddfcSYishai Hadas }
3119a550ddfcSYishai Hadas 
312035d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
312135d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
312235d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
312335d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
312435d19011SMaor Gottlieb {
312535d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
312635d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
312735d19011SMaor Gottlieb 
312835d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
312935d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
313035d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
313135d19011SMaor Gottlieb 					       flow_attr, dst);
313235d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
313374491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3134d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
313535d19011SMaor Gottlieb 			kfree(handler);
313635d19011SMaor Gottlieb 			handler = handler_dst;
313735d19011SMaor Gottlieb 		} else {
313835d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
313935d19011SMaor Gottlieb 		}
314035d19011SMaor Gottlieb 	}
314135d19011SMaor Gottlieb 
314235d19011SMaor Gottlieb 	return handler;
314335d19011SMaor Gottlieb }
3144038d2ef8SMaor Gottlieb enum {
3145038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
3146038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
3147038d2ef8SMaor Gottlieb };
3148038d2ef8SMaor Gottlieb 
3149038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3150038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
3151038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
3152038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
3153038d2ef8SMaor Gottlieb {
3154038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
3155038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3156038d2ef8SMaor Gottlieb 
3157038d2ef8SMaor Gottlieb 	static struct {
3158038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
3159038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
3160038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
3161038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
3162038d2ef8SMaor Gottlieb 			.flow_attr = {
3163038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3164038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3165038d2ef8SMaor Gottlieb 			},
3166038d2ef8SMaor Gottlieb 			.eth_flow = {
3167038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3168038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3169038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3170038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
3171038d2ef8SMaor Gottlieb 			}
3172038d2ef8SMaor Gottlieb 		},
3173038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
3174038d2ef8SMaor Gottlieb 			.flow_attr = {
3175038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3176038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3177038d2ef8SMaor Gottlieb 			},
3178038d2ef8SMaor Gottlieb 			.eth_flow = {
3179038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3180038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3181038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3182038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
3183038d2ef8SMaor Gottlieb 			}
3184038d2ef8SMaor Gottlieb 		}
3185038d2ef8SMaor Gottlieb 	};
3186038d2ef8SMaor Gottlieb 
3187038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
3188038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
3189038d2ef8SMaor Gottlieb 				   dst);
3190038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
3191038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3192038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
3193038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3194038d2ef8SMaor Gottlieb 						 dst);
3195038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
319674491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3197d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
3198038d2ef8SMaor Gottlieb 			kfree(handler);
3199038d2ef8SMaor Gottlieb 			handler = handler_ucast;
3200038d2ef8SMaor Gottlieb 		} else {
3201038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
3202038d2ef8SMaor Gottlieb 		}
3203038d2ef8SMaor Gottlieb 	}
3204038d2ef8SMaor Gottlieb 
3205038d2ef8SMaor Gottlieb 	return handler;
3206038d2ef8SMaor Gottlieb }
3207038d2ef8SMaor Gottlieb 
3208cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3209cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
3210cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
3211cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
3212cc0e5d42SMaor Gottlieb {
3213cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
3214cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
3215cc0e5d42SMaor Gottlieb 	int err;
3216cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
3217cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
3218cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
3219cc0e5d42SMaor Gottlieb 	};
3220cc0e5d42SMaor Gottlieb 
3221cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3222cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
3223cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
3224cc0e5d42SMaor Gottlieb 		goto err;
3225cc0e5d42SMaor Gottlieb 	}
3226cc0e5d42SMaor Gottlieb 
3227cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3228cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
3229cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
3230cc0e5d42SMaor Gottlieb 		goto err_tx;
3231cc0e5d42SMaor Gottlieb 	}
3232cc0e5d42SMaor Gottlieb 
3233cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
3234cc0e5d42SMaor Gottlieb 
3235cc0e5d42SMaor Gottlieb 	return handler_rx;
3236cc0e5d42SMaor Gottlieb 
3237cc0e5d42SMaor Gottlieb err_tx:
323874491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
3239cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
3240cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
3241cc0e5d42SMaor Gottlieb err:
3242cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
3243cc0e5d42SMaor Gottlieb }
3244cc0e5d42SMaor Gottlieb 
3245038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3246038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
3247038d2ef8SMaor Gottlieb 					   int domain)
3248038d2ef8SMaor Gottlieb {
3249038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3250d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
3251038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3252038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
3253cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3254038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
3255802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3256038d2ef8SMaor Gottlieb 	int err;
3257a550ddfcSYishai Hadas 	int underlay_qpn;
3258038d2ef8SMaor Gottlieb 
3259038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
3260dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
3261038d2ef8SMaor Gottlieb 
3262038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
3263508562d6SDaniel Jurgens 	    flow_attr->port > dev->num_ports ||
3264802c2125SAviad Yehezkel 	    (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3265802c2125SAviad Yehezkel 				  IB_FLOW_ATTR_FLAGS_EGRESS)))
3266802c2125SAviad Yehezkel 		return ERR_PTR(-EINVAL);
3267802c2125SAviad Yehezkel 
3268802c2125SAviad Yehezkel 	if (is_egress &&
3269802c2125SAviad Yehezkel 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3270802c2125SAviad Yehezkel 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT))
3271038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3272038d2ef8SMaor Gottlieb 
3273038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3274038d2ef8SMaor Gottlieb 	if (!dst)
3275038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
3276038d2ef8SMaor Gottlieb 
32779a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3278038d2ef8SMaor Gottlieb 
3279802c2125SAviad Yehezkel 	ft_prio = get_flow_table(dev, flow_attr,
3280802c2125SAviad Yehezkel 				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3281038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
3282038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
3283038d2ef8SMaor Gottlieb 		goto unlock;
3284038d2ef8SMaor Gottlieb 	}
3285cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3286cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3287cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
3288cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
3289cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
3290cc0e5d42SMaor Gottlieb 			goto destroy_ft;
3291cc0e5d42SMaor Gottlieb 		}
3292cc0e5d42SMaor Gottlieb 	}
3293038d2ef8SMaor Gottlieb 
3294802c2125SAviad Yehezkel 	if (is_egress) {
3295802c2125SAviad Yehezkel 		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3296802c2125SAviad Yehezkel 	} else {
3297038d2ef8SMaor Gottlieb 		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3298d9f88e5aSYishai Hadas 		if (mqp->flags & MLX5_IB_QP_RSS)
3299d9f88e5aSYishai Hadas 			dst->tir_num = mqp->rss_qp.tirn;
3300d9f88e5aSYishai Hadas 		else
3301d9f88e5aSYishai Hadas 			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3302802c2125SAviad Yehezkel 	}
3303038d2ef8SMaor Gottlieb 
3304038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
330535d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
330635d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
330735d19011SMaor Gottlieb 							flow_attr, dst);
330835d19011SMaor Gottlieb 		} else {
3309a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3310a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
3311a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
3312a550ddfcSYishai Hadas 						    dst, underlay_qpn);
331335d19011SMaor Gottlieb 		}
3314038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3315038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3316038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3317038d2ef8SMaor Gottlieb 						dst);
3318cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3319cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3320038d2ef8SMaor Gottlieb 	} else {
3321038d2ef8SMaor Gottlieb 		err = -EINVAL;
3322038d2ef8SMaor Gottlieb 		goto destroy_ft;
3323038d2ef8SMaor Gottlieb 	}
3324038d2ef8SMaor Gottlieb 
3325038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
3326038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
3327038d2ef8SMaor Gottlieb 		handler = NULL;
3328038d2ef8SMaor Gottlieb 		goto destroy_ft;
3329038d2ef8SMaor Gottlieb 	}
3330038d2ef8SMaor Gottlieb 
33319a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3332038d2ef8SMaor Gottlieb 	kfree(dst);
3333038d2ef8SMaor Gottlieb 
3334038d2ef8SMaor Gottlieb 	return &handler->ibflow;
3335038d2ef8SMaor Gottlieb 
3336038d2ef8SMaor Gottlieb destroy_ft:
3337038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
3338cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
3339cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
3340038d2ef8SMaor Gottlieb unlock:
33419a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3342038d2ef8SMaor Gottlieb 	kfree(dst);
3343038d2ef8SMaor Gottlieb 	kfree(handler);
3344038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
3345038d2ef8SMaor Gottlieb }
3346038d2ef8SMaor Gottlieb 
3347c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
3348c6475a0bSAviad Yehezkel {
3349c6475a0bSAviad Yehezkel 	u32 flags = 0;
3350c6475a0bSAviad Yehezkel 
3351c6475a0bSAviad Yehezkel 	if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
3352c6475a0bSAviad Yehezkel 		flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
3353c6475a0bSAviad Yehezkel 
3354c6475a0bSAviad Yehezkel 	return flags;
3355c6475a0bSAviad Yehezkel }
3356c6475a0bSAviad Yehezkel 
3357c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED	MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
3358c6475a0bSAviad Yehezkel static struct ib_flow_action *
3359c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device,
3360c6475a0bSAviad Yehezkel 			       const struct ib_flow_action_attrs_esp *attr,
3361c6475a0bSAviad Yehezkel 			       struct uverbs_attr_bundle *attrs)
3362c6475a0bSAviad Yehezkel {
3363c6475a0bSAviad Yehezkel 	struct mlx5_ib_dev *mdev = to_mdev(device);
3364c6475a0bSAviad Yehezkel 	struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
3365c6475a0bSAviad Yehezkel 	struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
3366c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *action;
3367c6475a0bSAviad Yehezkel 	u64 action_flags;
3368c6475a0bSAviad Yehezkel 	u64 flags;
3369c6475a0bSAviad Yehezkel 	int err = 0;
3370c6475a0bSAviad Yehezkel 
3371c6475a0bSAviad Yehezkel 	if (IS_UVERBS_COPY_ERR(uverbs_copy_from(&action_flags, attrs,
3372c6475a0bSAviad Yehezkel 						MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS)))
3373c6475a0bSAviad Yehezkel 		return ERR_PTR(-EFAULT);
3374c6475a0bSAviad Yehezkel 
3375c6475a0bSAviad Yehezkel 	if (action_flags >= (MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1))
3376c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3377c6475a0bSAviad Yehezkel 
3378c6475a0bSAviad Yehezkel 	flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
3379c6475a0bSAviad Yehezkel 
3380c6475a0bSAviad Yehezkel 	/* We current only support a subset of the standard features. Only a
3381c6475a0bSAviad Yehezkel 	 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
3382c6475a0bSAviad Yehezkel 	 * (with overlap). Full offload mode isn't supported.
3383c6475a0bSAviad Yehezkel 	 */
3384c6475a0bSAviad Yehezkel 	if (!attr->keymat || attr->replay || attr->encap ||
3385c6475a0bSAviad Yehezkel 	    attr->spi || attr->seq || attr->tfc_pad ||
3386c6475a0bSAviad Yehezkel 	    attr->hard_limit_pkts ||
3387c6475a0bSAviad Yehezkel 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3388c6475a0bSAviad Yehezkel 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
3389c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3390c6475a0bSAviad Yehezkel 
3391c6475a0bSAviad Yehezkel 	if (attr->keymat->protocol !=
3392c6475a0bSAviad Yehezkel 	    IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
3393c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3394c6475a0bSAviad Yehezkel 
3395c6475a0bSAviad Yehezkel 	aes_gcm = &attr->keymat->keymat.aes_gcm;
3396c6475a0bSAviad Yehezkel 
3397c6475a0bSAviad Yehezkel 	if (aes_gcm->icv_len != 16 ||
3398c6475a0bSAviad Yehezkel 	    aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
3399c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3400c6475a0bSAviad Yehezkel 
3401c6475a0bSAviad Yehezkel 	action = kmalloc(sizeof(*action), GFP_KERNEL);
3402c6475a0bSAviad Yehezkel 	if (!action)
3403c6475a0bSAviad Yehezkel 		return ERR_PTR(-ENOMEM);
3404c6475a0bSAviad Yehezkel 
3405c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
3406c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
3407c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.aes_key));
3408c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
3409c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
3410c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.salt));
3411c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
3412c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
3413c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
3414c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
3415c6475a0bSAviad Yehezkel 	accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
3416c6475a0bSAviad Yehezkel 
3417c6475a0bSAviad Yehezkel 	accel_attrs.esn = attr->esn;
3418c6475a0bSAviad Yehezkel 	if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
3419c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
3420c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
3421c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3422c6475a0bSAviad Yehezkel 
3423c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
3424c6475a0bSAviad Yehezkel 		accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
3425c6475a0bSAviad Yehezkel 
3426c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ctx =
3427c6475a0bSAviad Yehezkel 		mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
3428c6475a0bSAviad Yehezkel 	if (IS_ERR(action->esp_aes_gcm.ctx)) {
3429c6475a0bSAviad Yehezkel 		err = PTR_ERR(action->esp_aes_gcm.ctx);
3430c6475a0bSAviad Yehezkel 		goto err_parse;
3431c6475a0bSAviad Yehezkel 	}
3432c6475a0bSAviad Yehezkel 
3433c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
3434c6475a0bSAviad Yehezkel 
3435c6475a0bSAviad Yehezkel 	return &action->ib_action;
3436c6475a0bSAviad Yehezkel 
3437c6475a0bSAviad Yehezkel err_parse:
3438c6475a0bSAviad Yehezkel 	kfree(action);
3439c6475a0bSAviad Yehezkel 	return ERR_PTR(err);
3440c6475a0bSAviad Yehezkel }
3441c6475a0bSAviad Yehezkel 
3442349705c1SMatan Barak static int
3443349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
3444349705c1SMatan Barak 			       const struct ib_flow_action_attrs_esp *attr,
3445349705c1SMatan Barak 			       struct uverbs_attr_bundle *attrs)
3446349705c1SMatan Barak {
3447349705c1SMatan Barak 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
3448349705c1SMatan Barak 	struct mlx5_accel_esp_xfrm_attrs accel_attrs;
3449349705c1SMatan Barak 	int err = 0;
3450349705c1SMatan Barak 
3451349705c1SMatan Barak 	if (attr->keymat || attr->replay || attr->encap ||
3452349705c1SMatan Barak 	    attr->spi || attr->seq || attr->tfc_pad ||
3453349705c1SMatan Barak 	    attr->hard_limit_pkts ||
3454349705c1SMatan Barak 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3455349705c1SMatan Barak 			     IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
3456349705c1SMatan Barak 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
3457349705c1SMatan Barak 		return -EOPNOTSUPP;
3458349705c1SMatan Barak 
3459349705c1SMatan Barak 	/* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
3460349705c1SMatan Barak 	 * be modified.
3461349705c1SMatan Barak 	 */
3462349705c1SMatan Barak 	if (!(maction->esp_aes_gcm.ib_flags &
3463349705c1SMatan Barak 	      IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
3464349705c1SMatan Barak 	    attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3465349705c1SMatan Barak 			   IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
3466349705c1SMatan Barak 		return -EINVAL;
3467349705c1SMatan Barak 
3468349705c1SMatan Barak 	memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
3469349705c1SMatan Barak 	       sizeof(accel_attrs));
3470349705c1SMatan Barak 
3471349705c1SMatan Barak 	accel_attrs.esn = attr->esn;
3472349705c1SMatan Barak 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
3473349705c1SMatan Barak 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3474349705c1SMatan Barak 	else
3475349705c1SMatan Barak 		accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3476349705c1SMatan Barak 
3477349705c1SMatan Barak 	err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
3478349705c1SMatan Barak 					 &accel_attrs);
3479349705c1SMatan Barak 	if (err)
3480349705c1SMatan Barak 		return err;
3481349705c1SMatan Barak 
3482349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags &=
3483349705c1SMatan Barak 		~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
3484349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags |=
3485349705c1SMatan Barak 		attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
3486349705c1SMatan Barak 
3487349705c1SMatan Barak 	return 0;
3488349705c1SMatan Barak }
3489349705c1SMatan Barak 
3490c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
3491c6475a0bSAviad Yehezkel {
3492c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
3493c6475a0bSAviad Yehezkel 
3494c6475a0bSAviad Yehezkel 	switch (action->type) {
3495c6475a0bSAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
3496c6475a0bSAviad Yehezkel 		/*
3497c6475a0bSAviad Yehezkel 		 * We only support aes_gcm by now, so we implicitly know this is
3498c6475a0bSAviad Yehezkel 		 * the underline crypto.
3499c6475a0bSAviad Yehezkel 		 */
3500c6475a0bSAviad Yehezkel 		mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
3501c6475a0bSAviad Yehezkel 		break;
3502c6475a0bSAviad Yehezkel 	default:
3503c6475a0bSAviad Yehezkel 		WARN_ON(true);
3504c6475a0bSAviad Yehezkel 		break;
3505c6475a0bSAviad Yehezkel 	}
3506c6475a0bSAviad Yehezkel 
3507c6475a0bSAviad Yehezkel 	kfree(maction);
3508c6475a0bSAviad Yehezkel 	return 0;
3509c6475a0bSAviad Yehezkel }
3510c6475a0bSAviad Yehezkel 
3511e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3512e126ba97SEli Cohen {
3513e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
351481e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
3515e126ba97SEli Cohen 	int err;
3516e126ba97SEli Cohen 
351781e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
351881e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
351981e30880SYishai Hadas 		return -EOPNOTSUPP;
352081e30880SYishai Hadas 	}
352181e30880SYishai Hadas 
35229603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
3523e126ba97SEli Cohen 	if (err)
3524e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
3525e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
3526e126ba97SEli Cohen 
3527e126ba97SEli Cohen 	return err;
3528e126ba97SEli Cohen }
3529e126ba97SEli Cohen 
3530e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3531e126ba97SEli Cohen {
3532e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3533e126ba97SEli Cohen 	int err;
3534e126ba97SEli Cohen 
35359603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
3536e126ba97SEli Cohen 	if (err)
3537e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
3538e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
3539e126ba97SEli Cohen 
3540e126ba97SEli Cohen 	return err;
3541e126ba97SEli Cohen }
3542e126ba97SEli Cohen 
3543e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
3544e126ba97SEli Cohen {
35451b5daf11SMajd Dibbiny 	int err;
3546e126ba97SEli Cohen 
35471b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
3548e126ba97SEli Cohen 	if (err)
3549e126ba97SEli Cohen 		return err;
35501b5daf11SMajd Dibbiny 
35511b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
35521b5daf11SMajd Dibbiny 
35531b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
3554e126ba97SEli Cohen }
3555e126ba97SEli Cohen 
3556e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
3557e126ba97SEli Cohen 			     char *buf)
3558e126ba97SEli Cohen {
3559e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3560e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3561e126ba97SEli Cohen 
35629603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
3563e126ba97SEli Cohen }
3564e126ba97SEli Cohen 
3565e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
3566e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
3567e126ba97SEli Cohen {
3568e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3569e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3570e126ba97SEli Cohen 
35716aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
3572e126ba97SEli Cohen }
3573e126ba97SEli Cohen 
3574e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
3575e126ba97SEli Cohen 			char *buf)
3576e126ba97SEli Cohen {
3577e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3578e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
35799603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
3580e126ba97SEli Cohen }
3581e126ba97SEli Cohen 
3582e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
3583e126ba97SEli Cohen 			char *buf)
3584e126ba97SEli Cohen {
3585e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3586e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
35879603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
3588e126ba97SEli Cohen }
3589e126ba97SEli Cohen 
3590e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
3591e126ba97SEli Cohen 			  char *buf)
3592e126ba97SEli Cohen {
3593e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3594e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3595e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
35969603b61dSJack Morgenstein 		       dev->mdev->board_id);
3597e126ba97SEli Cohen }
3598e126ba97SEli Cohen 
3599e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
3600e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
3601e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
3602e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
3603e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
3604e126ba97SEli Cohen 
3605e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
3606e126ba97SEli Cohen 	&dev_attr_hw_rev,
3607e126ba97SEli Cohen 	&dev_attr_hca_type,
3608e126ba97SEli Cohen 	&dev_attr_board_id,
3609e126ba97SEli Cohen 	&dev_attr_fw_pages,
3610e126ba97SEli Cohen 	&dev_attr_reg_pages,
3611e126ba97SEli Cohen };
3612e126ba97SEli Cohen 
36137722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
36147722f47eSHaggai Eran {
36157722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
36167722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
36177722f47eSHaggai Eran 			     pkey_change_work);
36187722f47eSHaggai Eran 
36197722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
36207722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
36217722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
36227722f47eSHaggai Eran }
36237722f47eSHaggai Eran 
362489ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
362589ea94a7SMaor Gottlieb {
362689ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
362789ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
362889ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
362989ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
363089ea94a7SMaor Gottlieb 	unsigned long flags_qp;
363189ea94a7SMaor Gottlieb 	unsigned long flags_cq;
363289ea94a7SMaor Gottlieb 	unsigned long flags;
363389ea94a7SMaor Gottlieb 
363489ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
363589ea94a7SMaor Gottlieb 
363689ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
363789ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
363889ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
363989ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
364089ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
364189ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
364289ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
364389ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
364489ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
364589ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
364689ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
364789ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
364889ea94a7SMaor Gottlieb 						      &cq_armed_list);
364989ea94a7SMaor Gottlieb 				}
365089ea94a7SMaor Gottlieb 			}
365189ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
365289ea94a7SMaor Gottlieb 		}
365389ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
365489ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
365589ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
365689ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
365789ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
365889ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
365989ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
366089ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
366189ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
366289ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
366389ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
366489ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
366589ea94a7SMaor Gottlieb 							      &cq_armed_list);
366689ea94a7SMaor Gottlieb 					}
366789ea94a7SMaor Gottlieb 				}
366889ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
366989ea94a7SMaor Gottlieb 						       flags_cq);
367089ea94a7SMaor Gottlieb 			}
367189ea94a7SMaor Gottlieb 		}
367289ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
367389ea94a7SMaor Gottlieb 	}
367489ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
367589ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
367689ea94a7SMaor Gottlieb 	 */
367789ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
367889ea94a7SMaor Gottlieb 		mcq->comp(mcq);
367989ea94a7SMaor Gottlieb 	}
368089ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
368189ea94a7SMaor Gottlieb }
368289ea94a7SMaor Gottlieb 
368303404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
368403404e8aSMaor Gottlieb {
368503404e8aSMaor Gottlieb 	int err;
368603404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
368703404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
368803404e8aSMaor Gottlieb 			     delay_drop_work);
368903404e8aSMaor Gottlieb 
3690fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
3691fe248c3aSMaor Gottlieb 
369203404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
369303404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
369403404e8aSMaor Gottlieb 				       delay_drop->timeout);
369503404e8aSMaor Gottlieb 	if (err) {
369603404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
369703404e8aSMaor Gottlieb 			     delay_drop->timeout);
369803404e8aSMaor Gottlieb 		delay_drop->activate = false;
369903404e8aSMaor Gottlieb 	}
370003404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
370103404e8aSMaor Gottlieb }
370203404e8aSMaor Gottlieb 
3703d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
3704e126ba97SEli Cohen {
3705d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
3706d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
3707d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
3708e126ba97SEli Cohen 	struct ib_event ibev;
3709dbaaff2aSEli Cohen 	bool fatal = false;
3710aba46213SDaniel Jurgens 	u8 port = (u8)work->param;
3711e126ba97SEli Cohen 
3712d69a24e0SDaniel Jurgens 	if (mlx5_core_is_mp_slave(work->dev)) {
3713d69a24e0SDaniel Jurgens 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->context);
3714d69a24e0SDaniel Jurgens 		if (!ibdev)
3715d69a24e0SDaniel Jurgens 			goto out;
3716d69a24e0SDaniel Jurgens 	} else {
3717d69a24e0SDaniel Jurgens 		ibdev = work->context;
3718d69a24e0SDaniel Jurgens 	}
3719d69a24e0SDaniel Jurgens 
3720d69a24e0SDaniel Jurgens 	switch (work->event) {
3721e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
3722e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
372389ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
3724dbaaff2aSEli Cohen 		fatal = true;
3725e126ba97SEli Cohen 		break;
3726e126ba97SEli Cohen 
3727e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
3728e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
37292788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
37305ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
37315ec8c83eSAviv Heller 		 * mlx5_netdev_event().
37325ec8c83eSAviv Heller 		 */
37335ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
37345ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
3735d69a24e0SDaniel Jurgens 			goto out;
37365ec8c83eSAviv Heller 
3737d69a24e0SDaniel Jurgens 		ibev.event = (work->event == MLX5_DEV_EVENT_PORT_UP) ?
37385ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
3739e126ba97SEli Cohen 		break;
3740e126ba97SEli Cohen 
3741e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
3742e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
3743e126ba97SEli Cohen 		break;
3744e126ba97SEli Cohen 
3745e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
3746e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
37477722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
3748e126ba97SEli Cohen 		break;
3749e126ba97SEli Cohen 
3750e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
3751e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
3752e126ba97SEli Cohen 		break;
3753e126ba97SEli Cohen 
3754e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
3755e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
3756e126ba97SEli Cohen 		break;
375703404e8aSMaor Gottlieb 	case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
375803404e8aSMaor Gottlieb 		schedule_work(&ibdev->delay_drop.delay_drop_work);
375903404e8aSMaor Gottlieb 		goto out;
3760bdc37924SSaeed Mahameed 	default:
376103404e8aSMaor Gottlieb 		goto out;
3762e126ba97SEli Cohen 	}
3763e126ba97SEli Cohen 
3764e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
3765e126ba97SEli Cohen 	ibev.element.port_num = port;
3766e126ba97SEli Cohen 
3767aba46213SDaniel Jurgens 	if (!rdma_is_port_valid(&ibdev->ib_dev, port)) {
3768a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
376903404e8aSMaor Gottlieb 		goto out;
3770a0c84c32SEli Cohen 	}
3771a0c84c32SEli Cohen 
3772e126ba97SEli Cohen 	if (ibdev->ib_active)
3773e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
3774dbaaff2aSEli Cohen 
3775dbaaff2aSEli Cohen 	if (fatal)
3776dbaaff2aSEli Cohen 		ibdev->ib_active = false;
377703404e8aSMaor Gottlieb out:
3778d69a24e0SDaniel Jurgens 	kfree(work);
3779d69a24e0SDaniel Jurgens }
3780d69a24e0SDaniel Jurgens 
3781d69a24e0SDaniel Jurgens static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
3782d69a24e0SDaniel Jurgens 			  enum mlx5_dev_event event, unsigned long param)
3783d69a24e0SDaniel Jurgens {
3784d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
3785d69a24e0SDaniel Jurgens 
3786d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
378710bea9c8SLeon Romanovsky 	if (!work)
378810bea9c8SLeon Romanovsky 		return;
378910bea9c8SLeon Romanovsky 
3790d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
3791d69a24e0SDaniel Jurgens 	work->dev = dev;
3792d69a24e0SDaniel Jurgens 	work->param = param;
3793d69a24e0SDaniel Jurgens 	work->context = context;
3794d69a24e0SDaniel Jurgens 	work->event = event;
3795d69a24e0SDaniel Jurgens 
3796d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
3797d69a24e0SDaniel Jurgens }
3798d69a24e0SDaniel Jurgens 
3799c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
3800c43f1112SMaor Gottlieb {
3801c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
3802c43f1112SMaor Gottlieb 	int err;
3803c43f1112SMaor Gottlieb 	int port;
3804c43f1112SMaor Gottlieb 
3805508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++) {
3806c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
3807c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3808c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
3809c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3810c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
3811c43f1112SMaor Gottlieb 								   port, 0,
3812c43f1112SMaor Gottlieb 								   &vport_ctx);
3813c43f1112SMaor Gottlieb 				if (err) {
3814c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3815c43f1112SMaor Gottlieb 						    port, err);
3816c43f1112SMaor Gottlieb 					return err;
3817c43f1112SMaor Gottlieb 				}
3818c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
3819c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
3820c43f1112SMaor Gottlieb 			} else {
3821c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
3822c43f1112SMaor Gottlieb 			}
3823c43f1112SMaor Gottlieb 		}
3824c43f1112SMaor Gottlieb 	}
3825c43f1112SMaor Gottlieb 	return 0;
3826c43f1112SMaor Gottlieb }
3827c43f1112SMaor Gottlieb 
3828e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3829e126ba97SEli Cohen {
3830e126ba97SEli Cohen 	int port;
3831e126ba97SEli Cohen 
3832508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
3833e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
3834e126ba97SEli Cohen }
3835e126ba97SEli Cohen 
383632f69e4bSDaniel Jurgens static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
3837e126ba97SEli Cohen {
3838e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
3839e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
3840f614fc15SDan Carpenter 	int err = -ENOMEM;
38412528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
3842e126ba97SEli Cohen 
3843e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3844e126ba97SEli Cohen 	if (!pprops)
3845e126ba97SEli Cohen 		goto out;
3846e126ba97SEli Cohen 
3847e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3848e126ba97SEli Cohen 	if (!dprops)
3849e126ba97SEli Cohen 		goto out;
3850e126ba97SEli Cohen 
3851c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
3852c43f1112SMaor Gottlieb 	if (err)
3853c43f1112SMaor Gottlieb 		goto out;
3854c43f1112SMaor Gottlieb 
38552528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
3856e126ba97SEli Cohen 	if (err) {
3857e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
3858e126ba97SEli Cohen 		goto out;
3859e126ba97SEli Cohen 	}
3860e126ba97SEli Cohen 
3861c4550c63SOr Gerlitz 	memset(pprops, 0, sizeof(*pprops));
3862e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3863e126ba97SEli Cohen 	if (err) {
3864938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
3865938fe83cSSaeed Mahameed 			     port, err);
386632f69e4bSDaniel Jurgens 		goto out;
3867e126ba97SEli Cohen 	}
386832f69e4bSDaniel Jurgens 
3869938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
3870938fe83cSSaeed Mahameed 					dprops->max_pkeys;
3871938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
3872938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
387332f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
387432f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
3875e126ba97SEli Cohen 
3876e126ba97SEli Cohen out:
3877e126ba97SEli Cohen 	kfree(pprops);
3878e126ba97SEli Cohen 	kfree(dprops);
3879e126ba97SEli Cohen 
3880e126ba97SEli Cohen 	return err;
3881e126ba97SEli Cohen }
3882e126ba97SEli Cohen 
3883e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3884e126ba97SEli Cohen {
3885e126ba97SEli Cohen 	int err;
3886e126ba97SEli Cohen 
3887e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
3888e126ba97SEli Cohen 	if (err)
3889e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3890e126ba97SEli Cohen 
389132927e28SMark Bloch 	if (dev->umrc.qp)
3892e126ba97SEli Cohen 		mlx5_ib_destroy_qp(dev->umrc.qp);
389332927e28SMark Bloch 	if (dev->umrc.cq)
3894add08d76SChristoph Hellwig 		ib_free_cq(dev->umrc.cq);
389532927e28SMark Bloch 	if (dev->umrc.pd)
3896e126ba97SEli Cohen 		ib_dealloc_pd(dev->umrc.pd);
3897e126ba97SEli Cohen }
3898e126ba97SEli Cohen 
3899e126ba97SEli Cohen enum {
3900e126ba97SEli Cohen 	MAX_UMR_WR = 128,
3901e126ba97SEli Cohen };
3902e126ba97SEli Cohen 
3903e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
3904e126ba97SEli Cohen {
3905e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
3906e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
3907e126ba97SEli Cohen 	struct ib_pd *pd;
3908e126ba97SEli Cohen 	struct ib_cq *cq;
3909e126ba97SEli Cohen 	struct ib_qp *qp;
3910e126ba97SEli Cohen 	int ret;
3911e126ba97SEli Cohen 
3912e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3913e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3914e126ba97SEli Cohen 	if (!attr || !init_attr) {
3915e126ba97SEli Cohen 		ret = -ENOMEM;
3916e126ba97SEli Cohen 		goto error_0;
3917e126ba97SEli Cohen 	}
3918e126ba97SEli Cohen 
3919ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
3920e126ba97SEli Cohen 	if (IS_ERR(pd)) {
3921e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3922e126ba97SEli Cohen 		ret = PTR_ERR(pd);
3923e126ba97SEli Cohen 		goto error_0;
3924e126ba97SEli Cohen 	}
3925e126ba97SEli Cohen 
3926add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
3927e126ba97SEli Cohen 	if (IS_ERR(cq)) {
3928e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3929e126ba97SEli Cohen 		ret = PTR_ERR(cq);
3930e126ba97SEli Cohen 		goto error_2;
3931e126ba97SEli Cohen 	}
3932e126ba97SEli Cohen 
3933e126ba97SEli Cohen 	init_attr->send_cq = cq;
3934e126ba97SEli Cohen 	init_attr->recv_cq = cq;
3935e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3936e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
3937e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
3938e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3939e126ba97SEli Cohen 	init_attr->port_num = 1;
3940e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3941e126ba97SEli Cohen 	if (IS_ERR(qp)) {
3942e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3943e126ba97SEli Cohen 		ret = PTR_ERR(qp);
3944e126ba97SEli Cohen 		goto error_3;
3945e126ba97SEli Cohen 	}
3946e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
3947e126ba97SEli Cohen 	qp->real_qp    = qp;
3948e126ba97SEli Cohen 	qp->uobject    = NULL;
3949e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
395031fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
395131fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
3952e126ba97SEli Cohen 
3953e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
3954e126ba97SEli Cohen 	attr->port_num = 1;
3955e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3956e126ba97SEli Cohen 				IB_QP_PORT, NULL);
3957e126ba97SEli Cohen 	if (ret) {
3958e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3959e126ba97SEli Cohen 		goto error_4;
3960e126ba97SEli Cohen 	}
3961e126ba97SEli Cohen 
3962e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3963e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
3964e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
3965e126ba97SEli Cohen 
3966e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3967e126ba97SEli Cohen 	if (ret) {
3968e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3969e126ba97SEli Cohen 		goto error_4;
3970e126ba97SEli Cohen 	}
3971e126ba97SEli Cohen 
3972e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3973e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
3974e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3975e126ba97SEli Cohen 	if (ret) {
3976e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3977e126ba97SEli Cohen 		goto error_4;
3978e126ba97SEli Cohen 	}
3979e126ba97SEli Cohen 
3980e126ba97SEli Cohen 	dev->umrc.qp = qp;
3981e126ba97SEli Cohen 	dev->umrc.cq = cq;
3982e126ba97SEli Cohen 	dev->umrc.pd = pd;
3983e126ba97SEli Cohen 
3984e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
3985e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
3986e126ba97SEli Cohen 	if (ret) {
3987e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3988e126ba97SEli Cohen 		goto error_4;
3989e126ba97SEli Cohen 	}
3990e126ba97SEli Cohen 
3991e126ba97SEli Cohen 	kfree(attr);
3992e126ba97SEli Cohen 	kfree(init_attr);
3993e126ba97SEli Cohen 
3994e126ba97SEli Cohen 	return 0;
3995e126ba97SEli Cohen 
3996e126ba97SEli Cohen error_4:
3997e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
399832927e28SMark Bloch 	dev->umrc.qp = NULL;
3999e126ba97SEli Cohen 
4000e126ba97SEli Cohen error_3:
4001add08d76SChristoph Hellwig 	ib_free_cq(cq);
400232927e28SMark Bloch 	dev->umrc.cq = NULL;
4003e126ba97SEli Cohen 
4004e126ba97SEli Cohen error_2:
4005e126ba97SEli Cohen 	ib_dealloc_pd(pd);
400632927e28SMark Bloch 	dev->umrc.pd = NULL;
4007e126ba97SEli Cohen 
4008e126ba97SEli Cohen error_0:
4009e126ba97SEli Cohen 	kfree(attr);
4010e126ba97SEli Cohen 	kfree(init_attr);
4011e126ba97SEli Cohen 	return ret;
4012e126ba97SEli Cohen }
4013e126ba97SEli Cohen 
40146e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
40156e8484c5SMax Gurtovoy {
40166e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
40176e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
40186e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
40196e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
40206e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
40216e8484c5SMax Gurtovoy 	default:
40226e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
40236e8484c5SMax Gurtovoy 	}
40246e8484c5SMax Gurtovoy }
40256e8484c5SMax Gurtovoy 
4026e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
4027e126ba97SEli Cohen {
4028e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
4029e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
4030bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
40317722f47eSHaggai Eran 	int port;
4032e126ba97SEli Cohen 	int ret = 0;
4033e126ba97SEli Cohen 
4034e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
4035e126ba97SEli Cohen 
4036d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
4037d16e91daSHaggai Eran 
4038e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
4039e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
4040e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
4041e126ba97SEli Cohen 		goto error0;
4042e126ba97SEli Cohen 	}
4043e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
4044e126ba97SEli Cohen 	devr->p0->uobject = NULL;
4045e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
4046e126ba97SEli Cohen 
4047bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
4048e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
4049e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
4050e126ba97SEli Cohen 		goto error1;
4051e126ba97SEli Cohen 	}
4052e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
4053e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
4054e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
4055e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
4056e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
4057e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
4058e126ba97SEli Cohen 
4059e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4060e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
4061e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
4062e126ba97SEli Cohen 		goto error2;
4063e126ba97SEli Cohen 	}
4064e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
4065e126ba97SEli Cohen 	devr->x0->inode = NULL;
4066e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
4067e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
4068e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4069e126ba97SEli Cohen 
4070e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4071e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
4072e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
4073e126ba97SEli Cohen 		goto error3;
4074e126ba97SEli Cohen 	}
4075e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
4076e126ba97SEli Cohen 	devr->x1->inode = NULL;
4077e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
4078e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
4079e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4080e126ba97SEli Cohen 
4081e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
4082e126ba97SEli Cohen 	attr.attr.max_sge = 1;
4083e126ba97SEli Cohen 	attr.attr.max_wr = 1;
4084e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
40851a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
4086e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
4087e126ba97SEli Cohen 
4088e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4089e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
4090e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
4091e126ba97SEli Cohen 		goto error4;
4092e126ba97SEli Cohen 	}
4093e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
4094e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
4095e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
4096e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
4097e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
4098e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
4099e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
41001a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
4101e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
41021a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
4103e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
4104e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
4105e126ba97SEli Cohen 
41064aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
41074aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
41084aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
41094aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
41104aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
41114aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
41124aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
41134aa17b28SHaggai Abramonvsky 		goto error5;
41144aa17b28SHaggai Abramonvsky 	}
41154aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
41164aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
41174aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
41184aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
41194aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
41204aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
41211a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
41224aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
41231a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
41244aa17b28SHaggai Abramonvsky 
41257722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
41267722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
41277722f47eSHaggai Eran 			  pkey_change_handler);
41287722f47eSHaggai Eran 		devr->ports[port].devr = devr;
41297722f47eSHaggai Eran 	}
41307722f47eSHaggai Eran 
4131e126ba97SEli Cohen 	return 0;
4132e126ba97SEli Cohen 
41334aa17b28SHaggai Abramonvsky error5:
41344aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
4135e126ba97SEli Cohen error4:
4136e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
4137e126ba97SEli Cohen error3:
4138e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
4139e126ba97SEli Cohen error2:
4140e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
4141e126ba97SEli Cohen error1:
4142e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
4143e126ba97SEli Cohen error0:
4144e126ba97SEli Cohen 	return ret;
4145e126ba97SEli Cohen }
4146e126ba97SEli Cohen 
4147e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
4148e126ba97SEli Cohen {
41497722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
41507722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
41517722f47eSHaggai Eran 	int port;
41527722f47eSHaggai Eran 
41534aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
4154e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
4155e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
4156e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
4157e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
4158e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
41597722f47eSHaggai Eran 
41607722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
41617722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
41627722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
4163e126ba97SEli Cohen }
4164e126ba97SEli Cohen 
4165e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
4166e53505a8SAchiad Shochat {
4167e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4168e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
4169e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
4170e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
417185c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
4172e53505a8SAchiad Shochat 	u32 ret = 0;
4173e53505a8SAchiad Shochat 
4174e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
4175e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
4176e53505a8SAchiad Shochat 
417785c7c014SDaniel Jurgens 	if (raw_support)
417872cd5717SOr Gerlitz 		ret = RDMA_CORE_PORT_RAW_PACKET;
417972cd5717SOr Gerlitz 
4180e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
418172cd5717SOr Gerlitz 		return ret;
4182e53505a8SAchiad Shochat 
4183e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
418472cd5717SOr Gerlitz 		return ret;
4185e53505a8SAchiad Shochat 
4186e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
4187e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
4188e53505a8SAchiad Shochat 
4189e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
4190e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
4191e53505a8SAchiad Shochat 
4192e53505a8SAchiad Shochat 	return ret;
4193e53505a8SAchiad Shochat }
4194e53505a8SAchiad Shochat 
41957738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
41967738613eSIra Weiny 			       struct ib_port_immutable *immutable)
41977738613eSIra Weiny {
41987738613eSIra Weiny 	struct ib_port_attr attr;
4199ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4200ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
42017738613eSIra Weiny 	int err;
42027738613eSIra Weiny 
4203c4550c63SOr Gerlitz 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
4204c4550c63SOr Gerlitz 
4205c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
42067738613eSIra Weiny 	if (err)
42077738613eSIra Weiny 		return err;
42087738613eSIra Weiny 
42097738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
42107738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
4211e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
4212ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
4213337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
42147738613eSIra Weiny 
42157738613eSIra Weiny 	return 0;
42167738613eSIra Weiny }
42177738613eSIra Weiny 
42188e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
42198e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
42208e6efa3aSMark Bloch {
42218e6efa3aSMark Bloch 	struct ib_port_attr attr;
42228e6efa3aSMark Bloch 	int err;
42238e6efa3aSMark Bloch 
42248e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
42258e6efa3aSMark Bloch 
42268e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
42278e6efa3aSMark Bloch 	if (err)
42288e6efa3aSMark Bloch 		return err;
42298e6efa3aSMark Bloch 
42308e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
42318e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
42328e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
42338e6efa3aSMark Bloch 
42348e6efa3aSMark Bloch 	return 0;
42358e6efa3aSMark Bloch }
42368e6efa3aSMark Bloch 
42379abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
4238c7342823SIra Weiny {
4239c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
4240c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
42419abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
42429abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
42439abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
4244c7342823SIra Weiny }
4245c7342823SIra Weiny 
424645f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
42479ef9c640SAviv Heller {
42489ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
42499ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
42509ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
42519ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
42529ef9c640SAviv Heller 	int err;
42539ef9c640SAviv Heller 
42549ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
42559ef9c640SAviv Heller 		return 0;
42569ef9c640SAviv Heller 
42579ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
42589ef9c640SAviv Heller 	if (err)
42599ef9c640SAviv Heller 		return err;
42609ef9c640SAviv Heller 
42619ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
42629ef9c640SAviv Heller 	if (IS_ERR(ft)) {
42639ef9c640SAviv Heller 		err = PTR_ERR(ft);
42649ef9c640SAviv Heller 		goto err_destroy_vport_lag;
42659ef9c640SAviv Heller 	}
42669ef9c640SAviv Heller 
42679a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
42689ef9c640SAviv Heller 	return 0;
42699ef9c640SAviv Heller 
42709ef9c640SAviv Heller err_destroy_vport_lag:
42719ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
42729ef9c640SAviv Heller 	return err;
42739ef9c640SAviv Heller }
42749ef9c640SAviv Heller 
427545f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
42769ef9c640SAviv Heller {
42779ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
42789ef9c640SAviv Heller 
42799a4ca38dSMark Bloch 	if (dev->flow_db->lag_demux_ft) {
42809a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
42819a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
42829ef9c640SAviv Heller 
42839ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
42849ef9c640SAviv Heller 	}
42859ef9c640SAviv Heller }
42869ef9c640SAviv Heller 
42877fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
4288fc24fc5eSAchiad Shochat {
4289e53505a8SAchiad Shochat 	int err;
4290e53505a8SAchiad Shochat 
42917fd8aefbSDaniel Jurgens 	dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
42927fd8aefbSDaniel Jurgens 	err = register_netdevice_notifier(&dev->roce[port_num].nb);
42935ec8c83eSAviv Heller 	if (err) {
42947fd8aefbSDaniel Jurgens 		dev->roce[port_num].nb.notifier_call = NULL;
4295e53505a8SAchiad Shochat 		return err;
42965ec8c83eSAviv Heller 	}
4297e53505a8SAchiad Shochat 
4298d012f5d6SOr Gerlitz 	return 0;
4299d012f5d6SOr Gerlitz }
4300d012f5d6SOr Gerlitz 
43017fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
4302e126ba97SEli Cohen {
43037fd8aefbSDaniel Jurgens 	if (dev->roce[port_num].nb.notifier_call) {
43047fd8aefbSDaniel Jurgens 		unregister_netdevice_notifier(&dev->roce[port_num].nb);
43057fd8aefbSDaniel Jurgens 		dev->roce[port_num].nb.notifier_call = NULL;
4306e126ba97SEli Cohen 	}
4307e126ba97SEli Cohen }
4308e126ba97SEli Cohen 
43097fd8aefbSDaniel Jurgens static int mlx5_enable_eth(struct mlx5_ib_dev *dev, u8 port_num)
4310e126ba97SEli Cohen {
4311e126ba97SEli Cohen 	int err;
4312e126ba97SEli Cohen 
4313ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
4314e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
4315e53505a8SAchiad Shochat 		if (err)
43168e6efa3aSMark Bloch 			return err;
4317ca5b91d6SOr Gerlitz 	}
4318e53505a8SAchiad Shochat 
431945f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
43209ef9c640SAviv Heller 	if (err)
43219ef9c640SAviv Heller 		goto err_disable_roce;
43229ef9c640SAviv Heller 
4323e53505a8SAchiad Shochat 	return 0;
4324e53505a8SAchiad Shochat 
43259ef9c640SAviv Heller err_disable_roce:
4326ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
43279ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
43289ef9c640SAviv Heller 
4329e53505a8SAchiad Shochat 	return err;
4330fc24fc5eSAchiad Shochat }
4331fc24fc5eSAchiad Shochat 
433245f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
4333fc24fc5eSAchiad Shochat {
433445f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
4335ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
4336e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
4337fc24fc5eSAchiad Shochat }
4338fc24fc5eSAchiad Shochat 
4339e1f24a79SParav Pandit struct mlx5_ib_counter {
43407c16f477SKamal Heib 	const char *name;
43417c16f477SKamal Heib 	size_t offset;
43427c16f477SKamal Heib };
43437c16f477SKamal Heib 
43447c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
43457c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
43467c16f477SKamal Heib 
4347e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
43487c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
43497c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
43507c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
43517c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
43527c16f477SKamal Heib };
43537c16f477SKamal Heib 
4354e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
43557c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
43567c16f477SKamal Heib };
43577c16f477SKamal Heib 
4358e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
43597c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
43607c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
43617c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
43627c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
43637c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
43647c16f477SKamal Heib };
43657c16f477SKamal Heib 
4366e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
4367e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
4368e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
4369e1f24a79SParav Pandit 
4370e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
4371e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
4372e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
4373e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
4374e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
4375e1f24a79SParav Pandit };
4376e1f24a79SParav Pandit 
437758dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
437858dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
437958dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
438058dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
438158dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
438258dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
438358dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
438458dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
438558dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
438658dcb60aSParav Pandit };
438758dcb60aSParav Pandit 
4388e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
43890837e86aSMark Bloch {
4390aac4492eSDaniel Jurgens 	int i;
43910837e86aSMark Bloch 
43927c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
4393aac4492eSDaniel Jurgens 		if (dev->port[i].cnts.set_id)
43940837e86aSMark Bloch 			mlx5_core_dealloc_q_counter(dev->mdev,
4395e1f24a79SParav Pandit 						    dev->port[i].cnts.set_id);
4396e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
4397e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
43987c16f477SKamal Heib 	}
43997c16f477SKamal Heib }
44007c16f477SKamal Heib 
4401e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
4402e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
44037c16f477SKamal Heib {
44047c16f477SKamal Heib 	u32 num_counters;
44057c16f477SKamal Heib 
44067c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
44077c16f477SKamal Heib 
44087c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
44097c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
44107c16f477SKamal Heib 
44117c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
44127c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
441358dcb60aSParav Pandit 
441458dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
441558dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
441658dcb60aSParav Pandit 
4417e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
44187c16f477SKamal Heib 
4419e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4420e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
4421e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
4422e1f24a79SParav Pandit 	}
4423e1f24a79SParav Pandit 
4424e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
4425e1f24a79SParav Pandit 	if (!cnts->names)
44267c16f477SKamal Heib 		return -ENOMEM;
44277c16f477SKamal Heib 
4428e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
4429e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
4430e1f24a79SParav Pandit 	if (!cnts->offsets)
44317c16f477SKamal Heib 		goto err_names;
44327c16f477SKamal Heib 
44337c16f477SKamal Heib 	return 0;
44347c16f477SKamal Heib 
44357c16f477SKamal Heib err_names:
4436e1f24a79SParav Pandit 	kfree(cnts->names);
4437aac4492eSDaniel Jurgens 	cnts->names = NULL;
44387c16f477SKamal Heib 	return -ENOMEM;
44397c16f477SKamal Heib }
44407c16f477SKamal Heib 
4441e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
44427c16f477SKamal Heib 				  const char **names,
44437c16f477SKamal Heib 				  size_t *offsets)
44447c16f477SKamal Heib {
44457c16f477SKamal Heib 	int i;
44467c16f477SKamal Heib 	int j = 0;
44477c16f477SKamal Heib 
44487c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
44497c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
44507c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
44517c16f477SKamal Heib 	}
44527c16f477SKamal Heib 
44537c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
44547c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
44557c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
44567c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
44577c16f477SKamal Heib 		}
44587c16f477SKamal Heib 	}
44597c16f477SKamal Heib 
44607c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
44617c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
44627c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
44637c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
44647c16f477SKamal Heib 		}
44657c16f477SKamal Heib 	}
4466e1f24a79SParav Pandit 
446758dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
446858dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
446958dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
447058dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
447158dcb60aSParav Pandit 		}
447258dcb60aSParav Pandit 	}
447358dcb60aSParav Pandit 
4474e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4475e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
4476e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
4477e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
4478e1f24a79SParav Pandit 		}
4479e1f24a79SParav Pandit 	}
44800837e86aSMark Bloch }
44810837e86aSMark Bloch 
4482e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
44830837e86aSMark Bloch {
4484aac4492eSDaniel Jurgens 	int err = 0;
44850837e86aSMark Bloch 	int i;
44860837e86aSMark Bloch 
44870837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
4488aac4492eSDaniel Jurgens 		err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
4489aac4492eSDaniel Jurgens 		if (err)
4490aac4492eSDaniel Jurgens 			goto err_alloc;
44917c16f477SKamal Heib 
4492aac4492eSDaniel Jurgens 		mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
4493aac4492eSDaniel Jurgens 				      dev->port[i].cnts.offsets);
4494aac4492eSDaniel Jurgens 
4495aac4492eSDaniel Jurgens 		err = mlx5_core_alloc_q_counter(dev->mdev,
4496aac4492eSDaniel Jurgens 						&dev->port[i].cnts.set_id);
4497aac4492eSDaniel Jurgens 		if (err) {
44980837e86aSMark Bloch 			mlx5_ib_warn(dev,
44990837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
4500aac4492eSDaniel Jurgens 				     i + 1, err);
4501aac4492eSDaniel Jurgens 			goto err_alloc;
45020837e86aSMark Bloch 		}
4503aac4492eSDaniel Jurgens 		dev->port[i].cnts.set_id_valid = true;
45040837e86aSMark Bloch 	}
45050837e86aSMark Bloch 
45060837e86aSMark Bloch 	return 0;
45070837e86aSMark Bloch 
4508aac4492eSDaniel Jurgens err_alloc:
4509aac4492eSDaniel Jurgens 	mlx5_ib_dealloc_counters(dev);
4510aac4492eSDaniel Jurgens 	return err;
45110837e86aSMark Bloch }
45120837e86aSMark Bloch 
45130ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
45140ad17a8fSMark Bloch 						    u8 port_num)
45150ad17a8fSMark Bloch {
45167c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
45177c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
45180ad17a8fSMark Bloch 
45190ad17a8fSMark Bloch 	/* We support only per port stats */
45200ad17a8fSMark Bloch 	if (port_num == 0)
45210ad17a8fSMark Bloch 		return NULL;
45220ad17a8fSMark Bloch 
4523e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
4524e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
4525e1f24a79SParav Pandit 					  port->cnts.num_cong_counters,
45260ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
45270ad17a8fSMark Bloch }
45280ad17a8fSMark Bloch 
4529aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
4530e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
4531e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
4532e1f24a79SParav Pandit {
4533e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
4534e1f24a79SParav Pandit 	void *out;
4535e1f24a79SParav Pandit 	__be32 val;
4536e1f24a79SParav Pandit 	int ret, i;
4537e1f24a79SParav Pandit 
45381b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
4539e1f24a79SParav Pandit 	if (!out)
4540e1f24a79SParav Pandit 		return -ENOMEM;
4541e1f24a79SParav Pandit 
4542aac4492eSDaniel Jurgens 	ret = mlx5_core_query_q_counter(mdev,
4543e1f24a79SParav Pandit 					port->cnts.set_id, 0,
4544e1f24a79SParav Pandit 					out, outlen);
4545e1f24a79SParav Pandit 	if (ret)
4546e1f24a79SParav Pandit 		goto free;
4547e1f24a79SParav Pandit 
4548e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
4549e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
4550e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
4551e1f24a79SParav Pandit 	}
4552e1f24a79SParav Pandit 
4553e1f24a79SParav Pandit free:
4554e1f24a79SParav Pandit 	kvfree(out);
4555e1f24a79SParav Pandit 	return ret;
4556e1f24a79SParav Pandit }
4557e1f24a79SParav Pandit 
45580ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
45590ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
45607c16f477SKamal Heib 				u8 port_num, int index)
45610ad17a8fSMark Bloch {
45620ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
45637c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
4564aac4492eSDaniel Jurgens 	struct mlx5_core_dev *mdev;
4565e1f24a79SParav Pandit 	int ret, num_counters;
4566aac4492eSDaniel Jurgens 	u8 mdev_port_num;
45670ad17a8fSMark Bloch 
45687c16f477SKamal Heib 	if (!stats)
4569e1f24a79SParav Pandit 		return -EINVAL;
45700ad17a8fSMark Bloch 
4571aac4492eSDaniel Jurgens 	num_counters = port->cnts.num_q_counters + port->cnts.num_cong_counters;
4572aac4492eSDaniel Jurgens 
4573aac4492eSDaniel Jurgens 	/* q_counters are per IB device, query the master mdev */
4574aac4492eSDaniel Jurgens 	ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
45750ad17a8fSMark Bloch 	if (ret)
4576e1f24a79SParav Pandit 		return ret;
45770ad17a8fSMark Bloch 
4578e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4579aac4492eSDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
4580aac4492eSDaniel Jurgens 						    &mdev_port_num);
4581aac4492eSDaniel Jurgens 		if (!mdev) {
4582aac4492eSDaniel Jurgens 			/* If port is not affiliated yet, its in down state
4583aac4492eSDaniel Jurgens 			 * which doesn't have any counters yet, so it would be
4584aac4492eSDaniel Jurgens 			 * zero. So no need to read from the HCA.
4585aac4492eSDaniel Jurgens 			 */
4586aac4492eSDaniel Jurgens 			goto done;
4587aac4492eSDaniel Jurgens 		}
458871a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
458971a0ff65SMajd Dibbiny 						   stats->value +
459071a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters,
459171a0ff65SMajd Dibbiny 						   port->cnts.num_cong_counters,
459271a0ff65SMajd Dibbiny 						   port->cnts.offsets +
459371a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters);
4594aac4492eSDaniel Jurgens 
4595aac4492eSDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
4596e1f24a79SParav Pandit 		if (ret)
4597e1f24a79SParav Pandit 			return ret;
45980ad17a8fSMark Bloch 	}
45997c16f477SKamal Heib 
4600aac4492eSDaniel Jurgens done:
4601e1f24a79SParav Pandit 	return num_counters;
46020ad17a8fSMark Bloch }
46030ad17a8fSMark Bloch 
46048e959601SNiranjana Vishwanathapura static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
46058e959601SNiranjana Vishwanathapura {
46068e959601SNiranjana Vishwanathapura 	return mlx5_rdma_netdev_free(netdev);
46078e959601SNiranjana Vishwanathapura }
46088e959601SNiranjana Vishwanathapura 
4609693dfd5aSErez Shitrit static struct net_device*
4610693dfd5aSErez Shitrit mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
4611693dfd5aSErez Shitrit 			  u8 port_num,
4612693dfd5aSErez Shitrit 			  enum rdma_netdev_t type,
4613693dfd5aSErez Shitrit 			  const char *name,
4614693dfd5aSErez Shitrit 			  unsigned char name_assign_type,
4615693dfd5aSErez Shitrit 			  void (*setup)(struct net_device *))
4616693dfd5aSErez Shitrit {
46178e959601SNiranjana Vishwanathapura 	struct net_device *netdev;
46188e959601SNiranjana Vishwanathapura 	struct rdma_netdev *rn;
46198e959601SNiranjana Vishwanathapura 
4620693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
4621693dfd5aSErez Shitrit 		return ERR_PTR(-EOPNOTSUPP);
4622693dfd5aSErez Shitrit 
46238e959601SNiranjana Vishwanathapura 	netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
4624693dfd5aSErez Shitrit 					name, setup);
46258e959601SNiranjana Vishwanathapura 	if (likely(!IS_ERR_OR_NULL(netdev))) {
46268e959601SNiranjana Vishwanathapura 		rn = netdev_priv(netdev);
46278e959601SNiranjana Vishwanathapura 		rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
4628693dfd5aSErez Shitrit 	}
46298e959601SNiranjana Vishwanathapura 	return netdev;
4630693dfd5aSErez Shitrit }
4631693dfd5aSErez Shitrit 
4632fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
4633fe248c3aSMaor Gottlieb {
4634fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
4635fe248c3aSMaor Gottlieb 		return;
4636fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
4637fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
4638fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
4639fe248c3aSMaor Gottlieb }
4640fe248c3aSMaor Gottlieb 
464103404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
464203404e8aSMaor Gottlieb {
464303404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
464403404e8aSMaor Gottlieb 		return;
464503404e8aSMaor Gottlieb 
464603404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
4647fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
4648fe248c3aSMaor Gottlieb }
4649fe248c3aSMaor Gottlieb 
4650fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
4651fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
4652fe248c3aSMaor Gottlieb {
4653fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
4654fe248c3aSMaor Gottlieb 	char lbuf[20];
4655fe248c3aSMaor Gottlieb 	int len;
4656fe248c3aSMaor Gottlieb 
4657fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
4658fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
4659fe248c3aSMaor Gottlieb }
4660fe248c3aSMaor Gottlieb 
4661fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
4662fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
4663fe248c3aSMaor Gottlieb {
4664fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
4665fe248c3aSMaor Gottlieb 	u32 timeout;
4666fe248c3aSMaor Gottlieb 	u32 var;
4667fe248c3aSMaor Gottlieb 
4668fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
4669fe248c3aSMaor Gottlieb 		return -EFAULT;
4670fe248c3aSMaor Gottlieb 
4671fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
4672fe248c3aSMaor Gottlieb 			1000);
4673fe248c3aSMaor Gottlieb 	if (timeout != var)
4674fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
4675fe248c3aSMaor Gottlieb 			    timeout);
4676fe248c3aSMaor Gottlieb 
4677fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
4678fe248c3aSMaor Gottlieb 
4679fe248c3aSMaor Gottlieb 	return count;
4680fe248c3aSMaor Gottlieb }
4681fe248c3aSMaor Gottlieb 
4682fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
4683fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
4684fe248c3aSMaor Gottlieb 	.open	= simple_open,
4685fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
4686fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
4687fe248c3aSMaor Gottlieb };
4688fe248c3aSMaor Gottlieb 
4689fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
4690fe248c3aSMaor Gottlieb {
4691fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
4692fe248c3aSMaor Gottlieb 
4693fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
4694fe248c3aSMaor Gottlieb 		return 0;
4695fe248c3aSMaor Gottlieb 
4696fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
4697fe248c3aSMaor Gottlieb 	if (!dbg)
4698fe248c3aSMaor Gottlieb 		return -ENOMEM;
4699fe248c3aSMaor Gottlieb 
4700cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
4701cbafad87SSudip Mukherjee 
4702fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
4703fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
4704fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
4705fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
4706cbafad87SSudip Mukherjee 		goto out_debugfs;
4707fe248c3aSMaor Gottlieb 
4708fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
4709fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
4710fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
4711fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
4712fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
4713fe248c3aSMaor Gottlieb 		goto out_debugfs;
4714fe248c3aSMaor Gottlieb 
4715fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
4716fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
4717fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
4718fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
4719fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
4720fe248c3aSMaor Gottlieb 		goto out_debugfs;
4721fe248c3aSMaor Gottlieb 
4722fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
4723fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
4724fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
4725fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
4726fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
4727fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
4728fe248c3aSMaor Gottlieb 		goto out_debugfs;
4729fe248c3aSMaor Gottlieb 
4730fe248c3aSMaor Gottlieb 	return 0;
4731fe248c3aSMaor Gottlieb 
4732fe248c3aSMaor Gottlieb out_debugfs:
4733fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
4734fe248c3aSMaor Gottlieb 	return -ENOMEM;
473503404e8aSMaor Gottlieb }
473603404e8aSMaor Gottlieb 
473703404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
473803404e8aSMaor Gottlieb {
473903404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
474003404e8aSMaor Gottlieb 		return;
474103404e8aSMaor Gottlieb 
474203404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
474303404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
474403404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
474503404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
474603404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
4747fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
4748fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
4749fe248c3aSMaor Gottlieb 
4750fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
4751fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
475203404e8aSMaor Gottlieb }
475303404e8aSMaor Gottlieb 
475484305d71SLeon Romanovsky static const struct cpumask *
475584305d71SLeon Romanovsky mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
475640b24403SSagi Grimberg {
475740b24403SSagi Grimberg 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
475840b24403SSagi Grimberg 
475940b24403SSagi Grimberg 	return mlx5_get_vector_affinity(dev->mdev, comp_vector);
476040b24403SSagi Grimberg }
476140b24403SSagi Grimberg 
476232f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
476332f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
476432f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
476532f69e4bSDaniel Jurgens {
476632f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
476732f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
476832f69e4bSDaniel Jurgens 	int comps;
476932f69e4bSDaniel Jurgens 	int err;
477032f69e4bSDaniel Jurgens 	int i;
477132f69e4bSDaniel Jurgens 
4772a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
4773a9e546e7SParav Pandit 
477432f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
477532f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
477632f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
477732f69e4bSDaniel Jurgens 		return;
477832f69e4bSDaniel Jurgens 	}
477932f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
478032f69e4bSDaniel Jurgens 
478132f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
478232f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
478332f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
478432f69e4bSDaniel Jurgens 
478532f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
478632f69e4bSDaniel Jurgens 	if (comps) {
478732f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
478832f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
478932f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
479032f69e4bSDaniel Jurgens 
479132f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
479232f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
479332f69e4bSDaniel Jurgens 
479432f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
479532f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
479632f69e4bSDaniel Jurgens 	}
479732f69e4bSDaniel Jurgens 
479832f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
479932f69e4bSDaniel Jurgens 
480032f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
480132f69e4bSDaniel Jurgens 
480232f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
480332f69e4bSDaniel Jurgens 
480432f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
480532f69e4bSDaniel Jurgens 
480632f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
480732f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
480832f69e4bSDaniel Jurgens 	 * it back to the list.
480932f69e4bSDaniel Jurgens 	 */
481032f69e4bSDaniel Jurgens 	if (err)
481132f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
481232f69e4bSDaniel Jurgens 			    port_num + 1);
481332f69e4bSDaniel Jurgens 
481432f69e4bSDaniel Jurgens 	ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
481532f69e4bSDaniel Jurgens }
481632f69e4bSDaniel Jurgens 
481732f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
481832f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
481932f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
482032f69e4bSDaniel Jurgens {
482132f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
482232f69e4bSDaniel Jurgens 	int err;
482332f69e4bSDaniel Jurgens 
482432f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
482532f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
482632f69e4bSDaniel Jurgens 		mlx5_ib_warn(ibdev, "port %d already affiliated.\n",
482732f69e4bSDaniel Jurgens 			     port_num + 1);
482832f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
482932f69e4bSDaniel Jurgens 		return false;
483032f69e4bSDaniel Jurgens 	}
483132f69e4bSDaniel Jurgens 
483232f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
483332f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
483432f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
483532f69e4bSDaniel Jurgens 
483632f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
483732f69e4bSDaniel Jurgens 	if (err)
483832f69e4bSDaniel Jurgens 		goto unbind;
483932f69e4bSDaniel Jurgens 
484032f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
484132f69e4bSDaniel Jurgens 	if (err)
484232f69e4bSDaniel Jurgens 		goto unbind;
484332f69e4bSDaniel Jurgens 
484432f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
484532f69e4bSDaniel Jurgens 	if (err) {
484632f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
484732f69e4bSDaniel Jurgens 			    port_num + 1);
484832f69e4bSDaniel Jurgens 		goto unbind;
484932f69e4bSDaniel Jurgens 	}
485032f69e4bSDaniel Jurgens 
4851a9e546e7SParav Pandit 	err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
4852a9e546e7SParav Pandit 	if (err)
4853a9e546e7SParav Pandit 		goto unbind;
4854a9e546e7SParav Pandit 
485532f69e4bSDaniel Jurgens 	return true;
485632f69e4bSDaniel Jurgens 
485732f69e4bSDaniel Jurgens unbind:
485832f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
485932f69e4bSDaniel Jurgens 	return false;
486032f69e4bSDaniel Jurgens }
486132f69e4bSDaniel Jurgens 
486232f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
486332f69e4bSDaniel Jurgens {
486432f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
486532f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
486632f69e4bSDaniel Jurgens 							  port_num + 1);
486732f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
486832f69e4bSDaniel Jurgens 	int err;
486932f69e4bSDaniel Jurgens 	int i;
487032f69e4bSDaniel Jurgens 
487132f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
487232f69e4bSDaniel Jurgens 		return 0;
487332f69e4bSDaniel Jurgens 
487432f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
487532f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
487632f69e4bSDaniel Jurgens 	if (err)
487732f69e4bSDaniel Jurgens 		return err;
487832f69e4bSDaniel Jurgens 
487932f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
488032f69e4bSDaniel Jurgens 	if (err)
488132f69e4bSDaniel Jurgens 		return err;
488232f69e4bSDaniel Jurgens 
488332f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
488432f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
488532f69e4bSDaniel Jurgens 		bool bound = false;
488632f69e4bSDaniel Jurgens 
488732f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
488832f69e4bSDaniel Jurgens 		if (i == port_num) {
488932f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
489032f69e4bSDaniel Jurgens 			if (!mpi) {
489132f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
489232f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
489332f69e4bSDaniel Jurgens 				return -ENOMEM;
489432f69e4bSDaniel Jurgens 			}
489532f69e4bSDaniel Jurgens 
489632f69e4bSDaniel Jurgens 			mpi->is_master = true;
489732f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
489832f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
489932f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
490032f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
490132f69e4bSDaniel Jurgens 			mpi = NULL;
490232f69e4bSDaniel Jurgens 			continue;
490332f69e4bSDaniel Jurgens 		}
490432f69e4bSDaniel Jurgens 
490532f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
490632f69e4bSDaniel Jurgens 				    list) {
490732f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
490832f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
490932f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
491032f69e4bSDaniel Jurgens 			}
491132f69e4bSDaniel Jurgens 
491232f69e4bSDaniel Jurgens 			if (bound) {
491332f69e4bSDaniel Jurgens 				dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
491432f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
491532f69e4bSDaniel Jurgens 				list_del(&mpi->list);
491632f69e4bSDaniel Jurgens 				break;
491732f69e4bSDaniel Jurgens 			}
491832f69e4bSDaniel Jurgens 		}
491932f69e4bSDaniel Jurgens 		if (!bound) {
492032f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
492132f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
492232f69e4bSDaniel Jurgens 				    i + 1);
492332f69e4bSDaniel Jurgens 		}
492432f69e4bSDaniel Jurgens 	}
492532f69e4bSDaniel Jurgens 
492632f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
492732f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
492832f69e4bSDaniel Jurgens 	return err;
492932f69e4bSDaniel Jurgens }
493032f69e4bSDaniel Jurgens 
493132f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
493232f69e4bSDaniel Jurgens {
493332f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
493432f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
493532f69e4bSDaniel Jurgens 							  port_num + 1);
493632f69e4bSDaniel Jurgens 	int i;
493732f69e4bSDaniel Jurgens 
493832f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
493932f69e4bSDaniel Jurgens 		return;
494032f69e4bSDaniel Jurgens 
494132f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
494232f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
494332f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
494432f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
494532f69e4bSDaniel Jurgens 			if (i == port_num) {
494632f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
494732f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
494832f69e4bSDaniel Jurgens 			} else {
494932f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
495032f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
495132f69e4bSDaniel Jurgens 			}
495232f69e4bSDaniel Jurgens 		}
495332f69e4bSDaniel Jurgens 	}
495432f69e4bSDaniel Jurgens 
495532f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
495632f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
495732f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
495832f69e4bSDaniel Jurgens 
495932f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
496032f69e4bSDaniel Jurgens }
496132f69e4bSDaniel Jurgens 
496224da0016SAriel Levkovich ADD_UVERBS_ATTRIBUTES_SIMPLE(mlx5_ib_dm, UVERBS_OBJECT_DM,
496324da0016SAriel Levkovich 			     UVERBS_METHOD_DM_ALLOC,
496424da0016SAriel Levkovich 			     &UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
496524da0016SAriel Levkovich 						  UVERBS_ATTR_TYPE(u64),
496624da0016SAriel Levkovich 						  UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)),
496724da0016SAriel Levkovich 			     &UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
496824da0016SAriel Levkovich 						  UVERBS_ATTR_TYPE(u16),
496924da0016SAriel Levkovich 						  UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)));
497024da0016SAriel Levkovich 
4971c6475a0bSAviad Yehezkel ADD_UVERBS_ATTRIBUTES_SIMPLE(mlx5_ib_flow_action, UVERBS_OBJECT_FLOW_ACTION,
4972c6475a0bSAviad Yehezkel 			     UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
4973c6475a0bSAviad Yehezkel 			     &UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4974c6475a0bSAviad Yehezkel 						 UVERBS_ATTR_TYPE(u64),
4975c6475a0bSAviad Yehezkel 						 UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)));
4976c6475a0bSAviad Yehezkel 
497724da0016SAriel Levkovich #define NUM_TREES	2
49788c84660bSMatan Barak static int populate_specs_root(struct mlx5_ib_dev *dev)
49798c84660bSMatan Barak {
49808c84660bSMatan Barak 	const struct uverbs_object_tree_def *default_root[NUM_TREES + 1] = {
49818c84660bSMatan Barak 		uverbs_default_get_objects()};
49828c84660bSMatan Barak 	size_t num_trees = 1;
49838c84660bSMatan Barak 
4984c6475a0bSAviad Yehezkel 	if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
4985c6475a0bSAviad Yehezkel 	    !WARN_ON(num_trees >= ARRAY_SIZE(default_root)))
4986c6475a0bSAviad Yehezkel 		default_root[num_trees++] = &mlx5_ib_flow_action;
4987c6475a0bSAviad Yehezkel 
498824da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(dev->mdev, memic) &&
498924da0016SAriel Levkovich 	    !WARN_ON(num_trees >= ARRAY_SIZE(default_root)))
499024da0016SAriel Levkovich 		default_root[num_trees++] = &mlx5_ib_dm;
499124da0016SAriel Levkovich 
49928c84660bSMatan Barak 	dev->ib_dev.specs_root =
49938c84660bSMatan Barak 		uverbs_alloc_spec_tree(num_trees, default_root);
49948c84660bSMatan Barak 
49958c84660bSMatan Barak 	return PTR_ERR_OR_ZERO(dev->ib_dev.specs_root);
49968c84660bSMatan Barak }
49978c84660bSMatan Barak 
49988c84660bSMatan Barak static void depopulate_specs_root(struct mlx5_ib_dev *dev)
49998c84660bSMatan Barak {
50008c84660bSMatan Barak 	uverbs_free_spec_tree(dev->ib_dev.specs_root);
50018c84660bSMatan Barak }
50028c84660bSMatan Barak 
5003b5ca15adSMark Bloch void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
5004e126ba97SEli Cohen {
500532f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
50063cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
50073cc297dbSMark Bloch 	cleanup_srcu_struct(&dev->mr_srcu);
50083cc297dbSMark Bloch #endif
500916c1975fSMark Bloch 	kfree(dev->port);
501016c1975fSMark Bloch }
501116c1975fSMark Bloch 
5012b5ca15adSMark Bloch int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
501316c1975fSMark Bloch {
501416c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
50154babcf97SAviv Heller 	const char *name;
5016e126ba97SEli Cohen 	int err;
501732f69e4bSDaniel Jurgens 	int i;
5018e126ba97SEli Cohen 
5019508562d6SDaniel Jurgens 	dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
50200837e86aSMark Bloch 			    GFP_KERNEL);
50210837e86aSMark Bloch 	if (!dev->port)
502216c1975fSMark Bloch 		return -ENOMEM;
50230837e86aSMark Bloch 
502432f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
502532f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
502632f69e4bSDaniel Jurgens 		rwlock_init(&dev->roce[i].netdev_lock);
502732f69e4bSDaniel Jurgens 	}
502832f69e4bSDaniel Jurgens 
502932f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
5030e126ba97SEli Cohen 	if (err)
50310837e86aSMark Bloch 		goto err_free_port;
5032e126ba97SEli Cohen 
503332f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
503432f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
503532f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
503632f69e4bSDaniel Jurgens 			if (err)
503732f69e4bSDaniel Jurgens 				break;
503832f69e4bSDaniel Jurgens 		}
503932f69e4bSDaniel Jurgens 	} else {
504032f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
504132f69e4bSDaniel Jurgens 	}
504232f69e4bSDaniel Jurgens 	if (err)
504332f69e4bSDaniel Jurgens 		goto err_mp;
504432f69e4bSDaniel Jurgens 
50451b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
5046e126ba97SEli Cohen 		get_ext_port_caps(dev);
5047e126ba97SEli Cohen 
50484babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
50494babcf97SAviv Heller 		name = "mlx5_%d";
50504babcf97SAviv Heller 	else
50514babcf97SAviv Heller 		name = "mlx5_bond_%d";
50524babcf97SAviv Heller 
50534babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
5054e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
5055e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
5056c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
5057e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
5058233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
5059233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
50609b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
5061e126ba97SEli Cohen 
50623cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
50633cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
50643cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
50653cc297dbSMark Bloch 
506624da0016SAriel Levkovich 	spin_lock_init(&dev->memic.memic_lock);
506724da0016SAriel Levkovich 	dev->memic.dev = mdev;
506824da0016SAriel Levkovich 
50693cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
50703cc297dbSMark Bloch 	err = init_srcu_struct(&dev->mr_srcu);
50713cc297dbSMark Bloch 	if (err)
50723cc297dbSMark Bloch 		goto err_free_port;
50733cc297dbSMark Bloch #endif
50743cc297dbSMark Bloch 
507516c1975fSMark Bloch 	return 0;
507632f69e4bSDaniel Jurgens err_mp:
507732f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
507816c1975fSMark Bloch 
507916c1975fSMark Bloch err_free_port:
508016c1975fSMark Bloch 	kfree(dev->port);
508116c1975fSMark Bloch 
508216c1975fSMark Bloch 	return -ENOMEM;
508316c1975fSMark Bloch }
508416c1975fSMark Bloch 
50859a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
50869a4ca38dSMark Bloch {
50879a4ca38dSMark Bloch 	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
50889a4ca38dSMark Bloch 
50899a4ca38dSMark Bloch 	if (!dev->flow_db)
50909a4ca38dSMark Bloch 		return -ENOMEM;
50919a4ca38dSMark Bloch 
50929a4ca38dSMark Bloch 	mutex_init(&dev->flow_db->lock);
50939a4ca38dSMark Bloch 
50949a4ca38dSMark Bloch 	return 0;
50959a4ca38dSMark Bloch }
50969a4ca38dSMark Bloch 
5097b5ca15adSMark Bloch int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
5098b5ca15adSMark Bloch {
5099b5ca15adSMark Bloch 	struct mlx5_ib_dev *nic_dev;
5100b5ca15adSMark Bloch 
5101b5ca15adSMark Bloch 	nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
5102b5ca15adSMark Bloch 
5103b5ca15adSMark Bloch 	if (!nic_dev)
5104b5ca15adSMark Bloch 		return -EINVAL;
5105b5ca15adSMark Bloch 
5106b5ca15adSMark Bloch 	dev->flow_db = nic_dev->flow_db;
5107b5ca15adSMark Bloch 
5108b5ca15adSMark Bloch 	return 0;
5109b5ca15adSMark Bloch }
5110b5ca15adSMark Bloch 
51119a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
51129a4ca38dSMark Bloch {
51139a4ca38dSMark Bloch 	kfree(dev->flow_db);
51149a4ca38dSMark Bloch }
51159a4ca38dSMark Bloch 
5116b5ca15adSMark Bloch int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
511716c1975fSMark Bloch {
511816c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
511916c1975fSMark Bloch 	int err;
512016c1975fSMark Bloch 
5121e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
5122e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
5123e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
5124e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
5125e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
5126e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
5127e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
512841c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
512941c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
5130e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
513156e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
5132e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
5133e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
5134e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
5135e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
5136e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
5137e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
5138e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
5139e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
5140e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
5141e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
5142e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
5143e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
5144e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
5145e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
5146e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
5147e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
5148e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
51491707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
5150d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
5151d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
51527d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
5153b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
5154b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
5155e126ba97SEli Cohen 
5156e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
5157ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
5158e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
51593cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
51603cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
5161e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
5162e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
5163e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
5164e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
5165e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
5166e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
5167e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
5168e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
5169e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
5170e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
5171e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
5172e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
5173e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
5174e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
5175e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
5176e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
5177e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
5178e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
5179e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
5180e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
5181e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
5182e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
5183e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
5184e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
5185e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
5186e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
5187e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
5188e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
5189e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
5190e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
519156e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
5192e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
5193e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
5194e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
5195e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
51969bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
51978a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
5198d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
5199c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
520040b24403SSagi Grimberg 	dev->ib_dev.get_vector_affinity	= mlx5_ib_get_vector_affinity;
52018e959601SNiranjana Vishwanathapura 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
5202693dfd5aSErez Shitrit 		dev->ib_dev.alloc_rdma_netdev	= mlx5_ib_alloc_rdma_netdev;
52038e959601SNiranjana Vishwanathapura 
5204eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
5205eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
5206eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
5207eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
5208eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
5209eff901d3SEli Cohen 	}
5210e126ba97SEli Cohen 
52117c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
52127c2344c3SMaor Gottlieb 
52136e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
52146e8484c5SMax Gurtovoy 
5215d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
5216d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
5217d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
5218d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
5219d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
5220d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
5221d2370e0aSMatan Barak 	}
5222d2370e0aSMatan Barak 
5223938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
5224e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
5225e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
5226e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
5227e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
5228e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
5229e126ba97SEli Cohen 	}
5230e126ba97SEli Cohen 
523124da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
523224da0016SAriel Levkovich 		dev->ib_dev.alloc_dm = mlx5_ib_alloc_dm;
523324da0016SAriel Levkovich 		dev->ib_dev.dealloc_dm = mlx5_ib_dealloc_dm;
52346c29f57eSAriel Levkovich 		dev->ib_dev.reg_dm_mr = mlx5_ib_reg_dm_mr;
523524da0016SAriel Levkovich 	}
523624da0016SAriel Levkovich 
5237038d2ef8SMaor Gottlieb 	dev->ib_dev.create_flow	= mlx5_ib_create_flow;
5238038d2ef8SMaor Gottlieb 	dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
523981e30880SYishai Hadas 	dev->ib_dev.uverbs_ex_cmd_mask |=
524081e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
524181e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
5242c6475a0bSAviad Yehezkel 	dev->ib_dev.create_flow_action_esp = mlx5_ib_create_flow_action_esp;
5243c6475a0bSAviad Yehezkel 	dev->ib_dev.destroy_flow_action = mlx5_ib_destroy_flow_action;
5244349705c1SMatan Barak 	dev->ib_dev.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp;
52450ede73bcSMatan Barak 	dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
524681e30880SYishai Hadas 
5247e126ba97SEli Cohen 	err = init_node_data(dev);
5248e126ba97SEli Cohen 	if (err)
524916c1975fSMark Bloch 		return err;
5250e126ba97SEli Cohen 
5251c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
5252e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
5253e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
5254c8b89924SMark Bloch 		mutex_init(&dev->lb_mutex);
5255c8b89924SMark Bloch 
525616c1975fSMark Bloch 	return 0;
525716c1975fSMark Bloch }
525816c1975fSMark Bloch 
52598e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
526016c1975fSMark Bloch {
52618e6efa3aSMark Bloch 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
52628e6efa3aSMark Bloch 	dev->ib_dev.query_port		= mlx5_ib_query_port;
52638e6efa3aSMark Bloch 
52648e6efa3aSMark Bloch 	return 0;
52658e6efa3aSMark Bloch }
52668e6efa3aSMark Bloch 
5267b5ca15adSMark Bloch int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
52688e6efa3aSMark Bloch {
52698e6efa3aSMark Bloch 	dev->ib_dev.get_port_immutable  = mlx5_port_rep_immutable;
52708e6efa3aSMark Bloch 	dev->ib_dev.query_port		= mlx5_ib_rep_query_port;
52718e6efa3aSMark Bloch 
52728e6efa3aSMark Bloch 	return 0;
52738e6efa3aSMark Bloch }
52748e6efa3aSMark Bloch 
52758e6efa3aSMark Bloch static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev,
52768e6efa3aSMark Bloch 					  u8 port_num)
52778e6efa3aSMark Bloch {
52787fd8aefbSDaniel Jurgens 	int i;
527916c1975fSMark Bloch 
52807fd8aefbSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
52817fd8aefbSDaniel Jurgens 		dev->roce[i].dev = dev;
52827fd8aefbSDaniel Jurgens 		dev->roce[i].native_port_num = i + 1;
52837fd8aefbSDaniel Jurgens 		dev->roce[i].last_port_state = IB_PORT_DOWN;
52847fd8aefbSDaniel Jurgens 	}
52857fd8aefbSDaniel Jurgens 
5286c11a226aSMark Bloch 	dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
5287c11a226aSMark Bloch 	dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
5288c11a226aSMark Bloch 	dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
5289c11a226aSMark Bloch 	dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
5290c11a226aSMark Bloch 	dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
5291c11a226aSMark Bloch 	dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
52928e6efa3aSMark Bloch 
5293c11a226aSMark Bloch 	dev->ib_dev.uverbs_ex_cmd_mask |=
5294c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
5295c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
5296c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
5297c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
5298c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
52998e6efa3aSMark Bloch 
53008e6efa3aSMark Bloch 	return mlx5_add_netdev_notifier(dev, port_num);
53018e6efa3aSMark Bloch }
53028e6efa3aSMark Bloch 
53038e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
53048e6efa3aSMark Bloch {
53058e6efa3aSMark Bloch 	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
53068e6efa3aSMark Bloch 
53078e6efa3aSMark Bloch 	mlx5_remove_netdev_notifier(dev, port_num);
53088e6efa3aSMark Bloch }
53098e6efa3aSMark Bloch 
53108e6efa3aSMark Bloch int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
53118e6efa3aSMark Bloch {
53128e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
53138e6efa3aSMark Bloch 	enum rdma_link_layer ll;
53148e6efa3aSMark Bloch 	int port_type_cap;
53158e6efa3aSMark Bloch 	int err = 0;
53168e6efa3aSMark Bloch 	u8 port_num;
53178e6efa3aSMark Bloch 
53188e6efa3aSMark Bloch 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
53198e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
53208e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
53218e6efa3aSMark Bloch 
53228e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET)
53238e6efa3aSMark Bloch 		err = mlx5_ib_stage_common_roce_init(dev, port_num);
53248e6efa3aSMark Bloch 
532516c1975fSMark Bloch 	return err;
5326fc24fc5eSAchiad Shochat }
5327fc24fc5eSAchiad Shochat 
53288e6efa3aSMark Bloch void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
53298e6efa3aSMark Bloch {
53308e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
53318e6efa3aSMark Bloch }
53328e6efa3aSMark Bloch 
53338e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
53348e6efa3aSMark Bloch {
53358e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
53368e6efa3aSMark Bloch 	enum rdma_link_layer ll;
53378e6efa3aSMark Bloch 	int port_type_cap;
53388e6efa3aSMark Bloch 	u8 port_num;
53398e6efa3aSMark Bloch 	int err;
53408e6efa3aSMark Bloch 
53418e6efa3aSMark Bloch 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
53428e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
53438e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
53448e6efa3aSMark Bloch 
53458e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
53468e6efa3aSMark Bloch 		err = mlx5_ib_stage_common_roce_init(dev, port_num);
53478e6efa3aSMark Bloch 		if (err)
53488e6efa3aSMark Bloch 			return err;
53498e6efa3aSMark Bloch 
53508e6efa3aSMark Bloch 		err = mlx5_enable_eth(dev, port_num);
53518e6efa3aSMark Bloch 		if (err)
53528e6efa3aSMark Bloch 			goto cleanup;
53538e6efa3aSMark Bloch 	}
53548e6efa3aSMark Bloch 
535516c1975fSMark Bloch 	return 0;
53568e6efa3aSMark Bloch cleanup:
53578e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
53588e6efa3aSMark Bloch 
53598e6efa3aSMark Bloch 	return err;
536045bded2cSKamal Heib }
53616aec21f6SHaggai Eran 
536216c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
536316c1975fSMark Bloch {
536416c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
536516c1975fSMark Bloch 	enum rdma_link_layer ll;
536616c1975fSMark Bloch 	int port_type_cap;
536732f69e4bSDaniel Jurgens 	u8 port_num;
53684a2da0b8SParav Pandit 
536932f69e4bSDaniel Jurgens 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
537016c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
537116c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
537216c1975fSMark Bloch 
537316c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
537416c1975fSMark Bloch 		mlx5_disable_eth(dev);
53758e6efa3aSMark Bloch 		mlx5_ib_stage_common_roce_cleanup(dev);
537616c1975fSMark Bloch 	}
537716c1975fSMark Bloch }
537816c1975fSMark Bloch 
5379b5ca15adSMark Bloch int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
538016c1975fSMark Bloch {
538116c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
538216c1975fSMark Bloch }
538316c1975fSMark Bloch 
5384b5ca15adSMark Bloch void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
538516c1975fSMark Bloch {
538616c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
538716c1975fSMark Bloch }
538816c1975fSMark Bloch 
538916c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
539016c1975fSMark Bloch {
539107321b3cSMark Bloch 	mlx5_ib_internal_fill_odp_caps(dev);
539207321b3cSMark Bloch 
539316c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
539416c1975fSMark Bloch }
539516c1975fSMark Bloch 
5396b5ca15adSMark Bloch int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
539716c1975fSMark Bloch {
53985e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
53995e1e7612SMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
54005e1e7612SMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
54015e1e7612SMark Bloch 
540216c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
54035e1e7612SMark Bloch 	}
540416c1975fSMark Bloch 
540516c1975fSMark Bloch 	return 0;
540616c1975fSMark Bloch }
540716c1975fSMark Bloch 
5408b5ca15adSMark Bloch void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
540916c1975fSMark Bloch {
541016c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
541116c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
541216c1975fSMark Bloch }
541316c1975fSMark Bloch 
541416c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
541516c1975fSMark Bloch {
5416a9e546e7SParav Pandit 	return mlx5_ib_init_cong_debugfs(dev,
5417a9e546e7SParav Pandit 					 mlx5_core_native_port_num(dev->mdev) - 1);
541816c1975fSMark Bloch }
541916c1975fSMark Bloch 
542016c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
542116c1975fSMark Bloch {
5422a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
5423a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
542416c1975fSMark Bloch }
542516c1975fSMark Bloch 
542616c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
542716c1975fSMark Bloch {
54285fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
5429444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
543016c1975fSMark Bloch }
543116c1975fSMark Bloch 
543216c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
543316c1975fSMark Bloch {
543416c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
543516c1975fSMark Bloch }
543616c1975fSMark Bloch 
5437b5ca15adSMark Bloch int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
543816c1975fSMark Bloch {
543916c1975fSMark Bloch 	int err;
54405fe9dec0SEli Cohen 
54415fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
54425fe9dec0SEli Cohen 	if (err)
544316c1975fSMark Bloch 		return err;
54445fe9dec0SEli Cohen 
54455fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
54465fe9dec0SEli Cohen 	if (err)
544716c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
54485fe9dec0SEli Cohen 
544916c1975fSMark Bloch 	return err;
545016c1975fSMark Bloch }
54510837e86aSMark Bloch 
5452b5ca15adSMark Bloch void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
545316c1975fSMark Bloch {
545416c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
545516c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
545616c1975fSMark Bloch }
5457e126ba97SEli Cohen 
54588c84660bSMatan Barak static int mlx5_ib_stage_populate_specs(struct mlx5_ib_dev *dev)
54598c84660bSMatan Barak {
54608c84660bSMatan Barak 	return populate_specs_root(dev);
54618c84660bSMatan Barak }
54628c84660bSMatan Barak 
5463b5ca15adSMark Bloch int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
546416c1975fSMark Bloch {
546516c1975fSMark Bloch 	return ib_register_device(&dev->ib_dev, NULL);
546616c1975fSMark Bloch }
546716c1975fSMark Bloch 
54688c84660bSMatan Barak static void mlx5_ib_stage_depopulate_specs(struct mlx5_ib_dev *dev)
54698c84660bSMatan Barak {
54708c84660bSMatan Barak 	depopulate_specs_root(dev);
54718c84660bSMatan Barak }
54728c84660bSMatan Barak 
547303fe2debSDavid S. Miller void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
547442cea83fSMark Bloch {
547542cea83fSMark Bloch 	destroy_umrc_res(dev);
547642cea83fSMark Bloch }
547742cea83fSMark Bloch 
5478b5ca15adSMark Bloch void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
547916c1975fSMark Bloch {
548016c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
548116c1975fSMark Bloch }
548216c1975fSMark Bloch 
548303fe2debSDavid S. Miller int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
548416c1975fSMark Bloch {
548516c1975fSMark Bloch 	return create_umr_res(dev);
548616c1975fSMark Bloch }
548716c1975fSMark Bloch 
548816c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
548916c1975fSMark Bloch {
549003404e8aSMaor Gottlieb 	init_delay_drop(dev);
549103404e8aSMaor Gottlieb 
549216c1975fSMark Bloch 	return 0;
549316c1975fSMark Bloch }
549416c1975fSMark Bloch 
549516c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
549616c1975fSMark Bloch {
549716c1975fSMark Bloch 	cancel_delay_drop(dev);
549816c1975fSMark Bloch }
549916c1975fSMark Bloch 
5500b5ca15adSMark Bloch int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev)
550116c1975fSMark Bloch {
550216c1975fSMark Bloch 	int err;
550316c1975fSMark Bloch 	int i;
550416c1975fSMark Bloch 
5505e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
5506281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
5507281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
5508281d1a92SWei Yongjun 		if (err)
550916c1975fSMark Bloch 			return err;
5510e126ba97SEli Cohen 	}
5511e126ba97SEli Cohen 
551216c1975fSMark Bloch 	return 0;
551316c1975fSMark Bloch }
551416c1975fSMark Bloch 
5515fc385b7aSMark Bloch static int mlx5_ib_stage_rep_reg_init(struct mlx5_ib_dev *dev)
5516fc385b7aSMark Bloch {
5517fc385b7aSMark Bloch 	mlx5_ib_register_vport_reps(dev);
5518fc385b7aSMark Bloch 
5519fc385b7aSMark Bloch 	return 0;
5520fc385b7aSMark Bloch }
5521fc385b7aSMark Bloch 
5522fc385b7aSMark Bloch static void mlx5_ib_stage_rep_reg_cleanup(struct mlx5_ib_dev *dev)
5523fc385b7aSMark Bloch {
5524fc385b7aSMark Bloch 	mlx5_ib_unregister_vport_reps(dev);
5525fc385b7aSMark Bloch }
5526fc385b7aSMark Bloch 
5527b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
552816c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
552916c1975fSMark Bloch 		      int stage)
553016c1975fSMark Bloch {
553116c1975fSMark Bloch 	/* Number of stages to cleanup */
553216c1975fSMark Bloch 	while (stage) {
553316c1975fSMark Bloch 		stage--;
553416c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
553516c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
553616c1975fSMark Bloch 	}
553716c1975fSMark Bloch 
553816c1975fSMark Bloch 	ib_dealloc_device((struct ib_device *)dev);
553916c1975fSMark Bloch }
554016c1975fSMark Bloch 
554132f69e4bSDaniel Jurgens static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num);
554232f69e4bSDaniel Jurgens 
5543b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
554416c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
554516c1975fSMark Bloch {
554616c1975fSMark Bloch 	int err;
554716c1975fSMark Bloch 	int i;
554816c1975fSMark Bloch 
554916c1975fSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
555016c1975fSMark Bloch 
555116c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
555216c1975fSMark Bloch 		if (profile->stage[i].init) {
555316c1975fSMark Bloch 			err = profile->stage[i].init(dev);
555416c1975fSMark Bloch 			if (err)
555516c1975fSMark Bloch 				goto err_out;
555616c1975fSMark Bloch 		}
555716c1975fSMark Bloch 	}
555816c1975fSMark Bloch 
555916c1975fSMark Bloch 	dev->profile = profile;
5560e126ba97SEli Cohen 	dev->ib_active = true;
5561e126ba97SEli Cohen 
55629603b61dSJack Morgenstein 	return dev;
5563e126ba97SEli Cohen 
556416c1975fSMark Bloch err_out:
556516c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
5566e126ba97SEli Cohen 
55679603b61dSJack Morgenstein 	return NULL;
5568e126ba97SEli Cohen }
5569e126ba97SEli Cohen 
557016c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
557116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
557216c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
557316c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
55749a4ca38dSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
55759a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_init,
55769a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
557716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
557816c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
557916c1975fSMark Bloch 		     NULL),
55808e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
55818e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
55828e6efa3aSMark Bloch 		     NULL),
558316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
558416c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
558516c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
558616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
558716c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
558816c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
558916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
559016c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
55913cc297dbSMark Bloch 		     NULL),
559216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
559316c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
559416c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
559516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
559616c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
559716c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
559816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
559916c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
560016c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
560116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
560216c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
560316c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
560442cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
560542cea83fSMark Bloch 		     NULL,
560642cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
56078c84660bSMatan Barak 	STAGE_CREATE(MLX5_IB_STAGE_SPECS,
56088c84660bSMatan Barak 		     mlx5_ib_stage_populate_specs,
56098c84660bSMatan Barak 		     mlx5_ib_stage_depopulate_specs),
561016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
561116c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
561216c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
561342cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
561442cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
561542cea83fSMark Bloch 		     NULL),
561616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
561716c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
561816c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
561916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
562016c1975fSMark Bloch 		     mlx5_ib_stage_class_attr_init,
562116c1975fSMark Bloch 		     NULL),
562216c1975fSMark Bloch };
562316c1975fSMark Bloch 
5624b5ca15adSMark Bloch static const struct mlx5_ib_profile nic_rep_profile = {
5625b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
5626b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
5627b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
5628b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
5629b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_init,
5630b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
5631b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
5632b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
5633b5ca15adSMark Bloch 		     NULL),
5634b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
5635b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_non_default_cb,
5636b5ca15adSMark Bloch 		     NULL),
5637b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
5638b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_init,
5639b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_cleanup),
5640b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
5641b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_init,
5642b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
5643b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
5644b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_init,
5645b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
5646b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
5647b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
5648b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
5649b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
5650b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
5651b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
565203fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
565303fe2debSDavid S. Miller 		     NULL,
565403fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
56558c84660bSMatan Barak 	STAGE_CREATE(MLX5_IB_STAGE_SPECS,
56568c84660bSMatan Barak 		     mlx5_ib_stage_populate_specs,
56578c84660bSMatan Barak 		     mlx5_ib_stage_depopulate_specs),
5658b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
5659b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
5660b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
566103fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
566203fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
566303fe2debSDavid S. Miller 		     NULL),
5664b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
5665b5ca15adSMark Bloch 		     mlx5_ib_stage_class_attr_init,
5666b5ca15adSMark Bloch 		     NULL),
5667b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_REP_REG,
5668b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_reg_init,
5669b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_reg_cleanup),
5670b5ca15adSMark Bloch };
5671b5ca15adSMark Bloch 
567232f69e4bSDaniel Jurgens static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num)
567332f69e4bSDaniel Jurgens {
567432f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
567532f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
567632f69e4bSDaniel Jurgens 	bool bound = false;
567732f69e4bSDaniel Jurgens 	int err;
567832f69e4bSDaniel Jurgens 
567932f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
568032f69e4bSDaniel Jurgens 	if (!mpi)
568132f69e4bSDaniel Jurgens 		return NULL;
568232f69e4bSDaniel Jurgens 
568332f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
568432f69e4bSDaniel Jurgens 
568532f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
568632f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
568732f69e4bSDaniel Jurgens 	if (err) {
568832f69e4bSDaniel Jurgens 		kfree(mpi);
568932f69e4bSDaniel Jurgens 		return NULL;
569032f69e4bSDaniel Jurgens 	}
569132f69e4bSDaniel Jurgens 
569232f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
569332f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
569432f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
569532f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
569632f69e4bSDaniel Jurgens 
569732f69e4bSDaniel Jurgens 		if (bound) {
569832f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
569932f69e4bSDaniel Jurgens 			break;
570032f69e4bSDaniel Jurgens 		}
570132f69e4bSDaniel Jurgens 	}
570232f69e4bSDaniel Jurgens 
570332f69e4bSDaniel Jurgens 	if (!bound) {
570432f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
570532f69e4bSDaniel Jurgens 		dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
570632f69e4bSDaniel Jurgens 	} else {
570732f69e4bSDaniel Jurgens 		mlx5_ib_dbg(dev, "bound port %u\n", port_num + 1);
570832f69e4bSDaniel Jurgens 	}
570932f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
571032f69e4bSDaniel Jurgens 
571132f69e4bSDaniel Jurgens 	return mpi;
571232f69e4bSDaniel Jurgens }
571332f69e4bSDaniel Jurgens 
571416c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
571516c1975fSMark Bloch {
571632f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
5717b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
571832f69e4bSDaniel Jurgens 	int port_type_cap;
571932f69e4bSDaniel Jurgens 
5720b5ca15adSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
5721b5ca15adSMark Bloch 
572232f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
572332f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
572432f69e4bSDaniel Jurgens 
572532f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET) {
572632f69e4bSDaniel Jurgens 		u8 port_num = mlx5_core_native_port_num(mdev) - 1;
572732f69e4bSDaniel Jurgens 
572832f69e4bSDaniel Jurgens 		return mlx5_ib_add_slave_port(mdev, port_num);
572932f69e4bSDaniel Jurgens 	}
573032f69e4bSDaniel Jurgens 
5731b5ca15adSMark Bloch 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
5732b5ca15adSMark Bloch 	if (!dev)
5733b5ca15adSMark Bloch 		return NULL;
5734b5ca15adSMark Bloch 
5735b5ca15adSMark Bloch 	dev->mdev = mdev;
5736b5ca15adSMark Bloch 	dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
5737b5ca15adSMark Bloch 			     MLX5_CAP_GEN(mdev, num_vhca_ports));
5738b5ca15adSMark Bloch 
5739b5ca15adSMark Bloch 	if (MLX5_VPORT_MANAGER(mdev) &&
5740b5ca15adSMark Bloch 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
5741b5ca15adSMark Bloch 		dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0);
5742b5ca15adSMark Bloch 
5743b5ca15adSMark Bloch 		return __mlx5_ib_add(dev, &nic_rep_profile);
5744b5ca15adSMark Bloch 	}
5745b5ca15adSMark Bloch 
5746b5ca15adSMark Bloch 	return __mlx5_ib_add(dev, &pf_profile);
574716c1975fSMark Bloch }
574816c1975fSMark Bloch 
57499603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
5750e126ba97SEli Cohen {
575132f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
575232f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
57536aec21f6SHaggai Eran 
575432f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
575532f69e4bSDaniel Jurgens 		mpi = context;
575632f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
575732f69e4bSDaniel Jurgens 		if (mpi->ibdev)
575832f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
575932f69e4bSDaniel Jurgens 		list_del(&mpi->list);
576032f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
576132f69e4bSDaniel Jurgens 		return;
576232f69e4bSDaniel Jurgens 	}
576332f69e4bSDaniel Jurgens 
576432f69e4bSDaniel Jurgens 	dev = context;
576516c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
5766e126ba97SEli Cohen }
5767e126ba97SEli Cohen 
57689603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
57699603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
57709603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
57719603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
5772d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5773d9aaed83SArtemy Kovalyov 	.pfault		= mlx5_ib_pfault,
5774d9aaed83SArtemy Kovalyov #endif
577564613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
5776e126ba97SEli Cohen };
5777e126ba97SEli Cohen 
5778c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
5779c44ef998SIlya Lesokhin {
5780c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
5781c44ef998SIlya Lesokhin 	return xlt_emergency_page;
5782c44ef998SIlya Lesokhin }
5783c44ef998SIlya Lesokhin 
5784c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
5785c44ef998SIlya Lesokhin {
5786c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
5787c44ef998SIlya Lesokhin }
5788c44ef998SIlya Lesokhin 
5789e126ba97SEli Cohen static int __init mlx5_ib_init(void)
5790e126ba97SEli Cohen {
57916aec21f6SHaggai Eran 	int err;
57926aec21f6SHaggai Eran 
5793c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
5794c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
5795d69a24e0SDaniel Jurgens 		return -ENOMEM;
5796d69a24e0SDaniel Jurgens 
5797c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
5798c44ef998SIlya Lesokhin 
5799c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
5800c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
5801c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
5802c44ef998SIlya Lesokhin 		return -ENOMEM;
5803c44ef998SIlya Lesokhin 	}
5804c44ef998SIlya Lesokhin 
580581713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
58069603b61dSJack Morgenstein 
58076aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
58086aec21f6SHaggai Eran 
58096aec21f6SHaggai Eran 	return err;
5810e126ba97SEli Cohen }
5811e126ba97SEli Cohen 
5812e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
5813e126ba97SEli Cohen {
58149603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
5815d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
5816c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
5817c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
5818e126ba97SEli Cohen }
5819e126ba97SEli Cohen 
5820e126ba97SEli Cohen module_init(mlx5_ib_init);
5821e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
5822