xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 7249c8ea227a582c14f63e9e8853eb7369122f10)
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)
183842a9c83SOr Gerlitz 				roce->netdev = ndev;
18484a6a7a9SParav Pandit 		} else if (ndev->dev.parent == &mdev->pdev->dev) {
185842a9c83SOr Gerlitz 			roce->netdev = ndev;
186bcf87f1dSMark Bloch 		}
1877fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
1885ec8c83eSAviv Heller 		break;
1895ec8c83eSAviv Heller 
190842a9c83SOr Gerlitz 	case NETDEV_UNREGISTER:
191842a9c83SOr Gerlitz 		write_lock(&roce->netdev_lock);
192842a9c83SOr Gerlitz 		if (roce->netdev == ndev)
193842a9c83SOr Gerlitz 			roce->netdev = NULL;
194842a9c83SOr Gerlitz 		write_unlock(&roce->netdev_lock);
195842a9c83SOr Gerlitz 		break;
196842a9c83SOr 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 
33408e8676fSAya Levin static int translate_eth_legacy_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 
39208e8676fSAya Levin static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed,
39308e8676fSAya Levin 					u8 *active_width)
39408e8676fSAya Levin {
39508e8676fSAya Levin 	switch (eth_proto_oper) {
39608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
39708e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
39808e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
39908e8676fSAya Levin 		*active_speed = IB_SPEED_SDR;
40008e8676fSAya Levin 		break;
40108e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
40208e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
40308e8676fSAya Levin 		*active_speed = IB_SPEED_DDR;
40408e8676fSAya Levin 		break;
40508e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
40608e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
40708e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
40808e8676fSAya Levin 		break;
40908e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
41008e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
41108e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
41208e8676fSAya Levin 		break;
41308e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
41408e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
41508e8676fSAya Levin 		*active_speed = IB_SPEED_EDR;
41608e8676fSAya Levin 		break;
41708e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
418cd272875SAya Levin 		*active_width = IB_WIDTH_2X;
419cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
420cd272875SAya Levin 		break;
42108e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
42208e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
42308e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
42408e8676fSAya Levin 		break;
425cd272875SAya Levin 	case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
426cd272875SAya Levin 		*active_width = IB_WIDTH_4X;
427cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
428cd272875SAya Levin 		break;
42908e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
43008e8676fSAya Levin 		*active_width = IB_WIDTH_2X;
43108e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
43208e8676fSAya Levin 		break;
43308e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
43408e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
43508e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
43608e8676fSAya Levin 		break;
43708e8676fSAya Levin 	default:
43808e8676fSAya Levin 		return -EINVAL;
43908e8676fSAya Levin 	}
44008e8676fSAya Levin 
44108e8676fSAya Levin 	return 0;
44208e8676fSAya Levin }
44308e8676fSAya Levin 
44408e8676fSAya Levin static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
44508e8676fSAya Levin 				    u8 *active_width, bool ext)
44608e8676fSAya Levin {
44708e8676fSAya Levin 	return ext ?
44808e8676fSAya Levin 		translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
44908e8676fSAya Levin 					     active_width) :
45008e8676fSAya Levin 		translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
45108e8676fSAya Levin 						active_width);
45208e8676fSAya Levin }
45308e8676fSAya Levin 
454095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
4553f89a643SAchiad Shochat 				struct ib_port_attr *props)
4563f89a643SAchiad Shochat {
4573f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
458bc4e12ffSAya Levin 	u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
459da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
46088621dfeSAviv Heller 	struct net_device *ndev, *upper;
4613f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
462b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
463c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
464f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
465b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
46608e8676fSAya Levin 	bool ext;
467095b0927SIlan Tayari 	int err;
4683f89a643SAchiad Shochat 
469b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
470b3cbd6f0SDaniel Jurgens 	if (!mdev) {
471b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
472b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
473b3cbd6f0SDaniel Jurgens 		 */
474b3cbd6f0SDaniel Jurgens 		put_mdev = false;
475b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
476b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
477b3cbd6f0SDaniel Jurgens 		port_num = 1;
478b3cbd6f0SDaniel Jurgens 	}
479b3cbd6f0SDaniel Jurgens 
480f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
481f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
48250f22fd8SNoa Osherovich 	 */
483bc4e12ffSAya Levin 	err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
484b3cbd6f0SDaniel Jurgens 				   mdev_port_num);
485095b0927SIlan Tayari 	if (err)
486b3cbd6f0SDaniel Jurgens 		goto out;
48708e8676fSAya Levin 	ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet);
48808e8676fSAya Levin 	eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
489f1b65df5SNoa Osherovich 
4907672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
4917672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
4927672ed33SHonggang Li 
493f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
49408e8676fSAya Levin 				 &props->active_width, ext);
4953f89a643SAchiad Shochat 
4963f89a643SAchiad Shochat 	props->port_cap_flags |= IB_PORT_CM_SUP;
4972f944c0fSJason Gunthorpe 	props->ip_gids = true;
4983f89a643SAchiad Shochat 
4993f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
5003f89a643SAchiad Shochat 						roce_address_table_size);
5013f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
5023f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
5033f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
5043f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
5053f89a643SAchiad Shochat 	props->phys_state       = 3;
5063f89a643SAchiad Shochat 
507b3cbd6f0SDaniel Jurgens 	mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
508c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
5093f89a643SAchiad Shochat 
510b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
511b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
512b3cbd6f0SDaniel Jurgens 		goto out;
513b3cbd6f0SDaniel Jurgens 
5143f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
5153f89a643SAchiad Shochat 	if (!ndev)
516b3cbd6f0SDaniel Jurgens 		goto out;
5173f89a643SAchiad Shochat 
5187c34ec19SAviv Heller 	if (dev->lag_active) {
51988621dfeSAviv Heller 		rcu_read_lock();
52088621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
52188621dfeSAviv Heller 		if (upper) {
52288621dfeSAviv Heller 			dev_put(ndev);
52388621dfeSAviv Heller 			ndev = upper;
52488621dfeSAviv Heller 			dev_hold(ndev);
52588621dfeSAviv Heller 		}
52688621dfeSAviv Heller 		rcu_read_unlock();
52788621dfeSAviv Heller 	}
52888621dfeSAviv Heller 
5293f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
5303f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
5313f89a643SAchiad Shochat 		props->phys_state = 5;
5323f89a643SAchiad Shochat 	}
5333f89a643SAchiad Shochat 
5343f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
5353f89a643SAchiad Shochat 
5363f89a643SAchiad Shochat 	dev_put(ndev);
5373f89a643SAchiad Shochat 
5383f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
539b3cbd6f0SDaniel Jurgens out:
540b3cbd6f0SDaniel Jurgens 	if (put_mdev)
541b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
542b3cbd6f0SDaniel Jurgens 	return err;
5433f89a643SAchiad Shochat }
5443f89a643SAchiad Shochat 
545cf34e1feSParav Pandit struct mlx5_ib_vlan_info {
546cf34e1feSParav Pandit 	u16 vlan_id;
547cf34e1feSParav Pandit 	bool vlan;
548cf34e1feSParav Pandit };
549cf34e1feSParav Pandit 
550cf34e1feSParav Pandit static int get_lower_dev_vlan(struct net_device *lower_dev, void *data)
551cf34e1feSParav Pandit {
552cf34e1feSParav Pandit 	struct mlx5_ib_vlan_info *vlan_info = data;
553cf34e1feSParav Pandit 
554cf34e1feSParav Pandit 	if (is_vlan_dev(lower_dev)) {
555cf34e1feSParav Pandit 		vlan_info->vlan = true;
556cf34e1feSParav Pandit 		vlan_info->vlan_id = vlan_dev_vlan_id(lower_dev);
557cf34e1feSParav Pandit 	}
558cf34e1feSParav Pandit 	/* We are interested only in first level vlan device, so
559cf34e1feSParav Pandit 	 * always return 1 to stop iterating over next level devices.
560cf34e1feSParav Pandit 	 */
561cf34e1feSParav Pandit 	return 1;
562cf34e1feSParav Pandit }
563cf34e1feSParav Pandit 
564095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
565095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
566095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
5673cca2606SAchiad Shochat {
568095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
569cf34e1feSParav Pandit 	struct mlx5_ib_vlan_info vlan_info = { };
570095b0927SIlan Tayari 	u8 roce_version = 0;
571095b0927SIlan Tayari 	u8 roce_l3_type = 0;
572095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
5733cca2606SAchiad Shochat 
574095b0927SIlan Tayari 	if (gid) {
575095b0927SIlan Tayari 		gid_type = attr->gid_type;
576095b0927SIlan Tayari 		ether_addr_copy(mac, attr->ndev->dev_addr);
5773cca2606SAchiad Shochat 
5783cca2606SAchiad Shochat 		if (is_vlan_dev(attr->ndev)) {
579cf34e1feSParav Pandit 			vlan_info.vlan = true;
580cf34e1feSParav Pandit 			vlan_info.vlan_id = vlan_dev_vlan_id(attr->ndev);
581cf34e1feSParav Pandit 		} else {
582cf34e1feSParav Pandit 			/* If the netdev is upper device and if it's lower
583cf34e1feSParav Pandit 			 * lower device is vlan device, consider vlan id of
584cf34e1feSParav Pandit 			 * the lower vlan device for this gid entry.
585cf34e1feSParav Pandit 			 */
586cf34e1feSParav Pandit 			rcu_read_lock();
587cf34e1feSParav Pandit 			netdev_walk_all_lower_dev_rcu(attr->ndev,
588cf34e1feSParav Pandit 					get_lower_dev_vlan, &vlan_info);
589cf34e1feSParav Pandit 			rcu_read_unlock();
590095b0927SIlan Tayari 		}
5913cca2606SAchiad Shochat 	}
5923cca2606SAchiad Shochat 
593095b0927SIlan Tayari 	switch (gid_type) {
5943cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
595095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
5963cca2606SAchiad Shochat 		break;
5973cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
598095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
599095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
600095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
601095b0927SIlan Tayari 		else
602095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
6033cca2606SAchiad Shochat 		break;
6043cca2606SAchiad Shochat 
6053cca2606SAchiad Shochat 	default:
606095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
6073cca2606SAchiad Shochat 	}
6083cca2606SAchiad Shochat 
609095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
610cf34e1feSParav Pandit 				      roce_l3_type, gid->raw, mac,
611cf34e1feSParav Pandit 				      vlan_info.vlan, vlan_info.vlan_id,
612cf34e1feSParav Pandit 				      port_num);
6133cca2606SAchiad Shochat }
6143cca2606SAchiad Shochat 
615f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
6163cca2606SAchiad Shochat 			   __always_unused void **context)
6173cca2606SAchiad Shochat {
618414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
619f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
6203cca2606SAchiad Shochat }
6213cca2606SAchiad Shochat 
622414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
623414448d2SParav Pandit 			   __always_unused void **context)
6243cca2606SAchiad Shochat {
625414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
626414448d2SParav Pandit 			     attr->index, NULL, NULL);
6273cca2606SAchiad Shochat }
6283cca2606SAchiad Shochat 
62947ec3866SParav Pandit __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
63047ec3866SParav Pandit 			       const struct ib_gid_attr *attr)
6312811ba51SAchiad Shochat {
63247ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
6332811ba51SAchiad Shochat 		return 0;
6342811ba51SAchiad Shochat 
6352811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
6362811ba51SAchiad Shochat }
6372811ba51SAchiad Shochat 
6381b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
6391b5daf11SMajd Dibbiny {
6407fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
641d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
6427fae6655SNoa Osherovich 	return 0;
6431b5daf11SMajd Dibbiny }
6441b5daf11SMajd Dibbiny 
6451b5daf11SMajd Dibbiny enum {
6461b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
6471b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
6481b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
6491b5daf11SMajd Dibbiny };
6501b5daf11SMajd Dibbiny 
6511b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
6521b5daf11SMajd Dibbiny {
6531b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
6541b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
6551b5daf11SMajd Dibbiny 
656ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
6571b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
6581b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
6591b5daf11SMajd Dibbiny 
6601b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
6611b5daf11SMajd Dibbiny }
6621b5daf11SMajd Dibbiny 
663da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
664776a3906SMoni Shoua 			    u8 atomic_size_qp,
665da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
666da7525d2SEran Ben Elisha {
667da7525d2SEran Ben Elisha 	u8 tmp;
668da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
669da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
670bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
671da7525d2SEran Ben Elisha 
672da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
673da7525d2SEran Ben Elisha 	 * of host endianness respond
674da7525d2SEran Ben Elisha 	 */
675da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
676da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
677da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
678da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
679da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
680da7525d2SEran Ben Elisha 	} else {
681da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
682da7525d2SEran Ben Elisha 	}
683da7525d2SEran Ben Elisha }
684da7525d2SEran Ben Elisha 
685776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
686776a3906SMoni Shoua 			       struct ib_device_attr *props)
687776a3906SMoni Shoua {
688776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
689776a3906SMoni Shoua 
690776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
691776a3906SMoni Shoua }
692776a3906SMoni Shoua 
693776a3906SMoni Shoua static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
694776a3906SMoni Shoua 			       struct ib_device_attr *props)
695776a3906SMoni Shoua {
696776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
697776a3906SMoni Shoua 
698776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
699776a3906SMoni Shoua }
700776a3906SMoni Shoua 
701776a3906SMoni Shoua bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
702776a3906SMoni Shoua {
703776a3906SMoni Shoua 	struct ib_device_attr props = {};
704776a3906SMoni Shoua 
705776a3906SMoni Shoua 	get_atomic_caps_dc(dev, &props);
706776a3906SMoni Shoua 	return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
707776a3906SMoni Shoua }
7081b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
7091b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
7101b5daf11SMajd Dibbiny {
7111b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7121b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7131b5daf11SMajd Dibbiny 	u64 tmp;
7141b5daf11SMajd Dibbiny 	int err;
7151b5daf11SMajd Dibbiny 
7161b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7171b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7181b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
7191b5daf11SMajd Dibbiny 							    sys_image_guid);
7201b5daf11SMajd Dibbiny 
7211b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7221b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
7233f89a643SAchiad Shochat 		break;
7243f89a643SAchiad Shochat 
7253f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7263f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
7273f89a643SAchiad Shochat 		break;
7281b5daf11SMajd Dibbiny 
7291b5daf11SMajd Dibbiny 	default:
7301b5daf11SMajd Dibbiny 		return -EINVAL;
7311b5daf11SMajd Dibbiny 	}
7323f89a643SAchiad Shochat 
7333f89a643SAchiad Shochat 	if (!err)
7343f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
7353f89a643SAchiad Shochat 
7363f89a643SAchiad Shochat 	return err;
7373f89a643SAchiad Shochat 
7381b5daf11SMajd Dibbiny }
7391b5daf11SMajd Dibbiny 
7401b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
7411b5daf11SMajd Dibbiny 				u16 *max_pkeys)
7421b5daf11SMajd Dibbiny {
7431b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7441b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7451b5daf11SMajd Dibbiny 
7461b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7471b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7481b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
7491b5daf11SMajd Dibbiny 
7501b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7511b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7521b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
7531b5daf11SMajd Dibbiny 						pkey_table_size));
7541b5daf11SMajd Dibbiny 		return 0;
7551b5daf11SMajd Dibbiny 
7561b5daf11SMajd Dibbiny 	default:
7571b5daf11SMajd Dibbiny 		return -EINVAL;
7581b5daf11SMajd Dibbiny 	}
7591b5daf11SMajd Dibbiny }
7601b5daf11SMajd Dibbiny 
7611b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
7621b5daf11SMajd Dibbiny 				u32 *vendor_id)
7631b5daf11SMajd Dibbiny {
7641b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7651b5daf11SMajd Dibbiny 
7661b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7671b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7681b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
7691b5daf11SMajd Dibbiny 
7701b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7711b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7721b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
7731b5daf11SMajd Dibbiny 
7741b5daf11SMajd Dibbiny 	default:
7751b5daf11SMajd Dibbiny 		return -EINVAL;
7761b5daf11SMajd Dibbiny 	}
7771b5daf11SMajd Dibbiny }
7781b5daf11SMajd Dibbiny 
7791b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
7801b5daf11SMajd Dibbiny 				__be64 *node_guid)
7811b5daf11SMajd Dibbiny {
7821b5daf11SMajd Dibbiny 	u64 tmp;
7831b5daf11SMajd Dibbiny 	int err;
7841b5daf11SMajd Dibbiny 
7851b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7861b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7871b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7881b5daf11SMajd Dibbiny 
7891b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7901b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7913f89a643SAchiad Shochat 		break;
7923f89a643SAchiad Shochat 
7933f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7943f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7953f89a643SAchiad Shochat 		break;
7961b5daf11SMajd Dibbiny 
7971b5daf11SMajd Dibbiny 	default:
7981b5daf11SMajd Dibbiny 		return -EINVAL;
7991b5daf11SMajd Dibbiny 	}
8003f89a643SAchiad Shochat 
8013f89a643SAchiad Shochat 	if (!err)
8023f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
8033f89a643SAchiad Shochat 
8043f89a643SAchiad Shochat 	return err;
8051b5daf11SMajd Dibbiny }
8061b5daf11SMajd Dibbiny 
8071b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
808bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
8091b5daf11SMajd Dibbiny };
8101b5daf11SMajd Dibbiny 
8111b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
8121b5daf11SMajd Dibbiny {
8131b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
8141b5daf11SMajd Dibbiny 
8151b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
8161b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
8171b5daf11SMajd Dibbiny 
8181b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
8191b5daf11SMajd Dibbiny 
8201b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
8211b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
8221b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
8231b5daf11SMajd Dibbiny }
8241b5daf11SMajd Dibbiny 
825e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
8262528e33eSMatan Barak 				struct ib_device_attr *props,
8272528e33eSMatan Barak 				struct ib_udata *uhw)
828e126ba97SEli Cohen {
829e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
830938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
831e126ba97SEli Cohen 	int err = -ENOMEM;
832288c01b7SEli Cohen 	int max_sq_desc;
833e126ba97SEli Cohen 	int max_rq_sg;
834e126ba97SEli Cohen 	int max_sq_sg;
835e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
83685c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
837402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
838402ca536SBodong Wang 	size_t resp_len;
839402ca536SBodong Wang 	u64 max_tso;
840e126ba97SEli Cohen 
841402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
842402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
843402ca536SBodong Wang 		return -EINVAL;
844402ca536SBodong Wang 	else
845402ca536SBodong Wang 		resp.response_length = resp_len;
846402ca536SBodong Wang 
847402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
8482528e33eSMatan Barak 		return -EINVAL;
8492528e33eSMatan Barak 
850e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
8511b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
8521b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
8531b5daf11SMajd Dibbiny 	if (err)
8541b5daf11SMajd Dibbiny 		return err;
8551b5daf11SMajd Dibbiny 
8561b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
8571b5daf11SMajd Dibbiny 	if (err)
8581b5daf11SMajd Dibbiny 		return err;
8591b5daf11SMajd Dibbiny 
8601b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
8611b5daf11SMajd Dibbiny 	if (err)
8621b5daf11SMajd Dibbiny 		return err;
863e126ba97SEli Cohen 
8649603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
8659603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
8669603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
867e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
868e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
869e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
8701a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
871938fe83cSSaeed Mahameed 
872938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
873e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
874938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
875e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
876938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
877e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
878938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
879e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
880d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
881d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
882d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
883d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
884b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
885b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
886d2370e0aSMatan Barak 	}
887e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
888938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
8892dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
8902dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8912dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8922dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8932dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8942dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8952dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8962dea9094SSagi Grimberg 	}
897938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
898f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
899e126ba97SEli Cohen 
90085c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
901e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
902e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
90388115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
904e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
905e8161334SNoa Osherovich 		}
906e8161334SNoa Osherovich 
907e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
908e8161334SNoa Osherovich 			props->raw_packet_caps |=
909e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
91088115fe7SBodong Wang 
911402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
912402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
913402ca536SBodong Wang 			if (max_tso) {
914402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
915402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
916402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
917402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
918402ca536SBodong Wang 			}
919402ca536SBodong Wang 		}
92031f69a82SYishai Hadas 
92131f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
92231f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
92331f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
92431f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
92531f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
92631f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
92731f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
92831f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
92931f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
93031f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
93131f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
9324e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
9334e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
9342d93fc85SMatan Barak 			if (mlx5_accel_ipsec_device_caps(dev->mdev) &
9352d93fc85SMatan Barak 			    MLX5_ACCEL_IPSEC_CAP_DEVICE)
9362d93fc85SMatan Barak 				resp.rss_caps.rx_hash_fields_mask |=
9372d93fc85SMatan Barak 					MLX5_RX_HASH_IPSEC_SPI;
93831f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
93931f69a82SYishai Hadas 		}
94031f69a82SYishai Hadas 	} else {
94131f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
94231f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
94331f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
94431f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
945402ca536SBodong Wang 	}
946402ca536SBodong Wang 
947f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
948f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
949f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
950f0313965SErez Shitrit 	}
951f0313965SErez Shitrit 
95203404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
95385c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
95485c7c014SDaniel Jurgens 	    raw_support)
95503404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
95603404e8aSMaor Gottlieb 
9571d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
9581d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
9591d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
9601d54f890SYishai Hadas 
961cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
96285c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
96385c7c014SDaniel Jurgens 	    raw_support) {
964e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
965cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
966e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
967e8161334SNoa Osherovich 	}
968cff5a0f3SMajd Dibbiny 
96924da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
97024da0016SAriel Levkovich 		props->max_dm_size =
97124da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
97224da0016SAriel Levkovich 	}
97324da0016SAriel Levkovich 
974da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
975da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
976da6d6ba3SMaor Gottlieb 
977b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
978b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
979b1383aa6SNoa Osherovich 
9801b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
9811b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
982e126ba97SEli Cohen 
983e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
984e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
985938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
986938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
987938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
988938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
989288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
990288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
991288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
992e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
99333023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
99433023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
995986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
996938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9979f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
998938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
999938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
1000938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
1001938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
1002938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
1003938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
1004938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
1005e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
1006e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
1007911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
1008911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
1009776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
101081bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
1011938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1012938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
1013e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1014e126ba97SEli Cohen 					   props->max_mcast_grp;
1015e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
101686695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
10177c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
10187c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
1019e126ba97SEli Cohen 
1020e502b8b0SLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
1021938fe83cSSaeed Mahameed 		if (MLX5_CAP_GEN(mdev, pg))
10228cdd312cSHaggai Eran 			props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
10238cdd312cSHaggai Eran 		props->odp_caps = dev->odp_caps;
1024e502b8b0SLeon Romanovsky 	}
10258cdd312cSHaggai Eran 
1026051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
1027051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1028051f2630SLeon Romanovsky 
1029eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
1030eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1031eff901d3SEli Cohen 
103231f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
103385c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
103431f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
103531f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
103631f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
103731f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
103831f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
103931f69a82SYishai Hadas 		props->max_wq_type_rq =
104031f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
104131f69a82SYishai Hadas 	}
104231f69a82SYishai Hadas 
1043eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
104478b1beb0SLeon Romanovsky 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
104578b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
1046eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
104778b1beb0SLeon Romanovsky 		props->tm_caps.flags = IB_TM_CAP_RC;
104878b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
1049eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
105078b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1051eb761894SArtemy Kovalyov 	}
1052eb761894SArtemy Kovalyov 
105387ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
105487ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
105587ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
105687ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
105787ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
105887ab3f52SYonatan Cohen 	}
105987ab3f52SYonatan Cohen 
10607e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
1061572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
1062572f46bfSYonatan Cohen 
1063572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
10647e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
1065572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
1066572f46bfSYonatan Cohen 					     cqe_compression_max_num);
1067572f46bfSYonatan Cohen 
10687e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
10697e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
10707e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
10716f1006a4SYonatan Cohen 
10726f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
10736f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
10746f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1075572f46bfSYonatan Cohen 		}
10767e43a2a5SBodong Wang 	}
10777e43a2a5SBodong Wang 
107885c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
107985c7c014SDaniel Jurgens 	    raw_support) {
1080d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1081d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
1082d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
1083d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1084d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
1085d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1086d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
1087d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
108861147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
108961147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
109061147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
109161147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
1092d949167dSBodong Wang 		}
1093d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1094d949167dSBodong Wang 	}
1095d949167dSBodong Wang 
10969f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
10979f885201SLeon Romanovsky 			uhw->outlen)) {
1098795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
10999f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1100795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1101050da902SBodong Wang 
1102050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1103050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1104050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1105050da902SBodong Wang 
11069f885201SLeon Romanovsky 		resp.response_length +=
11079f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
11089f885201SLeon Romanovsky 	}
11099f885201SLeon Romanovsky 
1110de57f2adSGuy Levi 	if (field_avail(typeof(resp), flags, uhw->outlen)) {
1111de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
11127a0c8f42SGuy Levi 
1113de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1114de57f2adSGuy Levi 			resp.flags |=
1115de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
11167a0c8f42SGuy Levi 
11177a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
11187a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
11197e11b911SDanit Goldberg 		if (MLX5_CAP_GEN(mdev, qp_packet_based))
11207e11b911SDanit Goldberg 			resp.flags |=
11217e11b911SDanit Goldberg 				MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
1122*7249c8eaSGuy Levi 
1123*7249c8eaSGuy Levi 		resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1124de57f2adSGuy Levi 	}
11259f885201SLeon Romanovsky 
112696dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
112796dc3fc5SNoa Osherovich 			uhw->outlen)) {
112896dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
112996dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
113096dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
113196dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
113296dc3fc5SNoa Osherovich 
113396dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
113496dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
113596dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
113696dc3fc5SNoa Osherovich 
113796dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
113896dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
113996dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
114096dc3fc5SNoa Osherovich 
114196dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
114296dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
114396dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
114496dc3fc5SNoa Osherovich 		}
114596dc3fc5SNoa Osherovich 	}
114696dc3fc5SNoa Osherovich 
114785c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
114885c7c014SDaniel Jurgens 	    raw_support) {
1149b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1150b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1151b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1152b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1153b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1154b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1155b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1156b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1157b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1158b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1159b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1160b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1161b4f34597SNoa Osherovich 		}
1162b4f34597SNoa Osherovich 	}
1163b4f34597SNoa Osherovich 
1164f95ef6cbSMaor Gottlieb 	if (field_avail(typeof(resp), tunnel_offloads_caps,
1165f95ef6cbSMaor Gottlieb 			uhw->outlen)) {
1166f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1167f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1168f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1169f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1170f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1171f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1172f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1173f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1174f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1175f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
1176e818e255SAriel Levkovich 		if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1177e818e255SAriel Levkovich 		    MLX5_FLEX_PROTO_CW_MPLS_GRE)
1178e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1179e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1180e818e255SAriel Levkovich 		if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1181e818e255SAriel Levkovich 		    MLX5_FLEX_PROTO_CW_MPLS_UDP)
1182e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1183e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1184f95ef6cbSMaor Gottlieb 	}
1185f95ef6cbSMaor Gottlieb 
1186402ca536SBodong Wang 	if (uhw->outlen) {
1187402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1188402ca536SBodong Wang 
1189402ca536SBodong Wang 		if (err)
1190402ca536SBodong Wang 			return err;
1191402ca536SBodong Wang 	}
1192402ca536SBodong Wang 
11931b5daf11SMajd Dibbiny 	return 0;
11941b5daf11SMajd Dibbiny }
1195e126ba97SEli Cohen 
11961b5daf11SMajd Dibbiny enum mlx5_ib_width {
11971b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
11981b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
11991b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
12001b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
12011b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
12021b5daf11SMajd Dibbiny };
12031b5daf11SMajd Dibbiny 
1204db7a691aSMichael Guralnik static void translate_active_width(struct ib_device *ibdev, u8 active_width,
12051b5daf11SMajd Dibbiny 				  u8 *ib_width)
12061b5daf11SMajd Dibbiny {
12071b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12081b5daf11SMajd Dibbiny 
1209db7a691aSMichael Guralnik 	if (active_width & MLX5_IB_WIDTH_1X)
12101b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1211d764970bSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_2X)
1212d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1213db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_4X)
12141b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1215db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_8X)
12161b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1217db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_12X)
12181b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1219db7a691aSMichael Guralnik 	else {
1220db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
12211b5daf11SMajd Dibbiny 			    (int)active_width);
1222db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
12231b5daf11SMajd Dibbiny 	}
12241b5daf11SMajd Dibbiny 
1225db7a691aSMichael Guralnik 	return;
12261b5daf11SMajd Dibbiny }
12271b5daf11SMajd Dibbiny 
12281b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
12291b5daf11SMajd Dibbiny {
12301b5daf11SMajd Dibbiny 	switch (mtu) {
12311b5daf11SMajd Dibbiny 	case 256: return 1;
12321b5daf11SMajd Dibbiny 	case 512: return 2;
12331b5daf11SMajd Dibbiny 	case 1024: return 3;
12341b5daf11SMajd Dibbiny 	case 2048: return 4;
12351b5daf11SMajd Dibbiny 	case 4096: return 5;
12361b5daf11SMajd Dibbiny 	default:
12371b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
12381b5daf11SMajd Dibbiny 		return -1;
12391b5daf11SMajd Dibbiny 	}
12401b5daf11SMajd Dibbiny }
12411b5daf11SMajd Dibbiny 
12421b5daf11SMajd Dibbiny enum ib_max_vl_num {
12431b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
12441b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
12451b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
12461b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
12471b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
12481b5daf11SMajd Dibbiny };
12491b5daf11SMajd Dibbiny 
12501b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
12511b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
12521b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
12531b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
12541b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
12551b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
12561b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
12571b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
12581b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
12591b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
12601b5daf11SMajd Dibbiny };
12611b5daf11SMajd Dibbiny 
12621b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
12631b5daf11SMajd Dibbiny 				u8 *max_vl_num)
12641b5daf11SMajd Dibbiny {
12651b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
12661b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
12671b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
12681b5daf11SMajd Dibbiny 		break;
12691b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
12701b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
12711b5daf11SMajd Dibbiny 		break;
12721b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
12731b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
12741b5daf11SMajd Dibbiny 		break;
12751b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
12761b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
12771b5daf11SMajd Dibbiny 		break;
12781b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
12791b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
12801b5daf11SMajd Dibbiny 		break;
12811b5daf11SMajd Dibbiny 
12821b5daf11SMajd Dibbiny 	default:
12831b5daf11SMajd Dibbiny 		return -EINVAL;
12841b5daf11SMajd Dibbiny 	}
12851b5daf11SMajd Dibbiny 
12861b5daf11SMajd Dibbiny 	return 0;
12871b5daf11SMajd Dibbiny }
12881b5daf11SMajd Dibbiny 
12891b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
12901b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
12911b5daf11SMajd Dibbiny {
12921b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12931b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
12941b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1295046339eaSSaeed Mahameed 	u16 max_mtu;
1296046339eaSSaeed Mahameed 	u16 oper_mtu;
12971b5daf11SMajd Dibbiny 	int err;
12981b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
12991b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
13001b5daf11SMajd Dibbiny 
13011b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
13021b5daf11SMajd Dibbiny 	if (!rep) {
13031b5daf11SMajd Dibbiny 		err = -ENOMEM;
13041b5daf11SMajd Dibbiny 		goto out;
13051b5daf11SMajd Dibbiny 	}
13061b5daf11SMajd Dibbiny 
1307c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
13081b5daf11SMajd Dibbiny 
13091b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
13101b5daf11SMajd Dibbiny 	if (err)
13111b5daf11SMajd Dibbiny 		goto out;
13121b5daf11SMajd Dibbiny 
13131b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
13141b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
13151b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
13161b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
13171b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
13181b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
13191b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
13201b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
13211b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
13221b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
13231b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
13241b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
13251b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
13261b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
13271b5daf11SMajd Dibbiny 
13284106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
13294106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
13304106a758SMichael Guralnik 
13311b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
13321b5daf11SMajd Dibbiny 	if (err)
13331b5daf11SMajd Dibbiny 		goto out;
13341b5daf11SMajd Dibbiny 
1335db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1336db7a691aSMichael Guralnik 
1337d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
13381b5daf11SMajd Dibbiny 	if (err)
13391b5daf11SMajd Dibbiny 		goto out;
13401b5daf11SMajd Dibbiny 
1341facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
13421b5daf11SMajd Dibbiny 
13431b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
13441b5daf11SMajd Dibbiny 
1345facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
13461b5daf11SMajd Dibbiny 
13471b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
13481b5daf11SMajd Dibbiny 
13491b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
13501b5daf11SMajd Dibbiny 	if (err)
13511b5daf11SMajd Dibbiny 		goto out;
13521b5daf11SMajd Dibbiny 
13531b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
13541b5daf11SMajd Dibbiny 				   &props->max_vl_num);
13551b5daf11SMajd Dibbiny out:
13561b5daf11SMajd Dibbiny 	kfree(rep);
1357e126ba97SEli Cohen 	return err;
1358e126ba97SEli Cohen }
1359e126ba97SEli Cohen 
1360e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1361e126ba97SEli Cohen 		       struct ib_port_attr *props)
1362e126ba97SEli Cohen {
1363095b0927SIlan Tayari 	unsigned int count;
1364095b0927SIlan Tayari 	int ret;
1365095b0927SIlan Tayari 
13661b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13671b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1368095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1369095b0927SIlan Tayari 		break;
1370e126ba97SEli Cohen 
13711b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1372095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1373095b0927SIlan Tayari 		break;
13741b5daf11SMajd Dibbiny 
13753f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1376095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1377095b0927SIlan Tayari 		break;
13783f89a643SAchiad Shochat 
13791b5daf11SMajd Dibbiny 	default:
1380095b0927SIlan Tayari 		ret = -EINVAL;
1381e126ba97SEli Cohen 	}
1382095b0927SIlan Tayari 
1383095b0927SIlan Tayari 	if (!ret && props) {
1384b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1385b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1386b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1387b3cbd6f0SDaniel Jurgens 
1388b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1389b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1390b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1391b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1392b3cbd6f0SDaniel Jurgens 			 */
1393b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1394b3cbd6f0SDaniel Jurgens 			port = 1;
1395b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1396b3cbd6f0SDaniel Jurgens 		}
1397b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1398b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1399b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1400095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1401095b0927SIlan Tayari 	}
1402095b0927SIlan Tayari 	return ret;
1403e126ba97SEli Cohen }
1404e126ba97SEli Cohen 
14058e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
14068e6efa3aSMark Bloch 				  struct ib_port_attr *props)
14078e6efa3aSMark Bloch {
14088e6efa3aSMark Bloch 	int ret;
14098e6efa3aSMark Bloch 
14108e6efa3aSMark Bloch 	/* Only link layer == ethernet is valid for representors */
14118e6efa3aSMark Bloch 	ret = mlx5_query_port_roce(ibdev, port, props);
14128e6efa3aSMark Bloch 	if (ret || !props)
14138e6efa3aSMark Bloch 		return ret;
14148e6efa3aSMark Bloch 
14158e6efa3aSMark Bloch 	/* We don't support GIDS */
14168e6efa3aSMark Bloch 	props->gid_tbl_len = 0;
14178e6efa3aSMark Bloch 
14188e6efa3aSMark Bloch 	return ret;
14198e6efa3aSMark Bloch }
14208e6efa3aSMark Bloch 
1421e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1422e126ba97SEli Cohen 			     union ib_gid *gid)
1423e126ba97SEli Cohen {
14241b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
14251b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1426e126ba97SEli Cohen 
14271b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14281b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14291b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1430e126ba97SEli Cohen 
14311b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14321b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1433e126ba97SEli Cohen 
14341b5daf11SMajd Dibbiny 	default:
14351b5daf11SMajd Dibbiny 		return -EINVAL;
14361b5daf11SMajd Dibbiny 	}
1437e126ba97SEli Cohen 
1438e126ba97SEli Cohen }
1439e126ba97SEli Cohen 
1440b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1441b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1442b3cbd6f0SDaniel Jurgens {
1443b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1444b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1445b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1446b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1447b3cbd6f0SDaniel Jurgens 	int err;
1448b3cbd6f0SDaniel Jurgens 
1449b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1450b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1451b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1452b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1453b3cbd6f0SDaniel Jurgens 		 */
1454b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1455b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1456b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1457b3cbd6f0SDaniel Jurgens 	}
1458b3cbd6f0SDaniel Jurgens 
1459b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1460b3cbd6f0SDaniel Jurgens 					index, pkey);
1461b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1462b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1463b3cbd6f0SDaniel Jurgens 
1464b3cbd6f0SDaniel Jurgens 	return err;
1465b3cbd6f0SDaniel Jurgens }
1466b3cbd6f0SDaniel Jurgens 
1467e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1468e126ba97SEli Cohen 			      u16 *pkey)
1469e126ba97SEli Cohen {
14701b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14711b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14721b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1473e126ba97SEli Cohen 
14741b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14751b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1476b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
14771b5daf11SMajd Dibbiny 	default:
14781b5daf11SMajd Dibbiny 		return -EINVAL;
1479e126ba97SEli Cohen 	}
14801b5daf11SMajd Dibbiny }
1481e126ba97SEli Cohen 
1482e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1483e126ba97SEli Cohen 				 struct ib_device_modify *props)
1484e126ba97SEli Cohen {
1485e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1486e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1487e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1488e126ba97SEli Cohen 	int err;
1489e126ba97SEli Cohen 
1490e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1491e126ba97SEli Cohen 		return -EOPNOTSUPP;
1492e126ba97SEli Cohen 
1493e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1494e126ba97SEli Cohen 		return 0;
1495e126ba97SEli Cohen 
1496e126ba97SEli Cohen 	/*
1497e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1498e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1499e126ba97SEli Cohen 	 */
1500bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
15019603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1502e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1503e126ba97SEli Cohen 	if (err)
1504e126ba97SEli Cohen 		return err;
1505e126ba97SEli Cohen 
1506bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1507e126ba97SEli Cohen 
1508e126ba97SEli Cohen 	return err;
1509e126ba97SEli Cohen }
1510e126ba97SEli Cohen 
1511cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1512cdbe33d0SEli Cohen 				u32 value)
1513cdbe33d0SEli Cohen {
1514cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1515b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1516b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1517cdbe33d0SEli Cohen 	int err;
1518cdbe33d0SEli Cohen 
1519b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1520b3cbd6f0SDaniel Jurgens 	if (!mdev)
1521b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1522b3cbd6f0SDaniel Jurgens 
1523b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1524cdbe33d0SEli Cohen 	if (err)
1525b3cbd6f0SDaniel Jurgens 		goto out;
1526cdbe33d0SEli Cohen 
1527cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1528cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1529cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1530b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1531b3cbd6f0SDaniel Jurgens 		goto out;
1532cdbe33d0SEli Cohen 	}
1533cdbe33d0SEli Cohen 
1534cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1535cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1536b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1537b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1538b3cbd6f0SDaniel Jurgens 
1539b3cbd6f0SDaniel Jurgens out:
1540b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1541cdbe33d0SEli Cohen 
1542cdbe33d0SEli Cohen 	return err;
1543cdbe33d0SEli Cohen }
1544cdbe33d0SEli Cohen 
1545e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1546e126ba97SEli Cohen 			       struct ib_port_modify *props)
1547e126ba97SEli Cohen {
1548e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1549e126ba97SEli Cohen 	struct ib_port_attr attr;
1550e126ba97SEli Cohen 	u32 tmp;
1551e126ba97SEli Cohen 	int err;
1552cdbe33d0SEli Cohen 	u32 change_mask;
1553cdbe33d0SEli Cohen 	u32 value;
1554cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1555cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1556cdbe33d0SEli Cohen 
1557ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1558ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1559ec255879SMajd Dibbiny 	 */
1560ec255879SMajd Dibbiny 	if (!is_ib)
1561ec255879SMajd Dibbiny 		return 0;
1562ec255879SMajd Dibbiny 
1563cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1564cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1565cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1566cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1567cdbe33d0SEli Cohen 	}
1568e126ba97SEli Cohen 
1569e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1570e126ba97SEli Cohen 
1571c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1572e126ba97SEli Cohen 	if (err)
1573e126ba97SEli Cohen 		goto out;
1574e126ba97SEli Cohen 
1575e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1576e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1577e126ba97SEli Cohen 
15789603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1579e126ba97SEli Cohen 
1580e126ba97SEli Cohen out:
1581e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1582e126ba97SEli Cohen 	return err;
1583e126ba97SEli Cohen }
1584e126ba97SEli Cohen 
158530aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
158630aa60b3SEli Cohen {
158730aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
158830aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
158930aa60b3SEli Cohen }
159030aa60b3SEli Cohen 
159131a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
159231a78a5aSYishai Hadas {
159331a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
159431a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
159531a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
159631a78a5aSYishai Hadas 
159731a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
159831a78a5aSYishai Hadas }
159931a78a5aSYishai Hadas 
1600b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1601b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
160231a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1603b037c29aSEli Cohen {
1604b037c29aSEli Cohen 	int uars_per_sys_page;
1605b037c29aSEli Cohen 	int bfregs_per_sys_page;
1606b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1607b037c29aSEli Cohen 
1608b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1609b037c29aSEli Cohen 		return -EINVAL;
1610b037c29aSEli Cohen 
1611b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1612b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1613b037c29aSEli Cohen 
1614b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1615b037c29aSEli Cohen 		return -ENOMEM;
1616b037c29aSEli Cohen 
1617b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1618b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
161931a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1620b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1621b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1622b037c29aSEli Cohen 		return -EINVAL;
1623b037c29aSEli Cohen 
162431a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
162531a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
162631a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
162731a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
162831a78a5aSYishai Hadas 
162931a78a5aSYishai 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",
1630b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1631b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
163231a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
163331a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1634b037c29aSEli Cohen 
1635b037c29aSEli Cohen 	return 0;
1636b037c29aSEli Cohen }
1637b037c29aSEli Cohen 
1638b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1639b037c29aSEli Cohen {
1640b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1641b037c29aSEli Cohen 	int err;
1642b037c29aSEli Cohen 	int i;
1643b037c29aSEli Cohen 
1644b037c29aSEli Cohen 	bfregi = &context->bfregi;
164531a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1646b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1647b037c29aSEli Cohen 		if (err)
1648b037c29aSEli Cohen 			goto error;
1649b037c29aSEli Cohen 
1650b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1651b037c29aSEli Cohen 	}
16524ed131d0SYishai Hadas 
16534ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
16544ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
16554ed131d0SYishai Hadas 
1656b037c29aSEli Cohen 	return 0;
1657b037c29aSEli Cohen 
1658b037c29aSEli Cohen error:
1659b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1660b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1661b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1662b037c29aSEli Cohen 
1663b037c29aSEli Cohen 	return err;
1664b037c29aSEli Cohen }
1665b037c29aSEli Cohen 
166615177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
166715177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1668b037c29aSEli Cohen {
1669b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1670b037c29aSEli Cohen 	int i;
1671b037c29aSEli Cohen 
1672b037c29aSEli Cohen 	bfregi = &context->bfregi;
167315177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
16744ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
167515177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
167615177999SLeon Romanovsky 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1677b037c29aSEli Cohen }
1678b037c29aSEli Cohen 
16790042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1680a560f1d9SMark Bloch {
1681a560f1d9SMark Bloch 	int err = 0;
1682a560f1d9SMark Bloch 
1683a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16840042f9e4SMark Bloch 	if (td)
1685a560f1d9SMark Bloch 		dev->lb.user_td++;
16860042f9e4SMark Bloch 	if (qp)
16870042f9e4SMark Bloch 		dev->lb.qps++;
1688a560f1d9SMark Bloch 
16890042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
16900042f9e4SMark Bloch 	    dev->lb.qps == 1) {
16910042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1692a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
16930042f9e4SMark Bloch 			dev->lb.enabled = true;
16940042f9e4SMark Bloch 		}
16950042f9e4SMark Bloch 	}
1696a560f1d9SMark Bloch 
1697a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1698a560f1d9SMark Bloch 
1699a560f1d9SMark Bloch 	return err;
1700a560f1d9SMark Bloch }
1701a560f1d9SMark Bloch 
17020042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1703a560f1d9SMark Bloch {
1704a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
17050042f9e4SMark Bloch 	if (td)
1706a560f1d9SMark Bloch 		dev->lb.user_td--;
17070042f9e4SMark Bloch 	if (qp)
17080042f9e4SMark Bloch 		dev->lb.qps--;
1709a560f1d9SMark Bloch 
17100042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
17110042f9e4SMark Bloch 	    dev->lb.qps == 0) {
17120042f9e4SMark Bloch 		if (dev->lb.enabled) {
1713a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
17140042f9e4SMark Bloch 			dev->lb.enabled = false;
17150042f9e4SMark Bloch 		}
17160042f9e4SMark Bloch 	}
1717a560f1d9SMark Bloch 
1718a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1719a560f1d9SMark Bloch }
1720a560f1d9SMark Bloch 
1721d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1722d2d19121SYishai Hadas 					  u16 uid)
1723c85023e1SHuy Nguyen {
1724c85023e1SHuy Nguyen 	int err;
1725c85023e1SHuy Nguyen 
1726cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1727cfdeb893SLeon Romanovsky 		return 0;
1728cfdeb893SLeon Romanovsky 
1729d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1730c85023e1SHuy Nguyen 	if (err)
1731c85023e1SHuy Nguyen 		return err;
1732c85023e1SHuy Nguyen 
1733c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17348978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17358978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1736c85023e1SHuy Nguyen 		return err;
1737c85023e1SHuy Nguyen 
17380042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1739c85023e1SHuy Nguyen }
1740c85023e1SHuy Nguyen 
1741d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1742d2d19121SYishai Hadas 					     u16 uid)
1743c85023e1SHuy Nguyen {
1744cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1745cfdeb893SLeon Romanovsky 		return;
1746cfdeb893SLeon Romanovsky 
1747d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1748c85023e1SHuy Nguyen 
1749c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17508978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17518978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1752c85023e1SHuy Nguyen 		return;
1753c85023e1SHuy Nguyen 
17540042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1755c85023e1SHuy Nguyen }
1756c85023e1SHuy Nguyen 
1757a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1758e126ba97SEli Cohen 				  struct ib_udata *udata)
1759e126ba97SEli Cohen {
1760a2a074efSLeon Romanovsky 	struct ib_device *ibdev = uctx->device;
1761e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1762b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1763b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
17645c99eaecSFeras Daoud 	struct mlx5_core_dev *mdev = dev->mdev;
1765a2a074efSLeon Romanovsky 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
17662f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
176778c0f98cSEli Cohen 	int ver;
1768e126ba97SEli Cohen 	int err;
1769a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1770a168a41cSMajd Dibbiny 				     max_cqe_version);
177125bb36e7SYonatan Cohen 	u32 dump_fill_mkey;
1772b037c29aSEli Cohen 	bool lib_uar_4k;
1773e126ba97SEli Cohen 
1774e126ba97SEli Cohen 	if (!dev->ib_active)
1775a2a074efSLeon Romanovsky 		return -EAGAIN;
1776e126ba97SEli Cohen 
1777e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
177878c0f98cSEli Cohen 		ver = 0;
1779e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
178078c0f98cSEli Cohen 		ver = 2;
178178c0f98cSEli Cohen 	else
1782a2a074efSLeon Romanovsky 		return -EINVAL;
178378c0f98cSEli Cohen 
1784e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1785e126ba97SEli Cohen 	if (err)
1786a2a074efSLeon Romanovsky 		return err;
1787e126ba97SEli Cohen 
1788a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1789a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
179078c0f98cSEli Cohen 
1791f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1792a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
1793b368d7cbSMatan Barak 
17942f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
17952f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
17962f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1797a2a074efSLeon Romanovsky 		return -EINVAL;
1798e126ba97SEli Cohen 
1799938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
18002cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1801938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1802b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1803938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1804938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1805938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1806938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1807938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1808f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1809f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1810f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
181130aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
181230aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
181330aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
181430aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1815b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1816b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1817e126ba97SEli Cohen 
1818c03faa56SMatan Barak 	if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1819c03faa56SMatan Barak 		if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1820c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1821c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1822c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1823c03faa56SMatan Barak 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1824c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1825c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1826c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1827c03faa56SMatan Barak 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1828c03faa56SMatan Barak 	}
1829c03faa56SMatan Barak 
183030aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
18312f5ff264SEli Cohen 	bfregi = &context->bfregi;
1832b037c29aSEli Cohen 
1833b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
183431a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1835b037c29aSEli Cohen 	if (err)
1836b037c29aSEli Cohen 		goto out_ctx;
1837b037c29aSEli Cohen 
18382f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1839b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
184031a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1841b037c29aSEli Cohen 				GFP_KERNEL);
1842b037c29aSEli Cohen 	if (!bfregi->count) {
1843e126ba97SEli Cohen 		err = -ENOMEM;
1844e126ba97SEli Cohen 		goto out_ctx;
1845e126ba97SEli Cohen 	}
1846e126ba97SEli Cohen 
1847b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1848b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1849e126ba97SEli Cohen 				    GFP_KERNEL);
1850b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1851e126ba97SEli Cohen 		err = -ENOMEM;
1852e126ba97SEli Cohen 		goto out_count;
1853e126ba97SEli Cohen 	}
1854e126ba97SEli Cohen 
1855b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1856b037c29aSEli Cohen 	if (err)
1857b037c29aSEli Cohen 		goto out_sys_pages;
1858b037c29aSEli Cohen 
185913859d5dSLeon Romanovsky 	if (ibdev->attrs.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING)
186013859d5dSLeon Romanovsky 		context->ibucontext.invalidate_range =
186113859d5dSLeon Romanovsky 			&mlx5_ib_invalidate_range;
1862b4cfe447SHaggai Eran 
1863a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1864fb98153bSYishai Hadas 		err = mlx5_ib_devx_create(dev, true);
186576dc5a84SYishai Hadas 		if (err < 0)
1866d2d19121SYishai Hadas 			goto out_uars;
186776dc5a84SYishai Hadas 		context->devx_uid = err;
1868a8b92ca1SYishai Hadas 	}
1869a8b92ca1SYishai Hadas 
1870d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1871d2d19121SYishai Hadas 					     context->devx_uid);
1872a8b92ca1SYishai Hadas 	if (err)
1873d2d19121SYishai Hadas 		goto out_devx;
1874a8b92ca1SYishai Hadas 
187525bb36e7SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
187625bb36e7SYonatan Cohen 		err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
187725bb36e7SYonatan Cohen 		if (err)
18788193abb6SJason Gunthorpe 			goto out_mdev;
187925bb36e7SYonatan Cohen 	}
188025bb36e7SYonatan Cohen 
1881e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1882e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1883e126ba97SEli Cohen 
18842f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1885508562d6SDaniel Jurgens 	resp.num_ports = dev->num_ports;
1886b368d7cbSMatan Barak 
1887f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1888f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1889b368d7cbSMatan Barak 
1890402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
18916ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
18926ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1893402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1894402ca536SBodong Wang 	}
1895402ca536SBodong Wang 
189678984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
189778984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
189878984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
189978984898SOr Gerlitz 			resp.eth_min_inline++;
190078984898SOr Gerlitz 		}
190178984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
190278984898SOr Gerlitz 	}
190378984898SOr Gerlitz 
19045c99eaecSFeras Daoud 	if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
19055c99eaecSFeras Daoud 		if (mdev->clock_info)
19065c99eaecSFeras Daoud 			resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
19075c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.clock_info_versions);
19085c99eaecSFeras Daoud 	}
19095c99eaecSFeras Daoud 
1910bc5c6eedSNoa Osherovich 	/*
1911bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1912bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1913bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1914bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1915bc5c6eedSNoa Osherovich 	 */
1916de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1917de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1918b368d7cbSMatan Barak 			resp.comp_mask |=
1919b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1920b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1921de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1922de8d6e02SEli Cohen 		}
19235c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.hca_core_clock_offset);
1924b368d7cbSMatan Barak 	}
1925b368d7cbSMatan Barak 
192630aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
192730aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
192830aa60b3SEli Cohen 
192930aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
193030aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
193130aa60b3SEli Cohen 
193231a78a5aSYishai Hadas 	if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
193331a78a5aSYishai Hadas 		resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
193431a78a5aSYishai Hadas 		resp.response_length += sizeof(resp.num_dyn_bfregs);
193531a78a5aSYishai Hadas 	}
193631a78a5aSYishai Hadas 
193725bb36e7SYonatan Cohen 	if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
193825bb36e7SYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
193925bb36e7SYonatan Cohen 			resp.dump_fill_mkey = dump_fill_mkey;
194025bb36e7SYonatan Cohen 			resp.comp_mask |=
194125bb36e7SYonatan Cohen 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
194225bb36e7SYonatan Cohen 		}
194325bb36e7SYonatan Cohen 		resp.response_length += sizeof(resp.dump_fill_mkey);
194425bb36e7SYonatan Cohen 	}
194525bb36e7SYonatan Cohen 
1946b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1947e126ba97SEli Cohen 	if (err)
1948a8b92ca1SYishai Hadas 		goto out_mdev;
1949e126ba97SEli Cohen 
19502f5ff264SEli Cohen 	bfregi->ver = ver;
19512f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1952f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
195330aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
195430aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1955f72300c5SHaggai Abramovsky 
19567c34ec19SAviv Heller 	if (dev->lag_active) {
1957c6a21c38SMajd Dibbiny 		u8 port = mlx5_core_native_port_num(dev->mdev);
1958c6a21c38SMajd Dibbiny 
1959c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1960c6a21c38SMajd Dibbiny 			   atomic_add_return(
1961c6a21c38SMajd Dibbiny 				   1, &dev->roce[port].tx_port_affinity));
1962c6a21c38SMajd Dibbiny 	}
1963c6a21c38SMajd Dibbiny 
1964a2a074efSLeon Romanovsky 	return 0;
1965e126ba97SEli Cohen 
1966a8b92ca1SYishai Hadas out_mdev:
1967d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1968d2d19121SYishai Hadas out_devx:
1969a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
197076dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1971146d2f1aSmajd@mellanox.com 
1972e126ba97SEli Cohen out_uars:
1973b037c29aSEli Cohen 	deallocate_uars(dev, context);
1974b037c29aSEli Cohen 
1975b037c29aSEli Cohen out_sys_pages:
1976b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1977b037c29aSEli Cohen 
1978e126ba97SEli Cohen out_count:
19792f5ff264SEli Cohen 	kfree(bfregi->count);
1980e126ba97SEli Cohen 
1981e126ba97SEli Cohen out_ctx:
1982a2a074efSLeon Romanovsky 	return err;
1983e126ba97SEli Cohen }
1984e126ba97SEli Cohen 
1985a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1986e126ba97SEli Cohen {
1987e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1988e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1989b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1990e126ba97SEli Cohen 
1991f27a0d50SJason Gunthorpe 	/* All umem's must be destroyed before destroying the ucontext. */
1992f27a0d50SJason Gunthorpe 	mutex_lock(&ibcontext->per_mm_list_lock);
1993f27a0d50SJason Gunthorpe 	WARN_ON(!list_empty(&ibcontext->per_mm_list));
1994f27a0d50SJason Gunthorpe 	mutex_unlock(&ibcontext->per_mm_list_lock);
1995a8b92ca1SYishai Hadas 
1996b037c29aSEli Cohen 	bfregi = &context->bfregi;
1997d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1998d2d19121SYishai Hadas 
1999b037c29aSEli Cohen 	if (context->devx_uid)
200076dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
2001e126ba97SEli Cohen 
2002e126ba97SEli Cohen 	deallocate_uars(dev, context);
20032f5ff264SEli Cohen 	kfree(bfregi->sys_pages);
20042f5ff264SEli Cohen 	kfree(bfregi->count);
2005e126ba97SEli Cohen }
2006e126ba97SEli Cohen 
2007e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2008e126ba97SEli Cohen 				 int uar_idx)
2009e126ba97SEli Cohen {
2010b037c29aSEli Cohen 	int fw_uars_per_page;
2011b037c29aSEli Cohen 
2012b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2013b037c29aSEli Cohen 
20144ed131d0SYishai Hadas 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2015e126ba97SEli Cohen }
2016e126ba97SEli Cohen 
2017e126ba97SEli Cohen static int get_command(unsigned long offset)
2018e126ba97SEli Cohen {
2019e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2020e126ba97SEli Cohen }
2021e126ba97SEli Cohen 
2022e126ba97SEli Cohen static int get_arg(unsigned long offset)
2023e126ba97SEli Cohen {
2024e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2025e126ba97SEli Cohen }
2026e126ba97SEli Cohen 
2027e126ba97SEli Cohen static int get_index(unsigned long offset)
2028e126ba97SEli Cohen {
2029e126ba97SEli Cohen 	return get_arg(offset);
2030e126ba97SEli Cohen }
2031e126ba97SEli Cohen 
20324ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
20334ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20344ed131d0SYishai Hadas {
20354ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20364ed131d0SYishai Hadas }
20374ed131d0SYishai Hadas 
20387c2344c3SMaor Gottlieb 
20397c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20407c2344c3SMaor Gottlieb {
20417c2344c3SMaor Gottlieb }
20427c2344c3SMaor Gottlieb 
204337aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2044e126ba97SEli Cohen {
204537aa5c36SGuy Levi 	switch (cmd) {
204637aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
204737aa5c36SGuy Levi 		return "WC";
2048e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
204937aa5c36SGuy Levi 		return "best effort WC";
205037aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
205137aa5c36SGuy Levi 		return "NC";
205224da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
205324da0016SAriel Levkovich 		return "Device Memory";
205437aa5c36SGuy Levi 	default:
205537aa5c36SGuy Levi 		return NULL;
205637aa5c36SGuy Levi 	}
205737aa5c36SGuy Levi }
205837aa5c36SGuy Levi 
20595c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20605c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20615c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20625c99eaecSFeras Daoud {
20635c99eaecSFeras Daoud 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
20645c99eaecSFeras Daoud 		return -EINVAL;
20655c99eaecSFeras Daoud 
20665c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20675c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20685c99eaecSFeras Daoud 
20695c99eaecSFeras Daoud 	if (vma->vm_flags & VM_WRITE)
20705c99eaecSFeras Daoud 		return -EPERM;
20715c99eaecSFeras Daoud 
20725c99eaecSFeras Daoud 	if (!dev->mdev->clock_info_page)
20735c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20745c99eaecSFeras Daoud 
2075e2cd1d1aSJason Gunthorpe 	return rdma_user_mmap_page(&context->ibucontext, vma,
2076e2cd1d1aSJason Gunthorpe 				   dev->mdev->clock_info_page, PAGE_SIZE);
20775c99eaecSFeras Daoud }
20785c99eaecSFeras Daoud 
207937aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
20807c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
20817c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
208237aa5c36SGuy Levi {
20832f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
208437aa5c36SGuy Levi 	int err;
208537aa5c36SGuy Levi 	unsigned long idx;
2086aa09ea6eSKamal Heib 	phys_addr_t pfn;
208737aa5c36SGuy Levi 	pgprot_t prot;
20884ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
20894ed131d0SYishai Hadas 	u32 uar_index;
20904ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
20914ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
20924ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2093b037c29aSEli Cohen 
2094b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2095b037c29aSEli Cohen 		return -EINVAL;
2096b037c29aSEli Cohen 
20974ed131d0SYishai Hadas 	if (dyn_uar)
20984ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
20994ed131d0SYishai Hadas 	else
2100b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
21014ed131d0SYishai Hadas 
21024ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21034ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21044ed131d0SYishai Hadas 			     idx, max_valid_idx);
2105b037c29aSEli Cohen 		return -EINVAL;
2106b037c29aSEli Cohen 	}
210737aa5c36SGuy Levi 
210837aa5c36SGuy Levi 	switch (cmd) {
210937aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21104ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
211137aa5c36SGuy Levi /* Some architectures don't support WC memory */
211237aa5c36SGuy Levi #if defined(CONFIG_X86)
211337aa5c36SGuy Levi 		if (!pat_enabled())
211437aa5c36SGuy Levi 			return -EPERM;
211537aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
211637aa5c36SGuy Levi 			return -EPERM;
211737aa5c36SGuy Levi #endif
211837aa5c36SGuy Levi 	/* fall through */
211937aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
212037aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
212137aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
212237aa5c36SGuy Levi 		break;
212337aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
212437aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
212537aa5c36SGuy Levi 		break;
212637aa5c36SGuy Levi 	default:
212737aa5c36SGuy Levi 		return -EINVAL;
212837aa5c36SGuy Levi 	}
212937aa5c36SGuy Levi 
21304ed131d0SYishai Hadas 	if (dyn_uar) {
21314ed131d0SYishai Hadas 		int uars_per_page;
21324ed131d0SYishai Hadas 
21334ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21344ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21354ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21364ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21374ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21384ed131d0SYishai Hadas 			return -EINVAL;
21394ed131d0SYishai Hadas 		}
21404ed131d0SYishai Hadas 
21414ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21424ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21434ed131d0SYishai Hadas 		 * page holds its count.
21444ed131d0SYishai Hadas 		 */
21454ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
21464ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
21474ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
21484ed131d0SYishai Hadas 			return -EINVAL;
21494ed131d0SYishai Hadas 		}
21504ed131d0SYishai Hadas 
21514ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
21524ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
21534ed131d0SYishai Hadas 
21544ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
21554ed131d0SYishai Hadas 		if (err) {
21564ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
21574ed131d0SYishai Hadas 			goto free_bfreg;
21584ed131d0SYishai Hadas 		}
21594ed131d0SYishai Hadas 	} else {
21604ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
21614ed131d0SYishai Hadas 	}
21624ed131d0SYishai Hadas 
21634ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
216437aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2165e126ba97SEli Cohen 
2166e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2167e2cd1d1aSJason Gunthorpe 				prot);
216837aa5c36SGuy Levi 	if (err) {
21698f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2170e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
21718f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
21724ed131d0SYishai Hadas 		goto err;
217337aa5c36SGuy Levi 	}
2174e126ba97SEli Cohen 
21754ed131d0SYishai Hadas 	if (dyn_uar)
21764ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
21774ed131d0SYishai Hadas 	return 0;
21784ed131d0SYishai Hadas 
21794ed131d0SYishai Hadas err:
21804ed131d0SYishai Hadas 	if (!dyn_uar)
21814ed131d0SYishai Hadas 		return err;
21824ed131d0SYishai Hadas 
21834ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
21844ed131d0SYishai Hadas 
21854ed131d0SYishai Hadas free_bfreg:
21864ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
21874ed131d0SYishai Hadas 
21884ed131d0SYishai Hadas 	return err;
218937aa5c36SGuy Levi }
219037aa5c36SGuy Levi 
219124da0016SAriel Levkovich static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
219224da0016SAriel Levkovich {
219324da0016SAriel Levkovich 	struct mlx5_ib_ucontext *mctx = to_mucontext(context);
219424da0016SAriel Levkovich 	struct mlx5_ib_dev *dev = to_mdev(context->device);
219524da0016SAriel Levkovich 	u16 page_idx = get_extended_index(vma->vm_pgoff);
219624da0016SAriel Levkovich 	size_t map_size = vma->vm_end - vma->vm_start;
219724da0016SAriel Levkovich 	u32 npages = map_size >> PAGE_SHIFT;
219824da0016SAriel Levkovich 	phys_addr_t pfn;
219924da0016SAriel Levkovich 
220024da0016SAriel Levkovich 	if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
220124da0016SAriel Levkovich 	    page_idx + npages)
220224da0016SAriel Levkovich 		return -EINVAL;
220324da0016SAriel Levkovich 
220424da0016SAriel Levkovich 	pfn = ((pci_resource_start(dev->mdev->pdev, 0) +
220524da0016SAriel Levkovich 	      MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
220624da0016SAriel Levkovich 	      PAGE_SHIFT) +
220724da0016SAriel Levkovich 	      page_idx;
2208e2cd1d1aSJason Gunthorpe 	return rdma_user_mmap_io(context, vma, pfn, map_size,
2209e2cd1d1aSJason Gunthorpe 				 pgprot_writecombine(vma->vm_page_prot));
221024da0016SAriel Levkovich }
221124da0016SAriel Levkovich 
221237aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
221337aa5c36SGuy Levi {
221437aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
221537aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
221637aa5c36SGuy Levi 	unsigned long command;
221737aa5c36SGuy Levi 	phys_addr_t pfn;
221837aa5c36SGuy Levi 
221937aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
222037aa5c36SGuy Levi 	switch (command) {
222137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
222237aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
222337aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
22244ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
22257c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2226e126ba97SEli Cohen 
2227e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2228e126ba97SEli Cohen 		return -ENOSYS;
2229e126ba97SEli Cohen 
2230d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2231d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2232d69e3bcfSMatan Barak 			return -EINVAL;
2233d69e3bcfSMatan Barak 
22346cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2235d69e3bcfSMatan Barak 			return -EPERM;
2236d69e3bcfSMatan Barak 
2237d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2238d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2239d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2240d69e3bcfSMatan Barak 
2241d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2242d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2243d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2244d69e3bcfSMatan Barak 			PAGE_SHIFT;
2245d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2246d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
2247d69e3bcfSMatan Barak 			return -EAGAIN;
2248d69e3bcfSMatan Barak 		break;
22495c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
22505c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2251d69e3bcfSMatan Barak 
225224da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
225324da0016SAriel Levkovich 		return dm_mmap(ibcontext, vma);
225424da0016SAriel Levkovich 
2255e126ba97SEli Cohen 	default:
2256e126ba97SEli Cohen 		return -EINVAL;
2257e126ba97SEli Cohen 	}
2258e126ba97SEli Cohen 
2259e126ba97SEli Cohen 	return 0;
2260e126ba97SEli Cohen }
2261e126ba97SEli Cohen 
226224da0016SAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
226324da0016SAriel Levkovich 			       struct ib_ucontext *context,
226424da0016SAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
226524da0016SAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
226624da0016SAriel Levkovich {
226724da0016SAriel Levkovich 	u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
226824da0016SAriel Levkovich 	struct mlx5_memic *memic = &to_mdev(ibdev)->memic;
226924da0016SAriel Levkovich 	phys_addr_t memic_addr;
227024da0016SAriel Levkovich 	struct mlx5_ib_dm *dm;
227124da0016SAriel Levkovich 	u64 start_offset;
227224da0016SAriel Levkovich 	u32 page_idx;
227324da0016SAriel Levkovich 	int err;
227424da0016SAriel Levkovich 
227524da0016SAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
227624da0016SAriel Levkovich 	if (!dm)
227724da0016SAriel Levkovich 		return ERR_PTR(-ENOMEM);
227824da0016SAriel Levkovich 
227924da0016SAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
228024da0016SAriel Levkovich 		    attr->length, act_size, attr->alignment);
228124da0016SAriel Levkovich 
228224da0016SAriel Levkovich 	err = mlx5_cmd_alloc_memic(memic, &memic_addr,
228324da0016SAriel Levkovich 				   act_size, attr->alignment);
228424da0016SAriel Levkovich 	if (err)
228524da0016SAriel Levkovich 		goto err_free;
228624da0016SAriel Levkovich 
228724da0016SAriel Levkovich 	start_offset = memic_addr & ~PAGE_MASK;
228824da0016SAriel Levkovich 	page_idx = (memic_addr - pci_resource_start(memic->dev->pdev, 0) -
228924da0016SAriel Levkovich 		    MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
229024da0016SAriel Levkovich 		    PAGE_SHIFT;
229124da0016SAriel Levkovich 
229224da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
229324da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
229424da0016SAriel Levkovich 			     &start_offset, sizeof(start_offset));
229524da0016SAriel Levkovich 	if (err)
229624da0016SAriel Levkovich 		goto err_dealloc;
229724da0016SAriel Levkovich 
229824da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
229924da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
230024da0016SAriel Levkovich 			     &page_idx, sizeof(page_idx));
230124da0016SAriel Levkovich 	if (err)
230224da0016SAriel Levkovich 		goto err_dealloc;
230324da0016SAriel Levkovich 
230424da0016SAriel Levkovich 	bitmap_set(to_mucontext(context)->dm_pages, page_idx,
230524da0016SAriel Levkovich 		   DIV_ROUND_UP(act_size, PAGE_SIZE));
230624da0016SAriel Levkovich 
230724da0016SAriel Levkovich 	dm->dev_addr = memic_addr;
230824da0016SAriel Levkovich 
230924da0016SAriel Levkovich 	return &dm->ibdm;
231024da0016SAriel Levkovich 
231124da0016SAriel Levkovich err_dealloc:
231224da0016SAriel Levkovich 	mlx5_cmd_dealloc_memic(memic, memic_addr,
231324da0016SAriel Levkovich 			       act_size);
231424da0016SAriel Levkovich err_free:
231524da0016SAriel Levkovich 	kfree(dm);
231624da0016SAriel Levkovich 	return ERR_PTR(err);
231724da0016SAriel Levkovich }
231824da0016SAriel Levkovich 
231924da0016SAriel Levkovich int mlx5_ib_dealloc_dm(struct ib_dm *ibdm)
232024da0016SAriel Levkovich {
232124da0016SAriel Levkovich 	struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic;
232224da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
232324da0016SAriel Levkovich 	u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE);
232424da0016SAriel Levkovich 	u32 page_idx;
232524da0016SAriel Levkovich 	int ret;
232624da0016SAriel Levkovich 
232724da0016SAriel Levkovich 	ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size);
232824da0016SAriel Levkovich 	if (ret)
232924da0016SAriel Levkovich 		return ret;
233024da0016SAriel Levkovich 
233124da0016SAriel Levkovich 	page_idx = (dm->dev_addr - pci_resource_start(memic->dev->pdev, 0) -
233224da0016SAriel Levkovich 		    MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
233324da0016SAriel Levkovich 		    PAGE_SHIFT;
233424da0016SAriel Levkovich 	bitmap_clear(to_mucontext(ibdm->uobject->context)->dm_pages,
233524da0016SAriel Levkovich 		     page_idx,
233624da0016SAriel Levkovich 		     DIV_ROUND_UP(act_size, PAGE_SIZE));
233724da0016SAriel Levkovich 
233824da0016SAriel Levkovich 	kfree(dm);
233924da0016SAriel Levkovich 
234024da0016SAriel Levkovich 	return 0;
234124da0016SAriel Levkovich }
234224da0016SAriel Levkovich 
234321a428a0SLeon Romanovsky static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_ucontext *context,
2344e126ba97SEli Cohen 			    struct ib_udata *udata)
2345e126ba97SEli Cohen {
234621a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
234721a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2348e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2349e126ba97SEli Cohen 	int err;
2350a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2351a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)]   = {};
2352a1069c1cSYishai Hadas 	u16 uid = 0;
2353e126ba97SEli Cohen 
235458895f0dSYishai Hadas 	uid = context ? to_mucontext(context)->devx_uid : 0;
2355a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2356a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
2357a1069c1cSYishai Hadas 	err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2358a1069c1cSYishai Hadas 			    out, sizeof(out));
235921a428a0SLeon Romanovsky 	if (err)
236021a428a0SLeon Romanovsky 		return err;
2361e126ba97SEli Cohen 
2362a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2363a1069c1cSYishai Hadas 	pd->uid = uid;
2364e126ba97SEli Cohen 	if (context) {
2365e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2366e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2367a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
236821a428a0SLeon Romanovsky 			return -EFAULT;
2369e126ba97SEli Cohen 		}
2370e126ba97SEli Cohen 	}
2371e126ba97SEli Cohen 
237221a428a0SLeon Romanovsky 	return 0;
2373e126ba97SEli Cohen }
2374e126ba97SEli Cohen 
237521a428a0SLeon Romanovsky static void mlx5_ib_dealloc_pd(struct ib_pd *pd)
2376e126ba97SEli Cohen {
2377e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2378e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2379e126ba97SEli Cohen 
2380a1069c1cSYishai Hadas 	mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2381e126ba97SEli Cohen }
2382e126ba97SEli Cohen 
2383466fa6d2SMaor Gottlieb enum {
2384466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2385466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
238671c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_INNER_BIT,
238771c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_MISC2_BIT
2388466fa6d2SMaor Gottlieb };
2389038d2ef8SMaor Gottlieb 
2390466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2391466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2392466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2393466fa6d2SMaor Gottlieb 
2394466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2395466fa6d2SMaor Gottlieb {
2396466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2397466fa6d2SMaor Gottlieb 
2398466fa6d2SMaor Gottlieb 	match_criteria_enable =
2399466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2400466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2401466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2402466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2403466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2404466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2405466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2406466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
240771c6e863SAriel Levkovich 	match_criteria_enable |=
240871c6e863SAriel Levkovich 		(!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
240971c6e863SAriel Levkovich 		MATCH_CRITERIA_ENABLE_MISC2_BIT;
2410466fa6d2SMaor Gottlieb 
2411466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2412038d2ef8SMaor Gottlieb }
2413038d2ef8SMaor Gottlieb 
24146113cc44SMaor Gottlieb static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2415ca0d4753SMaor Gottlieb {
24166113cc44SMaor Gottlieb 	u8 entry_mask;
24176113cc44SMaor Gottlieb 	u8 entry_val;
24186113cc44SMaor Gottlieb 	int err = 0;
24196113cc44SMaor Gottlieb 
24206113cc44SMaor Gottlieb 	if (!mask)
24216113cc44SMaor Gottlieb 		goto out;
24226113cc44SMaor Gottlieb 
24236113cc44SMaor Gottlieb 	entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
24246113cc44SMaor Gottlieb 			      ip_protocol);
24256113cc44SMaor Gottlieb 	entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
24266113cc44SMaor Gottlieb 			     ip_protocol);
24276113cc44SMaor Gottlieb 	if (!entry_mask) {
2428ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2429ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
24306113cc44SMaor Gottlieb 		goto out;
24316113cc44SMaor Gottlieb 	}
24326113cc44SMaor Gottlieb 	/* Don't override existing ip protocol */
24336113cc44SMaor Gottlieb 	if (mask != entry_mask || val != entry_val)
24346113cc44SMaor Gottlieb 		err = -EINVAL;
24356113cc44SMaor Gottlieb out:
24366113cc44SMaor Gottlieb 	return err;
2437ca0d4753SMaor Gottlieb }
2438ca0d4753SMaor Gottlieb 
243937da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
24402d1e697eSMoses Reuben 			   bool inner)
24412d1e697eSMoses Reuben {
24422d1e697eSMoses Reuben 	if (inner) {
24432d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24442d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
24452d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24462d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
24472d1e697eSMoses Reuben 	} else {
24482d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24492d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
24502d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24512d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
24522d1e697eSMoses Reuben 	}
24532d1e697eSMoses Reuben }
24542d1e697eSMoses Reuben 
2455ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2456ca0d4753SMaor Gottlieb {
2457ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2458ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2459ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2460ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2461ca0d4753SMaor Gottlieb }
2462ca0d4753SMaor Gottlieb 
246371c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
246471c6e863SAriel Levkovich {
246571c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
246671c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
246771c6e863SAriel Levkovich 		return -EOPNOTSUPP;
246871c6e863SAriel Levkovich 
246971c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
247071c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
247171c6e863SAriel Levkovich 		return -EOPNOTSUPP;
247271c6e863SAriel Levkovich 
247371c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
247471c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
247571c6e863SAriel Levkovich 		return -EOPNOTSUPP;
247671c6e863SAriel Levkovich 
247771c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
247871c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
247971c6e863SAriel Levkovich 		return -EOPNOTSUPP;
248071c6e863SAriel Levkovich 
248171c6e863SAriel Levkovich 	return 0;
248271c6e863SAriel Levkovich }
248371c6e863SAriel Levkovich 
2484c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2485c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2486ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2487466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2488c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2489ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
24902ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2491a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
24923b3233fbSRaed Salem #define LAST_COUNTERS_FIELD counters
2493c47ac6aeSMaor Gottlieb 
2494c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2495c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2496c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2497c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2498c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2499c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2500c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2501c47ac6aeSMaor Gottlieb 
25022ea26203SMark Bloch int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
25032ea26203SMark Bloch 			   bool is_egress,
2504802c2125SAviad Yehezkel 			   struct mlx5_flow_act *action)
2505802c2125SAviad Yehezkel {
2506802c2125SAviad Yehezkel 
2507802c2125SAviad Yehezkel 	switch (maction->ib_action.type) {
2508802c2125SAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
2509501f14e3SMark Bloch 		if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2510501f14e3SMark Bloch 				      MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2511501f14e3SMark Bloch 			return -EINVAL;
2512802c2125SAviad Yehezkel 		/* Currently only AES_GCM keymat is supported by the driver */
2513802c2125SAviad Yehezkel 		action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
25142ea26203SMark Bloch 		action->action |= is_egress ?
2515802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2516802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2517802c2125SAviad Yehezkel 		return 0;
2518b1085be3SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
2519b1085be3SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2520b1085be3SMark Bloch 		    MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
2521501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2522501f14e3SMark Bloch 				return -EINVAL;
2523b1085be3SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2524b1085be3SMark Bloch 			action->modify_id = maction->flow_action_raw.action_id;
2525b1085be3SMark Bloch 			return 0;
2526b1085be3SMark Bloch 		}
252710a30896SMark Bloch 		if (maction->flow_action_raw.sub_type ==
252810a30896SMark Bloch 		    MLX5_IB_FLOW_ACTION_DECAP) {
2529501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2530501f14e3SMark Bloch 				return -EINVAL;
253110a30896SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
253210a30896SMark Bloch 			return 0;
253310a30896SMark Bloch 		}
2534e806f932SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2535e806f932SMark Bloch 		    MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
2536501f14e3SMark Bloch 			if (action->action &
2537501f14e3SMark Bloch 			    MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2538501f14e3SMark Bloch 				return -EINVAL;
2539e806f932SMark Bloch 			action->action |=
2540e806f932SMark Bloch 				MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2541e806f932SMark Bloch 			action->reformat_id =
2542e806f932SMark Bloch 				maction->flow_action_raw.action_id;
2543e806f932SMark Bloch 			return 0;
2544e806f932SMark Bloch 		}
2545b1085be3SMark Bloch 		/* fall through */
2546802c2125SAviad Yehezkel 	default:
2547802c2125SAviad Yehezkel 		return -EOPNOTSUPP;
2548802c2125SAviad Yehezkel 	}
2549802c2125SAviad Yehezkel }
2550802c2125SAviad Yehezkel 
255119cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
255219cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
2553802c2125SAviad Yehezkel 			   const struct ib_flow_attr *flow_attr,
255471c6e863SAriel Levkovich 			   struct mlx5_flow_act *action, u32 prev_type)
2555038d2ef8SMaor Gottlieb {
2556466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2557466fa6d2SMaor Gottlieb 					   misc_parameters);
2558466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2559466fa6d2SMaor Gottlieb 					   misc_parameters);
256071c6e863SAriel Levkovich 	void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
256171c6e863SAriel Levkovich 					    misc_parameters_2);
256271c6e863SAriel Levkovich 	void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
256371c6e863SAriel Levkovich 					    misc_parameters_2);
25642d1e697eSMoses Reuben 	void *headers_c;
25652d1e697eSMoses Reuben 	void *headers_v;
256619cc7524SAriel Levkovich 	int match_ipv;
2567802c2125SAviad Yehezkel 	int ret;
2568466fa6d2SMaor Gottlieb 
25692d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
25702d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
25712d1e697eSMoses Reuben 					 inner_headers);
25722d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
25732d1e697eSMoses Reuben 					 inner_headers);
257419cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
257519cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
25762d1e697eSMoses Reuben 	} else {
25772d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
25782d1e697eSMoses Reuben 					 outer_headers);
25792d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
25802d1e697eSMoses Reuben 					 outer_headers);
258119cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
258219cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
25832d1e697eSMoses Reuben 	}
25842d1e697eSMoses Reuben 
25852d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2586038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2587c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
25881ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2589038d2ef8SMaor Gottlieb 
25902d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2591038d2ef8SMaor Gottlieb 					     dmac_47_16),
2592038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
25932d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2594038d2ef8SMaor Gottlieb 					     dmac_47_16),
2595038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2596038d2ef8SMaor Gottlieb 
25972d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2598ee3da804SMaor Gottlieb 					     smac_47_16),
2599ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
26002d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2601ee3da804SMaor Gottlieb 					     smac_47_16),
2602ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2603ee3da804SMaor Gottlieb 
2604038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
26052d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
260610543365SMohamad Haj Yahia 				 cvlan_tag, 1);
26072d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
260810543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2609038d2ef8SMaor Gottlieb 
26102d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2611038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
26122d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2613038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2614038d2ef8SMaor Gottlieb 
26152d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2616038d2ef8SMaor Gottlieb 				 first_cfi,
2617038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
26182d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2619038d2ef8SMaor Gottlieb 				 first_cfi,
2620038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2621038d2ef8SMaor Gottlieb 
26222d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2623038d2ef8SMaor Gottlieb 				 first_prio,
2624038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
26252d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2626038d2ef8SMaor Gottlieb 				 first_prio,
2627038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2628038d2ef8SMaor Gottlieb 		}
26292d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2630038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
26312d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2632038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2633038d2ef8SMaor Gottlieb 		break;
2634038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2635c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
26361ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2637038d2ef8SMaor Gottlieb 
263819cc7524SAriel Levkovich 		if (match_ipv) {
263919cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
264019cc7524SAriel Levkovich 				 ip_version, 0xf);
264119cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
26423346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV4_VERSION);
264319cc7524SAriel Levkovich 		} else {
26442d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2645038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
26462d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2647038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
264819cc7524SAriel Levkovich 		}
2649038d2ef8SMaor Gottlieb 
26502d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2651038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2652038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2653038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
26542d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2655038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2656038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2657038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
26582d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2659038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2660038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2661038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
26622d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2663038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2664038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2665038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2666ca0d4753SMaor Gottlieb 
26672d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2668ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2669ca0d4753SMaor Gottlieb 
26706113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
26716113cc44SMaor Gottlieb 			      ib_spec->ipv4.mask.proto,
26726113cc44SMaor Gottlieb 			      ib_spec->ipv4.val.proto))
26736113cc44SMaor Gottlieb 			return -EINVAL;
2674038d2ef8SMaor Gottlieb 		break;
2675026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2676c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
26771ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2678026bae0cSMaor Gottlieb 
267919cc7524SAriel Levkovich 		if (match_ipv) {
268019cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
268119cc7524SAriel Levkovich 				 ip_version, 0xf);
268219cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
26833346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV6_VERSION);
268419cc7524SAriel Levkovich 		} else {
26852d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2686026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
26872d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2688026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
268919cc7524SAriel Levkovich 		}
2690026bae0cSMaor Gottlieb 
26912d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2692026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2693026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2694026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
26952d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2696026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2697026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2698026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
26992d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2700026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2701026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2702026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
27032d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2704026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2705026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2706026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2707466fa6d2SMaor Gottlieb 
27082d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2709466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2710466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2711466fa6d2SMaor Gottlieb 
27126113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
2713466fa6d2SMaor Gottlieb 			      ib_spec->ipv6.mask.next_hdr,
27146113cc44SMaor Gottlieb 			      ib_spec->ipv6.val.next_hdr))
27156113cc44SMaor Gottlieb 			return -EINVAL;
2716466fa6d2SMaor Gottlieb 
27172d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
27182d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
27192d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
27202d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
2721802c2125SAviad Yehezkel 		break;
2722802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ESP:
2723802c2125SAviad Yehezkel 		if (ib_spec->esp.mask.seq)
2724802c2125SAviad Yehezkel 			return -EOPNOTSUPP;
27252d1e697eSMoses Reuben 
2726802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2727802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.mask.spi));
2728802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2729802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.val.spi));
2730026bae0cSMaor Gottlieb 		break;
2731038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2732c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2733c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
27341ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2735038d2ef8SMaor Gottlieb 
27366113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
27376113cc44SMaor Gottlieb 			return -EINVAL;
2738038d2ef8SMaor Gottlieb 
27392d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2740038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
27412d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2742038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2743038d2ef8SMaor Gottlieb 
27442d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2745038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
27462d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2747038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2748038d2ef8SMaor Gottlieb 		break;
2749038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2750c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2751c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
27521ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2753038d2ef8SMaor Gottlieb 
27546113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
27556113cc44SMaor Gottlieb 			return -EINVAL;
2756038d2ef8SMaor Gottlieb 
27572d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2758038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
27592d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2760038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2761038d2ef8SMaor Gottlieb 
27622d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2763038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
27642d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2765038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2766038d2ef8SMaor Gottlieb 		break;
2767da2f22aeSAriel Levkovich 	case IB_FLOW_SPEC_GRE:
2768da2f22aeSAriel Levkovich 		if (ib_spec->gre.mask.c_ks_res0_ver)
2769da2f22aeSAriel Levkovich 			return -EOPNOTSUPP;
2770da2f22aeSAriel Levkovich 
27716113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
27726113cc44SMaor Gottlieb 			return -EINVAL;
27736113cc44SMaor Gottlieb 
2774da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2775da2f22aeSAriel Levkovich 			 0xff);
2776da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2777da2f22aeSAriel Levkovich 			 IPPROTO_GRE);
2778da2f22aeSAriel Levkovich 
2779da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
2780a93b632cSMaor Gottlieb 			 ntohs(ib_spec->gre.mask.protocol));
2781da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2782da2f22aeSAriel Levkovich 			 ntohs(ib_spec->gre.val.protocol));
2783da2f22aeSAriel Levkovich 
2784da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
27855886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2786da2f22aeSAriel Levkovich 		       &ib_spec->gre.mask.key,
2787da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.mask.key));
2788da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
27895886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2790da2f22aeSAriel Levkovich 		       &ib_spec->gre.val.key,
2791da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.val.key));
2792da2f22aeSAriel Levkovich 		break;
279371c6e863SAriel Levkovich 	case IB_FLOW_SPEC_MPLS:
279471c6e863SAriel Levkovich 		switch (prev_type) {
279571c6e863SAriel Levkovich 		case IB_FLOW_SPEC_UDP:
279671c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
279771c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_udp),
279871c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
279971c6e863SAriel Levkovich 				return -EOPNOTSUPP;
280071c6e863SAriel Levkovich 
280171c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
280271c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
280371c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
280471c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
280571c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
280671c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
280771c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
280871c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
280971c6e863SAriel Levkovich 			break;
281071c6e863SAriel Levkovich 		case IB_FLOW_SPEC_GRE:
281171c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
281271c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_gre),
281371c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
281471c6e863SAriel Levkovich 				return -EOPNOTSUPP;
281571c6e863SAriel Levkovich 
281671c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
281771c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
281871c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
281971c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
282071c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
282171c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
282271c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
282371c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
282471c6e863SAriel Levkovich 			break;
282571c6e863SAriel Levkovich 		default:
282671c6e863SAriel Levkovich 			if (ib_spec->type & IB_FLOW_SPEC_INNER) {
282771c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
282871c6e863SAriel Levkovich 							   ft_field_support.inner_first_mpls),
282971c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
283071c6e863SAriel Levkovich 					return -EOPNOTSUPP;
283171c6e863SAriel Levkovich 
283271c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
283371c6e863SAriel Levkovich 						    inner_first_mpls),
283471c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
283571c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
283671c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
283771c6e863SAriel Levkovich 						    inner_first_mpls),
283871c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
283971c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
284071c6e863SAriel Levkovich 			} else {
284171c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
284271c6e863SAriel Levkovich 							   ft_field_support.outer_first_mpls),
284371c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
284471c6e863SAriel Levkovich 					return -EOPNOTSUPP;
284571c6e863SAriel Levkovich 
284671c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
284771c6e863SAriel Levkovich 						    outer_first_mpls),
284871c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
284971c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
285071c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
285171c6e863SAriel Levkovich 						    outer_first_mpls),
285271c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
285371c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
285471c6e863SAriel Levkovich 			}
285571c6e863SAriel Levkovich 		}
285671c6e863SAriel Levkovich 		break;
2857ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2858ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2859ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
28601ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2861ffb30d8fSMoses Reuben 
2862ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2863ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2864ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2865ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2866ffb30d8fSMoses Reuben 		break;
28672ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
28682ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
28692ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
28702ac693f9SMoses Reuben 			return -EOPNOTSUPP;
28712ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
28722ac693f9SMoses Reuben 			return -EINVAL;
28732ac693f9SMoses Reuben 
2874075572d4SBoris Pismenny 		action->flow_tag = ib_spec->flow_tag.tag_id;
2875d5634feeSPaul Blakey 		action->flags |= FLOW_ACT_HAS_TAG;
28762ac693f9SMoses Reuben 		break;
2877a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2878a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2879a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2880a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2881075572d4SBoris Pismenny 		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
2882a22ed86cSSlava Shwartsman 		break;
2883802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ACTION_HANDLE:
28842ea26203SMark Bloch 		ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
28852ea26203SMark Bloch 			flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
2886802c2125SAviad Yehezkel 		if (ret)
2887802c2125SAviad Yehezkel 			return ret;
2888802c2125SAviad Yehezkel 		break;
28893b3233fbSRaed Salem 	case IB_FLOW_SPEC_ACTION_COUNT:
28903b3233fbSRaed Salem 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
28913b3233fbSRaed Salem 					 LAST_COUNTERS_FIELD))
28923b3233fbSRaed Salem 			return -EOPNOTSUPP;
28933b3233fbSRaed Salem 
28943b3233fbSRaed Salem 		/* for now support only one counters spec per flow */
28953b3233fbSRaed Salem 		if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
28963b3233fbSRaed Salem 			return -EINVAL;
28973b3233fbSRaed Salem 
28983b3233fbSRaed Salem 		action->counters = ib_spec->flow_count.counters;
28993b3233fbSRaed Salem 		action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
29003b3233fbSRaed Salem 		break;
2901038d2ef8SMaor Gottlieb 	default:
2902038d2ef8SMaor Gottlieb 		return -EINVAL;
2903038d2ef8SMaor Gottlieb 	}
2904038d2ef8SMaor Gottlieb 
2905038d2ef8SMaor Gottlieb 	return 0;
2906038d2ef8SMaor Gottlieb }
2907038d2ef8SMaor Gottlieb 
2908038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
2909038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
2910038d2ef8SMaor Gottlieb  * could steal other multicast packets.
2911038d2ef8SMaor Gottlieb  */
2912a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2913038d2ef8SMaor Gottlieb {
291481e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
2915038d2ef8SMaor Gottlieb 
2916038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2917038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
2918038d2ef8SMaor Gottlieb 		return false;
2919038d2ef8SMaor Gottlieb 
292081e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
292181e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
292281e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
2923038d2ef8SMaor Gottlieb 
292481e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
292581e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
292681e30880SYishai Hadas 			return true;
292781e30880SYishai Hadas 
292881e30880SYishai Hadas 		return false;
292981e30880SYishai Hadas 	}
293081e30880SYishai Hadas 
293181e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
293281e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
293381e30880SYishai Hadas 
293481e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2935038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2936038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
2937038d2ef8SMaor Gottlieb 	}
2938038d2ef8SMaor Gottlieb 
293981e30880SYishai Hadas 	return false;
294081e30880SYishai Hadas }
294181e30880SYishai Hadas 
2942802c2125SAviad Yehezkel enum valid_spec {
2943802c2125SAviad Yehezkel 	VALID_SPEC_INVALID,
2944802c2125SAviad Yehezkel 	VALID_SPEC_VALID,
2945802c2125SAviad Yehezkel 	VALID_SPEC_NA,
2946802c2125SAviad Yehezkel };
2947802c2125SAviad Yehezkel 
2948802c2125SAviad Yehezkel static enum valid_spec
2949802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
2950802c2125SAviad Yehezkel 		     const struct mlx5_flow_spec *spec,
2951802c2125SAviad Yehezkel 		     const struct mlx5_flow_act *flow_act,
2952802c2125SAviad Yehezkel 		     bool egress)
2953802c2125SAviad Yehezkel {
2954802c2125SAviad Yehezkel 	const u32 *match_c = spec->match_criteria;
2955802c2125SAviad Yehezkel 	bool is_crypto =
2956802c2125SAviad Yehezkel 		(flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2957802c2125SAviad Yehezkel 				     MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
2958802c2125SAviad Yehezkel 	bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
2959802c2125SAviad Yehezkel 	bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
2960802c2125SAviad Yehezkel 
2961802c2125SAviad Yehezkel 	/*
2962802c2125SAviad Yehezkel 	 * Currently only crypto is supported in egress, when regular egress
2963802c2125SAviad Yehezkel 	 * rules would be supported, always return VALID_SPEC_NA.
2964802c2125SAviad Yehezkel 	 */
2965802c2125SAviad Yehezkel 	if (!is_crypto)
296678dd0c43SMark Bloch 		return VALID_SPEC_NA;
2967802c2125SAviad Yehezkel 
2968802c2125SAviad Yehezkel 	return is_crypto && is_ipsec &&
2969d5634feeSPaul Blakey 		(!egress || (!is_drop && !(flow_act->flags & FLOW_ACT_HAS_TAG))) ?
2970802c2125SAviad Yehezkel 		VALID_SPEC_VALID : VALID_SPEC_INVALID;
2971802c2125SAviad Yehezkel }
2972802c2125SAviad Yehezkel 
2973802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev,
2974802c2125SAviad Yehezkel 			  const struct mlx5_flow_spec *spec,
2975802c2125SAviad Yehezkel 			  const struct mlx5_flow_act *flow_act,
2976802c2125SAviad Yehezkel 			  bool egress)
2977802c2125SAviad Yehezkel {
2978802c2125SAviad Yehezkel 	/* We curretly only support ipsec egress flow */
2979802c2125SAviad Yehezkel 	return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
2980802c2125SAviad Yehezkel }
2981802c2125SAviad Yehezkel 
298219cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
298319cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
29840f750966SAriel Levkovich 			       bool check_inner)
2985038d2ef8SMaor Gottlieb {
2986038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
298719cc7524SAriel Levkovich 	int match_ipv = check_inner ?
298819cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
298919cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
299019cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
299119cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
29920f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
29930f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
29940f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
29950f750966SAriel Levkovich 	bool has_ethertype = false;
2996038d2ef8SMaor Gottlieb 	unsigned int spec_index;
29970f750966SAriel Levkovich 	bool mask_valid = true;
29980f750966SAriel Levkovich 	u16 eth_type = 0;
29990f750966SAriel Levkovich 	bool type_valid;
3000038d2ef8SMaor Gottlieb 
3001038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
3002038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
30030f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
3004038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
30050f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
30060f750966SAriel Levkovich 				      htons(0xffff));
30070f750966SAriel Levkovich 			has_ethertype = true;
30080f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
30090f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
30100f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
30110f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
3012038d2ef8SMaor Gottlieb 		}
3013038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
3014038d2ef8SMaor Gottlieb 	}
30150f750966SAriel Levkovich 
30160f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
30170f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
30180f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
30190f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
30200f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
30210f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
302219cc7524SAriel Levkovich 
302319cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
302419cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
302519cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
30260f750966SAriel Levkovich 	}
30270f750966SAriel Levkovich 
30280f750966SAriel Levkovich 	return type_valid;
30290f750966SAriel Levkovich }
30300f750966SAriel Levkovich 
303119cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
303219cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
30330f750966SAriel Levkovich {
303419cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
303519cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
3036038d2ef8SMaor Gottlieb }
3037038d2ef8SMaor Gottlieb 
3038038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
3039038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
3040038d2ef8SMaor Gottlieb {
3041038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
3042038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
3043038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
3044038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
3045038d2ef8SMaor Gottlieb 	}
3046038d2ef8SMaor Gottlieb }
3047038d2ef8SMaor Gottlieb 
30483b3233fbSRaed Salem static void counters_clear_description(struct ib_counters *counters)
30493b3233fbSRaed Salem {
30503b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
30513b3233fbSRaed Salem 
30523b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
30533b3233fbSRaed Salem 	kfree(mcounters->counters_data);
30543b3233fbSRaed Salem 	mcounters->counters_data = NULL;
30553b3233fbSRaed Salem 	mcounters->cntrs_max_index = 0;
30563b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
30573b3233fbSRaed Salem }
30583b3233fbSRaed Salem 
3059038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3060038d2ef8SMaor Gottlieb {
3061038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3062038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
3063038d2ef8SMaor Gottlieb 							  ibflow);
3064038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
3065d4be3f44SYishai Hadas 	struct mlx5_ib_dev *dev = handler->dev;
3066038d2ef8SMaor Gottlieb 
30679a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3068038d2ef8SMaor Gottlieb 
3069038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
307074491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
3071cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
3072038d2ef8SMaor Gottlieb 		list_del(&iter->list);
3073038d2ef8SMaor Gottlieb 		kfree(iter);
3074038d2ef8SMaor Gottlieb 	}
3075038d2ef8SMaor Gottlieb 
307674491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
30775497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
30783b3233fbSRaed Salem 	if (handler->ibcounters &&
30793b3233fbSRaed Salem 	    atomic_read(&handler->ibcounters->usecnt) == 1)
30803b3233fbSRaed Salem 		counters_clear_description(handler->ibcounters);
3081038d2ef8SMaor Gottlieb 
30823b3233fbSRaed Salem 	mutex_unlock(&dev->flow_db->lock);
3083d4be3f44SYishai Hadas 	if (handler->flow_matcher)
3084d4be3f44SYishai Hadas 		atomic_dec(&handler->flow_matcher->usecnt);
3085038d2ef8SMaor Gottlieb 	kfree(handler);
3086038d2ef8SMaor Gottlieb 
3087038d2ef8SMaor Gottlieb 	return 0;
3088038d2ef8SMaor Gottlieb }
3089038d2ef8SMaor Gottlieb 
309035d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
309135d19011SMaor Gottlieb {
309235d19011SMaor Gottlieb 	priority *= 2;
309335d19011SMaor Gottlieb 	if (!dont_trap)
309435d19011SMaor Gottlieb 		priority++;
309535d19011SMaor Gottlieb 	return priority;
309635d19011SMaor Gottlieb }
309735d19011SMaor Gottlieb 
3098cc0e5d42SMaor Gottlieb enum flow_table_type {
3099cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
3100cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
3101cc0e5d42SMaor Gottlieb };
3102cc0e5d42SMaor Gottlieb 
310300b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
310400b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
3105d4be3f44SYishai Hadas 
3106d4be3f44SYishai Hadas static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3107d4be3f44SYishai Hadas 					   struct mlx5_ib_flow_prio *prio,
3108d4be3f44SYishai Hadas 					   int priority,
31094adda112SMark Bloch 					   int num_entries, int num_groups,
31104adda112SMark Bloch 					   u32 flags)
3111d4be3f44SYishai Hadas {
3112d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft;
3113d4be3f44SYishai Hadas 
3114d4be3f44SYishai Hadas 	ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3115d4be3f44SYishai Hadas 						 num_entries,
3116d4be3f44SYishai Hadas 						 num_groups,
31174adda112SMark Bloch 						 0, flags);
3118d4be3f44SYishai Hadas 	if (IS_ERR(ft))
3119d4be3f44SYishai Hadas 		return ERR_CAST(ft);
3120d4be3f44SYishai Hadas 
3121d4be3f44SYishai Hadas 	prio->flow_table = ft;
3122d4be3f44SYishai Hadas 	prio->refcount = 0;
3123d4be3f44SYishai Hadas 	return prio;
3124d4be3f44SYishai Hadas }
3125d4be3f44SYishai Hadas 
3126038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
3127cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
3128cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
3129038d2ef8SMaor Gottlieb {
313035d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
3131038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
3132038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
3133038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
3134dac388efSMaor Gottlieb 	int max_table_size;
3135038d2ef8SMaor Gottlieb 	int num_entries;
3136038d2ef8SMaor Gottlieb 	int num_groups;
31374adda112SMark Bloch 	u32 flags = 0;
3138038d2ef8SMaor Gottlieb 	int priority;
3139038d2ef8SMaor Gottlieb 
3140dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3141dac388efSMaor Gottlieb 						       log_max_ft_size));
3142038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
314378dd0c43SMark Bloch 		enum mlx5_flow_namespace_type fn_type;
314478dd0c43SMark Bloch 
314578dd0c43SMark Bloch 		if (flow_is_multicast_only(flow_attr) &&
314635d19011SMaor Gottlieb 		    !dont_trap)
3147038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
3148038d2ef8SMaor Gottlieb 		else
314935d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
315035d19011SMaor Gottlieb 							dont_trap);
315178dd0c43SMark Bloch 		if (ft_type == MLX5_IB_FT_RX) {
315278dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
315378dd0c43SMark Bloch 			prio = &dev->flow_db->prios[priority];
31544adda112SMark Bloch 			if (!dev->rep &&
31554adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
31564adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
31575c2db53fSMark Bloch 			if (!dev->rep &&
31585c2db53fSMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
31595c2db53fSMark Bloch 					reformat_l3_tunnel_to_l2))
31605c2db53fSMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
316178dd0c43SMark Bloch 		} else {
316278dd0c43SMark Bloch 			max_table_size =
316378dd0c43SMark Bloch 				BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
316478dd0c43SMark Bloch 							      log_max_ft_size));
316578dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
316678dd0c43SMark Bloch 			prio = &dev->flow_db->egress_prios[priority];
31674adda112SMark Bloch 			if (!dev->rep &&
31684adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
31694adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
317078dd0c43SMark Bloch 		}
317178dd0c43SMark Bloch 		ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
3172038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
3173038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
3174038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3175038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3176038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
3177038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
3178038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
3179038d2ef8SMaor Gottlieb 					 &num_entries,
3180038d2ef8SMaor Gottlieb 					 &num_groups);
31819a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
3182cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3183cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3184cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
3185cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
3186cc0e5d42SMaor Gottlieb 
3187cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3188cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3189cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3190cc0e5d42SMaor Gottlieb 
31919a4ca38dSMark Bloch 		prio = &dev->flow_db->sniffer[ft_type];
3192cc0e5d42SMaor Gottlieb 		priority = 0;
3193cc0e5d42SMaor Gottlieb 		num_entries = 1;
3194cc0e5d42SMaor Gottlieb 		num_groups = 1;
3195038d2ef8SMaor Gottlieb 	}
3196038d2ef8SMaor Gottlieb 
3197038d2ef8SMaor Gottlieb 	if (!ns)
3198038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
3199038d2ef8SMaor Gottlieb 
3200dac388efSMaor Gottlieb 	if (num_entries > max_table_size)
3201dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
3202dac388efSMaor Gottlieb 
3203038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
3204d4be3f44SYishai Hadas 	if (!ft)
32054adda112SMark Bloch 		return _get_prio(ns, prio, priority, num_entries, num_groups,
32064adda112SMark Bloch 				 flags);
3207038d2ef8SMaor Gottlieb 
3208d4be3f44SYishai Hadas 	return prio;
3209038d2ef8SMaor Gottlieb }
3210038d2ef8SMaor Gottlieb 
3211a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
3212a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
3213a550ddfcSYishai Hadas 			    u32 underlay_qpn)
3214a550ddfcSYishai Hadas {
3215a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3216a550ddfcSYishai Hadas 					   spec->match_criteria,
3217a550ddfcSYishai Hadas 					   misc_parameters);
3218a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3219a550ddfcSYishai Hadas 					   misc_parameters);
3220a550ddfcSYishai Hadas 
3221a550ddfcSYishai Hadas 	if (underlay_qpn &&
3222a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3223a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
3224a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3225a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
3226a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3227a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
3228a550ddfcSYishai Hadas 	}
3229a550ddfcSYishai Hadas }
3230a550ddfcSYishai Hadas 
32315e95af5fSRaed Salem static int read_flow_counters(struct ib_device *ibdev,
32325e95af5fSRaed Salem 			      struct mlx5_read_counters_attr *read_attr)
32335e95af5fSRaed Salem {
32345e95af5fSRaed Salem 	struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
32355e95af5fSRaed Salem 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
32365e95af5fSRaed Salem 
32375e95af5fSRaed Salem 	return mlx5_fc_query(dev->mdev, fc,
32385e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_PACKETS],
32395e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_BYTES]);
32405e95af5fSRaed Salem }
32415e95af5fSRaed Salem 
32425e95af5fSRaed Salem /* flow counters currently expose two counters packets and bytes */
32435e95af5fSRaed Salem #define FLOW_COUNTERS_NUM 2
32443b3233fbSRaed Salem static int counters_set_description(struct ib_counters *counters,
32453b3233fbSRaed Salem 				    enum mlx5_ib_counters_type counters_type,
32463b3233fbSRaed Salem 				    struct mlx5_ib_flow_counters_desc *desc_data,
32473b3233fbSRaed Salem 				    u32 ncounters)
32483b3233fbSRaed Salem {
32493b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
32503b3233fbSRaed Salem 	u32 cntrs_max_index = 0;
32513b3233fbSRaed Salem 	int i;
32523b3233fbSRaed Salem 
32533b3233fbSRaed Salem 	if (counters_type != MLX5_IB_COUNTERS_FLOW)
32543b3233fbSRaed Salem 		return -EINVAL;
32553b3233fbSRaed Salem 
32563b3233fbSRaed Salem 	/* init the fields for the object */
32573b3233fbSRaed Salem 	mcounters->type = counters_type;
32585e95af5fSRaed Salem 	mcounters->read_counters = read_flow_counters;
32595e95af5fSRaed Salem 	mcounters->counters_num = FLOW_COUNTERS_NUM;
32603b3233fbSRaed Salem 	mcounters->ncounters = ncounters;
32613b3233fbSRaed Salem 	/* each counter entry have both description and index pair */
32623b3233fbSRaed Salem 	for (i = 0; i < ncounters; i++) {
32633b3233fbSRaed Salem 		if (desc_data[i].description > IB_COUNTER_BYTES)
32643b3233fbSRaed Salem 			return -EINVAL;
32653b3233fbSRaed Salem 
32663b3233fbSRaed Salem 		if (cntrs_max_index <= desc_data[i].index)
32673b3233fbSRaed Salem 			cntrs_max_index = desc_data[i].index + 1;
32683b3233fbSRaed Salem 	}
32693b3233fbSRaed Salem 
32703b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
32713b3233fbSRaed Salem 	mcounters->counters_data = desc_data;
32723b3233fbSRaed Salem 	mcounters->cntrs_max_index = cntrs_max_index;
32733b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
32743b3233fbSRaed Salem 
32753b3233fbSRaed Salem 	return 0;
32763b3233fbSRaed Salem }
32773b3233fbSRaed Salem 
32783b3233fbSRaed Salem #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
32793b3233fbSRaed Salem static int flow_counters_set_data(struct ib_counters *ibcounters,
32803b3233fbSRaed Salem 				  struct mlx5_ib_create_flow *ucmd)
32813b3233fbSRaed Salem {
32823b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
32833b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
32843b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc_data = NULL;
32853b3233fbSRaed Salem 	bool hw_hndl = false;
32863b3233fbSRaed Salem 	int ret = 0;
32873b3233fbSRaed Salem 
32883b3233fbSRaed Salem 	if (ucmd && ucmd->ncounters_data != 0) {
32893b3233fbSRaed Salem 		cntrs_data = ucmd->data;
32903b3233fbSRaed Salem 		if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
32913b3233fbSRaed Salem 			return -EINVAL;
32923b3233fbSRaed Salem 
32933b3233fbSRaed Salem 		desc_data = kcalloc(cntrs_data->ncounters,
32943b3233fbSRaed Salem 				    sizeof(*desc_data),
32953b3233fbSRaed Salem 				    GFP_KERNEL);
32963b3233fbSRaed Salem 		if (!desc_data)
32973b3233fbSRaed Salem 			return  -ENOMEM;
32983b3233fbSRaed Salem 
32993b3233fbSRaed Salem 		if (copy_from_user(desc_data,
33003b3233fbSRaed Salem 				   u64_to_user_ptr(cntrs_data->counters_data),
33013b3233fbSRaed Salem 				   sizeof(*desc_data) * cntrs_data->ncounters)) {
33023b3233fbSRaed Salem 			ret = -EFAULT;
33033b3233fbSRaed Salem 			goto free;
33043b3233fbSRaed Salem 		}
33053b3233fbSRaed Salem 	}
33063b3233fbSRaed Salem 
33073b3233fbSRaed Salem 	if (!mcounters->hw_cntrs_hndl) {
33083b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = mlx5_fc_create(
33093b3233fbSRaed Salem 			to_mdev(ibcounters->device)->mdev, false);
3310e31abf76Sweiyongjun (A) 		if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3311e31abf76Sweiyongjun (A) 			ret = PTR_ERR(mcounters->hw_cntrs_hndl);
33123b3233fbSRaed Salem 			goto free;
33133b3233fbSRaed Salem 		}
33143b3233fbSRaed Salem 		hw_hndl = true;
33153b3233fbSRaed Salem 	}
33163b3233fbSRaed Salem 
33173b3233fbSRaed Salem 	if (desc_data) {
33183b3233fbSRaed Salem 		/* counters already bound to at least one flow */
33193b3233fbSRaed Salem 		if (mcounters->cntrs_max_index) {
33203b3233fbSRaed Salem 			ret = -EINVAL;
33213b3233fbSRaed Salem 			goto free_hndl;
33223b3233fbSRaed Salem 		}
33233b3233fbSRaed Salem 
33243b3233fbSRaed Salem 		ret = counters_set_description(ibcounters,
33253b3233fbSRaed Salem 					       MLX5_IB_COUNTERS_FLOW,
33263b3233fbSRaed Salem 					       desc_data,
33273b3233fbSRaed Salem 					       cntrs_data->ncounters);
33283b3233fbSRaed Salem 		if (ret)
33293b3233fbSRaed Salem 			goto free_hndl;
33303b3233fbSRaed Salem 
33313b3233fbSRaed Salem 	} else if (!mcounters->cntrs_max_index) {
33323b3233fbSRaed Salem 		/* counters not bound yet, must have udata passed */
33333b3233fbSRaed Salem 		ret = -EINVAL;
33343b3233fbSRaed Salem 		goto free_hndl;
33353b3233fbSRaed Salem 	}
33363b3233fbSRaed Salem 
33373b3233fbSRaed Salem 	return 0;
33383b3233fbSRaed Salem 
33393b3233fbSRaed Salem free_hndl:
33403b3233fbSRaed Salem 	if (hw_hndl) {
33413b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
33423b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
33433b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = NULL;
33443b3233fbSRaed Salem 	}
33453b3233fbSRaed Salem free:
33463b3233fbSRaed Salem 	kfree(desc_data);
33473b3233fbSRaed Salem 	return ret;
33483b3233fbSRaed Salem }
33493b3233fbSRaed Salem 
3350a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3351038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
3352dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
3353a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
33543b3233fbSRaed Salem 						      u32 underlay_qpn,
33553b3233fbSRaed Salem 						      struct mlx5_ib_create_flow *ucmd)
3356038d2ef8SMaor Gottlieb {
3357038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
3358038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
3359075572d4SBoris Pismenny 	struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
3360c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
33613b3233fbSRaed Salem 	struct mlx5_flow_destination dest_arr[2] = {};
33623b3233fbSRaed Salem 	struct mlx5_flow_destination *rule_dst = dest_arr;
3363dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3364038d2ef8SMaor Gottlieb 	unsigned int spec_index;
336571c6e863SAriel Levkovich 	u32 prev_type = 0;
3366038d2ef8SMaor Gottlieb 	int err = 0;
33673b3233fbSRaed Salem 	int dest_num = 0;
3368802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3369038d2ef8SMaor Gottlieb 
337019cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
3371038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3372038d2ef8SMaor Gottlieb 
337378dd0c43SMark Bloch 	if (dev->rep && is_egress)
337478dd0c43SMark Bloch 		return ERR_PTR(-EINVAL);
337578dd0c43SMark Bloch 
33761b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3377038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3378c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
3379038d2ef8SMaor Gottlieb 		err = -ENOMEM;
3380038d2ef8SMaor Gottlieb 		goto free;
3381038d2ef8SMaor Gottlieb 	}
3382038d2ef8SMaor Gottlieb 
3383038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
33843b3233fbSRaed Salem 	if (dst) {
33853b3233fbSRaed Salem 		memcpy(&dest_arr[0], dst, sizeof(*dst));
33863b3233fbSRaed Salem 		dest_num++;
33873b3233fbSRaed Salem 	}
3388038d2ef8SMaor Gottlieb 
3389038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
339019cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
3391a22ed86cSSlava Shwartsman 				      spec->match_value,
339271c6e863SAriel Levkovich 				      ib_flow, flow_attr, &flow_act,
339371c6e863SAriel Levkovich 				      prev_type);
3394038d2ef8SMaor Gottlieb 		if (err < 0)
3395038d2ef8SMaor Gottlieb 			goto free;
3396038d2ef8SMaor Gottlieb 
339771c6e863SAriel Levkovich 		prev_type = ((union ib_flow_spec *)ib_flow)->type;
3398038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3399038d2ef8SMaor Gottlieb 	}
3400038d2ef8SMaor Gottlieb 
3401a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
3402a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
3403a550ddfcSYishai Hadas 
3404018a94eeSMark Bloch 	if (dev->rep) {
3405018a94eeSMark Bloch 		void *misc;
3406018a94eeSMark Bloch 
3407018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3408018a94eeSMark Bloch 				    misc_parameters);
3409018a94eeSMark Bloch 		MLX5_SET(fte_match_set_misc, misc, source_port,
3410018a94eeSMark Bloch 			 dev->rep->vport);
3411018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3412018a94eeSMark Bloch 				    misc_parameters);
3413018a94eeSMark Bloch 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3414018a94eeSMark Bloch 	}
3415018a94eeSMark Bloch 
3416466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3417802c2125SAviad Yehezkel 
3418802c2125SAviad Yehezkel 	if (is_egress &&
3419802c2125SAviad Yehezkel 	    !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3420802c2125SAviad Yehezkel 		err = -EINVAL;
3421802c2125SAviad Yehezkel 		goto free;
3422802c2125SAviad Yehezkel 	}
3423802c2125SAviad Yehezkel 
34243b3233fbSRaed Salem 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3425171c7625SMark Bloch 		struct mlx5_ib_mcounters *mcounters;
3426171c7625SMark Bloch 
34273b3233fbSRaed Salem 		err = flow_counters_set_data(flow_act.counters, ucmd);
34283b3233fbSRaed Salem 		if (err)
34293b3233fbSRaed Salem 			goto free;
34303b3233fbSRaed Salem 
3431171c7625SMark Bloch 		mcounters = to_mcounters(flow_act.counters);
34323b3233fbSRaed Salem 		handler->ibcounters = flow_act.counters;
34333b3233fbSRaed Salem 		dest_arr[dest_num].type =
34343b3233fbSRaed Salem 			MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3435171c7625SMark Bloch 		dest_arr[dest_num].counter_id =
3436171c7625SMark Bloch 			mlx5_fc_id(mcounters->hw_cntrs_hndl);
34373b3233fbSRaed Salem 		dest_num++;
34383b3233fbSRaed Salem 	}
34393b3233fbSRaed Salem 
3440075572d4SBoris Pismenny 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
34413b3233fbSRaed Salem 		if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3442a22ed86cSSlava Shwartsman 			rule_dst = NULL;
3443a22ed86cSSlava Shwartsman 			dest_num = 0;
34443b3233fbSRaed Salem 		}
3445a22ed86cSSlava Shwartsman 	} else {
3446802c2125SAviad Yehezkel 		if (is_egress)
3447802c2125SAviad Yehezkel 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3448802c2125SAviad Yehezkel 		else
3449802c2125SAviad Yehezkel 			flow_act.action |=
34503b3233fbSRaed Salem 				dest_num ?  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
345135d19011SMaor Gottlieb 					MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3452a22ed86cSSlava Shwartsman 	}
34532ac693f9SMoses Reuben 
3454d5634feeSPaul Blakey 	if ((flow_act.flags & FLOW_ACT_HAS_TAG)  &&
34552ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
34562ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
34572ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3458075572d4SBoris Pismenny 			     flow_act.flow_tag, flow_attr->type);
34592ac693f9SMoses Reuben 		err = -EINVAL;
34602ac693f9SMoses Reuben 		goto free;
34612ac693f9SMoses Reuben 	}
346274491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
346366958ed9SHadar Hen Zion 					    &flow_act,
3464a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
3465038d2ef8SMaor Gottlieb 
3466038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
3467038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
3468038d2ef8SMaor Gottlieb 		goto free;
3469038d2ef8SMaor Gottlieb 	}
3470038d2ef8SMaor Gottlieb 
3471d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
34725497adc6SMaor Gottlieb 	handler->prio = ft_prio;
3473d4be3f44SYishai Hadas 	handler->dev = dev;
3474038d2ef8SMaor Gottlieb 
3475038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
3476038d2ef8SMaor Gottlieb free:
34773b3233fbSRaed Salem 	if (err && handler) {
34783b3233fbSRaed Salem 		if (handler->ibcounters &&
34793b3233fbSRaed Salem 		    atomic_read(&handler->ibcounters->usecnt) == 1)
34803b3233fbSRaed Salem 			counters_clear_description(handler->ibcounters);
3481038d2ef8SMaor Gottlieb 		kfree(handler);
34823b3233fbSRaed Salem 	}
3483c5bb1730SMaor Gottlieb 	kvfree(spec);
3484038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
3485038d2ef8SMaor Gottlieb }
3486038d2ef8SMaor Gottlieb 
3487a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3488a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
3489a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
3490a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
3491a550ddfcSYishai Hadas {
34923b3233fbSRaed Salem 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
3493a550ddfcSYishai Hadas }
3494a550ddfcSYishai Hadas 
349535d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
349635d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
349735d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
349835d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
349935d19011SMaor Gottlieb {
350035d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
350135d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
350235d19011SMaor Gottlieb 
350335d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
350435d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
350535d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
350635d19011SMaor Gottlieb 					       flow_attr, dst);
350735d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
350874491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3509d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
351035d19011SMaor Gottlieb 			kfree(handler);
351135d19011SMaor Gottlieb 			handler = handler_dst;
351235d19011SMaor Gottlieb 		} else {
351335d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
351435d19011SMaor Gottlieb 		}
351535d19011SMaor Gottlieb 	}
351635d19011SMaor Gottlieb 
351735d19011SMaor Gottlieb 	return handler;
351835d19011SMaor Gottlieb }
3519038d2ef8SMaor Gottlieb enum {
3520038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
3521038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
3522038d2ef8SMaor Gottlieb };
3523038d2ef8SMaor Gottlieb 
3524038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3525038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
3526038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
3527038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
3528038d2ef8SMaor Gottlieb {
3529038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
3530038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3531038d2ef8SMaor Gottlieb 
3532038d2ef8SMaor Gottlieb 	static struct {
3533038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
3534038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
3535038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
3536038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
3537038d2ef8SMaor Gottlieb 			.flow_attr = {
3538038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3539038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3540038d2ef8SMaor Gottlieb 			},
3541038d2ef8SMaor Gottlieb 			.eth_flow = {
3542038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3543038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3544038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3545038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
3546038d2ef8SMaor Gottlieb 			}
3547038d2ef8SMaor Gottlieb 		},
3548038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
3549038d2ef8SMaor Gottlieb 			.flow_attr = {
3550038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3551038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3552038d2ef8SMaor Gottlieb 			},
3553038d2ef8SMaor Gottlieb 			.eth_flow = {
3554038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3555038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3556038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3557038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
3558038d2ef8SMaor Gottlieb 			}
3559038d2ef8SMaor Gottlieb 		}
3560038d2ef8SMaor Gottlieb 	};
3561038d2ef8SMaor Gottlieb 
3562038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
3563038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
3564038d2ef8SMaor Gottlieb 				   dst);
3565038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
3566038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3567038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
3568038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3569038d2ef8SMaor Gottlieb 						 dst);
3570038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
357174491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3572d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
3573038d2ef8SMaor Gottlieb 			kfree(handler);
3574038d2ef8SMaor Gottlieb 			handler = handler_ucast;
3575038d2ef8SMaor Gottlieb 		} else {
3576038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
3577038d2ef8SMaor Gottlieb 		}
3578038d2ef8SMaor Gottlieb 	}
3579038d2ef8SMaor Gottlieb 
3580038d2ef8SMaor Gottlieb 	return handler;
3581038d2ef8SMaor Gottlieb }
3582038d2ef8SMaor Gottlieb 
3583cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3584cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
3585cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
3586cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
3587cc0e5d42SMaor Gottlieb {
3588cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
3589cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
3590cc0e5d42SMaor Gottlieb 	int err;
3591cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
3592cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
3593cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
3594cc0e5d42SMaor Gottlieb 	};
3595cc0e5d42SMaor Gottlieb 
3596cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3597cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
3598cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
3599cc0e5d42SMaor Gottlieb 		goto err;
3600cc0e5d42SMaor Gottlieb 	}
3601cc0e5d42SMaor Gottlieb 
3602cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3603cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
3604cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
3605cc0e5d42SMaor Gottlieb 		goto err_tx;
3606cc0e5d42SMaor Gottlieb 	}
3607cc0e5d42SMaor Gottlieb 
3608cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
3609cc0e5d42SMaor Gottlieb 
3610cc0e5d42SMaor Gottlieb 	return handler_rx;
3611cc0e5d42SMaor Gottlieb 
3612cc0e5d42SMaor Gottlieb err_tx:
361374491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
3614cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
3615cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
3616cc0e5d42SMaor Gottlieb err:
3617cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
3618cc0e5d42SMaor Gottlieb }
3619cc0e5d42SMaor Gottlieb 
3620038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3621038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
362259082a32SMatan Barak 					   int domain,
362359082a32SMatan Barak 					   struct ib_udata *udata)
3624038d2ef8SMaor Gottlieb {
3625038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3626d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
3627038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3628038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
3629cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3630038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
3631802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
36323b3233fbSRaed Salem 	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
36333b3233fbSRaed Salem 	size_t min_ucmd_sz, required_ucmd_sz;
3634038d2ef8SMaor Gottlieb 	int err;
3635a550ddfcSYishai Hadas 	int underlay_qpn;
3636038d2ef8SMaor Gottlieb 
36373b3233fbSRaed Salem 	if (udata && udata->inlen) {
36383b3233fbSRaed Salem 		min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
36393b3233fbSRaed Salem 				sizeof(ucmd_hdr.reserved);
36403b3233fbSRaed Salem 		if (udata->inlen < min_ucmd_sz)
364159082a32SMatan Barak 			return ERR_PTR(-EOPNOTSUPP);
364259082a32SMatan Barak 
36433b3233fbSRaed Salem 		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
36443b3233fbSRaed Salem 		if (err)
36453b3233fbSRaed Salem 			return ERR_PTR(err);
36463b3233fbSRaed Salem 
36473b3233fbSRaed Salem 		/* currently supports only one counters data */
36483b3233fbSRaed Salem 		if (ucmd_hdr.ncounters_data > 1)
36493b3233fbSRaed Salem 			return ERR_PTR(-EINVAL);
36503b3233fbSRaed Salem 
36513b3233fbSRaed Salem 		required_ucmd_sz = min_ucmd_sz +
36523b3233fbSRaed Salem 			sizeof(struct mlx5_ib_flow_counters_data) *
36533b3233fbSRaed Salem 			ucmd_hdr.ncounters_data;
36543b3233fbSRaed Salem 		if (udata->inlen > required_ucmd_sz &&
36553b3233fbSRaed Salem 		    !ib_is_udata_cleared(udata, required_ucmd_sz,
36563b3233fbSRaed Salem 					 udata->inlen - required_ucmd_sz))
36573b3233fbSRaed Salem 			return ERR_PTR(-EOPNOTSUPP);
36583b3233fbSRaed Salem 
36593b3233fbSRaed Salem 		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
36603b3233fbSRaed Salem 		if (!ucmd)
36613b3233fbSRaed Salem 			return ERR_PTR(-ENOMEM);
36623b3233fbSRaed Salem 
36633b3233fbSRaed Salem 		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
3664299eafeeSGustavo A. R. Silva 		if (err)
3665299eafeeSGustavo A. R. Silva 			goto free_ucmd;
36663b3233fbSRaed Salem 	}
36673b3233fbSRaed Salem 
3668299eafeeSGustavo A. R. Silva 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3669299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3670299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3671299eafeeSGustavo A. R. Silva 	}
3672038d2ef8SMaor Gottlieb 
3673038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
3674508562d6SDaniel Jurgens 	    flow_attr->port > dev->num_ports ||
3675802c2125SAviad Yehezkel 	    (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3676299eafeeSGustavo A. R. Silva 				  IB_FLOW_ATTR_FLAGS_EGRESS))) {
3677299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3678299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3679299eafeeSGustavo A. R. Silva 	}
3680802c2125SAviad Yehezkel 
3681802c2125SAviad Yehezkel 	if (is_egress &&
3682802c2125SAviad Yehezkel 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3683299eafeeSGustavo A. R. Silva 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3684299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3685299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3686299eafeeSGustavo A. R. Silva 	}
3687038d2ef8SMaor Gottlieb 
3688038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3689299eafeeSGustavo A. R. Silva 	if (!dst) {
3690299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3691299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3692299eafeeSGustavo A. R. Silva 	}
3693038d2ef8SMaor Gottlieb 
36949a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3695038d2ef8SMaor Gottlieb 
3696802c2125SAviad Yehezkel 	ft_prio = get_flow_table(dev, flow_attr,
3697802c2125SAviad Yehezkel 				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3698038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
3699038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
3700038d2ef8SMaor Gottlieb 		goto unlock;
3701038d2ef8SMaor Gottlieb 	}
3702cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3703cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3704cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
3705cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
3706cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
3707cc0e5d42SMaor Gottlieb 			goto destroy_ft;
3708cc0e5d42SMaor Gottlieb 		}
3709cc0e5d42SMaor Gottlieb 	}
3710038d2ef8SMaor Gottlieb 
3711802c2125SAviad Yehezkel 	if (is_egress) {
3712802c2125SAviad Yehezkel 		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3713802c2125SAviad Yehezkel 	} else {
3714038d2ef8SMaor Gottlieb 		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3715d9f88e5aSYishai Hadas 		if (mqp->flags & MLX5_IB_QP_RSS)
3716d9f88e5aSYishai Hadas 			dst->tir_num = mqp->rss_qp.tirn;
3717d9f88e5aSYishai Hadas 		else
3718d9f88e5aSYishai Hadas 			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3719802c2125SAviad Yehezkel 	}
3720038d2ef8SMaor Gottlieb 
3721038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
372235d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
372335d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
372435d19011SMaor Gottlieb 							flow_attr, dst);
372535d19011SMaor Gottlieb 		} else {
3726a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3727a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
3728a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
37293b3233fbSRaed Salem 						    dst, underlay_qpn, ucmd);
373035d19011SMaor Gottlieb 		}
3731038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3732038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3733038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3734038d2ef8SMaor Gottlieb 						dst);
3735cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3736cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3737038d2ef8SMaor Gottlieb 	} else {
3738038d2ef8SMaor Gottlieb 		err = -EINVAL;
3739038d2ef8SMaor Gottlieb 		goto destroy_ft;
3740038d2ef8SMaor Gottlieb 	}
3741038d2ef8SMaor Gottlieb 
3742038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
3743038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
3744038d2ef8SMaor Gottlieb 		handler = NULL;
3745038d2ef8SMaor Gottlieb 		goto destroy_ft;
3746038d2ef8SMaor Gottlieb 	}
3747038d2ef8SMaor Gottlieb 
37489a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3749038d2ef8SMaor Gottlieb 	kfree(dst);
37503b3233fbSRaed Salem 	kfree(ucmd);
3751038d2ef8SMaor Gottlieb 
3752038d2ef8SMaor Gottlieb 	return &handler->ibflow;
3753038d2ef8SMaor Gottlieb 
3754038d2ef8SMaor Gottlieb destroy_ft:
3755038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
3756cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
3757cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
3758038d2ef8SMaor Gottlieb unlock:
37599a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3760038d2ef8SMaor Gottlieb 	kfree(dst);
3761299eafeeSGustavo A. R. Silva free_ucmd:
37623b3233fbSRaed Salem 	kfree(ucmd);
3763038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
3764038d2ef8SMaor Gottlieb }
3765038d2ef8SMaor Gottlieb 
3766b47fd4ffSMark Bloch static struct mlx5_ib_flow_prio *
3767b47fd4ffSMark Bloch _get_flow_table(struct mlx5_ib_dev *dev,
3768b47fd4ffSMark Bloch 		struct mlx5_ib_flow_matcher *fs_matcher,
3769b47fd4ffSMark Bloch 		bool mcast)
3770d4be3f44SYishai Hadas {
3771d4be3f44SYishai Hadas 	struct mlx5_flow_namespace *ns = NULL;
3772d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *prio;
3773b47fd4ffSMark Bloch 	int max_table_size;
3774b47fd4ffSMark Bloch 	u32 flags = 0;
3775b47fd4ffSMark Bloch 	int priority;
3776d4be3f44SYishai Hadas 
3777b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
3778d4be3f44SYishai Hadas 		max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3779d4be3f44SYishai Hadas 					log_max_ft_size));
3780b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3781b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3782b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3783b47fd4ffSMark Bloch 					      reformat_l3_tunnel_to_l2))
3784b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3785b47fd4ffSMark Bloch 	} else { /* Can only be MLX5_FLOW_NAMESPACE_EGRESS */
3786b47fd4ffSMark Bloch 		max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3787b47fd4ffSMark Bloch 					log_max_ft_size));
3788b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3789b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3790b47fd4ffSMark Bloch 	}
3791b47fd4ffSMark Bloch 
3792d4be3f44SYishai Hadas 	if (max_table_size < MLX5_FS_MAX_ENTRIES)
3793d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
3794d4be3f44SYishai Hadas 
3795d4be3f44SYishai Hadas 	if (mcast)
3796d4be3f44SYishai Hadas 		priority = MLX5_IB_FLOW_MCAST_PRIO;
3797d4be3f44SYishai Hadas 	else
3798b47fd4ffSMark Bloch 		priority = ib_prio_to_core_prio(fs_matcher->priority, false);
3799d4be3f44SYishai Hadas 
3800b47fd4ffSMark Bloch 	ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
3801d4be3f44SYishai Hadas 	if (!ns)
3802d4be3f44SYishai Hadas 		return ERR_PTR(-ENOTSUPP);
3803d4be3f44SYishai Hadas 
3804b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
3805d4be3f44SYishai Hadas 		prio = &dev->flow_db->prios[priority];
3806b47fd4ffSMark Bloch 	else
3807b47fd4ffSMark Bloch 		prio = &dev->flow_db->egress_prios[priority];
3808d4be3f44SYishai Hadas 
3809d4be3f44SYishai Hadas 	if (prio->flow_table)
3810d4be3f44SYishai Hadas 		return prio;
3811d4be3f44SYishai Hadas 
3812d4be3f44SYishai Hadas 	return _get_prio(ns, prio, priority, MLX5_FS_MAX_ENTRIES,
3813b47fd4ffSMark Bloch 			 MLX5_FS_MAX_TYPES, flags);
3814d4be3f44SYishai Hadas }
3815d4be3f44SYishai Hadas 
3816d4be3f44SYishai Hadas static struct mlx5_ib_flow_handler *
3817d4be3f44SYishai Hadas _create_raw_flow_rule(struct mlx5_ib_dev *dev,
3818d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_prio *ft_prio,
3819d4be3f44SYishai Hadas 		      struct mlx5_flow_destination *dst,
3820d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_matcher  *fs_matcher,
3821b823dd6dSMark Bloch 		      struct mlx5_flow_act *flow_act,
3822bfc5d839SMark Bloch 		      void *cmd_in, int inlen,
3823bfc5d839SMark Bloch 		      int dst_num)
3824d4be3f44SYishai Hadas {
3825d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
3826d4be3f44SYishai Hadas 	struct mlx5_flow_spec *spec;
3827d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft = ft_prio->flow_table;
3828d4be3f44SYishai Hadas 	int err = 0;
3829d4be3f44SYishai Hadas 
3830d4be3f44SYishai Hadas 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3831d4be3f44SYishai Hadas 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3832d4be3f44SYishai Hadas 	if (!handler || !spec) {
3833d4be3f44SYishai Hadas 		err = -ENOMEM;
3834d4be3f44SYishai Hadas 		goto free;
3835d4be3f44SYishai Hadas 	}
3836d4be3f44SYishai Hadas 
3837d4be3f44SYishai Hadas 	INIT_LIST_HEAD(&handler->list);
3838d4be3f44SYishai Hadas 
3839d4be3f44SYishai Hadas 	memcpy(spec->match_value, cmd_in, inlen);
3840d4be3f44SYishai Hadas 	memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
3841d4be3f44SYishai Hadas 	       fs_matcher->mask_len);
3842d4be3f44SYishai Hadas 	spec->match_criteria_enable = fs_matcher->match_criteria_enable;
3843d4be3f44SYishai Hadas 
3844d4be3f44SYishai Hadas 	handler->rule = mlx5_add_flow_rules(ft, spec,
3845bfc5d839SMark Bloch 					    flow_act, dst, dst_num);
3846d4be3f44SYishai Hadas 
3847d4be3f44SYishai Hadas 	if (IS_ERR(handler->rule)) {
3848d4be3f44SYishai Hadas 		err = PTR_ERR(handler->rule);
3849d4be3f44SYishai Hadas 		goto free;
3850d4be3f44SYishai Hadas 	}
3851d4be3f44SYishai Hadas 
3852d4be3f44SYishai Hadas 	ft_prio->refcount++;
3853d4be3f44SYishai Hadas 	handler->prio = ft_prio;
3854d4be3f44SYishai Hadas 	handler->dev = dev;
3855d4be3f44SYishai Hadas 	ft_prio->flow_table = ft;
3856d4be3f44SYishai Hadas 
3857d4be3f44SYishai Hadas free:
3858d4be3f44SYishai Hadas 	if (err)
3859d4be3f44SYishai Hadas 		kfree(handler);
3860d4be3f44SYishai Hadas 	kvfree(spec);
3861d4be3f44SYishai Hadas 	return err ? ERR_PTR(err) : handler;
3862d4be3f44SYishai Hadas }
3863d4be3f44SYishai Hadas 
3864d4be3f44SYishai Hadas static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
3865d4be3f44SYishai Hadas 				void *match_v)
3866d4be3f44SYishai Hadas {
3867d4be3f44SYishai Hadas 	void *match_c;
3868d4be3f44SYishai Hadas 	void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
3869d4be3f44SYishai Hadas 	void *dmac, *dmac_mask;
3870d4be3f44SYishai Hadas 	void *ipv4, *ipv4_mask;
3871d4be3f44SYishai Hadas 
3872d4be3f44SYishai Hadas 	if (!(fs_matcher->match_criteria_enable &
3873d4be3f44SYishai Hadas 	      (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
3874d4be3f44SYishai Hadas 		return false;
3875d4be3f44SYishai Hadas 
3876d4be3f44SYishai Hadas 	match_c = fs_matcher->matcher_mask.match_params;
3877d4be3f44SYishai Hadas 	match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
3878d4be3f44SYishai Hadas 					   outer_headers);
3879d4be3f44SYishai Hadas 	match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
3880d4be3f44SYishai Hadas 					   outer_headers);
3881d4be3f44SYishai Hadas 
3882d4be3f44SYishai Hadas 	dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3883d4be3f44SYishai Hadas 			    dmac_47_16);
3884d4be3f44SYishai Hadas 	dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3885d4be3f44SYishai Hadas 				 dmac_47_16);
3886d4be3f44SYishai Hadas 
3887d4be3f44SYishai Hadas 	if (is_multicast_ether_addr(dmac) &&
3888d4be3f44SYishai Hadas 	    is_multicast_ether_addr(dmac_mask))
3889d4be3f44SYishai Hadas 		return true;
3890d4be3f44SYishai Hadas 
3891d4be3f44SYishai Hadas 	ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3892d4be3f44SYishai Hadas 			    dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3893d4be3f44SYishai Hadas 
3894d4be3f44SYishai Hadas 	ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3895d4be3f44SYishai Hadas 				 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3896d4be3f44SYishai Hadas 
3897d4be3f44SYishai Hadas 	if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
3898d4be3f44SYishai Hadas 	    ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
3899d4be3f44SYishai Hadas 		return true;
3900d4be3f44SYishai Hadas 
3901d4be3f44SYishai Hadas 	return false;
3902d4be3f44SYishai Hadas }
3903d4be3f44SYishai Hadas 
390432269441SYishai Hadas struct mlx5_ib_flow_handler *
390532269441SYishai Hadas mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
390632269441SYishai Hadas 			struct mlx5_ib_flow_matcher *fs_matcher,
3907b823dd6dSMark Bloch 			struct mlx5_flow_act *flow_act,
3908bfc5d839SMark Bloch 			u32 counter_id,
390932269441SYishai Hadas 			void *cmd_in, int inlen, int dest_id,
391032269441SYishai Hadas 			int dest_type)
391132269441SYishai Hadas {
3912d4be3f44SYishai Hadas 	struct mlx5_flow_destination *dst;
3913d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *ft_prio;
3914d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
3915bfc5d839SMark Bloch 	int dst_num = 0;
3916d4be3f44SYishai Hadas 	bool mcast;
3917d4be3f44SYishai Hadas 	int err;
3918d4be3f44SYishai Hadas 
3919d4be3f44SYishai Hadas 	if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
392032269441SYishai Hadas 		return ERR_PTR(-EOPNOTSUPP);
3921d4be3f44SYishai Hadas 
3922d4be3f44SYishai Hadas 	if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
3923d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
3924d4be3f44SYishai Hadas 
39258e8aa145SGustavo A. R. Silva 	dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
3926d4be3f44SYishai Hadas 	if (!dst)
3927d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
3928d4be3f44SYishai Hadas 
3929d4be3f44SYishai Hadas 	mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
3930d4be3f44SYishai Hadas 	mutex_lock(&dev->flow_db->lock);
3931d4be3f44SYishai Hadas 
3932b47fd4ffSMark Bloch 	ft_prio = _get_flow_table(dev, fs_matcher, mcast);
3933d4be3f44SYishai Hadas 	if (IS_ERR(ft_prio)) {
3934d4be3f44SYishai Hadas 		err = PTR_ERR(ft_prio);
3935d4be3f44SYishai Hadas 		goto unlock;
3936d4be3f44SYishai Hadas 	}
3937d4be3f44SYishai Hadas 
39386346f0bfSYishai Hadas 	if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
3939bfc5d839SMark Bloch 		dst[dst_num].type = dest_type;
3940bfc5d839SMark Bloch 		dst[dst_num].tir_num = dest_id;
3941b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3942a7ee18bdSMark Bloch 	} else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
3943bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
3944bfc5d839SMark Bloch 		dst[dst_num].ft_num = dest_id;
3945b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3946a7ee18bdSMark Bloch 	} else {
3947bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3948a7ee18bdSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
39496346f0bfSYishai Hadas 	}
39506346f0bfSYishai Hadas 
3951bfc5d839SMark Bloch 	dst_num++;
3952bfc5d839SMark Bloch 
3953bfc5d839SMark Bloch 	if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3954bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3955bfc5d839SMark Bloch 		dst[dst_num].counter_id = counter_id;
3956bfc5d839SMark Bloch 		dst_num++;
3957bfc5d839SMark Bloch 	}
3958bfc5d839SMark Bloch 
3959b823dd6dSMark Bloch 	handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher, flow_act,
3960bfc5d839SMark Bloch 					cmd_in, inlen, dst_num);
3961d4be3f44SYishai Hadas 
3962d4be3f44SYishai Hadas 	if (IS_ERR(handler)) {
3963d4be3f44SYishai Hadas 		err = PTR_ERR(handler);
3964d4be3f44SYishai Hadas 		goto destroy_ft;
3965d4be3f44SYishai Hadas 	}
3966d4be3f44SYishai Hadas 
3967d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
3968d4be3f44SYishai Hadas 	atomic_inc(&fs_matcher->usecnt);
3969d4be3f44SYishai Hadas 	handler->flow_matcher = fs_matcher;
3970d4be3f44SYishai Hadas 
3971d4be3f44SYishai Hadas 	kfree(dst);
3972d4be3f44SYishai Hadas 
3973d4be3f44SYishai Hadas 	return handler;
3974d4be3f44SYishai Hadas 
3975d4be3f44SYishai Hadas destroy_ft:
3976d4be3f44SYishai Hadas 	put_flow_table(dev, ft_prio, false);
3977d4be3f44SYishai Hadas unlock:
3978d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
3979d4be3f44SYishai Hadas 	kfree(dst);
3980d4be3f44SYishai Hadas 
3981d4be3f44SYishai Hadas 	return ERR_PTR(err);
398232269441SYishai Hadas }
398332269441SYishai Hadas 
3984c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
3985c6475a0bSAviad Yehezkel {
3986c6475a0bSAviad Yehezkel 	u32 flags = 0;
3987c6475a0bSAviad Yehezkel 
3988c6475a0bSAviad Yehezkel 	if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
3989c6475a0bSAviad Yehezkel 		flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
3990c6475a0bSAviad Yehezkel 
3991c6475a0bSAviad Yehezkel 	return flags;
3992c6475a0bSAviad Yehezkel }
3993c6475a0bSAviad Yehezkel 
3994c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED	MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
3995c6475a0bSAviad Yehezkel static struct ib_flow_action *
3996c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device,
3997c6475a0bSAviad Yehezkel 			       const struct ib_flow_action_attrs_esp *attr,
3998c6475a0bSAviad Yehezkel 			       struct uverbs_attr_bundle *attrs)
3999c6475a0bSAviad Yehezkel {
4000c6475a0bSAviad Yehezkel 	struct mlx5_ib_dev *mdev = to_mdev(device);
4001c6475a0bSAviad Yehezkel 	struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
4002c6475a0bSAviad Yehezkel 	struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
4003c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *action;
4004c6475a0bSAviad Yehezkel 	u64 action_flags;
4005c6475a0bSAviad Yehezkel 	u64 flags;
4006c6475a0bSAviad Yehezkel 	int err = 0;
4007c6475a0bSAviad Yehezkel 
4008bccd0622SJason Gunthorpe 	err = uverbs_get_flags64(
4009bccd0622SJason Gunthorpe 		&action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4010bccd0622SJason Gunthorpe 		((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
4011bccd0622SJason Gunthorpe 	if (err)
4012bccd0622SJason Gunthorpe 		return ERR_PTR(err);
4013c6475a0bSAviad Yehezkel 
4014c6475a0bSAviad Yehezkel 	flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
4015c6475a0bSAviad Yehezkel 
4016c6475a0bSAviad Yehezkel 	/* We current only support a subset of the standard features. Only a
4017c6475a0bSAviad Yehezkel 	 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
4018c6475a0bSAviad Yehezkel 	 * (with overlap). Full offload mode isn't supported.
4019c6475a0bSAviad Yehezkel 	 */
4020c6475a0bSAviad Yehezkel 	if (!attr->keymat || attr->replay || attr->encap ||
4021c6475a0bSAviad Yehezkel 	    attr->spi || attr->seq || attr->tfc_pad ||
4022c6475a0bSAviad Yehezkel 	    attr->hard_limit_pkts ||
4023c6475a0bSAviad Yehezkel 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4024c6475a0bSAviad Yehezkel 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
4025c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4026c6475a0bSAviad Yehezkel 
4027c6475a0bSAviad Yehezkel 	if (attr->keymat->protocol !=
4028c6475a0bSAviad Yehezkel 	    IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
4029c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4030c6475a0bSAviad Yehezkel 
4031c6475a0bSAviad Yehezkel 	aes_gcm = &attr->keymat->keymat.aes_gcm;
4032c6475a0bSAviad Yehezkel 
4033c6475a0bSAviad Yehezkel 	if (aes_gcm->icv_len != 16 ||
4034c6475a0bSAviad Yehezkel 	    aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
4035c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4036c6475a0bSAviad Yehezkel 
4037c6475a0bSAviad Yehezkel 	action = kmalloc(sizeof(*action), GFP_KERNEL);
4038c6475a0bSAviad Yehezkel 	if (!action)
4039c6475a0bSAviad Yehezkel 		return ERR_PTR(-ENOMEM);
4040c6475a0bSAviad Yehezkel 
4041c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4042c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
4043c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.aes_key));
4044c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
4045c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
4046c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.salt));
4047c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
4048c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
4049c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
4050c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
4051c6475a0bSAviad Yehezkel 	accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
4052c6475a0bSAviad Yehezkel 
4053c6475a0bSAviad Yehezkel 	accel_attrs.esn = attr->esn;
4054c6475a0bSAviad Yehezkel 	if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4055c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4056c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4057c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4058c6475a0bSAviad Yehezkel 
4059c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4060c6475a0bSAviad Yehezkel 		accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4061c6475a0bSAviad Yehezkel 
4062c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ctx =
4063c6475a0bSAviad Yehezkel 		mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4064c6475a0bSAviad Yehezkel 	if (IS_ERR(action->esp_aes_gcm.ctx)) {
4065c6475a0bSAviad Yehezkel 		err = PTR_ERR(action->esp_aes_gcm.ctx);
4066c6475a0bSAviad Yehezkel 		goto err_parse;
4067c6475a0bSAviad Yehezkel 	}
4068c6475a0bSAviad Yehezkel 
4069c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4070c6475a0bSAviad Yehezkel 
4071c6475a0bSAviad Yehezkel 	return &action->ib_action;
4072c6475a0bSAviad Yehezkel 
4073c6475a0bSAviad Yehezkel err_parse:
4074c6475a0bSAviad Yehezkel 	kfree(action);
4075c6475a0bSAviad Yehezkel 	return ERR_PTR(err);
4076c6475a0bSAviad Yehezkel }
4077c6475a0bSAviad Yehezkel 
4078349705c1SMatan Barak static int
4079349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4080349705c1SMatan Barak 			       const struct ib_flow_action_attrs_esp *attr,
4081349705c1SMatan Barak 			       struct uverbs_attr_bundle *attrs)
4082349705c1SMatan Barak {
4083349705c1SMatan Barak 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4084349705c1SMatan Barak 	struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4085349705c1SMatan Barak 	int err = 0;
4086349705c1SMatan Barak 
4087349705c1SMatan Barak 	if (attr->keymat || attr->replay || attr->encap ||
4088349705c1SMatan Barak 	    attr->spi || attr->seq || attr->tfc_pad ||
4089349705c1SMatan Barak 	    attr->hard_limit_pkts ||
4090349705c1SMatan Barak 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4091349705c1SMatan Barak 			     IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4092349705c1SMatan Barak 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4093349705c1SMatan Barak 		return -EOPNOTSUPP;
4094349705c1SMatan Barak 
4095349705c1SMatan Barak 	/* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4096349705c1SMatan Barak 	 * be modified.
4097349705c1SMatan Barak 	 */
4098349705c1SMatan Barak 	if (!(maction->esp_aes_gcm.ib_flags &
4099349705c1SMatan Barak 	      IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4100349705c1SMatan Barak 	    attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4101349705c1SMatan Barak 			   IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4102349705c1SMatan Barak 		return -EINVAL;
4103349705c1SMatan Barak 
4104349705c1SMatan Barak 	memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4105349705c1SMatan Barak 	       sizeof(accel_attrs));
4106349705c1SMatan Barak 
4107349705c1SMatan Barak 	accel_attrs.esn = attr->esn;
4108349705c1SMatan Barak 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4109349705c1SMatan Barak 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4110349705c1SMatan Barak 	else
4111349705c1SMatan Barak 		accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4112349705c1SMatan Barak 
4113349705c1SMatan Barak 	err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4114349705c1SMatan Barak 					 &accel_attrs);
4115349705c1SMatan Barak 	if (err)
4116349705c1SMatan Barak 		return err;
4117349705c1SMatan Barak 
4118349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags &=
4119349705c1SMatan Barak 		~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4120349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags |=
4121349705c1SMatan Barak 		attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4122349705c1SMatan Barak 
4123349705c1SMatan Barak 	return 0;
4124349705c1SMatan Barak }
4125349705c1SMatan Barak 
4126c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4127c6475a0bSAviad Yehezkel {
4128c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4129c6475a0bSAviad Yehezkel 
4130c6475a0bSAviad Yehezkel 	switch (action->type) {
4131c6475a0bSAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
4132c6475a0bSAviad Yehezkel 		/*
4133c6475a0bSAviad Yehezkel 		 * We only support aes_gcm by now, so we implicitly know this is
4134c6475a0bSAviad Yehezkel 		 * the underline crypto.
4135c6475a0bSAviad Yehezkel 		 */
4136c6475a0bSAviad Yehezkel 		mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4137c6475a0bSAviad Yehezkel 		break;
4138b4749bf2SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
4139b4749bf2SMark Bloch 		mlx5_ib_destroy_flow_action_raw(maction);
4140b4749bf2SMark Bloch 		break;
4141c6475a0bSAviad Yehezkel 	default:
4142c6475a0bSAviad Yehezkel 		WARN_ON(true);
4143c6475a0bSAviad Yehezkel 		break;
4144c6475a0bSAviad Yehezkel 	}
4145c6475a0bSAviad Yehezkel 
4146c6475a0bSAviad Yehezkel 	kfree(maction);
4147c6475a0bSAviad Yehezkel 	return 0;
4148c6475a0bSAviad Yehezkel }
4149c6475a0bSAviad Yehezkel 
4150e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4151e126ba97SEli Cohen {
4152e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
415381e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
4154e126ba97SEli Cohen 	int err;
4155539ec982SYishai Hadas 	u16 uid;
4156539ec982SYishai Hadas 
4157539ec982SYishai Hadas 	uid = ibqp->pd ?
4158539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4159e126ba97SEli Cohen 
416081e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
416181e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
416281e30880SYishai Hadas 		return -EOPNOTSUPP;
416381e30880SYishai Hadas 	}
416481e30880SYishai Hadas 
4165539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4166e126ba97SEli Cohen 	if (err)
4167e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4168e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4169e126ba97SEli Cohen 
4170e126ba97SEli Cohen 	return err;
4171e126ba97SEli Cohen }
4172e126ba97SEli Cohen 
4173e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4174e126ba97SEli Cohen {
4175e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4176e126ba97SEli Cohen 	int err;
4177539ec982SYishai Hadas 	u16 uid;
4178e126ba97SEli Cohen 
4179539ec982SYishai Hadas 	uid = ibqp->pd ?
4180539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4181539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4182e126ba97SEli Cohen 	if (err)
4183e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4184e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4185e126ba97SEli Cohen 
4186e126ba97SEli Cohen 	return err;
4187e126ba97SEli Cohen }
4188e126ba97SEli Cohen 
4189e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
4190e126ba97SEli Cohen {
41911b5daf11SMajd Dibbiny 	int err;
4192e126ba97SEli Cohen 
41931b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
4194e126ba97SEli Cohen 	if (err)
4195e126ba97SEli Cohen 		return err;
41961b5daf11SMajd Dibbiny 
41971b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
41981b5daf11SMajd Dibbiny 
41991b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
4200e126ba97SEli Cohen }
4201e126ba97SEli Cohen 
4202508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
4203508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4204e126ba97SEli Cohen {
4205e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
420654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4207e126ba97SEli Cohen 
42089603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
4209e126ba97SEli Cohen }
4210508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
4211e126ba97SEli Cohen 
4212508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
4213e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
4214e126ba97SEli Cohen {
4215e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
421654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4217e126ba97SEli Cohen 
42186aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
4219e126ba97SEli Cohen }
4220508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
4221e126ba97SEli Cohen 
4222508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
4223508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4224e126ba97SEli Cohen {
4225e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
422654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
422754747231SParav Pandit 
42289603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
4229e126ba97SEli Cohen }
4230508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
4231e126ba97SEli Cohen 
4232508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
4233508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
4234e126ba97SEli Cohen {
4235e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
423654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
423754747231SParav Pandit 
42389603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
4239e126ba97SEli Cohen }
4240508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
4241e126ba97SEli Cohen 
4242508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
4243508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4244e126ba97SEli Cohen {
4245e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
424654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
424754747231SParav Pandit 
4248e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
42499603b61dSJack Morgenstein 		       dev->mdev->board_id);
4250e126ba97SEli Cohen }
4251508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
4252e126ba97SEli Cohen 
4253508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
4254508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
4255508a523fSParav Pandit 	&dev_attr_hca_type.attr,
4256508a523fSParav Pandit 	&dev_attr_board_id.attr,
4257508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
4258508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
4259508a523fSParav Pandit 	NULL,
4260508a523fSParav Pandit };
4261e126ba97SEli Cohen 
4262508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
4263508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
4264e126ba97SEli Cohen };
4265e126ba97SEli Cohen 
42667722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
42677722f47eSHaggai Eran {
42687722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
42697722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
42707722f47eSHaggai Eran 			     pkey_change_work);
42717722f47eSHaggai Eran 
42727722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
42737722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
42747722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
42757722f47eSHaggai Eran }
42767722f47eSHaggai Eran 
427789ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
427889ea94a7SMaor Gottlieb {
427989ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
428089ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
428189ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
428289ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
428389ea94a7SMaor Gottlieb 	unsigned long flags_qp;
428489ea94a7SMaor Gottlieb 	unsigned long flags_cq;
428589ea94a7SMaor Gottlieb 	unsigned long flags;
428689ea94a7SMaor Gottlieb 
428789ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
428889ea94a7SMaor Gottlieb 
428989ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
429089ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
429189ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
429289ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
429389ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
429489ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
429589ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
429689ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
429789ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
429889ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
429989ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
430089ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
430189ea94a7SMaor Gottlieb 						      &cq_armed_list);
430289ea94a7SMaor Gottlieb 				}
430389ea94a7SMaor Gottlieb 			}
430489ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
430589ea94a7SMaor Gottlieb 		}
430689ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
430789ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
430889ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
430989ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
431089ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
431189ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
431289ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
431389ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
431489ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
431589ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
431689ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
431789ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
431889ea94a7SMaor Gottlieb 							      &cq_armed_list);
431989ea94a7SMaor Gottlieb 					}
432089ea94a7SMaor Gottlieb 				}
432189ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
432289ea94a7SMaor Gottlieb 						       flags_cq);
432389ea94a7SMaor Gottlieb 			}
432489ea94a7SMaor Gottlieb 		}
432589ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
432689ea94a7SMaor Gottlieb 	}
432789ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
432889ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
432989ea94a7SMaor Gottlieb 	 */
433089ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
433189ea94a7SMaor Gottlieb 		mcq->comp(mcq);
433289ea94a7SMaor Gottlieb 	}
433389ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
433489ea94a7SMaor Gottlieb }
433589ea94a7SMaor Gottlieb 
433603404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
433703404e8aSMaor Gottlieb {
433803404e8aSMaor Gottlieb 	int err;
433903404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
434003404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
434103404e8aSMaor Gottlieb 			     delay_drop_work);
434203404e8aSMaor Gottlieb 
4343fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
4344fe248c3aSMaor Gottlieb 
434503404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
434603404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
434703404e8aSMaor Gottlieb 				       delay_drop->timeout);
434803404e8aSMaor Gottlieb 	if (err) {
434903404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
435003404e8aSMaor Gottlieb 			     delay_drop->timeout);
435103404e8aSMaor Gottlieb 		delay_drop->activate = false;
435203404e8aSMaor Gottlieb 	}
435303404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
435403404e8aSMaor Gottlieb }
435503404e8aSMaor Gottlieb 
435609e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
435709e574faSSaeed Mahameed 				 struct ib_event *ibev)
435809e574faSSaeed Mahameed {
435909e574faSSaeed Mahameed 	switch (eqe->sub_type) {
436009e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
436109e574faSSaeed Mahameed 		schedule_work(&ibdev->delay_drop.delay_drop_work);
436209e574faSSaeed Mahameed 		break;
436309e574faSSaeed Mahameed 	default: /* do nothing */
436409e574faSSaeed Mahameed 		return;
436509e574faSSaeed Mahameed 	}
436609e574faSSaeed Mahameed }
436709e574faSSaeed Mahameed 
4368134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4369134e9349SSaeed Mahameed 			      struct ib_event *ibev)
4370134e9349SSaeed Mahameed {
4371134e9349SSaeed Mahameed 	u8 port = (eqe->data.port.port >> 4) & 0xf;
4372134e9349SSaeed Mahameed 
4373134e9349SSaeed Mahameed 	ibev->element.port_num = port;
4374134e9349SSaeed Mahameed 
4375134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
4376134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4377134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4378134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4379134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
4380134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
4381134e9349SSaeed Mahameed 		 */
4382134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4383134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
4384134e9349SSaeed Mahameed 			return -EINVAL;
4385134e9349SSaeed Mahameed 
4386134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4387134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4388134e9349SSaeed Mahameed 		break;
4389134e9349SSaeed Mahameed 
4390134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
4391134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
4392134e9349SSaeed Mahameed 		break;
4393134e9349SSaeed Mahameed 
4394134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4395134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
4396134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4397134e9349SSaeed Mahameed 		break;
4398134e9349SSaeed Mahameed 
4399134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4400134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
4401134e9349SSaeed Mahameed 		break;
4402134e9349SSaeed Mahameed 
4403134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4404134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
4405134e9349SSaeed Mahameed 		break;
4406134e9349SSaeed Mahameed 	default:
4407134e9349SSaeed Mahameed 		return -EINVAL;
4408134e9349SSaeed Mahameed 	}
4409134e9349SSaeed Mahameed 
4410134e9349SSaeed Mahameed 	return 0;
4411134e9349SSaeed Mahameed }
4412134e9349SSaeed Mahameed 
4413d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
4414e126ba97SEli Cohen {
4415d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
4416d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
4417d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
4418e126ba97SEli Cohen 	struct ib_event ibev;
4419dbaaff2aSEli Cohen 	bool fatal = false;
4420e126ba97SEli Cohen 
4421df097a27SSaeed Mahameed 	if (work->is_slave) {
4422df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
4423d69a24e0SDaniel Jurgens 		if (!ibdev)
4424d69a24e0SDaniel Jurgens 			goto out;
4425d69a24e0SDaniel Jurgens 	} else {
4426df097a27SSaeed Mahameed 		ibdev = work->dev;
4427d69a24e0SDaniel Jurgens 	}
4428d69a24e0SDaniel Jurgens 
4429d69a24e0SDaniel Jurgens 	switch (work->event) {
4430e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
4431e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
443289ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
4433134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
4434dbaaff2aSEli Cohen 		fatal = true;
4435e126ba97SEli Cohen 		break;
4436134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
4437134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
4438d69a24e0SDaniel Jurgens 			goto out;
4439e126ba97SEli Cohen 		break;
444009e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
444109e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
444209e574faSSaeed Mahameed 		/* fall through */
4443bdc37924SSaeed Mahameed 	default:
444403404e8aSMaor Gottlieb 		goto out;
4445e126ba97SEli Cohen 	}
4446e126ba97SEli Cohen 
4447e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
4448e126ba97SEli Cohen 
4449134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4450134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
445103404e8aSMaor Gottlieb 		goto out;
4452a0c84c32SEli Cohen 	}
4453a0c84c32SEli Cohen 
4454e126ba97SEli Cohen 	if (ibdev->ib_active)
4455e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
4456dbaaff2aSEli Cohen 
4457dbaaff2aSEli Cohen 	if (fatal)
4458dbaaff2aSEli Cohen 		ibdev->ib_active = false;
445903404e8aSMaor Gottlieb out:
4460d69a24e0SDaniel Jurgens 	kfree(work);
4461d69a24e0SDaniel Jurgens }
4462d69a24e0SDaniel Jurgens 
4463df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
4464df097a27SSaeed Mahameed 			 unsigned long event, void *param)
4465d69a24e0SDaniel Jurgens {
4466d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
4467d69a24e0SDaniel Jurgens 
4468d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
446910bea9c8SLeon Romanovsky 	if (!work)
4470df097a27SSaeed Mahameed 		return NOTIFY_DONE;
447110bea9c8SLeon Romanovsky 
4472d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4473df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4474df097a27SSaeed Mahameed 	work->is_slave = false;
4475d69a24e0SDaniel Jurgens 	work->param = param;
4476d69a24e0SDaniel Jurgens 	work->event = event;
4477d69a24e0SDaniel Jurgens 
4478d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
4479df097a27SSaeed Mahameed 
4480df097a27SSaeed Mahameed 	return NOTIFY_OK;
4481df097a27SSaeed Mahameed }
4482df097a27SSaeed Mahameed 
4483df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4484df097a27SSaeed Mahameed 				    unsigned long event, void *param)
4485df097a27SSaeed Mahameed {
4486df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
4487df097a27SSaeed Mahameed 
4488df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
4489df097a27SSaeed Mahameed 	if (!work)
4490df097a27SSaeed Mahameed 		return NOTIFY_DONE;
4491df097a27SSaeed Mahameed 
4492df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4493df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4494df097a27SSaeed Mahameed 	work->is_slave = true;
4495df097a27SSaeed Mahameed 	work->param = param;
4496df097a27SSaeed Mahameed 	work->event = event;
4497df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
4498df097a27SSaeed Mahameed 
4499df097a27SSaeed Mahameed 	return NOTIFY_OK;
4500d69a24e0SDaniel Jurgens }
4501d69a24e0SDaniel Jurgens 
4502c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4503c43f1112SMaor Gottlieb {
4504c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
4505c43f1112SMaor Gottlieb 	int err;
4506c43f1112SMaor Gottlieb 	int port;
4507c43f1112SMaor Gottlieb 
4508508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++) {
4509c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
4510c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4511c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
4512c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4513c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
4514c43f1112SMaor Gottlieb 								   port, 0,
4515c43f1112SMaor Gottlieb 								   &vport_ctx);
4516c43f1112SMaor Gottlieb 				if (err) {
4517c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4518c43f1112SMaor Gottlieb 						    port, err);
4519c43f1112SMaor Gottlieb 					return err;
4520c43f1112SMaor Gottlieb 				}
4521c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
4522c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
4523c43f1112SMaor Gottlieb 			} else {
4524c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
4525c43f1112SMaor Gottlieb 			}
4526c43f1112SMaor Gottlieb 		}
4527c43f1112SMaor Gottlieb 	}
4528c43f1112SMaor Gottlieb 	return 0;
4529c43f1112SMaor Gottlieb }
4530c43f1112SMaor Gottlieb 
4531e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4532e126ba97SEli Cohen {
4533e126ba97SEli Cohen 	int port;
4534e126ba97SEli Cohen 
4535508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
4536e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
4537e126ba97SEli Cohen }
4538e126ba97SEli Cohen 
453932f69e4bSDaniel Jurgens static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4540e126ba97SEli Cohen {
4541e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
4542e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
4543f614fc15SDan Carpenter 	int err = -ENOMEM;
45442528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
4545e126ba97SEli Cohen 
4546e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
4547e126ba97SEli Cohen 	if (!pprops)
4548e126ba97SEli Cohen 		goto out;
4549e126ba97SEli Cohen 
4550e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4551e126ba97SEli Cohen 	if (!dprops)
4552e126ba97SEli Cohen 		goto out;
4553e126ba97SEli Cohen 
4554c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
4555c43f1112SMaor Gottlieb 	if (err)
4556c43f1112SMaor Gottlieb 		goto out;
4557c43f1112SMaor Gottlieb 
45582528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
4559e126ba97SEli Cohen 	if (err) {
4560e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
4561e126ba97SEli Cohen 		goto out;
4562e126ba97SEli Cohen 	}
4563e126ba97SEli Cohen 
4564c4550c63SOr Gerlitz 	memset(pprops, 0, sizeof(*pprops));
4565e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4566e126ba97SEli Cohen 	if (err) {
4567938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
4568938fe83cSSaeed Mahameed 			     port, err);
456932f69e4bSDaniel Jurgens 		goto out;
4570e126ba97SEli Cohen 	}
457132f69e4bSDaniel Jurgens 
4572938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
4573938fe83cSSaeed Mahameed 					dprops->max_pkeys;
4574938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
4575938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
457632f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
457732f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
4578e126ba97SEli Cohen 
4579e126ba97SEli Cohen out:
4580e126ba97SEli Cohen 	kfree(pprops);
4581e126ba97SEli Cohen 	kfree(dprops);
4582e126ba97SEli Cohen 
4583e126ba97SEli Cohen 	return err;
4584e126ba97SEli Cohen }
4585e126ba97SEli Cohen 
4586e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4587e126ba97SEli Cohen {
4588e126ba97SEli Cohen 	int err;
4589e126ba97SEli Cohen 
4590e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
4591e126ba97SEli Cohen 	if (err)
4592e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4593e126ba97SEli Cohen 
459432927e28SMark Bloch 	if (dev->umrc.qp)
4595e126ba97SEli Cohen 		mlx5_ib_destroy_qp(dev->umrc.qp);
459632927e28SMark Bloch 	if (dev->umrc.cq)
4597add08d76SChristoph Hellwig 		ib_free_cq(dev->umrc.cq);
459832927e28SMark Bloch 	if (dev->umrc.pd)
4599e126ba97SEli Cohen 		ib_dealloc_pd(dev->umrc.pd);
4600e126ba97SEli Cohen }
4601e126ba97SEli Cohen 
4602e126ba97SEli Cohen enum {
4603e126ba97SEli Cohen 	MAX_UMR_WR = 128,
4604e126ba97SEli Cohen };
4605e126ba97SEli Cohen 
4606e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
4607e126ba97SEli Cohen {
4608e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
4609e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
4610e126ba97SEli Cohen 	struct ib_pd *pd;
4611e126ba97SEli Cohen 	struct ib_cq *cq;
4612e126ba97SEli Cohen 	struct ib_qp *qp;
4613e126ba97SEli Cohen 	int ret;
4614e126ba97SEli Cohen 
4615e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4616e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4617e126ba97SEli Cohen 	if (!attr || !init_attr) {
4618e126ba97SEli Cohen 		ret = -ENOMEM;
4619e126ba97SEli Cohen 		goto error_0;
4620e126ba97SEli Cohen 	}
4621e126ba97SEli Cohen 
4622ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
4623e126ba97SEli Cohen 	if (IS_ERR(pd)) {
4624e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4625e126ba97SEli Cohen 		ret = PTR_ERR(pd);
4626e126ba97SEli Cohen 		goto error_0;
4627e126ba97SEli Cohen 	}
4628e126ba97SEli Cohen 
4629add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
4630e126ba97SEli Cohen 	if (IS_ERR(cq)) {
4631e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4632e126ba97SEli Cohen 		ret = PTR_ERR(cq);
4633e126ba97SEli Cohen 		goto error_2;
4634e126ba97SEli Cohen 	}
4635e126ba97SEli Cohen 
4636e126ba97SEli Cohen 	init_attr->send_cq = cq;
4637e126ba97SEli Cohen 	init_attr->recv_cq = cq;
4638e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4639e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
4640e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
4641e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4642e126ba97SEli Cohen 	init_attr->port_num = 1;
4643e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4644e126ba97SEli Cohen 	if (IS_ERR(qp)) {
4645e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4646e126ba97SEli Cohen 		ret = PTR_ERR(qp);
4647e126ba97SEli Cohen 		goto error_3;
4648e126ba97SEli Cohen 	}
4649e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
4650e126ba97SEli Cohen 	qp->real_qp    = qp;
4651e126ba97SEli Cohen 	qp->uobject    = NULL;
4652e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
465331fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
465431fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
4655e126ba97SEli Cohen 
4656e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
4657e126ba97SEli Cohen 	attr->port_num = 1;
4658e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4659e126ba97SEli Cohen 				IB_QP_PORT, NULL);
4660e126ba97SEli Cohen 	if (ret) {
4661e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4662e126ba97SEli Cohen 		goto error_4;
4663e126ba97SEli Cohen 	}
4664e126ba97SEli Cohen 
4665e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4666e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
4667e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
4668e126ba97SEli Cohen 
4669e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4670e126ba97SEli Cohen 	if (ret) {
4671e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4672e126ba97SEli Cohen 		goto error_4;
4673e126ba97SEli Cohen 	}
4674e126ba97SEli Cohen 
4675e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4676e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
4677e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4678e126ba97SEli Cohen 	if (ret) {
4679e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4680e126ba97SEli Cohen 		goto error_4;
4681e126ba97SEli Cohen 	}
4682e126ba97SEli Cohen 
4683e126ba97SEli Cohen 	dev->umrc.qp = qp;
4684e126ba97SEli Cohen 	dev->umrc.cq = cq;
4685e126ba97SEli Cohen 	dev->umrc.pd = pd;
4686e126ba97SEli Cohen 
4687e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
4688e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
4689e126ba97SEli Cohen 	if (ret) {
4690e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4691e126ba97SEli Cohen 		goto error_4;
4692e126ba97SEli Cohen 	}
4693e126ba97SEli Cohen 
4694e126ba97SEli Cohen 	kfree(attr);
4695e126ba97SEli Cohen 	kfree(init_attr);
4696e126ba97SEli Cohen 
4697e126ba97SEli Cohen 	return 0;
4698e126ba97SEli Cohen 
4699e126ba97SEli Cohen error_4:
4700e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
470132927e28SMark Bloch 	dev->umrc.qp = NULL;
4702e126ba97SEli Cohen 
4703e126ba97SEli Cohen error_3:
4704add08d76SChristoph Hellwig 	ib_free_cq(cq);
470532927e28SMark Bloch 	dev->umrc.cq = NULL;
4706e126ba97SEli Cohen 
4707e126ba97SEli Cohen error_2:
4708e126ba97SEli Cohen 	ib_dealloc_pd(pd);
470932927e28SMark Bloch 	dev->umrc.pd = NULL;
4710e126ba97SEli Cohen 
4711e126ba97SEli Cohen error_0:
4712e126ba97SEli Cohen 	kfree(attr);
4713e126ba97SEli Cohen 	kfree(init_attr);
4714e126ba97SEli Cohen 	return ret;
4715e126ba97SEli Cohen }
4716e126ba97SEli Cohen 
47176e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
47186e8484c5SMax Gurtovoy {
47196e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
47206e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
47216e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
47226e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
47236e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
47246e8484c5SMax Gurtovoy 	default:
47256e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
47266e8484c5SMax Gurtovoy 	}
47276e8484c5SMax Gurtovoy }
47286e8484c5SMax Gurtovoy 
4729e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
4730e126ba97SEli Cohen {
4731e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
4732e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
473321a428a0SLeon Romanovsky 	struct ib_device *ibdev;
4734bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
47357722f47eSHaggai Eran 	int port;
4736e126ba97SEli Cohen 	int ret = 0;
4737e126ba97SEli Cohen 
4738e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
473921a428a0SLeon Romanovsky 	ibdev = &dev->ib_dev;
4740e126ba97SEli Cohen 
4741d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
4742d16e91daSHaggai Eran 
474321a428a0SLeon Romanovsky 	devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
474421a428a0SLeon Romanovsky 	if (!devr->p0)
474521a428a0SLeon Romanovsky 		return -ENOMEM;
474621a428a0SLeon Romanovsky 
474721a428a0SLeon Romanovsky 	devr->p0->device  = ibdev;
4748e126ba97SEli Cohen 	devr->p0->uobject = NULL;
4749e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
4750e126ba97SEli Cohen 
475121a428a0SLeon Romanovsky 	ret = mlx5_ib_alloc_pd(devr->p0, NULL, NULL);
475221a428a0SLeon Romanovsky 	if (ret)
475321a428a0SLeon Romanovsky 		goto error0;
475421a428a0SLeon Romanovsky 
4755bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
4756e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
4757e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
4758e126ba97SEli Cohen 		goto error1;
4759e126ba97SEli Cohen 	}
4760e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
4761e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
4762e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
4763e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
4764e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
4765e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
4766e126ba97SEli Cohen 
4767e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4768e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
4769e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
4770e126ba97SEli Cohen 		goto error2;
4771e126ba97SEli Cohen 	}
4772e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
4773e126ba97SEli Cohen 	devr->x0->inode = NULL;
4774e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
4775e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
4776e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4777e126ba97SEli Cohen 
4778e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4779e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
4780e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
4781e126ba97SEli Cohen 		goto error3;
4782e126ba97SEli Cohen 	}
4783e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
4784e126ba97SEli Cohen 	devr->x1->inode = NULL;
4785e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
4786e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
4787e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4788e126ba97SEli Cohen 
4789e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
4790e126ba97SEli Cohen 	attr.attr.max_sge = 1;
4791e126ba97SEli Cohen 	attr.attr.max_wr = 1;
4792e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
47931a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
4794e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
4795e126ba97SEli Cohen 
4796e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4797e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
4798e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
4799e126ba97SEli Cohen 		goto error4;
4800e126ba97SEli Cohen 	}
4801e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
4802e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
4803e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
4804e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
4805e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
4806e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
4807e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
48081a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
4809e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
48101a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
4811e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
4812e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
4813e126ba97SEli Cohen 
48144aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
48154aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
48164aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
48174aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
48184aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
48194aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
48204aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
48214aa17b28SHaggai Abramonvsky 		goto error5;
48224aa17b28SHaggai Abramonvsky 	}
48234aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
48244aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
48254aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
48264aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
48274aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
48284aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
48291a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
48304aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
48311a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
48324aa17b28SHaggai Abramonvsky 
48337722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
48347722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
48357722f47eSHaggai Eran 			  pkey_change_handler);
48367722f47eSHaggai Eran 		devr->ports[port].devr = devr;
48377722f47eSHaggai Eran 	}
48387722f47eSHaggai Eran 
4839e126ba97SEli Cohen 	return 0;
4840e126ba97SEli Cohen 
48414aa17b28SHaggai Abramonvsky error5:
48424aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
4843e126ba97SEli Cohen error4:
4844e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
4845e126ba97SEli Cohen error3:
4846e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
4847e126ba97SEli Cohen error2:
4848e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
4849e126ba97SEli Cohen error1:
4850e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
4851e126ba97SEli Cohen error0:
485221a428a0SLeon Romanovsky 	kfree(devr->p0);
4853e126ba97SEli Cohen 	return ret;
4854e126ba97SEli Cohen }
4855e126ba97SEli Cohen 
4856e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
4857e126ba97SEli Cohen {
48587722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
48597722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
48607722f47eSHaggai Eran 	int port;
48617722f47eSHaggai Eran 
48624aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
4863e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
4864e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
4865e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
4866e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
4867e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
486821a428a0SLeon Romanovsky 	kfree(devr->p0);
48697722f47eSHaggai Eran 
48707722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
48717722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
48727722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
4873e126ba97SEli Cohen }
4874e126ba97SEli Cohen 
4875b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
4876b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
4877e53505a8SAchiad Shochat {
4878e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4879e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
4880e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
4881e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
488285c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
4883e53505a8SAchiad Shochat 	u32 ret = 0;
4884e53505a8SAchiad Shochat 
4885b02289b3SArtemy Kovalyov 	if (rep->grh_required)
4886b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
4887b02289b3SArtemy Kovalyov 
4888e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
4889b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
4890e53505a8SAchiad Shochat 
489185c7c014SDaniel Jurgens 	if (raw_support)
4892b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
489372cd5717SOr Gerlitz 
4894e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
489572cd5717SOr Gerlitz 		return ret;
4896e53505a8SAchiad Shochat 
4897e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
489872cd5717SOr Gerlitz 		return ret;
4899e53505a8SAchiad Shochat 
4900e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
4901e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
4902e53505a8SAchiad Shochat 
4903e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
4904e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
4905e53505a8SAchiad Shochat 
4906e53505a8SAchiad Shochat 	return ret;
4907e53505a8SAchiad Shochat }
4908e53505a8SAchiad Shochat 
49097738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
49107738613eSIra Weiny 			       struct ib_port_immutable *immutable)
49117738613eSIra Weiny {
49127738613eSIra Weiny 	struct ib_port_attr attr;
4913ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4914ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
4915b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
49167738613eSIra Weiny 	int err;
49177738613eSIra Weiny 
4918c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
49197738613eSIra Weiny 	if (err)
49207738613eSIra Weiny 		return err;
49217738613eSIra Weiny 
4922b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
4923b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
4924b02289b3SArtemy Kovalyov 						   &rep);
4925b02289b3SArtemy Kovalyov 		if (err)
4926b02289b3SArtemy Kovalyov 			return err;
4927b02289b3SArtemy Kovalyov 	}
4928b02289b3SArtemy Kovalyov 
49297738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
49307738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
4931b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
4932ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
4933337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
49347738613eSIra Weiny 
49357738613eSIra Weiny 	return 0;
49367738613eSIra Weiny }
49377738613eSIra Weiny 
49388e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
49398e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
49408e6efa3aSMark Bloch {
49418e6efa3aSMark Bloch 	struct ib_port_attr attr;
49428e6efa3aSMark Bloch 	int err;
49438e6efa3aSMark Bloch 
49448e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
49458e6efa3aSMark Bloch 
49468e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
49478e6efa3aSMark Bloch 	if (err)
49488e6efa3aSMark Bloch 		return err;
49498e6efa3aSMark Bloch 
49508e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
49518e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
49528e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
49538e6efa3aSMark Bloch 
49548e6efa3aSMark Bloch 	return 0;
49558e6efa3aSMark Bloch }
49568e6efa3aSMark Bloch 
49579abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
4958c7342823SIra Weiny {
4959c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
4960c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
49619abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
49629abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
49639abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
4964c7342823SIra Weiny }
4965c7342823SIra Weiny 
496645f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
49679ef9c640SAviv Heller {
49689ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
49699ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
49709ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
49719ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
49729ef9c640SAviv Heller 	int err;
49739ef9c640SAviv Heller 
49747c34ec19SAviv Heller 	if (!ns || !mlx5_lag_is_roce(mdev))
49759ef9c640SAviv Heller 		return 0;
49769ef9c640SAviv Heller 
49779ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
49789ef9c640SAviv Heller 	if (err)
49799ef9c640SAviv Heller 		return err;
49809ef9c640SAviv Heller 
49819ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
49829ef9c640SAviv Heller 	if (IS_ERR(ft)) {
49839ef9c640SAviv Heller 		err = PTR_ERR(ft);
49849ef9c640SAviv Heller 		goto err_destroy_vport_lag;
49859ef9c640SAviv Heller 	}
49869ef9c640SAviv Heller 
49879a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
49887c34ec19SAviv Heller 	dev->lag_active = true;
49899ef9c640SAviv Heller 	return 0;
49909ef9c640SAviv Heller 
49919ef9c640SAviv Heller err_destroy_vport_lag:
49929ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
49939ef9c640SAviv Heller 	return err;
49949ef9c640SAviv Heller }
49959ef9c640SAviv Heller 
499645f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
49979ef9c640SAviv Heller {
49989ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
49999ef9c640SAviv Heller 
50007c34ec19SAviv Heller 	if (dev->lag_active) {
50017c34ec19SAviv Heller 		dev->lag_active = false;
50027c34ec19SAviv Heller 
50039a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
50049a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
50059ef9c640SAviv Heller 
50069ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
50079ef9c640SAviv Heller 	}
50089ef9c640SAviv Heller }
50099ef9c640SAviv Heller 
50107fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5011fc24fc5eSAchiad Shochat {
5012e53505a8SAchiad Shochat 	int err;
5013e53505a8SAchiad Shochat 
50147fd8aefbSDaniel Jurgens 	dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
50157fd8aefbSDaniel Jurgens 	err = register_netdevice_notifier(&dev->roce[port_num].nb);
50165ec8c83eSAviv Heller 	if (err) {
50177fd8aefbSDaniel Jurgens 		dev->roce[port_num].nb.notifier_call = NULL;
5018e53505a8SAchiad Shochat 		return err;
50195ec8c83eSAviv Heller 	}
5020e53505a8SAchiad Shochat 
5021d012f5d6SOr Gerlitz 	return 0;
5022d012f5d6SOr Gerlitz }
5023d012f5d6SOr Gerlitz 
50247fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5025e126ba97SEli Cohen {
50267fd8aefbSDaniel Jurgens 	if (dev->roce[port_num].nb.notifier_call) {
50277fd8aefbSDaniel Jurgens 		unregister_netdevice_notifier(&dev->roce[port_num].nb);
50287fd8aefbSDaniel Jurgens 		dev->roce[port_num].nb.notifier_call = NULL;
5029e126ba97SEli Cohen 	}
5030e126ba97SEli Cohen }
5031e126ba97SEli Cohen 
5032e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
5033e126ba97SEli Cohen {
5034e126ba97SEli Cohen 	int err;
5035e126ba97SEli Cohen 
5036ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
5037e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
5038e53505a8SAchiad Shochat 		if (err)
50398e6efa3aSMark Bloch 			return err;
5040ca5b91d6SOr Gerlitz 	}
5041e53505a8SAchiad Shochat 
504245f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
50439ef9c640SAviv Heller 	if (err)
50449ef9c640SAviv Heller 		goto err_disable_roce;
50459ef9c640SAviv Heller 
5046e53505a8SAchiad Shochat 	return 0;
5047e53505a8SAchiad Shochat 
50489ef9c640SAviv Heller err_disable_roce:
5049ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
50509ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
50519ef9c640SAviv Heller 
5052e53505a8SAchiad Shochat 	return err;
5053fc24fc5eSAchiad Shochat }
5054fc24fc5eSAchiad Shochat 
505545f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
5056fc24fc5eSAchiad Shochat {
505745f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
5058ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
5059e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
5060fc24fc5eSAchiad Shochat }
5061fc24fc5eSAchiad Shochat 
5062e1f24a79SParav Pandit struct mlx5_ib_counter {
50637c16f477SKamal Heib 	const char *name;
50647c16f477SKamal Heib 	size_t offset;
50657c16f477SKamal Heib };
50667c16f477SKamal Heib 
50677c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
50687c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
50697c16f477SKamal Heib 
5070e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
50717c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
50727c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
50737c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
50747c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
50757c16f477SKamal Heib };
50767c16f477SKamal Heib 
5077e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
50787c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
50797c16f477SKamal Heib };
50807c16f477SKamal Heib 
5081e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
50827c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
50837c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
50847c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
50857c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
50867c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
50877c16f477SKamal Heib };
50887c16f477SKamal Heib 
5089e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
5090e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
5091e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
5092e1f24a79SParav Pandit 
5093e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
5094e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
5095e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
5096e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
5097e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
5098e1f24a79SParav Pandit };
5099e1f24a79SParav Pandit 
510058dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
510158dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
510258dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
510358dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
510458dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
510558dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
510658dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
510758dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
510858dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
510958dcb60aSParav Pandit };
511058dcb60aSParav Pandit 
51119f876f3dSTalat Batheesh #define INIT_EXT_PPCNT_COUNTER(_name)		\
51129f876f3dSTalat Batheesh 	{ .name = #_name, .offset =	\
51139f876f3dSTalat Batheesh 	MLX5_BYTE_OFF(ppcnt_reg, \
51149f876f3dSTalat Batheesh 		      counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
51159f876f3dSTalat Batheesh 
51169f876f3dSTalat Batheesh static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
51179f876f3dSTalat Batheesh 	INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
51189f876f3dSTalat Batheesh };
51199f876f3dSTalat Batheesh 
5120e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
51210837e86aSMark Bloch {
5122aac4492eSDaniel Jurgens 	int i;
51230837e86aSMark Bloch 
51247c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
5125921c0f5bSParav Pandit 		if (dev->port[i].cnts.set_id_valid)
51260837e86aSMark Bloch 			mlx5_core_dealloc_q_counter(dev->mdev,
5127e1f24a79SParav Pandit 						    dev->port[i].cnts.set_id);
5128e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
5129e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
51307c16f477SKamal Heib 	}
51317c16f477SKamal Heib }
51327c16f477SKamal Heib 
5133e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5134e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
51357c16f477SKamal Heib {
51367c16f477SKamal Heib 	u32 num_counters;
51377c16f477SKamal Heib 
51387c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
51397c16f477SKamal Heib 
51407c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
51417c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
51427c16f477SKamal Heib 
51437c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
51447c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
514558dcb60aSParav Pandit 
514658dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
514758dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
514858dcb60aSParav Pandit 
5149e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
51507c16f477SKamal Heib 
5151e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5152e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5153e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
5154e1f24a79SParav Pandit 	}
51559f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
51569f876f3dSTalat Batheesh 		cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
51579f876f3dSTalat Batheesh 		num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
51589f876f3dSTalat Batheesh 	}
5159e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5160e1f24a79SParav Pandit 	if (!cnts->names)
51617c16f477SKamal Heib 		return -ENOMEM;
51627c16f477SKamal Heib 
5163e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
5164e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
5165e1f24a79SParav Pandit 	if (!cnts->offsets)
51667c16f477SKamal Heib 		goto err_names;
51677c16f477SKamal Heib 
51687c16f477SKamal Heib 	return 0;
51697c16f477SKamal Heib 
51707c16f477SKamal Heib err_names:
5171e1f24a79SParav Pandit 	kfree(cnts->names);
5172aac4492eSDaniel Jurgens 	cnts->names = NULL;
51737c16f477SKamal Heib 	return -ENOMEM;
51747c16f477SKamal Heib }
51757c16f477SKamal Heib 
5176e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
51777c16f477SKamal Heib 				  const char **names,
51787c16f477SKamal Heib 				  size_t *offsets)
51797c16f477SKamal Heib {
51807c16f477SKamal Heib 	int i;
51817c16f477SKamal Heib 	int j = 0;
51827c16f477SKamal Heib 
51837c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
51847c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
51857c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
51867c16f477SKamal Heib 	}
51877c16f477SKamal Heib 
51887c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
51897c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
51907c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
51917c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
51927c16f477SKamal Heib 		}
51937c16f477SKamal Heib 	}
51947c16f477SKamal Heib 
51957c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
51967c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
51977c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
51987c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
51997c16f477SKamal Heib 		}
52007c16f477SKamal Heib 	}
5201e1f24a79SParav Pandit 
520258dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
520358dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
520458dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
520558dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
520658dcb60aSParav Pandit 		}
520758dcb60aSParav Pandit 	}
520858dcb60aSParav Pandit 
5209e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5210e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5211e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
5212e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
5213e1f24a79SParav Pandit 		}
5214e1f24a79SParav Pandit 	}
52159f876f3dSTalat Batheesh 
52169f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
52179f876f3dSTalat Batheesh 		for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
52189f876f3dSTalat Batheesh 			names[j] = ext_ppcnt_cnts[i].name;
52199f876f3dSTalat Batheesh 			offsets[j] = ext_ppcnt_cnts[i].offset;
52209f876f3dSTalat Batheesh 		}
52219f876f3dSTalat Batheesh 	}
52220837e86aSMark Bloch }
52230837e86aSMark Bloch 
5224e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
52250837e86aSMark Bloch {
5226aac4492eSDaniel Jurgens 	int err = 0;
52270837e86aSMark Bloch 	int i;
5228aa74be6eSYishai Hadas 	bool is_shared;
5229aa74be6eSYishai Hadas 
5230aa74be6eSYishai Hadas 	is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
52310837e86aSMark Bloch 
52320837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
5233aac4492eSDaniel Jurgens 		err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5234aac4492eSDaniel Jurgens 		if (err)
5235aac4492eSDaniel Jurgens 			goto err_alloc;
52367c16f477SKamal Heib 
5237aac4492eSDaniel Jurgens 		mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5238aac4492eSDaniel Jurgens 				      dev->port[i].cnts.offsets);
5239aac4492eSDaniel Jurgens 
5240aa74be6eSYishai Hadas 		err = mlx5_cmd_alloc_q_counter(dev->mdev,
5241aa74be6eSYishai Hadas 					       &dev->port[i].cnts.set_id,
5242aa74be6eSYishai Hadas 					       is_shared ?
5243aa74be6eSYishai Hadas 					       MLX5_SHARED_RESOURCE_UID : 0);
5244aac4492eSDaniel Jurgens 		if (err) {
52450837e86aSMark Bloch 			mlx5_ib_warn(dev,
52460837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
5247aac4492eSDaniel Jurgens 				     i + 1, err);
5248aac4492eSDaniel Jurgens 			goto err_alloc;
52490837e86aSMark Bloch 		}
5250aac4492eSDaniel Jurgens 		dev->port[i].cnts.set_id_valid = true;
52510837e86aSMark Bloch 	}
52520837e86aSMark Bloch 
52530837e86aSMark Bloch 	return 0;
52540837e86aSMark Bloch 
5255aac4492eSDaniel Jurgens err_alloc:
5256aac4492eSDaniel Jurgens 	mlx5_ib_dealloc_counters(dev);
5257aac4492eSDaniel Jurgens 	return err;
52580837e86aSMark Bloch }
52590837e86aSMark Bloch 
52600ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
52610ad17a8fSMark Bloch 						    u8 port_num)
52620ad17a8fSMark Bloch {
52637c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
52647c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
52650ad17a8fSMark Bloch 
52660ad17a8fSMark Bloch 	/* We support only per port stats */
52670ad17a8fSMark Bloch 	if (port_num == 0)
52680ad17a8fSMark Bloch 		return NULL;
52690ad17a8fSMark Bloch 
5270e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
5271e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
52729f876f3dSTalat Batheesh 					  port->cnts.num_cong_counters +
52739f876f3dSTalat Batheesh 					  port->cnts.num_ext_ppcnt_counters,
52740ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
52750ad17a8fSMark Bloch }
52760ad17a8fSMark Bloch 
5277aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
5278e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
5279e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
5280e1f24a79SParav Pandit {
5281e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5282e1f24a79SParav Pandit 	void *out;
5283e1f24a79SParav Pandit 	__be32 val;
5284e1f24a79SParav Pandit 	int ret, i;
5285e1f24a79SParav Pandit 
52861b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
5287e1f24a79SParav Pandit 	if (!out)
5288e1f24a79SParav Pandit 		return -ENOMEM;
5289e1f24a79SParav Pandit 
5290aac4492eSDaniel Jurgens 	ret = mlx5_core_query_q_counter(mdev,
5291e1f24a79SParav Pandit 					port->cnts.set_id, 0,
5292e1f24a79SParav Pandit 					out, outlen);
5293e1f24a79SParav Pandit 	if (ret)
5294e1f24a79SParav Pandit 		goto free;
5295e1f24a79SParav Pandit 
5296e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
5297e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
5298e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
5299e1f24a79SParav Pandit 	}
5300e1f24a79SParav Pandit 
5301e1f24a79SParav Pandit free:
5302e1f24a79SParav Pandit 	kvfree(out);
5303e1f24a79SParav Pandit 	return ret;
5304e1f24a79SParav Pandit }
5305e1f24a79SParav Pandit 
53069f876f3dSTalat Batheesh static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
53079f876f3dSTalat Batheesh 					  struct mlx5_ib_port *port,
53089f876f3dSTalat Batheesh 					  struct rdma_hw_stats *stats)
53099f876f3dSTalat Batheesh {
53109f876f3dSTalat Batheesh 	int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
53119f876f3dSTalat Batheesh 	int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
53129f876f3dSTalat Batheesh 	int ret, i;
53139f876f3dSTalat Batheesh 	void *out;
53149f876f3dSTalat Batheesh 
53159f876f3dSTalat Batheesh 	out = kvzalloc(sz, GFP_KERNEL);
53169f876f3dSTalat Batheesh 	if (!out)
53179f876f3dSTalat Batheesh 		return -ENOMEM;
53189f876f3dSTalat Batheesh 
53199f876f3dSTalat Batheesh 	ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
53209f876f3dSTalat Batheesh 	if (ret)
53219f876f3dSTalat Batheesh 		goto free;
53229f876f3dSTalat Batheesh 
53239f876f3dSTalat Batheesh 	for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
53249f876f3dSTalat Batheesh 		stats->value[i + offset] =
53259f876f3dSTalat Batheesh 			be64_to_cpup((__be64 *)(out +
53269f876f3dSTalat Batheesh 				    port->cnts.offsets[i + offset]));
53279f876f3dSTalat Batheesh 	}
53289f876f3dSTalat Batheesh 
53299f876f3dSTalat Batheesh free:
53309f876f3dSTalat Batheesh 	kvfree(out);
53319f876f3dSTalat Batheesh 	return ret;
53329f876f3dSTalat Batheesh }
53339f876f3dSTalat Batheesh 
53340ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
53350ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
53367c16f477SKamal Heib 				u8 port_num, int index)
53370ad17a8fSMark Bloch {
53380ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
53397c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
5340aac4492eSDaniel Jurgens 	struct mlx5_core_dev *mdev;
5341e1f24a79SParav Pandit 	int ret, num_counters;
5342aac4492eSDaniel Jurgens 	u8 mdev_port_num;
53430ad17a8fSMark Bloch 
53447c16f477SKamal Heib 	if (!stats)
5345e1f24a79SParav Pandit 		return -EINVAL;
53460ad17a8fSMark Bloch 
53479f876f3dSTalat Batheesh 	num_counters = port->cnts.num_q_counters +
53489f876f3dSTalat Batheesh 		       port->cnts.num_cong_counters +
53499f876f3dSTalat Batheesh 		       port->cnts.num_ext_ppcnt_counters;
5350aac4492eSDaniel Jurgens 
5351aac4492eSDaniel Jurgens 	/* q_counters are per IB device, query the master mdev */
5352aac4492eSDaniel Jurgens 	ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
53530ad17a8fSMark Bloch 	if (ret)
5354e1f24a79SParav Pandit 		return ret;
53550ad17a8fSMark Bloch 
53569f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
53579f876f3dSTalat Batheesh 		ret =  mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
53589f876f3dSTalat Batheesh 		if (ret)
53599f876f3dSTalat Batheesh 			return ret;
53609f876f3dSTalat Batheesh 	}
53619f876f3dSTalat Batheesh 
5362e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5363aac4492eSDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5364aac4492eSDaniel Jurgens 						    &mdev_port_num);
5365aac4492eSDaniel Jurgens 		if (!mdev) {
5366aac4492eSDaniel Jurgens 			/* If port is not affiliated yet, its in down state
5367aac4492eSDaniel Jurgens 			 * which doesn't have any counters yet, so it would be
5368aac4492eSDaniel Jurgens 			 * zero. So no need to read from the HCA.
5369aac4492eSDaniel Jurgens 			 */
5370aac4492eSDaniel Jurgens 			goto done;
5371aac4492eSDaniel Jurgens 		}
537271a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
537371a0ff65SMajd Dibbiny 						   stats->value +
537471a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters,
537571a0ff65SMajd Dibbiny 						   port->cnts.num_cong_counters,
537671a0ff65SMajd Dibbiny 						   port->cnts.offsets +
537771a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters);
5378aac4492eSDaniel Jurgens 
5379aac4492eSDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
5380e1f24a79SParav Pandit 		if (ret)
5381e1f24a79SParav Pandit 			return ret;
53820ad17a8fSMark Bloch 	}
53837c16f477SKamal Heib 
5384aac4492eSDaniel Jurgens done:
5385e1f24a79SParav Pandit 	return num_counters;
53860ad17a8fSMark Bloch }
53870ad17a8fSMark Bloch 
5388f6a8a19bSDenis Drozdov static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5389693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
5390f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
5391693dfd5aSErez Shitrit {
5392693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
5393f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
5394693dfd5aSErez Shitrit 
5395f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
5396693dfd5aSErez Shitrit }
5397693dfd5aSErez Shitrit 
5398fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5399fe248c3aSMaor Gottlieb {
5400fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
5401fe248c3aSMaor Gottlieb 		return;
5402fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5403fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
5404fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
5405fe248c3aSMaor Gottlieb }
5406fe248c3aSMaor Gottlieb 
540703404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
540803404e8aSMaor Gottlieb {
540903404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
541003404e8aSMaor Gottlieb 		return;
541103404e8aSMaor Gottlieb 
541203404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
5413fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5414fe248c3aSMaor Gottlieb }
5415fe248c3aSMaor Gottlieb 
5416fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5417fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
5418fe248c3aSMaor Gottlieb {
5419fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5420fe248c3aSMaor Gottlieb 	char lbuf[20];
5421fe248c3aSMaor Gottlieb 	int len;
5422fe248c3aSMaor Gottlieb 
5423fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5424fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
5425fe248c3aSMaor Gottlieb }
5426fe248c3aSMaor Gottlieb 
5427fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5428fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
5429fe248c3aSMaor Gottlieb {
5430fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5431fe248c3aSMaor Gottlieb 	u32 timeout;
5432fe248c3aSMaor Gottlieb 	u32 var;
5433fe248c3aSMaor Gottlieb 
5434fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
5435fe248c3aSMaor Gottlieb 		return -EFAULT;
5436fe248c3aSMaor Gottlieb 
5437fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5438fe248c3aSMaor Gottlieb 			1000);
5439fe248c3aSMaor Gottlieb 	if (timeout != var)
5440fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5441fe248c3aSMaor Gottlieb 			    timeout);
5442fe248c3aSMaor Gottlieb 
5443fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
5444fe248c3aSMaor Gottlieb 
5445fe248c3aSMaor Gottlieb 	return count;
5446fe248c3aSMaor Gottlieb }
5447fe248c3aSMaor Gottlieb 
5448fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
5449fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
5450fe248c3aSMaor Gottlieb 	.open	= simple_open,
5451fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
5452fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
5453fe248c3aSMaor Gottlieb };
5454fe248c3aSMaor Gottlieb 
5455fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5456fe248c3aSMaor Gottlieb {
5457fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
5458fe248c3aSMaor Gottlieb 
5459fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
5460fe248c3aSMaor Gottlieb 		return 0;
5461fe248c3aSMaor Gottlieb 
5462fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5463fe248c3aSMaor Gottlieb 	if (!dbg)
5464fe248c3aSMaor Gottlieb 		return -ENOMEM;
5465fe248c3aSMaor Gottlieb 
5466cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
5467cbafad87SSudip Mukherjee 
5468fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
5469fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
5470fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
5471fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
5472cbafad87SSudip Mukherjee 		goto out_debugfs;
5473fe248c3aSMaor Gottlieb 
5474fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
5475fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
5476fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5477fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
5478fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
5479fe248c3aSMaor Gottlieb 		goto out_debugfs;
5480fe248c3aSMaor Gottlieb 
5481fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
5482fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
5483fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5484fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
5485fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
5486fe248c3aSMaor Gottlieb 		goto out_debugfs;
5487fe248c3aSMaor Gottlieb 
5488fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
5489fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
5490fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
5491fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
5492fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
5493fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
5494fe248c3aSMaor Gottlieb 		goto out_debugfs;
5495fe248c3aSMaor Gottlieb 
5496fe248c3aSMaor Gottlieb 	return 0;
5497fe248c3aSMaor Gottlieb 
5498fe248c3aSMaor Gottlieb out_debugfs:
5499fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5500fe248c3aSMaor Gottlieb 	return -ENOMEM;
550103404e8aSMaor Gottlieb }
550203404e8aSMaor Gottlieb 
550303404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
550403404e8aSMaor Gottlieb {
550503404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
550603404e8aSMaor Gottlieb 		return;
550703404e8aSMaor Gottlieb 
550803404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
550903404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
551003404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
551103404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
551203404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
5513fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
5514fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
5515fe248c3aSMaor Gottlieb 
5516fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
5517fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
551803404e8aSMaor Gottlieb }
551903404e8aSMaor Gottlieb 
552032f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
552132f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
552232f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
552332f69e4bSDaniel Jurgens {
552432f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
552532f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
552632f69e4bSDaniel Jurgens 	int comps;
552732f69e4bSDaniel Jurgens 	int err;
552832f69e4bSDaniel Jurgens 	int i;
552932f69e4bSDaniel Jurgens 
5530a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5531a9e546e7SParav Pandit 
553232f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
553332f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
553432f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
553532f69e4bSDaniel Jurgens 		return;
553632f69e4bSDaniel Jurgens 	}
5537df097a27SSaeed Mahameed 
5538df097a27SSaeed Mahameed 	if (mpi->mdev_events.notifier_call)
5539df097a27SSaeed Mahameed 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
5540df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
5541df097a27SSaeed Mahameed 
554232f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
554332f69e4bSDaniel Jurgens 
554432f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
554532f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
554632f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
554732f69e4bSDaniel Jurgens 
554832f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
554932f69e4bSDaniel Jurgens 	if (comps) {
555032f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
555132f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
555232f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
555332f69e4bSDaniel Jurgens 
555432f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
555532f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
555632f69e4bSDaniel Jurgens 
555732f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
555832f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
555932f69e4bSDaniel Jurgens 	}
556032f69e4bSDaniel Jurgens 
556132f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
556232f69e4bSDaniel Jurgens 
556332f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
556432f69e4bSDaniel Jurgens 
556532f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
556632f69e4bSDaniel Jurgens 
556732f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
556832f69e4bSDaniel Jurgens 
556932f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
557032f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
557132f69e4bSDaniel Jurgens 	 * it back to the list.
557232f69e4bSDaniel Jurgens 	 */
557332f69e4bSDaniel Jurgens 	if (err)
557432f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
557532f69e4bSDaniel Jurgens 			    port_num + 1);
557632f69e4bSDaniel Jurgens 
557732f69e4bSDaniel Jurgens 	ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
557832f69e4bSDaniel Jurgens }
557932f69e4bSDaniel Jurgens 
558032f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
558132f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
558232f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
558332f69e4bSDaniel Jurgens {
558432f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
558532f69e4bSDaniel Jurgens 	int err;
558632f69e4bSDaniel Jurgens 
558732f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
558832f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
55892577188eSQing Huang 		mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
559032f69e4bSDaniel Jurgens 			    port_num + 1);
559132f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
559232f69e4bSDaniel Jurgens 		return false;
559332f69e4bSDaniel Jurgens 	}
559432f69e4bSDaniel Jurgens 
559532f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
559632f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
5597df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
559832f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
559932f69e4bSDaniel Jurgens 
560032f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
560132f69e4bSDaniel Jurgens 	if (err)
560232f69e4bSDaniel Jurgens 		goto unbind;
560332f69e4bSDaniel Jurgens 
560432f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
560532f69e4bSDaniel Jurgens 	if (err)
560632f69e4bSDaniel Jurgens 		goto unbind;
560732f69e4bSDaniel Jurgens 
560832f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
560932f69e4bSDaniel Jurgens 	if (err) {
561032f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
561132f69e4bSDaniel Jurgens 			    port_num + 1);
561232f69e4bSDaniel Jurgens 		goto unbind;
561332f69e4bSDaniel Jurgens 	}
561432f69e4bSDaniel Jurgens 
5615df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5616df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5617df097a27SSaeed Mahameed 
561873eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
5619a9e546e7SParav Pandit 
562032f69e4bSDaniel Jurgens 	return true;
562132f69e4bSDaniel Jurgens 
562232f69e4bSDaniel Jurgens unbind:
562332f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
562432f69e4bSDaniel Jurgens 	return false;
562532f69e4bSDaniel Jurgens }
562632f69e4bSDaniel Jurgens 
562732f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
562832f69e4bSDaniel Jurgens {
562932f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
563032f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
563132f69e4bSDaniel Jurgens 							  port_num + 1);
563232f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
563332f69e4bSDaniel Jurgens 	int err;
563432f69e4bSDaniel Jurgens 	int i;
563532f69e4bSDaniel Jurgens 
563632f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
563732f69e4bSDaniel Jurgens 		return 0;
563832f69e4bSDaniel Jurgens 
563932f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
564032f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
564132f69e4bSDaniel Jurgens 	if (err)
564232f69e4bSDaniel Jurgens 		return err;
564332f69e4bSDaniel Jurgens 
564432f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
564532f69e4bSDaniel Jurgens 	if (err)
564632f69e4bSDaniel Jurgens 		return err;
564732f69e4bSDaniel Jurgens 
564832f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
564932f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
565032f69e4bSDaniel Jurgens 		bool bound = false;
565132f69e4bSDaniel Jurgens 
565232f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
565332f69e4bSDaniel Jurgens 		if (i == port_num) {
565432f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
565532f69e4bSDaniel Jurgens 			if (!mpi) {
565632f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
565732f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
565832f69e4bSDaniel Jurgens 				return -ENOMEM;
565932f69e4bSDaniel Jurgens 			}
566032f69e4bSDaniel Jurgens 
566132f69e4bSDaniel Jurgens 			mpi->is_master = true;
566232f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
566332f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
566432f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
566532f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
566632f69e4bSDaniel Jurgens 			mpi = NULL;
566732f69e4bSDaniel Jurgens 			continue;
566832f69e4bSDaniel Jurgens 		}
566932f69e4bSDaniel Jurgens 
567032f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
567132f69e4bSDaniel Jurgens 				    list) {
567232f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
567332f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
567432f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
567532f69e4bSDaniel Jurgens 			}
567632f69e4bSDaniel Jurgens 
567732f69e4bSDaniel Jurgens 			if (bound) {
567832f69e4bSDaniel Jurgens 				dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
567932f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
568032f69e4bSDaniel Jurgens 				list_del(&mpi->list);
568132f69e4bSDaniel Jurgens 				break;
568232f69e4bSDaniel Jurgens 			}
568332f69e4bSDaniel Jurgens 		}
568432f69e4bSDaniel Jurgens 		if (!bound) {
568532f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
568632f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
568732f69e4bSDaniel Jurgens 				    i + 1);
568832f69e4bSDaniel Jurgens 		}
568932f69e4bSDaniel Jurgens 	}
569032f69e4bSDaniel Jurgens 
569132f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
569232f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
569332f69e4bSDaniel Jurgens 	return err;
569432f69e4bSDaniel Jurgens }
569532f69e4bSDaniel Jurgens 
569632f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
569732f69e4bSDaniel Jurgens {
569832f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
569932f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
570032f69e4bSDaniel Jurgens 							  port_num + 1);
570132f69e4bSDaniel Jurgens 	int i;
570232f69e4bSDaniel Jurgens 
570332f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
570432f69e4bSDaniel Jurgens 		return;
570532f69e4bSDaniel Jurgens 
570632f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
570732f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
570832f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
570932f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
571032f69e4bSDaniel Jurgens 			if (i == port_num) {
571132f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
571232f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
571332f69e4bSDaniel Jurgens 			} else {
571432f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
571532f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
571632f69e4bSDaniel Jurgens 			}
571732f69e4bSDaniel Jurgens 		}
571832f69e4bSDaniel Jurgens 	}
571932f69e4bSDaniel Jurgens 
572032f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
572132f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
572232f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
572332f69e4bSDaniel Jurgens 
572432f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
572532f69e4bSDaniel Jurgens }
572632f69e4bSDaniel Jurgens 
57279a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
57289a119cd5SJason Gunthorpe 	mlx5_ib_dm,
57299a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
573024da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
57319a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
573224da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
573383bb4442SJason Gunthorpe 			    UA_MANDATORY),
57349a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
573524da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
573683bb4442SJason Gunthorpe 			    UA_MANDATORY));
573724da0016SAriel Levkovich 
57389a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
57399a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
57409a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
5741c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
5742bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
5743bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
5744c6475a0bSAviad Yehezkel 
57450cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
57460cbf432dSJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
574736e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
57480cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
57490cbf432dSJason Gunthorpe #endif
57508c84660bSMatan Barak 
57510cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
57520cbf432dSJason Gunthorpe 				&mlx5_ib_flow_action),
57530cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
57540cbf432dSJason Gunthorpe 	{}
57550cbf432dSJason Gunthorpe };
57568c84660bSMatan Barak 
57571a1e03dcSRaed Salem static int mlx5_ib_read_counters(struct ib_counters *counters,
57581a1e03dcSRaed Salem 				 struct ib_counters_read_attr *read_attr,
57591a1e03dcSRaed Salem 				 struct uverbs_attr_bundle *attrs)
57601a1e03dcSRaed Salem {
57611a1e03dcSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
57621a1e03dcSRaed Salem 	struct mlx5_read_counters_attr mread_attr = {};
57631a1e03dcSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc;
57641a1e03dcSRaed Salem 	int ret, i;
57651a1e03dcSRaed Salem 
57661a1e03dcSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
57671a1e03dcSRaed Salem 	if (mcounters->cntrs_max_index > read_attr->ncounters) {
57681a1e03dcSRaed Salem 		ret = -EINVAL;
57691a1e03dcSRaed Salem 		goto err_bound;
57701a1e03dcSRaed Salem 	}
57711a1e03dcSRaed Salem 
57721a1e03dcSRaed Salem 	mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
57731a1e03dcSRaed Salem 				 GFP_KERNEL);
57741a1e03dcSRaed Salem 	if (!mread_attr.out) {
57751a1e03dcSRaed Salem 		ret = -ENOMEM;
57761a1e03dcSRaed Salem 		goto err_bound;
57771a1e03dcSRaed Salem 	}
57781a1e03dcSRaed Salem 
57791a1e03dcSRaed Salem 	mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
57801a1e03dcSRaed Salem 	mread_attr.flags = read_attr->flags;
57811a1e03dcSRaed Salem 	ret = mcounters->read_counters(counters->device, &mread_attr);
57821a1e03dcSRaed Salem 	if (ret)
57831a1e03dcSRaed Salem 		goto err_read;
57841a1e03dcSRaed Salem 
57851a1e03dcSRaed Salem 	/* do the pass over the counters data array to assign according to the
57861a1e03dcSRaed Salem 	 * descriptions and indexing pairs
57871a1e03dcSRaed Salem 	 */
57881a1e03dcSRaed Salem 	desc = mcounters->counters_data;
57891a1e03dcSRaed Salem 	for (i = 0; i < mcounters->ncounters; i++)
57901a1e03dcSRaed Salem 		read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
57911a1e03dcSRaed Salem 
57921a1e03dcSRaed Salem err_read:
57931a1e03dcSRaed Salem 	kfree(mread_attr.out);
57941a1e03dcSRaed Salem err_bound:
57951a1e03dcSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
57961a1e03dcSRaed Salem 	return ret;
57971a1e03dcSRaed Salem }
57981a1e03dcSRaed Salem 
5799b29e2a13SRaed Salem static int mlx5_ib_destroy_counters(struct ib_counters *counters)
5800b29e2a13SRaed Salem {
5801b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5802b29e2a13SRaed Salem 
58033b3233fbSRaed Salem 	counters_clear_description(counters);
58043b3233fbSRaed Salem 	if (mcounters->hw_cntrs_hndl)
58053b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(counters->device)->mdev,
58063b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
58073b3233fbSRaed Salem 
5808b29e2a13SRaed Salem 	kfree(mcounters);
5809b29e2a13SRaed Salem 
5810b29e2a13SRaed Salem 	return 0;
5811b29e2a13SRaed Salem }
5812b29e2a13SRaed Salem 
5813b29e2a13SRaed Salem static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
5814b29e2a13SRaed Salem 						   struct uverbs_attr_bundle *attrs)
5815b29e2a13SRaed Salem {
5816b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters;
5817b29e2a13SRaed Salem 
5818b29e2a13SRaed Salem 	mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
5819b29e2a13SRaed Salem 	if (!mcounters)
5820b29e2a13SRaed Salem 		return ERR_PTR(-ENOMEM);
5821b29e2a13SRaed Salem 
58223b3233fbSRaed Salem 	mutex_init(&mcounters->mcntrs_mutex);
58233b3233fbSRaed Salem 
5824b29e2a13SRaed Salem 	return &mcounters->ibcntrs;
5825b29e2a13SRaed Salem }
5826b29e2a13SRaed Salem 
5827b5ca15adSMark Bloch void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
5828e126ba97SEli Cohen {
582932f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
583013859d5dSLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
5831534fd7aaSYishai Hadas 		srcu_barrier(&dev->mr_srcu);
58323cc297dbSMark Bloch 		cleanup_srcu_struct(&dev->mr_srcu);
583313859d5dSLeon Romanovsky 	}
583416c1975fSMark Bloch 	kfree(dev->port);
583516c1975fSMark Bloch }
583616c1975fSMark Bloch 
5837b5ca15adSMark Bloch int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
583816c1975fSMark Bloch {
583916c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
5840e126ba97SEli Cohen 	int err;
584132f69e4bSDaniel Jurgens 	int i;
5842e126ba97SEli Cohen 
5843508562d6SDaniel Jurgens 	dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
58440837e86aSMark Bloch 			    GFP_KERNEL);
58450837e86aSMark Bloch 	if (!dev->port)
584616c1975fSMark Bloch 		return -ENOMEM;
58470837e86aSMark Bloch 
584832f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
584932f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
585032f69e4bSDaniel Jurgens 		rwlock_init(&dev->roce[i].netdev_lock);
585132f69e4bSDaniel Jurgens 	}
585232f69e4bSDaniel Jurgens 
585332f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
5854e126ba97SEli Cohen 	if (err)
58550837e86aSMark Bloch 		goto err_free_port;
5856e126ba97SEli Cohen 
585732f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
585832f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
585932f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
586032f69e4bSDaniel Jurgens 			if (err)
586132f69e4bSDaniel Jurgens 				break;
586232f69e4bSDaniel Jurgens 		}
586332f69e4bSDaniel Jurgens 	} else {
586432f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
586532f69e4bSDaniel Jurgens 	}
586632f69e4bSDaniel Jurgens 	if (err)
586732f69e4bSDaniel Jurgens 		goto err_mp;
586832f69e4bSDaniel Jurgens 
58691b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
5870e126ba97SEli Cohen 		get_ext_port_caps(dev);
5871e126ba97SEli Cohen 
5872e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
5873e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
5874c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
5875e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
5876f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
58779b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
5878e126ba97SEli Cohen 
58793cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
58803cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
58813cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
58823cc297dbSMark Bloch 
588324da0016SAriel Levkovich 	spin_lock_init(&dev->memic.memic_lock);
588424da0016SAriel Levkovich 	dev->memic.dev = mdev;
588524da0016SAriel Levkovich 
588613859d5dSLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
58873cc297dbSMark Bloch 		err = init_srcu_struct(&dev->mr_srcu);
5888a6bc3875SMoni Shoua 		if (err)
5889623d1543SJason Gunthorpe 			goto err_mp;
5890623d1543SJason Gunthorpe 	}
58913cc297dbSMark Bloch 
589216c1975fSMark Bloch 	return 0;
589332f69e4bSDaniel Jurgens err_mp:
589432f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
589516c1975fSMark Bloch 
589616c1975fSMark Bloch err_free_port:
589716c1975fSMark Bloch 	kfree(dev->port);
589816c1975fSMark Bloch 
589916c1975fSMark Bloch 	return -ENOMEM;
590016c1975fSMark Bloch }
590116c1975fSMark Bloch 
59029a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
59039a4ca38dSMark Bloch {
59049a4ca38dSMark Bloch 	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
59059a4ca38dSMark Bloch 
59069a4ca38dSMark Bloch 	if (!dev->flow_db)
59079a4ca38dSMark Bloch 		return -ENOMEM;
59089a4ca38dSMark Bloch 
59099a4ca38dSMark Bloch 	mutex_init(&dev->flow_db->lock);
59109a4ca38dSMark Bloch 
59119a4ca38dSMark Bloch 	return 0;
59129a4ca38dSMark Bloch }
59139a4ca38dSMark Bloch 
5914b5ca15adSMark Bloch int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
5915b5ca15adSMark Bloch {
5916b5ca15adSMark Bloch 	struct mlx5_ib_dev *nic_dev;
5917b5ca15adSMark Bloch 
5918b5ca15adSMark Bloch 	nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
5919b5ca15adSMark Bloch 
5920b5ca15adSMark Bloch 	if (!nic_dev)
5921b5ca15adSMark Bloch 		return -EINVAL;
5922b5ca15adSMark Bloch 
5923b5ca15adSMark Bloch 	dev->flow_db = nic_dev->flow_db;
5924b5ca15adSMark Bloch 
5925b5ca15adSMark Bloch 	return 0;
5926b5ca15adSMark Bloch }
5927b5ca15adSMark Bloch 
59289a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
59299a4ca38dSMark Bloch {
59309a4ca38dSMark Bloch 	kfree(dev->flow_db);
59319a4ca38dSMark Bloch }
59329a4ca38dSMark Bloch 
593396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
593496458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
593596458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
593696458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
593796458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
593896458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
593996458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
594096458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
594196458233SKamal Heib 	.create_counters = mlx5_ib_create_counters,
594296458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
594396458233SKamal Heib 	.create_flow = mlx5_ib_create_flow,
594496458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
594596458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
594696458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
594796458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
594896458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
594996458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
595096458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
595196458233SKamal Heib 	.destroy_counters = mlx5_ib_destroy_counters,
595296458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
595396458233SKamal Heib 	.destroy_flow = mlx5_ib_destroy_flow,
595496458233SKamal Heib 	.destroy_flow_action = mlx5_ib_destroy_flow_action,
595596458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
595696458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
595796458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
595896458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
595996458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
596096458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
596196458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
596296458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
596396458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
596496458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
596596458233SKamal Heib 	.mmap = mlx5_ib_mmap,
596696458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
596796458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
596896458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
596996458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
597096458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
597196458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
597296458233SKamal Heib 	.post_recv = mlx5_ib_post_recv,
597396458233SKamal Heib 	.post_send = mlx5_ib_post_send,
597496458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
597596458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
597696458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
597796458233SKamal Heib 	.query_device = mlx5_ib_query_device,
597896458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
597996458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
598096458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
598196458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
598296458233SKamal Heib 	.read_counters = mlx5_ib_read_counters,
598396458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
598496458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
598596458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
598696458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
598721a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
5988a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
598996458233SKamal Heib };
599096458233SKamal Heib 
599196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
599296458233SKamal Heib 	.create_flow_action_esp = mlx5_ib_create_flow_action_esp,
599396458233SKamal Heib 	.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
599496458233SKamal Heib };
599596458233SKamal Heib 
599696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
599796458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
599896458233SKamal Heib };
599996458233SKamal Heib 
600096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
600196458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
600296458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
600396458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
600496458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
600596458233SKamal Heib };
600696458233SKamal Heib 
600796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
600896458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
600996458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
601096458233SKamal Heib };
601196458233SKamal Heib 
601296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
601396458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
601496458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
601596458233SKamal Heib };
601696458233SKamal Heib 
601796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
601896458233SKamal Heib 	.alloc_dm = mlx5_ib_alloc_dm,
601996458233SKamal Heib 	.dealloc_dm = mlx5_ib_dealloc_dm,
602096458233SKamal Heib 	.reg_dm_mr = mlx5_ib_reg_dm_mr,
602196458233SKamal Heib };
602296458233SKamal Heib 
6023b5ca15adSMark Bloch int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
602416c1975fSMark Bloch {
602516c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
602616c1975fSMark Bloch 	int err;
602716c1975fSMark Bloch 
6028e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
6029e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
6030e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
6031e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
6032e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
6033e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
6034e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
603541c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
603641c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
6037e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
603856e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
6039e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
6040e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
6041e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
6042e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
6043e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
6044e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
6045e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
6046e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
6047e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
6048e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
6049e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
6050e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
6051e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
6052e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
6053e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
6054e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
6055e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
60561707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
6057d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
6058d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
60597d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
6060b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
606196458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ)	|
606296458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW)	|
606396458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
6064e126ba97SEli Cohen 
6065f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
6066f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
606796458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
606896458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
60698e959601SNiranjana Vishwanathapura 
607096458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
607196458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
60727c2344c3SMaor Gottlieb 
60736e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
60746e8484c5SMax Gurtovoy 
6075d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
6076d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
6077d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
6078d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
607996458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
6080d2370e0aSMatan Barak 	}
6081d2370e0aSMatan Barak 
6082938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
6083e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
6084e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
6085e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
608696458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
6087e126ba97SEli Cohen 	}
6088e126ba97SEli Cohen 
608996458233SKamal Heib 	if (MLX5_CAP_DEV_MEM(mdev, memic))
609096458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
609124da0016SAriel Levkovich 
6092dfb631a1SJason Gunthorpe 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
609396458233SKamal Heib 	    MLX5_ACCEL_IPSEC_CAP_DEVICE)
609496458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
60950ede73bcSMatan Barak 	dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
609696458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
609781e30880SYishai Hadas 
609836e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
609936e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
610081e30880SYishai Hadas 
6101e126ba97SEli Cohen 	err = init_node_data(dev);
6102e126ba97SEli Cohen 	if (err)
610316c1975fSMark Bloch 		return err;
6104e126ba97SEli Cohen 
6105c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
6106e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6107e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
6108a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
6109c8b89924SMark Bloch 
611016c1975fSMark Bloch 	return 0;
611116c1975fSMark Bloch }
611216c1975fSMark Bloch 
611396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
611496458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
611596458233SKamal Heib 	.query_port = mlx5_ib_query_port,
611696458233SKamal Heib };
611796458233SKamal Heib 
61188e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
611916c1975fSMark Bloch {
612096458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
61218e6efa3aSMark Bloch 	return 0;
61228e6efa3aSMark Bloch }
61238e6efa3aSMark Bloch 
612496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
612596458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
612696458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
612796458233SKamal Heib };
612896458233SKamal Heib 
6129b5ca15adSMark Bloch int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
61308e6efa3aSMark Bloch {
613196458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
61328e6efa3aSMark Bloch 	return 0;
61338e6efa3aSMark Bloch }
61348e6efa3aSMark Bloch 
613596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
613696458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
613796458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
613896458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
613996458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
614096458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
614196458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
614296458233SKamal Heib };
614396458233SKamal Heib 
6144e3f1ed1fSLeon Romanovsky static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
61458e6efa3aSMark Bloch {
6146e3f1ed1fSLeon Romanovsky 	u8 port_num;
61477fd8aefbSDaniel Jurgens 	int i;
614816c1975fSMark Bloch 
61497fd8aefbSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
61507fd8aefbSDaniel Jurgens 		dev->roce[i].dev = dev;
61517fd8aefbSDaniel Jurgens 		dev->roce[i].native_port_num = i + 1;
61527fd8aefbSDaniel Jurgens 		dev->roce[i].last_port_state = IB_PORT_DOWN;
61537fd8aefbSDaniel Jurgens 	}
61547fd8aefbSDaniel Jurgens 
6155c11a226aSMark Bloch 	dev->ib_dev.uverbs_ex_cmd_mask |=
6156c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6157c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6158c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6159c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6160c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
616196458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
61628e6efa3aSMark Bloch 
6163e3f1ed1fSLeon Romanovsky 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6164e3f1ed1fSLeon Romanovsky 
61658e6efa3aSMark Bloch 	return mlx5_add_netdev_notifier(dev, port_num);
61668e6efa3aSMark Bloch }
61678e6efa3aSMark Bloch 
61688e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
61698e6efa3aSMark Bloch {
61708e6efa3aSMark Bloch 	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
61718e6efa3aSMark Bloch 
61728e6efa3aSMark Bloch 	mlx5_remove_netdev_notifier(dev, port_num);
61738e6efa3aSMark Bloch }
61748e6efa3aSMark Bloch 
61758e6efa3aSMark Bloch int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
61768e6efa3aSMark Bloch {
61778e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
61788e6efa3aSMark Bloch 	enum rdma_link_layer ll;
61798e6efa3aSMark Bloch 	int port_type_cap;
61808e6efa3aSMark Bloch 	int err = 0;
61818e6efa3aSMark Bloch 
61828e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
61838e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
61848e6efa3aSMark Bloch 
61858e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET)
6186e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
61878e6efa3aSMark Bloch 
618816c1975fSMark Bloch 	return err;
6189fc24fc5eSAchiad Shochat }
6190fc24fc5eSAchiad Shochat 
61918e6efa3aSMark Bloch void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
61928e6efa3aSMark Bloch {
61938e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
61948e6efa3aSMark Bloch }
61958e6efa3aSMark Bloch 
61968e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
61978e6efa3aSMark Bloch {
61988e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
61998e6efa3aSMark Bloch 	enum rdma_link_layer ll;
62008e6efa3aSMark Bloch 	int port_type_cap;
62018e6efa3aSMark Bloch 	int err;
62028e6efa3aSMark Bloch 
62038e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
62048e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
62058e6efa3aSMark Bloch 
62068e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
6207e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
62088e6efa3aSMark Bloch 		if (err)
62098e6efa3aSMark Bloch 			return err;
62108e6efa3aSMark Bloch 
6211e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
62128e6efa3aSMark Bloch 		if (err)
62138e6efa3aSMark Bloch 			goto cleanup;
62148e6efa3aSMark Bloch 	}
62158e6efa3aSMark Bloch 
621616c1975fSMark Bloch 	return 0;
62178e6efa3aSMark Bloch cleanup:
62188e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
62198e6efa3aSMark Bloch 
62208e6efa3aSMark Bloch 	return err;
622145bded2cSKamal Heib }
62226aec21f6SHaggai Eran 
622316c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
622416c1975fSMark Bloch {
622516c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
622616c1975fSMark Bloch 	enum rdma_link_layer ll;
622716c1975fSMark Bloch 	int port_type_cap;
62284a2da0b8SParav Pandit 
622916c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
623016c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
623116c1975fSMark Bloch 
623216c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
623316c1975fSMark Bloch 		mlx5_disable_eth(dev);
62348e6efa3aSMark Bloch 		mlx5_ib_stage_common_roce_cleanup(dev);
623516c1975fSMark Bloch 	}
623616c1975fSMark Bloch }
623716c1975fSMark Bloch 
6238b5ca15adSMark Bloch int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
623916c1975fSMark Bloch {
624016c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
624116c1975fSMark Bloch }
624216c1975fSMark Bloch 
6243b5ca15adSMark Bloch void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
624416c1975fSMark Bloch {
624516c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
624616c1975fSMark Bloch }
624716c1975fSMark Bloch 
624816c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
624916c1975fSMark Bloch {
625007321b3cSMark Bloch 	mlx5_ib_internal_fill_odp_caps(dev);
625107321b3cSMark Bloch 
625216c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
625316c1975fSMark Bloch }
625416c1975fSMark Bloch 
6255f3ffed0cSKamal Heib static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
6256d5d284b8SSaeed Mahameed {
6257d5d284b8SSaeed Mahameed 	mlx5_ib_odp_cleanup_one(dev);
6258d5d284b8SSaeed Mahameed }
6259d5d284b8SSaeed Mahameed 
626096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
626196458233SKamal Heib 	.alloc_hw_stats = mlx5_ib_alloc_hw_stats,
626296458233SKamal Heib 	.get_hw_stats = mlx5_ib_get_hw_stats,
626396458233SKamal Heib };
626496458233SKamal Heib 
6265b5ca15adSMark Bloch int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
626616c1975fSMark Bloch {
62675e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
626896458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
62695e1e7612SMark Bloch 
627016c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
62715e1e7612SMark Bloch 	}
627216c1975fSMark Bloch 
627316c1975fSMark Bloch 	return 0;
627416c1975fSMark Bloch }
627516c1975fSMark Bloch 
6276b5ca15adSMark Bloch void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
627716c1975fSMark Bloch {
627816c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
627916c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
628016c1975fSMark Bloch }
628116c1975fSMark Bloch 
628216c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
628316c1975fSMark Bloch {
628473eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
6285a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
628673eb8f03SGreg Kroah-Hartman 	return 0;
628716c1975fSMark Bloch }
628816c1975fSMark Bloch 
628916c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
629016c1975fSMark Bloch {
6291a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
6292a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
629316c1975fSMark Bloch }
629416c1975fSMark Bloch 
629516c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
629616c1975fSMark Bloch {
62975fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
6298444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
629916c1975fSMark Bloch }
630016c1975fSMark Bloch 
630116c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
630216c1975fSMark Bloch {
630316c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
630416c1975fSMark Bloch }
630516c1975fSMark Bloch 
6306b5ca15adSMark Bloch int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
630716c1975fSMark Bloch {
630816c1975fSMark Bloch 	int err;
63095fe9dec0SEli Cohen 
63105fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
63115fe9dec0SEli Cohen 	if (err)
631216c1975fSMark Bloch 		return err;
63135fe9dec0SEli Cohen 
63145fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
63155fe9dec0SEli Cohen 	if (err)
631616c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
63175fe9dec0SEli Cohen 
631816c1975fSMark Bloch 	return err;
631916c1975fSMark Bloch }
63200837e86aSMark Bloch 
6321b5ca15adSMark Bloch void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
632216c1975fSMark Bloch {
632316c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
632416c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
632516c1975fSMark Bloch }
6326e126ba97SEli Cohen 
6327b5ca15adSMark Bloch int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
632816c1975fSMark Bloch {
6329e349f858SJason Gunthorpe 	const char *name;
6330e349f858SJason Gunthorpe 
6331508a523fSParav Pandit 	rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
63327c34ec19SAviv Heller 	if (!mlx5_lag_is_roce(dev->mdev))
6333e349f858SJason Gunthorpe 		name = "mlx5_%d";
6334e349f858SJason Gunthorpe 	else
6335e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
6336ea4baf7fSParav Pandit 	return ib_register_device(&dev->ib_dev, name);
633716c1975fSMark Bloch }
633816c1975fSMark Bloch 
633903fe2debSDavid S. Miller void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
634042cea83fSMark Bloch {
634142cea83fSMark Bloch 	destroy_umrc_res(dev);
634242cea83fSMark Bloch }
634342cea83fSMark Bloch 
6344b5ca15adSMark Bloch void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
634516c1975fSMark Bloch {
634616c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
634716c1975fSMark Bloch }
634816c1975fSMark Bloch 
634903fe2debSDavid S. Miller int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
635016c1975fSMark Bloch {
635116c1975fSMark Bloch 	return create_umr_res(dev);
635216c1975fSMark Bloch }
635316c1975fSMark Bloch 
635416c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
635516c1975fSMark Bloch {
635603404e8aSMaor Gottlieb 	init_delay_drop(dev);
635703404e8aSMaor Gottlieb 
635816c1975fSMark Bloch 	return 0;
635916c1975fSMark Bloch }
636016c1975fSMark Bloch 
636116c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
636216c1975fSMark Bloch {
636316c1975fSMark Bloch 	cancel_delay_drop(dev);
636416c1975fSMark Bloch }
636516c1975fSMark Bloch 
6366df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
6367df097a27SSaeed Mahameed {
6368df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
6369df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
6370df097a27SSaeed Mahameed 	return 0;
6371df097a27SSaeed Mahameed }
6372df097a27SSaeed Mahameed 
6373df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
6374df097a27SSaeed Mahameed {
6375df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
6376df097a27SSaeed Mahameed }
6377df097a27SSaeed Mahameed 
637881773ce5SLeon Romanovsky static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
637981773ce5SLeon Romanovsky {
638081773ce5SLeon Romanovsky 	int uid;
638181773ce5SLeon Romanovsky 
6382fb98153bSYishai Hadas 	uid = mlx5_ib_devx_create(dev, false);
638381773ce5SLeon Romanovsky 	if (uid > 0)
638481773ce5SLeon Romanovsky 		dev->devx_whitelist_uid = uid;
638581773ce5SLeon Romanovsky 
638681773ce5SLeon Romanovsky 	return 0;
638781773ce5SLeon Romanovsky }
638881773ce5SLeon Romanovsky static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
638981773ce5SLeon Romanovsky {
639081773ce5SLeon Romanovsky 	if (dev->devx_whitelist_uid)
639181773ce5SLeon Romanovsky 		mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
639281773ce5SLeon Romanovsky }
639381773ce5SLeon Romanovsky 
6394b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
639516c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
639616c1975fSMark Bloch 		      int stage)
639716c1975fSMark Bloch {
639816c1975fSMark Bloch 	/* Number of stages to cleanup */
639916c1975fSMark Bloch 	while (stage) {
640016c1975fSMark Bloch 		stage--;
640116c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
640216c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
640316c1975fSMark Bloch 	}
640416c1975fSMark Bloch }
640516c1975fSMark Bloch 
6406b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
640716c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
640816c1975fSMark Bloch {
640916c1975fSMark Bloch 	int err;
641016c1975fSMark Bloch 	int i;
641116c1975fSMark Bloch 
641216c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
641316c1975fSMark Bloch 		if (profile->stage[i].init) {
641416c1975fSMark Bloch 			err = profile->stage[i].init(dev);
641516c1975fSMark Bloch 			if (err)
641616c1975fSMark Bloch 				goto err_out;
641716c1975fSMark Bloch 		}
641816c1975fSMark Bloch 	}
641916c1975fSMark Bloch 
642016c1975fSMark Bloch 	dev->profile = profile;
6421e126ba97SEli Cohen 	dev->ib_active = true;
6422e126ba97SEli Cohen 
64239603b61dSJack Morgenstein 	return dev;
6424e126ba97SEli Cohen 
642516c1975fSMark Bloch err_out:
642616c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
6427e126ba97SEli Cohen 
64289603b61dSJack Morgenstein 	return NULL;
6429e126ba97SEli Cohen }
6430e126ba97SEli Cohen 
643116c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
643216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
643316c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
643416c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
64359a4ca38dSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
64369a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_init,
64379a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
643816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
643916c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
644016c1975fSMark Bloch 		     NULL),
64418e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
64428e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
64438e6efa3aSMark Bloch 		     NULL),
644416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
644516c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
644616c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
6447f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6448f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
6449f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
645016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
645116c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
645216c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
6453df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6454df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
6455df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
645616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
645716c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
6458d5d284b8SSaeed Mahameed 		     mlx5_ib_stage_odp_cleanup),
645916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
646016c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
646116c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
646216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
646316c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
646416c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
646516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
646616c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
646716c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
646816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
646916c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
647016c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
647142cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
647242cea83fSMark Bloch 		     NULL,
647342cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
647481773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
647581773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_init,
647681773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_cleanup),
647716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
647816c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
647916c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
648042cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
648142cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
648242cea83fSMark Bloch 		     NULL),
648316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
648416c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
648516c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
648616c1975fSMark Bloch };
648716c1975fSMark Bloch 
6488f0666f1fSBodong Wang const struct mlx5_ib_profile uplink_rep_profile = {
6489b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
6490b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
6491b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
6492b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6493b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_init,
6494b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
6495b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6496b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
6497b5ca15adSMark Bloch 		     NULL),
6498b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6499b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_non_default_cb,
6500b5ca15adSMark Bloch 		     NULL),
6501b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6502b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_init,
6503b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_cleanup),
6504f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6505f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
6506f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
6507b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6508b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_init,
6509b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
6510df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6511df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
6512df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
6513b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6514b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_init,
6515b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
6516b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
6517b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
6518b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
6519b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6520b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
6521b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
652203fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
652303fe2debSDavid S. Miller 		     NULL,
652403fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
6525b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6526b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
6527b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
652803fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
652903fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
653003fe2debSDavid S. Miller 		     NULL),
6531b5ca15adSMark Bloch };
6532b5ca15adSMark Bloch 
6533e3f1ed1fSLeon Romanovsky static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
653432f69e4bSDaniel Jurgens {
653532f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
653632f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
653732f69e4bSDaniel Jurgens 	bool bound = false;
653832f69e4bSDaniel Jurgens 	int err;
653932f69e4bSDaniel Jurgens 
654032f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
654132f69e4bSDaniel Jurgens 	if (!mpi)
654232f69e4bSDaniel Jurgens 		return NULL;
654332f69e4bSDaniel Jurgens 
654432f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
654532f69e4bSDaniel Jurgens 
654632f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
654732f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
654832f69e4bSDaniel Jurgens 	if (err) {
654932f69e4bSDaniel Jurgens 		kfree(mpi);
655032f69e4bSDaniel Jurgens 		return NULL;
655132f69e4bSDaniel Jurgens 	}
655232f69e4bSDaniel Jurgens 
655332f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
655432f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
655532f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
655632f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
655732f69e4bSDaniel Jurgens 
655832f69e4bSDaniel Jurgens 		if (bound) {
655932f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
656032f69e4bSDaniel Jurgens 			break;
656132f69e4bSDaniel Jurgens 		}
656232f69e4bSDaniel Jurgens 	}
656332f69e4bSDaniel Jurgens 
656432f69e4bSDaniel Jurgens 	if (!bound) {
656532f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
656632f69e4bSDaniel Jurgens 		dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
656732f69e4bSDaniel Jurgens 	}
656832f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
656932f69e4bSDaniel Jurgens 
657032f69e4bSDaniel Jurgens 	return mpi;
657132f69e4bSDaniel Jurgens }
657232f69e4bSDaniel Jurgens 
657316c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
657416c1975fSMark Bloch {
657532f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
6576b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
657732f69e4bSDaniel Jurgens 	int port_type_cap;
657832f69e4bSDaniel Jurgens 
6579b5ca15adSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
6580b5ca15adSMark Bloch 
6581f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) &&
6582f0666f1fSBodong Wang 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
6583f0666f1fSBodong Wang 		mlx5_ib_register_vport_reps(mdev);
6584f0666f1fSBodong Wang 		return mdev;
6585f0666f1fSBodong Wang 	}
6586f0666f1fSBodong Wang 
658732f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
658832f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
658932f69e4bSDaniel Jurgens 
6590e3f1ed1fSLeon Romanovsky 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6591e3f1ed1fSLeon Romanovsky 		return mlx5_ib_add_slave_port(mdev);
659232f69e4bSDaniel Jurgens 
6593459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
6594b5ca15adSMark Bloch 	if (!dev)
6595b5ca15adSMark Bloch 		return NULL;
6596b5ca15adSMark Bloch 
6597b5ca15adSMark Bloch 	dev->mdev = mdev;
6598b5ca15adSMark Bloch 	dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6599b5ca15adSMark Bloch 			     MLX5_CAP_GEN(mdev, num_vhca_ports));
6600b5ca15adSMark Bloch 
6601b5ca15adSMark Bloch 	return __mlx5_ib_add(dev, &pf_profile);
660216c1975fSMark Bloch }
660316c1975fSMark Bloch 
66049603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
6605e126ba97SEli Cohen {
660632f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
660732f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
66086aec21f6SHaggai Eran 
6609f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
6610f0666f1fSBodong Wang 		mlx5_ib_unregister_vport_reps(mdev);
6611f0666f1fSBodong Wang 		return;
6612f0666f1fSBodong Wang 	}
6613f0666f1fSBodong Wang 
661432f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
661532f69e4bSDaniel Jurgens 		mpi = context;
661632f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
661732f69e4bSDaniel Jurgens 		if (mpi->ibdev)
661832f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
661932f69e4bSDaniel Jurgens 		list_del(&mpi->list);
662032f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
662132f69e4bSDaniel Jurgens 		return;
662232f69e4bSDaniel Jurgens 	}
662332f69e4bSDaniel Jurgens 
662432f69e4bSDaniel Jurgens 	dev = context;
662516c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
662606cc74afSMark Bloch 
662706cc74afSMark Bloch 	ib_dealloc_device((struct ib_device *)dev);
6628e126ba97SEli Cohen }
6629e126ba97SEli Cohen 
66309603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
66319603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
66329603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
663364613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
6634e126ba97SEli Cohen };
6635e126ba97SEli Cohen 
6636c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
6637c44ef998SIlya Lesokhin {
6638c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
6639c44ef998SIlya Lesokhin 	return xlt_emergency_page;
6640c44ef998SIlya Lesokhin }
6641c44ef998SIlya Lesokhin 
6642c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
6643c44ef998SIlya Lesokhin {
6644c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
6645c44ef998SIlya Lesokhin }
6646c44ef998SIlya Lesokhin 
6647e126ba97SEli Cohen static int __init mlx5_ib_init(void)
6648e126ba97SEli Cohen {
66496aec21f6SHaggai Eran 	int err;
66506aec21f6SHaggai Eran 
6651c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
6652c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
6653d69a24e0SDaniel Jurgens 		return -ENOMEM;
6654d69a24e0SDaniel Jurgens 
6655c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
6656c44ef998SIlya Lesokhin 
6657c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6658c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
6659c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
6660c44ef998SIlya Lesokhin 		return -ENOMEM;
6661c44ef998SIlya Lesokhin 	}
6662c44ef998SIlya Lesokhin 
666381713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
66649603b61dSJack Morgenstein 
66656aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
66666aec21f6SHaggai Eran 
66676aec21f6SHaggai Eran 	return err;
6668e126ba97SEli Cohen }
6669e126ba97SEli Cohen 
6670e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
6671e126ba97SEli Cohen {
66729603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
6673d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
6674c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
6675c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
6676e126ba97SEli Cohen }
6677e126ba97SEli Cohen 
6678e126ba97SEli Cohen module_init(mlx5_ib_init);
6679e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
6680