xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 7c34ec19e10c0d13ca2f3435fb85d2dddccad917)
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"
63f3da6577SLeon Romanovsky #include "srq.h"
643346c487SBoris Pismenny #include <linux/mlx5/fs_helpers.h>
65c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h>
668c84660bSMatan Barak #include <rdma/uverbs_std_types.h>
67c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h>
68c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h>
698c84660bSMatan Barak 
708c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
718c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
72e126ba97SEli Cohen 
73e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
74b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0"
75e126ba97SEli Cohen 
76e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
77e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
78e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
79e126ba97SEli Cohen 
80e126ba97SEli Cohen static char mlx5_version[] =
81e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
82b359911dSTariq Toukan 	DRIVER_VERSION "\n";
83e126ba97SEli Cohen 
84d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
85d69a24e0SDaniel Jurgens 	struct work_struct	work;
86df097a27SSaeed Mahameed 	union {
87df097a27SSaeed Mahameed 		struct mlx5_ib_dev	      *dev;
88df097a27SSaeed Mahameed 		struct mlx5_ib_multiport_info *mpi;
89df097a27SSaeed Mahameed 	};
90df097a27SSaeed Mahameed 	bool			is_slave;
91134e9349SSaeed Mahameed 	unsigned int		event;
92df097a27SSaeed Mahameed 	void			*param;
93d69a24e0SDaniel Jurgens };
94d69a24e0SDaniel Jurgens 
95da7525d2SEran Ben Elisha enum {
96da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
97da7525d2SEran Ben Elisha };
981b5daf11SMajd Dibbiny 
99d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
10032f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
10132f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
10232f69e4bSDaniel Jurgens /*
10332f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
10432f69e4bSDaniel Jurgens  */
10532f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
10632f69e4bSDaniel Jurgens 
107c44ef998SIlya Lesokhin /* We can't use an array for xlt_emergency_page because dma_map_single
108c44ef998SIlya Lesokhin  * doesn't work on kernel modules memory
109c44ef998SIlya Lesokhin  */
110c44ef998SIlya Lesokhin static unsigned long xlt_emergency_page;
111c44ef998SIlya Lesokhin static struct mutex xlt_emergency_page_mutex;
112c44ef998SIlya Lesokhin 
11332f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
11432f69e4bSDaniel Jurgens {
11532f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
11632f69e4bSDaniel Jurgens 
11732f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
11832f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
11932f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
12032f69e4bSDaniel Jurgens 	return dev;
12132f69e4bSDaniel Jurgens }
12232f69e4bSDaniel Jurgens 
1231b5daf11SMajd Dibbiny static enum rdma_link_layer
124ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
1251b5daf11SMajd Dibbiny {
126ebd61f68SAchiad Shochat 	switch (port_type_cap) {
1271b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
1281b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
1291b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
1301b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
1311b5daf11SMajd Dibbiny 	default:
1321b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1331b5daf11SMajd Dibbiny 	}
1341b5daf11SMajd Dibbiny }
1351b5daf11SMajd Dibbiny 
136ebd61f68SAchiad Shochat static enum rdma_link_layer
137ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
138ebd61f68SAchiad Shochat {
139ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
140ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
141ebd61f68SAchiad Shochat 
142ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
143ebd61f68SAchiad Shochat }
144ebd61f68SAchiad Shochat 
145fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
146fd65f1b8SMoni Shoua 			  u8 port_num,
147fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
148fd65f1b8SMoni Shoua {
149fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
150fd65f1b8SMoni Shoua 	int ret;
151fd65f1b8SMoni Shoua 
152fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1538e6efa3aSMark Bloch 	ret = ibdev->query_port(ibdev, port_num, &attr);
154fd65f1b8SMoni Shoua 	if (!ret)
155fd65f1b8SMoni Shoua 		*state = attr.state;
156fd65f1b8SMoni Shoua 	return ret;
157fd65f1b8SMoni Shoua }
158fd65f1b8SMoni Shoua 
159fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
160fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
161fc24fc5eSAchiad Shochat {
1627fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
163fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1647fd8aefbSDaniel Jurgens 	u8 port_num = roce->native_port_num;
1657fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1667fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1677fd8aefbSDaniel Jurgens 
1687fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
16932f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
17032f69e4bSDaniel Jurgens 	if (!mdev)
17132f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
172fc24fc5eSAchiad Shochat 
1735ec8c83eSAviv Heller 	switch (event) {
1745ec8c83eSAviv Heller 	case NETDEV_REGISTER:
1755ec8c83eSAviv Heller 	case NETDEV_UNREGISTER:
1767fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
177bcf87f1dSMark Bloch 		if (ibdev->rep) {
178bcf87f1dSMark Bloch 			struct mlx5_eswitch *esw = ibdev->mdev->priv.eswitch;
179bcf87f1dSMark Bloch 			struct net_device *rep_ndev;
1807fd8aefbSDaniel Jurgens 
181bcf87f1dSMark Bloch 			rep_ndev = mlx5_ib_get_rep_netdev(esw,
182bcf87f1dSMark Bloch 							  ibdev->rep->vport);
183bcf87f1dSMark Bloch 			if (rep_ndev == ndev)
1847fd8aefbSDaniel Jurgens 				roce->netdev = (event == NETDEV_UNREGISTER) ?
1855ec8c83eSAviv Heller 					NULL : ndev;
18684a6a7a9SParav Pandit 		} else if (ndev->dev.parent == &mdev->pdev->dev) {
187bcf87f1dSMark Bloch 			roce->netdev = (event == NETDEV_UNREGISTER) ?
188bcf87f1dSMark Bloch 				NULL : ndev;
189bcf87f1dSMark Bloch 		}
1907fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
1915ec8c83eSAviv Heller 		break;
1925ec8c83eSAviv Heller 
193fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
1945ec8c83eSAviv Heller 	case NETDEV_UP:
19588621dfeSAviv Heller 	case NETDEV_DOWN: {
1967fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
19788621dfeSAviv Heller 		struct net_device *upper = NULL;
19888621dfeSAviv Heller 
19988621dfeSAviv Heller 		if (lag_ndev) {
20088621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
20188621dfeSAviv Heller 			dev_put(lag_ndev);
20288621dfeSAviv Heller 		}
20388621dfeSAviv Heller 
2047fd8aefbSDaniel Jurgens 		if ((upper == ndev || (!upper && ndev == roce->netdev))
20588621dfeSAviv Heller 		    && ibdev->ib_active) {
206626bc02dSBart Van Assche 			struct ib_event ibev = { };
207fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2085ec8c83eSAviv Heller 
2097fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2107fd8aefbSDaniel Jurgens 					   &port_state))
2117fd8aefbSDaniel Jurgens 				goto done;
212fd65f1b8SMoni Shoua 
2137fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2147fd8aefbSDaniel Jurgens 				goto done;
215fd65f1b8SMoni Shoua 
2167fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2175ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
218fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
219fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
220fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
221fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
222fd65f1b8SMoni Shoua 			else
2237fd8aefbSDaniel Jurgens 				goto done;
224fd65f1b8SMoni Shoua 
2257fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2265ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2275ec8c83eSAviv Heller 		}
2285ec8c83eSAviv Heller 		break;
22988621dfeSAviv Heller 	}
2305ec8c83eSAviv Heller 
2315ec8c83eSAviv Heller 	default:
2325ec8c83eSAviv Heller 		break;
2335ec8c83eSAviv Heller 	}
2347fd8aefbSDaniel Jurgens done:
23532f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
236fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
237fc24fc5eSAchiad Shochat }
238fc24fc5eSAchiad Shochat 
239fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
240fc24fc5eSAchiad Shochat 					     u8 port_num)
241fc24fc5eSAchiad Shochat {
242fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
243fc24fc5eSAchiad Shochat 	struct net_device *ndev;
24432f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
245fc24fc5eSAchiad Shochat 
24632f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
24732f69e4bSDaniel Jurgens 	if (!mdev)
24832f69e4bSDaniel Jurgens 		return NULL;
24932f69e4bSDaniel Jurgens 
25032f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
25188621dfeSAviv Heller 	if (ndev)
25232f69e4bSDaniel Jurgens 		goto out;
25388621dfeSAviv Heller 
254fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
255fc24fc5eSAchiad Shochat 	 */
2567fd8aefbSDaniel Jurgens 	read_lock(&ibdev->roce[port_num - 1].netdev_lock);
2577fd8aefbSDaniel Jurgens 	ndev = ibdev->roce[port_num - 1].netdev;
258fc24fc5eSAchiad Shochat 	if (ndev)
259fc24fc5eSAchiad Shochat 		dev_hold(ndev);
2607fd8aefbSDaniel Jurgens 	read_unlock(&ibdev->roce[port_num - 1].netdev_lock);
261fc24fc5eSAchiad Shochat 
26232f69e4bSDaniel Jurgens out:
26332f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
264fc24fc5eSAchiad Shochat 	return ndev;
265fc24fc5eSAchiad Shochat }
266fc24fc5eSAchiad Shochat 
26732f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
26832f69e4bSDaniel Jurgens 						   u8 ib_port_num,
26932f69e4bSDaniel Jurgens 						   u8 *native_port_num)
27032f69e4bSDaniel Jurgens {
27132f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
27232f69e4bSDaniel Jurgens 							  ib_port_num);
27332f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
27432f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
27532f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
27632f69e4bSDaniel Jurgens 
277210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
278210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
279210b1f78SMark Bloch 		if (native_port_num)
280210b1f78SMark Bloch 			*native_port_num = ib_port_num;
281210b1f78SMark Bloch 		return ibdev->mdev;
282210b1f78SMark Bloch 	}
283210b1f78SMark Bloch 
28432f69e4bSDaniel Jurgens 	if (native_port_num)
28532f69e4bSDaniel Jurgens 		*native_port_num = 1;
28632f69e4bSDaniel Jurgens 
28732f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
28832f69e4bSDaniel Jurgens 	if (!port)
28932f69e4bSDaniel Jurgens 		return NULL;
29032f69e4bSDaniel Jurgens 
29132f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
29232f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
29332f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
29432f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
29532f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
29632f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
29732f69e4bSDaniel Jurgens 		 */
29832f69e4bSDaniel Jurgens 		if (!mpi->is_master)
29932f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
30032f69e4bSDaniel Jurgens 	}
30132f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
30232f69e4bSDaniel Jurgens 
30332f69e4bSDaniel Jurgens 	return mdev;
30432f69e4bSDaniel Jurgens }
30532f69e4bSDaniel Jurgens 
30632f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
30732f69e4bSDaniel Jurgens {
30832f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
30932f69e4bSDaniel Jurgens 							  port_num);
31032f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
31132f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
31232f69e4bSDaniel Jurgens 
31332f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
31432f69e4bSDaniel Jurgens 		return;
31532f69e4bSDaniel Jurgens 
31632f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
31732f69e4bSDaniel Jurgens 
31832f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
31932f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
32032f69e4bSDaniel Jurgens 	if (mpi->is_master)
32132f69e4bSDaniel Jurgens 		goto out;
32232f69e4bSDaniel Jurgens 
32332f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
32432f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
32532f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
32632f69e4bSDaniel Jurgens out:
32732f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
32832f69e4bSDaniel Jurgens }
32932f69e4bSDaniel Jurgens 
330f1b65df5SNoa Osherovich static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
331f1b65df5SNoa Osherovich 				    u8 *active_width)
332f1b65df5SNoa Osherovich {
333f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
334f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
335f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
336f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
337f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
338f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
339f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
340f1b65df5SNoa Osherovich 		break;
341f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
342f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
343f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
344f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
345f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
346f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
347f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
348f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
349f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
350f1b65df5SNoa Osherovich 		break;
351f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
352f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
353f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
354f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
355f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
356f1b65df5SNoa Osherovich 		break;
357f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
358f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
359f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
360f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
361f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
362f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
363f1b65df5SNoa Osherovich 		break;
364f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
365f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
366f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
367f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
368f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
369f1b65df5SNoa Osherovich 		break;
370f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
371f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
372f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
373f1b65df5SNoa Osherovich 		break;
374f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
375f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
376f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
377f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
378f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
379f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
380f1b65df5SNoa Osherovich 		break;
381f1b65df5SNoa Osherovich 	default:
382f1b65df5SNoa Osherovich 		return -EINVAL;
383f1b65df5SNoa Osherovich 	}
384f1b65df5SNoa Osherovich 
385f1b65df5SNoa Osherovich 	return 0;
386f1b65df5SNoa Osherovich }
387f1b65df5SNoa Osherovich 
388095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
3893f89a643SAchiad Shochat 				struct ib_port_attr *props)
3903f89a643SAchiad Shochat {
3913f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
392da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
39388621dfeSAviv Heller 	struct net_device *ndev, *upper;
3943f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
395b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
396c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
397f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
398b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
399095b0927SIlan Tayari 	int err;
4003f89a643SAchiad Shochat 
401b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
402b3cbd6f0SDaniel Jurgens 	if (!mdev) {
403b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
404b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
405b3cbd6f0SDaniel Jurgens 		 */
406b3cbd6f0SDaniel Jurgens 		put_mdev = false;
407b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
408b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
409b3cbd6f0SDaniel Jurgens 		port_num = 1;
410b3cbd6f0SDaniel Jurgens 	}
411b3cbd6f0SDaniel Jurgens 
412f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
413f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
41450f22fd8SNoa Osherovich 	 */
415b3cbd6f0SDaniel Jurgens 	err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper,
416b3cbd6f0SDaniel Jurgens 					     mdev_port_num);
417095b0927SIlan Tayari 	if (err)
418b3cbd6f0SDaniel Jurgens 		goto out;
419f1b65df5SNoa Osherovich 
4207672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
4217672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
4227672ed33SHonggang Li 
423f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
424f1b65df5SNoa Osherovich 				 &props->active_width);
4253f89a643SAchiad Shochat 
4263f89a643SAchiad Shochat 	props->port_cap_flags |= IB_PORT_CM_SUP;
4272f944c0fSJason Gunthorpe 	props->ip_gids = true;
4283f89a643SAchiad Shochat 
4293f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
4303f89a643SAchiad Shochat 						roce_address_table_size);
4313f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
4323f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
4333f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
4343f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
4353f89a643SAchiad Shochat 	props->phys_state       = 3;
4363f89a643SAchiad Shochat 
437b3cbd6f0SDaniel Jurgens 	mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
438c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
4393f89a643SAchiad Shochat 
440b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
441b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
442b3cbd6f0SDaniel Jurgens 		goto out;
443b3cbd6f0SDaniel Jurgens 
4443f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
4453f89a643SAchiad Shochat 	if (!ndev)
446b3cbd6f0SDaniel Jurgens 		goto out;
4473f89a643SAchiad Shochat 
448*7c34ec19SAviv Heller 	if (dev->lag_active) {
44988621dfeSAviv Heller 		rcu_read_lock();
45088621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
45188621dfeSAviv Heller 		if (upper) {
45288621dfeSAviv Heller 			dev_put(ndev);
45388621dfeSAviv Heller 			ndev = upper;
45488621dfeSAviv Heller 			dev_hold(ndev);
45588621dfeSAviv Heller 		}
45688621dfeSAviv Heller 		rcu_read_unlock();
45788621dfeSAviv Heller 	}
45888621dfeSAviv Heller 
4593f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
4603f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
4613f89a643SAchiad Shochat 		props->phys_state = 5;
4623f89a643SAchiad Shochat 	}
4633f89a643SAchiad Shochat 
4643f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
4653f89a643SAchiad Shochat 
4663f89a643SAchiad Shochat 	dev_put(ndev);
4673f89a643SAchiad Shochat 
4683f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
469b3cbd6f0SDaniel Jurgens out:
470b3cbd6f0SDaniel Jurgens 	if (put_mdev)
471b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
472b3cbd6f0SDaniel Jurgens 	return err;
4733f89a643SAchiad Shochat }
4743f89a643SAchiad Shochat 
475095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
476095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
477095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
4783cca2606SAchiad Shochat {
479095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
480095b0927SIlan Tayari 	u8 roce_version = 0;
481095b0927SIlan Tayari 	u8 roce_l3_type = 0;
482095b0927SIlan Tayari 	bool vlan = false;
483095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
484095b0927SIlan Tayari 	u16 vlan_id = 0;
4853cca2606SAchiad Shochat 
486095b0927SIlan Tayari 	if (gid) {
487095b0927SIlan Tayari 		gid_type = attr->gid_type;
488095b0927SIlan Tayari 		ether_addr_copy(mac, attr->ndev->dev_addr);
4893cca2606SAchiad Shochat 
4903cca2606SAchiad Shochat 		if (is_vlan_dev(attr->ndev)) {
491095b0927SIlan Tayari 			vlan = true;
492095b0927SIlan Tayari 			vlan_id = vlan_dev_vlan_id(attr->ndev);
493095b0927SIlan Tayari 		}
4943cca2606SAchiad Shochat 	}
4953cca2606SAchiad Shochat 
496095b0927SIlan Tayari 	switch (gid_type) {
4973cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
498095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
4993cca2606SAchiad Shochat 		break;
5003cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
501095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
502095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
503095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
504095b0927SIlan Tayari 		else
505095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
5063cca2606SAchiad Shochat 		break;
5073cca2606SAchiad Shochat 
5083cca2606SAchiad Shochat 	default:
509095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
5103cca2606SAchiad Shochat 	}
5113cca2606SAchiad Shochat 
512095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
513095b0927SIlan Tayari 				      roce_l3_type, gid->raw, mac, vlan,
514cfe4e37fSDaniel Jurgens 				      vlan_id, port_num);
5153cca2606SAchiad Shochat }
5163cca2606SAchiad Shochat 
517f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
5183cca2606SAchiad Shochat 			   __always_unused void **context)
5193cca2606SAchiad Shochat {
520414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
521f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
5223cca2606SAchiad Shochat }
5233cca2606SAchiad Shochat 
524414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
525414448d2SParav Pandit 			   __always_unused void **context)
5263cca2606SAchiad Shochat {
527414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
528414448d2SParav Pandit 			     attr->index, NULL, NULL);
5293cca2606SAchiad Shochat }
5303cca2606SAchiad Shochat 
53147ec3866SParav Pandit __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
53247ec3866SParav Pandit 			       const struct ib_gid_attr *attr)
5332811ba51SAchiad Shochat {
53447ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
5352811ba51SAchiad Shochat 		return 0;
5362811ba51SAchiad Shochat 
5372811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
5382811ba51SAchiad Shochat }
5392811ba51SAchiad Shochat 
5401b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
5411b5daf11SMajd Dibbiny {
5427fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
543d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
5447fae6655SNoa Osherovich 	return 0;
5451b5daf11SMajd Dibbiny }
5461b5daf11SMajd Dibbiny 
5471b5daf11SMajd Dibbiny enum {
5481b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
5491b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
5501b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
5511b5daf11SMajd Dibbiny };
5521b5daf11SMajd Dibbiny 
5531b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
5541b5daf11SMajd Dibbiny {
5551b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
5561b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
5571b5daf11SMajd Dibbiny 
558ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
5591b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
5601b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
5611b5daf11SMajd Dibbiny 
5621b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
5631b5daf11SMajd Dibbiny }
5641b5daf11SMajd Dibbiny 
565da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
566776a3906SMoni Shoua 			    u8 atomic_size_qp,
567da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
568da7525d2SEran Ben Elisha {
569da7525d2SEran Ben Elisha 	u8 tmp;
570da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
571da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
572bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
573da7525d2SEran Ben Elisha 
574da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
575da7525d2SEran Ben Elisha 	 * of host endianness respond
576da7525d2SEran Ben Elisha 	 */
577da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
578da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
579da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
580da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
581da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
582da7525d2SEran Ben Elisha 	} else {
583da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
584da7525d2SEran Ben Elisha 	}
585da7525d2SEran Ben Elisha }
586da7525d2SEran Ben Elisha 
587776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
588776a3906SMoni Shoua 			       struct ib_device_attr *props)
589776a3906SMoni Shoua {
590776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
591776a3906SMoni Shoua 
592776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
593776a3906SMoni Shoua }
594776a3906SMoni Shoua 
595776a3906SMoni Shoua static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
596776a3906SMoni Shoua 			       struct ib_device_attr *props)
597776a3906SMoni Shoua {
598776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
599776a3906SMoni Shoua 
600776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
601776a3906SMoni Shoua }
602776a3906SMoni Shoua 
603776a3906SMoni Shoua bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
604776a3906SMoni Shoua {
605776a3906SMoni Shoua 	struct ib_device_attr props = {};
606776a3906SMoni Shoua 
607776a3906SMoni Shoua 	get_atomic_caps_dc(dev, &props);
608776a3906SMoni Shoua 	return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
609776a3906SMoni Shoua }
6101b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
6111b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
6121b5daf11SMajd Dibbiny {
6131b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6141b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6151b5daf11SMajd Dibbiny 	u64 tmp;
6161b5daf11SMajd Dibbiny 	int err;
6171b5daf11SMajd Dibbiny 
6181b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6191b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6201b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
6211b5daf11SMajd Dibbiny 							    sys_image_guid);
6221b5daf11SMajd Dibbiny 
6231b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6241b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
6253f89a643SAchiad Shochat 		break;
6263f89a643SAchiad Shochat 
6273f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6283f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
6293f89a643SAchiad Shochat 		break;
6301b5daf11SMajd Dibbiny 
6311b5daf11SMajd Dibbiny 	default:
6321b5daf11SMajd Dibbiny 		return -EINVAL;
6331b5daf11SMajd Dibbiny 	}
6343f89a643SAchiad Shochat 
6353f89a643SAchiad Shochat 	if (!err)
6363f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
6373f89a643SAchiad Shochat 
6383f89a643SAchiad Shochat 	return err;
6393f89a643SAchiad Shochat 
6401b5daf11SMajd Dibbiny }
6411b5daf11SMajd Dibbiny 
6421b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
6431b5daf11SMajd Dibbiny 				u16 *max_pkeys)
6441b5daf11SMajd Dibbiny {
6451b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6461b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6471b5daf11SMajd Dibbiny 
6481b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6491b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6501b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
6511b5daf11SMajd Dibbiny 
6521b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6531b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6541b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
6551b5daf11SMajd Dibbiny 						pkey_table_size));
6561b5daf11SMajd Dibbiny 		return 0;
6571b5daf11SMajd Dibbiny 
6581b5daf11SMajd Dibbiny 	default:
6591b5daf11SMajd Dibbiny 		return -EINVAL;
6601b5daf11SMajd Dibbiny 	}
6611b5daf11SMajd Dibbiny }
6621b5daf11SMajd Dibbiny 
6631b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
6641b5daf11SMajd Dibbiny 				u32 *vendor_id)
6651b5daf11SMajd Dibbiny {
6661b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6671b5daf11SMajd Dibbiny 
6681b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6691b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6701b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
6711b5daf11SMajd Dibbiny 
6721b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6731b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6741b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
6751b5daf11SMajd Dibbiny 
6761b5daf11SMajd Dibbiny 	default:
6771b5daf11SMajd Dibbiny 		return -EINVAL;
6781b5daf11SMajd Dibbiny 	}
6791b5daf11SMajd Dibbiny }
6801b5daf11SMajd Dibbiny 
6811b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
6821b5daf11SMajd Dibbiny 				__be64 *node_guid)
6831b5daf11SMajd Dibbiny {
6841b5daf11SMajd Dibbiny 	u64 tmp;
6851b5daf11SMajd Dibbiny 	int err;
6861b5daf11SMajd Dibbiny 
6871b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
6881b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6891b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
6901b5daf11SMajd Dibbiny 
6911b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6921b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
6933f89a643SAchiad Shochat 		break;
6943f89a643SAchiad Shochat 
6953f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6963f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
6973f89a643SAchiad Shochat 		break;
6981b5daf11SMajd Dibbiny 
6991b5daf11SMajd Dibbiny 	default:
7001b5daf11SMajd Dibbiny 		return -EINVAL;
7011b5daf11SMajd Dibbiny 	}
7023f89a643SAchiad Shochat 
7033f89a643SAchiad Shochat 	if (!err)
7043f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7053f89a643SAchiad Shochat 
7063f89a643SAchiad Shochat 	return err;
7071b5daf11SMajd Dibbiny }
7081b5daf11SMajd Dibbiny 
7091b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
710bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
7111b5daf11SMajd Dibbiny };
7121b5daf11SMajd Dibbiny 
7131b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
7141b5daf11SMajd Dibbiny {
7151b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
7161b5daf11SMajd Dibbiny 
7171b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
7181b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
7191b5daf11SMajd Dibbiny 
7201b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
7211b5daf11SMajd Dibbiny 
7221b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
7231b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
7241b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
7251b5daf11SMajd Dibbiny }
7261b5daf11SMajd Dibbiny 
727e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
7282528e33eSMatan Barak 				struct ib_device_attr *props,
7292528e33eSMatan Barak 				struct ib_udata *uhw)
730e126ba97SEli Cohen {
731e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
732938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
733e126ba97SEli Cohen 	int err = -ENOMEM;
734288c01b7SEli Cohen 	int max_sq_desc;
735e126ba97SEli Cohen 	int max_rq_sg;
736e126ba97SEli Cohen 	int max_sq_sg;
737e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
73885c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
739402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
740402ca536SBodong Wang 	size_t resp_len;
741402ca536SBodong Wang 	u64 max_tso;
742e126ba97SEli Cohen 
743402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
744402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
745402ca536SBodong Wang 		return -EINVAL;
746402ca536SBodong Wang 	else
747402ca536SBodong Wang 		resp.response_length = resp_len;
748402ca536SBodong Wang 
749402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
7502528e33eSMatan Barak 		return -EINVAL;
7512528e33eSMatan Barak 
752e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
7531b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
7541b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
7551b5daf11SMajd Dibbiny 	if (err)
7561b5daf11SMajd Dibbiny 		return err;
7571b5daf11SMajd Dibbiny 
7581b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
7591b5daf11SMajd Dibbiny 	if (err)
7601b5daf11SMajd Dibbiny 		return err;
7611b5daf11SMajd Dibbiny 
7621b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
7631b5daf11SMajd Dibbiny 	if (err)
7641b5daf11SMajd Dibbiny 		return err;
765e126ba97SEli Cohen 
7669603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
7679603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
7689603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
769e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
770e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
771e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
7721a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
773938fe83cSSaeed Mahameed 
774938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
775e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
776938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
777e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
778938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
779e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
780938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
781e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
782d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
783d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
784d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
785d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
786b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
787b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
788d2370e0aSMatan Barak 	}
789e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
790938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
7912dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
7922dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
7932dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
7942dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
7952dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
7962dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
7972dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
7982dea9094SSagi Grimberg 	}
799938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
800f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
801e126ba97SEli Cohen 
80285c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
803e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
804e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
80588115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
806e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
807e8161334SNoa Osherovich 		}
808e8161334SNoa Osherovich 
809e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
810e8161334SNoa Osherovich 			props->raw_packet_caps |=
811e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
81288115fe7SBodong Wang 
813402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
814402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
815402ca536SBodong Wang 			if (max_tso) {
816402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
817402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
818402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
819402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
820402ca536SBodong Wang 			}
821402ca536SBodong Wang 		}
82231f69a82SYishai Hadas 
82331f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
82431f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
82531f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
82631f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
82731f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
82831f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
82931f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
83031f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
83131f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
83231f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
83331f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
8344e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
8354e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
8362d93fc85SMatan Barak 			if (mlx5_accel_ipsec_device_caps(dev->mdev) &
8372d93fc85SMatan Barak 			    MLX5_ACCEL_IPSEC_CAP_DEVICE)
8382d93fc85SMatan Barak 				resp.rss_caps.rx_hash_fields_mask |=
8392d93fc85SMatan Barak 					MLX5_RX_HASH_IPSEC_SPI;
84031f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
84131f69a82SYishai Hadas 		}
84231f69a82SYishai Hadas 	} else {
84331f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
84431f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
84531f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
84631f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
847402ca536SBodong Wang 	}
848402ca536SBodong Wang 
849f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
850f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
851f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
852f0313965SErez Shitrit 	}
853f0313965SErez Shitrit 
85403404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
85585c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
85685c7c014SDaniel Jurgens 	    raw_support)
85703404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
85803404e8aSMaor Gottlieb 
8591d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
8601d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
8611d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
8621d54f890SYishai Hadas 
863cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
86485c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
86585c7c014SDaniel Jurgens 	    raw_support) {
866e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
867cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
868e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
869e8161334SNoa Osherovich 	}
870cff5a0f3SMajd Dibbiny 
87124da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
87224da0016SAriel Levkovich 		props->max_dm_size =
87324da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
87424da0016SAriel Levkovich 	}
87524da0016SAriel Levkovich 
876da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
877da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
878da6d6ba3SMaor Gottlieb 
879b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
880b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
881b1383aa6SNoa Osherovich 
8821b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
8831b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
884e126ba97SEli Cohen 
885e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
886e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
887938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
888938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
889938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
890938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
891288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
892288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
893288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
894e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
89533023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
89633023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
897986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
898938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
8999f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
900938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
901938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
902938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
903938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
904938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
905938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
906938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
907e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
908e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
909911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
910911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
911776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
91281bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
913938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
914938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
915e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
916e126ba97SEli Cohen 					   props->max_mcast_grp;
917e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
91886695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
9197c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
9207c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
921e126ba97SEli Cohen 
9228cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
923938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
9248cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
9258cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
9268cdd312cSHaggai Eran #endif
9278cdd312cSHaggai Eran 
928051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
929051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
930051f2630SLeon Romanovsky 
931eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
932eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
933eff901d3SEli Cohen 
93431f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
93585c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
93631f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
93731f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
93831f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
93931f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
94031f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
94131f69a82SYishai Hadas 		props->max_wq_type_rq =
94231f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
94331f69a82SYishai Hadas 	}
94431f69a82SYishai Hadas 
945eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
94678b1beb0SLeon Romanovsky 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
94778b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
948eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
94978b1beb0SLeon Romanovsky 		props->tm_caps.flags = IB_TM_CAP_RC;
95078b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
951eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
95278b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
953eb761894SArtemy Kovalyov 	}
954eb761894SArtemy Kovalyov 
95587ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
95687ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
95787ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
95887ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
95987ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
96087ab3f52SYonatan Cohen 	}
96187ab3f52SYonatan Cohen 
9627e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
963572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
964572f46bfSYonatan Cohen 
965572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
9667e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
967572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
968572f46bfSYonatan Cohen 					     cqe_compression_max_num);
969572f46bfSYonatan Cohen 
9707e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
9717e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
9727e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
9736f1006a4SYonatan Cohen 
9746f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
9756f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
9766f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
977572f46bfSYonatan Cohen 		}
9787e43a2a5SBodong Wang 	}
9797e43a2a5SBodong Wang 
98085c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
98185c7c014SDaniel Jurgens 	    raw_support) {
982d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
983d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
984d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
985d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
986d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
987d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
988d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
989d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
99061147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
99161147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
99261147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
99361147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
994d949167dSBodong Wang 		}
995d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
996d949167dSBodong Wang 	}
997d949167dSBodong Wang 
9989f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
9999f885201SLeon Romanovsky 			uhw->outlen)) {
1000795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
10019f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1002795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1003050da902SBodong Wang 
1004050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1005050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1006050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1007050da902SBodong Wang 
10089f885201SLeon Romanovsky 		resp.response_length +=
10099f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
10109f885201SLeon Romanovsky 	}
10119f885201SLeon Romanovsky 
1012de57f2adSGuy Levi 	if (field_avail(typeof(resp), flags, uhw->outlen)) {
1013de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
10147a0c8f42SGuy Levi 
1015de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1016de57f2adSGuy Levi 			resp.flags |=
1017de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
10187a0c8f42SGuy Levi 
10197a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
10207a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
1021de57f2adSGuy Levi 	}
10229f885201SLeon Romanovsky 
102396dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
102496dc3fc5SNoa Osherovich 			uhw->outlen)) {
102596dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
102696dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
102796dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
102896dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
102996dc3fc5SNoa Osherovich 
103096dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
103196dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
103296dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
103396dc3fc5SNoa Osherovich 
103496dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
103596dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
103696dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
103796dc3fc5SNoa Osherovich 
103896dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
103996dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
104096dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
104196dc3fc5SNoa Osherovich 		}
104296dc3fc5SNoa Osherovich 	}
104396dc3fc5SNoa Osherovich 
104485c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
104585c7c014SDaniel Jurgens 	    raw_support) {
1046b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1047b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1048b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1049b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1050b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1051b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1052b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1053b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1054b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1055b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1056b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1057b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1058b4f34597SNoa Osherovich 		}
1059b4f34597SNoa Osherovich 	}
1060b4f34597SNoa Osherovich 
1061f95ef6cbSMaor Gottlieb 	if (field_avail(typeof(resp), tunnel_offloads_caps,
1062f95ef6cbSMaor Gottlieb 			uhw->outlen)) {
1063f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1064f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1065f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1066f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1067f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1068f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1069f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1070f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1071f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1072f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
1073e818e255SAriel Levkovich 		if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1074e818e255SAriel Levkovich 		    MLX5_FLEX_PROTO_CW_MPLS_GRE)
1075e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1076e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1077e818e255SAriel Levkovich 		if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1078e818e255SAriel Levkovich 		    MLX5_FLEX_PROTO_CW_MPLS_UDP)
1079e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1080e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1081f95ef6cbSMaor Gottlieb 	}
1082f95ef6cbSMaor Gottlieb 
1083402ca536SBodong Wang 	if (uhw->outlen) {
1084402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1085402ca536SBodong Wang 
1086402ca536SBodong Wang 		if (err)
1087402ca536SBodong Wang 			return err;
1088402ca536SBodong Wang 	}
1089402ca536SBodong Wang 
10901b5daf11SMajd Dibbiny 	return 0;
10911b5daf11SMajd Dibbiny }
1092e126ba97SEli Cohen 
10931b5daf11SMajd Dibbiny enum mlx5_ib_width {
10941b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
10951b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
10961b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
10971b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
10981b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
10991b5daf11SMajd Dibbiny };
11001b5daf11SMajd Dibbiny 
1101db7a691aSMichael Guralnik static void translate_active_width(struct ib_device *ibdev, u8 active_width,
11021b5daf11SMajd Dibbiny 				  u8 *ib_width)
11031b5daf11SMajd Dibbiny {
11041b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11051b5daf11SMajd Dibbiny 
1106db7a691aSMichael Guralnik 	if (active_width & MLX5_IB_WIDTH_1X)
11071b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1108db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_4X)
11091b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1110db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_8X)
11111b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1112db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_12X)
11131b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1114db7a691aSMichael Guralnik 	else {
1115db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
11161b5daf11SMajd Dibbiny 			    (int)active_width);
1117db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
11181b5daf11SMajd Dibbiny 	}
11191b5daf11SMajd Dibbiny 
1120db7a691aSMichael Guralnik 	return;
11211b5daf11SMajd Dibbiny }
11221b5daf11SMajd Dibbiny 
11231b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
11241b5daf11SMajd Dibbiny {
11251b5daf11SMajd Dibbiny 	switch (mtu) {
11261b5daf11SMajd Dibbiny 	case 256: return 1;
11271b5daf11SMajd Dibbiny 	case 512: return 2;
11281b5daf11SMajd Dibbiny 	case 1024: return 3;
11291b5daf11SMajd Dibbiny 	case 2048: return 4;
11301b5daf11SMajd Dibbiny 	case 4096: return 5;
11311b5daf11SMajd Dibbiny 	default:
11321b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
11331b5daf11SMajd Dibbiny 		return -1;
11341b5daf11SMajd Dibbiny 	}
11351b5daf11SMajd Dibbiny }
11361b5daf11SMajd Dibbiny 
11371b5daf11SMajd Dibbiny enum ib_max_vl_num {
11381b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
11391b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
11401b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
11411b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
11421b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
11431b5daf11SMajd Dibbiny };
11441b5daf11SMajd Dibbiny 
11451b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
11461b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
11471b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
11481b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
11491b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
11501b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
11511b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
11521b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
11531b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
11541b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
11551b5daf11SMajd Dibbiny };
11561b5daf11SMajd Dibbiny 
11571b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
11581b5daf11SMajd Dibbiny 				u8 *max_vl_num)
11591b5daf11SMajd Dibbiny {
11601b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
11611b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
11621b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
11631b5daf11SMajd Dibbiny 		break;
11641b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
11651b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
11661b5daf11SMajd Dibbiny 		break;
11671b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
11681b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
11691b5daf11SMajd Dibbiny 		break;
11701b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
11711b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
11721b5daf11SMajd Dibbiny 		break;
11731b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
11741b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
11751b5daf11SMajd Dibbiny 		break;
11761b5daf11SMajd Dibbiny 
11771b5daf11SMajd Dibbiny 	default:
11781b5daf11SMajd Dibbiny 		return -EINVAL;
11791b5daf11SMajd Dibbiny 	}
11801b5daf11SMajd Dibbiny 
11811b5daf11SMajd Dibbiny 	return 0;
11821b5daf11SMajd Dibbiny }
11831b5daf11SMajd Dibbiny 
11841b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
11851b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
11861b5daf11SMajd Dibbiny {
11871b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11881b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
11891b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1190046339eaSSaeed Mahameed 	u16 max_mtu;
1191046339eaSSaeed Mahameed 	u16 oper_mtu;
11921b5daf11SMajd Dibbiny 	int err;
11931b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
11941b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
11951b5daf11SMajd Dibbiny 
11961b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
11971b5daf11SMajd Dibbiny 	if (!rep) {
11981b5daf11SMajd Dibbiny 		err = -ENOMEM;
11991b5daf11SMajd Dibbiny 		goto out;
12001b5daf11SMajd Dibbiny 	}
12011b5daf11SMajd Dibbiny 
1202c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
12031b5daf11SMajd Dibbiny 
12041b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
12051b5daf11SMajd Dibbiny 	if (err)
12061b5daf11SMajd Dibbiny 		goto out;
12071b5daf11SMajd Dibbiny 
12081b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
12091b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
12101b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
12111b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
12121b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
12131b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
12141b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
12151b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
12161b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
12171b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
12181b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
12191b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
12201b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
12211b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
12221b5daf11SMajd Dibbiny 
12231b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
12241b5daf11SMajd Dibbiny 	if (err)
12251b5daf11SMajd Dibbiny 		goto out;
12261b5daf11SMajd Dibbiny 
1227db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1228db7a691aSMichael Guralnik 
1229d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
12301b5daf11SMajd Dibbiny 	if (err)
12311b5daf11SMajd Dibbiny 		goto out;
12321b5daf11SMajd Dibbiny 
1233facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
12341b5daf11SMajd Dibbiny 
12351b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
12361b5daf11SMajd Dibbiny 
1237facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
12381b5daf11SMajd Dibbiny 
12391b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
12401b5daf11SMajd Dibbiny 
12411b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
12421b5daf11SMajd Dibbiny 	if (err)
12431b5daf11SMajd Dibbiny 		goto out;
12441b5daf11SMajd Dibbiny 
12451b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
12461b5daf11SMajd Dibbiny 				   &props->max_vl_num);
12471b5daf11SMajd Dibbiny out:
12481b5daf11SMajd Dibbiny 	kfree(rep);
1249e126ba97SEli Cohen 	return err;
1250e126ba97SEli Cohen }
1251e126ba97SEli Cohen 
1252e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1253e126ba97SEli Cohen 		       struct ib_port_attr *props)
1254e126ba97SEli Cohen {
1255095b0927SIlan Tayari 	unsigned int count;
1256095b0927SIlan Tayari 	int ret;
1257095b0927SIlan Tayari 
12581b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
12591b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1260095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1261095b0927SIlan Tayari 		break;
1262e126ba97SEli Cohen 
12631b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1264095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1265095b0927SIlan Tayari 		break;
12661b5daf11SMajd Dibbiny 
12673f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1268095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1269095b0927SIlan Tayari 		break;
12703f89a643SAchiad Shochat 
12711b5daf11SMajd Dibbiny 	default:
1272095b0927SIlan Tayari 		ret = -EINVAL;
1273e126ba97SEli Cohen 	}
1274095b0927SIlan Tayari 
1275095b0927SIlan Tayari 	if (!ret && props) {
1276b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1277b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1278b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1279b3cbd6f0SDaniel Jurgens 
1280b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1281b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1282b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1283b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1284b3cbd6f0SDaniel Jurgens 			 */
1285b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1286b3cbd6f0SDaniel Jurgens 			port = 1;
1287b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1288b3cbd6f0SDaniel Jurgens 		}
1289b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1290b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1291b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1292095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1293095b0927SIlan Tayari 	}
1294095b0927SIlan Tayari 	return ret;
1295e126ba97SEli Cohen }
1296e126ba97SEli Cohen 
12978e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
12988e6efa3aSMark Bloch 				  struct ib_port_attr *props)
12998e6efa3aSMark Bloch {
13008e6efa3aSMark Bloch 	int ret;
13018e6efa3aSMark Bloch 
13028e6efa3aSMark Bloch 	/* Only link layer == ethernet is valid for representors */
13038e6efa3aSMark Bloch 	ret = mlx5_query_port_roce(ibdev, port, props);
13048e6efa3aSMark Bloch 	if (ret || !props)
13058e6efa3aSMark Bloch 		return ret;
13068e6efa3aSMark Bloch 
13078e6efa3aSMark Bloch 	/* We don't support GIDS */
13088e6efa3aSMark Bloch 	props->gid_tbl_len = 0;
13098e6efa3aSMark Bloch 
13108e6efa3aSMark Bloch 	return ret;
13118e6efa3aSMark Bloch }
13128e6efa3aSMark Bloch 
1313e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1314e126ba97SEli Cohen 			     union ib_gid *gid)
1315e126ba97SEli Cohen {
13161b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
13171b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1318e126ba97SEli Cohen 
13191b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13201b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
13211b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1322e126ba97SEli Cohen 
13231b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
13241b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1325e126ba97SEli Cohen 
13261b5daf11SMajd Dibbiny 	default:
13271b5daf11SMajd Dibbiny 		return -EINVAL;
13281b5daf11SMajd Dibbiny 	}
1329e126ba97SEli Cohen 
1330e126ba97SEli Cohen }
1331e126ba97SEli Cohen 
1332b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1333b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1334b3cbd6f0SDaniel Jurgens {
1335b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1336b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1337b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1338b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1339b3cbd6f0SDaniel Jurgens 	int err;
1340b3cbd6f0SDaniel Jurgens 
1341b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1342b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1343b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1344b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1345b3cbd6f0SDaniel Jurgens 		 */
1346b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1347b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1348b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1349b3cbd6f0SDaniel Jurgens 	}
1350b3cbd6f0SDaniel Jurgens 
1351b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1352b3cbd6f0SDaniel Jurgens 					index, pkey);
1353b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1354b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1355b3cbd6f0SDaniel Jurgens 
1356b3cbd6f0SDaniel Jurgens 	return err;
1357b3cbd6f0SDaniel Jurgens }
1358b3cbd6f0SDaniel Jurgens 
1359e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1360e126ba97SEli Cohen 			      u16 *pkey)
1361e126ba97SEli Cohen {
13621b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13631b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
13641b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1365e126ba97SEli Cohen 
13661b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
13671b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1368b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
13691b5daf11SMajd Dibbiny 	default:
13701b5daf11SMajd Dibbiny 		return -EINVAL;
1371e126ba97SEli Cohen 	}
13721b5daf11SMajd Dibbiny }
1373e126ba97SEli Cohen 
1374e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1375e126ba97SEli Cohen 				 struct ib_device_modify *props)
1376e126ba97SEli Cohen {
1377e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1378e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1379e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1380e126ba97SEli Cohen 	int err;
1381e126ba97SEli Cohen 
1382e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1383e126ba97SEli Cohen 		return -EOPNOTSUPP;
1384e126ba97SEli Cohen 
1385e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1386e126ba97SEli Cohen 		return 0;
1387e126ba97SEli Cohen 
1388e126ba97SEli Cohen 	/*
1389e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1390e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1391e126ba97SEli Cohen 	 */
1392bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
13939603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1394e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1395e126ba97SEli Cohen 	if (err)
1396e126ba97SEli Cohen 		return err;
1397e126ba97SEli Cohen 
1398bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1399e126ba97SEli Cohen 
1400e126ba97SEli Cohen 	return err;
1401e126ba97SEli Cohen }
1402e126ba97SEli Cohen 
1403cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1404cdbe33d0SEli Cohen 				u32 value)
1405cdbe33d0SEli Cohen {
1406cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1407b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1408b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1409cdbe33d0SEli Cohen 	int err;
1410cdbe33d0SEli Cohen 
1411b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1412b3cbd6f0SDaniel Jurgens 	if (!mdev)
1413b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1414b3cbd6f0SDaniel Jurgens 
1415b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1416cdbe33d0SEli Cohen 	if (err)
1417b3cbd6f0SDaniel Jurgens 		goto out;
1418cdbe33d0SEli Cohen 
1419cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1420cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1421cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1422b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1423b3cbd6f0SDaniel Jurgens 		goto out;
1424cdbe33d0SEli Cohen 	}
1425cdbe33d0SEli Cohen 
1426cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1427cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1428b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1429b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1430b3cbd6f0SDaniel Jurgens 
1431b3cbd6f0SDaniel Jurgens out:
1432b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1433cdbe33d0SEli Cohen 
1434cdbe33d0SEli Cohen 	return err;
1435cdbe33d0SEli Cohen }
1436cdbe33d0SEli Cohen 
1437e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1438e126ba97SEli Cohen 			       struct ib_port_modify *props)
1439e126ba97SEli Cohen {
1440e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1441e126ba97SEli Cohen 	struct ib_port_attr attr;
1442e126ba97SEli Cohen 	u32 tmp;
1443e126ba97SEli Cohen 	int err;
1444cdbe33d0SEli Cohen 	u32 change_mask;
1445cdbe33d0SEli Cohen 	u32 value;
1446cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1447cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1448cdbe33d0SEli Cohen 
1449ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1450ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1451ec255879SMajd Dibbiny 	 */
1452ec255879SMajd Dibbiny 	if (!is_ib)
1453ec255879SMajd Dibbiny 		return 0;
1454ec255879SMajd Dibbiny 
1455cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1456cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1457cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1458cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1459cdbe33d0SEli Cohen 	}
1460e126ba97SEli Cohen 
1461e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1462e126ba97SEli Cohen 
1463c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1464e126ba97SEli Cohen 	if (err)
1465e126ba97SEli Cohen 		goto out;
1466e126ba97SEli Cohen 
1467e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1468e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1469e126ba97SEli Cohen 
14709603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1471e126ba97SEli Cohen 
1472e126ba97SEli Cohen out:
1473e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1474e126ba97SEli Cohen 	return err;
1475e126ba97SEli Cohen }
1476e126ba97SEli Cohen 
147730aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
147830aa60b3SEli Cohen {
147930aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
148030aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
148130aa60b3SEli Cohen }
148230aa60b3SEli Cohen 
148331a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
148431a78a5aSYishai Hadas {
148531a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
148631a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
148731a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
148831a78a5aSYishai Hadas 
148931a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
149031a78a5aSYishai Hadas }
149131a78a5aSYishai Hadas 
1492b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1493b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
149431a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1495b037c29aSEli Cohen {
1496b037c29aSEli Cohen 	int uars_per_sys_page;
1497b037c29aSEli Cohen 	int bfregs_per_sys_page;
1498b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1499b037c29aSEli Cohen 
1500b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1501b037c29aSEli Cohen 		return -EINVAL;
1502b037c29aSEli Cohen 
1503b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1504b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1505b037c29aSEli Cohen 
1506b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1507b037c29aSEli Cohen 		return -ENOMEM;
1508b037c29aSEli Cohen 
1509b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1510b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
151131a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1512b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1513b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1514b037c29aSEli Cohen 		return -EINVAL;
1515b037c29aSEli Cohen 
151631a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
151731a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
151831a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
151931a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
152031a78a5aSYishai Hadas 
152131a78a5aSYishai 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",
1522b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1523b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
152431a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
152531a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1526b037c29aSEli Cohen 
1527b037c29aSEli Cohen 	return 0;
1528b037c29aSEli Cohen }
1529b037c29aSEli Cohen 
1530b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1531b037c29aSEli Cohen {
1532b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1533b037c29aSEli Cohen 	int err;
1534b037c29aSEli Cohen 	int i;
1535b037c29aSEli Cohen 
1536b037c29aSEli Cohen 	bfregi = &context->bfregi;
153731a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1538b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1539b037c29aSEli Cohen 		if (err)
1540b037c29aSEli Cohen 			goto error;
1541b037c29aSEli Cohen 
1542b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1543b037c29aSEli Cohen 	}
15444ed131d0SYishai Hadas 
15454ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
15464ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
15474ed131d0SYishai Hadas 
1548b037c29aSEli Cohen 	return 0;
1549b037c29aSEli Cohen 
1550b037c29aSEli Cohen error:
1551b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1552b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1553b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1554b037c29aSEli Cohen 
1555b037c29aSEli Cohen 	return err;
1556b037c29aSEli Cohen }
1557b037c29aSEli Cohen 
155815177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
155915177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1560b037c29aSEli Cohen {
1561b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1562b037c29aSEli Cohen 	int i;
1563b037c29aSEli Cohen 
1564b037c29aSEli Cohen 	bfregi = &context->bfregi;
156515177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
15664ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
156715177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
156815177999SLeon Romanovsky 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1569b037c29aSEli Cohen }
1570b037c29aSEli Cohen 
15710042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1572a560f1d9SMark Bloch {
1573a560f1d9SMark Bloch 	int err = 0;
1574a560f1d9SMark Bloch 
1575a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
15760042f9e4SMark Bloch 	if (td)
1577a560f1d9SMark Bloch 		dev->lb.user_td++;
15780042f9e4SMark Bloch 	if (qp)
15790042f9e4SMark Bloch 		dev->lb.qps++;
1580a560f1d9SMark Bloch 
15810042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
15820042f9e4SMark Bloch 	    dev->lb.qps == 1) {
15830042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1584a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
15850042f9e4SMark Bloch 			dev->lb.enabled = true;
15860042f9e4SMark Bloch 		}
15870042f9e4SMark Bloch 	}
1588a560f1d9SMark Bloch 
1589a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1590a560f1d9SMark Bloch 
1591a560f1d9SMark Bloch 	return err;
1592a560f1d9SMark Bloch }
1593a560f1d9SMark Bloch 
15940042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1595a560f1d9SMark Bloch {
1596a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
15970042f9e4SMark Bloch 	if (td)
1598a560f1d9SMark Bloch 		dev->lb.user_td--;
15990042f9e4SMark Bloch 	if (qp)
16000042f9e4SMark Bloch 		dev->lb.qps--;
1601a560f1d9SMark Bloch 
16020042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
16030042f9e4SMark Bloch 	    dev->lb.qps == 0) {
16040042f9e4SMark Bloch 		if (dev->lb.enabled) {
1605a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
16060042f9e4SMark Bloch 			dev->lb.enabled = false;
16070042f9e4SMark Bloch 		}
16080042f9e4SMark Bloch 	}
1609a560f1d9SMark Bloch 
1610a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1611a560f1d9SMark Bloch }
1612a560f1d9SMark Bloch 
1613d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1614d2d19121SYishai Hadas 					  u16 uid)
1615c85023e1SHuy Nguyen {
1616c85023e1SHuy Nguyen 	int err;
1617c85023e1SHuy Nguyen 
1618cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1619cfdeb893SLeon Romanovsky 		return 0;
1620cfdeb893SLeon Romanovsky 
1621d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1622c85023e1SHuy Nguyen 	if (err)
1623c85023e1SHuy Nguyen 		return err;
1624c85023e1SHuy Nguyen 
1625c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
16268978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
16278978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1628c85023e1SHuy Nguyen 		return err;
1629c85023e1SHuy Nguyen 
16300042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1631c85023e1SHuy Nguyen }
1632c85023e1SHuy Nguyen 
1633d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1634d2d19121SYishai Hadas 					     u16 uid)
1635c85023e1SHuy Nguyen {
1636cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1637cfdeb893SLeon Romanovsky 		return;
1638cfdeb893SLeon Romanovsky 
1639d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1640c85023e1SHuy Nguyen 
1641c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
16428978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
16438978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1644c85023e1SHuy Nguyen 		return;
1645c85023e1SHuy Nguyen 
16460042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1647c85023e1SHuy Nguyen }
1648c85023e1SHuy Nguyen 
1649e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1650e126ba97SEli Cohen 						  struct ib_udata *udata)
1651e126ba97SEli Cohen {
1652e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1653b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1654b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
16555c99eaecSFeras Daoud 	struct mlx5_core_dev *mdev = dev->mdev;
1656e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
16572f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
165878c0f98cSEli Cohen 	int ver;
1659e126ba97SEli Cohen 	int err;
1660a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1661a168a41cSMajd Dibbiny 				     max_cqe_version);
166225bb36e7SYonatan Cohen 	u32 dump_fill_mkey;
1663b037c29aSEli Cohen 	bool lib_uar_4k;
1664e126ba97SEli Cohen 
1665e126ba97SEli Cohen 	if (!dev->ib_active)
1666e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1667e126ba97SEli Cohen 
1668e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
166978c0f98cSEli Cohen 		ver = 0;
1670e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
167178c0f98cSEli Cohen 		ver = 2;
167278c0f98cSEli Cohen 	else
167378c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
167478c0f98cSEli Cohen 
1675e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1676e126ba97SEli Cohen 	if (err)
1677e126ba97SEli Cohen 		return ERR_PTR(err);
1678e126ba97SEli Cohen 
1679a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1680a8b92ca1SYishai Hadas 		return ERR_PTR(-EOPNOTSUPP);
168178c0f98cSEli Cohen 
1682f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1683b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1684b368d7cbSMatan Barak 
16852f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
16862f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
16872f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1688e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1689e126ba97SEli Cohen 
1690938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
16912cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1692938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1693b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1694938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1695938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1696938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1697938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1698938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1699f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1700f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1701f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
170230aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
170330aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
170430aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
170530aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1706b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1707b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1708e126ba97SEli Cohen 
1709c03faa56SMatan Barak 	if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1710c03faa56SMatan Barak 		if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1711c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1712c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1713c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1714c03faa56SMatan Barak 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1715c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1716c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1717c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1718c03faa56SMatan Barak 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1719c03faa56SMatan Barak 	}
1720c03faa56SMatan Barak 
1721e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1722e126ba97SEli Cohen 	if (!context)
1723e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1724e126ba97SEli Cohen 
172530aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
17262f5ff264SEli Cohen 	bfregi = &context->bfregi;
1727b037c29aSEli Cohen 
1728b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
172931a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1730b037c29aSEli Cohen 	if (err)
1731b037c29aSEli Cohen 		goto out_ctx;
1732b037c29aSEli Cohen 
17332f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1734b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
173531a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1736b037c29aSEli Cohen 				GFP_KERNEL);
1737b037c29aSEli Cohen 	if (!bfregi->count) {
1738e126ba97SEli Cohen 		err = -ENOMEM;
1739e126ba97SEli Cohen 		goto out_ctx;
1740e126ba97SEli Cohen 	}
1741e126ba97SEli Cohen 
1742b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1743b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1744e126ba97SEli Cohen 				    GFP_KERNEL);
1745b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1746e126ba97SEli Cohen 		err = -ENOMEM;
1747e126ba97SEli Cohen 		goto out_count;
1748e126ba97SEli Cohen 	}
1749e126ba97SEli Cohen 
1750b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1751b037c29aSEli Cohen 	if (err)
1752b037c29aSEli Cohen 		goto out_sys_pages;
1753b037c29aSEli Cohen 
1754b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1755b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1756b4cfe447SHaggai Eran #endif
1757b4cfe447SHaggai Eran 
1758a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
175976dc5a84SYishai Hadas 		err = mlx5_ib_devx_create(dev);
176076dc5a84SYishai Hadas 		if (err < 0)
1761d2d19121SYishai Hadas 			goto out_uars;
176276dc5a84SYishai Hadas 		context->devx_uid = err;
1763a8b92ca1SYishai Hadas 	}
1764a8b92ca1SYishai Hadas 
1765d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1766d2d19121SYishai Hadas 					     context->devx_uid);
1767a8b92ca1SYishai Hadas 	if (err)
1768d2d19121SYishai Hadas 		goto out_devx;
1769a8b92ca1SYishai Hadas 
177025bb36e7SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
177125bb36e7SYonatan Cohen 		err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
177225bb36e7SYonatan Cohen 		if (err)
17738193abb6SJason Gunthorpe 			goto out_mdev;
177425bb36e7SYonatan Cohen 	}
177525bb36e7SYonatan Cohen 
1776e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1777e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1778e126ba97SEli Cohen 
17792f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1780508562d6SDaniel Jurgens 	resp.num_ports = dev->num_ports;
1781b368d7cbSMatan Barak 
1782f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1783f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1784b368d7cbSMatan Barak 
1785402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
17866ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
17876ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1788402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1789402ca536SBodong Wang 	}
1790402ca536SBodong Wang 
179178984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
179278984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
179378984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
179478984898SOr Gerlitz 			resp.eth_min_inline++;
179578984898SOr Gerlitz 		}
179678984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
179778984898SOr Gerlitz 	}
179878984898SOr Gerlitz 
17995c99eaecSFeras Daoud 	if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
18005c99eaecSFeras Daoud 		if (mdev->clock_info)
18015c99eaecSFeras Daoud 			resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
18025c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.clock_info_versions);
18035c99eaecSFeras Daoud 	}
18045c99eaecSFeras Daoud 
1805bc5c6eedSNoa Osherovich 	/*
1806bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1807bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1808bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1809bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1810bc5c6eedSNoa Osherovich 	 */
1811de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1812de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1813b368d7cbSMatan Barak 			resp.comp_mask |=
1814b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1815b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1816de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1817de8d6e02SEli Cohen 		}
18185c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.hca_core_clock_offset);
1819b368d7cbSMatan Barak 	}
1820b368d7cbSMatan Barak 
182130aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
182230aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
182330aa60b3SEli Cohen 
182430aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
182530aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
182630aa60b3SEli Cohen 
182731a78a5aSYishai Hadas 	if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
182831a78a5aSYishai Hadas 		resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
182931a78a5aSYishai Hadas 		resp.response_length += sizeof(resp.num_dyn_bfregs);
183031a78a5aSYishai Hadas 	}
183131a78a5aSYishai Hadas 
183225bb36e7SYonatan Cohen 	if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
183325bb36e7SYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
183425bb36e7SYonatan Cohen 			resp.dump_fill_mkey = dump_fill_mkey;
183525bb36e7SYonatan Cohen 			resp.comp_mask |=
183625bb36e7SYonatan Cohen 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
183725bb36e7SYonatan Cohen 		}
183825bb36e7SYonatan Cohen 		resp.response_length += sizeof(resp.dump_fill_mkey);
183925bb36e7SYonatan Cohen 	}
184025bb36e7SYonatan Cohen 
1841b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1842e126ba97SEli Cohen 	if (err)
1843a8b92ca1SYishai Hadas 		goto out_mdev;
1844e126ba97SEli Cohen 
18452f5ff264SEli Cohen 	bfregi->ver = ver;
18462f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1847f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
184830aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
184930aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1850f72300c5SHaggai Abramovsky 
1851*7c34ec19SAviv Heller 	if (dev->lag_active) {
1852c6a21c38SMajd Dibbiny 		u8 port = mlx5_core_native_port_num(dev->mdev);
1853c6a21c38SMajd Dibbiny 
1854c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1855c6a21c38SMajd Dibbiny 			   atomic_add_return(
1856c6a21c38SMajd Dibbiny 				   1, &dev->roce[port].tx_port_affinity));
1857c6a21c38SMajd Dibbiny 	}
1858c6a21c38SMajd Dibbiny 
1859e126ba97SEli Cohen 	return &context->ibucontext;
1860e126ba97SEli Cohen 
1861a8b92ca1SYishai Hadas out_mdev:
1862d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1863d2d19121SYishai Hadas out_devx:
1864a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
186576dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1866146d2f1aSmajd@mellanox.com 
1867e126ba97SEli Cohen out_uars:
1868b037c29aSEli Cohen 	deallocate_uars(dev, context);
1869b037c29aSEli Cohen 
1870b037c29aSEli Cohen out_sys_pages:
1871b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1872b037c29aSEli Cohen 
1873e126ba97SEli Cohen out_count:
18742f5ff264SEli Cohen 	kfree(bfregi->count);
1875e126ba97SEli Cohen 
1876e126ba97SEli Cohen out_ctx:
1877e126ba97SEli Cohen 	kfree(context);
1878b037c29aSEli Cohen 
1879e126ba97SEli Cohen 	return ERR_PTR(err);
1880e126ba97SEli Cohen }
1881e126ba97SEli Cohen 
1882e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1883e126ba97SEli Cohen {
1884e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1885e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1886b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1887e126ba97SEli Cohen 
1888f27a0d50SJason Gunthorpe #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1889f27a0d50SJason Gunthorpe 	/* All umem's must be destroyed before destroying the ucontext. */
1890f27a0d50SJason Gunthorpe 	mutex_lock(&ibcontext->per_mm_list_lock);
1891f27a0d50SJason Gunthorpe 	WARN_ON(!list_empty(&ibcontext->per_mm_list));
1892f27a0d50SJason Gunthorpe 	mutex_unlock(&ibcontext->per_mm_list_lock);
1893f27a0d50SJason Gunthorpe #endif
1894a8b92ca1SYishai Hadas 
1895b037c29aSEli Cohen 	bfregi = &context->bfregi;
1896d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1897d2d19121SYishai Hadas 
1898b037c29aSEli Cohen 	if (context->devx_uid)
189976dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1900e126ba97SEli Cohen 
1901e126ba97SEli Cohen 	deallocate_uars(dev, context);
19022f5ff264SEli Cohen 	kfree(bfregi->sys_pages);
19032f5ff264SEli Cohen 	kfree(bfregi->count);
19042f5ff264SEli Cohen 	kfree(context);
1905e126ba97SEli Cohen 
1906e126ba97SEli Cohen 	return 0;
1907e126ba97SEli Cohen }
1908e126ba97SEli Cohen 
1909e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1910e126ba97SEli Cohen 				 int uar_idx)
1911e126ba97SEli Cohen {
1912b037c29aSEli Cohen 	int fw_uars_per_page;
1913b037c29aSEli Cohen 
1914b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1915b037c29aSEli Cohen 
19164ed131d0SYishai Hadas 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
1917e126ba97SEli Cohen }
1918e126ba97SEli Cohen 
1919e126ba97SEli Cohen static int get_command(unsigned long offset)
1920e126ba97SEli Cohen {
1921e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1922e126ba97SEli Cohen }
1923e126ba97SEli Cohen 
1924e126ba97SEli Cohen static int get_arg(unsigned long offset)
1925e126ba97SEli Cohen {
1926e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1927e126ba97SEli Cohen }
1928e126ba97SEli Cohen 
1929e126ba97SEli Cohen static int get_index(unsigned long offset)
1930e126ba97SEli Cohen {
1931e126ba97SEli Cohen 	return get_arg(offset);
1932e126ba97SEli Cohen }
1933e126ba97SEli Cohen 
19344ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
19354ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
19364ed131d0SYishai Hadas {
19374ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
19384ed131d0SYishai Hadas }
19394ed131d0SYishai Hadas 
19407c2344c3SMaor Gottlieb 
19417c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
19427c2344c3SMaor Gottlieb {
19437c2344c3SMaor Gottlieb }
19447c2344c3SMaor Gottlieb 
194537aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1946e126ba97SEli Cohen {
194737aa5c36SGuy Levi 	switch (cmd) {
194837aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
194937aa5c36SGuy Levi 		return "WC";
1950e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
195137aa5c36SGuy Levi 		return "best effort WC";
195237aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
195337aa5c36SGuy Levi 		return "NC";
195424da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
195524da0016SAriel Levkovich 		return "Device Memory";
195637aa5c36SGuy Levi 	default:
195737aa5c36SGuy Levi 		return NULL;
195837aa5c36SGuy Levi 	}
195937aa5c36SGuy Levi }
196037aa5c36SGuy Levi 
19615c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
19625c99eaecSFeras Daoud 					struct vm_area_struct *vma,
19635c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
19645c99eaecSFeras Daoud {
19655c99eaecSFeras Daoud 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
19665c99eaecSFeras Daoud 		return -EINVAL;
19675c99eaecSFeras Daoud 
19685c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
19695c99eaecSFeras Daoud 		return -EOPNOTSUPP;
19705c99eaecSFeras Daoud 
19715c99eaecSFeras Daoud 	if (vma->vm_flags & VM_WRITE)
19725c99eaecSFeras Daoud 		return -EPERM;
19735c99eaecSFeras Daoud 
19745c99eaecSFeras Daoud 	if (!dev->mdev->clock_info_page)
19755c99eaecSFeras Daoud 		return -EOPNOTSUPP;
19765c99eaecSFeras Daoud 
1977e2cd1d1aSJason Gunthorpe 	return rdma_user_mmap_page(&context->ibucontext, vma,
1978e2cd1d1aSJason Gunthorpe 				   dev->mdev->clock_info_page, PAGE_SIZE);
19795c99eaecSFeras Daoud }
19805c99eaecSFeras Daoud 
198137aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
19827c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
19837c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
198437aa5c36SGuy Levi {
19852f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
198637aa5c36SGuy Levi 	int err;
198737aa5c36SGuy Levi 	unsigned long idx;
1988aa09ea6eSKamal Heib 	phys_addr_t pfn;
198937aa5c36SGuy Levi 	pgprot_t prot;
19904ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
19914ed131d0SYishai Hadas 	u32 uar_index;
19924ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
19934ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
19944ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
1995b037c29aSEli Cohen 
1996b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1997b037c29aSEli Cohen 		return -EINVAL;
1998b037c29aSEli Cohen 
19994ed131d0SYishai Hadas 	if (dyn_uar)
20004ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
20014ed131d0SYishai Hadas 	else
2002b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
20034ed131d0SYishai Hadas 
20044ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
20054ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
20064ed131d0SYishai Hadas 			     idx, max_valid_idx);
2007b037c29aSEli Cohen 		return -EINVAL;
2008b037c29aSEli Cohen 	}
200937aa5c36SGuy Levi 
201037aa5c36SGuy Levi 	switch (cmd) {
201137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
20124ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
201337aa5c36SGuy Levi /* Some architectures don't support WC memory */
201437aa5c36SGuy Levi #if defined(CONFIG_X86)
201537aa5c36SGuy Levi 		if (!pat_enabled())
201637aa5c36SGuy Levi 			return -EPERM;
201737aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
201837aa5c36SGuy Levi 			return -EPERM;
201937aa5c36SGuy Levi #endif
202037aa5c36SGuy Levi 	/* fall through */
202137aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
202237aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
202337aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
202437aa5c36SGuy Levi 		break;
202537aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
202637aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
202737aa5c36SGuy Levi 		break;
202837aa5c36SGuy Levi 	default:
202937aa5c36SGuy Levi 		return -EINVAL;
203037aa5c36SGuy Levi 	}
203137aa5c36SGuy Levi 
20324ed131d0SYishai Hadas 	if (dyn_uar) {
20334ed131d0SYishai Hadas 		int uars_per_page;
20344ed131d0SYishai Hadas 
20354ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
20364ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
20374ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
20384ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
20394ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
20404ed131d0SYishai Hadas 			return -EINVAL;
20414ed131d0SYishai Hadas 		}
20424ed131d0SYishai Hadas 
20434ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
20444ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
20454ed131d0SYishai Hadas 		 * page holds its count.
20464ed131d0SYishai Hadas 		 */
20474ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
20484ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
20494ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
20504ed131d0SYishai Hadas 			return -EINVAL;
20514ed131d0SYishai Hadas 		}
20524ed131d0SYishai Hadas 
20534ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
20544ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
20554ed131d0SYishai Hadas 
20564ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
20574ed131d0SYishai Hadas 		if (err) {
20584ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
20594ed131d0SYishai Hadas 			goto free_bfreg;
20604ed131d0SYishai Hadas 		}
20614ed131d0SYishai Hadas 	} else {
20624ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
20634ed131d0SYishai Hadas 	}
20644ed131d0SYishai Hadas 
20654ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
206637aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2067e126ba97SEli Cohen 
2068e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2069e2cd1d1aSJason Gunthorpe 				prot);
207037aa5c36SGuy Levi 	if (err) {
20718f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2072e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
20738f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
20744ed131d0SYishai Hadas 		goto err;
207537aa5c36SGuy Levi 	}
2076e126ba97SEli Cohen 
20774ed131d0SYishai Hadas 	if (dyn_uar)
20784ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
20794ed131d0SYishai Hadas 	return 0;
20804ed131d0SYishai Hadas 
20814ed131d0SYishai Hadas err:
20824ed131d0SYishai Hadas 	if (!dyn_uar)
20834ed131d0SYishai Hadas 		return err;
20844ed131d0SYishai Hadas 
20854ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
20864ed131d0SYishai Hadas 
20874ed131d0SYishai Hadas free_bfreg:
20884ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
20894ed131d0SYishai Hadas 
20904ed131d0SYishai Hadas 	return err;
209137aa5c36SGuy Levi }
209237aa5c36SGuy Levi 
209324da0016SAriel Levkovich static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
209424da0016SAriel Levkovich {
209524da0016SAriel Levkovich 	struct mlx5_ib_ucontext *mctx = to_mucontext(context);
209624da0016SAriel Levkovich 	struct mlx5_ib_dev *dev = to_mdev(context->device);
209724da0016SAriel Levkovich 	u16 page_idx = get_extended_index(vma->vm_pgoff);
209824da0016SAriel Levkovich 	size_t map_size = vma->vm_end - vma->vm_start;
209924da0016SAriel Levkovich 	u32 npages = map_size >> PAGE_SHIFT;
210024da0016SAriel Levkovich 	phys_addr_t pfn;
210124da0016SAriel Levkovich 
210224da0016SAriel Levkovich 	if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
210324da0016SAriel Levkovich 	    page_idx + npages)
210424da0016SAriel Levkovich 		return -EINVAL;
210524da0016SAriel Levkovich 
210624da0016SAriel Levkovich 	pfn = ((pci_resource_start(dev->mdev->pdev, 0) +
210724da0016SAriel Levkovich 	      MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
210824da0016SAriel Levkovich 	      PAGE_SHIFT) +
210924da0016SAriel Levkovich 	      page_idx;
2110e2cd1d1aSJason Gunthorpe 	return rdma_user_mmap_io(context, vma, pfn, map_size,
2111e2cd1d1aSJason Gunthorpe 				 pgprot_writecombine(vma->vm_page_prot));
211224da0016SAriel Levkovich }
211324da0016SAriel Levkovich 
211437aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
211537aa5c36SGuy Levi {
211637aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
211737aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
211837aa5c36SGuy Levi 	unsigned long command;
211937aa5c36SGuy Levi 	phys_addr_t pfn;
212037aa5c36SGuy Levi 
212137aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
212237aa5c36SGuy Levi 	switch (command) {
212337aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
212437aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
212537aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
21264ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
21277c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2128e126ba97SEli Cohen 
2129e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2130e126ba97SEli Cohen 		return -ENOSYS;
2131e126ba97SEli Cohen 
2132d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2133d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2134d69e3bcfSMatan Barak 			return -EINVAL;
2135d69e3bcfSMatan Barak 
21366cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2137d69e3bcfSMatan Barak 			return -EPERM;
2138d69e3bcfSMatan Barak 
2139d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2140d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2141d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2142d69e3bcfSMatan Barak 
2143d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2144d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2145d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2146d69e3bcfSMatan Barak 			PAGE_SHIFT;
2147d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2148d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
2149d69e3bcfSMatan Barak 			return -EAGAIN;
2150d69e3bcfSMatan Barak 		break;
21515c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
21525c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2153d69e3bcfSMatan Barak 
215424da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
215524da0016SAriel Levkovich 		return dm_mmap(ibcontext, vma);
215624da0016SAriel Levkovich 
2157e126ba97SEli Cohen 	default:
2158e126ba97SEli Cohen 		return -EINVAL;
2159e126ba97SEli Cohen 	}
2160e126ba97SEli Cohen 
2161e126ba97SEli Cohen 	return 0;
2162e126ba97SEli Cohen }
2163e126ba97SEli Cohen 
216424da0016SAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
216524da0016SAriel Levkovich 			       struct ib_ucontext *context,
216624da0016SAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
216724da0016SAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
216824da0016SAriel Levkovich {
216924da0016SAriel Levkovich 	u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
217024da0016SAriel Levkovich 	struct mlx5_memic *memic = &to_mdev(ibdev)->memic;
217124da0016SAriel Levkovich 	phys_addr_t memic_addr;
217224da0016SAriel Levkovich 	struct mlx5_ib_dm *dm;
217324da0016SAriel Levkovich 	u64 start_offset;
217424da0016SAriel Levkovich 	u32 page_idx;
217524da0016SAriel Levkovich 	int err;
217624da0016SAriel Levkovich 
217724da0016SAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
217824da0016SAriel Levkovich 	if (!dm)
217924da0016SAriel Levkovich 		return ERR_PTR(-ENOMEM);
218024da0016SAriel Levkovich 
218124da0016SAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
218224da0016SAriel Levkovich 		    attr->length, act_size, attr->alignment);
218324da0016SAriel Levkovich 
218424da0016SAriel Levkovich 	err = mlx5_cmd_alloc_memic(memic, &memic_addr,
218524da0016SAriel Levkovich 				   act_size, attr->alignment);
218624da0016SAriel Levkovich 	if (err)
218724da0016SAriel Levkovich 		goto err_free;
218824da0016SAriel Levkovich 
218924da0016SAriel Levkovich 	start_offset = memic_addr & ~PAGE_MASK;
219024da0016SAriel Levkovich 	page_idx = (memic_addr - pci_resource_start(memic->dev->pdev, 0) -
219124da0016SAriel Levkovich 		    MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
219224da0016SAriel Levkovich 		    PAGE_SHIFT;
219324da0016SAriel Levkovich 
219424da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
219524da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
219624da0016SAriel Levkovich 			     &start_offset, sizeof(start_offset));
219724da0016SAriel Levkovich 	if (err)
219824da0016SAriel Levkovich 		goto err_dealloc;
219924da0016SAriel Levkovich 
220024da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
220124da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
220224da0016SAriel Levkovich 			     &page_idx, sizeof(page_idx));
220324da0016SAriel Levkovich 	if (err)
220424da0016SAriel Levkovich 		goto err_dealloc;
220524da0016SAriel Levkovich 
220624da0016SAriel Levkovich 	bitmap_set(to_mucontext(context)->dm_pages, page_idx,
220724da0016SAriel Levkovich 		   DIV_ROUND_UP(act_size, PAGE_SIZE));
220824da0016SAriel Levkovich 
220924da0016SAriel Levkovich 	dm->dev_addr = memic_addr;
221024da0016SAriel Levkovich 
221124da0016SAriel Levkovich 	return &dm->ibdm;
221224da0016SAriel Levkovich 
221324da0016SAriel Levkovich err_dealloc:
221424da0016SAriel Levkovich 	mlx5_cmd_dealloc_memic(memic, memic_addr,
221524da0016SAriel Levkovich 			       act_size);
221624da0016SAriel Levkovich err_free:
221724da0016SAriel Levkovich 	kfree(dm);
221824da0016SAriel Levkovich 	return ERR_PTR(err);
221924da0016SAriel Levkovich }
222024da0016SAriel Levkovich 
222124da0016SAriel Levkovich int mlx5_ib_dealloc_dm(struct ib_dm *ibdm)
222224da0016SAriel Levkovich {
222324da0016SAriel Levkovich 	struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic;
222424da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
222524da0016SAriel Levkovich 	u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE);
222624da0016SAriel Levkovich 	u32 page_idx;
222724da0016SAriel Levkovich 	int ret;
222824da0016SAriel Levkovich 
222924da0016SAriel Levkovich 	ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size);
223024da0016SAriel Levkovich 	if (ret)
223124da0016SAriel Levkovich 		return ret;
223224da0016SAriel Levkovich 
223324da0016SAriel Levkovich 	page_idx = (dm->dev_addr - pci_resource_start(memic->dev->pdev, 0) -
223424da0016SAriel Levkovich 		    MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
223524da0016SAriel Levkovich 		    PAGE_SHIFT;
223624da0016SAriel Levkovich 	bitmap_clear(to_mucontext(ibdm->uobject->context)->dm_pages,
223724da0016SAriel Levkovich 		     page_idx,
223824da0016SAriel Levkovich 		     DIV_ROUND_UP(act_size, PAGE_SIZE));
223924da0016SAriel Levkovich 
224024da0016SAriel Levkovich 	kfree(dm);
224124da0016SAriel Levkovich 
224224da0016SAriel Levkovich 	return 0;
224324da0016SAriel Levkovich }
224424da0016SAriel Levkovich 
2245e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
2246e126ba97SEli Cohen 				      struct ib_ucontext *context,
2247e126ba97SEli Cohen 				      struct ib_udata *udata)
2248e126ba97SEli Cohen {
2249e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2250e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
2251e126ba97SEli Cohen 	int err;
2252a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2253a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)]   = {};
2254a1069c1cSYishai Hadas 	u16 uid = 0;
2255e126ba97SEli Cohen 
2256e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
2257e126ba97SEli Cohen 	if (!pd)
2258e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
2259e126ba97SEli Cohen 
226058895f0dSYishai Hadas 	uid = context ? to_mucontext(context)->devx_uid : 0;
2261a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2262a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
2263a1069c1cSYishai Hadas 	err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2264a1069c1cSYishai Hadas 			    out, sizeof(out));
2265e126ba97SEli Cohen 	if (err) {
2266e126ba97SEli Cohen 		kfree(pd);
2267e126ba97SEli Cohen 		return ERR_PTR(err);
2268e126ba97SEli Cohen 	}
2269e126ba97SEli Cohen 
2270a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2271a1069c1cSYishai Hadas 	pd->uid = uid;
2272e126ba97SEli Cohen 	if (context) {
2273e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2274e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2275a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
2276e126ba97SEli Cohen 			kfree(pd);
2277e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
2278e126ba97SEli Cohen 		}
2279e126ba97SEli Cohen 	}
2280e126ba97SEli Cohen 
2281e126ba97SEli Cohen 	return &pd->ibpd;
2282e126ba97SEli Cohen }
2283e126ba97SEli Cohen 
2284e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
2285e126ba97SEli Cohen {
2286e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2287e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2288e126ba97SEli Cohen 
2289a1069c1cSYishai Hadas 	mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2290e126ba97SEli Cohen 	kfree(mpd);
2291e126ba97SEli Cohen 
2292e126ba97SEli Cohen 	return 0;
2293e126ba97SEli Cohen }
2294e126ba97SEli Cohen 
2295466fa6d2SMaor Gottlieb enum {
2296466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2297466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
229871c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_INNER_BIT,
229971c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_MISC2_BIT
2300466fa6d2SMaor Gottlieb };
2301038d2ef8SMaor Gottlieb 
2302466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2303466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2304466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2305466fa6d2SMaor Gottlieb 
2306466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2307466fa6d2SMaor Gottlieb {
2308466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2309466fa6d2SMaor Gottlieb 
2310466fa6d2SMaor Gottlieb 	match_criteria_enable =
2311466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2312466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2313466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2314466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2315466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2316466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2317466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2318466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
231971c6e863SAriel Levkovich 	match_criteria_enable |=
232071c6e863SAriel Levkovich 		(!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
232171c6e863SAriel Levkovich 		MATCH_CRITERIA_ENABLE_MISC2_BIT;
2322466fa6d2SMaor Gottlieb 
2323466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2324038d2ef8SMaor Gottlieb }
2325038d2ef8SMaor Gottlieb 
2326ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2327ca0d4753SMaor Gottlieb {
2328ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2329ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2330ca0d4753SMaor Gottlieb }
2331ca0d4753SMaor Gottlieb 
233237da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
23332d1e697eSMoses Reuben 			   bool inner)
23342d1e697eSMoses Reuben {
23352d1e697eSMoses Reuben 	if (inner) {
23362d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
23372d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
23382d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
23392d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
23402d1e697eSMoses Reuben 	} else {
23412d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
23422d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
23432d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
23442d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
23452d1e697eSMoses Reuben 	}
23462d1e697eSMoses Reuben }
23472d1e697eSMoses Reuben 
2348ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2349ca0d4753SMaor Gottlieb {
2350ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2351ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2352ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2353ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2354ca0d4753SMaor Gottlieb }
2355ca0d4753SMaor Gottlieb 
235671c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
235771c6e863SAriel Levkovich {
235871c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
235971c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
236071c6e863SAriel Levkovich 		return -EOPNOTSUPP;
236171c6e863SAriel Levkovich 
236271c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
236371c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
236471c6e863SAriel Levkovich 		return -EOPNOTSUPP;
236571c6e863SAriel Levkovich 
236671c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
236771c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
236871c6e863SAriel Levkovich 		return -EOPNOTSUPP;
236971c6e863SAriel Levkovich 
237071c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
237171c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
237271c6e863SAriel Levkovich 		return -EOPNOTSUPP;
237371c6e863SAriel Levkovich 
237471c6e863SAriel Levkovich 	return 0;
237571c6e863SAriel Levkovich }
237671c6e863SAriel Levkovich 
2377c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2378c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2379ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2380466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2381c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2382ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
23832ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2384a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
23853b3233fbSRaed Salem #define LAST_COUNTERS_FIELD counters
2386c47ac6aeSMaor Gottlieb 
2387c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2388c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2389c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2390c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2391c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2392c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2393c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2394c47ac6aeSMaor Gottlieb 
23952ea26203SMark Bloch int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
23962ea26203SMark Bloch 			   bool is_egress,
2397802c2125SAviad Yehezkel 			   struct mlx5_flow_act *action)
2398802c2125SAviad Yehezkel {
2399802c2125SAviad Yehezkel 
2400802c2125SAviad Yehezkel 	switch (maction->ib_action.type) {
2401802c2125SAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
2402501f14e3SMark Bloch 		if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2403501f14e3SMark Bloch 				      MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2404501f14e3SMark Bloch 			return -EINVAL;
2405802c2125SAviad Yehezkel 		/* Currently only AES_GCM keymat is supported by the driver */
2406802c2125SAviad Yehezkel 		action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
24072ea26203SMark Bloch 		action->action |= is_egress ?
2408802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2409802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2410802c2125SAviad Yehezkel 		return 0;
2411b1085be3SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
2412b1085be3SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2413b1085be3SMark Bloch 		    MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
2414501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2415501f14e3SMark Bloch 				return -EINVAL;
2416b1085be3SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2417b1085be3SMark Bloch 			action->modify_id = maction->flow_action_raw.action_id;
2418b1085be3SMark Bloch 			return 0;
2419b1085be3SMark Bloch 		}
242010a30896SMark Bloch 		if (maction->flow_action_raw.sub_type ==
242110a30896SMark Bloch 		    MLX5_IB_FLOW_ACTION_DECAP) {
2422501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2423501f14e3SMark Bloch 				return -EINVAL;
242410a30896SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
242510a30896SMark Bloch 			return 0;
242610a30896SMark Bloch 		}
2427e806f932SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2428e806f932SMark Bloch 		    MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
2429501f14e3SMark Bloch 			if (action->action &
2430501f14e3SMark Bloch 			    MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2431501f14e3SMark Bloch 				return -EINVAL;
2432e806f932SMark Bloch 			action->action |=
2433e806f932SMark Bloch 				MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2434e806f932SMark Bloch 			action->reformat_id =
2435e806f932SMark Bloch 				maction->flow_action_raw.action_id;
2436e806f932SMark Bloch 			return 0;
2437e806f932SMark Bloch 		}
2438b1085be3SMark Bloch 		/* fall through */
2439802c2125SAviad Yehezkel 	default:
2440802c2125SAviad Yehezkel 		return -EOPNOTSUPP;
2441802c2125SAviad Yehezkel 	}
2442802c2125SAviad Yehezkel }
2443802c2125SAviad Yehezkel 
244419cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
244519cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
2446802c2125SAviad Yehezkel 			   const struct ib_flow_attr *flow_attr,
244771c6e863SAriel Levkovich 			   struct mlx5_flow_act *action, u32 prev_type)
2448038d2ef8SMaor Gottlieb {
2449466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2450466fa6d2SMaor Gottlieb 					   misc_parameters);
2451466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2452466fa6d2SMaor Gottlieb 					   misc_parameters);
245371c6e863SAriel Levkovich 	void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
245471c6e863SAriel Levkovich 					    misc_parameters_2);
245571c6e863SAriel Levkovich 	void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
245671c6e863SAriel Levkovich 					    misc_parameters_2);
24572d1e697eSMoses Reuben 	void *headers_c;
24582d1e697eSMoses Reuben 	void *headers_v;
245919cc7524SAriel Levkovich 	int match_ipv;
2460802c2125SAviad Yehezkel 	int ret;
2461466fa6d2SMaor Gottlieb 
24622d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
24632d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
24642d1e697eSMoses Reuben 					 inner_headers);
24652d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
24662d1e697eSMoses Reuben 					 inner_headers);
246719cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
246819cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
24692d1e697eSMoses Reuben 	} else {
24702d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
24712d1e697eSMoses Reuben 					 outer_headers);
24722d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
24732d1e697eSMoses Reuben 					 outer_headers);
247419cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
247519cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
24762d1e697eSMoses Reuben 	}
24772d1e697eSMoses Reuben 
24782d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2479038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2480c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
24811ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2482038d2ef8SMaor Gottlieb 
24832d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2484038d2ef8SMaor Gottlieb 					     dmac_47_16),
2485038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
24862d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2487038d2ef8SMaor Gottlieb 					     dmac_47_16),
2488038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2489038d2ef8SMaor Gottlieb 
24902d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2491ee3da804SMaor Gottlieb 					     smac_47_16),
2492ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
24932d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2494ee3da804SMaor Gottlieb 					     smac_47_16),
2495ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2496ee3da804SMaor Gottlieb 
2497038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
24982d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
249910543365SMohamad Haj Yahia 				 cvlan_tag, 1);
25002d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
250110543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2502038d2ef8SMaor Gottlieb 
25032d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2504038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
25052d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2506038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2507038d2ef8SMaor Gottlieb 
25082d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2509038d2ef8SMaor Gottlieb 				 first_cfi,
2510038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
25112d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2512038d2ef8SMaor Gottlieb 				 first_cfi,
2513038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2514038d2ef8SMaor Gottlieb 
25152d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2516038d2ef8SMaor Gottlieb 				 first_prio,
2517038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
25182d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2519038d2ef8SMaor Gottlieb 				 first_prio,
2520038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2521038d2ef8SMaor Gottlieb 		}
25222d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2523038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
25242d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2525038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2526038d2ef8SMaor Gottlieb 		break;
2527038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2528c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
25291ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2530038d2ef8SMaor Gottlieb 
253119cc7524SAriel Levkovich 		if (match_ipv) {
253219cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
253319cc7524SAriel Levkovich 				 ip_version, 0xf);
253419cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
25353346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV4_VERSION);
253619cc7524SAriel Levkovich 		} else {
25372d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2538038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
25392d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2540038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
254119cc7524SAriel Levkovich 		}
2542038d2ef8SMaor Gottlieb 
25432d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2544038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2545038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2546038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
25472d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2548038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2549038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2550038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
25512d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2552038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2553038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2554038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
25552d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2556038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2557038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2558038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2559ca0d4753SMaor Gottlieb 
25602d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2561ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2562ca0d4753SMaor Gottlieb 
25632d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2564ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
2565038d2ef8SMaor Gottlieb 		break;
2566026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2567c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
25681ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2569026bae0cSMaor Gottlieb 
257019cc7524SAriel Levkovich 		if (match_ipv) {
257119cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
257219cc7524SAriel Levkovich 				 ip_version, 0xf);
257319cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
25743346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV6_VERSION);
257519cc7524SAriel Levkovich 		} else {
25762d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2577026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
25782d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2579026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
258019cc7524SAriel Levkovich 		}
2581026bae0cSMaor Gottlieb 
25822d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2583026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2584026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2585026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
25862d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2587026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2588026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2589026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
25902d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2591026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2592026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2593026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
25942d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2595026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2596026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2597026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2598466fa6d2SMaor Gottlieb 
25992d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2600466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2601466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2602466fa6d2SMaor Gottlieb 
26032d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2604466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
2605466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
2606466fa6d2SMaor Gottlieb 
26072d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
26082d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
26092d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
26102d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
2611802c2125SAviad Yehezkel 		break;
2612802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ESP:
2613802c2125SAviad Yehezkel 		if (ib_spec->esp.mask.seq)
2614802c2125SAviad Yehezkel 			return -EOPNOTSUPP;
26152d1e697eSMoses Reuben 
2616802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2617802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.mask.spi));
2618802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2619802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.val.spi));
2620026bae0cSMaor Gottlieb 		break;
2621038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2622c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2623c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
26241ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2625038d2ef8SMaor Gottlieb 
26262d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2627038d2ef8SMaor Gottlieb 			 0xff);
26282d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2629038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
2630038d2ef8SMaor Gottlieb 
26312d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2632038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
26332d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2634038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2635038d2ef8SMaor Gottlieb 
26362d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2637038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
26382d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2639038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2640038d2ef8SMaor Gottlieb 		break;
2641038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2642c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2643c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
26441ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2645038d2ef8SMaor Gottlieb 
26462d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2647038d2ef8SMaor Gottlieb 			 0xff);
26482d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2649038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
2650038d2ef8SMaor Gottlieb 
26512d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2652038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
26532d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2654038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2655038d2ef8SMaor Gottlieb 
26562d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2657038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
26582d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2659038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2660038d2ef8SMaor Gottlieb 		break;
2661da2f22aeSAriel Levkovich 	case IB_FLOW_SPEC_GRE:
2662da2f22aeSAriel Levkovich 		if (ib_spec->gre.mask.c_ks_res0_ver)
2663da2f22aeSAriel Levkovich 			return -EOPNOTSUPP;
2664da2f22aeSAriel Levkovich 
2665da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2666da2f22aeSAriel Levkovich 			 0xff);
2667da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2668da2f22aeSAriel Levkovich 			 IPPROTO_GRE);
2669da2f22aeSAriel Levkovich 
2670da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
2671a93b632cSMaor Gottlieb 			 ntohs(ib_spec->gre.mask.protocol));
2672da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2673da2f22aeSAriel Levkovich 			 ntohs(ib_spec->gre.val.protocol));
2674da2f22aeSAriel Levkovich 
2675da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
26765886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2677da2f22aeSAriel Levkovich 		       &ib_spec->gre.mask.key,
2678da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.mask.key));
2679da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
26805886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2681da2f22aeSAriel Levkovich 		       &ib_spec->gre.val.key,
2682da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.val.key));
2683da2f22aeSAriel Levkovich 		break;
268471c6e863SAriel Levkovich 	case IB_FLOW_SPEC_MPLS:
268571c6e863SAriel Levkovich 		switch (prev_type) {
268671c6e863SAriel Levkovich 		case IB_FLOW_SPEC_UDP:
268771c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
268871c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_udp),
268971c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
269071c6e863SAriel Levkovich 				return -EOPNOTSUPP;
269171c6e863SAriel Levkovich 
269271c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
269371c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
269471c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
269571c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
269671c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
269771c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
269871c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
269971c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
270071c6e863SAriel Levkovich 			break;
270171c6e863SAriel Levkovich 		case IB_FLOW_SPEC_GRE:
270271c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
270371c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_gre),
270471c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
270571c6e863SAriel Levkovich 				return -EOPNOTSUPP;
270671c6e863SAriel Levkovich 
270771c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
270871c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
270971c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
271071c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
271171c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
271271c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
271371c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
271471c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
271571c6e863SAriel Levkovich 			break;
271671c6e863SAriel Levkovich 		default:
271771c6e863SAriel Levkovich 			if (ib_spec->type & IB_FLOW_SPEC_INNER) {
271871c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
271971c6e863SAriel Levkovich 							   ft_field_support.inner_first_mpls),
272071c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
272171c6e863SAriel Levkovich 					return -EOPNOTSUPP;
272271c6e863SAriel Levkovich 
272371c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
272471c6e863SAriel Levkovich 						    inner_first_mpls),
272571c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
272671c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
272771c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
272871c6e863SAriel Levkovich 						    inner_first_mpls),
272971c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
273071c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
273171c6e863SAriel Levkovich 			} else {
273271c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
273371c6e863SAriel Levkovich 							   ft_field_support.outer_first_mpls),
273471c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
273571c6e863SAriel Levkovich 					return -EOPNOTSUPP;
273671c6e863SAriel Levkovich 
273771c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
273871c6e863SAriel Levkovich 						    outer_first_mpls),
273971c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
274071c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
274171c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
274271c6e863SAriel Levkovich 						    outer_first_mpls),
274371c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
274471c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
274571c6e863SAriel Levkovich 			}
274671c6e863SAriel Levkovich 		}
274771c6e863SAriel Levkovich 		break;
2748ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2749ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2750ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
27511ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2752ffb30d8fSMoses Reuben 
2753ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2754ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2755ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2756ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2757ffb30d8fSMoses Reuben 		break;
27582ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
27592ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
27602ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
27612ac693f9SMoses Reuben 			return -EOPNOTSUPP;
27622ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
27632ac693f9SMoses Reuben 			return -EINVAL;
27642ac693f9SMoses Reuben 
2765075572d4SBoris Pismenny 		action->flow_tag = ib_spec->flow_tag.tag_id;
2766d5634feeSPaul Blakey 		action->flags |= FLOW_ACT_HAS_TAG;
27672ac693f9SMoses Reuben 		break;
2768a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2769a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2770a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2771a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2772075572d4SBoris Pismenny 		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
2773a22ed86cSSlava Shwartsman 		break;
2774802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ACTION_HANDLE:
27752ea26203SMark Bloch 		ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
27762ea26203SMark Bloch 			flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
2777802c2125SAviad Yehezkel 		if (ret)
2778802c2125SAviad Yehezkel 			return ret;
2779802c2125SAviad Yehezkel 		break;
27803b3233fbSRaed Salem 	case IB_FLOW_SPEC_ACTION_COUNT:
27813b3233fbSRaed Salem 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
27823b3233fbSRaed Salem 					 LAST_COUNTERS_FIELD))
27833b3233fbSRaed Salem 			return -EOPNOTSUPP;
27843b3233fbSRaed Salem 
27853b3233fbSRaed Salem 		/* for now support only one counters spec per flow */
27863b3233fbSRaed Salem 		if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
27873b3233fbSRaed Salem 			return -EINVAL;
27883b3233fbSRaed Salem 
27893b3233fbSRaed Salem 		action->counters = ib_spec->flow_count.counters;
27903b3233fbSRaed Salem 		action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
27913b3233fbSRaed Salem 		break;
2792038d2ef8SMaor Gottlieb 	default:
2793038d2ef8SMaor Gottlieb 		return -EINVAL;
2794038d2ef8SMaor Gottlieb 	}
2795038d2ef8SMaor Gottlieb 
2796038d2ef8SMaor Gottlieb 	return 0;
2797038d2ef8SMaor Gottlieb }
2798038d2ef8SMaor Gottlieb 
2799038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
2800038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
2801038d2ef8SMaor Gottlieb  * could steal other multicast packets.
2802038d2ef8SMaor Gottlieb  */
2803a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2804038d2ef8SMaor Gottlieb {
280581e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
2806038d2ef8SMaor Gottlieb 
2807038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2808038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
2809038d2ef8SMaor Gottlieb 		return false;
2810038d2ef8SMaor Gottlieb 
281181e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
281281e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
281381e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
2814038d2ef8SMaor Gottlieb 
281581e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
281681e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
281781e30880SYishai Hadas 			return true;
281881e30880SYishai Hadas 
281981e30880SYishai Hadas 		return false;
282081e30880SYishai Hadas 	}
282181e30880SYishai Hadas 
282281e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
282381e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
282481e30880SYishai Hadas 
282581e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2826038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2827038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
2828038d2ef8SMaor Gottlieb 	}
2829038d2ef8SMaor Gottlieb 
283081e30880SYishai Hadas 	return false;
283181e30880SYishai Hadas }
283281e30880SYishai Hadas 
2833802c2125SAviad Yehezkel enum valid_spec {
2834802c2125SAviad Yehezkel 	VALID_SPEC_INVALID,
2835802c2125SAviad Yehezkel 	VALID_SPEC_VALID,
2836802c2125SAviad Yehezkel 	VALID_SPEC_NA,
2837802c2125SAviad Yehezkel };
2838802c2125SAviad Yehezkel 
2839802c2125SAviad Yehezkel static enum valid_spec
2840802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
2841802c2125SAviad Yehezkel 		     const struct mlx5_flow_spec *spec,
2842802c2125SAviad Yehezkel 		     const struct mlx5_flow_act *flow_act,
2843802c2125SAviad Yehezkel 		     bool egress)
2844802c2125SAviad Yehezkel {
2845802c2125SAviad Yehezkel 	const u32 *match_c = spec->match_criteria;
2846802c2125SAviad Yehezkel 	bool is_crypto =
2847802c2125SAviad Yehezkel 		(flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2848802c2125SAviad Yehezkel 				     MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
2849802c2125SAviad Yehezkel 	bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
2850802c2125SAviad Yehezkel 	bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
2851802c2125SAviad Yehezkel 
2852802c2125SAviad Yehezkel 	/*
2853802c2125SAviad Yehezkel 	 * Currently only crypto is supported in egress, when regular egress
2854802c2125SAviad Yehezkel 	 * rules would be supported, always return VALID_SPEC_NA.
2855802c2125SAviad Yehezkel 	 */
2856802c2125SAviad Yehezkel 	if (!is_crypto)
285778dd0c43SMark Bloch 		return VALID_SPEC_NA;
2858802c2125SAviad Yehezkel 
2859802c2125SAviad Yehezkel 	return is_crypto && is_ipsec &&
2860d5634feeSPaul Blakey 		(!egress || (!is_drop && !(flow_act->flags & FLOW_ACT_HAS_TAG))) ?
2861802c2125SAviad Yehezkel 		VALID_SPEC_VALID : VALID_SPEC_INVALID;
2862802c2125SAviad Yehezkel }
2863802c2125SAviad Yehezkel 
2864802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev,
2865802c2125SAviad Yehezkel 			  const struct mlx5_flow_spec *spec,
2866802c2125SAviad Yehezkel 			  const struct mlx5_flow_act *flow_act,
2867802c2125SAviad Yehezkel 			  bool egress)
2868802c2125SAviad Yehezkel {
2869802c2125SAviad Yehezkel 	/* We curretly only support ipsec egress flow */
2870802c2125SAviad Yehezkel 	return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
2871802c2125SAviad Yehezkel }
2872802c2125SAviad Yehezkel 
287319cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
287419cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
28750f750966SAriel Levkovich 			       bool check_inner)
2876038d2ef8SMaor Gottlieb {
2877038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
287819cc7524SAriel Levkovich 	int match_ipv = check_inner ?
287919cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
288019cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
288119cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
288219cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
28830f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
28840f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
28850f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
28860f750966SAriel Levkovich 	bool has_ethertype = false;
2887038d2ef8SMaor Gottlieb 	unsigned int spec_index;
28880f750966SAriel Levkovich 	bool mask_valid = true;
28890f750966SAriel Levkovich 	u16 eth_type = 0;
28900f750966SAriel Levkovich 	bool type_valid;
2891038d2ef8SMaor Gottlieb 
2892038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
2893038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
28940f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2895038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
28960f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
28970f750966SAriel Levkovich 				      htons(0xffff));
28980f750966SAriel Levkovich 			has_ethertype = true;
28990f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
29000f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
29010f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
29020f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
2903038d2ef8SMaor Gottlieb 		}
2904038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
2905038d2ef8SMaor Gottlieb 	}
29060f750966SAriel Levkovich 
29070f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
29080f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
29090f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
29100f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
29110f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
29120f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
291319cc7524SAriel Levkovich 
291419cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
291519cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
291619cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
29170f750966SAriel Levkovich 	}
29180f750966SAriel Levkovich 
29190f750966SAriel Levkovich 	return type_valid;
29200f750966SAriel Levkovich }
29210f750966SAriel Levkovich 
292219cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
292319cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
29240f750966SAriel Levkovich {
292519cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
292619cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
2927038d2ef8SMaor Gottlieb }
2928038d2ef8SMaor Gottlieb 
2929038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
2930038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
2931038d2ef8SMaor Gottlieb {
2932038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
2933038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
2934038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
2935038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
2936038d2ef8SMaor Gottlieb 	}
2937038d2ef8SMaor Gottlieb }
2938038d2ef8SMaor Gottlieb 
29393b3233fbSRaed Salem static void counters_clear_description(struct ib_counters *counters)
29403b3233fbSRaed Salem {
29413b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
29423b3233fbSRaed Salem 
29433b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
29443b3233fbSRaed Salem 	kfree(mcounters->counters_data);
29453b3233fbSRaed Salem 	mcounters->counters_data = NULL;
29463b3233fbSRaed Salem 	mcounters->cntrs_max_index = 0;
29473b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
29483b3233fbSRaed Salem }
29493b3233fbSRaed Salem 
2950038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2951038d2ef8SMaor Gottlieb {
2952038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2953038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2954038d2ef8SMaor Gottlieb 							  ibflow);
2955038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2956d4be3f44SYishai Hadas 	struct mlx5_ib_dev *dev = handler->dev;
2957038d2ef8SMaor Gottlieb 
29589a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
2959038d2ef8SMaor Gottlieb 
2960038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
296174491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2962cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
2963038d2ef8SMaor Gottlieb 		list_del(&iter->list);
2964038d2ef8SMaor Gottlieb 		kfree(iter);
2965038d2ef8SMaor Gottlieb 	}
2966038d2ef8SMaor Gottlieb 
296774491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
29685497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
29693b3233fbSRaed Salem 	if (handler->ibcounters &&
29703b3233fbSRaed Salem 	    atomic_read(&handler->ibcounters->usecnt) == 1)
29713b3233fbSRaed Salem 		counters_clear_description(handler->ibcounters);
2972038d2ef8SMaor Gottlieb 
29733b3233fbSRaed Salem 	mutex_unlock(&dev->flow_db->lock);
2974d4be3f44SYishai Hadas 	if (handler->flow_matcher)
2975d4be3f44SYishai Hadas 		atomic_dec(&handler->flow_matcher->usecnt);
2976038d2ef8SMaor Gottlieb 	kfree(handler);
2977038d2ef8SMaor Gottlieb 
2978038d2ef8SMaor Gottlieb 	return 0;
2979038d2ef8SMaor Gottlieb }
2980038d2ef8SMaor Gottlieb 
298135d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
298235d19011SMaor Gottlieb {
298335d19011SMaor Gottlieb 	priority *= 2;
298435d19011SMaor Gottlieb 	if (!dont_trap)
298535d19011SMaor Gottlieb 		priority++;
298635d19011SMaor Gottlieb 	return priority;
298735d19011SMaor Gottlieb }
298835d19011SMaor Gottlieb 
2989cc0e5d42SMaor Gottlieb enum flow_table_type {
2990cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
2991cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
2992cc0e5d42SMaor Gottlieb };
2993cc0e5d42SMaor Gottlieb 
299400b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
299500b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
2996d4be3f44SYishai Hadas 
2997d4be3f44SYishai Hadas static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
2998d4be3f44SYishai Hadas 					   struct mlx5_ib_flow_prio *prio,
2999d4be3f44SYishai Hadas 					   int priority,
30004adda112SMark Bloch 					   int num_entries, int num_groups,
30014adda112SMark Bloch 					   u32 flags)
3002d4be3f44SYishai Hadas {
3003d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft;
3004d4be3f44SYishai Hadas 
3005d4be3f44SYishai Hadas 	ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3006d4be3f44SYishai Hadas 						 num_entries,
3007d4be3f44SYishai Hadas 						 num_groups,
30084adda112SMark Bloch 						 0, flags);
3009d4be3f44SYishai Hadas 	if (IS_ERR(ft))
3010d4be3f44SYishai Hadas 		return ERR_CAST(ft);
3011d4be3f44SYishai Hadas 
3012d4be3f44SYishai Hadas 	prio->flow_table = ft;
3013d4be3f44SYishai Hadas 	prio->refcount = 0;
3014d4be3f44SYishai Hadas 	return prio;
3015d4be3f44SYishai Hadas }
3016d4be3f44SYishai Hadas 
3017038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
3018cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
3019cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
3020038d2ef8SMaor Gottlieb {
302135d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
3022038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
3023038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
3024038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
3025dac388efSMaor Gottlieb 	int max_table_size;
3026038d2ef8SMaor Gottlieb 	int num_entries;
3027038d2ef8SMaor Gottlieb 	int num_groups;
30284adda112SMark Bloch 	u32 flags = 0;
3029038d2ef8SMaor Gottlieb 	int priority;
3030038d2ef8SMaor Gottlieb 
3031dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3032dac388efSMaor Gottlieb 						       log_max_ft_size));
3033038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
303478dd0c43SMark Bloch 		enum mlx5_flow_namespace_type fn_type;
303578dd0c43SMark Bloch 
303678dd0c43SMark Bloch 		if (flow_is_multicast_only(flow_attr) &&
303735d19011SMaor Gottlieb 		    !dont_trap)
3038038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
3039038d2ef8SMaor Gottlieb 		else
304035d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
304135d19011SMaor Gottlieb 							dont_trap);
304278dd0c43SMark Bloch 		if (ft_type == MLX5_IB_FT_RX) {
304378dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
304478dd0c43SMark Bloch 			prio = &dev->flow_db->prios[priority];
30454adda112SMark Bloch 			if (!dev->rep &&
30464adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
30474adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
30485c2db53fSMark Bloch 			if (!dev->rep &&
30495c2db53fSMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
30505c2db53fSMark Bloch 					reformat_l3_tunnel_to_l2))
30515c2db53fSMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
305278dd0c43SMark Bloch 		} else {
305378dd0c43SMark Bloch 			max_table_size =
305478dd0c43SMark Bloch 				BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
305578dd0c43SMark Bloch 							      log_max_ft_size));
305678dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
305778dd0c43SMark Bloch 			prio = &dev->flow_db->egress_prios[priority];
30584adda112SMark Bloch 			if (!dev->rep &&
30594adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
30604adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
306178dd0c43SMark Bloch 		}
306278dd0c43SMark Bloch 		ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
3063038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
3064038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
3065038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3066038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3067038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
3068038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
3069038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
3070038d2ef8SMaor Gottlieb 					 &num_entries,
3071038d2ef8SMaor Gottlieb 					 &num_groups);
30729a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
3073cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3074cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3075cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
3076cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
3077cc0e5d42SMaor Gottlieb 
3078cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3079cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3080cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3081cc0e5d42SMaor Gottlieb 
30829a4ca38dSMark Bloch 		prio = &dev->flow_db->sniffer[ft_type];
3083cc0e5d42SMaor Gottlieb 		priority = 0;
3084cc0e5d42SMaor Gottlieb 		num_entries = 1;
3085cc0e5d42SMaor Gottlieb 		num_groups = 1;
3086038d2ef8SMaor Gottlieb 	}
3087038d2ef8SMaor Gottlieb 
3088038d2ef8SMaor Gottlieb 	if (!ns)
3089038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
3090038d2ef8SMaor Gottlieb 
3091dac388efSMaor Gottlieb 	if (num_entries > max_table_size)
3092dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
3093dac388efSMaor Gottlieb 
3094038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
3095d4be3f44SYishai Hadas 	if (!ft)
30964adda112SMark Bloch 		return _get_prio(ns, prio, priority, num_entries, num_groups,
30974adda112SMark Bloch 				 flags);
3098038d2ef8SMaor Gottlieb 
3099d4be3f44SYishai Hadas 	return prio;
3100038d2ef8SMaor Gottlieb }
3101038d2ef8SMaor Gottlieb 
3102a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
3103a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
3104a550ddfcSYishai Hadas 			    u32 underlay_qpn)
3105a550ddfcSYishai Hadas {
3106a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3107a550ddfcSYishai Hadas 					   spec->match_criteria,
3108a550ddfcSYishai Hadas 					   misc_parameters);
3109a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3110a550ddfcSYishai Hadas 					   misc_parameters);
3111a550ddfcSYishai Hadas 
3112a550ddfcSYishai Hadas 	if (underlay_qpn &&
3113a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3114a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
3115a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3116a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
3117a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3118a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
3119a550ddfcSYishai Hadas 	}
3120a550ddfcSYishai Hadas }
3121a550ddfcSYishai Hadas 
31225e95af5fSRaed Salem static int read_flow_counters(struct ib_device *ibdev,
31235e95af5fSRaed Salem 			      struct mlx5_read_counters_attr *read_attr)
31245e95af5fSRaed Salem {
31255e95af5fSRaed Salem 	struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
31265e95af5fSRaed Salem 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
31275e95af5fSRaed Salem 
31285e95af5fSRaed Salem 	return mlx5_fc_query(dev->mdev, fc,
31295e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_PACKETS],
31305e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_BYTES]);
31315e95af5fSRaed Salem }
31325e95af5fSRaed Salem 
31335e95af5fSRaed Salem /* flow counters currently expose two counters packets and bytes */
31345e95af5fSRaed Salem #define FLOW_COUNTERS_NUM 2
31353b3233fbSRaed Salem static int counters_set_description(struct ib_counters *counters,
31363b3233fbSRaed Salem 				    enum mlx5_ib_counters_type counters_type,
31373b3233fbSRaed Salem 				    struct mlx5_ib_flow_counters_desc *desc_data,
31383b3233fbSRaed Salem 				    u32 ncounters)
31393b3233fbSRaed Salem {
31403b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
31413b3233fbSRaed Salem 	u32 cntrs_max_index = 0;
31423b3233fbSRaed Salem 	int i;
31433b3233fbSRaed Salem 
31443b3233fbSRaed Salem 	if (counters_type != MLX5_IB_COUNTERS_FLOW)
31453b3233fbSRaed Salem 		return -EINVAL;
31463b3233fbSRaed Salem 
31473b3233fbSRaed Salem 	/* init the fields for the object */
31483b3233fbSRaed Salem 	mcounters->type = counters_type;
31495e95af5fSRaed Salem 	mcounters->read_counters = read_flow_counters;
31505e95af5fSRaed Salem 	mcounters->counters_num = FLOW_COUNTERS_NUM;
31513b3233fbSRaed Salem 	mcounters->ncounters = ncounters;
31523b3233fbSRaed Salem 	/* each counter entry have both description and index pair */
31533b3233fbSRaed Salem 	for (i = 0; i < ncounters; i++) {
31543b3233fbSRaed Salem 		if (desc_data[i].description > IB_COUNTER_BYTES)
31553b3233fbSRaed Salem 			return -EINVAL;
31563b3233fbSRaed Salem 
31573b3233fbSRaed Salem 		if (cntrs_max_index <= desc_data[i].index)
31583b3233fbSRaed Salem 			cntrs_max_index = desc_data[i].index + 1;
31593b3233fbSRaed Salem 	}
31603b3233fbSRaed Salem 
31613b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
31623b3233fbSRaed Salem 	mcounters->counters_data = desc_data;
31633b3233fbSRaed Salem 	mcounters->cntrs_max_index = cntrs_max_index;
31643b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
31653b3233fbSRaed Salem 
31663b3233fbSRaed Salem 	return 0;
31673b3233fbSRaed Salem }
31683b3233fbSRaed Salem 
31693b3233fbSRaed Salem #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
31703b3233fbSRaed Salem static int flow_counters_set_data(struct ib_counters *ibcounters,
31713b3233fbSRaed Salem 				  struct mlx5_ib_create_flow *ucmd)
31723b3233fbSRaed Salem {
31733b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
31743b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
31753b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc_data = NULL;
31763b3233fbSRaed Salem 	bool hw_hndl = false;
31773b3233fbSRaed Salem 	int ret = 0;
31783b3233fbSRaed Salem 
31793b3233fbSRaed Salem 	if (ucmd && ucmd->ncounters_data != 0) {
31803b3233fbSRaed Salem 		cntrs_data = ucmd->data;
31813b3233fbSRaed Salem 		if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
31823b3233fbSRaed Salem 			return -EINVAL;
31833b3233fbSRaed Salem 
31843b3233fbSRaed Salem 		desc_data = kcalloc(cntrs_data->ncounters,
31853b3233fbSRaed Salem 				    sizeof(*desc_data),
31863b3233fbSRaed Salem 				    GFP_KERNEL);
31873b3233fbSRaed Salem 		if (!desc_data)
31883b3233fbSRaed Salem 			return  -ENOMEM;
31893b3233fbSRaed Salem 
31903b3233fbSRaed Salem 		if (copy_from_user(desc_data,
31913b3233fbSRaed Salem 				   u64_to_user_ptr(cntrs_data->counters_data),
31923b3233fbSRaed Salem 				   sizeof(*desc_data) * cntrs_data->ncounters)) {
31933b3233fbSRaed Salem 			ret = -EFAULT;
31943b3233fbSRaed Salem 			goto free;
31953b3233fbSRaed Salem 		}
31963b3233fbSRaed Salem 	}
31973b3233fbSRaed Salem 
31983b3233fbSRaed Salem 	if (!mcounters->hw_cntrs_hndl) {
31993b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = mlx5_fc_create(
32003b3233fbSRaed Salem 			to_mdev(ibcounters->device)->mdev, false);
3201e31abf76Sweiyongjun (A) 		if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3202e31abf76Sweiyongjun (A) 			ret = PTR_ERR(mcounters->hw_cntrs_hndl);
32033b3233fbSRaed Salem 			goto free;
32043b3233fbSRaed Salem 		}
32053b3233fbSRaed Salem 		hw_hndl = true;
32063b3233fbSRaed Salem 	}
32073b3233fbSRaed Salem 
32083b3233fbSRaed Salem 	if (desc_data) {
32093b3233fbSRaed Salem 		/* counters already bound to at least one flow */
32103b3233fbSRaed Salem 		if (mcounters->cntrs_max_index) {
32113b3233fbSRaed Salem 			ret = -EINVAL;
32123b3233fbSRaed Salem 			goto free_hndl;
32133b3233fbSRaed Salem 		}
32143b3233fbSRaed Salem 
32153b3233fbSRaed Salem 		ret = counters_set_description(ibcounters,
32163b3233fbSRaed Salem 					       MLX5_IB_COUNTERS_FLOW,
32173b3233fbSRaed Salem 					       desc_data,
32183b3233fbSRaed Salem 					       cntrs_data->ncounters);
32193b3233fbSRaed Salem 		if (ret)
32203b3233fbSRaed Salem 			goto free_hndl;
32213b3233fbSRaed Salem 
32223b3233fbSRaed Salem 	} else if (!mcounters->cntrs_max_index) {
32233b3233fbSRaed Salem 		/* counters not bound yet, must have udata passed */
32243b3233fbSRaed Salem 		ret = -EINVAL;
32253b3233fbSRaed Salem 		goto free_hndl;
32263b3233fbSRaed Salem 	}
32273b3233fbSRaed Salem 
32283b3233fbSRaed Salem 	return 0;
32293b3233fbSRaed Salem 
32303b3233fbSRaed Salem free_hndl:
32313b3233fbSRaed Salem 	if (hw_hndl) {
32323b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
32333b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
32343b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = NULL;
32353b3233fbSRaed Salem 	}
32363b3233fbSRaed Salem free:
32373b3233fbSRaed Salem 	kfree(desc_data);
32383b3233fbSRaed Salem 	return ret;
32393b3233fbSRaed Salem }
32403b3233fbSRaed Salem 
3241a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3242038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
3243dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
3244a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
32453b3233fbSRaed Salem 						      u32 underlay_qpn,
32463b3233fbSRaed Salem 						      struct mlx5_ib_create_flow *ucmd)
3247038d2ef8SMaor Gottlieb {
3248038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
3249038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
3250075572d4SBoris Pismenny 	struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
3251c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
32523b3233fbSRaed Salem 	struct mlx5_flow_destination dest_arr[2] = {};
32533b3233fbSRaed Salem 	struct mlx5_flow_destination *rule_dst = dest_arr;
3254dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3255038d2ef8SMaor Gottlieb 	unsigned int spec_index;
325671c6e863SAriel Levkovich 	u32 prev_type = 0;
3257038d2ef8SMaor Gottlieb 	int err = 0;
32583b3233fbSRaed Salem 	int dest_num = 0;
3259802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3260038d2ef8SMaor Gottlieb 
326119cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
3262038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3263038d2ef8SMaor Gottlieb 
326478dd0c43SMark Bloch 	if (dev->rep && is_egress)
326578dd0c43SMark Bloch 		return ERR_PTR(-EINVAL);
326678dd0c43SMark Bloch 
32671b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3268038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3269c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
3270038d2ef8SMaor Gottlieb 		err = -ENOMEM;
3271038d2ef8SMaor Gottlieb 		goto free;
3272038d2ef8SMaor Gottlieb 	}
3273038d2ef8SMaor Gottlieb 
3274038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
32753b3233fbSRaed Salem 	if (dst) {
32763b3233fbSRaed Salem 		memcpy(&dest_arr[0], dst, sizeof(*dst));
32773b3233fbSRaed Salem 		dest_num++;
32783b3233fbSRaed Salem 	}
3279038d2ef8SMaor Gottlieb 
3280038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
328119cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
3282a22ed86cSSlava Shwartsman 				      spec->match_value,
328371c6e863SAriel Levkovich 				      ib_flow, flow_attr, &flow_act,
328471c6e863SAriel Levkovich 				      prev_type);
3285038d2ef8SMaor Gottlieb 		if (err < 0)
3286038d2ef8SMaor Gottlieb 			goto free;
3287038d2ef8SMaor Gottlieb 
328871c6e863SAriel Levkovich 		prev_type = ((union ib_flow_spec *)ib_flow)->type;
3289038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3290038d2ef8SMaor Gottlieb 	}
3291038d2ef8SMaor Gottlieb 
3292a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
3293a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
3294a550ddfcSYishai Hadas 
3295018a94eeSMark Bloch 	if (dev->rep) {
3296018a94eeSMark Bloch 		void *misc;
3297018a94eeSMark Bloch 
3298018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3299018a94eeSMark Bloch 				    misc_parameters);
3300018a94eeSMark Bloch 		MLX5_SET(fte_match_set_misc, misc, source_port,
3301018a94eeSMark Bloch 			 dev->rep->vport);
3302018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3303018a94eeSMark Bloch 				    misc_parameters);
3304018a94eeSMark Bloch 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3305018a94eeSMark Bloch 	}
3306018a94eeSMark Bloch 
3307466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3308802c2125SAviad Yehezkel 
3309802c2125SAviad Yehezkel 	if (is_egress &&
3310802c2125SAviad Yehezkel 	    !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3311802c2125SAviad Yehezkel 		err = -EINVAL;
3312802c2125SAviad Yehezkel 		goto free;
3313802c2125SAviad Yehezkel 	}
3314802c2125SAviad Yehezkel 
33153b3233fbSRaed Salem 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3316171c7625SMark Bloch 		struct mlx5_ib_mcounters *mcounters;
3317171c7625SMark Bloch 
33183b3233fbSRaed Salem 		err = flow_counters_set_data(flow_act.counters, ucmd);
33193b3233fbSRaed Salem 		if (err)
33203b3233fbSRaed Salem 			goto free;
33213b3233fbSRaed Salem 
3322171c7625SMark Bloch 		mcounters = to_mcounters(flow_act.counters);
33233b3233fbSRaed Salem 		handler->ibcounters = flow_act.counters;
33243b3233fbSRaed Salem 		dest_arr[dest_num].type =
33253b3233fbSRaed Salem 			MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3326171c7625SMark Bloch 		dest_arr[dest_num].counter_id =
3327171c7625SMark Bloch 			mlx5_fc_id(mcounters->hw_cntrs_hndl);
33283b3233fbSRaed Salem 		dest_num++;
33293b3233fbSRaed Salem 	}
33303b3233fbSRaed Salem 
3331075572d4SBoris Pismenny 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
33323b3233fbSRaed Salem 		if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3333a22ed86cSSlava Shwartsman 			rule_dst = NULL;
3334a22ed86cSSlava Shwartsman 			dest_num = 0;
33353b3233fbSRaed Salem 		}
3336a22ed86cSSlava Shwartsman 	} else {
3337802c2125SAviad Yehezkel 		if (is_egress)
3338802c2125SAviad Yehezkel 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3339802c2125SAviad Yehezkel 		else
3340802c2125SAviad Yehezkel 			flow_act.action |=
33413b3233fbSRaed Salem 				dest_num ?  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
334235d19011SMaor Gottlieb 					MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3343a22ed86cSSlava Shwartsman 	}
33442ac693f9SMoses Reuben 
3345d5634feeSPaul Blakey 	if ((flow_act.flags & FLOW_ACT_HAS_TAG)  &&
33462ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
33472ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
33482ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3349075572d4SBoris Pismenny 			     flow_act.flow_tag, flow_attr->type);
33502ac693f9SMoses Reuben 		err = -EINVAL;
33512ac693f9SMoses Reuben 		goto free;
33522ac693f9SMoses Reuben 	}
335374491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
335466958ed9SHadar Hen Zion 					    &flow_act,
3355a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
3356038d2ef8SMaor Gottlieb 
3357038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
3358038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
3359038d2ef8SMaor Gottlieb 		goto free;
3360038d2ef8SMaor Gottlieb 	}
3361038d2ef8SMaor Gottlieb 
3362d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
33635497adc6SMaor Gottlieb 	handler->prio = ft_prio;
3364d4be3f44SYishai Hadas 	handler->dev = dev;
3365038d2ef8SMaor Gottlieb 
3366038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
3367038d2ef8SMaor Gottlieb free:
33683b3233fbSRaed Salem 	if (err && handler) {
33693b3233fbSRaed Salem 		if (handler->ibcounters &&
33703b3233fbSRaed Salem 		    atomic_read(&handler->ibcounters->usecnt) == 1)
33713b3233fbSRaed Salem 			counters_clear_description(handler->ibcounters);
3372038d2ef8SMaor Gottlieb 		kfree(handler);
33733b3233fbSRaed Salem 	}
3374c5bb1730SMaor Gottlieb 	kvfree(spec);
3375038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
3376038d2ef8SMaor Gottlieb }
3377038d2ef8SMaor Gottlieb 
3378a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3379a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
3380a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
3381a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
3382a550ddfcSYishai Hadas {
33833b3233fbSRaed Salem 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
3384a550ddfcSYishai Hadas }
3385a550ddfcSYishai Hadas 
338635d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
338735d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
338835d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
338935d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
339035d19011SMaor Gottlieb {
339135d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
339235d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
339335d19011SMaor Gottlieb 
339435d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
339535d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
339635d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
339735d19011SMaor Gottlieb 					       flow_attr, dst);
339835d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
339974491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3400d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
340135d19011SMaor Gottlieb 			kfree(handler);
340235d19011SMaor Gottlieb 			handler = handler_dst;
340335d19011SMaor Gottlieb 		} else {
340435d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
340535d19011SMaor Gottlieb 		}
340635d19011SMaor Gottlieb 	}
340735d19011SMaor Gottlieb 
340835d19011SMaor Gottlieb 	return handler;
340935d19011SMaor Gottlieb }
3410038d2ef8SMaor Gottlieb enum {
3411038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
3412038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
3413038d2ef8SMaor Gottlieb };
3414038d2ef8SMaor Gottlieb 
3415038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3416038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
3417038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
3418038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
3419038d2ef8SMaor Gottlieb {
3420038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
3421038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3422038d2ef8SMaor Gottlieb 
3423038d2ef8SMaor Gottlieb 	static struct {
3424038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
3425038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
3426038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
3427038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
3428038d2ef8SMaor Gottlieb 			.flow_attr = {
3429038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3430038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3431038d2ef8SMaor Gottlieb 			},
3432038d2ef8SMaor Gottlieb 			.eth_flow = {
3433038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3434038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3435038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3436038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
3437038d2ef8SMaor Gottlieb 			}
3438038d2ef8SMaor Gottlieb 		},
3439038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
3440038d2ef8SMaor Gottlieb 			.flow_attr = {
3441038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3442038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3443038d2ef8SMaor Gottlieb 			},
3444038d2ef8SMaor Gottlieb 			.eth_flow = {
3445038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3446038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3447038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3448038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
3449038d2ef8SMaor Gottlieb 			}
3450038d2ef8SMaor Gottlieb 		}
3451038d2ef8SMaor Gottlieb 	};
3452038d2ef8SMaor Gottlieb 
3453038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
3454038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
3455038d2ef8SMaor Gottlieb 				   dst);
3456038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
3457038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3458038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
3459038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3460038d2ef8SMaor Gottlieb 						 dst);
3461038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
346274491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3463d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
3464038d2ef8SMaor Gottlieb 			kfree(handler);
3465038d2ef8SMaor Gottlieb 			handler = handler_ucast;
3466038d2ef8SMaor Gottlieb 		} else {
3467038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
3468038d2ef8SMaor Gottlieb 		}
3469038d2ef8SMaor Gottlieb 	}
3470038d2ef8SMaor Gottlieb 
3471038d2ef8SMaor Gottlieb 	return handler;
3472038d2ef8SMaor Gottlieb }
3473038d2ef8SMaor Gottlieb 
3474cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3475cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
3476cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
3477cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
3478cc0e5d42SMaor Gottlieb {
3479cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
3480cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
3481cc0e5d42SMaor Gottlieb 	int err;
3482cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
3483cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
3484cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
3485cc0e5d42SMaor Gottlieb 	};
3486cc0e5d42SMaor Gottlieb 
3487cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3488cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
3489cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
3490cc0e5d42SMaor Gottlieb 		goto err;
3491cc0e5d42SMaor Gottlieb 	}
3492cc0e5d42SMaor Gottlieb 
3493cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3494cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
3495cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
3496cc0e5d42SMaor Gottlieb 		goto err_tx;
3497cc0e5d42SMaor Gottlieb 	}
3498cc0e5d42SMaor Gottlieb 
3499cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
3500cc0e5d42SMaor Gottlieb 
3501cc0e5d42SMaor Gottlieb 	return handler_rx;
3502cc0e5d42SMaor Gottlieb 
3503cc0e5d42SMaor Gottlieb err_tx:
350474491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
3505cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
3506cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
3507cc0e5d42SMaor Gottlieb err:
3508cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
3509cc0e5d42SMaor Gottlieb }
3510cc0e5d42SMaor Gottlieb 
3511038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3512038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
351359082a32SMatan Barak 					   int domain,
351459082a32SMatan Barak 					   struct ib_udata *udata)
3515038d2ef8SMaor Gottlieb {
3516038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3517d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
3518038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3519038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
3520cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3521038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
3522802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
35233b3233fbSRaed Salem 	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
35243b3233fbSRaed Salem 	size_t min_ucmd_sz, required_ucmd_sz;
3525038d2ef8SMaor Gottlieb 	int err;
3526a550ddfcSYishai Hadas 	int underlay_qpn;
3527038d2ef8SMaor Gottlieb 
35283b3233fbSRaed Salem 	if (udata && udata->inlen) {
35293b3233fbSRaed Salem 		min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
35303b3233fbSRaed Salem 				sizeof(ucmd_hdr.reserved);
35313b3233fbSRaed Salem 		if (udata->inlen < min_ucmd_sz)
353259082a32SMatan Barak 			return ERR_PTR(-EOPNOTSUPP);
353359082a32SMatan Barak 
35343b3233fbSRaed Salem 		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
35353b3233fbSRaed Salem 		if (err)
35363b3233fbSRaed Salem 			return ERR_PTR(err);
35373b3233fbSRaed Salem 
35383b3233fbSRaed Salem 		/* currently supports only one counters data */
35393b3233fbSRaed Salem 		if (ucmd_hdr.ncounters_data > 1)
35403b3233fbSRaed Salem 			return ERR_PTR(-EINVAL);
35413b3233fbSRaed Salem 
35423b3233fbSRaed Salem 		required_ucmd_sz = min_ucmd_sz +
35433b3233fbSRaed Salem 			sizeof(struct mlx5_ib_flow_counters_data) *
35443b3233fbSRaed Salem 			ucmd_hdr.ncounters_data;
35453b3233fbSRaed Salem 		if (udata->inlen > required_ucmd_sz &&
35463b3233fbSRaed Salem 		    !ib_is_udata_cleared(udata, required_ucmd_sz,
35473b3233fbSRaed Salem 					 udata->inlen - required_ucmd_sz))
35483b3233fbSRaed Salem 			return ERR_PTR(-EOPNOTSUPP);
35493b3233fbSRaed Salem 
35503b3233fbSRaed Salem 		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
35513b3233fbSRaed Salem 		if (!ucmd)
35523b3233fbSRaed Salem 			return ERR_PTR(-ENOMEM);
35533b3233fbSRaed Salem 
35543b3233fbSRaed Salem 		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
3555299eafeeSGustavo A. R. Silva 		if (err)
3556299eafeeSGustavo A. R. Silva 			goto free_ucmd;
35573b3233fbSRaed Salem 	}
35583b3233fbSRaed Salem 
3559299eafeeSGustavo A. R. Silva 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3560299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3561299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3562299eafeeSGustavo A. R. Silva 	}
3563038d2ef8SMaor Gottlieb 
3564038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
3565508562d6SDaniel Jurgens 	    flow_attr->port > dev->num_ports ||
3566802c2125SAviad Yehezkel 	    (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3567299eafeeSGustavo A. R. Silva 				  IB_FLOW_ATTR_FLAGS_EGRESS))) {
3568299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3569299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3570299eafeeSGustavo A. R. Silva 	}
3571802c2125SAviad Yehezkel 
3572802c2125SAviad Yehezkel 	if (is_egress &&
3573802c2125SAviad Yehezkel 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3574299eafeeSGustavo A. R. Silva 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3575299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3576299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3577299eafeeSGustavo A. R. Silva 	}
3578038d2ef8SMaor Gottlieb 
3579038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3580299eafeeSGustavo A. R. Silva 	if (!dst) {
3581299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3582299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3583299eafeeSGustavo A. R. Silva 	}
3584038d2ef8SMaor Gottlieb 
35859a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3586038d2ef8SMaor Gottlieb 
3587802c2125SAviad Yehezkel 	ft_prio = get_flow_table(dev, flow_attr,
3588802c2125SAviad Yehezkel 				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3589038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
3590038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
3591038d2ef8SMaor Gottlieb 		goto unlock;
3592038d2ef8SMaor Gottlieb 	}
3593cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3594cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3595cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
3596cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
3597cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
3598cc0e5d42SMaor Gottlieb 			goto destroy_ft;
3599cc0e5d42SMaor Gottlieb 		}
3600cc0e5d42SMaor Gottlieb 	}
3601038d2ef8SMaor Gottlieb 
3602802c2125SAviad Yehezkel 	if (is_egress) {
3603802c2125SAviad Yehezkel 		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3604802c2125SAviad Yehezkel 	} else {
3605038d2ef8SMaor Gottlieb 		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3606d9f88e5aSYishai Hadas 		if (mqp->flags & MLX5_IB_QP_RSS)
3607d9f88e5aSYishai Hadas 			dst->tir_num = mqp->rss_qp.tirn;
3608d9f88e5aSYishai Hadas 		else
3609d9f88e5aSYishai Hadas 			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3610802c2125SAviad Yehezkel 	}
3611038d2ef8SMaor Gottlieb 
3612038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
361335d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
361435d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
361535d19011SMaor Gottlieb 							flow_attr, dst);
361635d19011SMaor Gottlieb 		} else {
3617a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3618a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
3619a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
36203b3233fbSRaed Salem 						    dst, underlay_qpn, ucmd);
362135d19011SMaor Gottlieb 		}
3622038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3623038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3624038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3625038d2ef8SMaor Gottlieb 						dst);
3626cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3627cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3628038d2ef8SMaor Gottlieb 	} else {
3629038d2ef8SMaor Gottlieb 		err = -EINVAL;
3630038d2ef8SMaor Gottlieb 		goto destroy_ft;
3631038d2ef8SMaor Gottlieb 	}
3632038d2ef8SMaor Gottlieb 
3633038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
3634038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
3635038d2ef8SMaor Gottlieb 		handler = NULL;
3636038d2ef8SMaor Gottlieb 		goto destroy_ft;
3637038d2ef8SMaor Gottlieb 	}
3638038d2ef8SMaor Gottlieb 
36399a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3640038d2ef8SMaor Gottlieb 	kfree(dst);
36413b3233fbSRaed Salem 	kfree(ucmd);
3642038d2ef8SMaor Gottlieb 
3643038d2ef8SMaor Gottlieb 	return &handler->ibflow;
3644038d2ef8SMaor Gottlieb 
3645038d2ef8SMaor Gottlieb destroy_ft:
3646038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
3647cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
3648cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
3649038d2ef8SMaor Gottlieb unlock:
36509a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3651038d2ef8SMaor Gottlieb 	kfree(dst);
3652299eafeeSGustavo A. R. Silva free_ucmd:
36533b3233fbSRaed Salem 	kfree(ucmd);
3654038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
3655038d2ef8SMaor Gottlieb }
3656038d2ef8SMaor Gottlieb 
3657b47fd4ffSMark Bloch static struct mlx5_ib_flow_prio *
3658b47fd4ffSMark Bloch _get_flow_table(struct mlx5_ib_dev *dev,
3659b47fd4ffSMark Bloch 		struct mlx5_ib_flow_matcher *fs_matcher,
3660b47fd4ffSMark Bloch 		bool mcast)
3661d4be3f44SYishai Hadas {
3662d4be3f44SYishai Hadas 	struct mlx5_flow_namespace *ns = NULL;
3663d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *prio;
3664b47fd4ffSMark Bloch 	int max_table_size;
3665b47fd4ffSMark Bloch 	u32 flags = 0;
3666b47fd4ffSMark Bloch 	int priority;
3667d4be3f44SYishai Hadas 
3668b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
3669d4be3f44SYishai Hadas 		max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3670d4be3f44SYishai Hadas 					log_max_ft_size));
3671b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3672b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3673b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3674b47fd4ffSMark Bloch 					      reformat_l3_tunnel_to_l2))
3675b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3676b47fd4ffSMark Bloch 	} else { /* Can only be MLX5_FLOW_NAMESPACE_EGRESS */
3677b47fd4ffSMark Bloch 		max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3678b47fd4ffSMark Bloch 					log_max_ft_size));
3679b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3680b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3681b47fd4ffSMark Bloch 	}
3682b47fd4ffSMark Bloch 
3683d4be3f44SYishai Hadas 	if (max_table_size < MLX5_FS_MAX_ENTRIES)
3684d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
3685d4be3f44SYishai Hadas 
3686d4be3f44SYishai Hadas 	if (mcast)
3687d4be3f44SYishai Hadas 		priority = MLX5_IB_FLOW_MCAST_PRIO;
3688d4be3f44SYishai Hadas 	else
3689b47fd4ffSMark Bloch 		priority = ib_prio_to_core_prio(fs_matcher->priority, false);
3690d4be3f44SYishai Hadas 
3691b47fd4ffSMark Bloch 	ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
3692d4be3f44SYishai Hadas 	if (!ns)
3693d4be3f44SYishai Hadas 		return ERR_PTR(-ENOTSUPP);
3694d4be3f44SYishai Hadas 
3695b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
3696d4be3f44SYishai Hadas 		prio = &dev->flow_db->prios[priority];
3697b47fd4ffSMark Bloch 	else
3698b47fd4ffSMark Bloch 		prio = &dev->flow_db->egress_prios[priority];
3699d4be3f44SYishai Hadas 
3700d4be3f44SYishai Hadas 	if (prio->flow_table)
3701d4be3f44SYishai Hadas 		return prio;
3702d4be3f44SYishai Hadas 
3703d4be3f44SYishai Hadas 	return _get_prio(ns, prio, priority, MLX5_FS_MAX_ENTRIES,
3704b47fd4ffSMark Bloch 			 MLX5_FS_MAX_TYPES, flags);
3705d4be3f44SYishai Hadas }
3706d4be3f44SYishai Hadas 
3707d4be3f44SYishai Hadas static struct mlx5_ib_flow_handler *
3708d4be3f44SYishai Hadas _create_raw_flow_rule(struct mlx5_ib_dev *dev,
3709d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_prio *ft_prio,
3710d4be3f44SYishai Hadas 		      struct mlx5_flow_destination *dst,
3711d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_matcher  *fs_matcher,
3712b823dd6dSMark Bloch 		      struct mlx5_flow_act *flow_act,
3713d4be3f44SYishai Hadas 		      void *cmd_in, int inlen)
3714d4be3f44SYishai Hadas {
3715d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
3716d4be3f44SYishai Hadas 	struct mlx5_flow_spec *spec;
3717d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft = ft_prio->flow_table;
3718d4be3f44SYishai Hadas 	int err = 0;
3719d4be3f44SYishai Hadas 
3720d4be3f44SYishai Hadas 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3721d4be3f44SYishai Hadas 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3722d4be3f44SYishai Hadas 	if (!handler || !spec) {
3723d4be3f44SYishai Hadas 		err = -ENOMEM;
3724d4be3f44SYishai Hadas 		goto free;
3725d4be3f44SYishai Hadas 	}
3726d4be3f44SYishai Hadas 
3727d4be3f44SYishai Hadas 	INIT_LIST_HEAD(&handler->list);
3728d4be3f44SYishai Hadas 
3729d4be3f44SYishai Hadas 	memcpy(spec->match_value, cmd_in, inlen);
3730d4be3f44SYishai Hadas 	memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
3731d4be3f44SYishai Hadas 	       fs_matcher->mask_len);
3732d4be3f44SYishai Hadas 	spec->match_criteria_enable = fs_matcher->match_criteria_enable;
3733d4be3f44SYishai Hadas 
3734d4be3f44SYishai Hadas 	handler->rule = mlx5_add_flow_rules(ft, spec,
3735b823dd6dSMark Bloch 					    flow_act, dst, 1);
3736d4be3f44SYishai Hadas 
3737d4be3f44SYishai Hadas 	if (IS_ERR(handler->rule)) {
3738d4be3f44SYishai Hadas 		err = PTR_ERR(handler->rule);
3739d4be3f44SYishai Hadas 		goto free;
3740d4be3f44SYishai Hadas 	}
3741d4be3f44SYishai Hadas 
3742d4be3f44SYishai Hadas 	ft_prio->refcount++;
3743d4be3f44SYishai Hadas 	handler->prio = ft_prio;
3744d4be3f44SYishai Hadas 	handler->dev = dev;
3745d4be3f44SYishai Hadas 	ft_prio->flow_table = ft;
3746d4be3f44SYishai Hadas 
3747d4be3f44SYishai Hadas free:
3748d4be3f44SYishai Hadas 	if (err)
3749d4be3f44SYishai Hadas 		kfree(handler);
3750d4be3f44SYishai Hadas 	kvfree(spec);
3751d4be3f44SYishai Hadas 	return err ? ERR_PTR(err) : handler;
3752d4be3f44SYishai Hadas }
3753d4be3f44SYishai Hadas 
3754d4be3f44SYishai Hadas static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
3755d4be3f44SYishai Hadas 				void *match_v)
3756d4be3f44SYishai Hadas {
3757d4be3f44SYishai Hadas 	void *match_c;
3758d4be3f44SYishai Hadas 	void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
3759d4be3f44SYishai Hadas 	void *dmac, *dmac_mask;
3760d4be3f44SYishai Hadas 	void *ipv4, *ipv4_mask;
3761d4be3f44SYishai Hadas 
3762d4be3f44SYishai Hadas 	if (!(fs_matcher->match_criteria_enable &
3763d4be3f44SYishai Hadas 	      (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
3764d4be3f44SYishai Hadas 		return false;
3765d4be3f44SYishai Hadas 
3766d4be3f44SYishai Hadas 	match_c = fs_matcher->matcher_mask.match_params;
3767d4be3f44SYishai Hadas 	match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
3768d4be3f44SYishai Hadas 					   outer_headers);
3769d4be3f44SYishai Hadas 	match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
3770d4be3f44SYishai Hadas 					   outer_headers);
3771d4be3f44SYishai Hadas 
3772d4be3f44SYishai Hadas 	dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3773d4be3f44SYishai Hadas 			    dmac_47_16);
3774d4be3f44SYishai Hadas 	dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3775d4be3f44SYishai Hadas 				 dmac_47_16);
3776d4be3f44SYishai Hadas 
3777d4be3f44SYishai Hadas 	if (is_multicast_ether_addr(dmac) &&
3778d4be3f44SYishai Hadas 	    is_multicast_ether_addr(dmac_mask))
3779d4be3f44SYishai Hadas 		return true;
3780d4be3f44SYishai Hadas 
3781d4be3f44SYishai Hadas 	ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3782d4be3f44SYishai Hadas 			    dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3783d4be3f44SYishai Hadas 
3784d4be3f44SYishai Hadas 	ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3785d4be3f44SYishai Hadas 				 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3786d4be3f44SYishai Hadas 
3787d4be3f44SYishai Hadas 	if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
3788d4be3f44SYishai Hadas 	    ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
3789d4be3f44SYishai Hadas 		return true;
3790d4be3f44SYishai Hadas 
3791d4be3f44SYishai Hadas 	return false;
3792d4be3f44SYishai Hadas }
3793d4be3f44SYishai Hadas 
379432269441SYishai Hadas struct mlx5_ib_flow_handler *
379532269441SYishai Hadas mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
379632269441SYishai Hadas 			struct mlx5_ib_flow_matcher *fs_matcher,
3797b823dd6dSMark Bloch 			struct mlx5_flow_act *flow_act,
379832269441SYishai Hadas 			void *cmd_in, int inlen, int dest_id,
379932269441SYishai Hadas 			int dest_type)
380032269441SYishai Hadas {
3801d4be3f44SYishai Hadas 	struct mlx5_flow_destination *dst;
3802d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *ft_prio;
3803d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
3804d4be3f44SYishai Hadas 	bool mcast;
3805d4be3f44SYishai Hadas 	int err;
3806d4be3f44SYishai Hadas 
3807d4be3f44SYishai Hadas 	if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
380832269441SYishai Hadas 		return ERR_PTR(-EOPNOTSUPP);
3809d4be3f44SYishai Hadas 
3810d4be3f44SYishai Hadas 	if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
3811d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
3812d4be3f44SYishai Hadas 
3813d4be3f44SYishai Hadas 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3814d4be3f44SYishai Hadas 	if (!dst)
3815d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
3816d4be3f44SYishai Hadas 
3817d4be3f44SYishai Hadas 	mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
3818d4be3f44SYishai Hadas 	mutex_lock(&dev->flow_db->lock);
3819d4be3f44SYishai Hadas 
3820b47fd4ffSMark Bloch 	ft_prio = _get_flow_table(dev, fs_matcher, mcast);
3821d4be3f44SYishai Hadas 	if (IS_ERR(ft_prio)) {
3822d4be3f44SYishai Hadas 		err = PTR_ERR(ft_prio);
3823d4be3f44SYishai Hadas 		goto unlock;
3824d4be3f44SYishai Hadas 	}
3825d4be3f44SYishai Hadas 
38266346f0bfSYishai Hadas 	if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
3827d4be3f44SYishai Hadas 		dst->type = dest_type;
3828d4be3f44SYishai Hadas 		dst->tir_num = dest_id;
3829b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3830a7ee18bdSMark Bloch 	} else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
38316346f0bfSYishai Hadas 		dst->type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
38326346f0bfSYishai Hadas 		dst->ft_num = dest_id;
3833b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3834a7ee18bdSMark Bloch 	} else {
3835a7ee18bdSMark Bloch 		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3836a7ee18bdSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
38376346f0bfSYishai Hadas 	}
38386346f0bfSYishai Hadas 
3839b823dd6dSMark Bloch 	handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher, flow_act,
3840b823dd6dSMark Bloch 					cmd_in, inlen);
3841d4be3f44SYishai Hadas 
3842d4be3f44SYishai Hadas 	if (IS_ERR(handler)) {
3843d4be3f44SYishai Hadas 		err = PTR_ERR(handler);
3844d4be3f44SYishai Hadas 		goto destroy_ft;
3845d4be3f44SYishai Hadas 	}
3846d4be3f44SYishai Hadas 
3847d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
3848d4be3f44SYishai Hadas 	atomic_inc(&fs_matcher->usecnt);
3849d4be3f44SYishai Hadas 	handler->flow_matcher = fs_matcher;
3850d4be3f44SYishai Hadas 
3851d4be3f44SYishai Hadas 	kfree(dst);
3852d4be3f44SYishai Hadas 
3853d4be3f44SYishai Hadas 	return handler;
3854d4be3f44SYishai Hadas 
3855d4be3f44SYishai Hadas destroy_ft:
3856d4be3f44SYishai Hadas 	put_flow_table(dev, ft_prio, false);
3857d4be3f44SYishai Hadas unlock:
3858d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
3859d4be3f44SYishai Hadas 	kfree(dst);
3860d4be3f44SYishai Hadas 
3861d4be3f44SYishai Hadas 	return ERR_PTR(err);
386232269441SYishai Hadas }
386332269441SYishai Hadas 
3864c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
3865c6475a0bSAviad Yehezkel {
3866c6475a0bSAviad Yehezkel 	u32 flags = 0;
3867c6475a0bSAviad Yehezkel 
3868c6475a0bSAviad Yehezkel 	if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
3869c6475a0bSAviad Yehezkel 		flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
3870c6475a0bSAviad Yehezkel 
3871c6475a0bSAviad Yehezkel 	return flags;
3872c6475a0bSAviad Yehezkel }
3873c6475a0bSAviad Yehezkel 
3874c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED	MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
3875c6475a0bSAviad Yehezkel static struct ib_flow_action *
3876c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device,
3877c6475a0bSAviad Yehezkel 			       const struct ib_flow_action_attrs_esp *attr,
3878c6475a0bSAviad Yehezkel 			       struct uverbs_attr_bundle *attrs)
3879c6475a0bSAviad Yehezkel {
3880c6475a0bSAviad Yehezkel 	struct mlx5_ib_dev *mdev = to_mdev(device);
3881c6475a0bSAviad Yehezkel 	struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
3882c6475a0bSAviad Yehezkel 	struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
3883c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *action;
3884c6475a0bSAviad Yehezkel 	u64 action_flags;
3885c6475a0bSAviad Yehezkel 	u64 flags;
3886c6475a0bSAviad Yehezkel 	int err = 0;
3887c6475a0bSAviad Yehezkel 
3888bccd0622SJason Gunthorpe 	err = uverbs_get_flags64(
3889bccd0622SJason Gunthorpe 		&action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
3890bccd0622SJason Gunthorpe 		((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
3891bccd0622SJason Gunthorpe 	if (err)
3892bccd0622SJason Gunthorpe 		return ERR_PTR(err);
3893c6475a0bSAviad Yehezkel 
3894c6475a0bSAviad Yehezkel 	flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
3895c6475a0bSAviad Yehezkel 
3896c6475a0bSAviad Yehezkel 	/* We current only support a subset of the standard features. Only a
3897c6475a0bSAviad Yehezkel 	 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
3898c6475a0bSAviad Yehezkel 	 * (with overlap). Full offload mode isn't supported.
3899c6475a0bSAviad Yehezkel 	 */
3900c6475a0bSAviad Yehezkel 	if (!attr->keymat || attr->replay || attr->encap ||
3901c6475a0bSAviad Yehezkel 	    attr->spi || attr->seq || attr->tfc_pad ||
3902c6475a0bSAviad Yehezkel 	    attr->hard_limit_pkts ||
3903c6475a0bSAviad Yehezkel 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3904c6475a0bSAviad Yehezkel 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
3905c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3906c6475a0bSAviad Yehezkel 
3907c6475a0bSAviad Yehezkel 	if (attr->keymat->protocol !=
3908c6475a0bSAviad Yehezkel 	    IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
3909c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3910c6475a0bSAviad Yehezkel 
3911c6475a0bSAviad Yehezkel 	aes_gcm = &attr->keymat->keymat.aes_gcm;
3912c6475a0bSAviad Yehezkel 
3913c6475a0bSAviad Yehezkel 	if (aes_gcm->icv_len != 16 ||
3914c6475a0bSAviad Yehezkel 	    aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
3915c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3916c6475a0bSAviad Yehezkel 
3917c6475a0bSAviad Yehezkel 	action = kmalloc(sizeof(*action), GFP_KERNEL);
3918c6475a0bSAviad Yehezkel 	if (!action)
3919c6475a0bSAviad Yehezkel 		return ERR_PTR(-ENOMEM);
3920c6475a0bSAviad Yehezkel 
3921c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
3922c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
3923c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.aes_key));
3924c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
3925c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
3926c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.salt));
3927c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
3928c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
3929c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
3930c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
3931c6475a0bSAviad Yehezkel 	accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
3932c6475a0bSAviad Yehezkel 
3933c6475a0bSAviad Yehezkel 	accel_attrs.esn = attr->esn;
3934c6475a0bSAviad Yehezkel 	if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
3935c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
3936c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
3937c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3938c6475a0bSAviad Yehezkel 
3939c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
3940c6475a0bSAviad Yehezkel 		accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
3941c6475a0bSAviad Yehezkel 
3942c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ctx =
3943c6475a0bSAviad Yehezkel 		mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
3944c6475a0bSAviad Yehezkel 	if (IS_ERR(action->esp_aes_gcm.ctx)) {
3945c6475a0bSAviad Yehezkel 		err = PTR_ERR(action->esp_aes_gcm.ctx);
3946c6475a0bSAviad Yehezkel 		goto err_parse;
3947c6475a0bSAviad Yehezkel 	}
3948c6475a0bSAviad Yehezkel 
3949c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
3950c6475a0bSAviad Yehezkel 
3951c6475a0bSAviad Yehezkel 	return &action->ib_action;
3952c6475a0bSAviad Yehezkel 
3953c6475a0bSAviad Yehezkel err_parse:
3954c6475a0bSAviad Yehezkel 	kfree(action);
3955c6475a0bSAviad Yehezkel 	return ERR_PTR(err);
3956c6475a0bSAviad Yehezkel }
3957c6475a0bSAviad Yehezkel 
3958349705c1SMatan Barak static int
3959349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
3960349705c1SMatan Barak 			       const struct ib_flow_action_attrs_esp *attr,
3961349705c1SMatan Barak 			       struct uverbs_attr_bundle *attrs)
3962349705c1SMatan Barak {
3963349705c1SMatan Barak 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
3964349705c1SMatan Barak 	struct mlx5_accel_esp_xfrm_attrs accel_attrs;
3965349705c1SMatan Barak 	int err = 0;
3966349705c1SMatan Barak 
3967349705c1SMatan Barak 	if (attr->keymat || attr->replay || attr->encap ||
3968349705c1SMatan Barak 	    attr->spi || attr->seq || attr->tfc_pad ||
3969349705c1SMatan Barak 	    attr->hard_limit_pkts ||
3970349705c1SMatan Barak 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3971349705c1SMatan Barak 			     IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
3972349705c1SMatan Barak 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
3973349705c1SMatan Barak 		return -EOPNOTSUPP;
3974349705c1SMatan Barak 
3975349705c1SMatan Barak 	/* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
3976349705c1SMatan Barak 	 * be modified.
3977349705c1SMatan Barak 	 */
3978349705c1SMatan Barak 	if (!(maction->esp_aes_gcm.ib_flags &
3979349705c1SMatan Barak 	      IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
3980349705c1SMatan Barak 	    attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3981349705c1SMatan Barak 			   IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
3982349705c1SMatan Barak 		return -EINVAL;
3983349705c1SMatan Barak 
3984349705c1SMatan Barak 	memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
3985349705c1SMatan Barak 	       sizeof(accel_attrs));
3986349705c1SMatan Barak 
3987349705c1SMatan Barak 	accel_attrs.esn = attr->esn;
3988349705c1SMatan Barak 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
3989349705c1SMatan Barak 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3990349705c1SMatan Barak 	else
3991349705c1SMatan Barak 		accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3992349705c1SMatan Barak 
3993349705c1SMatan Barak 	err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
3994349705c1SMatan Barak 					 &accel_attrs);
3995349705c1SMatan Barak 	if (err)
3996349705c1SMatan Barak 		return err;
3997349705c1SMatan Barak 
3998349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags &=
3999349705c1SMatan Barak 		~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4000349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags |=
4001349705c1SMatan Barak 		attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4002349705c1SMatan Barak 
4003349705c1SMatan Barak 	return 0;
4004349705c1SMatan Barak }
4005349705c1SMatan Barak 
4006c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4007c6475a0bSAviad Yehezkel {
4008c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4009c6475a0bSAviad Yehezkel 
4010c6475a0bSAviad Yehezkel 	switch (action->type) {
4011c6475a0bSAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
4012c6475a0bSAviad Yehezkel 		/*
4013c6475a0bSAviad Yehezkel 		 * We only support aes_gcm by now, so we implicitly know this is
4014c6475a0bSAviad Yehezkel 		 * the underline crypto.
4015c6475a0bSAviad Yehezkel 		 */
4016c6475a0bSAviad Yehezkel 		mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4017c6475a0bSAviad Yehezkel 		break;
4018b4749bf2SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
4019b4749bf2SMark Bloch 		mlx5_ib_destroy_flow_action_raw(maction);
4020b4749bf2SMark Bloch 		break;
4021c6475a0bSAviad Yehezkel 	default:
4022c6475a0bSAviad Yehezkel 		WARN_ON(true);
4023c6475a0bSAviad Yehezkel 		break;
4024c6475a0bSAviad Yehezkel 	}
4025c6475a0bSAviad Yehezkel 
4026c6475a0bSAviad Yehezkel 	kfree(maction);
4027c6475a0bSAviad Yehezkel 	return 0;
4028c6475a0bSAviad Yehezkel }
4029c6475a0bSAviad Yehezkel 
4030e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4031e126ba97SEli Cohen {
4032e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
403381e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
4034e126ba97SEli Cohen 	int err;
4035539ec982SYishai Hadas 	u16 uid;
4036539ec982SYishai Hadas 
4037539ec982SYishai Hadas 	uid = ibqp->pd ?
4038539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4039e126ba97SEli Cohen 
404081e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
404181e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
404281e30880SYishai Hadas 		return -EOPNOTSUPP;
404381e30880SYishai Hadas 	}
404481e30880SYishai Hadas 
4045539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4046e126ba97SEli Cohen 	if (err)
4047e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4048e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4049e126ba97SEli Cohen 
4050e126ba97SEli Cohen 	return err;
4051e126ba97SEli Cohen }
4052e126ba97SEli Cohen 
4053e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4054e126ba97SEli Cohen {
4055e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4056e126ba97SEli Cohen 	int err;
4057539ec982SYishai Hadas 	u16 uid;
4058e126ba97SEli Cohen 
4059539ec982SYishai Hadas 	uid = ibqp->pd ?
4060539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4061539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4062e126ba97SEli Cohen 	if (err)
4063e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4064e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4065e126ba97SEli Cohen 
4066e126ba97SEli Cohen 	return err;
4067e126ba97SEli Cohen }
4068e126ba97SEli Cohen 
4069e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
4070e126ba97SEli Cohen {
40711b5daf11SMajd Dibbiny 	int err;
4072e126ba97SEli Cohen 
40731b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
4074e126ba97SEli Cohen 	if (err)
4075e126ba97SEli Cohen 		return err;
40761b5daf11SMajd Dibbiny 
40771b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
40781b5daf11SMajd Dibbiny 
40791b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
4080e126ba97SEli Cohen }
4081e126ba97SEli Cohen 
4082508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
4083508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4084e126ba97SEli Cohen {
4085e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4086e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4087e126ba97SEli Cohen 
40889603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
4089e126ba97SEli Cohen }
4090508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
4091e126ba97SEli Cohen 
4092508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
4093e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
4094e126ba97SEli Cohen {
4095e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4096e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4097e126ba97SEli Cohen 
40986aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
4099e126ba97SEli Cohen }
4100508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
4101e126ba97SEli Cohen 
4102508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
4103508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4104e126ba97SEli Cohen {
4105e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4106e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
41079603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
4108e126ba97SEli Cohen }
4109508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
4110e126ba97SEli Cohen 
4111508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
4112508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
4113e126ba97SEli Cohen {
4114e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4115e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
41169603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
4117e126ba97SEli Cohen }
4118508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
4119e126ba97SEli Cohen 
4120508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
4121508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4122e126ba97SEli Cohen {
4123e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4124e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4125e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
41269603b61dSJack Morgenstein 		       dev->mdev->board_id);
4127e126ba97SEli Cohen }
4128508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
4129e126ba97SEli Cohen 
4130508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
4131508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
4132508a523fSParav Pandit 	&dev_attr_hca_type.attr,
4133508a523fSParav Pandit 	&dev_attr_board_id.attr,
4134508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
4135508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
4136508a523fSParav Pandit 	NULL,
4137508a523fSParav Pandit };
4138e126ba97SEli Cohen 
4139508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
4140508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
4141e126ba97SEli Cohen };
4142e126ba97SEli Cohen 
41437722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
41447722f47eSHaggai Eran {
41457722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
41467722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
41477722f47eSHaggai Eran 			     pkey_change_work);
41487722f47eSHaggai Eran 
41497722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
41507722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
41517722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
41527722f47eSHaggai Eran }
41537722f47eSHaggai Eran 
415489ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
415589ea94a7SMaor Gottlieb {
415689ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
415789ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
415889ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
415989ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
416089ea94a7SMaor Gottlieb 	unsigned long flags_qp;
416189ea94a7SMaor Gottlieb 	unsigned long flags_cq;
416289ea94a7SMaor Gottlieb 	unsigned long flags;
416389ea94a7SMaor Gottlieb 
416489ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
416589ea94a7SMaor Gottlieb 
416689ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
416789ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
416889ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
416989ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
417089ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
417189ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
417289ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
417389ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
417489ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
417589ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
417689ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
417789ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
417889ea94a7SMaor Gottlieb 						      &cq_armed_list);
417989ea94a7SMaor Gottlieb 				}
418089ea94a7SMaor Gottlieb 			}
418189ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
418289ea94a7SMaor Gottlieb 		}
418389ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
418489ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
418589ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
418689ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
418789ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
418889ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
418989ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
419089ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
419189ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
419289ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
419389ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
419489ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
419589ea94a7SMaor Gottlieb 							      &cq_armed_list);
419689ea94a7SMaor Gottlieb 					}
419789ea94a7SMaor Gottlieb 				}
419889ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
419989ea94a7SMaor Gottlieb 						       flags_cq);
420089ea94a7SMaor Gottlieb 			}
420189ea94a7SMaor Gottlieb 		}
420289ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
420389ea94a7SMaor Gottlieb 	}
420489ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
420589ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
420689ea94a7SMaor Gottlieb 	 */
420789ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
420889ea94a7SMaor Gottlieb 		mcq->comp(mcq);
420989ea94a7SMaor Gottlieb 	}
421089ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
421189ea94a7SMaor Gottlieb }
421289ea94a7SMaor Gottlieb 
421303404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
421403404e8aSMaor Gottlieb {
421503404e8aSMaor Gottlieb 	int err;
421603404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
421703404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
421803404e8aSMaor Gottlieb 			     delay_drop_work);
421903404e8aSMaor Gottlieb 
4220fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
4221fe248c3aSMaor Gottlieb 
422203404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
422303404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
422403404e8aSMaor Gottlieb 				       delay_drop->timeout);
422503404e8aSMaor Gottlieb 	if (err) {
422603404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
422703404e8aSMaor Gottlieb 			     delay_drop->timeout);
422803404e8aSMaor Gottlieb 		delay_drop->activate = false;
422903404e8aSMaor Gottlieb 	}
423003404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
423103404e8aSMaor Gottlieb }
423203404e8aSMaor Gottlieb 
423309e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
423409e574faSSaeed Mahameed 				 struct ib_event *ibev)
423509e574faSSaeed Mahameed {
423609e574faSSaeed Mahameed 	switch (eqe->sub_type) {
423709e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
423809e574faSSaeed Mahameed 		schedule_work(&ibdev->delay_drop.delay_drop_work);
423909e574faSSaeed Mahameed 		break;
424009e574faSSaeed Mahameed 	default: /* do nothing */
424109e574faSSaeed Mahameed 		return;
424209e574faSSaeed Mahameed 	}
424309e574faSSaeed Mahameed }
424409e574faSSaeed Mahameed 
4245134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4246134e9349SSaeed Mahameed 			      struct ib_event *ibev)
4247134e9349SSaeed Mahameed {
4248134e9349SSaeed Mahameed 	u8 port = (eqe->data.port.port >> 4) & 0xf;
4249134e9349SSaeed Mahameed 
4250134e9349SSaeed Mahameed 	ibev->element.port_num = port;
4251134e9349SSaeed Mahameed 
4252134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
4253134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4254134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4255134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4256134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
4257134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
4258134e9349SSaeed Mahameed 		 */
4259134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4260134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
4261134e9349SSaeed Mahameed 			return -EINVAL;
4262134e9349SSaeed Mahameed 
4263134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4264134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4265134e9349SSaeed Mahameed 		break;
4266134e9349SSaeed Mahameed 
4267134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
4268134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
4269134e9349SSaeed Mahameed 		break;
4270134e9349SSaeed Mahameed 
4271134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4272134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
4273134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4274134e9349SSaeed Mahameed 		break;
4275134e9349SSaeed Mahameed 
4276134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4277134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
4278134e9349SSaeed Mahameed 		break;
4279134e9349SSaeed Mahameed 
4280134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4281134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
4282134e9349SSaeed Mahameed 		break;
4283134e9349SSaeed Mahameed 	default:
4284134e9349SSaeed Mahameed 		return -EINVAL;
4285134e9349SSaeed Mahameed 	}
4286134e9349SSaeed Mahameed 
4287134e9349SSaeed Mahameed 	return 0;
4288134e9349SSaeed Mahameed }
4289134e9349SSaeed Mahameed 
4290d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
4291e126ba97SEli Cohen {
4292d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
4293d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
4294d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
4295e126ba97SEli Cohen 	struct ib_event ibev;
4296dbaaff2aSEli Cohen 	bool fatal = false;
4297e126ba97SEli Cohen 
4298df097a27SSaeed Mahameed 	if (work->is_slave) {
4299df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
4300d69a24e0SDaniel Jurgens 		if (!ibdev)
4301d69a24e0SDaniel Jurgens 			goto out;
4302d69a24e0SDaniel Jurgens 	} else {
4303df097a27SSaeed Mahameed 		ibdev = work->dev;
4304d69a24e0SDaniel Jurgens 	}
4305d69a24e0SDaniel Jurgens 
4306d69a24e0SDaniel Jurgens 	switch (work->event) {
4307e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
4308e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
430989ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
4310134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
4311dbaaff2aSEli Cohen 		fatal = true;
4312e126ba97SEli Cohen 		break;
4313134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
4314134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
4315d69a24e0SDaniel Jurgens 			goto out;
4316e126ba97SEli Cohen 		break;
431709e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
431809e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
431909e574faSSaeed Mahameed 		/* fall through */
4320bdc37924SSaeed Mahameed 	default:
432103404e8aSMaor Gottlieb 		goto out;
4322e126ba97SEli Cohen 	}
4323e126ba97SEli Cohen 
4324e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
4325e126ba97SEli Cohen 
4326134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4327134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
432803404e8aSMaor Gottlieb 		goto out;
4329a0c84c32SEli Cohen 	}
4330a0c84c32SEli Cohen 
4331e126ba97SEli Cohen 	if (ibdev->ib_active)
4332e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
4333dbaaff2aSEli Cohen 
4334dbaaff2aSEli Cohen 	if (fatal)
4335dbaaff2aSEli Cohen 		ibdev->ib_active = false;
433603404e8aSMaor Gottlieb out:
4337d69a24e0SDaniel Jurgens 	kfree(work);
4338d69a24e0SDaniel Jurgens }
4339d69a24e0SDaniel Jurgens 
4340df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
4341df097a27SSaeed Mahameed 			 unsigned long event, void *param)
4342d69a24e0SDaniel Jurgens {
4343d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
4344d69a24e0SDaniel Jurgens 
4345d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
434610bea9c8SLeon Romanovsky 	if (!work)
4347df097a27SSaeed Mahameed 		return NOTIFY_DONE;
434810bea9c8SLeon Romanovsky 
4349d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4350df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4351df097a27SSaeed Mahameed 	work->is_slave = false;
4352d69a24e0SDaniel Jurgens 	work->param = param;
4353d69a24e0SDaniel Jurgens 	work->event = event;
4354d69a24e0SDaniel Jurgens 
4355d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
4356df097a27SSaeed Mahameed 
4357df097a27SSaeed Mahameed 	return NOTIFY_OK;
4358df097a27SSaeed Mahameed }
4359df097a27SSaeed Mahameed 
4360df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4361df097a27SSaeed Mahameed 				    unsigned long event, void *param)
4362df097a27SSaeed Mahameed {
4363df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
4364df097a27SSaeed Mahameed 
4365df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
4366df097a27SSaeed Mahameed 	if (!work)
4367df097a27SSaeed Mahameed 		return NOTIFY_DONE;
4368df097a27SSaeed Mahameed 
4369df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4370df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4371df097a27SSaeed Mahameed 	work->is_slave = true;
4372df097a27SSaeed Mahameed 	work->param = param;
4373df097a27SSaeed Mahameed 	work->event = event;
4374df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
4375df097a27SSaeed Mahameed 
4376df097a27SSaeed Mahameed 	return NOTIFY_OK;
4377d69a24e0SDaniel Jurgens }
4378d69a24e0SDaniel Jurgens 
4379c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4380c43f1112SMaor Gottlieb {
4381c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
4382c43f1112SMaor Gottlieb 	int err;
4383c43f1112SMaor Gottlieb 	int port;
4384c43f1112SMaor Gottlieb 
4385508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++) {
4386c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
4387c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4388c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
4389c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4390c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
4391c43f1112SMaor Gottlieb 								   port, 0,
4392c43f1112SMaor Gottlieb 								   &vport_ctx);
4393c43f1112SMaor Gottlieb 				if (err) {
4394c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4395c43f1112SMaor Gottlieb 						    port, err);
4396c43f1112SMaor Gottlieb 					return err;
4397c43f1112SMaor Gottlieb 				}
4398c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
4399c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
4400c43f1112SMaor Gottlieb 			} else {
4401c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
4402c43f1112SMaor Gottlieb 			}
4403c43f1112SMaor Gottlieb 		}
4404c43f1112SMaor Gottlieb 	}
4405c43f1112SMaor Gottlieb 	return 0;
4406c43f1112SMaor Gottlieb }
4407c43f1112SMaor Gottlieb 
4408e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4409e126ba97SEli Cohen {
4410e126ba97SEli Cohen 	int port;
4411e126ba97SEli Cohen 
4412508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
4413e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
4414e126ba97SEli Cohen }
4415e126ba97SEli Cohen 
441632f69e4bSDaniel Jurgens static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4417e126ba97SEli Cohen {
4418e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
4419e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
4420f614fc15SDan Carpenter 	int err = -ENOMEM;
44212528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
4422e126ba97SEli Cohen 
4423e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
4424e126ba97SEli Cohen 	if (!pprops)
4425e126ba97SEli Cohen 		goto out;
4426e126ba97SEli Cohen 
4427e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4428e126ba97SEli Cohen 	if (!dprops)
4429e126ba97SEli Cohen 		goto out;
4430e126ba97SEli Cohen 
4431c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
4432c43f1112SMaor Gottlieb 	if (err)
4433c43f1112SMaor Gottlieb 		goto out;
4434c43f1112SMaor Gottlieb 
44352528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
4436e126ba97SEli Cohen 	if (err) {
4437e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
4438e126ba97SEli Cohen 		goto out;
4439e126ba97SEli Cohen 	}
4440e126ba97SEli Cohen 
4441c4550c63SOr Gerlitz 	memset(pprops, 0, sizeof(*pprops));
4442e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4443e126ba97SEli Cohen 	if (err) {
4444938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
4445938fe83cSSaeed Mahameed 			     port, err);
444632f69e4bSDaniel Jurgens 		goto out;
4447e126ba97SEli Cohen 	}
444832f69e4bSDaniel Jurgens 
4449938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
4450938fe83cSSaeed Mahameed 					dprops->max_pkeys;
4451938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
4452938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
445332f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
445432f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
4455e126ba97SEli Cohen 
4456e126ba97SEli Cohen out:
4457e126ba97SEli Cohen 	kfree(pprops);
4458e126ba97SEli Cohen 	kfree(dprops);
4459e126ba97SEli Cohen 
4460e126ba97SEli Cohen 	return err;
4461e126ba97SEli Cohen }
4462e126ba97SEli Cohen 
4463e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4464e126ba97SEli Cohen {
4465e126ba97SEli Cohen 	int err;
4466e126ba97SEli Cohen 
4467e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
4468e126ba97SEli Cohen 	if (err)
4469e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4470e126ba97SEli Cohen 
447132927e28SMark Bloch 	if (dev->umrc.qp)
4472e126ba97SEli Cohen 		mlx5_ib_destroy_qp(dev->umrc.qp);
447332927e28SMark Bloch 	if (dev->umrc.cq)
4474add08d76SChristoph Hellwig 		ib_free_cq(dev->umrc.cq);
447532927e28SMark Bloch 	if (dev->umrc.pd)
4476e126ba97SEli Cohen 		ib_dealloc_pd(dev->umrc.pd);
4477e126ba97SEli Cohen }
4478e126ba97SEli Cohen 
4479e126ba97SEli Cohen enum {
4480e126ba97SEli Cohen 	MAX_UMR_WR = 128,
4481e126ba97SEli Cohen };
4482e126ba97SEli Cohen 
4483e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
4484e126ba97SEli Cohen {
4485e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
4486e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
4487e126ba97SEli Cohen 	struct ib_pd *pd;
4488e126ba97SEli Cohen 	struct ib_cq *cq;
4489e126ba97SEli Cohen 	struct ib_qp *qp;
4490e126ba97SEli Cohen 	int ret;
4491e126ba97SEli Cohen 
4492e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4493e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4494e126ba97SEli Cohen 	if (!attr || !init_attr) {
4495e126ba97SEli Cohen 		ret = -ENOMEM;
4496e126ba97SEli Cohen 		goto error_0;
4497e126ba97SEli Cohen 	}
4498e126ba97SEli Cohen 
4499ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
4500e126ba97SEli Cohen 	if (IS_ERR(pd)) {
4501e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4502e126ba97SEli Cohen 		ret = PTR_ERR(pd);
4503e126ba97SEli Cohen 		goto error_0;
4504e126ba97SEli Cohen 	}
4505e126ba97SEli Cohen 
4506add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
4507e126ba97SEli Cohen 	if (IS_ERR(cq)) {
4508e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4509e126ba97SEli Cohen 		ret = PTR_ERR(cq);
4510e126ba97SEli Cohen 		goto error_2;
4511e126ba97SEli Cohen 	}
4512e126ba97SEli Cohen 
4513e126ba97SEli Cohen 	init_attr->send_cq = cq;
4514e126ba97SEli Cohen 	init_attr->recv_cq = cq;
4515e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4516e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
4517e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
4518e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4519e126ba97SEli Cohen 	init_attr->port_num = 1;
4520e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4521e126ba97SEli Cohen 	if (IS_ERR(qp)) {
4522e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4523e126ba97SEli Cohen 		ret = PTR_ERR(qp);
4524e126ba97SEli Cohen 		goto error_3;
4525e126ba97SEli Cohen 	}
4526e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
4527e126ba97SEli Cohen 	qp->real_qp    = qp;
4528e126ba97SEli Cohen 	qp->uobject    = NULL;
4529e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
453031fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
453131fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
4532e126ba97SEli Cohen 
4533e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
4534e126ba97SEli Cohen 	attr->port_num = 1;
4535e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4536e126ba97SEli Cohen 				IB_QP_PORT, NULL);
4537e126ba97SEli Cohen 	if (ret) {
4538e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4539e126ba97SEli Cohen 		goto error_4;
4540e126ba97SEli Cohen 	}
4541e126ba97SEli Cohen 
4542e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4543e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
4544e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
4545e126ba97SEli Cohen 
4546e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4547e126ba97SEli Cohen 	if (ret) {
4548e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4549e126ba97SEli Cohen 		goto error_4;
4550e126ba97SEli Cohen 	}
4551e126ba97SEli Cohen 
4552e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4553e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
4554e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4555e126ba97SEli Cohen 	if (ret) {
4556e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4557e126ba97SEli Cohen 		goto error_4;
4558e126ba97SEli Cohen 	}
4559e126ba97SEli Cohen 
4560e126ba97SEli Cohen 	dev->umrc.qp = qp;
4561e126ba97SEli Cohen 	dev->umrc.cq = cq;
4562e126ba97SEli Cohen 	dev->umrc.pd = pd;
4563e126ba97SEli Cohen 
4564e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
4565e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
4566e126ba97SEli Cohen 	if (ret) {
4567e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4568e126ba97SEli Cohen 		goto error_4;
4569e126ba97SEli Cohen 	}
4570e126ba97SEli Cohen 
4571e126ba97SEli Cohen 	kfree(attr);
4572e126ba97SEli Cohen 	kfree(init_attr);
4573e126ba97SEli Cohen 
4574e126ba97SEli Cohen 	return 0;
4575e126ba97SEli Cohen 
4576e126ba97SEli Cohen error_4:
4577e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
457832927e28SMark Bloch 	dev->umrc.qp = NULL;
4579e126ba97SEli Cohen 
4580e126ba97SEli Cohen error_3:
4581add08d76SChristoph Hellwig 	ib_free_cq(cq);
458232927e28SMark Bloch 	dev->umrc.cq = NULL;
4583e126ba97SEli Cohen 
4584e126ba97SEli Cohen error_2:
4585e126ba97SEli Cohen 	ib_dealloc_pd(pd);
458632927e28SMark Bloch 	dev->umrc.pd = NULL;
4587e126ba97SEli Cohen 
4588e126ba97SEli Cohen error_0:
4589e126ba97SEli Cohen 	kfree(attr);
4590e126ba97SEli Cohen 	kfree(init_attr);
4591e126ba97SEli Cohen 	return ret;
4592e126ba97SEli Cohen }
4593e126ba97SEli Cohen 
45946e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
45956e8484c5SMax Gurtovoy {
45966e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
45976e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
45986e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
45996e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
46006e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
46016e8484c5SMax Gurtovoy 	default:
46026e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
46036e8484c5SMax Gurtovoy 	}
46046e8484c5SMax Gurtovoy }
46056e8484c5SMax Gurtovoy 
4606e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
4607e126ba97SEli Cohen {
4608e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
4609e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
4610bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
46117722f47eSHaggai Eran 	int port;
4612e126ba97SEli Cohen 	int ret = 0;
4613e126ba97SEli Cohen 
4614e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
4615e126ba97SEli Cohen 
4616d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
4617d16e91daSHaggai Eran 
4618e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
4619e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
4620e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
4621e126ba97SEli Cohen 		goto error0;
4622e126ba97SEli Cohen 	}
4623e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
4624e126ba97SEli Cohen 	devr->p0->uobject = NULL;
4625e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
4626e126ba97SEli Cohen 
4627bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
4628e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
4629e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
4630e126ba97SEli Cohen 		goto error1;
4631e126ba97SEli Cohen 	}
4632e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
4633e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
4634e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
4635e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
4636e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
4637e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
4638e126ba97SEli Cohen 
4639e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4640e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
4641e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
4642e126ba97SEli Cohen 		goto error2;
4643e126ba97SEli Cohen 	}
4644e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
4645e126ba97SEli Cohen 	devr->x0->inode = NULL;
4646e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
4647e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
4648e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4649e126ba97SEli Cohen 
4650e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4651e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
4652e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
4653e126ba97SEli Cohen 		goto error3;
4654e126ba97SEli Cohen 	}
4655e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
4656e126ba97SEli Cohen 	devr->x1->inode = NULL;
4657e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
4658e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
4659e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4660e126ba97SEli Cohen 
4661e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
4662e126ba97SEli Cohen 	attr.attr.max_sge = 1;
4663e126ba97SEli Cohen 	attr.attr.max_wr = 1;
4664e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
46651a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
4666e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
4667e126ba97SEli Cohen 
4668e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4669e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
4670e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
4671e126ba97SEli Cohen 		goto error4;
4672e126ba97SEli Cohen 	}
4673e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
4674e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
4675e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
4676e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
4677e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
4678e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
4679e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
46801a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
4681e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
46821a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
4683e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
4684e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
4685e126ba97SEli Cohen 
46864aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
46874aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
46884aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
46894aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
46904aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
46914aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
46924aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
46934aa17b28SHaggai Abramonvsky 		goto error5;
46944aa17b28SHaggai Abramonvsky 	}
46954aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
46964aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
46974aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
46984aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
46994aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
47004aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
47011a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
47024aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
47031a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
47044aa17b28SHaggai Abramonvsky 
47057722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
47067722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
47077722f47eSHaggai Eran 			  pkey_change_handler);
47087722f47eSHaggai Eran 		devr->ports[port].devr = devr;
47097722f47eSHaggai Eran 	}
47107722f47eSHaggai Eran 
4711e126ba97SEli Cohen 	return 0;
4712e126ba97SEli Cohen 
47134aa17b28SHaggai Abramonvsky error5:
47144aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
4715e126ba97SEli Cohen error4:
4716e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
4717e126ba97SEli Cohen error3:
4718e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
4719e126ba97SEli Cohen error2:
4720e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
4721e126ba97SEli Cohen error1:
4722e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
4723e126ba97SEli Cohen error0:
4724e126ba97SEli Cohen 	return ret;
4725e126ba97SEli Cohen }
4726e126ba97SEli Cohen 
4727e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
4728e126ba97SEli Cohen {
47297722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
47307722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
47317722f47eSHaggai Eran 	int port;
47327722f47eSHaggai Eran 
47334aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
4734e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
4735e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
4736e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
4737e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
4738e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
47397722f47eSHaggai Eran 
47407722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
47417722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
47427722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
4743e126ba97SEli Cohen }
4744e126ba97SEli Cohen 
4745b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
4746b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
4747e53505a8SAchiad Shochat {
4748e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4749e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
4750e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
4751e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
475285c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
4753e53505a8SAchiad Shochat 	u32 ret = 0;
4754e53505a8SAchiad Shochat 
4755b02289b3SArtemy Kovalyov 	if (rep->grh_required)
4756b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
4757b02289b3SArtemy Kovalyov 
4758e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
4759b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
4760e53505a8SAchiad Shochat 
476185c7c014SDaniel Jurgens 	if (raw_support)
4762b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
476372cd5717SOr Gerlitz 
4764e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
476572cd5717SOr Gerlitz 		return ret;
4766e53505a8SAchiad Shochat 
4767e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
476872cd5717SOr Gerlitz 		return ret;
4769e53505a8SAchiad Shochat 
4770e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
4771e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
4772e53505a8SAchiad Shochat 
4773e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
4774e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
4775e53505a8SAchiad Shochat 
4776e53505a8SAchiad Shochat 	return ret;
4777e53505a8SAchiad Shochat }
4778e53505a8SAchiad Shochat 
47797738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
47807738613eSIra Weiny 			       struct ib_port_immutable *immutable)
47817738613eSIra Weiny {
47827738613eSIra Weiny 	struct ib_port_attr attr;
4783ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4784ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
4785b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
47867738613eSIra Weiny 	int err;
47877738613eSIra Weiny 
4788c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
47897738613eSIra Weiny 	if (err)
47907738613eSIra Weiny 		return err;
47917738613eSIra Weiny 
4792b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
4793b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
4794b02289b3SArtemy Kovalyov 						   &rep);
4795b02289b3SArtemy Kovalyov 		if (err)
4796b02289b3SArtemy Kovalyov 			return err;
4797b02289b3SArtemy Kovalyov 	}
4798b02289b3SArtemy Kovalyov 
47997738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
48007738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
4801b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
4802ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
4803337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
48047738613eSIra Weiny 
48057738613eSIra Weiny 	return 0;
48067738613eSIra Weiny }
48077738613eSIra Weiny 
48088e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
48098e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
48108e6efa3aSMark Bloch {
48118e6efa3aSMark Bloch 	struct ib_port_attr attr;
48128e6efa3aSMark Bloch 	int err;
48138e6efa3aSMark Bloch 
48148e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
48158e6efa3aSMark Bloch 
48168e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
48178e6efa3aSMark Bloch 	if (err)
48188e6efa3aSMark Bloch 		return err;
48198e6efa3aSMark Bloch 
48208e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
48218e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
48228e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
48238e6efa3aSMark Bloch 
48248e6efa3aSMark Bloch 	return 0;
48258e6efa3aSMark Bloch }
48268e6efa3aSMark Bloch 
48279abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
4828c7342823SIra Weiny {
4829c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
4830c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
48319abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
48329abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
48339abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
4834c7342823SIra Weiny }
4835c7342823SIra Weiny 
483645f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
48379ef9c640SAviv Heller {
48389ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
48399ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
48409ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
48419ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
48429ef9c640SAviv Heller 	int err;
48439ef9c640SAviv Heller 
4844*7c34ec19SAviv Heller 	if (!ns || !mlx5_lag_is_roce(mdev))
48459ef9c640SAviv Heller 		return 0;
48469ef9c640SAviv Heller 
48479ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
48489ef9c640SAviv Heller 	if (err)
48499ef9c640SAviv Heller 		return err;
48509ef9c640SAviv Heller 
48519ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
48529ef9c640SAviv Heller 	if (IS_ERR(ft)) {
48539ef9c640SAviv Heller 		err = PTR_ERR(ft);
48549ef9c640SAviv Heller 		goto err_destroy_vport_lag;
48559ef9c640SAviv Heller 	}
48569ef9c640SAviv Heller 
48579a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
4858*7c34ec19SAviv Heller 	dev->lag_active = true;
48599ef9c640SAviv Heller 	return 0;
48609ef9c640SAviv Heller 
48619ef9c640SAviv Heller err_destroy_vport_lag:
48629ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
48639ef9c640SAviv Heller 	return err;
48649ef9c640SAviv Heller }
48659ef9c640SAviv Heller 
486645f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
48679ef9c640SAviv Heller {
48689ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
48699ef9c640SAviv Heller 
4870*7c34ec19SAviv Heller 	if (dev->lag_active) {
4871*7c34ec19SAviv Heller 		dev->lag_active = false;
4872*7c34ec19SAviv Heller 
48739a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
48749a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
48759ef9c640SAviv Heller 
48769ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
48779ef9c640SAviv Heller 	}
48789ef9c640SAviv Heller }
48799ef9c640SAviv Heller 
48807fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
4881fc24fc5eSAchiad Shochat {
4882e53505a8SAchiad Shochat 	int err;
4883e53505a8SAchiad Shochat 
48847fd8aefbSDaniel Jurgens 	dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
48857fd8aefbSDaniel Jurgens 	err = register_netdevice_notifier(&dev->roce[port_num].nb);
48865ec8c83eSAviv Heller 	if (err) {
48877fd8aefbSDaniel Jurgens 		dev->roce[port_num].nb.notifier_call = NULL;
4888e53505a8SAchiad Shochat 		return err;
48895ec8c83eSAviv Heller 	}
4890e53505a8SAchiad Shochat 
4891d012f5d6SOr Gerlitz 	return 0;
4892d012f5d6SOr Gerlitz }
4893d012f5d6SOr Gerlitz 
48947fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
4895e126ba97SEli Cohen {
48967fd8aefbSDaniel Jurgens 	if (dev->roce[port_num].nb.notifier_call) {
48977fd8aefbSDaniel Jurgens 		unregister_netdevice_notifier(&dev->roce[port_num].nb);
48987fd8aefbSDaniel Jurgens 		dev->roce[port_num].nb.notifier_call = NULL;
4899e126ba97SEli Cohen 	}
4900e126ba97SEli Cohen }
4901e126ba97SEli Cohen 
4902e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
4903e126ba97SEli Cohen {
4904e126ba97SEli Cohen 	int err;
4905e126ba97SEli Cohen 
4906ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
4907e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
4908e53505a8SAchiad Shochat 		if (err)
49098e6efa3aSMark Bloch 			return err;
4910ca5b91d6SOr Gerlitz 	}
4911e53505a8SAchiad Shochat 
491245f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
49139ef9c640SAviv Heller 	if (err)
49149ef9c640SAviv Heller 		goto err_disable_roce;
49159ef9c640SAviv Heller 
4916e53505a8SAchiad Shochat 	return 0;
4917e53505a8SAchiad Shochat 
49189ef9c640SAviv Heller err_disable_roce:
4919ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
49209ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
49219ef9c640SAviv Heller 
4922e53505a8SAchiad Shochat 	return err;
4923fc24fc5eSAchiad Shochat }
4924fc24fc5eSAchiad Shochat 
492545f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
4926fc24fc5eSAchiad Shochat {
492745f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
4928ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
4929e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
4930fc24fc5eSAchiad Shochat }
4931fc24fc5eSAchiad Shochat 
4932e1f24a79SParav Pandit struct mlx5_ib_counter {
49337c16f477SKamal Heib 	const char *name;
49347c16f477SKamal Heib 	size_t offset;
49357c16f477SKamal Heib };
49367c16f477SKamal Heib 
49377c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
49387c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
49397c16f477SKamal Heib 
4940e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
49417c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
49427c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
49437c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
49447c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
49457c16f477SKamal Heib };
49467c16f477SKamal Heib 
4947e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
49487c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
49497c16f477SKamal Heib };
49507c16f477SKamal Heib 
4951e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
49527c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
49537c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
49547c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
49557c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
49567c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
49577c16f477SKamal Heib };
49587c16f477SKamal Heib 
4959e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
4960e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
4961e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
4962e1f24a79SParav Pandit 
4963e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
4964e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
4965e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
4966e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
4967e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
4968e1f24a79SParav Pandit };
4969e1f24a79SParav Pandit 
497058dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
497158dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
497258dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
497358dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
497458dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
497558dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
497658dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
497758dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
497858dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
497958dcb60aSParav Pandit };
498058dcb60aSParav Pandit 
49819f876f3dSTalat Batheesh #define INIT_EXT_PPCNT_COUNTER(_name)		\
49829f876f3dSTalat Batheesh 	{ .name = #_name, .offset =	\
49839f876f3dSTalat Batheesh 	MLX5_BYTE_OFF(ppcnt_reg, \
49849f876f3dSTalat Batheesh 		      counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
49859f876f3dSTalat Batheesh 
49869f876f3dSTalat Batheesh static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
49879f876f3dSTalat Batheesh 	INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
49889f876f3dSTalat Batheesh };
49899f876f3dSTalat Batheesh 
4990e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
49910837e86aSMark Bloch {
4992aac4492eSDaniel Jurgens 	int i;
49930837e86aSMark Bloch 
49947c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
4995921c0f5bSParav Pandit 		if (dev->port[i].cnts.set_id_valid)
49960837e86aSMark Bloch 			mlx5_core_dealloc_q_counter(dev->mdev,
4997e1f24a79SParav Pandit 						    dev->port[i].cnts.set_id);
4998e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
4999e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
50007c16f477SKamal Heib 	}
50017c16f477SKamal Heib }
50027c16f477SKamal Heib 
5003e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5004e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
50057c16f477SKamal Heib {
50067c16f477SKamal Heib 	u32 num_counters;
50077c16f477SKamal Heib 
50087c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
50097c16f477SKamal Heib 
50107c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
50117c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
50127c16f477SKamal Heib 
50137c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
50147c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
501558dcb60aSParav Pandit 
501658dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
501758dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
501858dcb60aSParav Pandit 
5019e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
50207c16f477SKamal Heib 
5021e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5022e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5023e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
5024e1f24a79SParav Pandit 	}
50259f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
50269f876f3dSTalat Batheesh 		cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
50279f876f3dSTalat Batheesh 		num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
50289f876f3dSTalat Batheesh 	}
5029e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5030e1f24a79SParav Pandit 	if (!cnts->names)
50317c16f477SKamal Heib 		return -ENOMEM;
50327c16f477SKamal Heib 
5033e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
5034e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
5035e1f24a79SParav Pandit 	if (!cnts->offsets)
50367c16f477SKamal Heib 		goto err_names;
50377c16f477SKamal Heib 
50387c16f477SKamal Heib 	return 0;
50397c16f477SKamal Heib 
50407c16f477SKamal Heib err_names:
5041e1f24a79SParav Pandit 	kfree(cnts->names);
5042aac4492eSDaniel Jurgens 	cnts->names = NULL;
50437c16f477SKamal Heib 	return -ENOMEM;
50447c16f477SKamal Heib }
50457c16f477SKamal Heib 
5046e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
50477c16f477SKamal Heib 				  const char **names,
50487c16f477SKamal Heib 				  size_t *offsets)
50497c16f477SKamal Heib {
50507c16f477SKamal Heib 	int i;
50517c16f477SKamal Heib 	int j = 0;
50527c16f477SKamal Heib 
50537c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
50547c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
50557c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
50567c16f477SKamal Heib 	}
50577c16f477SKamal Heib 
50587c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
50597c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
50607c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
50617c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
50627c16f477SKamal Heib 		}
50637c16f477SKamal Heib 	}
50647c16f477SKamal Heib 
50657c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
50667c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
50677c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
50687c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
50697c16f477SKamal Heib 		}
50707c16f477SKamal Heib 	}
5071e1f24a79SParav Pandit 
507258dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
507358dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
507458dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
507558dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
507658dcb60aSParav Pandit 		}
507758dcb60aSParav Pandit 	}
507858dcb60aSParav Pandit 
5079e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5080e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5081e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
5082e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
5083e1f24a79SParav Pandit 		}
5084e1f24a79SParav Pandit 	}
50859f876f3dSTalat Batheesh 
50869f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
50879f876f3dSTalat Batheesh 		for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
50889f876f3dSTalat Batheesh 			names[j] = ext_ppcnt_cnts[i].name;
50899f876f3dSTalat Batheesh 			offsets[j] = ext_ppcnt_cnts[i].offset;
50909f876f3dSTalat Batheesh 		}
50919f876f3dSTalat Batheesh 	}
50920837e86aSMark Bloch }
50930837e86aSMark Bloch 
5094e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
50950837e86aSMark Bloch {
5096aac4492eSDaniel Jurgens 	int err = 0;
50970837e86aSMark Bloch 	int i;
50980837e86aSMark Bloch 
50990837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
5100aac4492eSDaniel Jurgens 		err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5101aac4492eSDaniel Jurgens 		if (err)
5102aac4492eSDaniel Jurgens 			goto err_alloc;
51037c16f477SKamal Heib 
5104aac4492eSDaniel Jurgens 		mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5105aac4492eSDaniel Jurgens 				      dev->port[i].cnts.offsets);
5106aac4492eSDaniel Jurgens 
5107aac4492eSDaniel Jurgens 		err = mlx5_core_alloc_q_counter(dev->mdev,
5108aac4492eSDaniel Jurgens 						&dev->port[i].cnts.set_id);
5109aac4492eSDaniel Jurgens 		if (err) {
51100837e86aSMark Bloch 			mlx5_ib_warn(dev,
51110837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
5112aac4492eSDaniel Jurgens 				     i + 1, err);
5113aac4492eSDaniel Jurgens 			goto err_alloc;
51140837e86aSMark Bloch 		}
5115aac4492eSDaniel Jurgens 		dev->port[i].cnts.set_id_valid = true;
51160837e86aSMark Bloch 	}
51170837e86aSMark Bloch 
51180837e86aSMark Bloch 	return 0;
51190837e86aSMark Bloch 
5120aac4492eSDaniel Jurgens err_alloc:
5121aac4492eSDaniel Jurgens 	mlx5_ib_dealloc_counters(dev);
5122aac4492eSDaniel Jurgens 	return err;
51230837e86aSMark Bloch }
51240837e86aSMark Bloch 
51250ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
51260ad17a8fSMark Bloch 						    u8 port_num)
51270ad17a8fSMark Bloch {
51287c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
51297c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
51300ad17a8fSMark Bloch 
51310ad17a8fSMark Bloch 	/* We support only per port stats */
51320ad17a8fSMark Bloch 	if (port_num == 0)
51330ad17a8fSMark Bloch 		return NULL;
51340ad17a8fSMark Bloch 
5135e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
5136e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
51379f876f3dSTalat Batheesh 					  port->cnts.num_cong_counters +
51389f876f3dSTalat Batheesh 					  port->cnts.num_ext_ppcnt_counters,
51390ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
51400ad17a8fSMark Bloch }
51410ad17a8fSMark Bloch 
5142aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
5143e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
5144e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
5145e1f24a79SParav Pandit {
5146e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5147e1f24a79SParav Pandit 	void *out;
5148e1f24a79SParav Pandit 	__be32 val;
5149e1f24a79SParav Pandit 	int ret, i;
5150e1f24a79SParav Pandit 
51511b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
5152e1f24a79SParav Pandit 	if (!out)
5153e1f24a79SParav Pandit 		return -ENOMEM;
5154e1f24a79SParav Pandit 
5155aac4492eSDaniel Jurgens 	ret = mlx5_core_query_q_counter(mdev,
5156e1f24a79SParav Pandit 					port->cnts.set_id, 0,
5157e1f24a79SParav Pandit 					out, outlen);
5158e1f24a79SParav Pandit 	if (ret)
5159e1f24a79SParav Pandit 		goto free;
5160e1f24a79SParav Pandit 
5161e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
5162e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
5163e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
5164e1f24a79SParav Pandit 	}
5165e1f24a79SParav Pandit 
5166e1f24a79SParav Pandit free:
5167e1f24a79SParav Pandit 	kvfree(out);
5168e1f24a79SParav Pandit 	return ret;
5169e1f24a79SParav Pandit }
5170e1f24a79SParav Pandit 
51719f876f3dSTalat Batheesh static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
51729f876f3dSTalat Batheesh 					  struct mlx5_ib_port *port,
51739f876f3dSTalat Batheesh 					  struct rdma_hw_stats *stats)
51749f876f3dSTalat Batheesh {
51759f876f3dSTalat Batheesh 	int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
51769f876f3dSTalat Batheesh 	int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
51779f876f3dSTalat Batheesh 	int ret, i;
51789f876f3dSTalat Batheesh 	void *out;
51799f876f3dSTalat Batheesh 
51809f876f3dSTalat Batheesh 	out = kvzalloc(sz, GFP_KERNEL);
51819f876f3dSTalat Batheesh 	if (!out)
51829f876f3dSTalat Batheesh 		return -ENOMEM;
51839f876f3dSTalat Batheesh 
51849f876f3dSTalat Batheesh 	ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
51859f876f3dSTalat Batheesh 	if (ret)
51869f876f3dSTalat Batheesh 		goto free;
51879f876f3dSTalat Batheesh 
51889f876f3dSTalat Batheesh 	for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
51899f876f3dSTalat Batheesh 		stats->value[i + offset] =
51909f876f3dSTalat Batheesh 			be64_to_cpup((__be64 *)(out +
51919f876f3dSTalat Batheesh 				    port->cnts.offsets[i + offset]));
51929f876f3dSTalat Batheesh 	}
51939f876f3dSTalat Batheesh 
51949f876f3dSTalat Batheesh free:
51959f876f3dSTalat Batheesh 	kvfree(out);
51969f876f3dSTalat Batheesh 	return ret;
51979f876f3dSTalat Batheesh }
51989f876f3dSTalat Batheesh 
51990ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
52000ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
52017c16f477SKamal Heib 				u8 port_num, int index)
52020ad17a8fSMark Bloch {
52030ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
52047c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
5205aac4492eSDaniel Jurgens 	struct mlx5_core_dev *mdev;
5206e1f24a79SParav Pandit 	int ret, num_counters;
5207aac4492eSDaniel Jurgens 	u8 mdev_port_num;
52080ad17a8fSMark Bloch 
52097c16f477SKamal Heib 	if (!stats)
5210e1f24a79SParav Pandit 		return -EINVAL;
52110ad17a8fSMark Bloch 
52129f876f3dSTalat Batheesh 	num_counters = port->cnts.num_q_counters +
52139f876f3dSTalat Batheesh 		       port->cnts.num_cong_counters +
52149f876f3dSTalat Batheesh 		       port->cnts.num_ext_ppcnt_counters;
5215aac4492eSDaniel Jurgens 
5216aac4492eSDaniel Jurgens 	/* q_counters are per IB device, query the master mdev */
5217aac4492eSDaniel Jurgens 	ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
52180ad17a8fSMark Bloch 	if (ret)
5219e1f24a79SParav Pandit 		return ret;
52200ad17a8fSMark Bloch 
52219f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
52229f876f3dSTalat Batheesh 		ret =  mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
52239f876f3dSTalat Batheesh 		if (ret)
52249f876f3dSTalat Batheesh 			return ret;
52259f876f3dSTalat Batheesh 	}
52269f876f3dSTalat Batheesh 
5227e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5228aac4492eSDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5229aac4492eSDaniel Jurgens 						    &mdev_port_num);
5230aac4492eSDaniel Jurgens 		if (!mdev) {
5231aac4492eSDaniel Jurgens 			/* If port is not affiliated yet, its in down state
5232aac4492eSDaniel Jurgens 			 * which doesn't have any counters yet, so it would be
5233aac4492eSDaniel Jurgens 			 * zero. So no need to read from the HCA.
5234aac4492eSDaniel Jurgens 			 */
5235aac4492eSDaniel Jurgens 			goto done;
5236aac4492eSDaniel Jurgens 		}
523771a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
523871a0ff65SMajd Dibbiny 						   stats->value +
523971a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters,
524071a0ff65SMajd Dibbiny 						   port->cnts.num_cong_counters,
524171a0ff65SMajd Dibbiny 						   port->cnts.offsets +
524271a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters);
5243aac4492eSDaniel Jurgens 
5244aac4492eSDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
5245e1f24a79SParav Pandit 		if (ret)
5246e1f24a79SParav Pandit 			return ret;
52470ad17a8fSMark Bloch 	}
52487c16f477SKamal Heib 
5249aac4492eSDaniel Jurgens done:
5250e1f24a79SParav Pandit 	return num_counters;
52510ad17a8fSMark Bloch }
52520ad17a8fSMark Bloch 
5253f6a8a19bSDenis Drozdov static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5254693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
5255f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
5256693dfd5aSErez Shitrit {
5257693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
5258f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
5259693dfd5aSErez Shitrit 
5260f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
5261693dfd5aSErez Shitrit }
5262693dfd5aSErez Shitrit 
5263fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5264fe248c3aSMaor Gottlieb {
5265fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
5266fe248c3aSMaor Gottlieb 		return;
5267fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5268fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
5269fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
5270fe248c3aSMaor Gottlieb }
5271fe248c3aSMaor Gottlieb 
527203404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
527303404e8aSMaor Gottlieb {
527403404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
527503404e8aSMaor Gottlieb 		return;
527603404e8aSMaor Gottlieb 
527703404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
5278fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5279fe248c3aSMaor Gottlieb }
5280fe248c3aSMaor Gottlieb 
5281fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5282fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
5283fe248c3aSMaor Gottlieb {
5284fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5285fe248c3aSMaor Gottlieb 	char lbuf[20];
5286fe248c3aSMaor Gottlieb 	int len;
5287fe248c3aSMaor Gottlieb 
5288fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5289fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
5290fe248c3aSMaor Gottlieb }
5291fe248c3aSMaor Gottlieb 
5292fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5293fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
5294fe248c3aSMaor Gottlieb {
5295fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5296fe248c3aSMaor Gottlieb 	u32 timeout;
5297fe248c3aSMaor Gottlieb 	u32 var;
5298fe248c3aSMaor Gottlieb 
5299fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
5300fe248c3aSMaor Gottlieb 		return -EFAULT;
5301fe248c3aSMaor Gottlieb 
5302fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5303fe248c3aSMaor Gottlieb 			1000);
5304fe248c3aSMaor Gottlieb 	if (timeout != var)
5305fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5306fe248c3aSMaor Gottlieb 			    timeout);
5307fe248c3aSMaor Gottlieb 
5308fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
5309fe248c3aSMaor Gottlieb 
5310fe248c3aSMaor Gottlieb 	return count;
5311fe248c3aSMaor Gottlieb }
5312fe248c3aSMaor Gottlieb 
5313fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
5314fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
5315fe248c3aSMaor Gottlieb 	.open	= simple_open,
5316fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
5317fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
5318fe248c3aSMaor Gottlieb };
5319fe248c3aSMaor Gottlieb 
5320fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5321fe248c3aSMaor Gottlieb {
5322fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
5323fe248c3aSMaor Gottlieb 
5324fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
5325fe248c3aSMaor Gottlieb 		return 0;
5326fe248c3aSMaor Gottlieb 
5327fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5328fe248c3aSMaor Gottlieb 	if (!dbg)
5329fe248c3aSMaor Gottlieb 		return -ENOMEM;
5330fe248c3aSMaor Gottlieb 
5331cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
5332cbafad87SSudip Mukherjee 
5333fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
5334fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
5335fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
5336fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
5337cbafad87SSudip Mukherjee 		goto out_debugfs;
5338fe248c3aSMaor Gottlieb 
5339fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
5340fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
5341fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5342fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
5343fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
5344fe248c3aSMaor Gottlieb 		goto out_debugfs;
5345fe248c3aSMaor Gottlieb 
5346fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
5347fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
5348fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5349fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
5350fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
5351fe248c3aSMaor Gottlieb 		goto out_debugfs;
5352fe248c3aSMaor Gottlieb 
5353fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
5354fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
5355fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
5356fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
5357fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
5358fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
5359fe248c3aSMaor Gottlieb 		goto out_debugfs;
5360fe248c3aSMaor Gottlieb 
5361fe248c3aSMaor Gottlieb 	return 0;
5362fe248c3aSMaor Gottlieb 
5363fe248c3aSMaor Gottlieb out_debugfs:
5364fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5365fe248c3aSMaor Gottlieb 	return -ENOMEM;
536603404e8aSMaor Gottlieb }
536703404e8aSMaor Gottlieb 
536803404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
536903404e8aSMaor Gottlieb {
537003404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
537103404e8aSMaor Gottlieb 		return;
537203404e8aSMaor Gottlieb 
537303404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
537403404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
537503404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
537603404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
537703404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
5378fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
5379fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
5380fe248c3aSMaor Gottlieb 
5381fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
5382fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
538303404e8aSMaor Gottlieb }
538403404e8aSMaor Gottlieb 
538584305d71SLeon Romanovsky static const struct cpumask *
538684305d71SLeon Romanovsky mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
538740b24403SSagi Grimberg {
538840b24403SSagi Grimberg 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
538940b24403SSagi Grimberg 
5390f2f3df55SSaeed Mahameed 	return mlx5_comp_irq_get_affinity_mask(dev->mdev, comp_vector);
539140b24403SSagi Grimberg }
539240b24403SSagi Grimberg 
539332f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
539432f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
539532f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
539632f69e4bSDaniel Jurgens {
539732f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
539832f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
539932f69e4bSDaniel Jurgens 	int comps;
540032f69e4bSDaniel Jurgens 	int err;
540132f69e4bSDaniel Jurgens 	int i;
540232f69e4bSDaniel Jurgens 
5403a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5404a9e546e7SParav Pandit 
540532f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
540632f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
540732f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
540832f69e4bSDaniel Jurgens 		return;
540932f69e4bSDaniel Jurgens 	}
5410df097a27SSaeed Mahameed 
5411df097a27SSaeed Mahameed 	if (mpi->mdev_events.notifier_call)
5412df097a27SSaeed Mahameed 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
5413df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
5414df097a27SSaeed Mahameed 
541532f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
541632f69e4bSDaniel Jurgens 
541732f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
541832f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
541932f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
542032f69e4bSDaniel Jurgens 
542132f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
542232f69e4bSDaniel Jurgens 	if (comps) {
542332f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
542432f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
542532f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
542632f69e4bSDaniel Jurgens 
542732f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
542832f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
542932f69e4bSDaniel Jurgens 
543032f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
543132f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
543232f69e4bSDaniel Jurgens 	}
543332f69e4bSDaniel Jurgens 
543432f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
543532f69e4bSDaniel Jurgens 
543632f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
543732f69e4bSDaniel Jurgens 
543832f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
543932f69e4bSDaniel Jurgens 
544032f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
544132f69e4bSDaniel Jurgens 
544232f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
544332f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
544432f69e4bSDaniel Jurgens 	 * it back to the list.
544532f69e4bSDaniel Jurgens 	 */
544632f69e4bSDaniel Jurgens 	if (err)
544732f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
544832f69e4bSDaniel Jurgens 			    port_num + 1);
544932f69e4bSDaniel Jurgens 
545032f69e4bSDaniel Jurgens 	ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
545132f69e4bSDaniel Jurgens }
545232f69e4bSDaniel Jurgens 
545332f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
545432f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
545532f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
545632f69e4bSDaniel Jurgens {
545732f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
545832f69e4bSDaniel Jurgens 	int err;
545932f69e4bSDaniel Jurgens 
546032f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
546132f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
54622577188eSQing Huang 		mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
546332f69e4bSDaniel Jurgens 			    port_num + 1);
546432f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
546532f69e4bSDaniel Jurgens 		return false;
546632f69e4bSDaniel Jurgens 	}
546732f69e4bSDaniel Jurgens 
546832f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
546932f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
5470df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
547132f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
547232f69e4bSDaniel Jurgens 
547332f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
547432f69e4bSDaniel Jurgens 	if (err)
547532f69e4bSDaniel Jurgens 		goto unbind;
547632f69e4bSDaniel Jurgens 
547732f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
547832f69e4bSDaniel Jurgens 	if (err)
547932f69e4bSDaniel Jurgens 		goto unbind;
548032f69e4bSDaniel Jurgens 
548132f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
548232f69e4bSDaniel Jurgens 	if (err) {
548332f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
548432f69e4bSDaniel Jurgens 			    port_num + 1);
548532f69e4bSDaniel Jurgens 		goto unbind;
548632f69e4bSDaniel Jurgens 	}
548732f69e4bSDaniel Jurgens 
5488df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5489df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5490df097a27SSaeed Mahameed 
5491a9e546e7SParav Pandit 	err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
5492a9e546e7SParav Pandit 	if (err)
5493a9e546e7SParav Pandit 		goto unbind;
5494a9e546e7SParav Pandit 
549532f69e4bSDaniel Jurgens 	return true;
549632f69e4bSDaniel Jurgens 
549732f69e4bSDaniel Jurgens unbind:
549832f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
549932f69e4bSDaniel Jurgens 	return false;
550032f69e4bSDaniel Jurgens }
550132f69e4bSDaniel Jurgens 
550232f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
550332f69e4bSDaniel Jurgens {
550432f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
550532f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
550632f69e4bSDaniel Jurgens 							  port_num + 1);
550732f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
550832f69e4bSDaniel Jurgens 	int err;
550932f69e4bSDaniel Jurgens 	int i;
551032f69e4bSDaniel Jurgens 
551132f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
551232f69e4bSDaniel Jurgens 		return 0;
551332f69e4bSDaniel Jurgens 
551432f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
551532f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
551632f69e4bSDaniel Jurgens 	if (err)
551732f69e4bSDaniel Jurgens 		return err;
551832f69e4bSDaniel Jurgens 
551932f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
552032f69e4bSDaniel Jurgens 	if (err)
552132f69e4bSDaniel Jurgens 		return err;
552232f69e4bSDaniel Jurgens 
552332f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
552432f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
552532f69e4bSDaniel Jurgens 		bool bound = false;
552632f69e4bSDaniel Jurgens 
552732f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
552832f69e4bSDaniel Jurgens 		if (i == port_num) {
552932f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
553032f69e4bSDaniel Jurgens 			if (!mpi) {
553132f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
553232f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
553332f69e4bSDaniel Jurgens 				return -ENOMEM;
553432f69e4bSDaniel Jurgens 			}
553532f69e4bSDaniel Jurgens 
553632f69e4bSDaniel Jurgens 			mpi->is_master = true;
553732f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
553832f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
553932f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
554032f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
554132f69e4bSDaniel Jurgens 			mpi = NULL;
554232f69e4bSDaniel Jurgens 			continue;
554332f69e4bSDaniel Jurgens 		}
554432f69e4bSDaniel Jurgens 
554532f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
554632f69e4bSDaniel Jurgens 				    list) {
554732f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
554832f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
554932f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
555032f69e4bSDaniel Jurgens 			}
555132f69e4bSDaniel Jurgens 
555232f69e4bSDaniel Jurgens 			if (bound) {
555332f69e4bSDaniel Jurgens 				dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
555432f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
555532f69e4bSDaniel Jurgens 				list_del(&mpi->list);
555632f69e4bSDaniel Jurgens 				break;
555732f69e4bSDaniel Jurgens 			}
555832f69e4bSDaniel Jurgens 		}
555932f69e4bSDaniel Jurgens 		if (!bound) {
556032f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
556132f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
556232f69e4bSDaniel Jurgens 				    i + 1);
556332f69e4bSDaniel Jurgens 		}
556432f69e4bSDaniel Jurgens 	}
556532f69e4bSDaniel Jurgens 
556632f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
556732f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
556832f69e4bSDaniel Jurgens 	return err;
556932f69e4bSDaniel Jurgens }
557032f69e4bSDaniel Jurgens 
557132f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
557232f69e4bSDaniel Jurgens {
557332f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
557432f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
557532f69e4bSDaniel Jurgens 							  port_num + 1);
557632f69e4bSDaniel Jurgens 	int i;
557732f69e4bSDaniel Jurgens 
557832f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
557932f69e4bSDaniel Jurgens 		return;
558032f69e4bSDaniel Jurgens 
558132f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
558232f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
558332f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
558432f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
558532f69e4bSDaniel Jurgens 			if (i == port_num) {
558632f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
558732f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
558832f69e4bSDaniel Jurgens 			} else {
558932f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
559032f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
559132f69e4bSDaniel Jurgens 			}
559232f69e4bSDaniel Jurgens 		}
559332f69e4bSDaniel Jurgens 	}
559432f69e4bSDaniel Jurgens 
559532f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
559632f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
559732f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
559832f69e4bSDaniel Jurgens 
559932f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
560032f69e4bSDaniel Jurgens }
560132f69e4bSDaniel Jurgens 
56029a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
56039a119cd5SJason Gunthorpe 	mlx5_ib_dm,
56049a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
560524da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
56069a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
560724da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
560883bb4442SJason Gunthorpe 			    UA_MANDATORY),
56099a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
561024da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
561183bb4442SJason Gunthorpe 			    UA_MANDATORY));
561224da0016SAriel Levkovich 
56139a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
56149a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
56159a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
5616c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
5617bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
5618bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
5619c6475a0bSAviad Yehezkel 
56208c84660bSMatan Barak static int populate_specs_root(struct mlx5_ib_dev *dev)
56218c84660bSMatan Barak {
56227d96c9b1SJason Gunthorpe 	const struct uverbs_object_tree_def **trees = dev->driver_trees;
56237d96c9b1SJason Gunthorpe 	size_t num_trees = 0;
56248c84660bSMatan Barak 
56257d96c9b1SJason Gunthorpe 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
56267d96c9b1SJason Gunthorpe 	    MLX5_ACCEL_IPSEC_CAP_DEVICE)
56277d96c9b1SJason Gunthorpe 		trees[num_trees++] = &mlx5_ib_flow_action;
5628c6475a0bSAviad Yehezkel 
56297d96c9b1SJason Gunthorpe 	if (MLX5_CAP_DEV_MEM(dev->mdev, memic))
56307d96c9b1SJason Gunthorpe 		trees[num_trees++] = &mlx5_ib_dm;
563124da0016SAriel Levkovich 
5632c59450c4SYishai Hadas 	if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
56337d96c9b1SJason Gunthorpe 	    MLX5_GENERAL_OBJ_TYPES_CAP_UCTX)
56347d96c9b1SJason Gunthorpe 		trees[num_trees++] = mlx5_ib_get_devx_tree();
5635c59450c4SYishai Hadas 
56367d96c9b1SJason Gunthorpe 	num_trees += mlx5_ib_get_flow_trees(trees + num_trees);
5637cb80fb18SYishai Hadas 
56387d96c9b1SJason Gunthorpe 	WARN_ON(num_trees >= ARRAY_SIZE(dev->driver_trees));
56397d96c9b1SJason Gunthorpe 	trees[num_trees] = NULL;
56407d96c9b1SJason Gunthorpe 	dev->ib_dev.driver_specs = trees;
56418c84660bSMatan Barak 
56427d96c9b1SJason Gunthorpe 	return 0;
56438c84660bSMatan Barak }
56448c84660bSMatan Barak 
56451a1e03dcSRaed Salem static int mlx5_ib_read_counters(struct ib_counters *counters,
56461a1e03dcSRaed Salem 				 struct ib_counters_read_attr *read_attr,
56471a1e03dcSRaed Salem 				 struct uverbs_attr_bundle *attrs)
56481a1e03dcSRaed Salem {
56491a1e03dcSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
56501a1e03dcSRaed Salem 	struct mlx5_read_counters_attr mread_attr = {};
56511a1e03dcSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc;
56521a1e03dcSRaed Salem 	int ret, i;
56531a1e03dcSRaed Salem 
56541a1e03dcSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
56551a1e03dcSRaed Salem 	if (mcounters->cntrs_max_index > read_attr->ncounters) {
56561a1e03dcSRaed Salem 		ret = -EINVAL;
56571a1e03dcSRaed Salem 		goto err_bound;
56581a1e03dcSRaed Salem 	}
56591a1e03dcSRaed Salem 
56601a1e03dcSRaed Salem 	mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
56611a1e03dcSRaed Salem 				 GFP_KERNEL);
56621a1e03dcSRaed Salem 	if (!mread_attr.out) {
56631a1e03dcSRaed Salem 		ret = -ENOMEM;
56641a1e03dcSRaed Salem 		goto err_bound;
56651a1e03dcSRaed Salem 	}
56661a1e03dcSRaed Salem 
56671a1e03dcSRaed Salem 	mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
56681a1e03dcSRaed Salem 	mread_attr.flags = read_attr->flags;
56691a1e03dcSRaed Salem 	ret = mcounters->read_counters(counters->device, &mread_attr);
56701a1e03dcSRaed Salem 	if (ret)
56711a1e03dcSRaed Salem 		goto err_read;
56721a1e03dcSRaed Salem 
56731a1e03dcSRaed Salem 	/* do the pass over the counters data array to assign according to the
56741a1e03dcSRaed Salem 	 * descriptions and indexing pairs
56751a1e03dcSRaed Salem 	 */
56761a1e03dcSRaed Salem 	desc = mcounters->counters_data;
56771a1e03dcSRaed Salem 	for (i = 0; i < mcounters->ncounters; i++)
56781a1e03dcSRaed Salem 		read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
56791a1e03dcSRaed Salem 
56801a1e03dcSRaed Salem err_read:
56811a1e03dcSRaed Salem 	kfree(mread_attr.out);
56821a1e03dcSRaed Salem err_bound:
56831a1e03dcSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
56841a1e03dcSRaed Salem 	return ret;
56851a1e03dcSRaed Salem }
56861a1e03dcSRaed Salem 
5687b29e2a13SRaed Salem static int mlx5_ib_destroy_counters(struct ib_counters *counters)
5688b29e2a13SRaed Salem {
5689b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5690b29e2a13SRaed Salem 
56913b3233fbSRaed Salem 	counters_clear_description(counters);
56923b3233fbSRaed Salem 	if (mcounters->hw_cntrs_hndl)
56933b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(counters->device)->mdev,
56943b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
56953b3233fbSRaed Salem 
5696b29e2a13SRaed Salem 	kfree(mcounters);
5697b29e2a13SRaed Salem 
5698b29e2a13SRaed Salem 	return 0;
5699b29e2a13SRaed Salem }
5700b29e2a13SRaed Salem 
5701b29e2a13SRaed Salem static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
5702b29e2a13SRaed Salem 						   struct uverbs_attr_bundle *attrs)
5703b29e2a13SRaed Salem {
5704b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters;
5705b29e2a13SRaed Salem 
5706b29e2a13SRaed Salem 	mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
5707b29e2a13SRaed Salem 	if (!mcounters)
5708b29e2a13SRaed Salem 		return ERR_PTR(-ENOMEM);
5709b29e2a13SRaed Salem 
57103b3233fbSRaed Salem 	mutex_init(&mcounters->mcntrs_mutex);
57113b3233fbSRaed Salem 
5712b29e2a13SRaed Salem 	return &mcounters->ibcntrs;
5713b29e2a13SRaed Salem }
5714b29e2a13SRaed Salem 
5715b5ca15adSMark Bloch void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
5716e126ba97SEli Cohen {
571732f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
57183cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
57193cc297dbSMark Bloch 	cleanup_srcu_struct(&dev->mr_srcu);
57203cc297dbSMark Bloch #endif
572116c1975fSMark Bloch 	kfree(dev->port);
572216c1975fSMark Bloch }
572316c1975fSMark Bloch 
5724b5ca15adSMark Bloch int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
572516c1975fSMark Bloch {
572616c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
5727e126ba97SEli Cohen 	int err;
572832f69e4bSDaniel Jurgens 	int i;
5729e126ba97SEli Cohen 
5730508562d6SDaniel Jurgens 	dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
57310837e86aSMark Bloch 			    GFP_KERNEL);
57320837e86aSMark Bloch 	if (!dev->port)
573316c1975fSMark Bloch 		return -ENOMEM;
57340837e86aSMark Bloch 
573532f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
573632f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
573732f69e4bSDaniel Jurgens 		rwlock_init(&dev->roce[i].netdev_lock);
573832f69e4bSDaniel Jurgens 	}
573932f69e4bSDaniel Jurgens 
574032f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
5741e126ba97SEli Cohen 	if (err)
57420837e86aSMark Bloch 		goto err_free_port;
5743e126ba97SEli Cohen 
574432f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
574532f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
574632f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
574732f69e4bSDaniel Jurgens 			if (err)
574832f69e4bSDaniel Jurgens 				break;
574932f69e4bSDaniel Jurgens 		}
575032f69e4bSDaniel Jurgens 	} else {
575132f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
575232f69e4bSDaniel Jurgens 	}
575332f69e4bSDaniel Jurgens 	if (err)
575432f69e4bSDaniel Jurgens 		goto err_mp;
575532f69e4bSDaniel Jurgens 
57561b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
5757e126ba97SEli Cohen 		get_ext_port_caps(dev);
5758e126ba97SEli Cohen 
5759e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
5760e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
5761c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
5762e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
5763f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
57649b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
5765e126ba97SEli Cohen 
57663cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
57673cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
57683cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
57693cc297dbSMark Bloch 
577024da0016SAriel Levkovich 	spin_lock_init(&dev->memic.memic_lock);
577124da0016SAriel Levkovich 	dev->memic.dev = mdev;
577224da0016SAriel Levkovich 
57733cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
57743cc297dbSMark Bloch 	err = init_srcu_struct(&dev->mr_srcu);
57753cc297dbSMark Bloch 	if (err)
57763cc297dbSMark Bloch 		goto err_free_port;
57773cc297dbSMark Bloch #endif
57783cc297dbSMark Bloch 
577916c1975fSMark Bloch 	return 0;
578032f69e4bSDaniel Jurgens err_mp:
578132f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
578216c1975fSMark Bloch 
578316c1975fSMark Bloch err_free_port:
578416c1975fSMark Bloch 	kfree(dev->port);
578516c1975fSMark Bloch 
578616c1975fSMark Bloch 	return -ENOMEM;
578716c1975fSMark Bloch }
578816c1975fSMark Bloch 
57899a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
57909a4ca38dSMark Bloch {
57919a4ca38dSMark Bloch 	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
57929a4ca38dSMark Bloch 
57939a4ca38dSMark Bloch 	if (!dev->flow_db)
57949a4ca38dSMark Bloch 		return -ENOMEM;
57959a4ca38dSMark Bloch 
57969a4ca38dSMark Bloch 	mutex_init(&dev->flow_db->lock);
57979a4ca38dSMark Bloch 
57989a4ca38dSMark Bloch 	return 0;
57999a4ca38dSMark Bloch }
58009a4ca38dSMark Bloch 
5801b5ca15adSMark Bloch int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
5802b5ca15adSMark Bloch {
5803b5ca15adSMark Bloch 	struct mlx5_ib_dev *nic_dev;
5804b5ca15adSMark Bloch 
5805b5ca15adSMark Bloch 	nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
5806b5ca15adSMark Bloch 
5807b5ca15adSMark Bloch 	if (!nic_dev)
5808b5ca15adSMark Bloch 		return -EINVAL;
5809b5ca15adSMark Bloch 
5810b5ca15adSMark Bloch 	dev->flow_db = nic_dev->flow_db;
5811b5ca15adSMark Bloch 
5812b5ca15adSMark Bloch 	return 0;
5813b5ca15adSMark Bloch }
5814b5ca15adSMark Bloch 
58159a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
58169a4ca38dSMark Bloch {
58179a4ca38dSMark Bloch 	kfree(dev->flow_db);
58189a4ca38dSMark Bloch }
58199a4ca38dSMark Bloch 
5820b5ca15adSMark Bloch int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
582116c1975fSMark Bloch {
582216c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
582316c1975fSMark Bloch 	int err;
582416c1975fSMark Bloch 
5825e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
5826e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
5827e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
5828e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
5829e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
5830e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
5831e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
583241c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
583341c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
5834e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
583556e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
5836e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
5837e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
5838e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
5839e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
5840e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
5841e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
5842e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
5843e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
5844e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
5845e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
5846e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
5847e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
5848e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
5849e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
5850e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
5851e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
5852e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
58531707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
5854d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
5855d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
58567d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
5857b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
5858b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
5859e126ba97SEli Cohen 
5860e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
5861ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
5862e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
58633cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
58643cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
5865e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
5866e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
5867e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
5868e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
5869e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
5870e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
5871e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
5872e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
5873e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
5874e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
5875e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
5876e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
5877e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
5878e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
5879e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
5880e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
5881e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
5882e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
5883e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
5884e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
5885d0e84c0aSYishai Hadas 	dev->ib_dev.drain_sq		= mlx5_ib_drain_sq;
5886d0e84c0aSYishai Hadas 	dev->ib_dev.drain_rq		= mlx5_ib_drain_rq;
5887e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
5888e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
5889e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
5890e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
5891e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
5892e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
5893e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
5894e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
5895e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
5896e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
589756e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
5898e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
5899e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
5900e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
5901e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
59029bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
59038a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
5904d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
5905c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
590640b24403SSagi Grimberg 	dev->ib_dev.get_vector_affinity	= mlx5_ib_get_vector_affinity;
5907f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
5908f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
5909f6a8a19bSDenis Drozdov 		dev->ib_dev.rdma_netdev_get_params = mlx5_ib_rn_get_params;
59108e959601SNiranjana Vishwanathapura 
5911eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
5912eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
5913eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
5914eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
5915eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
5916eff901d3SEli Cohen 	}
5917e126ba97SEli Cohen 
59187c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
59197c2344c3SMaor Gottlieb 
59206e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
59216e8484c5SMax Gurtovoy 
5922d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
5923d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
5924d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
5925d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
5926d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
5927d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
5928d2370e0aSMatan Barak 	}
5929d2370e0aSMatan Barak 
5930938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
5931e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
5932e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
5933e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
5934e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
5935e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
5936e126ba97SEli Cohen 	}
5937e126ba97SEli Cohen 
593824da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
593924da0016SAriel Levkovich 		dev->ib_dev.alloc_dm = mlx5_ib_alloc_dm;
594024da0016SAriel Levkovich 		dev->ib_dev.dealloc_dm = mlx5_ib_dealloc_dm;
59416c29f57eSAriel Levkovich 		dev->ib_dev.reg_dm_mr = mlx5_ib_reg_dm_mr;
594224da0016SAriel Levkovich 	}
594324da0016SAriel Levkovich 
5944038d2ef8SMaor Gottlieb 	dev->ib_dev.create_flow	= mlx5_ib_create_flow;
5945038d2ef8SMaor Gottlieb 	dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
594681e30880SYishai Hadas 	dev->ib_dev.uverbs_ex_cmd_mask |=
594781e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
594881e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
5949c6475a0bSAviad Yehezkel 	dev->ib_dev.create_flow_action_esp = mlx5_ib_create_flow_action_esp;
5950c6475a0bSAviad Yehezkel 	dev->ib_dev.destroy_flow_action = mlx5_ib_destroy_flow_action;
5951349705c1SMatan Barak 	dev->ib_dev.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp;
59520ede73bcSMatan Barak 	dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
5953b29e2a13SRaed Salem 	dev->ib_dev.create_counters = mlx5_ib_create_counters;
5954b29e2a13SRaed Salem 	dev->ib_dev.destroy_counters = mlx5_ib_destroy_counters;
59551a1e03dcSRaed Salem 	dev->ib_dev.read_counters = mlx5_ib_read_counters;
595681e30880SYishai Hadas 
5957e126ba97SEli Cohen 	err = init_node_data(dev);
5958e126ba97SEli Cohen 	if (err)
595916c1975fSMark Bloch 		return err;
5960e126ba97SEli Cohen 
5961c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
5962e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
5963e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
5964a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
5965c8b89924SMark Bloch 
596616c1975fSMark Bloch 	return 0;
596716c1975fSMark Bloch }
596816c1975fSMark Bloch 
59698e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
597016c1975fSMark Bloch {
59718e6efa3aSMark Bloch 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
59728e6efa3aSMark Bloch 	dev->ib_dev.query_port		= mlx5_ib_query_port;
59738e6efa3aSMark Bloch 
59748e6efa3aSMark Bloch 	return 0;
59758e6efa3aSMark Bloch }
59768e6efa3aSMark Bloch 
5977b5ca15adSMark Bloch int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
59788e6efa3aSMark Bloch {
59798e6efa3aSMark Bloch 	dev->ib_dev.get_port_immutable  = mlx5_port_rep_immutable;
59808e6efa3aSMark Bloch 	dev->ib_dev.query_port		= mlx5_ib_rep_query_port;
59818e6efa3aSMark Bloch 
59828e6efa3aSMark Bloch 	return 0;
59838e6efa3aSMark Bloch }
59848e6efa3aSMark Bloch 
5985e3f1ed1fSLeon Romanovsky static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
59868e6efa3aSMark Bloch {
5987e3f1ed1fSLeon Romanovsky 	u8 port_num;
59887fd8aefbSDaniel Jurgens 	int i;
598916c1975fSMark Bloch 
59907fd8aefbSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
59917fd8aefbSDaniel Jurgens 		dev->roce[i].dev = dev;
59927fd8aefbSDaniel Jurgens 		dev->roce[i].native_port_num = i + 1;
59937fd8aefbSDaniel Jurgens 		dev->roce[i].last_port_state = IB_PORT_DOWN;
59947fd8aefbSDaniel Jurgens 	}
59957fd8aefbSDaniel Jurgens 
5996c11a226aSMark Bloch 	dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
5997c11a226aSMark Bloch 	dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
5998c11a226aSMark Bloch 	dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
5999c11a226aSMark Bloch 	dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
6000c11a226aSMark Bloch 	dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
6001c11a226aSMark Bloch 	dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
60028e6efa3aSMark Bloch 
6003c11a226aSMark Bloch 	dev->ib_dev.uverbs_ex_cmd_mask |=
6004c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6005c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6006c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6007c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6008c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
60098e6efa3aSMark Bloch 
6010e3f1ed1fSLeon Romanovsky 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6011e3f1ed1fSLeon Romanovsky 
60128e6efa3aSMark Bloch 	return mlx5_add_netdev_notifier(dev, port_num);
60138e6efa3aSMark Bloch }
60148e6efa3aSMark Bloch 
60158e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
60168e6efa3aSMark Bloch {
60178e6efa3aSMark Bloch 	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
60188e6efa3aSMark Bloch 
60198e6efa3aSMark Bloch 	mlx5_remove_netdev_notifier(dev, port_num);
60208e6efa3aSMark Bloch }
60218e6efa3aSMark Bloch 
60228e6efa3aSMark Bloch int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
60238e6efa3aSMark Bloch {
60248e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
60258e6efa3aSMark Bloch 	enum rdma_link_layer ll;
60268e6efa3aSMark Bloch 	int port_type_cap;
60278e6efa3aSMark Bloch 	int err = 0;
60288e6efa3aSMark Bloch 
60298e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
60308e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
60318e6efa3aSMark Bloch 
60328e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET)
6033e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
60348e6efa3aSMark Bloch 
603516c1975fSMark Bloch 	return err;
6036fc24fc5eSAchiad Shochat }
6037fc24fc5eSAchiad Shochat 
60388e6efa3aSMark Bloch void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
60398e6efa3aSMark Bloch {
60408e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
60418e6efa3aSMark Bloch }
60428e6efa3aSMark Bloch 
60438e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
60448e6efa3aSMark Bloch {
60458e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
60468e6efa3aSMark Bloch 	enum rdma_link_layer ll;
60478e6efa3aSMark Bloch 	int port_type_cap;
60488e6efa3aSMark Bloch 	int err;
60498e6efa3aSMark Bloch 
60508e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
60518e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
60528e6efa3aSMark Bloch 
60538e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
6054e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
60558e6efa3aSMark Bloch 		if (err)
60568e6efa3aSMark Bloch 			return err;
60578e6efa3aSMark Bloch 
6058e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
60598e6efa3aSMark Bloch 		if (err)
60608e6efa3aSMark Bloch 			goto cleanup;
60618e6efa3aSMark Bloch 	}
60628e6efa3aSMark Bloch 
606316c1975fSMark Bloch 	return 0;
60648e6efa3aSMark Bloch cleanup:
60658e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
60668e6efa3aSMark Bloch 
60678e6efa3aSMark Bloch 	return err;
606845bded2cSKamal Heib }
60696aec21f6SHaggai Eran 
607016c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
607116c1975fSMark Bloch {
607216c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
607316c1975fSMark Bloch 	enum rdma_link_layer ll;
607416c1975fSMark Bloch 	int port_type_cap;
60754a2da0b8SParav Pandit 
607616c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
607716c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
607816c1975fSMark Bloch 
607916c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
608016c1975fSMark Bloch 		mlx5_disable_eth(dev);
60818e6efa3aSMark Bloch 		mlx5_ib_stage_common_roce_cleanup(dev);
608216c1975fSMark Bloch 	}
608316c1975fSMark Bloch }
608416c1975fSMark Bloch 
6085b5ca15adSMark Bloch int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
608616c1975fSMark Bloch {
608716c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
608816c1975fSMark Bloch }
608916c1975fSMark Bloch 
6090b5ca15adSMark Bloch void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
609116c1975fSMark Bloch {
609216c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
609316c1975fSMark Bloch }
609416c1975fSMark Bloch 
609516c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
609616c1975fSMark Bloch {
609707321b3cSMark Bloch 	mlx5_ib_internal_fill_odp_caps(dev);
609807321b3cSMark Bloch 
609916c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
610016c1975fSMark Bloch }
610116c1975fSMark Bloch 
6102d5d284b8SSaeed Mahameed void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
6103d5d284b8SSaeed Mahameed {
6104d5d284b8SSaeed Mahameed 	mlx5_ib_odp_cleanup_one(dev);
6105d5d284b8SSaeed Mahameed }
6106d5d284b8SSaeed Mahameed 
6107b5ca15adSMark Bloch int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
610816c1975fSMark Bloch {
61095e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
61105e1e7612SMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
61115e1e7612SMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
61125e1e7612SMark Bloch 
611316c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
61145e1e7612SMark Bloch 	}
611516c1975fSMark Bloch 
611616c1975fSMark Bloch 	return 0;
611716c1975fSMark Bloch }
611816c1975fSMark Bloch 
6119b5ca15adSMark Bloch void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
612016c1975fSMark Bloch {
612116c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
612216c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
612316c1975fSMark Bloch }
612416c1975fSMark Bloch 
612516c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
612616c1975fSMark Bloch {
6127a9e546e7SParav Pandit 	return mlx5_ib_init_cong_debugfs(dev,
6128a9e546e7SParav Pandit 					 mlx5_core_native_port_num(dev->mdev) - 1);
612916c1975fSMark Bloch }
613016c1975fSMark Bloch 
613116c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
613216c1975fSMark Bloch {
6133a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
6134a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
613516c1975fSMark Bloch }
613616c1975fSMark Bloch 
613716c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
613816c1975fSMark Bloch {
61395fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
6140444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
614116c1975fSMark Bloch }
614216c1975fSMark Bloch 
614316c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
614416c1975fSMark Bloch {
614516c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
614616c1975fSMark Bloch }
614716c1975fSMark Bloch 
6148b5ca15adSMark Bloch int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
614916c1975fSMark Bloch {
615016c1975fSMark Bloch 	int err;
61515fe9dec0SEli Cohen 
61525fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
61535fe9dec0SEli Cohen 	if (err)
615416c1975fSMark Bloch 		return err;
61555fe9dec0SEli Cohen 
61565fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
61575fe9dec0SEli Cohen 	if (err)
615816c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
61595fe9dec0SEli Cohen 
616016c1975fSMark Bloch 	return err;
616116c1975fSMark Bloch }
61620837e86aSMark Bloch 
6163b5ca15adSMark Bloch void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
616416c1975fSMark Bloch {
616516c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
616616c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
616716c1975fSMark Bloch }
6168e126ba97SEli Cohen 
61698c84660bSMatan Barak static int mlx5_ib_stage_populate_specs(struct mlx5_ib_dev *dev)
61708c84660bSMatan Barak {
61718c84660bSMatan Barak 	return populate_specs_root(dev);
61728c84660bSMatan Barak }
61738c84660bSMatan Barak 
6174b5ca15adSMark Bloch int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
617516c1975fSMark Bloch {
6176e349f858SJason Gunthorpe 	const char *name;
6177e349f858SJason Gunthorpe 
6178508a523fSParav Pandit 	rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
6179*7c34ec19SAviv Heller 	if (!mlx5_lag_is_roce(dev->mdev))
6180e349f858SJason Gunthorpe 		name = "mlx5_%d";
6181e349f858SJason Gunthorpe 	else
6182e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
6183e349f858SJason Gunthorpe 	return ib_register_device(&dev->ib_dev, name, NULL);
618416c1975fSMark Bloch }
618516c1975fSMark Bloch 
618603fe2debSDavid S. Miller void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
618742cea83fSMark Bloch {
618842cea83fSMark Bloch 	destroy_umrc_res(dev);
618942cea83fSMark Bloch }
619042cea83fSMark Bloch 
6191b5ca15adSMark Bloch void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
619216c1975fSMark Bloch {
619316c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
619416c1975fSMark Bloch }
619516c1975fSMark Bloch 
619603fe2debSDavid S. Miller int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
619716c1975fSMark Bloch {
619816c1975fSMark Bloch 	return create_umr_res(dev);
619916c1975fSMark Bloch }
620016c1975fSMark Bloch 
620116c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
620216c1975fSMark Bloch {
620303404e8aSMaor Gottlieb 	init_delay_drop(dev);
620403404e8aSMaor Gottlieb 
620516c1975fSMark Bloch 	return 0;
620616c1975fSMark Bloch }
620716c1975fSMark Bloch 
620816c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
620916c1975fSMark Bloch {
621016c1975fSMark Bloch 	cancel_delay_drop(dev);
621116c1975fSMark Bloch }
621216c1975fSMark Bloch 
6213df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
6214df097a27SSaeed Mahameed {
6215df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
6216df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
6217df097a27SSaeed Mahameed 	return 0;
6218df097a27SSaeed Mahameed }
6219df097a27SSaeed Mahameed 
6220df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
6221df097a27SSaeed Mahameed {
6222df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
6223df097a27SSaeed Mahameed }
6224df097a27SSaeed Mahameed 
622581773ce5SLeon Romanovsky static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
622681773ce5SLeon Romanovsky {
622781773ce5SLeon Romanovsky 	int uid;
622881773ce5SLeon Romanovsky 
622981773ce5SLeon Romanovsky 	uid = mlx5_ib_devx_create(dev);
623081773ce5SLeon Romanovsky 	if (uid > 0)
623181773ce5SLeon Romanovsky 		dev->devx_whitelist_uid = uid;
623281773ce5SLeon Romanovsky 
623381773ce5SLeon Romanovsky 	return 0;
623481773ce5SLeon Romanovsky }
623581773ce5SLeon Romanovsky static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
623681773ce5SLeon Romanovsky {
623781773ce5SLeon Romanovsky 	if (dev->devx_whitelist_uid)
623881773ce5SLeon Romanovsky 		mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
623981773ce5SLeon Romanovsky }
624081773ce5SLeon Romanovsky 
6241b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
624216c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
624316c1975fSMark Bloch 		      int stage)
624416c1975fSMark Bloch {
624516c1975fSMark Bloch 	/* Number of stages to cleanup */
624616c1975fSMark Bloch 	while (stage) {
624716c1975fSMark Bloch 		stage--;
624816c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
624916c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
625016c1975fSMark Bloch 	}
625116c1975fSMark Bloch }
625216c1975fSMark Bloch 
6253b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
625416c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
625516c1975fSMark Bloch {
625616c1975fSMark Bloch 	int err;
625716c1975fSMark Bloch 	int i;
625816c1975fSMark Bloch 
625916c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
626016c1975fSMark Bloch 		if (profile->stage[i].init) {
626116c1975fSMark Bloch 			err = profile->stage[i].init(dev);
626216c1975fSMark Bloch 			if (err)
626316c1975fSMark Bloch 				goto err_out;
626416c1975fSMark Bloch 		}
626516c1975fSMark Bloch 	}
626616c1975fSMark Bloch 
626716c1975fSMark Bloch 	dev->profile = profile;
6268e126ba97SEli Cohen 	dev->ib_active = true;
6269e126ba97SEli Cohen 
62709603b61dSJack Morgenstein 	return dev;
6271e126ba97SEli Cohen 
627216c1975fSMark Bloch err_out:
627316c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
6274e126ba97SEli Cohen 
62759603b61dSJack Morgenstein 	return NULL;
6276e126ba97SEli Cohen }
6277e126ba97SEli Cohen 
627816c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
627916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
628016c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
628116c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
62829a4ca38dSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
62839a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_init,
62849a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
628516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
628616c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
628716c1975fSMark Bloch 		     NULL),
62888e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
62898e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
62908e6efa3aSMark Bloch 		     NULL),
629116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
629216c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
629316c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
6294f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6295f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
6296f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
629716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
629816c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
629916c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
6300df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6301df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
6302df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
630316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
630416c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
6305d5d284b8SSaeed Mahameed 		     mlx5_ib_stage_odp_cleanup),
630616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
630716c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
630816c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
630916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
631016c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
631116c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
631216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
631316c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
631416c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
631516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
631616c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
631716c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
631842cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
631942cea83fSMark Bloch 		     NULL,
632042cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
63218c84660bSMatan Barak 	STAGE_CREATE(MLX5_IB_STAGE_SPECS,
63228c84660bSMatan Barak 		     mlx5_ib_stage_populate_specs,
63237d96c9b1SJason Gunthorpe 		     NULL),
632481773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
632581773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_init,
632681773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_cleanup),
632716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
632816c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
632916c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
633042cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
633142cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
633242cea83fSMark Bloch 		     NULL),
633316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
633416c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
633516c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
633616c1975fSMark Bloch };
633716c1975fSMark Bloch 
6338b5ca15adSMark Bloch static const struct mlx5_ib_profile nic_rep_profile = {
6339b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
6340b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
6341b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
6342b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6343b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_init,
6344b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
6345b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6346b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
6347b5ca15adSMark Bloch 		     NULL),
6348b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6349b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_non_default_cb,
6350b5ca15adSMark Bloch 		     NULL),
6351b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6352b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_init,
6353b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_cleanup),
6354f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6355f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
6356f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
6357b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6358b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_init,
6359b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
6360df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6361df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
6362df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
6363b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6364b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_init,
6365b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
6366b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
6367b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
6368b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
6369b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6370b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
6371b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
637203fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
637303fe2debSDavid S. Miller 		     NULL,
637403fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
63758c84660bSMatan Barak 	STAGE_CREATE(MLX5_IB_STAGE_SPECS,
63768c84660bSMatan Barak 		     mlx5_ib_stage_populate_specs,
63777d96c9b1SJason Gunthorpe 		     NULL),
6378b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6379b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
6380b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
638103fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
638203fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
638303fe2debSDavid S. Miller 		     NULL),
6384b5ca15adSMark Bloch };
6385b5ca15adSMark Bloch 
6386e3f1ed1fSLeon Romanovsky static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
638732f69e4bSDaniel Jurgens {
638832f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
638932f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
639032f69e4bSDaniel Jurgens 	bool bound = false;
639132f69e4bSDaniel Jurgens 	int err;
639232f69e4bSDaniel Jurgens 
639332f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
639432f69e4bSDaniel Jurgens 	if (!mpi)
639532f69e4bSDaniel Jurgens 		return NULL;
639632f69e4bSDaniel Jurgens 
639732f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
639832f69e4bSDaniel Jurgens 
639932f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
640032f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
640132f69e4bSDaniel Jurgens 	if (err) {
640232f69e4bSDaniel Jurgens 		kfree(mpi);
640332f69e4bSDaniel Jurgens 		return NULL;
640432f69e4bSDaniel Jurgens 	}
640532f69e4bSDaniel Jurgens 
640632f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
640732f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
640832f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
640932f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
641032f69e4bSDaniel Jurgens 
641132f69e4bSDaniel Jurgens 		if (bound) {
641232f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
641332f69e4bSDaniel Jurgens 			break;
641432f69e4bSDaniel Jurgens 		}
641532f69e4bSDaniel Jurgens 	}
641632f69e4bSDaniel Jurgens 
641732f69e4bSDaniel Jurgens 	if (!bound) {
641832f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
641932f69e4bSDaniel Jurgens 		dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
642032f69e4bSDaniel Jurgens 	}
642132f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
642232f69e4bSDaniel Jurgens 
642332f69e4bSDaniel Jurgens 	return mpi;
642432f69e4bSDaniel Jurgens }
642532f69e4bSDaniel Jurgens 
642616c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
642716c1975fSMark Bloch {
642832f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
6429b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
643032f69e4bSDaniel Jurgens 	int port_type_cap;
643132f69e4bSDaniel Jurgens 
6432b5ca15adSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
6433b5ca15adSMark Bloch 
643432f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
643532f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
643632f69e4bSDaniel Jurgens 
6437e3f1ed1fSLeon Romanovsky 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6438e3f1ed1fSLeon Romanovsky 		return mlx5_ib_add_slave_port(mdev);
643932f69e4bSDaniel Jurgens 
6440b5ca15adSMark Bloch 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
6441b5ca15adSMark Bloch 	if (!dev)
6442b5ca15adSMark Bloch 		return NULL;
6443b5ca15adSMark Bloch 
6444b5ca15adSMark Bloch 	dev->mdev = mdev;
6445b5ca15adSMark Bloch 	dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6446b5ca15adSMark Bloch 			     MLX5_CAP_GEN(mdev, num_vhca_ports));
6447b5ca15adSMark Bloch 
6448aff2252aSOr Gerlitz 	if (MLX5_ESWITCH_MANAGER(mdev) &&
6449b5ca15adSMark Bloch 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
6450b5ca15adSMark Bloch 		dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0);
645106cc74afSMark Bloch 		dev->profile = &nic_rep_profile;
645206cc74afSMark Bloch 		mlx5_ib_register_vport_reps(dev);
645306cc74afSMark Bloch 		return dev;
6454b5ca15adSMark Bloch 	}
6455b5ca15adSMark Bloch 
6456b5ca15adSMark Bloch 	return __mlx5_ib_add(dev, &pf_profile);
645716c1975fSMark Bloch }
645816c1975fSMark Bloch 
64599603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
6460e126ba97SEli Cohen {
646132f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
646232f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
64636aec21f6SHaggai Eran 
646432f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
646532f69e4bSDaniel Jurgens 		mpi = context;
646632f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
646732f69e4bSDaniel Jurgens 		if (mpi->ibdev)
646832f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
646932f69e4bSDaniel Jurgens 		list_del(&mpi->list);
647032f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
647132f69e4bSDaniel Jurgens 		return;
647232f69e4bSDaniel Jurgens 	}
647332f69e4bSDaniel Jurgens 
647432f69e4bSDaniel Jurgens 	dev = context;
647506cc74afSMark Bloch 	if (dev->profile == &nic_rep_profile)
647606cc74afSMark Bloch 		mlx5_ib_unregister_vport_reps(dev);
647706cc74afSMark Bloch 	else
647816c1975fSMark Bloch 		__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
647906cc74afSMark Bloch 
648006cc74afSMark Bloch 	ib_dealloc_device((struct ib_device *)dev);
6481e126ba97SEli Cohen }
6482e126ba97SEli Cohen 
64839603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
64849603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
64859603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
648664613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
6487e126ba97SEli Cohen };
6488e126ba97SEli Cohen 
6489c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
6490c44ef998SIlya Lesokhin {
6491c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
6492c44ef998SIlya Lesokhin 	return xlt_emergency_page;
6493c44ef998SIlya Lesokhin }
6494c44ef998SIlya Lesokhin 
6495c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
6496c44ef998SIlya Lesokhin {
6497c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
6498c44ef998SIlya Lesokhin }
6499c44ef998SIlya Lesokhin 
6500e126ba97SEli Cohen static int __init mlx5_ib_init(void)
6501e126ba97SEli Cohen {
65026aec21f6SHaggai Eran 	int err;
65036aec21f6SHaggai Eran 
6504c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
6505c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
6506d69a24e0SDaniel Jurgens 		return -ENOMEM;
6507d69a24e0SDaniel Jurgens 
6508c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
6509c44ef998SIlya Lesokhin 
6510c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6511c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
6512c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
6513c44ef998SIlya Lesokhin 		return -ENOMEM;
6514c44ef998SIlya Lesokhin 	}
6515c44ef998SIlya Lesokhin 
651681713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
65179603b61dSJack Morgenstein 
65186aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
65196aec21f6SHaggai Eran 
65206aec21f6SHaggai Eran 	return err;
6521e126ba97SEli Cohen }
6522e126ba97SEli Cohen 
6523e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
6524e126ba97SEli Cohen {
65259603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
6526d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
6527c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
6528c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
6529e126ba97SEli Cohen }
6530e126ba97SEli Cohen 
6531e126ba97SEli Cohen module_init(mlx5_ib_init);
6532e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
6533