xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 842a9c837e2017127250a93204368f13e660ed9d)
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));
1533023a1e9SKamal Heib 	ret = ibdev->ops.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:
1757fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
176bcf87f1dSMark Bloch 		if (ibdev->rep) {
177bcf87f1dSMark Bloch 			struct mlx5_eswitch *esw = ibdev->mdev->priv.eswitch;
178bcf87f1dSMark Bloch 			struct net_device *rep_ndev;
1797fd8aefbSDaniel Jurgens 
180bcf87f1dSMark Bloch 			rep_ndev = mlx5_ib_get_rep_netdev(esw,
181bcf87f1dSMark Bloch 							  ibdev->rep->vport);
182bcf87f1dSMark Bloch 			if (rep_ndev == ndev)
183*842a9c83SOr Gerlitz 				roce->netdev = ndev;
18484a6a7a9SParav Pandit 		} else if (ndev->dev.parent == &mdev->pdev->dev) {
185*842a9c83SOr Gerlitz 			roce->netdev = ndev;
186bcf87f1dSMark Bloch 		}
1877fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
1885ec8c83eSAviv Heller 		break;
1895ec8c83eSAviv Heller 
190*842a9c83SOr Gerlitz 	case NETDEV_UNREGISTER:
191*842a9c83SOr Gerlitz 		write_lock(&roce->netdev_lock);
192*842a9c83SOr Gerlitz 		if (roce->netdev == ndev)
193*842a9c83SOr Gerlitz 			roce->netdev = NULL;
194*842a9c83SOr Gerlitz 		write_unlock(&roce->netdev_lock);
195*842a9c83SOr Gerlitz 		break;
196*842a9c83SOr Gerlitz 
197fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
1985ec8c83eSAviv Heller 	case NETDEV_UP:
19988621dfeSAviv Heller 	case NETDEV_DOWN: {
2007fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
20188621dfeSAviv Heller 		struct net_device *upper = NULL;
20288621dfeSAviv Heller 
20388621dfeSAviv Heller 		if (lag_ndev) {
20488621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
20588621dfeSAviv Heller 			dev_put(lag_ndev);
20688621dfeSAviv Heller 		}
20788621dfeSAviv Heller 
2087fd8aefbSDaniel Jurgens 		if ((upper == ndev || (!upper && ndev == roce->netdev))
20988621dfeSAviv Heller 		    && ibdev->ib_active) {
210626bc02dSBart Van Assche 			struct ib_event ibev = { };
211fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2125ec8c83eSAviv Heller 
2137fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2147fd8aefbSDaniel Jurgens 					   &port_state))
2157fd8aefbSDaniel Jurgens 				goto done;
216fd65f1b8SMoni Shoua 
2177fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2187fd8aefbSDaniel Jurgens 				goto done;
219fd65f1b8SMoni Shoua 
2207fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2215ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
222fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
223fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
224fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
225fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
226fd65f1b8SMoni Shoua 			else
2277fd8aefbSDaniel Jurgens 				goto done;
228fd65f1b8SMoni Shoua 
2297fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2305ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2315ec8c83eSAviv Heller 		}
2325ec8c83eSAviv Heller 		break;
23388621dfeSAviv Heller 	}
2345ec8c83eSAviv Heller 
2355ec8c83eSAviv Heller 	default:
2365ec8c83eSAviv Heller 		break;
2375ec8c83eSAviv Heller 	}
2387fd8aefbSDaniel Jurgens done:
23932f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
240fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
241fc24fc5eSAchiad Shochat }
242fc24fc5eSAchiad Shochat 
243fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
244fc24fc5eSAchiad Shochat 					     u8 port_num)
245fc24fc5eSAchiad Shochat {
246fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
247fc24fc5eSAchiad Shochat 	struct net_device *ndev;
24832f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
249fc24fc5eSAchiad Shochat 
25032f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
25132f69e4bSDaniel Jurgens 	if (!mdev)
25232f69e4bSDaniel Jurgens 		return NULL;
25332f69e4bSDaniel Jurgens 
25432f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
25588621dfeSAviv Heller 	if (ndev)
25632f69e4bSDaniel Jurgens 		goto out;
25788621dfeSAviv Heller 
258fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
259fc24fc5eSAchiad Shochat 	 */
2607fd8aefbSDaniel Jurgens 	read_lock(&ibdev->roce[port_num - 1].netdev_lock);
2617fd8aefbSDaniel Jurgens 	ndev = ibdev->roce[port_num - 1].netdev;
262fc24fc5eSAchiad Shochat 	if (ndev)
263fc24fc5eSAchiad Shochat 		dev_hold(ndev);
2647fd8aefbSDaniel Jurgens 	read_unlock(&ibdev->roce[port_num - 1].netdev_lock);
265fc24fc5eSAchiad Shochat 
26632f69e4bSDaniel Jurgens out:
26732f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
268fc24fc5eSAchiad Shochat 	return ndev;
269fc24fc5eSAchiad Shochat }
270fc24fc5eSAchiad Shochat 
27132f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
27232f69e4bSDaniel Jurgens 						   u8 ib_port_num,
27332f69e4bSDaniel Jurgens 						   u8 *native_port_num)
27432f69e4bSDaniel Jurgens {
27532f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
27632f69e4bSDaniel Jurgens 							  ib_port_num);
27732f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
27832f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
27932f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
28032f69e4bSDaniel Jurgens 
281210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
282210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
283210b1f78SMark Bloch 		if (native_port_num)
284210b1f78SMark Bloch 			*native_port_num = ib_port_num;
285210b1f78SMark Bloch 		return ibdev->mdev;
286210b1f78SMark Bloch 	}
287210b1f78SMark Bloch 
28832f69e4bSDaniel Jurgens 	if (native_port_num)
28932f69e4bSDaniel Jurgens 		*native_port_num = 1;
29032f69e4bSDaniel Jurgens 
29132f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
29232f69e4bSDaniel Jurgens 	if (!port)
29332f69e4bSDaniel Jurgens 		return NULL;
29432f69e4bSDaniel Jurgens 
29532f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
29632f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
29732f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
29832f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
29932f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
30032f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
30132f69e4bSDaniel Jurgens 		 */
30232f69e4bSDaniel Jurgens 		if (!mpi->is_master)
30332f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
30432f69e4bSDaniel Jurgens 	}
30532f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
30632f69e4bSDaniel Jurgens 
30732f69e4bSDaniel Jurgens 	return mdev;
30832f69e4bSDaniel Jurgens }
30932f69e4bSDaniel Jurgens 
31032f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
31132f69e4bSDaniel Jurgens {
31232f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
31332f69e4bSDaniel Jurgens 							  port_num);
31432f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
31532f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
31632f69e4bSDaniel Jurgens 
31732f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
31832f69e4bSDaniel Jurgens 		return;
31932f69e4bSDaniel Jurgens 
32032f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
32132f69e4bSDaniel Jurgens 
32232f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
32332f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
32432f69e4bSDaniel Jurgens 	if (mpi->is_master)
32532f69e4bSDaniel Jurgens 		goto out;
32632f69e4bSDaniel Jurgens 
32732f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
32832f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
32932f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
33032f69e4bSDaniel Jurgens out:
33132f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
33232f69e4bSDaniel Jurgens }
33332f69e4bSDaniel Jurgens 
334f1b65df5SNoa Osherovich static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
335f1b65df5SNoa Osherovich 				    u8 *active_width)
336f1b65df5SNoa Osherovich {
337f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
338f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
339f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
340f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
341f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
342f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
343f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
344f1b65df5SNoa Osherovich 		break;
345f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
346f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
347f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
348f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
349f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
350f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
351f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
352f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
353f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
354f1b65df5SNoa Osherovich 		break;
355f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
356f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
357f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
358f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
359f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
360f1b65df5SNoa Osherovich 		break;
361f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
362f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
363f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
364f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
365f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
366f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
367f1b65df5SNoa Osherovich 		break;
368f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
369f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
370f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
371f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
372f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
373f1b65df5SNoa Osherovich 		break;
374f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
375f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
376f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
377f1b65df5SNoa Osherovich 		break;
378f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
379f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
380f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
381f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
382f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
383f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
384f1b65df5SNoa Osherovich 		break;
385f1b65df5SNoa Osherovich 	default:
386f1b65df5SNoa Osherovich 		return -EINVAL;
387f1b65df5SNoa Osherovich 	}
388f1b65df5SNoa Osherovich 
389f1b65df5SNoa Osherovich 	return 0;
390f1b65df5SNoa Osherovich }
391f1b65df5SNoa Osherovich 
392095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
3933f89a643SAchiad Shochat 				struct ib_port_attr *props)
3943f89a643SAchiad Shochat {
3953f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
396da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
39788621dfeSAviv Heller 	struct net_device *ndev, *upper;
3983f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
399b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
400c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
401f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
402b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
403095b0927SIlan Tayari 	int err;
4043f89a643SAchiad Shochat 
405b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
406b3cbd6f0SDaniel Jurgens 	if (!mdev) {
407b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
408b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
409b3cbd6f0SDaniel Jurgens 		 */
410b3cbd6f0SDaniel Jurgens 		put_mdev = false;
411b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
412b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
413b3cbd6f0SDaniel Jurgens 		port_num = 1;
414b3cbd6f0SDaniel Jurgens 	}
415b3cbd6f0SDaniel Jurgens 
416f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
417f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
41850f22fd8SNoa Osherovich 	 */
419b3cbd6f0SDaniel Jurgens 	err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper,
420b3cbd6f0SDaniel Jurgens 					     mdev_port_num);
421095b0927SIlan Tayari 	if (err)
422b3cbd6f0SDaniel Jurgens 		goto out;
423f1b65df5SNoa Osherovich 
4247672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
4257672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
4267672ed33SHonggang Li 
427f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
428f1b65df5SNoa Osherovich 				 &props->active_width);
4293f89a643SAchiad Shochat 
4303f89a643SAchiad Shochat 	props->port_cap_flags |= IB_PORT_CM_SUP;
4312f944c0fSJason Gunthorpe 	props->ip_gids = true;
4323f89a643SAchiad Shochat 
4333f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
4343f89a643SAchiad Shochat 						roce_address_table_size);
4353f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
4363f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
4373f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
4383f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
4393f89a643SAchiad Shochat 	props->phys_state       = 3;
4403f89a643SAchiad Shochat 
441b3cbd6f0SDaniel Jurgens 	mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
442c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
4433f89a643SAchiad Shochat 
444b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
445b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
446b3cbd6f0SDaniel Jurgens 		goto out;
447b3cbd6f0SDaniel Jurgens 
4483f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
4493f89a643SAchiad Shochat 	if (!ndev)
450b3cbd6f0SDaniel Jurgens 		goto out;
4513f89a643SAchiad Shochat 
45288621dfeSAviv Heller 	if (mlx5_lag_is_active(dev->mdev)) {
45388621dfeSAviv Heller 		rcu_read_lock();
45488621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
45588621dfeSAviv Heller 		if (upper) {
45688621dfeSAviv Heller 			dev_put(ndev);
45788621dfeSAviv Heller 			ndev = upper;
45888621dfeSAviv Heller 			dev_hold(ndev);
45988621dfeSAviv Heller 		}
46088621dfeSAviv Heller 		rcu_read_unlock();
46188621dfeSAviv Heller 	}
46288621dfeSAviv Heller 
4633f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
4643f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
4653f89a643SAchiad Shochat 		props->phys_state = 5;
4663f89a643SAchiad Shochat 	}
4673f89a643SAchiad Shochat 
4683f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
4693f89a643SAchiad Shochat 
4703f89a643SAchiad Shochat 	dev_put(ndev);
4713f89a643SAchiad Shochat 
4723f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
473b3cbd6f0SDaniel Jurgens out:
474b3cbd6f0SDaniel Jurgens 	if (put_mdev)
475b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
476b3cbd6f0SDaniel Jurgens 	return err;
4773f89a643SAchiad Shochat }
4783f89a643SAchiad Shochat 
479095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
480095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
481095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
4823cca2606SAchiad Shochat {
483095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
484095b0927SIlan Tayari 	u8 roce_version = 0;
485095b0927SIlan Tayari 	u8 roce_l3_type = 0;
486095b0927SIlan Tayari 	bool vlan = false;
487095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
488095b0927SIlan Tayari 	u16 vlan_id = 0;
4893cca2606SAchiad Shochat 
490095b0927SIlan Tayari 	if (gid) {
491095b0927SIlan Tayari 		gid_type = attr->gid_type;
492095b0927SIlan Tayari 		ether_addr_copy(mac, attr->ndev->dev_addr);
4933cca2606SAchiad Shochat 
4943cca2606SAchiad Shochat 		if (is_vlan_dev(attr->ndev)) {
495095b0927SIlan Tayari 			vlan = true;
496095b0927SIlan Tayari 			vlan_id = vlan_dev_vlan_id(attr->ndev);
497095b0927SIlan Tayari 		}
4983cca2606SAchiad Shochat 	}
4993cca2606SAchiad Shochat 
500095b0927SIlan Tayari 	switch (gid_type) {
5013cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
502095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
5033cca2606SAchiad Shochat 		break;
5043cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
505095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
506095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
507095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
508095b0927SIlan Tayari 		else
509095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
5103cca2606SAchiad Shochat 		break;
5113cca2606SAchiad Shochat 
5123cca2606SAchiad Shochat 	default:
513095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
5143cca2606SAchiad Shochat 	}
5153cca2606SAchiad Shochat 
516095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
517095b0927SIlan Tayari 				      roce_l3_type, gid->raw, mac, vlan,
518cfe4e37fSDaniel Jurgens 				      vlan_id, port_num);
5193cca2606SAchiad Shochat }
5203cca2606SAchiad Shochat 
521f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
5223cca2606SAchiad Shochat 			   __always_unused void **context)
5233cca2606SAchiad Shochat {
524414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
525f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
5263cca2606SAchiad Shochat }
5273cca2606SAchiad Shochat 
528414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
529414448d2SParav Pandit 			   __always_unused void **context)
5303cca2606SAchiad Shochat {
531414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
532414448d2SParav Pandit 			     attr->index, NULL, NULL);
5333cca2606SAchiad Shochat }
5343cca2606SAchiad Shochat 
53547ec3866SParav Pandit __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
53647ec3866SParav Pandit 			       const struct ib_gid_attr *attr)
5372811ba51SAchiad Shochat {
53847ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
5392811ba51SAchiad Shochat 		return 0;
5402811ba51SAchiad Shochat 
5412811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
5422811ba51SAchiad Shochat }
5432811ba51SAchiad Shochat 
5441b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
5451b5daf11SMajd Dibbiny {
5467fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
547d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
5487fae6655SNoa Osherovich 	return 0;
5491b5daf11SMajd Dibbiny }
5501b5daf11SMajd Dibbiny 
5511b5daf11SMajd Dibbiny enum {
5521b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
5531b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
5541b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
5551b5daf11SMajd Dibbiny };
5561b5daf11SMajd Dibbiny 
5571b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
5581b5daf11SMajd Dibbiny {
5591b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
5601b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
5611b5daf11SMajd Dibbiny 
562ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
5631b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
5641b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
5651b5daf11SMajd Dibbiny 
5661b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
5671b5daf11SMajd Dibbiny }
5681b5daf11SMajd Dibbiny 
569da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
570776a3906SMoni Shoua 			    u8 atomic_size_qp,
571da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
572da7525d2SEran Ben Elisha {
573da7525d2SEran Ben Elisha 	u8 tmp;
574da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
575da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
576bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
577da7525d2SEran Ben Elisha 
578da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
579da7525d2SEran Ben Elisha 	 * of host endianness respond
580da7525d2SEran Ben Elisha 	 */
581da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
582da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
583da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
584da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
585da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
586da7525d2SEran Ben Elisha 	} else {
587da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
588da7525d2SEran Ben Elisha 	}
589da7525d2SEran Ben Elisha }
590da7525d2SEran Ben Elisha 
591776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
592776a3906SMoni Shoua 			       struct ib_device_attr *props)
593776a3906SMoni Shoua {
594776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
595776a3906SMoni Shoua 
596776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
597776a3906SMoni Shoua }
598776a3906SMoni Shoua 
599776a3906SMoni Shoua static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
600776a3906SMoni Shoua 			       struct ib_device_attr *props)
601776a3906SMoni Shoua {
602776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
603776a3906SMoni Shoua 
604776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
605776a3906SMoni Shoua }
606776a3906SMoni Shoua 
607776a3906SMoni Shoua bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
608776a3906SMoni Shoua {
609776a3906SMoni Shoua 	struct ib_device_attr props = {};
610776a3906SMoni Shoua 
611776a3906SMoni Shoua 	get_atomic_caps_dc(dev, &props);
612776a3906SMoni Shoua 	return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
613776a3906SMoni Shoua }
6141b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
6151b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
6161b5daf11SMajd Dibbiny {
6171b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6181b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6191b5daf11SMajd Dibbiny 	u64 tmp;
6201b5daf11SMajd Dibbiny 	int err;
6211b5daf11SMajd Dibbiny 
6221b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6231b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6241b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
6251b5daf11SMajd Dibbiny 							    sys_image_guid);
6261b5daf11SMajd Dibbiny 
6271b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6281b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
6293f89a643SAchiad Shochat 		break;
6303f89a643SAchiad Shochat 
6313f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6323f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
6333f89a643SAchiad Shochat 		break;
6341b5daf11SMajd Dibbiny 
6351b5daf11SMajd Dibbiny 	default:
6361b5daf11SMajd Dibbiny 		return -EINVAL;
6371b5daf11SMajd Dibbiny 	}
6383f89a643SAchiad Shochat 
6393f89a643SAchiad Shochat 	if (!err)
6403f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
6413f89a643SAchiad Shochat 
6423f89a643SAchiad Shochat 	return err;
6433f89a643SAchiad Shochat 
6441b5daf11SMajd Dibbiny }
6451b5daf11SMajd Dibbiny 
6461b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
6471b5daf11SMajd Dibbiny 				u16 *max_pkeys)
6481b5daf11SMajd Dibbiny {
6491b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6501b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6511b5daf11SMajd Dibbiny 
6521b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6531b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6541b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
6551b5daf11SMajd Dibbiny 
6561b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6571b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6581b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
6591b5daf11SMajd Dibbiny 						pkey_table_size));
6601b5daf11SMajd Dibbiny 		return 0;
6611b5daf11SMajd Dibbiny 
6621b5daf11SMajd Dibbiny 	default:
6631b5daf11SMajd Dibbiny 		return -EINVAL;
6641b5daf11SMajd Dibbiny 	}
6651b5daf11SMajd Dibbiny }
6661b5daf11SMajd Dibbiny 
6671b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
6681b5daf11SMajd Dibbiny 				u32 *vendor_id)
6691b5daf11SMajd Dibbiny {
6701b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6711b5daf11SMajd Dibbiny 
6721b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6731b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6741b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
6751b5daf11SMajd Dibbiny 
6761b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6771b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6781b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
6791b5daf11SMajd Dibbiny 
6801b5daf11SMajd Dibbiny 	default:
6811b5daf11SMajd Dibbiny 		return -EINVAL;
6821b5daf11SMajd Dibbiny 	}
6831b5daf11SMajd Dibbiny }
6841b5daf11SMajd Dibbiny 
6851b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
6861b5daf11SMajd Dibbiny 				__be64 *node_guid)
6871b5daf11SMajd Dibbiny {
6881b5daf11SMajd Dibbiny 	u64 tmp;
6891b5daf11SMajd Dibbiny 	int err;
6901b5daf11SMajd Dibbiny 
6911b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
6921b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6931b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
6941b5daf11SMajd Dibbiny 
6951b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6961b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
6973f89a643SAchiad Shochat 		break;
6983f89a643SAchiad Shochat 
6993f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7003f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7013f89a643SAchiad Shochat 		break;
7021b5daf11SMajd Dibbiny 
7031b5daf11SMajd Dibbiny 	default:
7041b5daf11SMajd Dibbiny 		return -EINVAL;
7051b5daf11SMajd Dibbiny 	}
7063f89a643SAchiad Shochat 
7073f89a643SAchiad Shochat 	if (!err)
7083f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7093f89a643SAchiad Shochat 
7103f89a643SAchiad Shochat 	return err;
7111b5daf11SMajd Dibbiny }
7121b5daf11SMajd Dibbiny 
7131b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
714bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
7151b5daf11SMajd Dibbiny };
7161b5daf11SMajd Dibbiny 
7171b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
7181b5daf11SMajd Dibbiny {
7191b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
7201b5daf11SMajd Dibbiny 
7211b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
7221b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
7231b5daf11SMajd Dibbiny 
7241b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
7251b5daf11SMajd Dibbiny 
7261b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
7271b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
7281b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
7291b5daf11SMajd Dibbiny }
7301b5daf11SMajd Dibbiny 
731e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
7322528e33eSMatan Barak 				struct ib_device_attr *props,
7332528e33eSMatan Barak 				struct ib_udata *uhw)
734e126ba97SEli Cohen {
735e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
736938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
737e126ba97SEli Cohen 	int err = -ENOMEM;
738288c01b7SEli Cohen 	int max_sq_desc;
739e126ba97SEli Cohen 	int max_rq_sg;
740e126ba97SEli Cohen 	int max_sq_sg;
741e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
74285c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
743402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
744402ca536SBodong Wang 	size_t resp_len;
745402ca536SBodong Wang 	u64 max_tso;
746e126ba97SEli Cohen 
747402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
748402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
749402ca536SBodong Wang 		return -EINVAL;
750402ca536SBodong Wang 	else
751402ca536SBodong Wang 		resp.response_length = resp_len;
752402ca536SBodong Wang 
753402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
7542528e33eSMatan Barak 		return -EINVAL;
7552528e33eSMatan Barak 
756e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
7571b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
7581b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
7591b5daf11SMajd Dibbiny 	if (err)
7601b5daf11SMajd Dibbiny 		return err;
7611b5daf11SMajd Dibbiny 
7621b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
7631b5daf11SMajd Dibbiny 	if (err)
7641b5daf11SMajd Dibbiny 		return err;
7651b5daf11SMajd Dibbiny 
7661b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
7671b5daf11SMajd Dibbiny 	if (err)
7681b5daf11SMajd Dibbiny 		return err;
769e126ba97SEli Cohen 
7709603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
7719603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
7729603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
773e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
774e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
775e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
7761a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
777938fe83cSSaeed Mahameed 
778938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
779e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
780938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
781e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
782938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
783e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
784938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
785e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
786d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
787d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
788d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
789d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
790b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
791b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
792d2370e0aSMatan Barak 	}
793e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
794938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
7952dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
7962dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
7972dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
7982dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
7992dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8002dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8012dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8022dea9094SSagi Grimberg 	}
803938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
804f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
805e126ba97SEli Cohen 
80685c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
807e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
808e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
80988115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
810e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
811e8161334SNoa Osherovich 		}
812e8161334SNoa Osherovich 
813e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
814e8161334SNoa Osherovich 			props->raw_packet_caps |=
815e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
81688115fe7SBodong Wang 
817402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
818402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
819402ca536SBodong Wang 			if (max_tso) {
820402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
821402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
822402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
823402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
824402ca536SBodong Wang 			}
825402ca536SBodong Wang 		}
82631f69a82SYishai Hadas 
82731f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
82831f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
82931f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
83031f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
83131f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
83231f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
83331f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
83431f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
83531f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
83631f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
83731f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
8384e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
8394e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
8402d93fc85SMatan Barak 			if (mlx5_accel_ipsec_device_caps(dev->mdev) &
8412d93fc85SMatan Barak 			    MLX5_ACCEL_IPSEC_CAP_DEVICE)
8422d93fc85SMatan Barak 				resp.rss_caps.rx_hash_fields_mask |=
8432d93fc85SMatan Barak 					MLX5_RX_HASH_IPSEC_SPI;
84431f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
84531f69a82SYishai Hadas 		}
84631f69a82SYishai Hadas 	} else {
84731f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
84831f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
84931f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
85031f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
851402ca536SBodong Wang 	}
852402ca536SBodong Wang 
853f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
854f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
855f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
856f0313965SErez Shitrit 	}
857f0313965SErez Shitrit 
85803404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
85985c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
86085c7c014SDaniel Jurgens 	    raw_support)
86103404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
86203404e8aSMaor Gottlieb 
8631d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
8641d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
8651d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
8661d54f890SYishai Hadas 
867cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
86885c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
86985c7c014SDaniel Jurgens 	    raw_support) {
870e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
871cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
872e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
873e8161334SNoa Osherovich 	}
874cff5a0f3SMajd Dibbiny 
87524da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
87624da0016SAriel Levkovich 		props->max_dm_size =
87724da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
87824da0016SAriel Levkovich 	}
87924da0016SAriel Levkovich 
880da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
881da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
882da6d6ba3SMaor Gottlieb 
883b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
884b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
885b1383aa6SNoa Osherovich 
8861b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
8871b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
888e126ba97SEli Cohen 
889e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
890e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
891938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
892938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
893938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
894938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
895288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
896288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
897288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
898e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
89933023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
90033023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
901986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
902938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9039f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
904938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
905938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
906938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
907938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
908938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
909938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
910938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
911e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
912e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
913911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
914911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
915776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
91681bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
917938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
918938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
919e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
920e126ba97SEli Cohen 					   props->max_mcast_grp;
921e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
92286695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
9237c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
9247c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
925e126ba97SEli Cohen 
9268cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
927938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
9288cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
9298cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
9308cdd312cSHaggai Eran #endif
9318cdd312cSHaggai Eran 
932051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
933051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
934051f2630SLeon Romanovsky 
935eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
936eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
937eff901d3SEli Cohen 
93831f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
93985c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
94031f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
94131f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
94231f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
94331f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
94431f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
94531f69a82SYishai Hadas 		props->max_wq_type_rq =
94631f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
94731f69a82SYishai Hadas 	}
94831f69a82SYishai Hadas 
949eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
95078b1beb0SLeon Romanovsky 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
95178b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
952eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
95378b1beb0SLeon Romanovsky 		props->tm_caps.flags = IB_TM_CAP_RC;
95478b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
955eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
95678b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
957eb761894SArtemy Kovalyov 	}
958eb761894SArtemy Kovalyov 
95987ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
96087ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
96187ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
96287ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
96387ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
96487ab3f52SYonatan Cohen 	}
96587ab3f52SYonatan Cohen 
9667e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
967572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
968572f46bfSYonatan Cohen 
969572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
9707e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
971572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
972572f46bfSYonatan Cohen 					     cqe_compression_max_num);
973572f46bfSYonatan Cohen 
9747e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
9757e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
9767e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
9776f1006a4SYonatan Cohen 
9786f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
9796f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
9806f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
981572f46bfSYonatan Cohen 		}
9827e43a2a5SBodong Wang 	}
9837e43a2a5SBodong Wang 
98485c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
98585c7c014SDaniel Jurgens 	    raw_support) {
986d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
987d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
988d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
989d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
990d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
991d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
992d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
993d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
99461147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
99561147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
99661147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
99761147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
998d949167dSBodong Wang 		}
999d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1000d949167dSBodong Wang 	}
1001d949167dSBodong Wang 
10029f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
10039f885201SLeon Romanovsky 			uhw->outlen)) {
1004795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
10059f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1006795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1007050da902SBodong Wang 
1008050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1009050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1010050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1011050da902SBodong Wang 
10129f885201SLeon Romanovsky 		resp.response_length +=
10139f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
10149f885201SLeon Romanovsky 	}
10159f885201SLeon Romanovsky 
1016de57f2adSGuy Levi 	if (field_avail(typeof(resp), flags, uhw->outlen)) {
1017de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
10187a0c8f42SGuy Levi 
1019de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1020de57f2adSGuy Levi 			resp.flags |=
1021de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
10227a0c8f42SGuy Levi 
10237a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
10247a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
10257e11b911SDanit Goldberg 		if (MLX5_CAP_GEN(mdev, qp_packet_based))
10267e11b911SDanit Goldberg 			resp.flags |=
10277e11b911SDanit Goldberg 				MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
1028de57f2adSGuy Levi 	}
10299f885201SLeon Romanovsky 
103096dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
103196dc3fc5SNoa Osherovich 			uhw->outlen)) {
103296dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
103396dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
103496dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
103596dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
103696dc3fc5SNoa Osherovich 
103796dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
103896dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
103996dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
104096dc3fc5SNoa Osherovich 
104196dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
104296dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
104396dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
104496dc3fc5SNoa Osherovich 
104596dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
104696dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
104796dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
104896dc3fc5SNoa Osherovich 		}
104996dc3fc5SNoa Osherovich 	}
105096dc3fc5SNoa Osherovich 
105185c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
105285c7c014SDaniel Jurgens 	    raw_support) {
1053b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1054b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1055b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1056b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1057b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1058b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1059b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1060b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1061b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1062b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1063b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1064b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1065b4f34597SNoa Osherovich 		}
1066b4f34597SNoa Osherovich 	}
1067b4f34597SNoa Osherovich 
1068f95ef6cbSMaor Gottlieb 	if (field_avail(typeof(resp), tunnel_offloads_caps,
1069f95ef6cbSMaor Gottlieb 			uhw->outlen)) {
1070f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1071f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1072f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1073f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1074f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1075f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1076f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1077f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1078f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1079f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
1080e818e255SAriel Levkovich 		if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1081e818e255SAriel Levkovich 		    MLX5_FLEX_PROTO_CW_MPLS_GRE)
1082e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1083e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1084e818e255SAriel Levkovich 		if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1085e818e255SAriel Levkovich 		    MLX5_FLEX_PROTO_CW_MPLS_UDP)
1086e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1087e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1088f95ef6cbSMaor Gottlieb 	}
1089f95ef6cbSMaor Gottlieb 
1090402ca536SBodong Wang 	if (uhw->outlen) {
1091402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1092402ca536SBodong Wang 
1093402ca536SBodong Wang 		if (err)
1094402ca536SBodong Wang 			return err;
1095402ca536SBodong Wang 	}
1096402ca536SBodong Wang 
10971b5daf11SMajd Dibbiny 	return 0;
10981b5daf11SMajd Dibbiny }
1099e126ba97SEli Cohen 
11001b5daf11SMajd Dibbiny enum mlx5_ib_width {
11011b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
11021b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
11031b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
11041b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
11051b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
11061b5daf11SMajd Dibbiny };
11071b5daf11SMajd Dibbiny 
1108db7a691aSMichael Guralnik static void translate_active_width(struct ib_device *ibdev, u8 active_width,
11091b5daf11SMajd Dibbiny 				  u8 *ib_width)
11101b5daf11SMajd Dibbiny {
11111b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11121b5daf11SMajd Dibbiny 
1113db7a691aSMichael Guralnik 	if (active_width & MLX5_IB_WIDTH_1X)
11141b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1115d764970bSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_2X)
1116d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1117db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_4X)
11181b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1119db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_8X)
11201b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1121db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_12X)
11221b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1123db7a691aSMichael Guralnik 	else {
1124db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
11251b5daf11SMajd Dibbiny 			    (int)active_width);
1126db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
11271b5daf11SMajd Dibbiny 	}
11281b5daf11SMajd Dibbiny 
1129db7a691aSMichael Guralnik 	return;
11301b5daf11SMajd Dibbiny }
11311b5daf11SMajd Dibbiny 
11321b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
11331b5daf11SMajd Dibbiny {
11341b5daf11SMajd Dibbiny 	switch (mtu) {
11351b5daf11SMajd Dibbiny 	case 256: return 1;
11361b5daf11SMajd Dibbiny 	case 512: return 2;
11371b5daf11SMajd Dibbiny 	case 1024: return 3;
11381b5daf11SMajd Dibbiny 	case 2048: return 4;
11391b5daf11SMajd Dibbiny 	case 4096: return 5;
11401b5daf11SMajd Dibbiny 	default:
11411b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
11421b5daf11SMajd Dibbiny 		return -1;
11431b5daf11SMajd Dibbiny 	}
11441b5daf11SMajd Dibbiny }
11451b5daf11SMajd Dibbiny 
11461b5daf11SMajd Dibbiny enum ib_max_vl_num {
11471b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
11481b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
11491b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
11501b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
11511b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
11521b5daf11SMajd Dibbiny };
11531b5daf11SMajd Dibbiny 
11541b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
11551b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
11561b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
11571b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
11581b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
11591b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
11601b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
11611b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
11621b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
11631b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
11641b5daf11SMajd Dibbiny };
11651b5daf11SMajd Dibbiny 
11661b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
11671b5daf11SMajd Dibbiny 				u8 *max_vl_num)
11681b5daf11SMajd Dibbiny {
11691b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
11701b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
11711b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
11721b5daf11SMajd Dibbiny 		break;
11731b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
11741b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
11751b5daf11SMajd Dibbiny 		break;
11761b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
11771b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
11781b5daf11SMajd Dibbiny 		break;
11791b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
11801b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
11811b5daf11SMajd Dibbiny 		break;
11821b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
11831b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
11841b5daf11SMajd Dibbiny 		break;
11851b5daf11SMajd Dibbiny 
11861b5daf11SMajd Dibbiny 	default:
11871b5daf11SMajd Dibbiny 		return -EINVAL;
11881b5daf11SMajd Dibbiny 	}
11891b5daf11SMajd Dibbiny 
11901b5daf11SMajd Dibbiny 	return 0;
11911b5daf11SMajd Dibbiny }
11921b5daf11SMajd Dibbiny 
11931b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
11941b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
11951b5daf11SMajd Dibbiny {
11961b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11971b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
11981b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1199046339eaSSaeed Mahameed 	u16 max_mtu;
1200046339eaSSaeed Mahameed 	u16 oper_mtu;
12011b5daf11SMajd Dibbiny 	int err;
12021b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
12031b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
12041b5daf11SMajd Dibbiny 
12051b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
12061b5daf11SMajd Dibbiny 	if (!rep) {
12071b5daf11SMajd Dibbiny 		err = -ENOMEM;
12081b5daf11SMajd Dibbiny 		goto out;
12091b5daf11SMajd Dibbiny 	}
12101b5daf11SMajd Dibbiny 
1211c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
12121b5daf11SMajd Dibbiny 
12131b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
12141b5daf11SMajd Dibbiny 	if (err)
12151b5daf11SMajd Dibbiny 		goto out;
12161b5daf11SMajd Dibbiny 
12171b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
12181b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
12191b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
12201b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
12211b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
12221b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
12231b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
12241b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
12251b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
12261b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
12271b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
12281b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
12291b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
12301b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
12311b5daf11SMajd Dibbiny 
12324106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
12334106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
12344106a758SMichael Guralnik 
12351b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
12361b5daf11SMajd Dibbiny 	if (err)
12371b5daf11SMajd Dibbiny 		goto out;
12381b5daf11SMajd Dibbiny 
1239db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1240db7a691aSMichael Guralnik 
1241d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
12421b5daf11SMajd Dibbiny 	if (err)
12431b5daf11SMajd Dibbiny 		goto out;
12441b5daf11SMajd Dibbiny 
1245facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
12461b5daf11SMajd Dibbiny 
12471b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
12481b5daf11SMajd Dibbiny 
1249facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
12501b5daf11SMajd Dibbiny 
12511b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
12521b5daf11SMajd Dibbiny 
12531b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
12541b5daf11SMajd Dibbiny 	if (err)
12551b5daf11SMajd Dibbiny 		goto out;
12561b5daf11SMajd Dibbiny 
12571b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
12581b5daf11SMajd Dibbiny 				   &props->max_vl_num);
12591b5daf11SMajd Dibbiny out:
12601b5daf11SMajd Dibbiny 	kfree(rep);
1261e126ba97SEli Cohen 	return err;
1262e126ba97SEli Cohen }
1263e126ba97SEli Cohen 
1264e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1265e126ba97SEli Cohen 		       struct ib_port_attr *props)
1266e126ba97SEli Cohen {
1267095b0927SIlan Tayari 	unsigned int count;
1268095b0927SIlan Tayari 	int ret;
1269095b0927SIlan Tayari 
12701b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
12711b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1272095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1273095b0927SIlan Tayari 		break;
1274e126ba97SEli Cohen 
12751b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1276095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1277095b0927SIlan Tayari 		break;
12781b5daf11SMajd Dibbiny 
12793f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1280095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1281095b0927SIlan Tayari 		break;
12823f89a643SAchiad Shochat 
12831b5daf11SMajd Dibbiny 	default:
1284095b0927SIlan Tayari 		ret = -EINVAL;
1285e126ba97SEli Cohen 	}
1286095b0927SIlan Tayari 
1287095b0927SIlan Tayari 	if (!ret && props) {
1288b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1289b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1290b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1291b3cbd6f0SDaniel Jurgens 
1292b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1293b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1294b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1295b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1296b3cbd6f0SDaniel Jurgens 			 */
1297b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1298b3cbd6f0SDaniel Jurgens 			port = 1;
1299b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1300b3cbd6f0SDaniel Jurgens 		}
1301b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1302b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1303b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1304095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1305095b0927SIlan Tayari 	}
1306095b0927SIlan Tayari 	return ret;
1307e126ba97SEli Cohen }
1308e126ba97SEli Cohen 
13098e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
13108e6efa3aSMark Bloch 				  struct ib_port_attr *props)
13118e6efa3aSMark Bloch {
13128e6efa3aSMark Bloch 	int ret;
13138e6efa3aSMark Bloch 
13148e6efa3aSMark Bloch 	/* Only link layer == ethernet is valid for representors */
13158e6efa3aSMark Bloch 	ret = mlx5_query_port_roce(ibdev, port, props);
13168e6efa3aSMark Bloch 	if (ret || !props)
13178e6efa3aSMark Bloch 		return ret;
13188e6efa3aSMark Bloch 
13198e6efa3aSMark Bloch 	/* We don't support GIDS */
13208e6efa3aSMark Bloch 	props->gid_tbl_len = 0;
13218e6efa3aSMark Bloch 
13228e6efa3aSMark Bloch 	return ret;
13238e6efa3aSMark Bloch }
13248e6efa3aSMark Bloch 
1325e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1326e126ba97SEli Cohen 			     union ib_gid *gid)
1327e126ba97SEli Cohen {
13281b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
13291b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1330e126ba97SEli Cohen 
13311b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13321b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
13331b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1334e126ba97SEli Cohen 
13351b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
13361b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1337e126ba97SEli Cohen 
13381b5daf11SMajd Dibbiny 	default:
13391b5daf11SMajd Dibbiny 		return -EINVAL;
13401b5daf11SMajd Dibbiny 	}
1341e126ba97SEli Cohen 
1342e126ba97SEli Cohen }
1343e126ba97SEli Cohen 
1344b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1345b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1346b3cbd6f0SDaniel Jurgens {
1347b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1348b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1349b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1350b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1351b3cbd6f0SDaniel Jurgens 	int err;
1352b3cbd6f0SDaniel Jurgens 
1353b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1354b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1355b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1356b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1357b3cbd6f0SDaniel Jurgens 		 */
1358b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1359b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1360b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1361b3cbd6f0SDaniel Jurgens 	}
1362b3cbd6f0SDaniel Jurgens 
1363b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1364b3cbd6f0SDaniel Jurgens 					index, pkey);
1365b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1366b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1367b3cbd6f0SDaniel Jurgens 
1368b3cbd6f0SDaniel Jurgens 	return err;
1369b3cbd6f0SDaniel Jurgens }
1370b3cbd6f0SDaniel Jurgens 
1371e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1372e126ba97SEli Cohen 			      u16 *pkey)
1373e126ba97SEli Cohen {
13741b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13751b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
13761b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1377e126ba97SEli Cohen 
13781b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
13791b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1380b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
13811b5daf11SMajd Dibbiny 	default:
13821b5daf11SMajd Dibbiny 		return -EINVAL;
1383e126ba97SEli Cohen 	}
13841b5daf11SMajd Dibbiny }
1385e126ba97SEli Cohen 
1386e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1387e126ba97SEli Cohen 				 struct ib_device_modify *props)
1388e126ba97SEli Cohen {
1389e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1390e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1391e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1392e126ba97SEli Cohen 	int err;
1393e126ba97SEli Cohen 
1394e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1395e126ba97SEli Cohen 		return -EOPNOTSUPP;
1396e126ba97SEli Cohen 
1397e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1398e126ba97SEli Cohen 		return 0;
1399e126ba97SEli Cohen 
1400e126ba97SEli Cohen 	/*
1401e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1402e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1403e126ba97SEli Cohen 	 */
1404bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
14059603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1406e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1407e126ba97SEli Cohen 	if (err)
1408e126ba97SEli Cohen 		return err;
1409e126ba97SEli Cohen 
1410bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1411e126ba97SEli Cohen 
1412e126ba97SEli Cohen 	return err;
1413e126ba97SEli Cohen }
1414e126ba97SEli Cohen 
1415cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1416cdbe33d0SEli Cohen 				u32 value)
1417cdbe33d0SEli Cohen {
1418cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1419b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1420b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1421cdbe33d0SEli Cohen 	int err;
1422cdbe33d0SEli Cohen 
1423b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1424b3cbd6f0SDaniel Jurgens 	if (!mdev)
1425b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1426b3cbd6f0SDaniel Jurgens 
1427b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1428cdbe33d0SEli Cohen 	if (err)
1429b3cbd6f0SDaniel Jurgens 		goto out;
1430cdbe33d0SEli Cohen 
1431cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1432cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1433cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1434b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1435b3cbd6f0SDaniel Jurgens 		goto out;
1436cdbe33d0SEli Cohen 	}
1437cdbe33d0SEli Cohen 
1438cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1439cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1440b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1441b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1442b3cbd6f0SDaniel Jurgens 
1443b3cbd6f0SDaniel Jurgens out:
1444b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1445cdbe33d0SEli Cohen 
1446cdbe33d0SEli Cohen 	return err;
1447cdbe33d0SEli Cohen }
1448cdbe33d0SEli Cohen 
1449e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1450e126ba97SEli Cohen 			       struct ib_port_modify *props)
1451e126ba97SEli Cohen {
1452e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1453e126ba97SEli Cohen 	struct ib_port_attr attr;
1454e126ba97SEli Cohen 	u32 tmp;
1455e126ba97SEli Cohen 	int err;
1456cdbe33d0SEli Cohen 	u32 change_mask;
1457cdbe33d0SEli Cohen 	u32 value;
1458cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1459cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1460cdbe33d0SEli Cohen 
1461ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1462ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1463ec255879SMajd Dibbiny 	 */
1464ec255879SMajd Dibbiny 	if (!is_ib)
1465ec255879SMajd Dibbiny 		return 0;
1466ec255879SMajd Dibbiny 
1467cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1468cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1469cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1470cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1471cdbe33d0SEli Cohen 	}
1472e126ba97SEli Cohen 
1473e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1474e126ba97SEli Cohen 
1475c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1476e126ba97SEli Cohen 	if (err)
1477e126ba97SEli Cohen 		goto out;
1478e126ba97SEli Cohen 
1479e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1480e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1481e126ba97SEli Cohen 
14829603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1483e126ba97SEli Cohen 
1484e126ba97SEli Cohen out:
1485e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1486e126ba97SEli Cohen 	return err;
1487e126ba97SEli Cohen }
1488e126ba97SEli Cohen 
148930aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
149030aa60b3SEli Cohen {
149130aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
149230aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
149330aa60b3SEli Cohen }
149430aa60b3SEli Cohen 
149531a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
149631a78a5aSYishai Hadas {
149731a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
149831a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
149931a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
150031a78a5aSYishai Hadas 
150131a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
150231a78a5aSYishai Hadas }
150331a78a5aSYishai Hadas 
1504b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1505b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
150631a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1507b037c29aSEli Cohen {
1508b037c29aSEli Cohen 	int uars_per_sys_page;
1509b037c29aSEli Cohen 	int bfregs_per_sys_page;
1510b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1511b037c29aSEli Cohen 
1512b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1513b037c29aSEli Cohen 		return -EINVAL;
1514b037c29aSEli Cohen 
1515b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1516b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1517b037c29aSEli Cohen 
1518b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1519b037c29aSEli Cohen 		return -ENOMEM;
1520b037c29aSEli Cohen 
1521b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1522b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
152331a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1524b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1525b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1526b037c29aSEli Cohen 		return -EINVAL;
1527b037c29aSEli Cohen 
152831a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
152931a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
153031a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
153131a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
153231a78a5aSYishai Hadas 
153331a78a5aSYishai 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",
1534b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1535b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
153631a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
153731a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1538b037c29aSEli Cohen 
1539b037c29aSEli Cohen 	return 0;
1540b037c29aSEli Cohen }
1541b037c29aSEli Cohen 
1542b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1543b037c29aSEli Cohen {
1544b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1545b037c29aSEli Cohen 	int err;
1546b037c29aSEli Cohen 	int i;
1547b037c29aSEli Cohen 
1548b037c29aSEli Cohen 	bfregi = &context->bfregi;
154931a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1550b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1551b037c29aSEli Cohen 		if (err)
1552b037c29aSEli Cohen 			goto error;
1553b037c29aSEli Cohen 
1554b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1555b037c29aSEli Cohen 	}
15564ed131d0SYishai Hadas 
15574ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
15584ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
15594ed131d0SYishai Hadas 
1560b037c29aSEli Cohen 	return 0;
1561b037c29aSEli Cohen 
1562b037c29aSEli Cohen error:
1563b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1564b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1565b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1566b037c29aSEli Cohen 
1567b037c29aSEli Cohen 	return err;
1568b037c29aSEli Cohen }
1569b037c29aSEli Cohen 
157015177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
157115177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1572b037c29aSEli Cohen {
1573b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1574b037c29aSEli Cohen 	int i;
1575b037c29aSEli Cohen 
1576b037c29aSEli Cohen 	bfregi = &context->bfregi;
157715177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
15784ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
157915177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
158015177999SLeon Romanovsky 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1581b037c29aSEli Cohen }
1582b037c29aSEli Cohen 
15830042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1584a560f1d9SMark Bloch {
1585a560f1d9SMark Bloch 	int err = 0;
1586a560f1d9SMark Bloch 
1587a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
15880042f9e4SMark Bloch 	if (td)
1589a560f1d9SMark Bloch 		dev->lb.user_td++;
15900042f9e4SMark Bloch 	if (qp)
15910042f9e4SMark Bloch 		dev->lb.qps++;
1592a560f1d9SMark Bloch 
15930042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
15940042f9e4SMark Bloch 	    dev->lb.qps == 1) {
15950042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1596a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
15970042f9e4SMark Bloch 			dev->lb.enabled = true;
15980042f9e4SMark Bloch 		}
15990042f9e4SMark Bloch 	}
1600a560f1d9SMark Bloch 
1601a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1602a560f1d9SMark Bloch 
1603a560f1d9SMark Bloch 	return err;
1604a560f1d9SMark Bloch }
1605a560f1d9SMark Bloch 
16060042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1607a560f1d9SMark Bloch {
1608a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16090042f9e4SMark Bloch 	if (td)
1610a560f1d9SMark Bloch 		dev->lb.user_td--;
16110042f9e4SMark Bloch 	if (qp)
16120042f9e4SMark Bloch 		dev->lb.qps--;
1613a560f1d9SMark Bloch 
16140042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
16150042f9e4SMark Bloch 	    dev->lb.qps == 0) {
16160042f9e4SMark Bloch 		if (dev->lb.enabled) {
1617a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
16180042f9e4SMark Bloch 			dev->lb.enabled = false;
16190042f9e4SMark Bloch 		}
16200042f9e4SMark Bloch 	}
1621a560f1d9SMark Bloch 
1622a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1623a560f1d9SMark Bloch }
1624a560f1d9SMark Bloch 
1625d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1626d2d19121SYishai Hadas 					  u16 uid)
1627c85023e1SHuy Nguyen {
1628c85023e1SHuy Nguyen 	int err;
1629c85023e1SHuy Nguyen 
1630cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1631cfdeb893SLeon Romanovsky 		return 0;
1632cfdeb893SLeon Romanovsky 
1633d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1634c85023e1SHuy Nguyen 	if (err)
1635c85023e1SHuy Nguyen 		return err;
1636c85023e1SHuy Nguyen 
1637c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
16388978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
16398978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1640c85023e1SHuy Nguyen 		return err;
1641c85023e1SHuy Nguyen 
16420042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1643c85023e1SHuy Nguyen }
1644c85023e1SHuy Nguyen 
1645d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1646d2d19121SYishai Hadas 					     u16 uid)
1647c85023e1SHuy Nguyen {
1648cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1649cfdeb893SLeon Romanovsky 		return;
1650cfdeb893SLeon Romanovsky 
1651d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1652c85023e1SHuy Nguyen 
1653c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
16548978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
16558978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1656c85023e1SHuy Nguyen 		return;
1657c85023e1SHuy Nguyen 
16580042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1659c85023e1SHuy Nguyen }
1660c85023e1SHuy Nguyen 
1661e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1662e126ba97SEli Cohen 						  struct ib_udata *udata)
1663e126ba97SEli Cohen {
1664e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1665b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1666b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
16675c99eaecSFeras Daoud 	struct mlx5_core_dev *mdev = dev->mdev;
1668e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
16692f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
167078c0f98cSEli Cohen 	int ver;
1671e126ba97SEli Cohen 	int err;
1672a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1673a168a41cSMajd Dibbiny 				     max_cqe_version);
167425bb36e7SYonatan Cohen 	u32 dump_fill_mkey;
1675b037c29aSEli Cohen 	bool lib_uar_4k;
1676e126ba97SEli Cohen 
1677e126ba97SEli Cohen 	if (!dev->ib_active)
1678e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1679e126ba97SEli Cohen 
1680e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
168178c0f98cSEli Cohen 		ver = 0;
1682e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
168378c0f98cSEli Cohen 		ver = 2;
168478c0f98cSEli Cohen 	else
168578c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
168678c0f98cSEli Cohen 
1687e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1688e126ba97SEli Cohen 	if (err)
1689e126ba97SEli Cohen 		return ERR_PTR(err);
1690e126ba97SEli Cohen 
1691a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1692a8b92ca1SYishai Hadas 		return ERR_PTR(-EOPNOTSUPP);
169378c0f98cSEli Cohen 
1694f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1695b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1696b368d7cbSMatan Barak 
16972f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
16982f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
16992f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1700e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1701e126ba97SEli Cohen 
1702938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
17032cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1704938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1705b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1706938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1707938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1708938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1709938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1710938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1711f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1712f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1713f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
171430aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
171530aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
171630aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
171730aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1718b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1719b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1720e126ba97SEli Cohen 
1721c03faa56SMatan Barak 	if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1722c03faa56SMatan Barak 		if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1723c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1724c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1725c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1726c03faa56SMatan Barak 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1727c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1728c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1729c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1730c03faa56SMatan Barak 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1731c03faa56SMatan Barak 	}
1732c03faa56SMatan Barak 
1733e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1734e126ba97SEli Cohen 	if (!context)
1735e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1736e126ba97SEli Cohen 
173730aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
17382f5ff264SEli Cohen 	bfregi = &context->bfregi;
1739b037c29aSEli Cohen 
1740b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
174131a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1742b037c29aSEli Cohen 	if (err)
1743b037c29aSEli Cohen 		goto out_ctx;
1744b037c29aSEli Cohen 
17452f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1746b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
174731a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1748b037c29aSEli Cohen 				GFP_KERNEL);
1749b037c29aSEli Cohen 	if (!bfregi->count) {
1750e126ba97SEli Cohen 		err = -ENOMEM;
1751e126ba97SEli Cohen 		goto out_ctx;
1752e126ba97SEli Cohen 	}
1753e126ba97SEli Cohen 
1754b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1755b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1756e126ba97SEli Cohen 				    GFP_KERNEL);
1757b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1758e126ba97SEli Cohen 		err = -ENOMEM;
1759e126ba97SEli Cohen 		goto out_count;
1760e126ba97SEli Cohen 	}
1761e126ba97SEli Cohen 
1762b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1763b037c29aSEli Cohen 	if (err)
1764b037c29aSEli Cohen 		goto out_sys_pages;
1765b037c29aSEli Cohen 
1766b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1767b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1768b4cfe447SHaggai Eran #endif
1769b4cfe447SHaggai Eran 
1770a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1771fb98153bSYishai Hadas 		err = mlx5_ib_devx_create(dev, true);
177276dc5a84SYishai Hadas 		if (err < 0)
1773d2d19121SYishai Hadas 			goto out_uars;
177476dc5a84SYishai Hadas 		context->devx_uid = err;
1775a8b92ca1SYishai Hadas 	}
1776a8b92ca1SYishai Hadas 
1777d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1778d2d19121SYishai Hadas 					     context->devx_uid);
1779a8b92ca1SYishai Hadas 	if (err)
1780d2d19121SYishai Hadas 		goto out_devx;
1781a8b92ca1SYishai Hadas 
178225bb36e7SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
178325bb36e7SYonatan Cohen 		err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
178425bb36e7SYonatan Cohen 		if (err)
17858193abb6SJason Gunthorpe 			goto out_mdev;
178625bb36e7SYonatan Cohen 	}
178725bb36e7SYonatan Cohen 
1788e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1789e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1790e126ba97SEli Cohen 
17912f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1792508562d6SDaniel Jurgens 	resp.num_ports = dev->num_ports;
1793b368d7cbSMatan Barak 
1794f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1795f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1796b368d7cbSMatan Barak 
1797402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
17986ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
17996ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1800402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1801402ca536SBodong Wang 	}
1802402ca536SBodong Wang 
180378984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
180478984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
180578984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
180678984898SOr Gerlitz 			resp.eth_min_inline++;
180778984898SOr Gerlitz 		}
180878984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
180978984898SOr Gerlitz 	}
181078984898SOr Gerlitz 
18115c99eaecSFeras Daoud 	if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
18125c99eaecSFeras Daoud 		if (mdev->clock_info)
18135c99eaecSFeras Daoud 			resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
18145c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.clock_info_versions);
18155c99eaecSFeras Daoud 	}
18165c99eaecSFeras Daoud 
1817bc5c6eedSNoa Osherovich 	/*
1818bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1819bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1820bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1821bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1822bc5c6eedSNoa Osherovich 	 */
1823de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1824de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1825b368d7cbSMatan Barak 			resp.comp_mask |=
1826b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1827b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1828de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1829de8d6e02SEli Cohen 		}
18305c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.hca_core_clock_offset);
1831b368d7cbSMatan Barak 	}
1832b368d7cbSMatan Barak 
183330aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
183430aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
183530aa60b3SEli Cohen 
183630aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
183730aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
183830aa60b3SEli Cohen 
183931a78a5aSYishai Hadas 	if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
184031a78a5aSYishai Hadas 		resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
184131a78a5aSYishai Hadas 		resp.response_length += sizeof(resp.num_dyn_bfregs);
184231a78a5aSYishai Hadas 	}
184331a78a5aSYishai Hadas 
184425bb36e7SYonatan Cohen 	if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
184525bb36e7SYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
184625bb36e7SYonatan Cohen 			resp.dump_fill_mkey = dump_fill_mkey;
184725bb36e7SYonatan Cohen 			resp.comp_mask |=
184825bb36e7SYonatan Cohen 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
184925bb36e7SYonatan Cohen 		}
185025bb36e7SYonatan Cohen 		resp.response_length += sizeof(resp.dump_fill_mkey);
185125bb36e7SYonatan Cohen 	}
185225bb36e7SYonatan Cohen 
1853b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1854e126ba97SEli Cohen 	if (err)
1855a8b92ca1SYishai Hadas 		goto out_mdev;
1856e126ba97SEli Cohen 
18572f5ff264SEli Cohen 	bfregi->ver = ver;
18582f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1859f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
186030aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
186130aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1862f72300c5SHaggai Abramovsky 
1863c6a21c38SMajd Dibbiny 	if (mlx5_lag_is_active(dev->mdev)) {
1864c6a21c38SMajd Dibbiny 		u8 port = mlx5_core_native_port_num(dev->mdev);
1865c6a21c38SMajd Dibbiny 
1866c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1867c6a21c38SMajd Dibbiny 			   atomic_add_return(
1868c6a21c38SMajd Dibbiny 				   1, &dev->roce[port].tx_port_affinity));
1869c6a21c38SMajd Dibbiny 	}
1870c6a21c38SMajd Dibbiny 
1871e126ba97SEli Cohen 	return &context->ibucontext;
1872e126ba97SEli Cohen 
1873a8b92ca1SYishai Hadas out_mdev:
1874d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1875d2d19121SYishai Hadas out_devx:
1876a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
187776dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1878146d2f1aSmajd@mellanox.com 
1879e126ba97SEli Cohen out_uars:
1880b037c29aSEli Cohen 	deallocate_uars(dev, context);
1881b037c29aSEli Cohen 
1882b037c29aSEli Cohen out_sys_pages:
1883b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1884b037c29aSEli Cohen 
1885e126ba97SEli Cohen out_count:
18862f5ff264SEli Cohen 	kfree(bfregi->count);
1887e126ba97SEli Cohen 
1888e126ba97SEli Cohen out_ctx:
1889e126ba97SEli Cohen 	kfree(context);
1890b037c29aSEli Cohen 
1891e126ba97SEli Cohen 	return ERR_PTR(err);
1892e126ba97SEli Cohen }
1893e126ba97SEli Cohen 
1894e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1895e126ba97SEli Cohen {
1896e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1897e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1898b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1899e126ba97SEli Cohen 
1900f27a0d50SJason Gunthorpe #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1901f27a0d50SJason Gunthorpe 	/* All umem's must be destroyed before destroying the ucontext. */
1902f27a0d50SJason Gunthorpe 	mutex_lock(&ibcontext->per_mm_list_lock);
1903f27a0d50SJason Gunthorpe 	WARN_ON(!list_empty(&ibcontext->per_mm_list));
1904f27a0d50SJason Gunthorpe 	mutex_unlock(&ibcontext->per_mm_list_lock);
1905f27a0d50SJason Gunthorpe #endif
1906a8b92ca1SYishai Hadas 
1907b037c29aSEli Cohen 	bfregi = &context->bfregi;
1908d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1909d2d19121SYishai Hadas 
1910b037c29aSEli Cohen 	if (context->devx_uid)
191176dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1912e126ba97SEli Cohen 
1913e126ba97SEli Cohen 	deallocate_uars(dev, context);
19142f5ff264SEli Cohen 	kfree(bfregi->sys_pages);
19152f5ff264SEli Cohen 	kfree(bfregi->count);
19162f5ff264SEli Cohen 	kfree(context);
1917e126ba97SEli Cohen 
1918e126ba97SEli Cohen 	return 0;
1919e126ba97SEli Cohen }
1920e126ba97SEli Cohen 
1921e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1922e126ba97SEli Cohen 				 int uar_idx)
1923e126ba97SEli Cohen {
1924b037c29aSEli Cohen 	int fw_uars_per_page;
1925b037c29aSEli Cohen 
1926b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1927b037c29aSEli Cohen 
19284ed131d0SYishai Hadas 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
1929e126ba97SEli Cohen }
1930e126ba97SEli Cohen 
1931e126ba97SEli Cohen static int get_command(unsigned long offset)
1932e126ba97SEli Cohen {
1933e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1934e126ba97SEli Cohen }
1935e126ba97SEli Cohen 
1936e126ba97SEli Cohen static int get_arg(unsigned long offset)
1937e126ba97SEli Cohen {
1938e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1939e126ba97SEli Cohen }
1940e126ba97SEli Cohen 
1941e126ba97SEli Cohen static int get_index(unsigned long offset)
1942e126ba97SEli Cohen {
1943e126ba97SEli Cohen 	return get_arg(offset);
1944e126ba97SEli Cohen }
1945e126ba97SEli Cohen 
19464ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
19474ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
19484ed131d0SYishai Hadas {
19494ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
19504ed131d0SYishai Hadas }
19514ed131d0SYishai Hadas 
19527c2344c3SMaor Gottlieb 
19537c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
19547c2344c3SMaor Gottlieb {
19557c2344c3SMaor Gottlieb }
19567c2344c3SMaor Gottlieb 
195737aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1958e126ba97SEli Cohen {
195937aa5c36SGuy Levi 	switch (cmd) {
196037aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
196137aa5c36SGuy Levi 		return "WC";
1962e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
196337aa5c36SGuy Levi 		return "best effort WC";
196437aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
196537aa5c36SGuy Levi 		return "NC";
196624da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
196724da0016SAriel Levkovich 		return "Device Memory";
196837aa5c36SGuy Levi 	default:
196937aa5c36SGuy Levi 		return NULL;
197037aa5c36SGuy Levi 	}
197137aa5c36SGuy Levi }
197237aa5c36SGuy Levi 
19735c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
19745c99eaecSFeras Daoud 					struct vm_area_struct *vma,
19755c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
19765c99eaecSFeras Daoud {
19775c99eaecSFeras Daoud 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
19785c99eaecSFeras Daoud 		return -EINVAL;
19795c99eaecSFeras Daoud 
19805c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
19815c99eaecSFeras Daoud 		return -EOPNOTSUPP;
19825c99eaecSFeras Daoud 
19835c99eaecSFeras Daoud 	if (vma->vm_flags & VM_WRITE)
19845c99eaecSFeras Daoud 		return -EPERM;
19855c99eaecSFeras Daoud 
19865c99eaecSFeras Daoud 	if (!dev->mdev->clock_info_page)
19875c99eaecSFeras Daoud 		return -EOPNOTSUPP;
19885c99eaecSFeras Daoud 
1989e2cd1d1aSJason Gunthorpe 	return rdma_user_mmap_page(&context->ibucontext, vma,
1990e2cd1d1aSJason Gunthorpe 				   dev->mdev->clock_info_page, PAGE_SIZE);
19915c99eaecSFeras Daoud }
19925c99eaecSFeras Daoud 
199337aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
19947c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
19957c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
199637aa5c36SGuy Levi {
19972f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
199837aa5c36SGuy Levi 	int err;
199937aa5c36SGuy Levi 	unsigned long idx;
2000aa09ea6eSKamal Heib 	phys_addr_t pfn;
200137aa5c36SGuy Levi 	pgprot_t prot;
20024ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
20034ed131d0SYishai Hadas 	u32 uar_index;
20044ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
20054ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
20064ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2007b037c29aSEli Cohen 
2008b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2009b037c29aSEli Cohen 		return -EINVAL;
2010b037c29aSEli Cohen 
20114ed131d0SYishai Hadas 	if (dyn_uar)
20124ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
20134ed131d0SYishai Hadas 	else
2014b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
20154ed131d0SYishai Hadas 
20164ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
20174ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
20184ed131d0SYishai Hadas 			     idx, max_valid_idx);
2019b037c29aSEli Cohen 		return -EINVAL;
2020b037c29aSEli Cohen 	}
202137aa5c36SGuy Levi 
202237aa5c36SGuy Levi 	switch (cmd) {
202337aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
20244ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
202537aa5c36SGuy Levi /* Some architectures don't support WC memory */
202637aa5c36SGuy Levi #if defined(CONFIG_X86)
202737aa5c36SGuy Levi 		if (!pat_enabled())
202837aa5c36SGuy Levi 			return -EPERM;
202937aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
203037aa5c36SGuy Levi 			return -EPERM;
203137aa5c36SGuy Levi #endif
203237aa5c36SGuy Levi 	/* fall through */
203337aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
203437aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
203537aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
203637aa5c36SGuy Levi 		break;
203737aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
203837aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
203937aa5c36SGuy Levi 		break;
204037aa5c36SGuy Levi 	default:
204137aa5c36SGuy Levi 		return -EINVAL;
204237aa5c36SGuy Levi 	}
204337aa5c36SGuy Levi 
20444ed131d0SYishai Hadas 	if (dyn_uar) {
20454ed131d0SYishai Hadas 		int uars_per_page;
20464ed131d0SYishai Hadas 
20474ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
20484ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
20494ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
20504ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
20514ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
20524ed131d0SYishai Hadas 			return -EINVAL;
20534ed131d0SYishai Hadas 		}
20544ed131d0SYishai Hadas 
20554ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
20564ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
20574ed131d0SYishai Hadas 		 * page holds its count.
20584ed131d0SYishai Hadas 		 */
20594ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
20604ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
20614ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
20624ed131d0SYishai Hadas 			return -EINVAL;
20634ed131d0SYishai Hadas 		}
20644ed131d0SYishai Hadas 
20654ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
20664ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
20674ed131d0SYishai Hadas 
20684ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
20694ed131d0SYishai Hadas 		if (err) {
20704ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
20714ed131d0SYishai Hadas 			goto free_bfreg;
20724ed131d0SYishai Hadas 		}
20734ed131d0SYishai Hadas 	} else {
20744ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
20754ed131d0SYishai Hadas 	}
20764ed131d0SYishai Hadas 
20774ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
207837aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2079e126ba97SEli Cohen 
2080e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2081e2cd1d1aSJason Gunthorpe 				prot);
208237aa5c36SGuy Levi 	if (err) {
20838f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2084e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
20858f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
20864ed131d0SYishai Hadas 		goto err;
208737aa5c36SGuy Levi 	}
2088e126ba97SEli Cohen 
20894ed131d0SYishai Hadas 	if (dyn_uar)
20904ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
20914ed131d0SYishai Hadas 	return 0;
20924ed131d0SYishai Hadas 
20934ed131d0SYishai Hadas err:
20944ed131d0SYishai Hadas 	if (!dyn_uar)
20954ed131d0SYishai Hadas 		return err;
20964ed131d0SYishai Hadas 
20974ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
20984ed131d0SYishai Hadas 
20994ed131d0SYishai Hadas free_bfreg:
21004ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
21014ed131d0SYishai Hadas 
21024ed131d0SYishai Hadas 	return err;
210337aa5c36SGuy Levi }
210437aa5c36SGuy Levi 
210524da0016SAriel Levkovich static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
210624da0016SAriel Levkovich {
210724da0016SAriel Levkovich 	struct mlx5_ib_ucontext *mctx = to_mucontext(context);
210824da0016SAriel Levkovich 	struct mlx5_ib_dev *dev = to_mdev(context->device);
210924da0016SAriel Levkovich 	u16 page_idx = get_extended_index(vma->vm_pgoff);
211024da0016SAriel Levkovich 	size_t map_size = vma->vm_end - vma->vm_start;
211124da0016SAriel Levkovich 	u32 npages = map_size >> PAGE_SHIFT;
211224da0016SAriel Levkovich 	phys_addr_t pfn;
211324da0016SAriel Levkovich 
211424da0016SAriel Levkovich 	if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
211524da0016SAriel Levkovich 	    page_idx + npages)
211624da0016SAriel Levkovich 		return -EINVAL;
211724da0016SAriel Levkovich 
211824da0016SAriel Levkovich 	pfn = ((pci_resource_start(dev->mdev->pdev, 0) +
211924da0016SAriel Levkovich 	      MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
212024da0016SAriel Levkovich 	      PAGE_SHIFT) +
212124da0016SAriel Levkovich 	      page_idx;
2122e2cd1d1aSJason Gunthorpe 	return rdma_user_mmap_io(context, vma, pfn, map_size,
2123e2cd1d1aSJason Gunthorpe 				 pgprot_writecombine(vma->vm_page_prot));
212424da0016SAriel Levkovich }
212524da0016SAriel Levkovich 
212637aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
212737aa5c36SGuy Levi {
212837aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
212937aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
213037aa5c36SGuy Levi 	unsigned long command;
213137aa5c36SGuy Levi 	phys_addr_t pfn;
213237aa5c36SGuy Levi 
213337aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
213437aa5c36SGuy Levi 	switch (command) {
213537aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
213637aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
213737aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
21384ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
21397c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2140e126ba97SEli Cohen 
2141e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2142e126ba97SEli Cohen 		return -ENOSYS;
2143e126ba97SEli Cohen 
2144d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2145d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2146d69e3bcfSMatan Barak 			return -EINVAL;
2147d69e3bcfSMatan Barak 
21486cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2149d69e3bcfSMatan Barak 			return -EPERM;
2150d69e3bcfSMatan Barak 
2151d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2152d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2153d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2154d69e3bcfSMatan Barak 
2155d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2156d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2157d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2158d69e3bcfSMatan Barak 			PAGE_SHIFT;
2159d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2160d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
2161d69e3bcfSMatan Barak 			return -EAGAIN;
2162d69e3bcfSMatan Barak 		break;
21635c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
21645c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2165d69e3bcfSMatan Barak 
216624da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
216724da0016SAriel Levkovich 		return dm_mmap(ibcontext, vma);
216824da0016SAriel Levkovich 
2169e126ba97SEli Cohen 	default:
2170e126ba97SEli Cohen 		return -EINVAL;
2171e126ba97SEli Cohen 	}
2172e126ba97SEli Cohen 
2173e126ba97SEli Cohen 	return 0;
2174e126ba97SEli Cohen }
2175e126ba97SEli Cohen 
217624da0016SAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
217724da0016SAriel Levkovich 			       struct ib_ucontext *context,
217824da0016SAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
217924da0016SAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
218024da0016SAriel Levkovich {
218124da0016SAriel Levkovich 	u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
218224da0016SAriel Levkovich 	struct mlx5_memic *memic = &to_mdev(ibdev)->memic;
218324da0016SAriel Levkovich 	phys_addr_t memic_addr;
218424da0016SAriel Levkovich 	struct mlx5_ib_dm *dm;
218524da0016SAriel Levkovich 	u64 start_offset;
218624da0016SAriel Levkovich 	u32 page_idx;
218724da0016SAriel Levkovich 	int err;
218824da0016SAriel Levkovich 
218924da0016SAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
219024da0016SAriel Levkovich 	if (!dm)
219124da0016SAriel Levkovich 		return ERR_PTR(-ENOMEM);
219224da0016SAriel Levkovich 
219324da0016SAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
219424da0016SAriel Levkovich 		    attr->length, act_size, attr->alignment);
219524da0016SAriel Levkovich 
219624da0016SAriel Levkovich 	err = mlx5_cmd_alloc_memic(memic, &memic_addr,
219724da0016SAriel Levkovich 				   act_size, attr->alignment);
219824da0016SAriel Levkovich 	if (err)
219924da0016SAriel Levkovich 		goto err_free;
220024da0016SAriel Levkovich 
220124da0016SAriel Levkovich 	start_offset = memic_addr & ~PAGE_MASK;
220224da0016SAriel Levkovich 	page_idx = (memic_addr - pci_resource_start(memic->dev->pdev, 0) -
220324da0016SAriel Levkovich 		    MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
220424da0016SAriel Levkovich 		    PAGE_SHIFT;
220524da0016SAriel Levkovich 
220624da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
220724da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
220824da0016SAriel Levkovich 			     &start_offset, sizeof(start_offset));
220924da0016SAriel Levkovich 	if (err)
221024da0016SAriel Levkovich 		goto err_dealloc;
221124da0016SAriel Levkovich 
221224da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
221324da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
221424da0016SAriel Levkovich 			     &page_idx, sizeof(page_idx));
221524da0016SAriel Levkovich 	if (err)
221624da0016SAriel Levkovich 		goto err_dealloc;
221724da0016SAriel Levkovich 
221824da0016SAriel Levkovich 	bitmap_set(to_mucontext(context)->dm_pages, page_idx,
221924da0016SAriel Levkovich 		   DIV_ROUND_UP(act_size, PAGE_SIZE));
222024da0016SAriel Levkovich 
222124da0016SAriel Levkovich 	dm->dev_addr = memic_addr;
222224da0016SAriel Levkovich 
222324da0016SAriel Levkovich 	return &dm->ibdm;
222424da0016SAriel Levkovich 
222524da0016SAriel Levkovich err_dealloc:
222624da0016SAriel Levkovich 	mlx5_cmd_dealloc_memic(memic, memic_addr,
222724da0016SAriel Levkovich 			       act_size);
222824da0016SAriel Levkovich err_free:
222924da0016SAriel Levkovich 	kfree(dm);
223024da0016SAriel Levkovich 	return ERR_PTR(err);
223124da0016SAriel Levkovich }
223224da0016SAriel Levkovich 
223324da0016SAriel Levkovich int mlx5_ib_dealloc_dm(struct ib_dm *ibdm)
223424da0016SAriel Levkovich {
223524da0016SAriel Levkovich 	struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic;
223624da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
223724da0016SAriel Levkovich 	u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE);
223824da0016SAriel Levkovich 	u32 page_idx;
223924da0016SAriel Levkovich 	int ret;
224024da0016SAriel Levkovich 
224124da0016SAriel Levkovich 	ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size);
224224da0016SAriel Levkovich 	if (ret)
224324da0016SAriel Levkovich 		return ret;
224424da0016SAriel Levkovich 
224524da0016SAriel Levkovich 	page_idx = (dm->dev_addr - pci_resource_start(memic->dev->pdev, 0) -
224624da0016SAriel Levkovich 		    MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
224724da0016SAriel Levkovich 		    PAGE_SHIFT;
224824da0016SAriel Levkovich 	bitmap_clear(to_mucontext(ibdm->uobject->context)->dm_pages,
224924da0016SAriel Levkovich 		     page_idx,
225024da0016SAriel Levkovich 		     DIV_ROUND_UP(act_size, PAGE_SIZE));
225124da0016SAriel Levkovich 
225224da0016SAriel Levkovich 	kfree(dm);
225324da0016SAriel Levkovich 
225424da0016SAriel Levkovich 	return 0;
225524da0016SAriel Levkovich }
225624da0016SAriel Levkovich 
2257e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
2258e126ba97SEli Cohen 				      struct ib_ucontext *context,
2259e126ba97SEli Cohen 				      struct ib_udata *udata)
2260e126ba97SEli Cohen {
2261e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2262e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
2263e126ba97SEli Cohen 	int err;
2264a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2265a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)]   = {};
2266a1069c1cSYishai Hadas 	u16 uid = 0;
2267e126ba97SEli Cohen 
2268e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
2269e126ba97SEli Cohen 	if (!pd)
2270e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
2271e126ba97SEli Cohen 
227258895f0dSYishai Hadas 	uid = context ? to_mucontext(context)->devx_uid : 0;
2273a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2274a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
2275a1069c1cSYishai Hadas 	err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2276a1069c1cSYishai Hadas 			    out, sizeof(out));
2277e126ba97SEli Cohen 	if (err) {
2278e126ba97SEli Cohen 		kfree(pd);
2279e126ba97SEli Cohen 		return ERR_PTR(err);
2280e126ba97SEli Cohen 	}
2281e126ba97SEli Cohen 
2282a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2283a1069c1cSYishai Hadas 	pd->uid = uid;
2284e126ba97SEli Cohen 	if (context) {
2285e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2286e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2287a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
2288e126ba97SEli Cohen 			kfree(pd);
2289e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
2290e126ba97SEli Cohen 		}
2291e126ba97SEli Cohen 	}
2292e126ba97SEli Cohen 
2293e126ba97SEli Cohen 	return &pd->ibpd;
2294e126ba97SEli Cohen }
2295e126ba97SEli Cohen 
2296e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
2297e126ba97SEli Cohen {
2298e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2299e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2300e126ba97SEli Cohen 
2301a1069c1cSYishai Hadas 	mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2302e126ba97SEli Cohen 	kfree(mpd);
2303e126ba97SEli Cohen 
2304e126ba97SEli Cohen 	return 0;
2305e126ba97SEli Cohen }
2306e126ba97SEli Cohen 
2307466fa6d2SMaor Gottlieb enum {
2308466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2309466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
231071c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_INNER_BIT,
231171c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_MISC2_BIT
2312466fa6d2SMaor Gottlieb };
2313038d2ef8SMaor Gottlieb 
2314466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2315466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2316466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2317466fa6d2SMaor Gottlieb 
2318466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2319466fa6d2SMaor Gottlieb {
2320466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2321466fa6d2SMaor Gottlieb 
2322466fa6d2SMaor Gottlieb 	match_criteria_enable =
2323466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2324466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2325466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2326466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2327466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2328466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2329466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2330466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
233171c6e863SAriel Levkovich 	match_criteria_enable |=
233271c6e863SAriel Levkovich 		(!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
233371c6e863SAriel Levkovich 		MATCH_CRITERIA_ENABLE_MISC2_BIT;
2334466fa6d2SMaor Gottlieb 
2335466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2336038d2ef8SMaor Gottlieb }
2337038d2ef8SMaor Gottlieb 
2338ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2339ca0d4753SMaor Gottlieb {
2340ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2341ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2342ca0d4753SMaor Gottlieb }
2343ca0d4753SMaor Gottlieb 
234437da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
23452d1e697eSMoses Reuben 			   bool inner)
23462d1e697eSMoses Reuben {
23472d1e697eSMoses Reuben 	if (inner) {
23482d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
23492d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
23502d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
23512d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
23522d1e697eSMoses Reuben 	} else {
23532d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
23542d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
23552d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
23562d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
23572d1e697eSMoses Reuben 	}
23582d1e697eSMoses Reuben }
23592d1e697eSMoses Reuben 
2360ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2361ca0d4753SMaor Gottlieb {
2362ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2363ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2364ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2365ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2366ca0d4753SMaor Gottlieb }
2367ca0d4753SMaor Gottlieb 
236871c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
236971c6e863SAriel Levkovich {
237071c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
237171c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
237271c6e863SAriel Levkovich 		return -EOPNOTSUPP;
237371c6e863SAriel Levkovich 
237471c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
237571c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
237671c6e863SAriel Levkovich 		return -EOPNOTSUPP;
237771c6e863SAriel Levkovich 
237871c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
237971c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
238071c6e863SAriel Levkovich 		return -EOPNOTSUPP;
238171c6e863SAriel Levkovich 
238271c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
238371c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
238471c6e863SAriel Levkovich 		return -EOPNOTSUPP;
238571c6e863SAriel Levkovich 
238671c6e863SAriel Levkovich 	return 0;
238771c6e863SAriel Levkovich }
238871c6e863SAriel Levkovich 
2389c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2390c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2391ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2392466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2393c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2394ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
23952ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2396a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
23973b3233fbSRaed Salem #define LAST_COUNTERS_FIELD counters
2398c47ac6aeSMaor Gottlieb 
2399c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2400c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2401c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2402c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2403c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2404c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2405c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2406c47ac6aeSMaor Gottlieb 
24072ea26203SMark Bloch int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
24082ea26203SMark Bloch 			   bool is_egress,
2409802c2125SAviad Yehezkel 			   struct mlx5_flow_act *action)
2410802c2125SAviad Yehezkel {
2411802c2125SAviad Yehezkel 
2412802c2125SAviad Yehezkel 	switch (maction->ib_action.type) {
2413802c2125SAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
2414501f14e3SMark Bloch 		if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2415501f14e3SMark Bloch 				      MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2416501f14e3SMark Bloch 			return -EINVAL;
2417802c2125SAviad Yehezkel 		/* Currently only AES_GCM keymat is supported by the driver */
2418802c2125SAviad Yehezkel 		action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
24192ea26203SMark Bloch 		action->action |= is_egress ?
2420802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2421802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2422802c2125SAviad Yehezkel 		return 0;
2423b1085be3SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
2424b1085be3SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2425b1085be3SMark Bloch 		    MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
2426501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2427501f14e3SMark Bloch 				return -EINVAL;
2428b1085be3SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2429b1085be3SMark Bloch 			action->modify_id = maction->flow_action_raw.action_id;
2430b1085be3SMark Bloch 			return 0;
2431b1085be3SMark Bloch 		}
243210a30896SMark Bloch 		if (maction->flow_action_raw.sub_type ==
243310a30896SMark Bloch 		    MLX5_IB_FLOW_ACTION_DECAP) {
2434501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2435501f14e3SMark Bloch 				return -EINVAL;
243610a30896SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
243710a30896SMark Bloch 			return 0;
243810a30896SMark Bloch 		}
2439e806f932SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2440e806f932SMark Bloch 		    MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
2441501f14e3SMark Bloch 			if (action->action &
2442501f14e3SMark Bloch 			    MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2443501f14e3SMark Bloch 				return -EINVAL;
2444e806f932SMark Bloch 			action->action |=
2445e806f932SMark Bloch 				MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2446e806f932SMark Bloch 			action->reformat_id =
2447e806f932SMark Bloch 				maction->flow_action_raw.action_id;
2448e806f932SMark Bloch 			return 0;
2449e806f932SMark Bloch 		}
2450b1085be3SMark Bloch 		/* fall through */
2451802c2125SAviad Yehezkel 	default:
2452802c2125SAviad Yehezkel 		return -EOPNOTSUPP;
2453802c2125SAviad Yehezkel 	}
2454802c2125SAviad Yehezkel }
2455802c2125SAviad Yehezkel 
245619cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
245719cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
2458802c2125SAviad Yehezkel 			   const struct ib_flow_attr *flow_attr,
245971c6e863SAriel Levkovich 			   struct mlx5_flow_act *action, u32 prev_type)
2460038d2ef8SMaor Gottlieb {
2461466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2462466fa6d2SMaor Gottlieb 					   misc_parameters);
2463466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2464466fa6d2SMaor Gottlieb 					   misc_parameters);
246571c6e863SAriel Levkovich 	void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
246671c6e863SAriel Levkovich 					    misc_parameters_2);
246771c6e863SAriel Levkovich 	void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
246871c6e863SAriel Levkovich 					    misc_parameters_2);
24692d1e697eSMoses Reuben 	void *headers_c;
24702d1e697eSMoses Reuben 	void *headers_v;
247119cc7524SAriel Levkovich 	int match_ipv;
2472802c2125SAviad Yehezkel 	int ret;
2473466fa6d2SMaor Gottlieb 
24742d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
24752d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
24762d1e697eSMoses Reuben 					 inner_headers);
24772d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
24782d1e697eSMoses Reuben 					 inner_headers);
247919cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
248019cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
24812d1e697eSMoses Reuben 	} else {
24822d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
24832d1e697eSMoses Reuben 					 outer_headers);
24842d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
24852d1e697eSMoses Reuben 					 outer_headers);
248619cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
248719cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
24882d1e697eSMoses Reuben 	}
24892d1e697eSMoses Reuben 
24902d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2491038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2492c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
24931ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2494038d2ef8SMaor Gottlieb 
24952d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2496038d2ef8SMaor Gottlieb 					     dmac_47_16),
2497038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
24982d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2499038d2ef8SMaor Gottlieb 					     dmac_47_16),
2500038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2501038d2ef8SMaor Gottlieb 
25022d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2503ee3da804SMaor Gottlieb 					     smac_47_16),
2504ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
25052d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2506ee3da804SMaor Gottlieb 					     smac_47_16),
2507ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2508ee3da804SMaor Gottlieb 
2509038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
25102d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
251110543365SMohamad Haj Yahia 				 cvlan_tag, 1);
25122d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
251310543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2514038d2ef8SMaor Gottlieb 
25152d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2516038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
25172d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2518038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2519038d2ef8SMaor Gottlieb 
25202d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2521038d2ef8SMaor Gottlieb 				 first_cfi,
2522038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
25232d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2524038d2ef8SMaor Gottlieb 				 first_cfi,
2525038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2526038d2ef8SMaor Gottlieb 
25272d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2528038d2ef8SMaor Gottlieb 				 first_prio,
2529038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
25302d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2531038d2ef8SMaor Gottlieb 				 first_prio,
2532038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2533038d2ef8SMaor Gottlieb 		}
25342d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2535038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
25362d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2537038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2538038d2ef8SMaor Gottlieb 		break;
2539038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2540c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
25411ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2542038d2ef8SMaor Gottlieb 
254319cc7524SAriel Levkovich 		if (match_ipv) {
254419cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
254519cc7524SAriel Levkovich 				 ip_version, 0xf);
254619cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
25473346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV4_VERSION);
254819cc7524SAriel Levkovich 		} else {
25492d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2550038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
25512d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2552038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
255319cc7524SAriel Levkovich 		}
2554038d2ef8SMaor Gottlieb 
25552d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2556038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2557038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2558038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
25592d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2560038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2561038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2562038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
25632d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2564038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2565038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2566038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
25672d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2568038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2569038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2570038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2571ca0d4753SMaor Gottlieb 
25722d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2573ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2574ca0d4753SMaor Gottlieb 
25752d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2576ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
2577038d2ef8SMaor Gottlieb 		break;
2578026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2579c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
25801ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2581026bae0cSMaor Gottlieb 
258219cc7524SAriel Levkovich 		if (match_ipv) {
258319cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
258419cc7524SAriel Levkovich 				 ip_version, 0xf);
258519cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
25863346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV6_VERSION);
258719cc7524SAriel Levkovich 		} else {
25882d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2589026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
25902d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2591026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
259219cc7524SAriel Levkovich 		}
2593026bae0cSMaor Gottlieb 
25942d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2595026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2596026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2597026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
25982d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2599026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2600026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2601026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
26022d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2603026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2604026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2605026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
26062d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2607026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2608026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2609026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2610466fa6d2SMaor Gottlieb 
26112d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2612466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2613466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2614466fa6d2SMaor Gottlieb 
26152d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2616466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
2617466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
2618466fa6d2SMaor Gottlieb 
26192d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
26202d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
26212d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
26222d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
2623802c2125SAviad Yehezkel 		break;
2624802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ESP:
2625802c2125SAviad Yehezkel 		if (ib_spec->esp.mask.seq)
2626802c2125SAviad Yehezkel 			return -EOPNOTSUPP;
26272d1e697eSMoses Reuben 
2628802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2629802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.mask.spi));
2630802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2631802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.val.spi));
2632026bae0cSMaor Gottlieb 		break;
2633038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2634c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2635c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
26361ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2637038d2ef8SMaor Gottlieb 
26382d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2639038d2ef8SMaor Gottlieb 			 0xff);
26402d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2641038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
2642038d2ef8SMaor Gottlieb 
26432d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2644038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
26452d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2646038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2647038d2ef8SMaor Gottlieb 
26482d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2649038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
26502d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2651038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2652038d2ef8SMaor Gottlieb 		break;
2653038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2654c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2655c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
26561ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2657038d2ef8SMaor Gottlieb 
26582d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2659038d2ef8SMaor Gottlieb 			 0xff);
26602d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2661038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
2662038d2ef8SMaor Gottlieb 
26632d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2664038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
26652d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2666038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2667038d2ef8SMaor Gottlieb 
26682d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2669038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
26702d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2671038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2672038d2ef8SMaor Gottlieb 		break;
2673da2f22aeSAriel Levkovich 	case IB_FLOW_SPEC_GRE:
2674da2f22aeSAriel Levkovich 		if (ib_spec->gre.mask.c_ks_res0_ver)
2675da2f22aeSAriel Levkovich 			return -EOPNOTSUPP;
2676da2f22aeSAriel Levkovich 
2677da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2678da2f22aeSAriel Levkovich 			 0xff);
2679da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2680da2f22aeSAriel Levkovich 			 IPPROTO_GRE);
2681da2f22aeSAriel Levkovich 
2682da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
2683a93b632cSMaor Gottlieb 			 ntohs(ib_spec->gre.mask.protocol));
2684da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2685da2f22aeSAriel Levkovich 			 ntohs(ib_spec->gre.val.protocol));
2686da2f22aeSAriel Levkovich 
2687da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
26885886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2689da2f22aeSAriel Levkovich 		       &ib_spec->gre.mask.key,
2690da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.mask.key));
2691da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
26925886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2693da2f22aeSAriel Levkovich 		       &ib_spec->gre.val.key,
2694da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.val.key));
2695da2f22aeSAriel Levkovich 		break;
269671c6e863SAriel Levkovich 	case IB_FLOW_SPEC_MPLS:
269771c6e863SAriel Levkovich 		switch (prev_type) {
269871c6e863SAriel Levkovich 		case IB_FLOW_SPEC_UDP:
269971c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
270071c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_udp),
270171c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
270271c6e863SAriel Levkovich 				return -EOPNOTSUPP;
270371c6e863SAriel Levkovich 
270471c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
270571c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
270671c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
270771c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
270871c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
270971c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
271071c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
271171c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
271271c6e863SAriel Levkovich 			break;
271371c6e863SAriel Levkovich 		case IB_FLOW_SPEC_GRE:
271471c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
271571c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_gre),
271671c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
271771c6e863SAriel Levkovich 				return -EOPNOTSUPP;
271871c6e863SAriel Levkovich 
271971c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
272071c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
272171c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
272271c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
272371c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
272471c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
272571c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
272671c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
272771c6e863SAriel Levkovich 			break;
272871c6e863SAriel Levkovich 		default:
272971c6e863SAriel Levkovich 			if (ib_spec->type & IB_FLOW_SPEC_INNER) {
273071c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
273171c6e863SAriel Levkovich 							   ft_field_support.inner_first_mpls),
273271c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
273371c6e863SAriel Levkovich 					return -EOPNOTSUPP;
273471c6e863SAriel Levkovich 
273571c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
273671c6e863SAriel Levkovich 						    inner_first_mpls),
273771c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
273871c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
273971c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
274071c6e863SAriel Levkovich 						    inner_first_mpls),
274171c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
274271c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
274371c6e863SAriel Levkovich 			} else {
274471c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
274571c6e863SAriel Levkovich 							   ft_field_support.outer_first_mpls),
274671c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
274771c6e863SAriel Levkovich 					return -EOPNOTSUPP;
274871c6e863SAriel Levkovich 
274971c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
275071c6e863SAriel Levkovich 						    outer_first_mpls),
275171c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
275271c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
275371c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
275471c6e863SAriel Levkovich 						    outer_first_mpls),
275571c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
275671c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
275771c6e863SAriel Levkovich 			}
275871c6e863SAriel Levkovich 		}
275971c6e863SAriel Levkovich 		break;
2760ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2761ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2762ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
27631ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2764ffb30d8fSMoses Reuben 
2765ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2766ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2767ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2768ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2769ffb30d8fSMoses Reuben 		break;
27702ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
27712ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
27722ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
27732ac693f9SMoses Reuben 			return -EOPNOTSUPP;
27742ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
27752ac693f9SMoses Reuben 			return -EINVAL;
27762ac693f9SMoses Reuben 
2777075572d4SBoris Pismenny 		action->flow_tag = ib_spec->flow_tag.tag_id;
2778d5634feeSPaul Blakey 		action->flags |= FLOW_ACT_HAS_TAG;
27792ac693f9SMoses Reuben 		break;
2780a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2781a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2782a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2783a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2784075572d4SBoris Pismenny 		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
2785a22ed86cSSlava Shwartsman 		break;
2786802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ACTION_HANDLE:
27872ea26203SMark Bloch 		ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
27882ea26203SMark Bloch 			flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
2789802c2125SAviad Yehezkel 		if (ret)
2790802c2125SAviad Yehezkel 			return ret;
2791802c2125SAviad Yehezkel 		break;
27923b3233fbSRaed Salem 	case IB_FLOW_SPEC_ACTION_COUNT:
27933b3233fbSRaed Salem 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
27943b3233fbSRaed Salem 					 LAST_COUNTERS_FIELD))
27953b3233fbSRaed Salem 			return -EOPNOTSUPP;
27963b3233fbSRaed Salem 
27973b3233fbSRaed Salem 		/* for now support only one counters spec per flow */
27983b3233fbSRaed Salem 		if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
27993b3233fbSRaed Salem 			return -EINVAL;
28003b3233fbSRaed Salem 
28013b3233fbSRaed Salem 		action->counters = ib_spec->flow_count.counters;
28023b3233fbSRaed Salem 		action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
28033b3233fbSRaed Salem 		break;
2804038d2ef8SMaor Gottlieb 	default:
2805038d2ef8SMaor Gottlieb 		return -EINVAL;
2806038d2ef8SMaor Gottlieb 	}
2807038d2ef8SMaor Gottlieb 
2808038d2ef8SMaor Gottlieb 	return 0;
2809038d2ef8SMaor Gottlieb }
2810038d2ef8SMaor Gottlieb 
2811038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
2812038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
2813038d2ef8SMaor Gottlieb  * could steal other multicast packets.
2814038d2ef8SMaor Gottlieb  */
2815a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2816038d2ef8SMaor Gottlieb {
281781e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
2818038d2ef8SMaor Gottlieb 
2819038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2820038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
2821038d2ef8SMaor Gottlieb 		return false;
2822038d2ef8SMaor Gottlieb 
282381e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
282481e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
282581e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
2826038d2ef8SMaor Gottlieb 
282781e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
282881e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
282981e30880SYishai Hadas 			return true;
283081e30880SYishai Hadas 
283181e30880SYishai Hadas 		return false;
283281e30880SYishai Hadas 	}
283381e30880SYishai Hadas 
283481e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
283581e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
283681e30880SYishai Hadas 
283781e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2838038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2839038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
2840038d2ef8SMaor Gottlieb 	}
2841038d2ef8SMaor Gottlieb 
284281e30880SYishai Hadas 	return false;
284381e30880SYishai Hadas }
284481e30880SYishai Hadas 
2845802c2125SAviad Yehezkel enum valid_spec {
2846802c2125SAviad Yehezkel 	VALID_SPEC_INVALID,
2847802c2125SAviad Yehezkel 	VALID_SPEC_VALID,
2848802c2125SAviad Yehezkel 	VALID_SPEC_NA,
2849802c2125SAviad Yehezkel };
2850802c2125SAviad Yehezkel 
2851802c2125SAviad Yehezkel static enum valid_spec
2852802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
2853802c2125SAviad Yehezkel 		     const struct mlx5_flow_spec *spec,
2854802c2125SAviad Yehezkel 		     const struct mlx5_flow_act *flow_act,
2855802c2125SAviad Yehezkel 		     bool egress)
2856802c2125SAviad Yehezkel {
2857802c2125SAviad Yehezkel 	const u32 *match_c = spec->match_criteria;
2858802c2125SAviad Yehezkel 	bool is_crypto =
2859802c2125SAviad Yehezkel 		(flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2860802c2125SAviad Yehezkel 				     MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
2861802c2125SAviad Yehezkel 	bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
2862802c2125SAviad Yehezkel 	bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
2863802c2125SAviad Yehezkel 
2864802c2125SAviad Yehezkel 	/*
2865802c2125SAviad Yehezkel 	 * Currently only crypto is supported in egress, when regular egress
2866802c2125SAviad Yehezkel 	 * rules would be supported, always return VALID_SPEC_NA.
2867802c2125SAviad Yehezkel 	 */
2868802c2125SAviad Yehezkel 	if (!is_crypto)
286978dd0c43SMark Bloch 		return VALID_SPEC_NA;
2870802c2125SAviad Yehezkel 
2871802c2125SAviad Yehezkel 	return is_crypto && is_ipsec &&
2872d5634feeSPaul Blakey 		(!egress || (!is_drop && !(flow_act->flags & FLOW_ACT_HAS_TAG))) ?
2873802c2125SAviad Yehezkel 		VALID_SPEC_VALID : VALID_SPEC_INVALID;
2874802c2125SAviad Yehezkel }
2875802c2125SAviad Yehezkel 
2876802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev,
2877802c2125SAviad Yehezkel 			  const struct mlx5_flow_spec *spec,
2878802c2125SAviad Yehezkel 			  const struct mlx5_flow_act *flow_act,
2879802c2125SAviad Yehezkel 			  bool egress)
2880802c2125SAviad Yehezkel {
2881802c2125SAviad Yehezkel 	/* We curretly only support ipsec egress flow */
2882802c2125SAviad Yehezkel 	return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
2883802c2125SAviad Yehezkel }
2884802c2125SAviad Yehezkel 
288519cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
288619cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
28870f750966SAriel Levkovich 			       bool check_inner)
2888038d2ef8SMaor Gottlieb {
2889038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
289019cc7524SAriel Levkovich 	int match_ipv = check_inner ?
289119cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
289219cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
289319cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
289419cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
28950f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
28960f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
28970f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
28980f750966SAriel Levkovich 	bool has_ethertype = false;
2899038d2ef8SMaor Gottlieb 	unsigned int spec_index;
29000f750966SAriel Levkovich 	bool mask_valid = true;
29010f750966SAriel Levkovich 	u16 eth_type = 0;
29020f750966SAriel Levkovich 	bool type_valid;
2903038d2ef8SMaor Gottlieb 
2904038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
2905038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
29060f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2907038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
29080f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
29090f750966SAriel Levkovich 				      htons(0xffff));
29100f750966SAriel Levkovich 			has_ethertype = true;
29110f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
29120f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
29130f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
29140f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
2915038d2ef8SMaor Gottlieb 		}
2916038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
2917038d2ef8SMaor Gottlieb 	}
29180f750966SAriel Levkovich 
29190f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
29200f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
29210f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
29220f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
29230f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
29240f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
292519cc7524SAriel Levkovich 
292619cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
292719cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
292819cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
29290f750966SAriel Levkovich 	}
29300f750966SAriel Levkovich 
29310f750966SAriel Levkovich 	return type_valid;
29320f750966SAriel Levkovich }
29330f750966SAriel Levkovich 
293419cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
293519cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
29360f750966SAriel Levkovich {
293719cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
293819cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
2939038d2ef8SMaor Gottlieb }
2940038d2ef8SMaor Gottlieb 
2941038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
2942038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
2943038d2ef8SMaor Gottlieb {
2944038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
2945038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
2946038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
2947038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
2948038d2ef8SMaor Gottlieb 	}
2949038d2ef8SMaor Gottlieb }
2950038d2ef8SMaor Gottlieb 
29513b3233fbSRaed Salem static void counters_clear_description(struct ib_counters *counters)
29523b3233fbSRaed Salem {
29533b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
29543b3233fbSRaed Salem 
29553b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
29563b3233fbSRaed Salem 	kfree(mcounters->counters_data);
29573b3233fbSRaed Salem 	mcounters->counters_data = NULL;
29583b3233fbSRaed Salem 	mcounters->cntrs_max_index = 0;
29593b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
29603b3233fbSRaed Salem }
29613b3233fbSRaed Salem 
2962038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2963038d2ef8SMaor Gottlieb {
2964038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2965038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2966038d2ef8SMaor Gottlieb 							  ibflow);
2967038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2968d4be3f44SYishai Hadas 	struct mlx5_ib_dev *dev = handler->dev;
2969038d2ef8SMaor Gottlieb 
29709a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
2971038d2ef8SMaor Gottlieb 
2972038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
297374491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2974cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
2975038d2ef8SMaor Gottlieb 		list_del(&iter->list);
2976038d2ef8SMaor Gottlieb 		kfree(iter);
2977038d2ef8SMaor Gottlieb 	}
2978038d2ef8SMaor Gottlieb 
297974491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
29805497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
29813b3233fbSRaed Salem 	if (handler->ibcounters &&
29823b3233fbSRaed Salem 	    atomic_read(&handler->ibcounters->usecnt) == 1)
29833b3233fbSRaed Salem 		counters_clear_description(handler->ibcounters);
2984038d2ef8SMaor Gottlieb 
29853b3233fbSRaed Salem 	mutex_unlock(&dev->flow_db->lock);
2986d4be3f44SYishai Hadas 	if (handler->flow_matcher)
2987d4be3f44SYishai Hadas 		atomic_dec(&handler->flow_matcher->usecnt);
2988038d2ef8SMaor Gottlieb 	kfree(handler);
2989038d2ef8SMaor Gottlieb 
2990038d2ef8SMaor Gottlieb 	return 0;
2991038d2ef8SMaor Gottlieb }
2992038d2ef8SMaor Gottlieb 
299335d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
299435d19011SMaor Gottlieb {
299535d19011SMaor Gottlieb 	priority *= 2;
299635d19011SMaor Gottlieb 	if (!dont_trap)
299735d19011SMaor Gottlieb 		priority++;
299835d19011SMaor Gottlieb 	return priority;
299935d19011SMaor Gottlieb }
300035d19011SMaor Gottlieb 
3001cc0e5d42SMaor Gottlieb enum flow_table_type {
3002cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
3003cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
3004cc0e5d42SMaor Gottlieb };
3005cc0e5d42SMaor Gottlieb 
300600b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
300700b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
3008d4be3f44SYishai Hadas 
3009d4be3f44SYishai Hadas static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3010d4be3f44SYishai Hadas 					   struct mlx5_ib_flow_prio *prio,
3011d4be3f44SYishai Hadas 					   int priority,
30124adda112SMark Bloch 					   int num_entries, int num_groups,
30134adda112SMark Bloch 					   u32 flags)
3014d4be3f44SYishai Hadas {
3015d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft;
3016d4be3f44SYishai Hadas 
3017d4be3f44SYishai Hadas 	ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3018d4be3f44SYishai Hadas 						 num_entries,
3019d4be3f44SYishai Hadas 						 num_groups,
30204adda112SMark Bloch 						 0, flags);
3021d4be3f44SYishai Hadas 	if (IS_ERR(ft))
3022d4be3f44SYishai Hadas 		return ERR_CAST(ft);
3023d4be3f44SYishai Hadas 
3024d4be3f44SYishai Hadas 	prio->flow_table = ft;
3025d4be3f44SYishai Hadas 	prio->refcount = 0;
3026d4be3f44SYishai Hadas 	return prio;
3027d4be3f44SYishai Hadas }
3028d4be3f44SYishai Hadas 
3029038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
3030cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
3031cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
3032038d2ef8SMaor Gottlieb {
303335d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
3034038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
3035038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
3036038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
3037dac388efSMaor Gottlieb 	int max_table_size;
3038038d2ef8SMaor Gottlieb 	int num_entries;
3039038d2ef8SMaor Gottlieb 	int num_groups;
30404adda112SMark Bloch 	u32 flags = 0;
3041038d2ef8SMaor Gottlieb 	int priority;
3042038d2ef8SMaor Gottlieb 
3043dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3044dac388efSMaor Gottlieb 						       log_max_ft_size));
3045038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
304678dd0c43SMark Bloch 		enum mlx5_flow_namespace_type fn_type;
304778dd0c43SMark Bloch 
304878dd0c43SMark Bloch 		if (flow_is_multicast_only(flow_attr) &&
304935d19011SMaor Gottlieb 		    !dont_trap)
3050038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
3051038d2ef8SMaor Gottlieb 		else
305235d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
305335d19011SMaor Gottlieb 							dont_trap);
305478dd0c43SMark Bloch 		if (ft_type == MLX5_IB_FT_RX) {
305578dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
305678dd0c43SMark Bloch 			prio = &dev->flow_db->prios[priority];
30574adda112SMark Bloch 			if (!dev->rep &&
30584adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
30594adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
30605c2db53fSMark Bloch 			if (!dev->rep &&
30615c2db53fSMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
30625c2db53fSMark Bloch 					reformat_l3_tunnel_to_l2))
30635c2db53fSMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
306478dd0c43SMark Bloch 		} else {
306578dd0c43SMark Bloch 			max_table_size =
306678dd0c43SMark Bloch 				BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
306778dd0c43SMark Bloch 							      log_max_ft_size));
306878dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
306978dd0c43SMark Bloch 			prio = &dev->flow_db->egress_prios[priority];
30704adda112SMark Bloch 			if (!dev->rep &&
30714adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
30724adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
307378dd0c43SMark Bloch 		}
307478dd0c43SMark Bloch 		ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
3075038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
3076038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
3077038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3078038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3079038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
3080038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
3081038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
3082038d2ef8SMaor Gottlieb 					 &num_entries,
3083038d2ef8SMaor Gottlieb 					 &num_groups);
30849a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
3085cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3086cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3087cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
3088cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
3089cc0e5d42SMaor Gottlieb 
3090cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3091cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3092cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3093cc0e5d42SMaor Gottlieb 
30949a4ca38dSMark Bloch 		prio = &dev->flow_db->sniffer[ft_type];
3095cc0e5d42SMaor Gottlieb 		priority = 0;
3096cc0e5d42SMaor Gottlieb 		num_entries = 1;
3097cc0e5d42SMaor Gottlieb 		num_groups = 1;
3098038d2ef8SMaor Gottlieb 	}
3099038d2ef8SMaor Gottlieb 
3100038d2ef8SMaor Gottlieb 	if (!ns)
3101038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
3102038d2ef8SMaor Gottlieb 
3103dac388efSMaor Gottlieb 	if (num_entries > max_table_size)
3104dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
3105dac388efSMaor Gottlieb 
3106038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
3107d4be3f44SYishai Hadas 	if (!ft)
31084adda112SMark Bloch 		return _get_prio(ns, prio, priority, num_entries, num_groups,
31094adda112SMark Bloch 				 flags);
3110038d2ef8SMaor Gottlieb 
3111d4be3f44SYishai Hadas 	return prio;
3112038d2ef8SMaor Gottlieb }
3113038d2ef8SMaor Gottlieb 
3114a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
3115a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
3116a550ddfcSYishai Hadas 			    u32 underlay_qpn)
3117a550ddfcSYishai Hadas {
3118a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3119a550ddfcSYishai Hadas 					   spec->match_criteria,
3120a550ddfcSYishai Hadas 					   misc_parameters);
3121a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3122a550ddfcSYishai Hadas 					   misc_parameters);
3123a550ddfcSYishai Hadas 
3124a550ddfcSYishai Hadas 	if (underlay_qpn &&
3125a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3126a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
3127a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3128a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
3129a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3130a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
3131a550ddfcSYishai Hadas 	}
3132a550ddfcSYishai Hadas }
3133a550ddfcSYishai Hadas 
31345e95af5fSRaed Salem static int read_flow_counters(struct ib_device *ibdev,
31355e95af5fSRaed Salem 			      struct mlx5_read_counters_attr *read_attr)
31365e95af5fSRaed Salem {
31375e95af5fSRaed Salem 	struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
31385e95af5fSRaed Salem 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
31395e95af5fSRaed Salem 
31405e95af5fSRaed Salem 	return mlx5_fc_query(dev->mdev, fc,
31415e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_PACKETS],
31425e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_BYTES]);
31435e95af5fSRaed Salem }
31445e95af5fSRaed Salem 
31455e95af5fSRaed Salem /* flow counters currently expose two counters packets and bytes */
31465e95af5fSRaed Salem #define FLOW_COUNTERS_NUM 2
31473b3233fbSRaed Salem static int counters_set_description(struct ib_counters *counters,
31483b3233fbSRaed Salem 				    enum mlx5_ib_counters_type counters_type,
31493b3233fbSRaed Salem 				    struct mlx5_ib_flow_counters_desc *desc_data,
31503b3233fbSRaed Salem 				    u32 ncounters)
31513b3233fbSRaed Salem {
31523b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
31533b3233fbSRaed Salem 	u32 cntrs_max_index = 0;
31543b3233fbSRaed Salem 	int i;
31553b3233fbSRaed Salem 
31563b3233fbSRaed Salem 	if (counters_type != MLX5_IB_COUNTERS_FLOW)
31573b3233fbSRaed Salem 		return -EINVAL;
31583b3233fbSRaed Salem 
31593b3233fbSRaed Salem 	/* init the fields for the object */
31603b3233fbSRaed Salem 	mcounters->type = counters_type;
31615e95af5fSRaed Salem 	mcounters->read_counters = read_flow_counters;
31625e95af5fSRaed Salem 	mcounters->counters_num = FLOW_COUNTERS_NUM;
31633b3233fbSRaed Salem 	mcounters->ncounters = ncounters;
31643b3233fbSRaed Salem 	/* each counter entry have both description and index pair */
31653b3233fbSRaed Salem 	for (i = 0; i < ncounters; i++) {
31663b3233fbSRaed Salem 		if (desc_data[i].description > IB_COUNTER_BYTES)
31673b3233fbSRaed Salem 			return -EINVAL;
31683b3233fbSRaed Salem 
31693b3233fbSRaed Salem 		if (cntrs_max_index <= desc_data[i].index)
31703b3233fbSRaed Salem 			cntrs_max_index = desc_data[i].index + 1;
31713b3233fbSRaed Salem 	}
31723b3233fbSRaed Salem 
31733b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
31743b3233fbSRaed Salem 	mcounters->counters_data = desc_data;
31753b3233fbSRaed Salem 	mcounters->cntrs_max_index = cntrs_max_index;
31763b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
31773b3233fbSRaed Salem 
31783b3233fbSRaed Salem 	return 0;
31793b3233fbSRaed Salem }
31803b3233fbSRaed Salem 
31813b3233fbSRaed Salem #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
31823b3233fbSRaed Salem static int flow_counters_set_data(struct ib_counters *ibcounters,
31833b3233fbSRaed Salem 				  struct mlx5_ib_create_flow *ucmd)
31843b3233fbSRaed Salem {
31853b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
31863b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
31873b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc_data = NULL;
31883b3233fbSRaed Salem 	bool hw_hndl = false;
31893b3233fbSRaed Salem 	int ret = 0;
31903b3233fbSRaed Salem 
31913b3233fbSRaed Salem 	if (ucmd && ucmd->ncounters_data != 0) {
31923b3233fbSRaed Salem 		cntrs_data = ucmd->data;
31933b3233fbSRaed Salem 		if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
31943b3233fbSRaed Salem 			return -EINVAL;
31953b3233fbSRaed Salem 
31963b3233fbSRaed Salem 		desc_data = kcalloc(cntrs_data->ncounters,
31973b3233fbSRaed Salem 				    sizeof(*desc_data),
31983b3233fbSRaed Salem 				    GFP_KERNEL);
31993b3233fbSRaed Salem 		if (!desc_data)
32003b3233fbSRaed Salem 			return  -ENOMEM;
32013b3233fbSRaed Salem 
32023b3233fbSRaed Salem 		if (copy_from_user(desc_data,
32033b3233fbSRaed Salem 				   u64_to_user_ptr(cntrs_data->counters_data),
32043b3233fbSRaed Salem 				   sizeof(*desc_data) * cntrs_data->ncounters)) {
32053b3233fbSRaed Salem 			ret = -EFAULT;
32063b3233fbSRaed Salem 			goto free;
32073b3233fbSRaed Salem 		}
32083b3233fbSRaed Salem 	}
32093b3233fbSRaed Salem 
32103b3233fbSRaed Salem 	if (!mcounters->hw_cntrs_hndl) {
32113b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = mlx5_fc_create(
32123b3233fbSRaed Salem 			to_mdev(ibcounters->device)->mdev, false);
3213e31abf76Sweiyongjun (A) 		if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3214e31abf76Sweiyongjun (A) 			ret = PTR_ERR(mcounters->hw_cntrs_hndl);
32153b3233fbSRaed Salem 			goto free;
32163b3233fbSRaed Salem 		}
32173b3233fbSRaed Salem 		hw_hndl = true;
32183b3233fbSRaed Salem 	}
32193b3233fbSRaed Salem 
32203b3233fbSRaed Salem 	if (desc_data) {
32213b3233fbSRaed Salem 		/* counters already bound to at least one flow */
32223b3233fbSRaed Salem 		if (mcounters->cntrs_max_index) {
32233b3233fbSRaed Salem 			ret = -EINVAL;
32243b3233fbSRaed Salem 			goto free_hndl;
32253b3233fbSRaed Salem 		}
32263b3233fbSRaed Salem 
32273b3233fbSRaed Salem 		ret = counters_set_description(ibcounters,
32283b3233fbSRaed Salem 					       MLX5_IB_COUNTERS_FLOW,
32293b3233fbSRaed Salem 					       desc_data,
32303b3233fbSRaed Salem 					       cntrs_data->ncounters);
32313b3233fbSRaed Salem 		if (ret)
32323b3233fbSRaed Salem 			goto free_hndl;
32333b3233fbSRaed Salem 
32343b3233fbSRaed Salem 	} else if (!mcounters->cntrs_max_index) {
32353b3233fbSRaed Salem 		/* counters not bound yet, must have udata passed */
32363b3233fbSRaed Salem 		ret = -EINVAL;
32373b3233fbSRaed Salem 		goto free_hndl;
32383b3233fbSRaed Salem 	}
32393b3233fbSRaed Salem 
32403b3233fbSRaed Salem 	return 0;
32413b3233fbSRaed Salem 
32423b3233fbSRaed Salem free_hndl:
32433b3233fbSRaed Salem 	if (hw_hndl) {
32443b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
32453b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
32463b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = NULL;
32473b3233fbSRaed Salem 	}
32483b3233fbSRaed Salem free:
32493b3233fbSRaed Salem 	kfree(desc_data);
32503b3233fbSRaed Salem 	return ret;
32513b3233fbSRaed Salem }
32523b3233fbSRaed Salem 
3253a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3254038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
3255dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
3256a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
32573b3233fbSRaed Salem 						      u32 underlay_qpn,
32583b3233fbSRaed Salem 						      struct mlx5_ib_create_flow *ucmd)
3259038d2ef8SMaor Gottlieb {
3260038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
3261038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
3262075572d4SBoris Pismenny 	struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
3263c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
32643b3233fbSRaed Salem 	struct mlx5_flow_destination dest_arr[2] = {};
32653b3233fbSRaed Salem 	struct mlx5_flow_destination *rule_dst = dest_arr;
3266dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3267038d2ef8SMaor Gottlieb 	unsigned int spec_index;
326871c6e863SAriel Levkovich 	u32 prev_type = 0;
3269038d2ef8SMaor Gottlieb 	int err = 0;
32703b3233fbSRaed Salem 	int dest_num = 0;
3271802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3272038d2ef8SMaor Gottlieb 
327319cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
3274038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3275038d2ef8SMaor Gottlieb 
327678dd0c43SMark Bloch 	if (dev->rep && is_egress)
327778dd0c43SMark Bloch 		return ERR_PTR(-EINVAL);
327878dd0c43SMark Bloch 
32791b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3280038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3281c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
3282038d2ef8SMaor Gottlieb 		err = -ENOMEM;
3283038d2ef8SMaor Gottlieb 		goto free;
3284038d2ef8SMaor Gottlieb 	}
3285038d2ef8SMaor Gottlieb 
3286038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
32873b3233fbSRaed Salem 	if (dst) {
32883b3233fbSRaed Salem 		memcpy(&dest_arr[0], dst, sizeof(*dst));
32893b3233fbSRaed Salem 		dest_num++;
32903b3233fbSRaed Salem 	}
3291038d2ef8SMaor Gottlieb 
3292038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
329319cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
3294a22ed86cSSlava Shwartsman 				      spec->match_value,
329571c6e863SAriel Levkovich 				      ib_flow, flow_attr, &flow_act,
329671c6e863SAriel Levkovich 				      prev_type);
3297038d2ef8SMaor Gottlieb 		if (err < 0)
3298038d2ef8SMaor Gottlieb 			goto free;
3299038d2ef8SMaor Gottlieb 
330071c6e863SAriel Levkovich 		prev_type = ((union ib_flow_spec *)ib_flow)->type;
3301038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3302038d2ef8SMaor Gottlieb 	}
3303038d2ef8SMaor Gottlieb 
3304a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
3305a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
3306a550ddfcSYishai Hadas 
3307018a94eeSMark Bloch 	if (dev->rep) {
3308018a94eeSMark Bloch 		void *misc;
3309018a94eeSMark Bloch 
3310018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3311018a94eeSMark Bloch 				    misc_parameters);
3312018a94eeSMark Bloch 		MLX5_SET(fte_match_set_misc, misc, source_port,
3313018a94eeSMark Bloch 			 dev->rep->vport);
3314018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3315018a94eeSMark Bloch 				    misc_parameters);
3316018a94eeSMark Bloch 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3317018a94eeSMark Bloch 	}
3318018a94eeSMark Bloch 
3319466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3320802c2125SAviad Yehezkel 
3321802c2125SAviad Yehezkel 	if (is_egress &&
3322802c2125SAviad Yehezkel 	    !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3323802c2125SAviad Yehezkel 		err = -EINVAL;
3324802c2125SAviad Yehezkel 		goto free;
3325802c2125SAviad Yehezkel 	}
3326802c2125SAviad Yehezkel 
33273b3233fbSRaed Salem 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3328171c7625SMark Bloch 		struct mlx5_ib_mcounters *mcounters;
3329171c7625SMark Bloch 
33303b3233fbSRaed Salem 		err = flow_counters_set_data(flow_act.counters, ucmd);
33313b3233fbSRaed Salem 		if (err)
33323b3233fbSRaed Salem 			goto free;
33333b3233fbSRaed Salem 
3334171c7625SMark Bloch 		mcounters = to_mcounters(flow_act.counters);
33353b3233fbSRaed Salem 		handler->ibcounters = flow_act.counters;
33363b3233fbSRaed Salem 		dest_arr[dest_num].type =
33373b3233fbSRaed Salem 			MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3338171c7625SMark Bloch 		dest_arr[dest_num].counter_id =
3339171c7625SMark Bloch 			mlx5_fc_id(mcounters->hw_cntrs_hndl);
33403b3233fbSRaed Salem 		dest_num++;
33413b3233fbSRaed Salem 	}
33423b3233fbSRaed Salem 
3343075572d4SBoris Pismenny 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
33443b3233fbSRaed Salem 		if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3345a22ed86cSSlava Shwartsman 			rule_dst = NULL;
3346a22ed86cSSlava Shwartsman 			dest_num = 0;
33473b3233fbSRaed Salem 		}
3348a22ed86cSSlava Shwartsman 	} else {
3349802c2125SAviad Yehezkel 		if (is_egress)
3350802c2125SAviad Yehezkel 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3351802c2125SAviad Yehezkel 		else
3352802c2125SAviad Yehezkel 			flow_act.action |=
33533b3233fbSRaed Salem 				dest_num ?  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
335435d19011SMaor Gottlieb 					MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3355a22ed86cSSlava Shwartsman 	}
33562ac693f9SMoses Reuben 
3357d5634feeSPaul Blakey 	if ((flow_act.flags & FLOW_ACT_HAS_TAG)  &&
33582ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
33592ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
33602ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3361075572d4SBoris Pismenny 			     flow_act.flow_tag, flow_attr->type);
33622ac693f9SMoses Reuben 		err = -EINVAL;
33632ac693f9SMoses Reuben 		goto free;
33642ac693f9SMoses Reuben 	}
336574491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
336666958ed9SHadar Hen Zion 					    &flow_act,
3367a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
3368038d2ef8SMaor Gottlieb 
3369038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
3370038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
3371038d2ef8SMaor Gottlieb 		goto free;
3372038d2ef8SMaor Gottlieb 	}
3373038d2ef8SMaor Gottlieb 
3374d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
33755497adc6SMaor Gottlieb 	handler->prio = ft_prio;
3376d4be3f44SYishai Hadas 	handler->dev = dev;
3377038d2ef8SMaor Gottlieb 
3378038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
3379038d2ef8SMaor Gottlieb free:
33803b3233fbSRaed Salem 	if (err && handler) {
33813b3233fbSRaed Salem 		if (handler->ibcounters &&
33823b3233fbSRaed Salem 		    atomic_read(&handler->ibcounters->usecnt) == 1)
33833b3233fbSRaed Salem 			counters_clear_description(handler->ibcounters);
3384038d2ef8SMaor Gottlieb 		kfree(handler);
33853b3233fbSRaed Salem 	}
3386c5bb1730SMaor Gottlieb 	kvfree(spec);
3387038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
3388038d2ef8SMaor Gottlieb }
3389038d2ef8SMaor Gottlieb 
3390a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3391a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
3392a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
3393a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
3394a550ddfcSYishai Hadas {
33953b3233fbSRaed Salem 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
3396a550ddfcSYishai Hadas }
3397a550ddfcSYishai Hadas 
339835d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
339935d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
340035d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
340135d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
340235d19011SMaor Gottlieb {
340335d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
340435d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
340535d19011SMaor Gottlieb 
340635d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
340735d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
340835d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
340935d19011SMaor Gottlieb 					       flow_attr, dst);
341035d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
341174491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3412d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
341335d19011SMaor Gottlieb 			kfree(handler);
341435d19011SMaor Gottlieb 			handler = handler_dst;
341535d19011SMaor Gottlieb 		} else {
341635d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
341735d19011SMaor Gottlieb 		}
341835d19011SMaor Gottlieb 	}
341935d19011SMaor Gottlieb 
342035d19011SMaor Gottlieb 	return handler;
342135d19011SMaor Gottlieb }
3422038d2ef8SMaor Gottlieb enum {
3423038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
3424038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
3425038d2ef8SMaor Gottlieb };
3426038d2ef8SMaor Gottlieb 
3427038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3428038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
3429038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
3430038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
3431038d2ef8SMaor Gottlieb {
3432038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
3433038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3434038d2ef8SMaor Gottlieb 
3435038d2ef8SMaor Gottlieb 	static struct {
3436038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
3437038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
3438038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
3439038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
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 = {0x1} }
3449038d2ef8SMaor Gottlieb 			}
3450038d2ef8SMaor Gottlieb 		},
3451038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
3452038d2ef8SMaor Gottlieb 			.flow_attr = {
3453038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3454038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3455038d2ef8SMaor Gottlieb 			},
3456038d2ef8SMaor Gottlieb 			.eth_flow = {
3457038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3458038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3459038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3460038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
3461038d2ef8SMaor Gottlieb 			}
3462038d2ef8SMaor Gottlieb 		}
3463038d2ef8SMaor Gottlieb 	};
3464038d2ef8SMaor Gottlieb 
3465038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
3466038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
3467038d2ef8SMaor Gottlieb 				   dst);
3468038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
3469038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3470038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
3471038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3472038d2ef8SMaor Gottlieb 						 dst);
3473038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
347474491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3475d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
3476038d2ef8SMaor Gottlieb 			kfree(handler);
3477038d2ef8SMaor Gottlieb 			handler = handler_ucast;
3478038d2ef8SMaor Gottlieb 		} else {
3479038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
3480038d2ef8SMaor Gottlieb 		}
3481038d2ef8SMaor Gottlieb 	}
3482038d2ef8SMaor Gottlieb 
3483038d2ef8SMaor Gottlieb 	return handler;
3484038d2ef8SMaor Gottlieb }
3485038d2ef8SMaor Gottlieb 
3486cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3487cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
3488cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
3489cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
3490cc0e5d42SMaor Gottlieb {
3491cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
3492cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
3493cc0e5d42SMaor Gottlieb 	int err;
3494cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
3495cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
3496cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
3497cc0e5d42SMaor Gottlieb 	};
3498cc0e5d42SMaor Gottlieb 
3499cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3500cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
3501cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
3502cc0e5d42SMaor Gottlieb 		goto err;
3503cc0e5d42SMaor Gottlieb 	}
3504cc0e5d42SMaor Gottlieb 
3505cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3506cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
3507cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
3508cc0e5d42SMaor Gottlieb 		goto err_tx;
3509cc0e5d42SMaor Gottlieb 	}
3510cc0e5d42SMaor Gottlieb 
3511cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
3512cc0e5d42SMaor Gottlieb 
3513cc0e5d42SMaor Gottlieb 	return handler_rx;
3514cc0e5d42SMaor Gottlieb 
3515cc0e5d42SMaor Gottlieb err_tx:
351674491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
3517cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
3518cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
3519cc0e5d42SMaor Gottlieb err:
3520cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
3521cc0e5d42SMaor Gottlieb }
3522cc0e5d42SMaor Gottlieb 
3523038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3524038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
352559082a32SMatan Barak 					   int domain,
352659082a32SMatan Barak 					   struct ib_udata *udata)
3527038d2ef8SMaor Gottlieb {
3528038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3529d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
3530038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3531038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
3532cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3533038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
3534802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
35353b3233fbSRaed Salem 	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
35363b3233fbSRaed Salem 	size_t min_ucmd_sz, required_ucmd_sz;
3537038d2ef8SMaor Gottlieb 	int err;
3538a550ddfcSYishai Hadas 	int underlay_qpn;
3539038d2ef8SMaor Gottlieb 
35403b3233fbSRaed Salem 	if (udata && udata->inlen) {
35413b3233fbSRaed Salem 		min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
35423b3233fbSRaed Salem 				sizeof(ucmd_hdr.reserved);
35433b3233fbSRaed Salem 		if (udata->inlen < min_ucmd_sz)
354459082a32SMatan Barak 			return ERR_PTR(-EOPNOTSUPP);
354559082a32SMatan Barak 
35463b3233fbSRaed Salem 		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
35473b3233fbSRaed Salem 		if (err)
35483b3233fbSRaed Salem 			return ERR_PTR(err);
35493b3233fbSRaed Salem 
35503b3233fbSRaed Salem 		/* currently supports only one counters data */
35513b3233fbSRaed Salem 		if (ucmd_hdr.ncounters_data > 1)
35523b3233fbSRaed Salem 			return ERR_PTR(-EINVAL);
35533b3233fbSRaed Salem 
35543b3233fbSRaed Salem 		required_ucmd_sz = min_ucmd_sz +
35553b3233fbSRaed Salem 			sizeof(struct mlx5_ib_flow_counters_data) *
35563b3233fbSRaed Salem 			ucmd_hdr.ncounters_data;
35573b3233fbSRaed Salem 		if (udata->inlen > required_ucmd_sz &&
35583b3233fbSRaed Salem 		    !ib_is_udata_cleared(udata, required_ucmd_sz,
35593b3233fbSRaed Salem 					 udata->inlen - required_ucmd_sz))
35603b3233fbSRaed Salem 			return ERR_PTR(-EOPNOTSUPP);
35613b3233fbSRaed Salem 
35623b3233fbSRaed Salem 		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
35633b3233fbSRaed Salem 		if (!ucmd)
35643b3233fbSRaed Salem 			return ERR_PTR(-ENOMEM);
35653b3233fbSRaed Salem 
35663b3233fbSRaed Salem 		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
3567299eafeeSGustavo A. R. Silva 		if (err)
3568299eafeeSGustavo A. R. Silva 			goto free_ucmd;
35693b3233fbSRaed Salem 	}
35703b3233fbSRaed Salem 
3571299eafeeSGustavo A. R. Silva 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3572299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3573299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3574299eafeeSGustavo A. R. Silva 	}
3575038d2ef8SMaor Gottlieb 
3576038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
3577508562d6SDaniel Jurgens 	    flow_attr->port > dev->num_ports ||
3578802c2125SAviad Yehezkel 	    (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3579299eafeeSGustavo A. R. Silva 				  IB_FLOW_ATTR_FLAGS_EGRESS))) {
3580299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3581299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3582299eafeeSGustavo A. R. Silva 	}
3583802c2125SAviad Yehezkel 
3584802c2125SAviad Yehezkel 	if (is_egress &&
3585802c2125SAviad Yehezkel 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3586299eafeeSGustavo A. R. Silva 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3587299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3588299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3589299eafeeSGustavo A. R. Silva 	}
3590038d2ef8SMaor Gottlieb 
3591038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3592299eafeeSGustavo A. R. Silva 	if (!dst) {
3593299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3594299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3595299eafeeSGustavo A. R. Silva 	}
3596038d2ef8SMaor Gottlieb 
35979a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3598038d2ef8SMaor Gottlieb 
3599802c2125SAviad Yehezkel 	ft_prio = get_flow_table(dev, flow_attr,
3600802c2125SAviad Yehezkel 				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3601038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
3602038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
3603038d2ef8SMaor Gottlieb 		goto unlock;
3604038d2ef8SMaor Gottlieb 	}
3605cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3606cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3607cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
3608cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
3609cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
3610cc0e5d42SMaor Gottlieb 			goto destroy_ft;
3611cc0e5d42SMaor Gottlieb 		}
3612cc0e5d42SMaor Gottlieb 	}
3613038d2ef8SMaor Gottlieb 
3614802c2125SAviad Yehezkel 	if (is_egress) {
3615802c2125SAviad Yehezkel 		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3616802c2125SAviad Yehezkel 	} else {
3617038d2ef8SMaor Gottlieb 		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3618d9f88e5aSYishai Hadas 		if (mqp->flags & MLX5_IB_QP_RSS)
3619d9f88e5aSYishai Hadas 			dst->tir_num = mqp->rss_qp.tirn;
3620d9f88e5aSYishai Hadas 		else
3621d9f88e5aSYishai Hadas 			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3622802c2125SAviad Yehezkel 	}
3623038d2ef8SMaor Gottlieb 
3624038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
362535d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
362635d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
362735d19011SMaor Gottlieb 							flow_attr, dst);
362835d19011SMaor Gottlieb 		} else {
3629a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3630a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
3631a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
36323b3233fbSRaed Salem 						    dst, underlay_qpn, ucmd);
363335d19011SMaor Gottlieb 		}
3634038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3635038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3636038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3637038d2ef8SMaor Gottlieb 						dst);
3638cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3639cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3640038d2ef8SMaor Gottlieb 	} else {
3641038d2ef8SMaor Gottlieb 		err = -EINVAL;
3642038d2ef8SMaor Gottlieb 		goto destroy_ft;
3643038d2ef8SMaor Gottlieb 	}
3644038d2ef8SMaor Gottlieb 
3645038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
3646038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
3647038d2ef8SMaor Gottlieb 		handler = NULL;
3648038d2ef8SMaor Gottlieb 		goto destroy_ft;
3649038d2ef8SMaor Gottlieb 	}
3650038d2ef8SMaor Gottlieb 
36519a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3652038d2ef8SMaor Gottlieb 	kfree(dst);
36533b3233fbSRaed Salem 	kfree(ucmd);
3654038d2ef8SMaor Gottlieb 
3655038d2ef8SMaor Gottlieb 	return &handler->ibflow;
3656038d2ef8SMaor Gottlieb 
3657038d2ef8SMaor Gottlieb destroy_ft:
3658038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
3659cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
3660cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
3661038d2ef8SMaor Gottlieb unlock:
36629a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3663038d2ef8SMaor Gottlieb 	kfree(dst);
3664299eafeeSGustavo A. R. Silva free_ucmd:
36653b3233fbSRaed Salem 	kfree(ucmd);
3666038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
3667038d2ef8SMaor Gottlieb }
3668038d2ef8SMaor Gottlieb 
3669b47fd4ffSMark Bloch static struct mlx5_ib_flow_prio *
3670b47fd4ffSMark Bloch _get_flow_table(struct mlx5_ib_dev *dev,
3671b47fd4ffSMark Bloch 		struct mlx5_ib_flow_matcher *fs_matcher,
3672b47fd4ffSMark Bloch 		bool mcast)
3673d4be3f44SYishai Hadas {
3674d4be3f44SYishai Hadas 	struct mlx5_flow_namespace *ns = NULL;
3675d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *prio;
3676b47fd4ffSMark Bloch 	int max_table_size;
3677b47fd4ffSMark Bloch 	u32 flags = 0;
3678b47fd4ffSMark Bloch 	int priority;
3679d4be3f44SYishai Hadas 
3680b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
3681d4be3f44SYishai Hadas 		max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3682d4be3f44SYishai Hadas 					log_max_ft_size));
3683b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3684b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3685b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3686b47fd4ffSMark Bloch 					      reformat_l3_tunnel_to_l2))
3687b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3688b47fd4ffSMark Bloch 	} else { /* Can only be MLX5_FLOW_NAMESPACE_EGRESS */
3689b47fd4ffSMark Bloch 		max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3690b47fd4ffSMark Bloch 					log_max_ft_size));
3691b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3692b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3693b47fd4ffSMark Bloch 	}
3694b47fd4ffSMark Bloch 
3695d4be3f44SYishai Hadas 	if (max_table_size < MLX5_FS_MAX_ENTRIES)
3696d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
3697d4be3f44SYishai Hadas 
3698d4be3f44SYishai Hadas 	if (mcast)
3699d4be3f44SYishai Hadas 		priority = MLX5_IB_FLOW_MCAST_PRIO;
3700d4be3f44SYishai Hadas 	else
3701b47fd4ffSMark Bloch 		priority = ib_prio_to_core_prio(fs_matcher->priority, false);
3702d4be3f44SYishai Hadas 
3703b47fd4ffSMark Bloch 	ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
3704d4be3f44SYishai Hadas 	if (!ns)
3705d4be3f44SYishai Hadas 		return ERR_PTR(-ENOTSUPP);
3706d4be3f44SYishai Hadas 
3707b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
3708d4be3f44SYishai Hadas 		prio = &dev->flow_db->prios[priority];
3709b47fd4ffSMark Bloch 	else
3710b47fd4ffSMark Bloch 		prio = &dev->flow_db->egress_prios[priority];
3711d4be3f44SYishai Hadas 
3712d4be3f44SYishai Hadas 	if (prio->flow_table)
3713d4be3f44SYishai Hadas 		return prio;
3714d4be3f44SYishai Hadas 
3715d4be3f44SYishai Hadas 	return _get_prio(ns, prio, priority, MLX5_FS_MAX_ENTRIES,
3716b47fd4ffSMark Bloch 			 MLX5_FS_MAX_TYPES, flags);
3717d4be3f44SYishai Hadas }
3718d4be3f44SYishai Hadas 
3719d4be3f44SYishai Hadas static struct mlx5_ib_flow_handler *
3720d4be3f44SYishai Hadas _create_raw_flow_rule(struct mlx5_ib_dev *dev,
3721d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_prio *ft_prio,
3722d4be3f44SYishai Hadas 		      struct mlx5_flow_destination *dst,
3723d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_matcher  *fs_matcher,
3724b823dd6dSMark Bloch 		      struct mlx5_flow_act *flow_act,
3725bfc5d839SMark Bloch 		      void *cmd_in, int inlen,
3726bfc5d839SMark Bloch 		      int dst_num)
3727d4be3f44SYishai Hadas {
3728d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
3729d4be3f44SYishai Hadas 	struct mlx5_flow_spec *spec;
3730d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft = ft_prio->flow_table;
3731d4be3f44SYishai Hadas 	int err = 0;
3732d4be3f44SYishai Hadas 
3733d4be3f44SYishai Hadas 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3734d4be3f44SYishai Hadas 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3735d4be3f44SYishai Hadas 	if (!handler || !spec) {
3736d4be3f44SYishai Hadas 		err = -ENOMEM;
3737d4be3f44SYishai Hadas 		goto free;
3738d4be3f44SYishai Hadas 	}
3739d4be3f44SYishai Hadas 
3740d4be3f44SYishai Hadas 	INIT_LIST_HEAD(&handler->list);
3741d4be3f44SYishai Hadas 
3742d4be3f44SYishai Hadas 	memcpy(spec->match_value, cmd_in, inlen);
3743d4be3f44SYishai Hadas 	memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
3744d4be3f44SYishai Hadas 	       fs_matcher->mask_len);
3745d4be3f44SYishai Hadas 	spec->match_criteria_enable = fs_matcher->match_criteria_enable;
3746d4be3f44SYishai Hadas 
3747d4be3f44SYishai Hadas 	handler->rule = mlx5_add_flow_rules(ft, spec,
3748bfc5d839SMark Bloch 					    flow_act, dst, dst_num);
3749d4be3f44SYishai Hadas 
3750d4be3f44SYishai Hadas 	if (IS_ERR(handler->rule)) {
3751d4be3f44SYishai Hadas 		err = PTR_ERR(handler->rule);
3752d4be3f44SYishai Hadas 		goto free;
3753d4be3f44SYishai Hadas 	}
3754d4be3f44SYishai Hadas 
3755d4be3f44SYishai Hadas 	ft_prio->refcount++;
3756d4be3f44SYishai Hadas 	handler->prio = ft_prio;
3757d4be3f44SYishai Hadas 	handler->dev = dev;
3758d4be3f44SYishai Hadas 	ft_prio->flow_table = ft;
3759d4be3f44SYishai Hadas 
3760d4be3f44SYishai Hadas free:
3761d4be3f44SYishai Hadas 	if (err)
3762d4be3f44SYishai Hadas 		kfree(handler);
3763d4be3f44SYishai Hadas 	kvfree(spec);
3764d4be3f44SYishai Hadas 	return err ? ERR_PTR(err) : handler;
3765d4be3f44SYishai Hadas }
3766d4be3f44SYishai Hadas 
3767d4be3f44SYishai Hadas static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
3768d4be3f44SYishai Hadas 				void *match_v)
3769d4be3f44SYishai Hadas {
3770d4be3f44SYishai Hadas 	void *match_c;
3771d4be3f44SYishai Hadas 	void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
3772d4be3f44SYishai Hadas 	void *dmac, *dmac_mask;
3773d4be3f44SYishai Hadas 	void *ipv4, *ipv4_mask;
3774d4be3f44SYishai Hadas 
3775d4be3f44SYishai Hadas 	if (!(fs_matcher->match_criteria_enable &
3776d4be3f44SYishai Hadas 	      (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
3777d4be3f44SYishai Hadas 		return false;
3778d4be3f44SYishai Hadas 
3779d4be3f44SYishai Hadas 	match_c = fs_matcher->matcher_mask.match_params;
3780d4be3f44SYishai Hadas 	match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
3781d4be3f44SYishai Hadas 					   outer_headers);
3782d4be3f44SYishai Hadas 	match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
3783d4be3f44SYishai Hadas 					   outer_headers);
3784d4be3f44SYishai Hadas 
3785d4be3f44SYishai Hadas 	dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3786d4be3f44SYishai Hadas 			    dmac_47_16);
3787d4be3f44SYishai Hadas 	dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3788d4be3f44SYishai Hadas 				 dmac_47_16);
3789d4be3f44SYishai Hadas 
3790d4be3f44SYishai Hadas 	if (is_multicast_ether_addr(dmac) &&
3791d4be3f44SYishai Hadas 	    is_multicast_ether_addr(dmac_mask))
3792d4be3f44SYishai Hadas 		return true;
3793d4be3f44SYishai Hadas 
3794d4be3f44SYishai Hadas 	ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3795d4be3f44SYishai Hadas 			    dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3796d4be3f44SYishai Hadas 
3797d4be3f44SYishai Hadas 	ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3798d4be3f44SYishai Hadas 				 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3799d4be3f44SYishai Hadas 
3800d4be3f44SYishai Hadas 	if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
3801d4be3f44SYishai Hadas 	    ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
3802d4be3f44SYishai Hadas 		return true;
3803d4be3f44SYishai Hadas 
3804d4be3f44SYishai Hadas 	return false;
3805d4be3f44SYishai Hadas }
3806d4be3f44SYishai Hadas 
380732269441SYishai Hadas struct mlx5_ib_flow_handler *
380832269441SYishai Hadas mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
380932269441SYishai Hadas 			struct mlx5_ib_flow_matcher *fs_matcher,
3810b823dd6dSMark Bloch 			struct mlx5_flow_act *flow_act,
3811bfc5d839SMark Bloch 			u32 counter_id,
381232269441SYishai Hadas 			void *cmd_in, int inlen, int dest_id,
381332269441SYishai Hadas 			int dest_type)
381432269441SYishai Hadas {
3815d4be3f44SYishai Hadas 	struct mlx5_flow_destination *dst;
3816d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *ft_prio;
3817d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
3818bfc5d839SMark Bloch 	int dst_num = 0;
3819d4be3f44SYishai Hadas 	bool mcast;
3820d4be3f44SYishai Hadas 	int err;
3821d4be3f44SYishai Hadas 
3822d4be3f44SYishai Hadas 	if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
382332269441SYishai Hadas 		return ERR_PTR(-EOPNOTSUPP);
3824d4be3f44SYishai Hadas 
3825d4be3f44SYishai Hadas 	if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
3826d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
3827d4be3f44SYishai Hadas 
3828bfc5d839SMark Bloch 	dst = kzalloc(sizeof(*dst) * 2, GFP_KERNEL);
3829d4be3f44SYishai Hadas 	if (!dst)
3830d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
3831d4be3f44SYishai Hadas 
3832d4be3f44SYishai Hadas 	mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
3833d4be3f44SYishai Hadas 	mutex_lock(&dev->flow_db->lock);
3834d4be3f44SYishai Hadas 
3835b47fd4ffSMark Bloch 	ft_prio = _get_flow_table(dev, fs_matcher, mcast);
3836d4be3f44SYishai Hadas 	if (IS_ERR(ft_prio)) {
3837d4be3f44SYishai Hadas 		err = PTR_ERR(ft_prio);
3838d4be3f44SYishai Hadas 		goto unlock;
3839d4be3f44SYishai Hadas 	}
3840d4be3f44SYishai Hadas 
38416346f0bfSYishai Hadas 	if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
3842bfc5d839SMark Bloch 		dst[dst_num].type = dest_type;
3843bfc5d839SMark Bloch 		dst[dst_num].tir_num = dest_id;
3844b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3845a7ee18bdSMark Bloch 	} else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
3846bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
3847bfc5d839SMark Bloch 		dst[dst_num].ft_num = dest_id;
3848b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3849a7ee18bdSMark Bloch 	} else {
3850bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3851a7ee18bdSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
38526346f0bfSYishai Hadas 	}
38536346f0bfSYishai Hadas 
3854bfc5d839SMark Bloch 	dst_num++;
3855bfc5d839SMark Bloch 
3856bfc5d839SMark Bloch 	if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3857bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3858bfc5d839SMark Bloch 		dst[dst_num].counter_id = counter_id;
3859bfc5d839SMark Bloch 		dst_num++;
3860bfc5d839SMark Bloch 	}
3861bfc5d839SMark Bloch 
3862b823dd6dSMark Bloch 	handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher, flow_act,
3863bfc5d839SMark Bloch 					cmd_in, inlen, dst_num);
3864d4be3f44SYishai Hadas 
3865d4be3f44SYishai Hadas 	if (IS_ERR(handler)) {
3866d4be3f44SYishai Hadas 		err = PTR_ERR(handler);
3867d4be3f44SYishai Hadas 		goto destroy_ft;
3868d4be3f44SYishai Hadas 	}
3869d4be3f44SYishai Hadas 
3870d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
3871d4be3f44SYishai Hadas 	atomic_inc(&fs_matcher->usecnt);
3872d4be3f44SYishai Hadas 	handler->flow_matcher = fs_matcher;
3873d4be3f44SYishai Hadas 
3874d4be3f44SYishai Hadas 	kfree(dst);
3875d4be3f44SYishai Hadas 
3876d4be3f44SYishai Hadas 	return handler;
3877d4be3f44SYishai Hadas 
3878d4be3f44SYishai Hadas destroy_ft:
3879d4be3f44SYishai Hadas 	put_flow_table(dev, ft_prio, false);
3880d4be3f44SYishai Hadas unlock:
3881d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
3882d4be3f44SYishai Hadas 	kfree(dst);
3883d4be3f44SYishai Hadas 
3884d4be3f44SYishai Hadas 	return ERR_PTR(err);
388532269441SYishai Hadas }
388632269441SYishai Hadas 
3887c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
3888c6475a0bSAviad Yehezkel {
3889c6475a0bSAviad Yehezkel 	u32 flags = 0;
3890c6475a0bSAviad Yehezkel 
3891c6475a0bSAviad Yehezkel 	if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
3892c6475a0bSAviad Yehezkel 		flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
3893c6475a0bSAviad Yehezkel 
3894c6475a0bSAviad Yehezkel 	return flags;
3895c6475a0bSAviad Yehezkel }
3896c6475a0bSAviad Yehezkel 
3897c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED	MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
3898c6475a0bSAviad Yehezkel static struct ib_flow_action *
3899c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device,
3900c6475a0bSAviad Yehezkel 			       const struct ib_flow_action_attrs_esp *attr,
3901c6475a0bSAviad Yehezkel 			       struct uverbs_attr_bundle *attrs)
3902c6475a0bSAviad Yehezkel {
3903c6475a0bSAviad Yehezkel 	struct mlx5_ib_dev *mdev = to_mdev(device);
3904c6475a0bSAviad Yehezkel 	struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
3905c6475a0bSAviad Yehezkel 	struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
3906c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *action;
3907c6475a0bSAviad Yehezkel 	u64 action_flags;
3908c6475a0bSAviad Yehezkel 	u64 flags;
3909c6475a0bSAviad Yehezkel 	int err = 0;
3910c6475a0bSAviad Yehezkel 
3911bccd0622SJason Gunthorpe 	err = uverbs_get_flags64(
3912bccd0622SJason Gunthorpe 		&action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
3913bccd0622SJason Gunthorpe 		((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
3914bccd0622SJason Gunthorpe 	if (err)
3915bccd0622SJason Gunthorpe 		return ERR_PTR(err);
3916c6475a0bSAviad Yehezkel 
3917c6475a0bSAviad Yehezkel 	flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
3918c6475a0bSAviad Yehezkel 
3919c6475a0bSAviad Yehezkel 	/* We current only support a subset of the standard features. Only a
3920c6475a0bSAviad Yehezkel 	 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
3921c6475a0bSAviad Yehezkel 	 * (with overlap). Full offload mode isn't supported.
3922c6475a0bSAviad Yehezkel 	 */
3923c6475a0bSAviad Yehezkel 	if (!attr->keymat || attr->replay || attr->encap ||
3924c6475a0bSAviad Yehezkel 	    attr->spi || attr->seq || attr->tfc_pad ||
3925c6475a0bSAviad Yehezkel 	    attr->hard_limit_pkts ||
3926c6475a0bSAviad Yehezkel 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3927c6475a0bSAviad Yehezkel 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
3928c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3929c6475a0bSAviad Yehezkel 
3930c6475a0bSAviad Yehezkel 	if (attr->keymat->protocol !=
3931c6475a0bSAviad Yehezkel 	    IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
3932c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3933c6475a0bSAviad Yehezkel 
3934c6475a0bSAviad Yehezkel 	aes_gcm = &attr->keymat->keymat.aes_gcm;
3935c6475a0bSAviad Yehezkel 
3936c6475a0bSAviad Yehezkel 	if (aes_gcm->icv_len != 16 ||
3937c6475a0bSAviad Yehezkel 	    aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
3938c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3939c6475a0bSAviad Yehezkel 
3940c6475a0bSAviad Yehezkel 	action = kmalloc(sizeof(*action), GFP_KERNEL);
3941c6475a0bSAviad Yehezkel 	if (!action)
3942c6475a0bSAviad Yehezkel 		return ERR_PTR(-ENOMEM);
3943c6475a0bSAviad Yehezkel 
3944c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
3945c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
3946c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.aes_key));
3947c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
3948c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
3949c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.salt));
3950c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
3951c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
3952c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
3953c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
3954c6475a0bSAviad Yehezkel 	accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
3955c6475a0bSAviad Yehezkel 
3956c6475a0bSAviad Yehezkel 	accel_attrs.esn = attr->esn;
3957c6475a0bSAviad Yehezkel 	if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
3958c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
3959c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
3960c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3961c6475a0bSAviad Yehezkel 
3962c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
3963c6475a0bSAviad Yehezkel 		accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
3964c6475a0bSAviad Yehezkel 
3965c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ctx =
3966c6475a0bSAviad Yehezkel 		mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
3967c6475a0bSAviad Yehezkel 	if (IS_ERR(action->esp_aes_gcm.ctx)) {
3968c6475a0bSAviad Yehezkel 		err = PTR_ERR(action->esp_aes_gcm.ctx);
3969c6475a0bSAviad Yehezkel 		goto err_parse;
3970c6475a0bSAviad Yehezkel 	}
3971c6475a0bSAviad Yehezkel 
3972c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
3973c6475a0bSAviad Yehezkel 
3974c6475a0bSAviad Yehezkel 	return &action->ib_action;
3975c6475a0bSAviad Yehezkel 
3976c6475a0bSAviad Yehezkel err_parse:
3977c6475a0bSAviad Yehezkel 	kfree(action);
3978c6475a0bSAviad Yehezkel 	return ERR_PTR(err);
3979c6475a0bSAviad Yehezkel }
3980c6475a0bSAviad Yehezkel 
3981349705c1SMatan Barak static int
3982349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
3983349705c1SMatan Barak 			       const struct ib_flow_action_attrs_esp *attr,
3984349705c1SMatan Barak 			       struct uverbs_attr_bundle *attrs)
3985349705c1SMatan Barak {
3986349705c1SMatan Barak 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
3987349705c1SMatan Barak 	struct mlx5_accel_esp_xfrm_attrs accel_attrs;
3988349705c1SMatan Barak 	int err = 0;
3989349705c1SMatan Barak 
3990349705c1SMatan Barak 	if (attr->keymat || attr->replay || attr->encap ||
3991349705c1SMatan Barak 	    attr->spi || attr->seq || attr->tfc_pad ||
3992349705c1SMatan Barak 	    attr->hard_limit_pkts ||
3993349705c1SMatan Barak 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3994349705c1SMatan Barak 			     IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
3995349705c1SMatan Barak 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
3996349705c1SMatan Barak 		return -EOPNOTSUPP;
3997349705c1SMatan Barak 
3998349705c1SMatan Barak 	/* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
3999349705c1SMatan Barak 	 * be modified.
4000349705c1SMatan Barak 	 */
4001349705c1SMatan Barak 	if (!(maction->esp_aes_gcm.ib_flags &
4002349705c1SMatan Barak 	      IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4003349705c1SMatan Barak 	    attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4004349705c1SMatan Barak 			   IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4005349705c1SMatan Barak 		return -EINVAL;
4006349705c1SMatan Barak 
4007349705c1SMatan Barak 	memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4008349705c1SMatan Barak 	       sizeof(accel_attrs));
4009349705c1SMatan Barak 
4010349705c1SMatan Barak 	accel_attrs.esn = attr->esn;
4011349705c1SMatan Barak 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4012349705c1SMatan Barak 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4013349705c1SMatan Barak 	else
4014349705c1SMatan Barak 		accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4015349705c1SMatan Barak 
4016349705c1SMatan Barak 	err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4017349705c1SMatan Barak 					 &accel_attrs);
4018349705c1SMatan Barak 	if (err)
4019349705c1SMatan Barak 		return err;
4020349705c1SMatan Barak 
4021349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags &=
4022349705c1SMatan Barak 		~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4023349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags |=
4024349705c1SMatan Barak 		attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4025349705c1SMatan Barak 
4026349705c1SMatan Barak 	return 0;
4027349705c1SMatan Barak }
4028349705c1SMatan Barak 
4029c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4030c6475a0bSAviad Yehezkel {
4031c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4032c6475a0bSAviad Yehezkel 
4033c6475a0bSAviad Yehezkel 	switch (action->type) {
4034c6475a0bSAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
4035c6475a0bSAviad Yehezkel 		/*
4036c6475a0bSAviad Yehezkel 		 * We only support aes_gcm by now, so we implicitly know this is
4037c6475a0bSAviad Yehezkel 		 * the underline crypto.
4038c6475a0bSAviad Yehezkel 		 */
4039c6475a0bSAviad Yehezkel 		mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4040c6475a0bSAviad Yehezkel 		break;
4041b4749bf2SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
4042b4749bf2SMark Bloch 		mlx5_ib_destroy_flow_action_raw(maction);
4043b4749bf2SMark Bloch 		break;
4044c6475a0bSAviad Yehezkel 	default:
4045c6475a0bSAviad Yehezkel 		WARN_ON(true);
4046c6475a0bSAviad Yehezkel 		break;
4047c6475a0bSAviad Yehezkel 	}
4048c6475a0bSAviad Yehezkel 
4049c6475a0bSAviad Yehezkel 	kfree(maction);
4050c6475a0bSAviad Yehezkel 	return 0;
4051c6475a0bSAviad Yehezkel }
4052c6475a0bSAviad Yehezkel 
4053e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4054e126ba97SEli Cohen {
4055e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
405681e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
4057e126ba97SEli Cohen 	int err;
4058539ec982SYishai Hadas 	u16 uid;
4059539ec982SYishai Hadas 
4060539ec982SYishai Hadas 	uid = ibqp->pd ?
4061539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4062e126ba97SEli Cohen 
406381e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
406481e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
406581e30880SYishai Hadas 		return -EOPNOTSUPP;
406681e30880SYishai Hadas 	}
406781e30880SYishai Hadas 
4068539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4069e126ba97SEli Cohen 	if (err)
4070e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4071e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4072e126ba97SEli Cohen 
4073e126ba97SEli Cohen 	return err;
4074e126ba97SEli Cohen }
4075e126ba97SEli Cohen 
4076e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4077e126ba97SEli Cohen {
4078e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4079e126ba97SEli Cohen 	int err;
4080539ec982SYishai Hadas 	u16 uid;
4081e126ba97SEli Cohen 
4082539ec982SYishai Hadas 	uid = ibqp->pd ?
4083539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4084539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4085e126ba97SEli Cohen 	if (err)
4086e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4087e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4088e126ba97SEli Cohen 
4089e126ba97SEli Cohen 	return err;
4090e126ba97SEli Cohen }
4091e126ba97SEli Cohen 
4092e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
4093e126ba97SEli Cohen {
40941b5daf11SMajd Dibbiny 	int err;
4095e126ba97SEli Cohen 
40961b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
4097e126ba97SEli Cohen 	if (err)
4098e126ba97SEli Cohen 		return err;
40991b5daf11SMajd Dibbiny 
41001b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
41011b5daf11SMajd Dibbiny 
41021b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
4103e126ba97SEli Cohen }
4104e126ba97SEli Cohen 
4105508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
4106508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4107e126ba97SEli Cohen {
4108e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4109e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4110e126ba97SEli Cohen 
41119603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
4112e126ba97SEli Cohen }
4113508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
4114e126ba97SEli Cohen 
4115508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
4116e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
4117e126ba97SEli Cohen {
4118e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4119e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4120e126ba97SEli Cohen 
41216aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
4122e126ba97SEli Cohen }
4123508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
4124e126ba97SEli Cohen 
4125508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
4126508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4127e126ba97SEli Cohen {
4128e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4129e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
41309603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
4131e126ba97SEli Cohen }
4132508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
4133e126ba97SEli Cohen 
4134508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
4135508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
4136e126ba97SEli Cohen {
4137e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4138e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
41399603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
4140e126ba97SEli Cohen }
4141508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
4142e126ba97SEli Cohen 
4143508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
4144508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4145e126ba97SEli Cohen {
4146e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4147e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4148e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
41499603b61dSJack Morgenstein 		       dev->mdev->board_id);
4150e126ba97SEli Cohen }
4151508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
4152e126ba97SEli Cohen 
4153508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
4154508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
4155508a523fSParav Pandit 	&dev_attr_hca_type.attr,
4156508a523fSParav Pandit 	&dev_attr_board_id.attr,
4157508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
4158508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
4159508a523fSParav Pandit 	NULL,
4160508a523fSParav Pandit };
4161e126ba97SEli Cohen 
4162508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
4163508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
4164e126ba97SEli Cohen };
4165e126ba97SEli Cohen 
41667722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
41677722f47eSHaggai Eran {
41687722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
41697722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
41707722f47eSHaggai Eran 			     pkey_change_work);
41717722f47eSHaggai Eran 
41727722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
41737722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
41747722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
41757722f47eSHaggai Eran }
41767722f47eSHaggai Eran 
417789ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
417889ea94a7SMaor Gottlieb {
417989ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
418089ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
418189ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
418289ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
418389ea94a7SMaor Gottlieb 	unsigned long flags_qp;
418489ea94a7SMaor Gottlieb 	unsigned long flags_cq;
418589ea94a7SMaor Gottlieb 	unsigned long flags;
418689ea94a7SMaor Gottlieb 
418789ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
418889ea94a7SMaor Gottlieb 
418989ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
419089ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
419189ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
419289ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
419389ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
419489ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
419589ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
419689ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
419789ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
419889ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
419989ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
420089ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
420189ea94a7SMaor Gottlieb 						      &cq_armed_list);
420289ea94a7SMaor Gottlieb 				}
420389ea94a7SMaor Gottlieb 			}
420489ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
420589ea94a7SMaor Gottlieb 		}
420689ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
420789ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
420889ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
420989ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
421089ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
421189ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
421289ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
421389ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
421489ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
421589ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
421689ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
421789ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
421889ea94a7SMaor Gottlieb 							      &cq_armed_list);
421989ea94a7SMaor Gottlieb 					}
422089ea94a7SMaor Gottlieb 				}
422189ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
422289ea94a7SMaor Gottlieb 						       flags_cq);
422389ea94a7SMaor Gottlieb 			}
422489ea94a7SMaor Gottlieb 		}
422589ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
422689ea94a7SMaor Gottlieb 	}
422789ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
422889ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
422989ea94a7SMaor Gottlieb 	 */
423089ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
423189ea94a7SMaor Gottlieb 		mcq->comp(mcq);
423289ea94a7SMaor Gottlieb 	}
423389ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
423489ea94a7SMaor Gottlieb }
423589ea94a7SMaor Gottlieb 
423603404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
423703404e8aSMaor Gottlieb {
423803404e8aSMaor Gottlieb 	int err;
423903404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
424003404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
424103404e8aSMaor Gottlieb 			     delay_drop_work);
424203404e8aSMaor Gottlieb 
4243fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
4244fe248c3aSMaor Gottlieb 
424503404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
424603404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
424703404e8aSMaor Gottlieb 				       delay_drop->timeout);
424803404e8aSMaor Gottlieb 	if (err) {
424903404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
425003404e8aSMaor Gottlieb 			     delay_drop->timeout);
425103404e8aSMaor Gottlieb 		delay_drop->activate = false;
425203404e8aSMaor Gottlieb 	}
425303404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
425403404e8aSMaor Gottlieb }
425503404e8aSMaor Gottlieb 
425609e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
425709e574faSSaeed Mahameed 				 struct ib_event *ibev)
425809e574faSSaeed Mahameed {
425909e574faSSaeed Mahameed 	switch (eqe->sub_type) {
426009e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
426109e574faSSaeed Mahameed 		schedule_work(&ibdev->delay_drop.delay_drop_work);
426209e574faSSaeed Mahameed 		break;
426309e574faSSaeed Mahameed 	default: /* do nothing */
426409e574faSSaeed Mahameed 		return;
426509e574faSSaeed Mahameed 	}
426609e574faSSaeed Mahameed }
426709e574faSSaeed Mahameed 
4268134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4269134e9349SSaeed Mahameed 			      struct ib_event *ibev)
4270134e9349SSaeed Mahameed {
4271134e9349SSaeed Mahameed 	u8 port = (eqe->data.port.port >> 4) & 0xf;
4272134e9349SSaeed Mahameed 
4273134e9349SSaeed Mahameed 	ibev->element.port_num = port;
4274134e9349SSaeed Mahameed 
4275134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
4276134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4277134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4278134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4279134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
4280134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
4281134e9349SSaeed Mahameed 		 */
4282134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4283134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
4284134e9349SSaeed Mahameed 			return -EINVAL;
4285134e9349SSaeed Mahameed 
4286134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4287134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4288134e9349SSaeed Mahameed 		break;
4289134e9349SSaeed Mahameed 
4290134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
4291134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
4292134e9349SSaeed Mahameed 		break;
4293134e9349SSaeed Mahameed 
4294134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4295134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
4296134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4297134e9349SSaeed Mahameed 		break;
4298134e9349SSaeed Mahameed 
4299134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4300134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
4301134e9349SSaeed Mahameed 		break;
4302134e9349SSaeed Mahameed 
4303134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4304134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
4305134e9349SSaeed Mahameed 		break;
4306134e9349SSaeed Mahameed 	default:
4307134e9349SSaeed Mahameed 		return -EINVAL;
4308134e9349SSaeed Mahameed 	}
4309134e9349SSaeed Mahameed 
4310134e9349SSaeed Mahameed 	return 0;
4311134e9349SSaeed Mahameed }
4312134e9349SSaeed Mahameed 
4313d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
4314e126ba97SEli Cohen {
4315d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
4316d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
4317d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
4318e126ba97SEli Cohen 	struct ib_event ibev;
4319dbaaff2aSEli Cohen 	bool fatal = false;
4320e126ba97SEli Cohen 
4321df097a27SSaeed Mahameed 	if (work->is_slave) {
4322df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
4323d69a24e0SDaniel Jurgens 		if (!ibdev)
4324d69a24e0SDaniel Jurgens 			goto out;
4325d69a24e0SDaniel Jurgens 	} else {
4326df097a27SSaeed Mahameed 		ibdev = work->dev;
4327d69a24e0SDaniel Jurgens 	}
4328d69a24e0SDaniel Jurgens 
4329d69a24e0SDaniel Jurgens 	switch (work->event) {
4330e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
4331e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
433289ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
4333134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
4334dbaaff2aSEli Cohen 		fatal = true;
4335e126ba97SEli Cohen 		break;
4336134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
4337134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
4338d69a24e0SDaniel Jurgens 			goto out;
4339e126ba97SEli Cohen 		break;
434009e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
434109e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
434209e574faSSaeed Mahameed 		/* fall through */
4343bdc37924SSaeed Mahameed 	default:
434403404e8aSMaor Gottlieb 		goto out;
4345e126ba97SEli Cohen 	}
4346e126ba97SEli Cohen 
4347e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
4348e126ba97SEli Cohen 
4349134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4350134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
435103404e8aSMaor Gottlieb 		goto out;
4352a0c84c32SEli Cohen 	}
4353a0c84c32SEli Cohen 
4354e126ba97SEli Cohen 	if (ibdev->ib_active)
4355e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
4356dbaaff2aSEli Cohen 
4357dbaaff2aSEli Cohen 	if (fatal)
4358dbaaff2aSEli Cohen 		ibdev->ib_active = false;
435903404e8aSMaor Gottlieb out:
4360d69a24e0SDaniel Jurgens 	kfree(work);
4361d69a24e0SDaniel Jurgens }
4362d69a24e0SDaniel Jurgens 
4363df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
4364df097a27SSaeed Mahameed 			 unsigned long event, void *param)
4365d69a24e0SDaniel Jurgens {
4366d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
4367d69a24e0SDaniel Jurgens 
4368d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
436910bea9c8SLeon Romanovsky 	if (!work)
4370df097a27SSaeed Mahameed 		return NOTIFY_DONE;
437110bea9c8SLeon Romanovsky 
4372d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4373df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4374df097a27SSaeed Mahameed 	work->is_slave = false;
4375d69a24e0SDaniel Jurgens 	work->param = param;
4376d69a24e0SDaniel Jurgens 	work->event = event;
4377d69a24e0SDaniel Jurgens 
4378d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
4379df097a27SSaeed Mahameed 
4380df097a27SSaeed Mahameed 	return NOTIFY_OK;
4381df097a27SSaeed Mahameed }
4382df097a27SSaeed Mahameed 
4383df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4384df097a27SSaeed Mahameed 				    unsigned long event, void *param)
4385df097a27SSaeed Mahameed {
4386df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
4387df097a27SSaeed Mahameed 
4388df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
4389df097a27SSaeed Mahameed 	if (!work)
4390df097a27SSaeed Mahameed 		return NOTIFY_DONE;
4391df097a27SSaeed Mahameed 
4392df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4393df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4394df097a27SSaeed Mahameed 	work->is_slave = true;
4395df097a27SSaeed Mahameed 	work->param = param;
4396df097a27SSaeed Mahameed 	work->event = event;
4397df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
4398df097a27SSaeed Mahameed 
4399df097a27SSaeed Mahameed 	return NOTIFY_OK;
4400d69a24e0SDaniel Jurgens }
4401d69a24e0SDaniel Jurgens 
4402c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4403c43f1112SMaor Gottlieb {
4404c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
4405c43f1112SMaor Gottlieb 	int err;
4406c43f1112SMaor Gottlieb 	int port;
4407c43f1112SMaor Gottlieb 
4408508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++) {
4409c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
4410c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4411c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
4412c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4413c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
4414c43f1112SMaor Gottlieb 								   port, 0,
4415c43f1112SMaor Gottlieb 								   &vport_ctx);
4416c43f1112SMaor Gottlieb 				if (err) {
4417c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4418c43f1112SMaor Gottlieb 						    port, err);
4419c43f1112SMaor Gottlieb 					return err;
4420c43f1112SMaor Gottlieb 				}
4421c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
4422c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
4423c43f1112SMaor Gottlieb 			} else {
4424c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
4425c43f1112SMaor Gottlieb 			}
4426c43f1112SMaor Gottlieb 		}
4427c43f1112SMaor Gottlieb 	}
4428c43f1112SMaor Gottlieb 	return 0;
4429c43f1112SMaor Gottlieb }
4430c43f1112SMaor Gottlieb 
4431e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4432e126ba97SEli Cohen {
4433e126ba97SEli Cohen 	int port;
4434e126ba97SEli Cohen 
4435508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
4436e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
4437e126ba97SEli Cohen }
4438e126ba97SEli Cohen 
443932f69e4bSDaniel Jurgens static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4440e126ba97SEli Cohen {
4441e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
4442e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
4443f614fc15SDan Carpenter 	int err = -ENOMEM;
44442528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
4445e126ba97SEli Cohen 
4446e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
4447e126ba97SEli Cohen 	if (!pprops)
4448e126ba97SEli Cohen 		goto out;
4449e126ba97SEli Cohen 
4450e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4451e126ba97SEli Cohen 	if (!dprops)
4452e126ba97SEli Cohen 		goto out;
4453e126ba97SEli Cohen 
4454c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
4455c43f1112SMaor Gottlieb 	if (err)
4456c43f1112SMaor Gottlieb 		goto out;
4457c43f1112SMaor Gottlieb 
44582528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
4459e126ba97SEli Cohen 	if (err) {
4460e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
4461e126ba97SEli Cohen 		goto out;
4462e126ba97SEli Cohen 	}
4463e126ba97SEli Cohen 
4464c4550c63SOr Gerlitz 	memset(pprops, 0, sizeof(*pprops));
4465e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4466e126ba97SEli Cohen 	if (err) {
4467938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
4468938fe83cSSaeed Mahameed 			     port, err);
446932f69e4bSDaniel Jurgens 		goto out;
4470e126ba97SEli Cohen 	}
447132f69e4bSDaniel Jurgens 
4472938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
4473938fe83cSSaeed Mahameed 					dprops->max_pkeys;
4474938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
4475938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
447632f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
447732f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
4478e126ba97SEli Cohen 
4479e126ba97SEli Cohen out:
4480e126ba97SEli Cohen 	kfree(pprops);
4481e126ba97SEli Cohen 	kfree(dprops);
4482e126ba97SEli Cohen 
4483e126ba97SEli Cohen 	return err;
4484e126ba97SEli Cohen }
4485e126ba97SEli Cohen 
4486e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4487e126ba97SEli Cohen {
4488e126ba97SEli Cohen 	int err;
4489e126ba97SEli Cohen 
4490e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
4491e126ba97SEli Cohen 	if (err)
4492e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4493e126ba97SEli Cohen 
449432927e28SMark Bloch 	if (dev->umrc.qp)
4495e126ba97SEli Cohen 		mlx5_ib_destroy_qp(dev->umrc.qp);
449632927e28SMark Bloch 	if (dev->umrc.cq)
4497add08d76SChristoph Hellwig 		ib_free_cq(dev->umrc.cq);
449832927e28SMark Bloch 	if (dev->umrc.pd)
4499e126ba97SEli Cohen 		ib_dealloc_pd(dev->umrc.pd);
4500e126ba97SEli Cohen }
4501e126ba97SEli Cohen 
4502e126ba97SEli Cohen enum {
4503e126ba97SEli Cohen 	MAX_UMR_WR = 128,
4504e126ba97SEli Cohen };
4505e126ba97SEli Cohen 
4506e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
4507e126ba97SEli Cohen {
4508e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
4509e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
4510e126ba97SEli Cohen 	struct ib_pd *pd;
4511e126ba97SEli Cohen 	struct ib_cq *cq;
4512e126ba97SEli Cohen 	struct ib_qp *qp;
4513e126ba97SEli Cohen 	int ret;
4514e126ba97SEli Cohen 
4515e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4516e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4517e126ba97SEli Cohen 	if (!attr || !init_attr) {
4518e126ba97SEli Cohen 		ret = -ENOMEM;
4519e126ba97SEli Cohen 		goto error_0;
4520e126ba97SEli Cohen 	}
4521e126ba97SEli Cohen 
4522ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
4523e126ba97SEli Cohen 	if (IS_ERR(pd)) {
4524e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4525e126ba97SEli Cohen 		ret = PTR_ERR(pd);
4526e126ba97SEli Cohen 		goto error_0;
4527e126ba97SEli Cohen 	}
4528e126ba97SEli Cohen 
4529add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
4530e126ba97SEli Cohen 	if (IS_ERR(cq)) {
4531e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4532e126ba97SEli Cohen 		ret = PTR_ERR(cq);
4533e126ba97SEli Cohen 		goto error_2;
4534e126ba97SEli Cohen 	}
4535e126ba97SEli Cohen 
4536e126ba97SEli Cohen 	init_attr->send_cq = cq;
4537e126ba97SEli Cohen 	init_attr->recv_cq = cq;
4538e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4539e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
4540e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
4541e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4542e126ba97SEli Cohen 	init_attr->port_num = 1;
4543e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4544e126ba97SEli Cohen 	if (IS_ERR(qp)) {
4545e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4546e126ba97SEli Cohen 		ret = PTR_ERR(qp);
4547e126ba97SEli Cohen 		goto error_3;
4548e126ba97SEli Cohen 	}
4549e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
4550e126ba97SEli Cohen 	qp->real_qp    = qp;
4551e126ba97SEli Cohen 	qp->uobject    = NULL;
4552e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
455331fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
455431fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
4555e126ba97SEli Cohen 
4556e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
4557e126ba97SEli Cohen 	attr->port_num = 1;
4558e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4559e126ba97SEli Cohen 				IB_QP_PORT, NULL);
4560e126ba97SEli Cohen 	if (ret) {
4561e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4562e126ba97SEli Cohen 		goto error_4;
4563e126ba97SEli Cohen 	}
4564e126ba97SEli Cohen 
4565e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4566e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
4567e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
4568e126ba97SEli Cohen 
4569e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4570e126ba97SEli Cohen 	if (ret) {
4571e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4572e126ba97SEli Cohen 		goto error_4;
4573e126ba97SEli Cohen 	}
4574e126ba97SEli Cohen 
4575e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4576e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
4577e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4578e126ba97SEli Cohen 	if (ret) {
4579e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4580e126ba97SEli Cohen 		goto error_4;
4581e126ba97SEli Cohen 	}
4582e126ba97SEli Cohen 
4583e126ba97SEli Cohen 	dev->umrc.qp = qp;
4584e126ba97SEli Cohen 	dev->umrc.cq = cq;
4585e126ba97SEli Cohen 	dev->umrc.pd = pd;
4586e126ba97SEli Cohen 
4587e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
4588e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
4589e126ba97SEli Cohen 	if (ret) {
4590e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4591e126ba97SEli Cohen 		goto error_4;
4592e126ba97SEli Cohen 	}
4593e126ba97SEli Cohen 
4594e126ba97SEli Cohen 	kfree(attr);
4595e126ba97SEli Cohen 	kfree(init_attr);
4596e126ba97SEli Cohen 
4597e126ba97SEli Cohen 	return 0;
4598e126ba97SEli Cohen 
4599e126ba97SEli Cohen error_4:
4600e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
460132927e28SMark Bloch 	dev->umrc.qp = NULL;
4602e126ba97SEli Cohen 
4603e126ba97SEli Cohen error_3:
4604add08d76SChristoph Hellwig 	ib_free_cq(cq);
460532927e28SMark Bloch 	dev->umrc.cq = NULL;
4606e126ba97SEli Cohen 
4607e126ba97SEli Cohen error_2:
4608e126ba97SEli Cohen 	ib_dealloc_pd(pd);
460932927e28SMark Bloch 	dev->umrc.pd = NULL;
4610e126ba97SEli Cohen 
4611e126ba97SEli Cohen error_0:
4612e126ba97SEli Cohen 	kfree(attr);
4613e126ba97SEli Cohen 	kfree(init_attr);
4614e126ba97SEli Cohen 	return ret;
4615e126ba97SEli Cohen }
4616e126ba97SEli Cohen 
46176e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
46186e8484c5SMax Gurtovoy {
46196e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
46206e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
46216e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
46226e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
46236e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
46246e8484c5SMax Gurtovoy 	default:
46256e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
46266e8484c5SMax Gurtovoy 	}
46276e8484c5SMax Gurtovoy }
46286e8484c5SMax Gurtovoy 
4629e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
4630e126ba97SEli Cohen {
4631e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
4632e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
4633bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
46347722f47eSHaggai Eran 	int port;
4635e126ba97SEli Cohen 	int ret = 0;
4636e126ba97SEli Cohen 
4637e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
4638e126ba97SEli Cohen 
4639d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
4640d16e91daSHaggai Eran 
4641e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
4642e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
4643e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
4644e126ba97SEli Cohen 		goto error0;
4645e126ba97SEli Cohen 	}
4646e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
4647e126ba97SEli Cohen 	devr->p0->uobject = NULL;
4648e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
4649e126ba97SEli Cohen 
4650bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
4651e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
4652e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
4653e126ba97SEli Cohen 		goto error1;
4654e126ba97SEli Cohen 	}
4655e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
4656e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
4657e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
4658e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
4659e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
4660e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
4661e126ba97SEli Cohen 
4662e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4663e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
4664e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
4665e126ba97SEli Cohen 		goto error2;
4666e126ba97SEli Cohen 	}
4667e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
4668e126ba97SEli Cohen 	devr->x0->inode = NULL;
4669e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
4670e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
4671e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4672e126ba97SEli Cohen 
4673e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4674e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
4675e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
4676e126ba97SEli Cohen 		goto error3;
4677e126ba97SEli Cohen 	}
4678e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
4679e126ba97SEli Cohen 	devr->x1->inode = NULL;
4680e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
4681e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
4682e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4683e126ba97SEli Cohen 
4684e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
4685e126ba97SEli Cohen 	attr.attr.max_sge = 1;
4686e126ba97SEli Cohen 	attr.attr.max_wr = 1;
4687e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
46881a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
4689e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
4690e126ba97SEli Cohen 
4691e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4692e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
4693e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
4694e126ba97SEli Cohen 		goto error4;
4695e126ba97SEli Cohen 	}
4696e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
4697e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
4698e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
4699e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
4700e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
4701e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
4702e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
47031a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
4704e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
47051a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
4706e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
4707e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
4708e126ba97SEli Cohen 
47094aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
47104aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
47114aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
47124aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
47134aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
47144aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
47154aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
47164aa17b28SHaggai Abramonvsky 		goto error5;
47174aa17b28SHaggai Abramonvsky 	}
47184aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
47194aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
47204aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
47214aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
47224aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
47234aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
47241a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
47254aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
47261a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
47274aa17b28SHaggai Abramonvsky 
47287722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
47297722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
47307722f47eSHaggai Eran 			  pkey_change_handler);
47317722f47eSHaggai Eran 		devr->ports[port].devr = devr;
47327722f47eSHaggai Eran 	}
47337722f47eSHaggai Eran 
4734e126ba97SEli Cohen 	return 0;
4735e126ba97SEli Cohen 
47364aa17b28SHaggai Abramonvsky error5:
47374aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
4738e126ba97SEli Cohen error4:
4739e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
4740e126ba97SEli Cohen error3:
4741e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
4742e126ba97SEli Cohen error2:
4743e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
4744e126ba97SEli Cohen error1:
4745e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
4746e126ba97SEli Cohen error0:
4747e126ba97SEli Cohen 	return ret;
4748e126ba97SEli Cohen }
4749e126ba97SEli Cohen 
4750e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
4751e126ba97SEli Cohen {
47527722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
47537722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
47547722f47eSHaggai Eran 	int port;
47557722f47eSHaggai Eran 
47564aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
4757e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
4758e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
4759e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
4760e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
4761e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
47627722f47eSHaggai Eran 
47637722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
47647722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
47657722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
4766e126ba97SEli Cohen }
4767e126ba97SEli Cohen 
4768b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
4769b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
4770e53505a8SAchiad Shochat {
4771e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4772e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
4773e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
4774e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
477585c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
4776e53505a8SAchiad Shochat 	u32 ret = 0;
4777e53505a8SAchiad Shochat 
4778b02289b3SArtemy Kovalyov 	if (rep->grh_required)
4779b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
4780b02289b3SArtemy Kovalyov 
4781e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
4782b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
4783e53505a8SAchiad Shochat 
478485c7c014SDaniel Jurgens 	if (raw_support)
4785b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
478672cd5717SOr Gerlitz 
4787e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
478872cd5717SOr Gerlitz 		return ret;
4789e53505a8SAchiad Shochat 
4790e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
479172cd5717SOr Gerlitz 		return ret;
4792e53505a8SAchiad Shochat 
4793e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
4794e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
4795e53505a8SAchiad Shochat 
4796e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
4797e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
4798e53505a8SAchiad Shochat 
4799e53505a8SAchiad Shochat 	return ret;
4800e53505a8SAchiad Shochat }
4801e53505a8SAchiad Shochat 
48027738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
48037738613eSIra Weiny 			       struct ib_port_immutable *immutable)
48047738613eSIra Weiny {
48057738613eSIra Weiny 	struct ib_port_attr attr;
4806ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4807ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
4808b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
48097738613eSIra Weiny 	int err;
48107738613eSIra Weiny 
4811c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
48127738613eSIra Weiny 	if (err)
48137738613eSIra Weiny 		return err;
48147738613eSIra Weiny 
4815b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
4816b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
4817b02289b3SArtemy Kovalyov 						   &rep);
4818b02289b3SArtemy Kovalyov 		if (err)
4819b02289b3SArtemy Kovalyov 			return err;
4820b02289b3SArtemy Kovalyov 	}
4821b02289b3SArtemy Kovalyov 
48227738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
48237738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
4824b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
4825ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
4826337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
48277738613eSIra Weiny 
48287738613eSIra Weiny 	return 0;
48297738613eSIra Weiny }
48307738613eSIra Weiny 
48318e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
48328e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
48338e6efa3aSMark Bloch {
48348e6efa3aSMark Bloch 	struct ib_port_attr attr;
48358e6efa3aSMark Bloch 	int err;
48368e6efa3aSMark Bloch 
48378e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
48388e6efa3aSMark Bloch 
48398e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
48408e6efa3aSMark Bloch 	if (err)
48418e6efa3aSMark Bloch 		return err;
48428e6efa3aSMark Bloch 
48438e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
48448e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
48458e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
48468e6efa3aSMark Bloch 
48478e6efa3aSMark Bloch 	return 0;
48488e6efa3aSMark Bloch }
48498e6efa3aSMark Bloch 
48509abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
4851c7342823SIra Weiny {
4852c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
4853c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
48549abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
48559abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
48569abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
4857c7342823SIra Weiny }
4858c7342823SIra Weiny 
485945f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
48609ef9c640SAviv Heller {
48619ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
48629ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
48639ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
48649ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
48659ef9c640SAviv Heller 	int err;
48669ef9c640SAviv Heller 
48679ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
48689ef9c640SAviv Heller 		return 0;
48699ef9c640SAviv Heller 
48709ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
48719ef9c640SAviv Heller 	if (err)
48729ef9c640SAviv Heller 		return err;
48739ef9c640SAviv Heller 
48749ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
48759ef9c640SAviv Heller 	if (IS_ERR(ft)) {
48769ef9c640SAviv Heller 		err = PTR_ERR(ft);
48779ef9c640SAviv Heller 		goto err_destroy_vport_lag;
48789ef9c640SAviv Heller 	}
48799ef9c640SAviv Heller 
48809a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
48819ef9c640SAviv Heller 	return 0;
48829ef9c640SAviv Heller 
48839ef9c640SAviv Heller err_destroy_vport_lag:
48849ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
48859ef9c640SAviv Heller 	return err;
48869ef9c640SAviv Heller }
48879ef9c640SAviv Heller 
488845f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
48899ef9c640SAviv Heller {
48909ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
48919ef9c640SAviv Heller 
48929a4ca38dSMark Bloch 	if (dev->flow_db->lag_demux_ft) {
48939a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
48949a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
48959ef9c640SAviv Heller 
48969ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
48979ef9c640SAviv Heller 	}
48989ef9c640SAviv Heller }
48999ef9c640SAviv Heller 
49007fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
4901fc24fc5eSAchiad Shochat {
4902e53505a8SAchiad Shochat 	int err;
4903e53505a8SAchiad Shochat 
49047fd8aefbSDaniel Jurgens 	dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
49057fd8aefbSDaniel Jurgens 	err = register_netdevice_notifier(&dev->roce[port_num].nb);
49065ec8c83eSAviv Heller 	if (err) {
49077fd8aefbSDaniel Jurgens 		dev->roce[port_num].nb.notifier_call = NULL;
4908e53505a8SAchiad Shochat 		return err;
49095ec8c83eSAviv Heller 	}
4910e53505a8SAchiad Shochat 
4911d012f5d6SOr Gerlitz 	return 0;
4912d012f5d6SOr Gerlitz }
4913d012f5d6SOr Gerlitz 
49147fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
4915e126ba97SEli Cohen {
49167fd8aefbSDaniel Jurgens 	if (dev->roce[port_num].nb.notifier_call) {
49177fd8aefbSDaniel Jurgens 		unregister_netdevice_notifier(&dev->roce[port_num].nb);
49187fd8aefbSDaniel Jurgens 		dev->roce[port_num].nb.notifier_call = NULL;
4919e126ba97SEli Cohen 	}
4920e126ba97SEli Cohen }
4921e126ba97SEli Cohen 
4922e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
4923e126ba97SEli Cohen {
4924e126ba97SEli Cohen 	int err;
4925e126ba97SEli Cohen 
4926ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
4927e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
4928e53505a8SAchiad Shochat 		if (err)
49298e6efa3aSMark Bloch 			return err;
4930ca5b91d6SOr Gerlitz 	}
4931e53505a8SAchiad Shochat 
493245f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
49339ef9c640SAviv Heller 	if (err)
49349ef9c640SAviv Heller 		goto err_disable_roce;
49359ef9c640SAviv Heller 
4936e53505a8SAchiad Shochat 	return 0;
4937e53505a8SAchiad Shochat 
49389ef9c640SAviv Heller err_disable_roce:
4939ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
49409ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
49419ef9c640SAviv Heller 
4942e53505a8SAchiad Shochat 	return err;
4943fc24fc5eSAchiad Shochat }
4944fc24fc5eSAchiad Shochat 
494545f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
4946fc24fc5eSAchiad Shochat {
494745f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
4948ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
4949e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
4950fc24fc5eSAchiad Shochat }
4951fc24fc5eSAchiad Shochat 
4952e1f24a79SParav Pandit struct mlx5_ib_counter {
49537c16f477SKamal Heib 	const char *name;
49547c16f477SKamal Heib 	size_t offset;
49557c16f477SKamal Heib };
49567c16f477SKamal Heib 
49577c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
49587c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
49597c16f477SKamal Heib 
4960e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
49617c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
49627c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
49637c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
49647c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
49657c16f477SKamal Heib };
49667c16f477SKamal Heib 
4967e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
49687c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
49697c16f477SKamal Heib };
49707c16f477SKamal Heib 
4971e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
49727c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
49737c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
49747c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
49757c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
49767c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
49777c16f477SKamal Heib };
49787c16f477SKamal Heib 
4979e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
4980e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
4981e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
4982e1f24a79SParav Pandit 
4983e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
4984e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
4985e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
4986e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
4987e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
4988e1f24a79SParav Pandit };
4989e1f24a79SParav Pandit 
499058dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
499158dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
499258dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
499358dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
499458dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
499558dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
499658dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
499758dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
499858dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
499958dcb60aSParav Pandit };
500058dcb60aSParav Pandit 
50019f876f3dSTalat Batheesh #define INIT_EXT_PPCNT_COUNTER(_name)		\
50029f876f3dSTalat Batheesh 	{ .name = #_name, .offset =	\
50039f876f3dSTalat Batheesh 	MLX5_BYTE_OFF(ppcnt_reg, \
50049f876f3dSTalat Batheesh 		      counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
50059f876f3dSTalat Batheesh 
50069f876f3dSTalat Batheesh static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
50079f876f3dSTalat Batheesh 	INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
50089f876f3dSTalat Batheesh };
50099f876f3dSTalat Batheesh 
5010e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
50110837e86aSMark Bloch {
5012aac4492eSDaniel Jurgens 	int i;
50130837e86aSMark Bloch 
50147c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
5015921c0f5bSParav Pandit 		if (dev->port[i].cnts.set_id_valid)
50160837e86aSMark Bloch 			mlx5_core_dealloc_q_counter(dev->mdev,
5017e1f24a79SParav Pandit 						    dev->port[i].cnts.set_id);
5018e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
5019e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
50207c16f477SKamal Heib 	}
50217c16f477SKamal Heib }
50227c16f477SKamal Heib 
5023e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5024e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
50257c16f477SKamal Heib {
50267c16f477SKamal Heib 	u32 num_counters;
50277c16f477SKamal Heib 
50287c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
50297c16f477SKamal Heib 
50307c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
50317c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
50327c16f477SKamal Heib 
50337c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
50347c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
503558dcb60aSParav Pandit 
503658dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
503758dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
503858dcb60aSParav Pandit 
5039e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
50407c16f477SKamal Heib 
5041e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5042e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5043e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
5044e1f24a79SParav Pandit 	}
50459f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
50469f876f3dSTalat Batheesh 		cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
50479f876f3dSTalat Batheesh 		num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
50489f876f3dSTalat Batheesh 	}
5049e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5050e1f24a79SParav Pandit 	if (!cnts->names)
50517c16f477SKamal Heib 		return -ENOMEM;
50527c16f477SKamal Heib 
5053e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
5054e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
5055e1f24a79SParav Pandit 	if (!cnts->offsets)
50567c16f477SKamal Heib 		goto err_names;
50577c16f477SKamal Heib 
50587c16f477SKamal Heib 	return 0;
50597c16f477SKamal Heib 
50607c16f477SKamal Heib err_names:
5061e1f24a79SParav Pandit 	kfree(cnts->names);
5062aac4492eSDaniel Jurgens 	cnts->names = NULL;
50637c16f477SKamal Heib 	return -ENOMEM;
50647c16f477SKamal Heib }
50657c16f477SKamal Heib 
5066e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
50677c16f477SKamal Heib 				  const char **names,
50687c16f477SKamal Heib 				  size_t *offsets)
50697c16f477SKamal Heib {
50707c16f477SKamal Heib 	int i;
50717c16f477SKamal Heib 	int j = 0;
50727c16f477SKamal Heib 
50737c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
50747c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
50757c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
50767c16f477SKamal Heib 	}
50777c16f477SKamal Heib 
50787c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
50797c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
50807c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
50817c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
50827c16f477SKamal Heib 		}
50837c16f477SKamal Heib 	}
50847c16f477SKamal Heib 
50857c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
50867c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
50877c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
50887c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
50897c16f477SKamal Heib 		}
50907c16f477SKamal Heib 	}
5091e1f24a79SParav Pandit 
509258dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
509358dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
509458dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
509558dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
509658dcb60aSParav Pandit 		}
509758dcb60aSParav Pandit 	}
509858dcb60aSParav Pandit 
5099e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5100e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5101e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
5102e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
5103e1f24a79SParav Pandit 		}
5104e1f24a79SParav Pandit 	}
51059f876f3dSTalat Batheesh 
51069f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
51079f876f3dSTalat Batheesh 		for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
51089f876f3dSTalat Batheesh 			names[j] = ext_ppcnt_cnts[i].name;
51099f876f3dSTalat Batheesh 			offsets[j] = ext_ppcnt_cnts[i].offset;
51109f876f3dSTalat Batheesh 		}
51119f876f3dSTalat Batheesh 	}
51120837e86aSMark Bloch }
51130837e86aSMark Bloch 
5114e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
51150837e86aSMark Bloch {
5116aac4492eSDaniel Jurgens 	int err = 0;
51170837e86aSMark Bloch 	int i;
51180837e86aSMark Bloch 
51190837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
5120aac4492eSDaniel Jurgens 		err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5121aac4492eSDaniel Jurgens 		if (err)
5122aac4492eSDaniel Jurgens 			goto err_alloc;
51237c16f477SKamal Heib 
5124aac4492eSDaniel Jurgens 		mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5125aac4492eSDaniel Jurgens 				      dev->port[i].cnts.offsets);
5126aac4492eSDaniel Jurgens 
5127aac4492eSDaniel Jurgens 		err = mlx5_core_alloc_q_counter(dev->mdev,
5128aac4492eSDaniel Jurgens 						&dev->port[i].cnts.set_id);
5129aac4492eSDaniel Jurgens 		if (err) {
51300837e86aSMark Bloch 			mlx5_ib_warn(dev,
51310837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
5132aac4492eSDaniel Jurgens 				     i + 1, err);
5133aac4492eSDaniel Jurgens 			goto err_alloc;
51340837e86aSMark Bloch 		}
5135aac4492eSDaniel Jurgens 		dev->port[i].cnts.set_id_valid = true;
51360837e86aSMark Bloch 	}
51370837e86aSMark Bloch 
51380837e86aSMark Bloch 	return 0;
51390837e86aSMark Bloch 
5140aac4492eSDaniel Jurgens err_alloc:
5141aac4492eSDaniel Jurgens 	mlx5_ib_dealloc_counters(dev);
5142aac4492eSDaniel Jurgens 	return err;
51430837e86aSMark Bloch }
51440837e86aSMark Bloch 
51450ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
51460ad17a8fSMark Bloch 						    u8 port_num)
51470ad17a8fSMark Bloch {
51487c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
51497c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
51500ad17a8fSMark Bloch 
51510ad17a8fSMark Bloch 	/* We support only per port stats */
51520ad17a8fSMark Bloch 	if (port_num == 0)
51530ad17a8fSMark Bloch 		return NULL;
51540ad17a8fSMark Bloch 
5155e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
5156e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
51579f876f3dSTalat Batheesh 					  port->cnts.num_cong_counters +
51589f876f3dSTalat Batheesh 					  port->cnts.num_ext_ppcnt_counters,
51590ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
51600ad17a8fSMark Bloch }
51610ad17a8fSMark Bloch 
5162aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
5163e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
5164e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
5165e1f24a79SParav Pandit {
5166e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5167e1f24a79SParav Pandit 	void *out;
5168e1f24a79SParav Pandit 	__be32 val;
5169e1f24a79SParav Pandit 	int ret, i;
5170e1f24a79SParav Pandit 
51711b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
5172e1f24a79SParav Pandit 	if (!out)
5173e1f24a79SParav Pandit 		return -ENOMEM;
5174e1f24a79SParav Pandit 
5175aac4492eSDaniel Jurgens 	ret = mlx5_core_query_q_counter(mdev,
5176e1f24a79SParav Pandit 					port->cnts.set_id, 0,
5177e1f24a79SParav Pandit 					out, outlen);
5178e1f24a79SParav Pandit 	if (ret)
5179e1f24a79SParav Pandit 		goto free;
5180e1f24a79SParav Pandit 
5181e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
5182e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
5183e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
5184e1f24a79SParav Pandit 	}
5185e1f24a79SParav Pandit 
5186e1f24a79SParav Pandit free:
5187e1f24a79SParav Pandit 	kvfree(out);
5188e1f24a79SParav Pandit 	return ret;
5189e1f24a79SParav Pandit }
5190e1f24a79SParav Pandit 
51919f876f3dSTalat Batheesh static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
51929f876f3dSTalat Batheesh 					  struct mlx5_ib_port *port,
51939f876f3dSTalat Batheesh 					  struct rdma_hw_stats *stats)
51949f876f3dSTalat Batheesh {
51959f876f3dSTalat Batheesh 	int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
51969f876f3dSTalat Batheesh 	int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
51979f876f3dSTalat Batheesh 	int ret, i;
51989f876f3dSTalat Batheesh 	void *out;
51999f876f3dSTalat Batheesh 
52009f876f3dSTalat Batheesh 	out = kvzalloc(sz, GFP_KERNEL);
52019f876f3dSTalat Batheesh 	if (!out)
52029f876f3dSTalat Batheesh 		return -ENOMEM;
52039f876f3dSTalat Batheesh 
52049f876f3dSTalat Batheesh 	ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
52059f876f3dSTalat Batheesh 	if (ret)
52069f876f3dSTalat Batheesh 		goto free;
52079f876f3dSTalat Batheesh 
52089f876f3dSTalat Batheesh 	for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
52099f876f3dSTalat Batheesh 		stats->value[i + offset] =
52109f876f3dSTalat Batheesh 			be64_to_cpup((__be64 *)(out +
52119f876f3dSTalat Batheesh 				    port->cnts.offsets[i + offset]));
52129f876f3dSTalat Batheesh 	}
52139f876f3dSTalat Batheesh 
52149f876f3dSTalat Batheesh free:
52159f876f3dSTalat Batheesh 	kvfree(out);
52169f876f3dSTalat Batheesh 	return ret;
52179f876f3dSTalat Batheesh }
52189f876f3dSTalat Batheesh 
52190ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
52200ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
52217c16f477SKamal Heib 				u8 port_num, int index)
52220ad17a8fSMark Bloch {
52230ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
52247c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
5225aac4492eSDaniel Jurgens 	struct mlx5_core_dev *mdev;
5226e1f24a79SParav Pandit 	int ret, num_counters;
5227aac4492eSDaniel Jurgens 	u8 mdev_port_num;
52280ad17a8fSMark Bloch 
52297c16f477SKamal Heib 	if (!stats)
5230e1f24a79SParav Pandit 		return -EINVAL;
52310ad17a8fSMark Bloch 
52329f876f3dSTalat Batheesh 	num_counters = port->cnts.num_q_counters +
52339f876f3dSTalat Batheesh 		       port->cnts.num_cong_counters +
52349f876f3dSTalat Batheesh 		       port->cnts.num_ext_ppcnt_counters;
5235aac4492eSDaniel Jurgens 
5236aac4492eSDaniel Jurgens 	/* q_counters are per IB device, query the master mdev */
5237aac4492eSDaniel Jurgens 	ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
52380ad17a8fSMark Bloch 	if (ret)
5239e1f24a79SParav Pandit 		return ret;
52400ad17a8fSMark Bloch 
52419f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
52429f876f3dSTalat Batheesh 		ret =  mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
52439f876f3dSTalat Batheesh 		if (ret)
52449f876f3dSTalat Batheesh 			return ret;
52459f876f3dSTalat Batheesh 	}
52469f876f3dSTalat Batheesh 
5247e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5248aac4492eSDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5249aac4492eSDaniel Jurgens 						    &mdev_port_num);
5250aac4492eSDaniel Jurgens 		if (!mdev) {
5251aac4492eSDaniel Jurgens 			/* If port is not affiliated yet, its in down state
5252aac4492eSDaniel Jurgens 			 * which doesn't have any counters yet, so it would be
5253aac4492eSDaniel Jurgens 			 * zero. So no need to read from the HCA.
5254aac4492eSDaniel Jurgens 			 */
5255aac4492eSDaniel Jurgens 			goto done;
5256aac4492eSDaniel Jurgens 		}
525771a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
525871a0ff65SMajd Dibbiny 						   stats->value +
525971a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters,
526071a0ff65SMajd Dibbiny 						   port->cnts.num_cong_counters,
526171a0ff65SMajd Dibbiny 						   port->cnts.offsets +
526271a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters);
5263aac4492eSDaniel Jurgens 
5264aac4492eSDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
5265e1f24a79SParav Pandit 		if (ret)
5266e1f24a79SParav Pandit 			return ret;
52670ad17a8fSMark Bloch 	}
52687c16f477SKamal Heib 
5269aac4492eSDaniel Jurgens done:
5270e1f24a79SParav Pandit 	return num_counters;
52710ad17a8fSMark Bloch }
52720ad17a8fSMark Bloch 
5273f6a8a19bSDenis Drozdov static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5274693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
5275f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
5276693dfd5aSErez Shitrit {
5277693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
5278f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
5279693dfd5aSErez Shitrit 
5280f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
5281693dfd5aSErez Shitrit }
5282693dfd5aSErez Shitrit 
5283fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5284fe248c3aSMaor Gottlieb {
5285fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
5286fe248c3aSMaor Gottlieb 		return;
5287fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5288fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
5289fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
5290fe248c3aSMaor Gottlieb }
5291fe248c3aSMaor Gottlieb 
529203404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
529303404e8aSMaor Gottlieb {
529403404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
529503404e8aSMaor Gottlieb 		return;
529603404e8aSMaor Gottlieb 
529703404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
5298fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5299fe248c3aSMaor Gottlieb }
5300fe248c3aSMaor Gottlieb 
5301fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5302fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
5303fe248c3aSMaor Gottlieb {
5304fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5305fe248c3aSMaor Gottlieb 	char lbuf[20];
5306fe248c3aSMaor Gottlieb 	int len;
5307fe248c3aSMaor Gottlieb 
5308fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5309fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
5310fe248c3aSMaor Gottlieb }
5311fe248c3aSMaor Gottlieb 
5312fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5313fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
5314fe248c3aSMaor Gottlieb {
5315fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5316fe248c3aSMaor Gottlieb 	u32 timeout;
5317fe248c3aSMaor Gottlieb 	u32 var;
5318fe248c3aSMaor Gottlieb 
5319fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
5320fe248c3aSMaor Gottlieb 		return -EFAULT;
5321fe248c3aSMaor Gottlieb 
5322fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5323fe248c3aSMaor Gottlieb 			1000);
5324fe248c3aSMaor Gottlieb 	if (timeout != var)
5325fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5326fe248c3aSMaor Gottlieb 			    timeout);
5327fe248c3aSMaor Gottlieb 
5328fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
5329fe248c3aSMaor Gottlieb 
5330fe248c3aSMaor Gottlieb 	return count;
5331fe248c3aSMaor Gottlieb }
5332fe248c3aSMaor Gottlieb 
5333fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
5334fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
5335fe248c3aSMaor Gottlieb 	.open	= simple_open,
5336fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
5337fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
5338fe248c3aSMaor Gottlieb };
5339fe248c3aSMaor Gottlieb 
5340fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5341fe248c3aSMaor Gottlieb {
5342fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
5343fe248c3aSMaor Gottlieb 
5344fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
5345fe248c3aSMaor Gottlieb 		return 0;
5346fe248c3aSMaor Gottlieb 
5347fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5348fe248c3aSMaor Gottlieb 	if (!dbg)
5349fe248c3aSMaor Gottlieb 		return -ENOMEM;
5350fe248c3aSMaor Gottlieb 
5351cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
5352cbafad87SSudip Mukherjee 
5353fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
5354fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
5355fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
5356fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
5357cbafad87SSudip Mukherjee 		goto out_debugfs;
5358fe248c3aSMaor Gottlieb 
5359fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
5360fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
5361fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5362fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
5363fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
5364fe248c3aSMaor Gottlieb 		goto out_debugfs;
5365fe248c3aSMaor Gottlieb 
5366fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
5367fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
5368fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5369fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
5370fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
5371fe248c3aSMaor Gottlieb 		goto out_debugfs;
5372fe248c3aSMaor Gottlieb 
5373fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
5374fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
5375fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
5376fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
5377fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
5378fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
5379fe248c3aSMaor Gottlieb 		goto out_debugfs;
5380fe248c3aSMaor Gottlieb 
5381fe248c3aSMaor Gottlieb 	return 0;
5382fe248c3aSMaor Gottlieb 
5383fe248c3aSMaor Gottlieb out_debugfs:
5384fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5385fe248c3aSMaor Gottlieb 	return -ENOMEM;
538603404e8aSMaor Gottlieb }
538703404e8aSMaor Gottlieb 
538803404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
538903404e8aSMaor Gottlieb {
539003404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
539103404e8aSMaor Gottlieb 		return;
539203404e8aSMaor Gottlieb 
539303404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
539403404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
539503404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
539603404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
539703404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
5398fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
5399fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
5400fe248c3aSMaor Gottlieb 
5401fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
5402fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
540303404e8aSMaor Gottlieb }
540403404e8aSMaor Gottlieb 
540532f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
540632f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
540732f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
540832f69e4bSDaniel Jurgens {
540932f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
541032f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
541132f69e4bSDaniel Jurgens 	int comps;
541232f69e4bSDaniel Jurgens 	int err;
541332f69e4bSDaniel Jurgens 	int i;
541432f69e4bSDaniel Jurgens 
5415a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5416a9e546e7SParav Pandit 
541732f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
541832f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
541932f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
542032f69e4bSDaniel Jurgens 		return;
542132f69e4bSDaniel Jurgens 	}
5422df097a27SSaeed Mahameed 
5423df097a27SSaeed Mahameed 	if (mpi->mdev_events.notifier_call)
5424df097a27SSaeed Mahameed 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
5425df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
5426df097a27SSaeed Mahameed 
542732f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
542832f69e4bSDaniel Jurgens 
542932f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
543032f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
543132f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
543232f69e4bSDaniel Jurgens 
543332f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
543432f69e4bSDaniel Jurgens 	if (comps) {
543532f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
543632f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
543732f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
543832f69e4bSDaniel Jurgens 
543932f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
544032f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
544132f69e4bSDaniel Jurgens 
544232f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
544332f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
544432f69e4bSDaniel Jurgens 	}
544532f69e4bSDaniel Jurgens 
544632f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
544732f69e4bSDaniel Jurgens 
544832f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
544932f69e4bSDaniel Jurgens 
545032f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
545132f69e4bSDaniel Jurgens 
545232f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
545332f69e4bSDaniel Jurgens 
545432f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
545532f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
545632f69e4bSDaniel Jurgens 	 * it back to the list.
545732f69e4bSDaniel Jurgens 	 */
545832f69e4bSDaniel Jurgens 	if (err)
545932f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
546032f69e4bSDaniel Jurgens 			    port_num + 1);
546132f69e4bSDaniel Jurgens 
546232f69e4bSDaniel Jurgens 	ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
546332f69e4bSDaniel Jurgens }
546432f69e4bSDaniel Jurgens 
546532f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
546632f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
546732f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
546832f69e4bSDaniel Jurgens {
546932f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
547032f69e4bSDaniel Jurgens 	int err;
547132f69e4bSDaniel Jurgens 
547232f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
547332f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
54742577188eSQing Huang 		mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
547532f69e4bSDaniel Jurgens 			    port_num + 1);
547632f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
547732f69e4bSDaniel Jurgens 		return false;
547832f69e4bSDaniel Jurgens 	}
547932f69e4bSDaniel Jurgens 
548032f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
548132f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
5482df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
548332f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
548432f69e4bSDaniel Jurgens 
548532f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
548632f69e4bSDaniel Jurgens 	if (err)
548732f69e4bSDaniel Jurgens 		goto unbind;
548832f69e4bSDaniel Jurgens 
548932f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
549032f69e4bSDaniel Jurgens 	if (err)
549132f69e4bSDaniel Jurgens 		goto unbind;
549232f69e4bSDaniel Jurgens 
549332f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
549432f69e4bSDaniel Jurgens 	if (err) {
549532f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
549632f69e4bSDaniel Jurgens 			    port_num + 1);
549732f69e4bSDaniel Jurgens 		goto unbind;
549832f69e4bSDaniel Jurgens 	}
549932f69e4bSDaniel Jurgens 
5500df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5501df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5502df097a27SSaeed Mahameed 
5503a9e546e7SParav Pandit 	err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
5504a9e546e7SParav Pandit 	if (err)
5505a9e546e7SParav Pandit 		goto unbind;
5506a9e546e7SParav Pandit 
550732f69e4bSDaniel Jurgens 	return true;
550832f69e4bSDaniel Jurgens 
550932f69e4bSDaniel Jurgens unbind:
551032f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
551132f69e4bSDaniel Jurgens 	return false;
551232f69e4bSDaniel Jurgens }
551332f69e4bSDaniel Jurgens 
551432f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
551532f69e4bSDaniel Jurgens {
551632f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
551732f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
551832f69e4bSDaniel Jurgens 							  port_num + 1);
551932f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
552032f69e4bSDaniel Jurgens 	int err;
552132f69e4bSDaniel Jurgens 	int i;
552232f69e4bSDaniel Jurgens 
552332f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
552432f69e4bSDaniel Jurgens 		return 0;
552532f69e4bSDaniel Jurgens 
552632f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
552732f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
552832f69e4bSDaniel Jurgens 	if (err)
552932f69e4bSDaniel Jurgens 		return err;
553032f69e4bSDaniel Jurgens 
553132f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
553232f69e4bSDaniel Jurgens 	if (err)
553332f69e4bSDaniel Jurgens 		return err;
553432f69e4bSDaniel Jurgens 
553532f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
553632f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
553732f69e4bSDaniel Jurgens 		bool bound = false;
553832f69e4bSDaniel Jurgens 
553932f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
554032f69e4bSDaniel Jurgens 		if (i == port_num) {
554132f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
554232f69e4bSDaniel Jurgens 			if (!mpi) {
554332f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
554432f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
554532f69e4bSDaniel Jurgens 				return -ENOMEM;
554632f69e4bSDaniel Jurgens 			}
554732f69e4bSDaniel Jurgens 
554832f69e4bSDaniel Jurgens 			mpi->is_master = true;
554932f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
555032f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
555132f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
555232f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
555332f69e4bSDaniel Jurgens 			mpi = NULL;
555432f69e4bSDaniel Jurgens 			continue;
555532f69e4bSDaniel Jurgens 		}
555632f69e4bSDaniel Jurgens 
555732f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
555832f69e4bSDaniel Jurgens 				    list) {
555932f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
556032f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
556132f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
556232f69e4bSDaniel Jurgens 			}
556332f69e4bSDaniel Jurgens 
556432f69e4bSDaniel Jurgens 			if (bound) {
556532f69e4bSDaniel Jurgens 				dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
556632f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
556732f69e4bSDaniel Jurgens 				list_del(&mpi->list);
556832f69e4bSDaniel Jurgens 				break;
556932f69e4bSDaniel Jurgens 			}
557032f69e4bSDaniel Jurgens 		}
557132f69e4bSDaniel Jurgens 		if (!bound) {
557232f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
557332f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
557432f69e4bSDaniel Jurgens 				    i + 1);
557532f69e4bSDaniel Jurgens 		}
557632f69e4bSDaniel Jurgens 	}
557732f69e4bSDaniel Jurgens 
557832f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
557932f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
558032f69e4bSDaniel Jurgens 	return err;
558132f69e4bSDaniel Jurgens }
558232f69e4bSDaniel Jurgens 
558332f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
558432f69e4bSDaniel Jurgens {
558532f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
558632f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
558732f69e4bSDaniel Jurgens 							  port_num + 1);
558832f69e4bSDaniel Jurgens 	int i;
558932f69e4bSDaniel Jurgens 
559032f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
559132f69e4bSDaniel Jurgens 		return;
559232f69e4bSDaniel Jurgens 
559332f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
559432f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
559532f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
559632f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
559732f69e4bSDaniel Jurgens 			if (i == port_num) {
559832f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
559932f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
560032f69e4bSDaniel Jurgens 			} else {
560132f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
560232f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
560332f69e4bSDaniel Jurgens 			}
560432f69e4bSDaniel Jurgens 		}
560532f69e4bSDaniel Jurgens 	}
560632f69e4bSDaniel Jurgens 
560732f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
560832f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
560932f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
561032f69e4bSDaniel Jurgens 
561132f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
561232f69e4bSDaniel Jurgens }
561332f69e4bSDaniel Jurgens 
56149a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
56159a119cd5SJason Gunthorpe 	mlx5_ib_dm,
56169a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
561724da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
56189a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
561924da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
562083bb4442SJason Gunthorpe 			    UA_MANDATORY),
56219a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
562224da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
562383bb4442SJason Gunthorpe 			    UA_MANDATORY));
562424da0016SAriel Levkovich 
56259a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
56269a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
56279a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
5628c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
5629bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
5630bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
5631c6475a0bSAviad Yehezkel 
56320cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
56330cbf432dSJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
563436e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
56350cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
56360cbf432dSJason Gunthorpe #endif
56370cbf432dSJason Gunthorpe 
56380cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
56390cbf432dSJason Gunthorpe 				&mlx5_ib_flow_action),
56400cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
56410cbf432dSJason Gunthorpe 	{}
56420cbf432dSJason Gunthorpe };
56430cbf432dSJason Gunthorpe 
56441a1e03dcSRaed Salem static int mlx5_ib_read_counters(struct ib_counters *counters,
56451a1e03dcSRaed Salem 				 struct ib_counters_read_attr *read_attr,
56461a1e03dcSRaed Salem 				 struct uverbs_attr_bundle *attrs)
56471a1e03dcSRaed Salem {
56481a1e03dcSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
56491a1e03dcSRaed Salem 	struct mlx5_read_counters_attr mread_attr = {};
56501a1e03dcSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc;
56511a1e03dcSRaed Salem 	int ret, i;
56521a1e03dcSRaed Salem 
56531a1e03dcSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
56541a1e03dcSRaed Salem 	if (mcounters->cntrs_max_index > read_attr->ncounters) {
56551a1e03dcSRaed Salem 		ret = -EINVAL;
56561a1e03dcSRaed Salem 		goto err_bound;
56571a1e03dcSRaed Salem 	}
56581a1e03dcSRaed Salem 
56591a1e03dcSRaed Salem 	mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
56601a1e03dcSRaed Salem 				 GFP_KERNEL);
56611a1e03dcSRaed Salem 	if (!mread_attr.out) {
56621a1e03dcSRaed Salem 		ret = -ENOMEM;
56631a1e03dcSRaed Salem 		goto err_bound;
56641a1e03dcSRaed Salem 	}
56651a1e03dcSRaed Salem 
56661a1e03dcSRaed Salem 	mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
56671a1e03dcSRaed Salem 	mread_attr.flags = read_attr->flags;
56681a1e03dcSRaed Salem 	ret = mcounters->read_counters(counters->device, &mread_attr);
56691a1e03dcSRaed Salem 	if (ret)
56701a1e03dcSRaed Salem 		goto err_read;
56711a1e03dcSRaed Salem 
56721a1e03dcSRaed Salem 	/* do the pass over the counters data array to assign according to the
56731a1e03dcSRaed Salem 	 * descriptions and indexing pairs
56741a1e03dcSRaed Salem 	 */
56751a1e03dcSRaed Salem 	desc = mcounters->counters_data;
56761a1e03dcSRaed Salem 	for (i = 0; i < mcounters->ncounters; i++)
56771a1e03dcSRaed Salem 		read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
56781a1e03dcSRaed Salem 
56791a1e03dcSRaed Salem err_read:
56801a1e03dcSRaed Salem 	kfree(mread_attr.out);
56811a1e03dcSRaed Salem err_bound:
56821a1e03dcSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
56831a1e03dcSRaed Salem 	return ret;
56841a1e03dcSRaed Salem }
56851a1e03dcSRaed Salem 
5686b29e2a13SRaed Salem static int mlx5_ib_destroy_counters(struct ib_counters *counters)
5687b29e2a13SRaed Salem {
5688b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5689b29e2a13SRaed Salem 
56903b3233fbSRaed Salem 	counters_clear_description(counters);
56913b3233fbSRaed Salem 	if (mcounters->hw_cntrs_hndl)
56923b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(counters->device)->mdev,
56933b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
56943b3233fbSRaed Salem 
5695b29e2a13SRaed Salem 	kfree(mcounters);
5696b29e2a13SRaed Salem 
5697b29e2a13SRaed Salem 	return 0;
5698b29e2a13SRaed Salem }
5699b29e2a13SRaed Salem 
5700b29e2a13SRaed Salem static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
5701b29e2a13SRaed Salem 						   struct uverbs_attr_bundle *attrs)
5702b29e2a13SRaed Salem {
5703b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters;
5704b29e2a13SRaed Salem 
5705b29e2a13SRaed Salem 	mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
5706b29e2a13SRaed Salem 	if (!mcounters)
5707b29e2a13SRaed Salem 		return ERR_PTR(-ENOMEM);
5708b29e2a13SRaed Salem 
57093b3233fbSRaed Salem 	mutex_init(&mcounters->mcntrs_mutex);
57103b3233fbSRaed Salem 
5711b29e2a13SRaed Salem 	return &mcounters->ibcntrs;
5712b29e2a13SRaed Salem }
5713b29e2a13SRaed Salem 
5714b5ca15adSMark Bloch void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
5715e126ba97SEli Cohen {
571632f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
57173cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
57183cc297dbSMark Bloch 	cleanup_srcu_struct(&dev->mr_srcu);
5719813e90b1SMoni Shoua 	drain_workqueue(dev->advise_mr_wq);
5720813e90b1SMoni Shoua 	destroy_workqueue(dev->advise_mr_wq);
57213cc297dbSMark Bloch #endif
572216c1975fSMark Bloch 	kfree(dev->port);
572316c1975fSMark Bloch }
572416c1975fSMark Bloch 
5725b5ca15adSMark Bloch int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
572616c1975fSMark Bloch {
572716c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
5728e126ba97SEli Cohen 	int err;
572932f69e4bSDaniel Jurgens 	int i;
5730e126ba97SEli Cohen 
5731508562d6SDaniel Jurgens 	dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
57320837e86aSMark Bloch 			    GFP_KERNEL);
57330837e86aSMark Bloch 	if (!dev->port)
573416c1975fSMark Bloch 		return -ENOMEM;
57350837e86aSMark Bloch 
573632f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
573732f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
573832f69e4bSDaniel Jurgens 		rwlock_init(&dev->roce[i].netdev_lock);
573932f69e4bSDaniel Jurgens 	}
574032f69e4bSDaniel Jurgens 
574132f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
5742e126ba97SEli Cohen 	if (err)
57430837e86aSMark Bloch 		goto err_free_port;
5744e126ba97SEli Cohen 
574532f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
574632f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
574732f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
574832f69e4bSDaniel Jurgens 			if (err)
574932f69e4bSDaniel Jurgens 				break;
575032f69e4bSDaniel Jurgens 		}
575132f69e4bSDaniel Jurgens 	} else {
575232f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
575332f69e4bSDaniel Jurgens 	}
575432f69e4bSDaniel Jurgens 	if (err)
575532f69e4bSDaniel Jurgens 		goto err_mp;
575632f69e4bSDaniel Jurgens 
57571b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
5758e126ba97SEli Cohen 		get_ext_port_caps(dev);
5759e126ba97SEli Cohen 
5760e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
5761e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
5762c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
5763e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
5764f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
57659b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
5766e126ba97SEli Cohen 
57673cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
57683cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
57693cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
57703cc297dbSMark Bloch 
577124da0016SAriel Levkovich 	spin_lock_init(&dev->memic.memic_lock);
577224da0016SAriel Levkovich 	dev->memic.dev = mdev;
577324da0016SAriel Levkovich 
57743cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5775813e90b1SMoni Shoua 	dev->advise_mr_wq = alloc_ordered_workqueue("mlx5_ib_advise_mr_wq", 0);
5776813e90b1SMoni Shoua 	if (!dev->advise_mr_wq) {
5777813e90b1SMoni Shoua 		err = -ENOMEM;
5778813e90b1SMoni Shoua 		goto err_free_port;
5779813e90b1SMoni Shoua 	}
5780813e90b1SMoni Shoua 
57813cc297dbSMark Bloch 	err = init_srcu_struct(&dev->mr_srcu);
57823cc297dbSMark Bloch 	if (err)
57833cc297dbSMark Bloch 		goto err_free_port;
57843cc297dbSMark Bloch #endif
57853cc297dbSMark Bloch 
578616c1975fSMark Bloch 	return 0;
578732f69e4bSDaniel Jurgens err_mp:
578832f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
578916c1975fSMark Bloch 
579016c1975fSMark Bloch err_free_port:
579116c1975fSMark Bloch 	kfree(dev->port);
579216c1975fSMark Bloch 
579316c1975fSMark Bloch 	return -ENOMEM;
579416c1975fSMark Bloch }
579516c1975fSMark Bloch 
57969a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
57979a4ca38dSMark Bloch {
57989a4ca38dSMark Bloch 	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
57999a4ca38dSMark Bloch 
58009a4ca38dSMark Bloch 	if (!dev->flow_db)
58019a4ca38dSMark Bloch 		return -ENOMEM;
58029a4ca38dSMark Bloch 
58039a4ca38dSMark Bloch 	mutex_init(&dev->flow_db->lock);
58049a4ca38dSMark Bloch 
58059a4ca38dSMark Bloch 	return 0;
58069a4ca38dSMark Bloch }
58079a4ca38dSMark Bloch 
5808b5ca15adSMark Bloch int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
5809b5ca15adSMark Bloch {
5810b5ca15adSMark Bloch 	struct mlx5_ib_dev *nic_dev;
5811b5ca15adSMark Bloch 
5812b5ca15adSMark Bloch 	nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
5813b5ca15adSMark Bloch 
5814b5ca15adSMark Bloch 	if (!nic_dev)
5815b5ca15adSMark Bloch 		return -EINVAL;
5816b5ca15adSMark Bloch 
5817b5ca15adSMark Bloch 	dev->flow_db = nic_dev->flow_db;
5818b5ca15adSMark Bloch 
5819b5ca15adSMark Bloch 	return 0;
5820b5ca15adSMark Bloch }
5821b5ca15adSMark Bloch 
58229a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
58239a4ca38dSMark Bloch {
58249a4ca38dSMark Bloch 	kfree(dev->flow_db);
58259a4ca38dSMark Bloch }
58269a4ca38dSMark Bloch 
582796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
582896458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
582996458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
583096458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
583196458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
583296458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
583396458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
583496458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
583596458233SKamal Heib 	.create_counters = mlx5_ib_create_counters,
583696458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
583796458233SKamal Heib 	.create_flow = mlx5_ib_create_flow,
583896458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
583996458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
584096458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
584196458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
584296458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
584396458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
584496458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
584596458233SKamal Heib 	.destroy_counters = mlx5_ib_destroy_counters,
584696458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
584796458233SKamal Heib 	.destroy_flow = mlx5_ib_destroy_flow,
584896458233SKamal Heib 	.destroy_flow_action = mlx5_ib_destroy_flow_action,
584996458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
585096458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
585196458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
585296458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
585396458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
585496458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
585596458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
585696458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
585796458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
585896458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
585996458233SKamal Heib 	.mmap = mlx5_ib_mmap,
586096458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
586196458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
586296458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
586396458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
586496458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
586596458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
586696458233SKamal Heib 	.post_recv = mlx5_ib_post_recv,
586796458233SKamal Heib 	.post_send = mlx5_ib_post_send,
586896458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
586996458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
587096458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
587196458233SKamal Heib 	.query_device = mlx5_ib_query_device,
587296458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
587396458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
587496458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
587596458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
587696458233SKamal Heib 	.read_counters = mlx5_ib_read_counters,
587796458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
587896458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
587996458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
588096458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
588196458233SKamal Heib };
588296458233SKamal Heib 
588396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
588496458233SKamal Heib 	.create_flow_action_esp = mlx5_ib_create_flow_action_esp,
588596458233SKamal Heib 	.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
588696458233SKamal Heib };
588796458233SKamal Heib 
588896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
588996458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
589096458233SKamal Heib };
589196458233SKamal Heib 
589296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
589396458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
589496458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
589596458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
589696458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
589796458233SKamal Heib };
589896458233SKamal Heib 
589996458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
590096458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
590196458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
590296458233SKamal Heib };
590396458233SKamal Heib 
590496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
590596458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
590696458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
590796458233SKamal Heib };
590896458233SKamal Heib 
590996458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
591096458233SKamal Heib 	.alloc_dm = mlx5_ib_alloc_dm,
591196458233SKamal Heib 	.dealloc_dm = mlx5_ib_dealloc_dm,
591296458233SKamal Heib 	.reg_dm_mr = mlx5_ib_reg_dm_mr,
591396458233SKamal Heib };
591496458233SKamal Heib 
5915b5ca15adSMark Bloch int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
591616c1975fSMark Bloch {
591716c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
591816c1975fSMark Bloch 	int err;
591916c1975fSMark Bloch 
5920e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
5921e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
5922e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
5923e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
5924e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
5925e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
5926e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
592741c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
592841c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
5929e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
593056e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
5931e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
5932e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
5933e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
5934e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
5935e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
5936e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
5937e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
5938e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
5939e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
5940e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
5941e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
5942e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
5943e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
5944e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
5945e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
5946e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
5947e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
59481707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
5949d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
5950d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
59517d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
5952b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
595396458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ)	|
595496458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW)	|
595596458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
5956e126ba97SEli Cohen 
5957f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
5958f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
595996458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
596096458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
59618e959601SNiranjana Vishwanathapura 
596296458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
596396458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
59647c2344c3SMaor Gottlieb 
59656e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
59666e8484c5SMax Gurtovoy 
5967d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
5968d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
5969d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
5970d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
597196458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
5972d2370e0aSMatan Barak 	}
5973d2370e0aSMatan Barak 
5974938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
5975e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
5976e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
5977e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
597896458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
5979e126ba97SEli Cohen 	}
5980e126ba97SEli Cohen 
598196458233SKamal Heib 	if (MLX5_CAP_DEV_MEM(mdev, memic))
598296458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
598324da0016SAriel Levkovich 
5984dfb631a1SJason Gunthorpe 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
598596458233SKamal Heib 	    MLX5_ACCEL_IPSEC_CAP_DEVICE)
598696458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
59870ede73bcSMatan Barak 	dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
598896458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
598981e30880SYishai Hadas 
599036e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
599136e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
599236e235c8SJason Gunthorpe 
5993e126ba97SEli Cohen 	err = init_node_data(dev);
5994e126ba97SEli Cohen 	if (err)
599516c1975fSMark Bloch 		return err;
5996e126ba97SEli Cohen 
5997c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
5998e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
5999e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
6000a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
6001c8b89924SMark Bloch 
600216c1975fSMark Bloch 	return 0;
600316c1975fSMark Bloch }
600416c1975fSMark Bloch 
600596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
600696458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
600796458233SKamal Heib 	.query_port = mlx5_ib_query_port,
600896458233SKamal Heib };
600996458233SKamal Heib 
60108e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
601116c1975fSMark Bloch {
601296458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
60138e6efa3aSMark Bloch 	return 0;
60148e6efa3aSMark Bloch }
60158e6efa3aSMark Bloch 
601696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
601796458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
601896458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
601996458233SKamal Heib };
602096458233SKamal Heib 
6021b5ca15adSMark Bloch int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
60228e6efa3aSMark Bloch {
602396458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
60248e6efa3aSMark Bloch 	return 0;
60258e6efa3aSMark Bloch }
60268e6efa3aSMark Bloch 
602796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
602896458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
602996458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
603096458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
603196458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
603296458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
603396458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
603496458233SKamal Heib };
603596458233SKamal Heib 
6036e3f1ed1fSLeon Romanovsky static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
60378e6efa3aSMark Bloch {
6038e3f1ed1fSLeon Romanovsky 	u8 port_num;
60397fd8aefbSDaniel Jurgens 	int i;
604016c1975fSMark Bloch 
60417fd8aefbSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
60427fd8aefbSDaniel Jurgens 		dev->roce[i].dev = dev;
60437fd8aefbSDaniel Jurgens 		dev->roce[i].native_port_num = i + 1;
60447fd8aefbSDaniel Jurgens 		dev->roce[i].last_port_state = IB_PORT_DOWN;
60457fd8aefbSDaniel Jurgens 	}
60467fd8aefbSDaniel Jurgens 
6047c11a226aSMark Bloch 	dev->ib_dev.uverbs_ex_cmd_mask |=
6048c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6049c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6050c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6051c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6052c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
605396458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
60548e6efa3aSMark Bloch 
6055e3f1ed1fSLeon Romanovsky 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6056e3f1ed1fSLeon Romanovsky 
60578e6efa3aSMark Bloch 	return mlx5_add_netdev_notifier(dev, port_num);
60588e6efa3aSMark Bloch }
60598e6efa3aSMark Bloch 
60608e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
60618e6efa3aSMark Bloch {
60628e6efa3aSMark Bloch 	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
60638e6efa3aSMark Bloch 
60648e6efa3aSMark Bloch 	mlx5_remove_netdev_notifier(dev, port_num);
60658e6efa3aSMark Bloch }
60668e6efa3aSMark Bloch 
60678e6efa3aSMark Bloch int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
60688e6efa3aSMark Bloch {
60698e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
60708e6efa3aSMark Bloch 	enum rdma_link_layer ll;
60718e6efa3aSMark Bloch 	int port_type_cap;
60728e6efa3aSMark Bloch 	int err = 0;
60738e6efa3aSMark Bloch 
60748e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
60758e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
60768e6efa3aSMark Bloch 
60778e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET)
6078e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
60798e6efa3aSMark Bloch 
608016c1975fSMark Bloch 	return err;
6081fc24fc5eSAchiad Shochat }
6082fc24fc5eSAchiad Shochat 
60838e6efa3aSMark Bloch void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
60848e6efa3aSMark Bloch {
60858e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
60868e6efa3aSMark Bloch }
60878e6efa3aSMark Bloch 
60888e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
60898e6efa3aSMark Bloch {
60908e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
60918e6efa3aSMark Bloch 	enum rdma_link_layer ll;
60928e6efa3aSMark Bloch 	int port_type_cap;
60938e6efa3aSMark Bloch 	int err;
60948e6efa3aSMark Bloch 
60958e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
60968e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
60978e6efa3aSMark Bloch 
60988e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
6099e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
61008e6efa3aSMark Bloch 		if (err)
61018e6efa3aSMark Bloch 			return err;
61028e6efa3aSMark Bloch 
6103e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
61048e6efa3aSMark Bloch 		if (err)
61058e6efa3aSMark Bloch 			goto cleanup;
61068e6efa3aSMark Bloch 	}
61078e6efa3aSMark Bloch 
610816c1975fSMark Bloch 	return 0;
61098e6efa3aSMark Bloch cleanup:
61108e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
61118e6efa3aSMark Bloch 
61128e6efa3aSMark Bloch 	return err;
611345bded2cSKamal Heib }
61146aec21f6SHaggai Eran 
611516c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
611616c1975fSMark Bloch {
611716c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
611816c1975fSMark Bloch 	enum rdma_link_layer ll;
611916c1975fSMark Bloch 	int port_type_cap;
61204a2da0b8SParav Pandit 
612116c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
612216c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
612316c1975fSMark Bloch 
612416c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
612516c1975fSMark Bloch 		mlx5_disable_eth(dev);
61268e6efa3aSMark Bloch 		mlx5_ib_stage_common_roce_cleanup(dev);
612716c1975fSMark Bloch 	}
612816c1975fSMark Bloch }
612916c1975fSMark Bloch 
6130b5ca15adSMark Bloch int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
613116c1975fSMark Bloch {
613216c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
613316c1975fSMark Bloch }
613416c1975fSMark Bloch 
6135b5ca15adSMark Bloch void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
613616c1975fSMark Bloch {
613716c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
613816c1975fSMark Bloch }
613916c1975fSMark Bloch 
614016c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
614116c1975fSMark Bloch {
614207321b3cSMark Bloch 	mlx5_ib_internal_fill_odp_caps(dev);
614307321b3cSMark Bloch 
614416c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
614516c1975fSMark Bloch }
614616c1975fSMark Bloch 
6147d5d284b8SSaeed Mahameed void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
6148d5d284b8SSaeed Mahameed {
6149d5d284b8SSaeed Mahameed 	mlx5_ib_odp_cleanup_one(dev);
6150d5d284b8SSaeed Mahameed }
6151d5d284b8SSaeed Mahameed 
615296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
615396458233SKamal Heib 	.alloc_hw_stats = mlx5_ib_alloc_hw_stats,
615496458233SKamal Heib 	.get_hw_stats = mlx5_ib_get_hw_stats,
615596458233SKamal Heib };
615696458233SKamal Heib 
6157b5ca15adSMark Bloch int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
615816c1975fSMark Bloch {
61595e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
616096458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
61615e1e7612SMark Bloch 
616216c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
61635e1e7612SMark Bloch 	}
616416c1975fSMark Bloch 
616516c1975fSMark Bloch 	return 0;
616616c1975fSMark Bloch }
616716c1975fSMark Bloch 
6168b5ca15adSMark Bloch void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
616916c1975fSMark Bloch {
617016c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
617116c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
617216c1975fSMark Bloch }
617316c1975fSMark Bloch 
617416c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
617516c1975fSMark Bloch {
6176a9e546e7SParav Pandit 	return mlx5_ib_init_cong_debugfs(dev,
6177a9e546e7SParav Pandit 					 mlx5_core_native_port_num(dev->mdev) - 1);
617816c1975fSMark Bloch }
617916c1975fSMark Bloch 
618016c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
618116c1975fSMark Bloch {
6182a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
6183a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
618416c1975fSMark Bloch }
618516c1975fSMark Bloch 
618616c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
618716c1975fSMark Bloch {
61885fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
6189444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
619016c1975fSMark Bloch }
619116c1975fSMark Bloch 
619216c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
619316c1975fSMark Bloch {
619416c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
619516c1975fSMark Bloch }
619616c1975fSMark Bloch 
6197b5ca15adSMark Bloch int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
619816c1975fSMark Bloch {
619916c1975fSMark Bloch 	int err;
62005fe9dec0SEli Cohen 
62015fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
62025fe9dec0SEli Cohen 	if (err)
620316c1975fSMark Bloch 		return err;
62045fe9dec0SEli Cohen 
62055fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
62065fe9dec0SEli Cohen 	if (err)
620716c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
62085fe9dec0SEli Cohen 
620916c1975fSMark Bloch 	return err;
621016c1975fSMark Bloch }
62110837e86aSMark Bloch 
6212b5ca15adSMark Bloch void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
621316c1975fSMark Bloch {
621416c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
621516c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
621616c1975fSMark Bloch }
6217e126ba97SEli Cohen 
6218b5ca15adSMark Bloch int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
621916c1975fSMark Bloch {
6220e349f858SJason Gunthorpe 	const char *name;
6221e349f858SJason Gunthorpe 
6222508a523fSParav Pandit 	rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
6223e349f858SJason Gunthorpe 	if (!mlx5_lag_is_active(dev->mdev))
6224e349f858SJason Gunthorpe 		name = "mlx5_%d";
6225e349f858SJason Gunthorpe 	else
6226e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
6227e349f858SJason Gunthorpe 	return ib_register_device(&dev->ib_dev, name, NULL);
622816c1975fSMark Bloch }
622916c1975fSMark Bloch 
623003fe2debSDavid S. Miller void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
623142cea83fSMark Bloch {
623242cea83fSMark Bloch 	destroy_umrc_res(dev);
623342cea83fSMark Bloch }
623442cea83fSMark Bloch 
6235b5ca15adSMark Bloch void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
623616c1975fSMark Bloch {
623716c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
623816c1975fSMark Bloch }
623916c1975fSMark Bloch 
624003fe2debSDavid S. Miller int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
624116c1975fSMark Bloch {
624216c1975fSMark Bloch 	return create_umr_res(dev);
624316c1975fSMark Bloch }
624416c1975fSMark Bloch 
624516c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
624616c1975fSMark Bloch {
624703404e8aSMaor Gottlieb 	init_delay_drop(dev);
624803404e8aSMaor Gottlieb 
624916c1975fSMark Bloch 	return 0;
625016c1975fSMark Bloch }
625116c1975fSMark Bloch 
625216c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
625316c1975fSMark Bloch {
625416c1975fSMark Bloch 	cancel_delay_drop(dev);
625516c1975fSMark Bloch }
625616c1975fSMark Bloch 
6257df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
6258df097a27SSaeed Mahameed {
6259df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
6260df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
6261df097a27SSaeed Mahameed 	return 0;
6262df097a27SSaeed Mahameed }
6263df097a27SSaeed Mahameed 
6264df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
6265df097a27SSaeed Mahameed {
6266df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
6267df097a27SSaeed Mahameed }
6268df097a27SSaeed Mahameed 
626981773ce5SLeon Romanovsky static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
627081773ce5SLeon Romanovsky {
627181773ce5SLeon Romanovsky 	int uid;
627281773ce5SLeon Romanovsky 
6273fb98153bSYishai Hadas 	uid = mlx5_ib_devx_create(dev, false);
627481773ce5SLeon Romanovsky 	if (uid > 0)
627581773ce5SLeon Romanovsky 		dev->devx_whitelist_uid = uid;
627681773ce5SLeon Romanovsky 
627781773ce5SLeon Romanovsky 	return 0;
627881773ce5SLeon Romanovsky }
627981773ce5SLeon Romanovsky static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
628081773ce5SLeon Romanovsky {
628181773ce5SLeon Romanovsky 	if (dev->devx_whitelist_uid)
628281773ce5SLeon Romanovsky 		mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
628381773ce5SLeon Romanovsky }
628481773ce5SLeon Romanovsky 
6285b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
628616c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
628716c1975fSMark Bloch 		      int stage)
628816c1975fSMark Bloch {
628916c1975fSMark Bloch 	/* Number of stages to cleanup */
629016c1975fSMark Bloch 	while (stage) {
629116c1975fSMark Bloch 		stage--;
629216c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
629316c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
629416c1975fSMark Bloch 	}
629516c1975fSMark Bloch }
629616c1975fSMark Bloch 
6297b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
629816c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
629916c1975fSMark Bloch {
630016c1975fSMark Bloch 	int err;
630116c1975fSMark Bloch 	int i;
630216c1975fSMark Bloch 
630316c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
630416c1975fSMark Bloch 		if (profile->stage[i].init) {
630516c1975fSMark Bloch 			err = profile->stage[i].init(dev);
630616c1975fSMark Bloch 			if (err)
630716c1975fSMark Bloch 				goto err_out;
630816c1975fSMark Bloch 		}
630916c1975fSMark Bloch 	}
631016c1975fSMark Bloch 
631116c1975fSMark Bloch 	dev->profile = profile;
6312e126ba97SEli Cohen 	dev->ib_active = true;
6313e126ba97SEli Cohen 
63149603b61dSJack Morgenstein 	return dev;
6315e126ba97SEli Cohen 
631616c1975fSMark Bloch err_out:
631716c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
6318e126ba97SEli Cohen 
63199603b61dSJack Morgenstein 	return NULL;
6320e126ba97SEli Cohen }
6321e126ba97SEli Cohen 
632216c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
632316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
632416c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
632516c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
63269a4ca38dSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
63279a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_init,
63289a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
632916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
633016c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
633116c1975fSMark Bloch 		     NULL),
63328e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
63338e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
63348e6efa3aSMark Bloch 		     NULL),
633516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
633616c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
633716c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
6338f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6339f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
6340f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
634116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
634216c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
634316c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
6344df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6345df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
6346df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
634716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
634816c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
6349d5d284b8SSaeed Mahameed 		     mlx5_ib_stage_odp_cleanup),
635016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
635116c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
635216c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
635316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
635416c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
635516c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
635616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
635716c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
635816c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
635916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
636016c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
636116c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
636242cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
636342cea83fSMark Bloch 		     NULL,
636442cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
636581773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
636681773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_init,
636781773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_cleanup),
636816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
636916c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
637016c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
637142cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
637242cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
637342cea83fSMark Bloch 		     NULL),
637416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
637516c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
637616c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
637716c1975fSMark Bloch };
637816c1975fSMark Bloch 
6379b5ca15adSMark Bloch static const struct mlx5_ib_profile nic_rep_profile = {
6380b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
6381b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
6382b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
6383b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6384b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_init,
6385b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
6386b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6387b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
6388b5ca15adSMark Bloch 		     NULL),
6389b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6390b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_non_default_cb,
6391b5ca15adSMark Bloch 		     NULL),
6392b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6393b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_init,
6394b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_cleanup),
6395f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6396f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
6397f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
6398b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6399b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_init,
6400b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
6401df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6402df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
6403df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
6404b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6405b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_init,
6406b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
6407b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
6408b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
6409b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
6410b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6411b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
6412b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
641303fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
641403fe2debSDavid S. Miller 		     NULL,
641503fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
6416b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6417b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
6418b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
641903fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
642003fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
642103fe2debSDavid S. Miller 		     NULL),
6422b5ca15adSMark Bloch };
6423b5ca15adSMark Bloch 
6424e3f1ed1fSLeon Romanovsky static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
642532f69e4bSDaniel Jurgens {
642632f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
642732f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
642832f69e4bSDaniel Jurgens 	bool bound = false;
642932f69e4bSDaniel Jurgens 	int err;
643032f69e4bSDaniel Jurgens 
643132f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
643232f69e4bSDaniel Jurgens 	if (!mpi)
643332f69e4bSDaniel Jurgens 		return NULL;
643432f69e4bSDaniel Jurgens 
643532f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
643632f69e4bSDaniel Jurgens 
643732f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
643832f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
643932f69e4bSDaniel Jurgens 	if (err) {
644032f69e4bSDaniel Jurgens 		kfree(mpi);
644132f69e4bSDaniel Jurgens 		return NULL;
644232f69e4bSDaniel Jurgens 	}
644332f69e4bSDaniel Jurgens 
644432f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
644532f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
644632f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
644732f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
644832f69e4bSDaniel Jurgens 
644932f69e4bSDaniel Jurgens 		if (bound) {
645032f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
645132f69e4bSDaniel Jurgens 			break;
645232f69e4bSDaniel Jurgens 		}
645332f69e4bSDaniel Jurgens 	}
645432f69e4bSDaniel Jurgens 
645532f69e4bSDaniel Jurgens 	if (!bound) {
645632f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
645732f69e4bSDaniel Jurgens 		dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
645832f69e4bSDaniel Jurgens 	}
645932f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
646032f69e4bSDaniel Jurgens 
646132f69e4bSDaniel Jurgens 	return mpi;
646232f69e4bSDaniel Jurgens }
646332f69e4bSDaniel Jurgens 
646416c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
646516c1975fSMark Bloch {
646632f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
6467b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
646832f69e4bSDaniel Jurgens 	int port_type_cap;
646932f69e4bSDaniel Jurgens 
6470b5ca15adSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
6471b5ca15adSMark Bloch 
647232f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
647332f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
647432f69e4bSDaniel Jurgens 
6475e3f1ed1fSLeon Romanovsky 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6476e3f1ed1fSLeon Romanovsky 		return mlx5_ib_add_slave_port(mdev);
647732f69e4bSDaniel Jurgens 
6478b5ca15adSMark Bloch 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
6479b5ca15adSMark Bloch 	if (!dev)
6480b5ca15adSMark Bloch 		return NULL;
6481b5ca15adSMark Bloch 
6482b5ca15adSMark Bloch 	dev->mdev = mdev;
6483b5ca15adSMark Bloch 	dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6484b5ca15adSMark Bloch 			     MLX5_CAP_GEN(mdev, num_vhca_ports));
6485b5ca15adSMark Bloch 
6486aff2252aSOr Gerlitz 	if (MLX5_ESWITCH_MANAGER(mdev) &&
6487b5ca15adSMark Bloch 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
6488b5ca15adSMark Bloch 		dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0);
648906cc74afSMark Bloch 		dev->profile = &nic_rep_profile;
649006cc74afSMark Bloch 		mlx5_ib_register_vport_reps(dev);
649106cc74afSMark Bloch 		return dev;
6492b5ca15adSMark Bloch 	}
6493b5ca15adSMark Bloch 
6494b5ca15adSMark Bloch 	return __mlx5_ib_add(dev, &pf_profile);
649516c1975fSMark Bloch }
649616c1975fSMark Bloch 
64979603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
6498e126ba97SEli Cohen {
649932f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
650032f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
65016aec21f6SHaggai Eran 
650232f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
650332f69e4bSDaniel Jurgens 		mpi = context;
650432f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
650532f69e4bSDaniel Jurgens 		if (mpi->ibdev)
650632f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
650732f69e4bSDaniel Jurgens 		list_del(&mpi->list);
650832f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
650932f69e4bSDaniel Jurgens 		return;
651032f69e4bSDaniel Jurgens 	}
651132f69e4bSDaniel Jurgens 
651232f69e4bSDaniel Jurgens 	dev = context;
651306cc74afSMark Bloch 	if (dev->profile == &nic_rep_profile)
651406cc74afSMark Bloch 		mlx5_ib_unregister_vport_reps(dev);
651506cc74afSMark Bloch 	else
651616c1975fSMark Bloch 		__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
651706cc74afSMark Bloch 
651806cc74afSMark Bloch 	ib_dealloc_device((struct ib_device *)dev);
6519e126ba97SEli Cohen }
6520e126ba97SEli Cohen 
65219603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
65229603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
65239603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
652464613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
6525e126ba97SEli Cohen };
6526e126ba97SEli Cohen 
6527c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
6528c44ef998SIlya Lesokhin {
6529c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
6530c44ef998SIlya Lesokhin 	return xlt_emergency_page;
6531c44ef998SIlya Lesokhin }
6532c44ef998SIlya Lesokhin 
6533c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
6534c44ef998SIlya Lesokhin {
6535c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
6536c44ef998SIlya Lesokhin }
6537c44ef998SIlya Lesokhin 
6538e126ba97SEli Cohen static int __init mlx5_ib_init(void)
6539e126ba97SEli Cohen {
65406aec21f6SHaggai Eran 	int err;
65416aec21f6SHaggai Eran 
6542c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
6543c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
6544d69a24e0SDaniel Jurgens 		return -ENOMEM;
6545d69a24e0SDaniel Jurgens 
6546c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
6547c44ef998SIlya Lesokhin 
6548c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6549c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
6550c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
6551c44ef998SIlya Lesokhin 		return -ENOMEM;
6552c44ef998SIlya Lesokhin 	}
6553c44ef998SIlya Lesokhin 
655481713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
65559603b61dSJack Morgenstein 
65566aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
65576aec21f6SHaggai Eran 
65586aec21f6SHaggai Eran 	return err;
6559e126ba97SEli Cohen }
6560e126ba97SEli Cohen 
6561e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
6562e126ba97SEli Cohen {
65639603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
6564d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
6565c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
6566c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
6567e126ba97SEli Cohen }
6568e126ba97SEli Cohen 
6569e126ba97SEli Cohen module_init(mlx5_ib_init);
6570e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
6571