xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision c6a21c3864fc7f5febae7d096cd136f397c791f2)
1e126ba97SEli Cohen /*
26cf0a15fSSaeed Mahameed  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3e126ba97SEli Cohen  *
4e126ba97SEli Cohen  * This software is available to you under a choice of one of two
5e126ba97SEli Cohen  * licenses.  You may choose to be licensed under the terms of the GNU
6e126ba97SEli Cohen  * General Public License (GPL) Version 2, available from the file
7e126ba97SEli Cohen  * COPYING in the main directory of this source tree, or the
8e126ba97SEli Cohen  * OpenIB.org BSD license below:
9e126ba97SEli Cohen  *
10e126ba97SEli Cohen  *     Redistribution and use in source and binary forms, with or
11e126ba97SEli Cohen  *     without modification, are permitted provided that the following
12e126ba97SEli Cohen  *     conditions are met:
13e126ba97SEli Cohen  *
14e126ba97SEli Cohen  *      - Redistributions of source code must retain the above
15e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
16e126ba97SEli Cohen  *        disclaimer.
17e126ba97SEli Cohen  *
18e126ba97SEli Cohen  *      - Redistributions in binary form must reproduce the above
19e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
20e126ba97SEli Cohen  *        disclaimer in the documentation and/or other materials
21e126ba97SEli Cohen  *        provided with the distribution.
22e126ba97SEli Cohen  *
23e126ba97SEli Cohen  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e126ba97SEli Cohen  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e126ba97SEli Cohen  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e126ba97SEli Cohen  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e126ba97SEli Cohen  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e126ba97SEli Cohen  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e126ba97SEli Cohen  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e126ba97SEli Cohen  * SOFTWARE.
31e126ba97SEli Cohen  */
32e126ba97SEli Cohen 
33fe248c3aSMaor Gottlieb #include <linux/debugfs.h>
34adec640eSChristoph Hellwig #include <linux/highmem.h>
35e126ba97SEli Cohen #include <linux/module.h>
36e126ba97SEli Cohen #include <linux/init.h>
37e126ba97SEli Cohen #include <linux/errno.h>
38e126ba97SEli Cohen #include <linux/pci.h>
39e126ba97SEli Cohen #include <linux/dma-mapping.h>
40e126ba97SEli Cohen #include <linux/slab.h>
4124da0016SAriel Levkovich #include <linux/bitmap.h>
4237aa5c36SGuy Levi #if defined(CONFIG_X86)
4337aa5c36SGuy Levi #include <asm/pat.h>
4437aa5c36SGuy Levi #endif
45e126ba97SEli Cohen #include <linux/sched.h>
466e84f315SIngo Molnar #include <linux/sched/mm.h>
470881e7bdSIngo Molnar #include <linux/sched/task.h>
487c2344c3SMaor Gottlieb #include <linux/delay.h>
49e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
503f89a643SAchiad Shochat #include <rdma/ib_addr.h>
512811ba51SAchiad Shochat #include <rdma/ib_cache.h>
52ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
531b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
5472c7fe90SPravin Shedge #include <linux/mlx5/fs.h>
557c2344c3SMaor Gottlieb #include <linux/list.h>
56e126ba97SEli Cohen #include <rdma/ib_smi.h>
57e126ba97SEli Cohen #include <rdma/ib_umem.h>
58038d2ef8SMaor Gottlieb #include <linux/in.h>
59038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
60e126ba97SEli Cohen #include "mlx5_ib.h"
61fc385b7aSMark Bloch #include "ib_rep.h"
62e1f24a79SParav Pandit #include "cmd.h"
633346c487SBoris Pismenny #include <linux/mlx5/fs_helpers.h>
64c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h>
658c84660bSMatan Barak #include <rdma/uverbs_std_types.h>
66c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h>
67c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h>
688c84660bSMatan Barak 
698c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
708c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
71e126ba97SEli Cohen 
72e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
73b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0"
74e126ba97SEli Cohen 
75e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
76e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
77e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
78e126ba97SEli Cohen 
79e126ba97SEli Cohen static char mlx5_version[] =
80e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
81b359911dSTariq Toukan 	DRIVER_VERSION "\n";
82e126ba97SEli Cohen 
83d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
84d69a24e0SDaniel Jurgens 	struct work_struct	work;
85d69a24e0SDaniel Jurgens 	struct mlx5_core_dev	*dev;
86d69a24e0SDaniel Jurgens 	void			*context;
87d69a24e0SDaniel Jurgens 	enum mlx5_dev_event	event;
88d69a24e0SDaniel Jurgens 	unsigned long		param;
89d69a24e0SDaniel Jurgens };
90d69a24e0SDaniel Jurgens 
91da7525d2SEran Ben Elisha enum {
92da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
93da7525d2SEran Ben Elisha };
941b5daf11SMajd Dibbiny 
95d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
9632f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
9732f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
9832f69e4bSDaniel Jurgens /*
9932f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
10032f69e4bSDaniel Jurgens  */
10132f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
10232f69e4bSDaniel Jurgens 
103c44ef998SIlya Lesokhin /* We can't use an array for xlt_emergency_page because dma_map_single
104c44ef998SIlya Lesokhin  * doesn't work on kernel modules memory
105c44ef998SIlya Lesokhin  */
106c44ef998SIlya Lesokhin static unsigned long xlt_emergency_page;
107c44ef998SIlya Lesokhin static struct mutex xlt_emergency_page_mutex;
108c44ef998SIlya Lesokhin 
10932f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
11032f69e4bSDaniel Jurgens {
11132f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
11232f69e4bSDaniel Jurgens 
11332f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
11432f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
11532f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
11632f69e4bSDaniel Jurgens 	return dev;
11732f69e4bSDaniel Jurgens }
11832f69e4bSDaniel Jurgens 
1191b5daf11SMajd Dibbiny static enum rdma_link_layer
120ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
1211b5daf11SMajd Dibbiny {
122ebd61f68SAchiad Shochat 	switch (port_type_cap) {
1231b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
1241b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
1251b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
1261b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
1271b5daf11SMajd Dibbiny 	default:
1281b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1291b5daf11SMajd Dibbiny 	}
1301b5daf11SMajd Dibbiny }
1311b5daf11SMajd Dibbiny 
132ebd61f68SAchiad Shochat static enum rdma_link_layer
133ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
134ebd61f68SAchiad Shochat {
135ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
136ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
137ebd61f68SAchiad Shochat 
138ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
139ebd61f68SAchiad Shochat }
140ebd61f68SAchiad Shochat 
141fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
142fd65f1b8SMoni Shoua 			  u8 port_num,
143fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
144fd65f1b8SMoni Shoua {
145fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
146fd65f1b8SMoni Shoua 	int ret;
147fd65f1b8SMoni Shoua 
148fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1498e6efa3aSMark Bloch 	ret = ibdev->query_port(ibdev, port_num, &attr);
150fd65f1b8SMoni Shoua 	if (!ret)
151fd65f1b8SMoni Shoua 		*state = attr.state;
152fd65f1b8SMoni Shoua 	return ret;
153fd65f1b8SMoni Shoua }
154fd65f1b8SMoni Shoua 
155fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
156fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
157fc24fc5eSAchiad Shochat {
1587fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
159fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1607fd8aefbSDaniel Jurgens 	u8 port_num = roce->native_port_num;
1617fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1627fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1637fd8aefbSDaniel Jurgens 
1647fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
16532f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
16632f69e4bSDaniel Jurgens 	if (!mdev)
16732f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
168fc24fc5eSAchiad Shochat 
1695ec8c83eSAviv Heller 	switch (event) {
1705ec8c83eSAviv Heller 	case NETDEV_REGISTER:
1715ec8c83eSAviv Heller 	case NETDEV_UNREGISTER:
1727fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
173bcf87f1dSMark Bloch 		if (ibdev->rep) {
174bcf87f1dSMark Bloch 			struct mlx5_eswitch *esw = ibdev->mdev->priv.eswitch;
175bcf87f1dSMark Bloch 			struct net_device *rep_ndev;
1767fd8aefbSDaniel Jurgens 
177bcf87f1dSMark Bloch 			rep_ndev = mlx5_ib_get_rep_netdev(esw,
178bcf87f1dSMark Bloch 							  ibdev->rep->vport);
179bcf87f1dSMark Bloch 			if (rep_ndev == ndev)
1807fd8aefbSDaniel Jurgens 				roce->netdev = (event == NETDEV_UNREGISTER) ?
1815ec8c83eSAviv Heller 					NULL : ndev;
18284a6a7a9SParav Pandit 		} else if (ndev->dev.parent == &mdev->pdev->dev) {
183bcf87f1dSMark Bloch 			roce->netdev = (event == NETDEV_UNREGISTER) ?
184bcf87f1dSMark Bloch 				NULL : ndev;
185bcf87f1dSMark Bloch 		}
1867fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
1875ec8c83eSAviv Heller 		break;
1885ec8c83eSAviv Heller 
189fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
1905ec8c83eSAviv Heller 	case NETDEV_UP:
19188621dfeSAviv Heller 	case NETDEV_DOWN: {
1927fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
19388621dfeSAviv Heller 		struct net_device *upper = NULL;
19488621dfeSAviv Heller 
19588621dfeSAviv Heller 		if (lag_ndev) {
19688621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
19788621dfeSAviv Heller 			dev_put(lag_ndev);
19888621dfeSAviv Heller 		}
19988621dfeSAviv Heller 
2007fd8aefbSDaniel Jurgens 		if ((upper == ndev || (!upper && ndev == roce->netdev))
20188621dfeSAviv Heller 		    && ibdev->ib_active) {
202626bc02dSBart Van Assche 			struct ib_event ibev = { };
203fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2045ec8c83eSAviv Heller 
2057fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2067fd8aefbSDaniel Jurgens 					   &port_state))
2077fd8aefbSDaniel Jurgens 				goto done;
208fd65f1b8SMoni Shoua 
2097fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2107fd8aefbSDaniel Jurgens 				goto done;
211fd65f1b8SMoni Shoua 
2127fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2135ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
214fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
215fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
216fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
217fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
218fd65f1b8SMoni Shoua 			else
2197fd8aefbSDaniel Jurgens 				goto done;
220fd65f1b8SMoni Shoua 
2217fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2225ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2235ec8c83eSAviv Heller 		}
2245ec8c83eSAviv Heller 		break;
22588621dfeSAviv Heller 	}
2265ec8c83eSAviv Heller 
2275ec8c83eSAviv Heller 	default:
2285ec8c83eSAviv Heller 		break;
2295ec8c83eSAviv Heller 	}
2307fd8aefbSDaniel Jurgens done:
23132f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
232fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
233fc24fc5eSAchiad Shochat }
234fc24fc5eSAchiad Shochat 
235fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
236fc24fc5eSAchiad Shochat 					     u8 port_num)
237fc24fc5eSAchiad Shochat {
238fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
239fc24fc5eSAchiad Shochat 	struct net_device *ndev;
24032f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
241fc24fc5eSAchiad Shochat 
24232f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
24332f69e4bSDaniel Jurgens 	if (!mdev)
24432f69e4bSDaniel Jurgens 		return NULL;
24532f69e4bSDaniel Jurgens 
24632f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
24788621dfeSAviv Heller 	if (ndev)
24832f69e4bSDaniel Jurgens 		goto out;
24988621dfeSAviv Heller 
250fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
251fc24fc5eSAchiad Shochat 	 */
2527fd8aefbSDaniel Jurgens 	read_lock(&ibdev->roce[port_num - 1].netdev_lock);
2537fd8aefbSDaniel Jurgens 	ndev = ibdev->roce[port_num - 1].netdev;
254fc24fc5eSAchiad Shochat 	if (ndev)
255fc24fc5eSAchiad Shochat 		dev_hold(ndev);
2567fd8aefbSDaniel Jurgens 	read_unlock(&ibdev->roce[port_num - 1].netdev_lock);
257fc24fc5eSAchiad Shochat 
25832f69e4bSDaniel Jurgens out:
25932f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
260fc24fc5eSAchiad Shochat 	return ndev;
261fc24fc5eSAchiad Shochat }
262fc24fc5eSAchiad Shochat 
26332f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
26432f69e4bSDaniel Jurgens 						   u8 ib_port_num,
26532f69e4bSDaniel Jurgens 						   u8 *native_port_num)
26632f69e4bSDaniel Jurgens {
26732f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
26832f69e4bSDaniel Jurgens 							  ib_port_num);
26932f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
27032f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
27132f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
27232f69e4bSDaniel Jurgens 
273210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
274210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
275210b1f78SMark Bloch 		if (native_port_num)
276210b1f78SMark Bloch 			*native_port_num = ib_port_num;
277210b1f78SMark Bloch 		return ibdev->mdev;
278210b1f78SMark Bloch 	}
279210b1f78SMark Bloch 
28032f69e4bSDaniel Jurgens 	if (native_port_num)
28132f69e4bSDaniel Jurgens 		*native_port_num = 1;
28232f69e4bSDaniel Jurgens 
28332f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
28432f69e4bSDaniel Jurgens 	if (!port)
28532f69e4bSDaniel Jurgens 		return NULL;
28632f69e4bSDaniel Jurgens 
28732f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
28832f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
28932f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
29032f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
29132f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
29232f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
29332f69e4bSDaniel Jurgens 		 */
29432f69e4bSDaniel Jurgens 		if (!mpi->is_master)
29532f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
29632f69e4bSDaniel Jurgens 	}
29732f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
29832f69e4bSDaniel Jurgens 
29932f69e4bSDaniel Jurgens 	return mdev;
30032f69e4bSDaniel Jurgens }
30132f69e4bSDaniel Jurgens 
30232f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
30332f69e4bSDaniel Jurgens {
30432f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
30532f69e4bSDaniel Jurgens 							  port_num);
30632f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
30732f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
30832f69e4bSDaniel Jurgens 
30932f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
31032f69e4bSDaniel Jurgens 		return;
31132f69e4bSDaniel Jurgens 
31232f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
31332f69e4bSDaniel Jurgens 
31432f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
31532f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
31632f69e4bSDaniel Jurgens 	if (mpi->is_master)
31732f69e4bSDaniel Jurgens 		goto out;
31832f69e4bSDaniel Jurgens 
31932f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
32032f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
32132f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
32232f69e4bSDaniel Jurgens out:
32332f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
32432f69e4bSDaniel Jurgens }
32532f69e4bSDaniel Jurgens 
326f1b65df5SNoa Osherovich static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
327f1b65df5SNoa Osherovich 				    u8 *active_width)
328f1b65df5SNoa Osherovich {
329f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
330f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
331f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
332f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
333f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
334f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
335f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
336f1b65df5SNoa Osherovich 		break;
337f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
338f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
339f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
340f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
341f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
342f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
343f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
344f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
345f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
346f1b65df5SNoa Osherovich 		break;
347f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
348f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
349f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
350f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
351f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
352f1b65df5SNoa Osherovich 		break;
353f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
354f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
355f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
356f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
357f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
358f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
359f1b65df5SNoa Osherovich 		break;
360f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
361f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
362f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
363f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
364f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
365f1b65df5SNoa Osherovich 		break;
366f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
367f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
368f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
369f1b65df5SNoa Osherovich 		break;
370f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
371f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
372f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
373f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
374f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
375f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
376f1b65df5SNoa Osherovich 		break;
377f1b65df5SNoa Osherovich 	default:
378f1b65df5SNoa Osherovich 		return -EINVAL;
379f1b65df5SNoa Osherovich 	}
380f1b65df5SNoa Osherovich 
381f1b65df5SNoa Osherovich 	return 0;
382f1b65df5SNoa Osherovich }
383f1b65df5SNoa Osherovich 
384095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
3853f89a643SAchiad Shochat 				struct ib_port_attr *props)
3863f89a643SAchiad Shochat {
3873f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
388da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
38988621dfeSAviv Heller 	struct net_device *ndev, *upper;
3903f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
391b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
392c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
393f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
394b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
395095b0927SIlan Tayari 	int err;
3963f89a643SAchiad Shochat 
397b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
398b3cbd6f0SDaniel Jurgens 	if (!mdev) {
399b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
400b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
401b3cbd6f0SDaniel Jurgens 		 */
402b3cbd6f0SDaniel Jurgens 		put_mdev = false;
403b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
404b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
405b3cbd6f0SDaniel Jurgens 		port_num = 1;
406b3cbd6f0SDaniel Jurgens 	}
407b3cbd6f0SDaniel Jurgens 
408f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
409f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
41050f22fd8SNoa Osherovich 	 */
411b3cbd6f0SDaniel Jurgens 	err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper,
412b3cbd6f0SDaniel Jurgens 					     mdev_port_num);
413095b0927SIlan Tayari 	if (err)
414b3cbd6f0SDaniel Jurgens 		goto out;
415f1b65df5SNoa Osherovich 
4167672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
4177672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
4187672ed33SHonggang Li 
419f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
420f1b65df5SNoa Osherovich 				 &props->active_width);
4213f89a643SAchiad Shochat 
4223f89a643SAchiad Shochat 	props->port_cap_flags |= IB_PORT_CM_SUP;
4232f944c0fSJason Gunthorpe 	props->ip_gids = true;
4243f89a643SAchiad Shochat 
4253f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
4263f89a643SAchiad Shochat 						roce_address_table_size);
4273f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
4283f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
4293f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
4303f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
4313f89a643SAchiad Shochat 	props->phys_state       = 3;
4323f89a643SAchiad Shochat 
433b3cbd6f0SDaniel Jurgens 	mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
434c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
4353f89a643SAchiad Shochat 
436b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
437b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
438b3cbd6f0SDaniel Jurgens 		goto out;
439b3cbd6f0SDaniel Jurgens 
4403f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
4413f89a643SAchiad Shochat 	if (!ndev)
442b3cbd6f0SDaniel Jurgens 		goto out;
4433f89a643SAchiad Shochat 
44488621dfeSAviv Heller 	if (mlx5_lag_is_active(dev->mdev)) {
44588621dfeSAviv Heller 		rcu_read_lock();
44688621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
44788621dfeSAviv Heller 		if (upper) {
44888621dfeSAviv Heller 			dev_put(ndev);
44988621dfeSAviv Heller 			ndev = upper;
45088621dfeSAviv Heller 			dev_hold(ndev);
45188621dfeSAviv Heller 		}
45288621dfeSAviv Heller 		rcu_read_unlock();
45388621dfeSAviv Heller 	}
45488621dfeSAviv Heller 
4553f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
4563f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
4573f89a643SAchiad Shochat 		props->phys_state = 5;
4583f89a643SAchiad Shochat 	}
4593f89a643SAchiad Shochat 
4603f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
4613f89a643SAchiad Shochat 
4623f89a643SAchiad Shochat 	dev_put(ndev);
4633f89a643SAchiad Shochat 
4643f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
465b3cbd6f0SDaniel Jurgens out:
466b3cbd6f0SDaniel Jurgens 	if (put_mdev)
467b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
468b3cbd6f0SDaniel Jurgens 	return err;
4693f89a643SAchiad Shochat }
4703f89a643SAchiad Shochat 
471095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
472095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
473095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
4743cca2606SAchiad Shochat {
475095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
476095b0927SIlan Tayari 	u8 roce_version = 0;
477095b0927SIlan Tayari 	u8 roce_l3_type = 0;
478095b0927SIlan Tayari 	bool vlan = false;
479095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
480095b0927SIlan Tayari 	u16 vlan_id = 0;
4813cca2606SAchiad Shochat 
482095b0927SIlan Tayari 	if (gid) {
483095b0927SIlan Tayari 		gid_type = attr->gid_type;
484095b0927SIlan Tayari 		ether_addr_copy(mac, attr->ndev->dev_addr);
4853cca2606SAchiad Shochat 
4863cca2606SAchiad Shochat 		if (is_vlan_dev(attr->ndev)) {
487095b0927SIlan Tayari 			vlan = true;
488095b0927SIlan Tayari 			vlan_id = vlan_dev_vlan_id(attr->ndev);
489095b0927SIlan Tayari 		}
4903cca2606SAchiad Shochat 	}
4913cca2606SAchiad Shochat 
492095b0927SIlan Tayari 	switch (gid_type) {
4933cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
494095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
4953cca2606SAchiad Shochat 		break;
4963cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
497095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
498095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
499095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
500095b0927SIlan Tayari 		else
501095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
5023cca2606SAchiad Shochat 		break;
5033cca2606SAchiad Shochat 
5043cca2606SAchiad Shochat 	default:
505095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
5063cca2606SAchiad Shochat 	}
5073cca2606SAchiad Shochat 
508095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
509095b0927SIlan Tayari 				      roce_l3_type, gid->raw, mac, vlan,
510cfe4e37fSDaniel Jurgens 				      vlan_id, port_num);
5113cca2606SAchiad Shochat }
5123cca2606SAchiad Shochat 
513f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
5143cca2606SAchiad Shochat 			   __always_unused void **context)
5153cca2606SAchiad Shochat {
516414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
517f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
5183cca2606SAchiad Shochat }
5193cca2606SAchiad Shochat 
520414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
521414448d2SParav Pandit 			   __always_unused void **context)
5223cca2606SAchiad Shochat {
523414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
524414448d2SParav Pandit 			     attr->index, NULL, NULL);
5253cca2606SAchiad Shochat }
5263cca2606SAchiad Shochat 
52747ec3866SParav Pandit __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
52847ec3866SParav Pandit 			       const struct ib_gid_attr *attr)
5292811ba51SAchiad Shochat {
53047ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
5312811ba51SAchiad Shochat 		return 0;
5322811ba51SAchiad Shochat 
5332811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
5342811ba51SAchiad Shochat }
5352811ba51SAchiad Shochat 
5361b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
5371b5daf11SMajd Dibbiny {
5387fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
539d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
5407fae6655SNoa Osherovich 	return 0;
5411b5daf11SMajd Dibbiny }
5421b5daf11SMajd Dibbiny 
5431b5daf11SMajd Dibbiny enum {
5441b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
5451b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
5461b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
5471b5daf11SMajd Dibbiny };
5481b5daf11SMajd Dibbiny 
5491b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
5501b5daf11SMajd Dibbiny {
5511b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
5521b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
5531b5daf11SMajd Dibbiny 
554ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
5551b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
5561b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
5571b5daf11SMajd Dibbiny 
5581b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
5591b5daf11SMajd Dibbiny }
5601b5daf11SMajd Dibbiny 
561da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
562776a3906SMoni Shoua 			    u8 atomic_size_qp,
563da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
564da7525d2SEran Ben Elisha {
565da7525d2SEran Ben Elisha 	u8 tmp;
566da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
567da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
568bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
569da7525d2SEran Ben Elisha 
570da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
571da7525d2SEran Ben Elisha 	 * of host endianness respond
572da7525d2SEran Ben Elisha 	 */
573da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
574da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
575da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
576da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
577da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
578da7525d2SEran Ben Elisha 	} else {
579da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
580da7525d2SEran Ben Elisha 	}
581da7525d2SEran Ben Elisha }
582da7525d2SEran Ben Elisha 
583776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
584776a3906SMoni Shoua 			       struct ib_device_attr *props)
585776a3906SMoni Shoua {
586776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
587776a3906SMoni Shoua 
588776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
589776a3906SMoni Shoua }
590776a3906SMoni Shoua 
591776a3906SMoni Shoua static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
592776a3906SMoni Shoua 			       struct ib_device_attr *props)
593776a3906SMoni Shoua {
594776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
595776a3906SMoni Shoua 
596776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
597776a3906SMoni Shoua }
598776a3906SMoni Shoua 
599776a3906SMoni Shoua bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
600776a3906SMoni Shoua {
601776a3906SMoni Shoua 	struct ib_device_attr props = {};
602776a3906SMoni Shoua 
603776a3906SMoni Shoua 	get_atomic_caps_dc(dev, &props);
604776a3906SMoni Shoua 	return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
605776a3906SMoni Shoua }
6061b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
6071b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
6081b5daf11SMajd Dibbiny {
6091b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6101b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6111b5daf11SMajd Dibbiny 	u64 tmp;
6121b5daf11SMajd Dibbiny 	int err;
6131b5daf11SMajd Dibbiny 
6141b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6151b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6161b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
6171b5daf11SMajd Dibbiny 							    sys_image_guid);
6181b5daf11SMajd Dibbiny 
6191b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6201b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
6213f89a643SAchiad Shochat 		break;
6223f89a643SAchiad Shochat 
6233f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6243f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
6253f89a643SAchiad Shochat 		break;
6261b5daf11SMajd Dibbiny 
6271b5daf11SMajd Dibbiny 	default:
6281b5daf11SMajd Dibbiny 		return -EINVAL;
6291b5daf11SMajd Dibbiny 	}
6303f89a643SAchiad Shochat 
6313f89a643SAchiad Shochat 	if (!err)
6323f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
6333f89a643SAchiad Shochat 
6343f89a643SAchiad Shochat 	return err;
6353f89a643SAchiad Shochat 
6361b5daf11SMajd Dibbiny }
6371b5daf11SMajd Dibbiny 
6381b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
6391b5daf11SMajd Dibbiny 				u16 *max_pkeys)
6401b5daf11SMajd Dibbiny {
6411b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6421b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6431b5daf11SMajd Dibbiny 
6441b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6451b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6461b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
6471b5daf11SMajd Dibbiny 
6481b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6491b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6501b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
6511b5daf11SMajd Dibbiny 						pkey_table_size));
6521b5daf11SMajd Dibbiny 		return 0;
6531b5daf11SMajd Dibbiny 
6541b5daf11SMajd Dibbiny 	default:
6551b5daf11SMajd Dibbiny 		return -EINVAL;
6561b5daf11SMajd Dibbiny 	}
6571b5daf11SMajd Dibbiny }
6581b5daf11SMajd Dibbiny 
6591b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
6601b5daf11SMajd Dibbiny 				u32 *vendor_id)
6611b5daf11SMajd Dibbiny {
6621b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6631b5daf11SMajd Dibbiny 
6641b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6651b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6661b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
6671b5daf11SMajd Dibbiny 
6681b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6691b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6701b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
6711b5daf11SMajd Dibbiny 
6721b5daf11SMajd Dibbiny 	default:
6731b5daf11SMajd Dibbiny 		return -EINVAL;
6741b5daf11SMajd Dibbiny 	}
6751b5daf11SMajd Dibbiny }
6761b5daf11SMajd Dibbiny 
6771b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
6781b5daf11SMajd Dibbiny 				__be64 *node_guid)
6791b5daf11SMajd Dibbiny {
6801b5daf11SMajd Dibbiny 	u64 tmp;
6811b5daf11SMajd Dibbiny 	int err;
6821b5daf11SMajd Dibbiny 
6831b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
6841b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6851b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
6861b5daf11SMajd Dibbiny 
6871b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6881b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
6893f89a643SAchiad Shochat 		break;
6903f89a643SAchiad Shochat 
6913f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6923f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
6933f89a643SAchiad Shochat 		break;
6941b5daf11SMajd Dibbiny 
6951b5daf11SMajd Dibbiny 	default:
6961b5daf11SMajd Dibbiny 		return -EINVAL;
6971b5daf11SMajd Dibbiny 	}
6983f89a643SAchiad Shochat 
6993f89a643SAchiad Shochat 	if (!err)
7003f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7013f89a643SAchiad Shochat 
7023f89a643SAchiad Shochat 	return err;
7031b5daf11SMajd Dibbiny }
7041b5daf11SMajd Dibbiny 
7051b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
706bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
7071b5daf11SMajd Dibbiny };
7081b5daf11SMajd Dibbiny 
7091b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
7101b5daf11SMajd Dibbiny {
7111b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
7121b5daf11SMajd Dibbiny 
7131b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
7141b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
7151b5daf11SMajd Dibbiny 
7161b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
7171b5daf11SMajd Dibbiny 
7181b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
7191b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
7201b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
7211b5daf11SMajd Dibbiny }
7221b5daf11SMajd Dibbiny 
723e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
7242528e33eSMatan Barak 				struct ib_device_attr *props,
7252528e33eSMatan Barak 				struct ib_udata *uhw)
726e126ba97SEli Cohen {
727e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
728938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
729e126ba97SEli Cohen 	int err = -ENOMEM;
730288c01b7SEli Cohen 	int max_sq_desc;
731e126ba97SEli Cohen 	int max_rq_sg;
732e126ba97SEli Cohen 	int max_sq_sg;
733e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
73485c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
735402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
736402ca536SBodong Wang 	size_t resp_len;
737402ca536SBodong Wang 	u64 max_tso;
738e126ba97SEli Cohen 
739402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
740402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
741402ca536SBodong Wang 		return -EINVAL;
742402ca536SBodong Wang 	else
743402ca536SBodong Wang 		resp.response_length = resp_len;
744402ca536SBodong Wang 
745402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
7462528e33eSMatan Barak 		return -EINVAL;
7472528e33eSMatan Barak 
748e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
7491b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
7501b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
7511b5daf11SMajd Dibbiny 	if (err)
7521b5daf11SMajd Dibbiny 		return err;
7531b5daf11SMajd Dibbiny 
7541b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
7551b5daf11SMajd Dibbiny 	if (err)
7561b5daf11SMajd Dibbiny 		return err;
7571b5daf11SMajd Dibbiny 
7581b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
7591b5daf11SMajd Dibbiny 	if (err)
7601b5daf11SMajd Dibbiny 		return err;
761e126ba97SEli Cohen 
7629603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
7639603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
7649603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
765e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
766e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
767e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
7681a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
769938fe83cSSaeed Mahameed 
770938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
771e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
772938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
773e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
774938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
775e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
776938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
777e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
778d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
779d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
780d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
781d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
782b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
783b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
784d2370e0aSMatan Barak 	}
785e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
786938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
7872dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
7882dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
7892dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
7902dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
7912dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
7922dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
7932dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
7942dea9094SSagi Grimberg 	}
795938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
796f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
797e126ba97SEli Cohen 
79885c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
799e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
800e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
80188115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
802e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
803e8161334SNoa Osherovich 		}
804e8161334SNoa Osherovich 
805e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
806e8161334SNoa Osherovich 			props->raw_packet_caps |=
807e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
80888115fe7SBodong Wang 
809402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
810402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
811402ca536SBodong Wang 			if (max_tso) {
812402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
813402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
814402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
815402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
816402ca536SBodong Wang 			}
817402ca536SBodong Wang 		}
81831f69a82SYishai Hadas 
81931f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
82031f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
82131f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
82231f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
82331f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
82431f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
82531f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
82631f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
82731f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
82831f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
82931f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
8304e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
8314e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
8322d93fc85SMatan Barak 			if (mlx5_accel_ipsec_device_caps(dev->mdev) &
8332d93fc85SMatan Barak 			    MLX5_ACCEL_IPSEC_CAP_DEVICE)
8342d93fc85SMatan Barak 				resp.rss_caps.rx_hash_fields_mask |=
8352d93fc85SMatan Barak 					MLX5_RX_HASH_IPSEC_SPI;
83631f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
83731f69a82SYishai Hadas 		}
83831f69a82SYishai Hadas 	} else {
83931f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
84031f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
84131f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
84231f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
843402ca536SBodong Wang 	}
844402ca536SBodong Wang 
845f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
846f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
847f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
848f0313965SErez Shitrit 	}
849f0313965SErez Shitrit 
85003404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
85185c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
85285c7c014SDaniel Jurgens 	    raw_support)
85303404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
85403404e8aSMaor Gottlieb 
8551d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
8561d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
8571d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
8581d54f890SYishai Hadas 
859cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
86085c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
86185c7c014SDaniel Jurgens 	    raw_support) {
862e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
863cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
864e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
865e8161334SNoa Osherovich 	}
866cff5a0f3SMajd Dibbiny 
86724da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
86824da0016SAriel Levkovich 		props->max_dm_size =
86924da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
87024da0016SAriel Levkovich 	}
87124da0016SAriel Levkovich 
872da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
873da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
874da6d6ba3SMaor Gottlieb 
875b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
876b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
877b1383aa6SNoa Osherovich 
8781b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
8791b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
880e126ba97SEli Cohen 
881e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
882e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
883938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
884938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
885938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
886938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
887288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
888288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
889288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
890e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
89133023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
89233023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
893986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
894938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
8959f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
896938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
897938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
898938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
899938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
900938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
901938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
902938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
903e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
904e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
905911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
906911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
907776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
90881bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
909938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
910938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
911e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
912e126ba97SEli Cohen 					   props->max_mcast_grp;
913e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
91486695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
9157c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
9167c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
917e126ba97SEli Cohen 
9188cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
919938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
9208cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
9218cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
9228cdd312cSHaggai Eran #endif
9238cdd312cSHaggai Eran 
924051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
925051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
926051f2630SLeon Romanovsky 
927eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
928eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
929eff901d3SEli Cohen 
93031f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
93185c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
93231f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
93331f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
93431f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
93531f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
93631f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
93731f69a82SYishai Hadas 		props->max_wq_type_rq =
93831f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
93931f69a82SYishai Hadas 	}
94031f69a82SYishai Hadas 
941eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
94278b1beb0SLeon Romanovsky 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
94378b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
944eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
94578b1beb0SLeon Romanovsky 		props->tm_caps.flags = IB_TM_CAP_RC;
94678b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
947eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
94878b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
949eb761894SArtemy Kovalyov 	}
950eb761894SArtemy Kovalyov 
95187ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
95287ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
95387ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
95487ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
95587ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
95687ab3f52SYonatan Cohen 	}
95787ab3f52SYonatan Cohen 
9587e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
959572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
960572f46bfSYonatan Cohen 
961572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
9627e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
963572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
964572f46bfSYonatan Cohen 					     cqe_compression_max_num);
965572f46bfSYonatan Cohen 
9667e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
9677e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
9687e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
9696f1006a4SYonatan Cohen 
9706f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
9716f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
9726f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
973572f46bfSYonatan Cohen 		}
9747e43a2a5SBodong Wang 	}
9757e43a2a5SBodong Wang 
97685c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
97785c7c014SDaniel Jurgens 	    raw_support) {
978d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
979d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
980d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
981d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
982d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
983d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
984d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
985d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
98661147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
98761147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
98861147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
98961147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
990d949167dSBodong Wang 		}
991d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
992d949167dSBodong Wang 	}
993d949167dSBodong Wang 
9949f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
9959f885201SLeon Romanovsky 			uhw->outlen)) {
996795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
9979f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
998795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
999050da902SBodong Wang 
1000050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1001050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1002050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1003050da902SBodong Wang 
10049f885201SLeon Romanovsky 		resp.response_length +=
10059f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
10069f885201SLeon Romanovsky 	}
10079f885201SLeon Romanovsky 
1008de57f2adSGuy Levi 	if (field_avail(typeof(resp), flags, uhw->outlen)) {
1009de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
10107a0c8f42SGuy Levi 
1011de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1012de57f2adSGuy Levi 			resp.flags |=
1013de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
10147a0c8f42SGuy Levi 
10157a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
10167a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
1017de57f2adSGuy Levi 	}
10189f885201SLeon Romanovsky 
101996dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
102096dc3fc5SNoa Osherovich 			uhw->outlen)) {
102196dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
102296dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
102396dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
102496dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
102596dc3fc5SNoa Osherovich 
102696dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
102796dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
102896dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
102996dc3fc5SNoa Osherovich 
103096dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
103196dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
103296dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
103396dc3fc5SNoa Osherovich 
103496dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
103596dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
103696dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
103796dc3fc5SNoa Osherovich 		}
103896dc3fc5SNoa Osherovich 	}
103996dc3fc5SNoa Osherovich 
104085c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
104185c7c014SDaniel Jurgens 	    raw_support) {
1042b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1043b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1044b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1045b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1046b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1047b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1048b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1049b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1050b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1051b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1052b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1053b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1054b4f34597SNoa Osherovich 		}
1055b4f34597SNoa Osherovich 	}
1056b4f34597SNoa Osherovich 
1057f95ef6cbSMaor Gottlieb 	if (field_avail(typeof(resp), tunnel_offloads_caps,
1058f95ef6cbSMaor Gottlieb 			uhw->outlen)) {
1059f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1060f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1061f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1062f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1063f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1064f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1065f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1066f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1067f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1068f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
1069e818e255SAriel Levkovich 		if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1070e818e255SAriel Levkovich 		    MLX5_FLEX_PROTO_CW_MPLS_GRE)
1071e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1072e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1073e818e255SAriel Levkovich 		if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1074e818e255SAriel Levkovich 		    MLX5_FLEX_PROTO_CW_MPLS_UDP)
1075e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1076e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1077f95ef6cbSMaor Gottlieb 	}
1078f95ef6cbSMaor Gottlieb 
1079402ca536SBodong Wang 	if (uhw->outlen) {
1080402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1081402ca536SBodong Wang 
1082402ca536SBodong Wang 		if (err)
1083402ca536SBodong Wang 			return err;
1084402ca536SBodong Wang 	}
1085402ca536SBodong Wang 
10861b5daf11SMajd Dibbiny 	return 0;
10871b5daf11SMajd Dibbiny }
1088e126ba97SEli Cohen 
10891b5daf11SMajd Dibbiny enum mlx5_ib_width {
10901b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
10911b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
10921b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
10931b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
10941b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
10951b5daf11SMajd Dibbiny };
10961b5daf11SMajd Dibbiny 
10971b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
10981b5daf11SMajd Dibbiny 				  u8 *ib_width)
10991b5daf11SMajd Dibbiny {
11001b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11011b5daf11SMajd Dibbiny 	int err = 0;
11021b5daf11SMajd Dibbiny 
11031b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
11041b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
11051b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
11061b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
11071b5daf11SMajd Dibbiny 			    (int)active_width);
11081b5daf11SMajd Dibbiny 		err = -EINVAL;
11091b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
11101b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
11111b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
11121b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
11131b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
11141b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
11151b5daf11SMajd Dibbiny 	} else {
11161b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
11171b5daf11SMajd Dibbiny 			    (int)active_width);
11181b5daf11SMajd Dibbiny 		err = -EINVAL;
11191b5daf11SMajd Dibbiny 	}
11201b5daf11SMajd Dibbiny 
11211b5daf11SMajd Dibbiny 	return err;
11221b5daf11SMajd Dibbiny }
11231b5daf11SMajd Dibbiny 
11241b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
11251b5daf11SMajd Dibbiny {
11261b5daf11SMajd Dibbiny 	switch (mtu) {
11271b5daf11SMajd Dibbiny 	case 256: return 1;
11281b5daf11SMajd Dibbiny 	case 512: return 2;
11291b5daf11SMajd Dibbiny 	case 1024: return 3;
11301b5daf11SMajd Dibbiny 	case 2048: return 4;
11311b5daf11SMajd Dibbiny 	case 4096: return 5;
11321b5daf11SMajd Dibbiny 	default:
11331b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
11341b5daf11SMajd Dibbiny 		return -1;
11351b5daf11SMajd Dibbiny 	}
11361b5daf11SMajd Dibbiny }
11371b5daf11SMajd Dibbiny 
11381b5daf11SMajd Dibbiny enum ib_max_vl_num {
11391b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
11401b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
11411b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
11421b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
11431b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
11441b5daf11SMajd Dibbiny };
11451b5daf11SMajd Dibbiny 
11461b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
11471b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
11481b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
11491b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
11501b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
11511b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
11521b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
11531b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
11541b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
11551b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
11561b5daf11SMajd Dibbiny };
11571b5daf11SMajd Dibbiny 
11581b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
11591b5daf11SMajd Dibbiny 				u8 *max_vl_num)
11601b5daf11SMajd Dibbiny {
11611b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
11621b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
11631b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
11641b5daf11SMajd Dibbiny 		break;
11651b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
11661b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
11671b5daf11SMajd Dibbiny 		break;
11681b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
11691b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
11701b5daf11SMajd Dibbiny 		break;
11711b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
11721b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
11731b5daf11SMajd Dibbiny 		break;
11741b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
11751b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
11761b5daf11SMajd Dibbiny 		break;
11771b5daf11SMajd Dibbiny 
11781b5daf11SMajd Dibbiny 	default:
11791b5daf11SMajd Dibbiny 		return -EINVAL;
11801b5daf11SMajd Dibbiny 	}
11811b5daf11SMajd Dibbiny 
11821b5daf11SMajd Dibbiny 	return 0;
11831b5daf11SMajd Dibbiny }
11841b5daf11SMajd Dibbiny 
11851b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
11861b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
11871b5daf11SMajd Dibbiny {
11881b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11891b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
11901b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1191046339eaSSaeed Mahameed 	u16 max_mtu;
1192046339eaSSaeed Mahameed 	u16 oper_mtu;
11931b5daf11SMajd Dibbiny 	int err;
11941b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
11951b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
11961b5daf11SMajd Dibbiny 
11971b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
11981b5daf11SMajd Dibbiny 	if (!rep) {
11991b5daf11SMajd Dibbiny 		err = -ENOMEM;
12001b5daf11SMajd Dibbiny 		goto out;
12011b5daf11SMajd Dibbiny 	}
12021b5daf11SMajd Dibbiny 
1203c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
12041b5daf11SMajd Dibbiny 
12051b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
12061b5daf11SMajd Dibbiny 	if (err)
12071b5daf11SMajd Dibbiny 		goto out;
12081b5daf11SMajd Dibbiny 
12091b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
12101b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
12111b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
12121b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
12131b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
12141b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
12151b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
12161b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
12171b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
12181b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
12191b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
12201b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
12211b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
12221b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
12231b5daf11SMajd Dibbiny 
12241b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
12251b5daf11SMajd Dibbiny 	if (err)
12261b5daf11SMajd Dibbiny 		goto out;
12271b5daf11SMajd Dibbiny 
12281b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
12291b5daf11SMajd Dibbiny 				     &props->active_width);
12301b5daf11SMajd Dibbiny 	if (err)
12311b5daf11SMajd Dibbiny 		goto out;
1232d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
12331b5daf11SMajd Dibbiny 	if (err)
12341b5daf11SMajd Dibbiny 		goto out;
12351b5daf11SMajd Dibbiny 
1236facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
12371b5daf11SMajd Dibbiny 
12381b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
12391b5daf11SMajd Dibbiny 
1240facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
12411b5daf11SMajd Dibbiny 
12421b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
12431b5daf11SMajd Dibbiny 
12441b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
12451b5daf11SMajd Dibbiny 	if (err)
12461b5daf11SMajd Dibbiny 		goto out;
12471b5daf11SMajd Dibbiny 
12481b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
12491b5daf11SMajd Dibbiny 				   &props->max_vl_num);
12501b5daf11SMajd Dibbiny out:
12511b5daf11SMajd Dibbiny 	kfree(rep);
1252e126ba97SEli Cohen 	return err;
1253e126ba97SEli Cohen }
1254e126ba97SEli Cohen 
1255e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1256e126ba97SEli Cohen 		       struct ib_port_attr *props)
1257e126ba97SEli Cohen {
1258095b0927SIlan Tayari 	unsigned int count;
1259095b0927SIlan Tayari 	int ret;
1260095b0927SIlan Tayari 
12611b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
12621b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1263095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1264095b0927SIlan Tayari 		break;
1265e126ba97SEli Cohen 
12661b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1267095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1268095b0927SIlan Tayari 		break;
12691b5daf11SMajd Dibbiny 
12703f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1271095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1272095b0927SIlan Tayari 		break;
12733f89a643SAchiad Shochat 
12741b5daf11SMajd Dibbiny 	default:
1275095b0927SIlan Tayari 		ret = -EINVAL;
1276e126ba97SEli Cohen 	}
1277095b0927SIlan Tayari 
1278095b0927SIlan Tayari 	if (!ret && props) {
1279b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1280b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1281b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1282b3cbd6f0SDaniel Jurgens 
1283b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1284b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1285b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1286b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1287b3cbd6f0SDaniel Jurgens 			 */
1288b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1289b3cbd6f0SDaniel Jurgens 			port = 1;
1290b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1291b3cbd6f0SDaniel Jurgens 		}
1292b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1293b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1294b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1295095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1296095b0927SIlan Tayari 	}
1297095b0927SIlan Tayari 	return ret;
1298e126ba97SEli Cohen }
1299e126ba97SEli Cohen 
13008e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
13018e6efa3aSMark Bloch 				  struct ib_port_attr *props)
13028e6efa3aSMark Bloch {
13038e6efa3aSMark Bloch 	int ret;
13048e6efa3aSMark Bloch 
13058e6efa3aSMark Bloch 	/* Only link layer == ethernet is valid for representors */
13068e6efa3aSMark Bloch 	ret = mlx5_query_port_roce(ibdev, port, props);
13078e6efa3aSMark Bloch 	if (ret || !props)
13088e6efa3aSMark Bloch 		return ret;
13098e6efa3aSMark Bloch 
13108e6efa3aSMark Bloch 	/* We don't support GIDS */
13118e6efa3aSMark Bloch 	props->gid_tbl_len = 0;
13128e6efa3aSMark Bloch 
13138e6efa3aSMark Bloch 	return ret;
13148e6efa3aSMark Bloch }
13158e6efa3aSMark Bloch 
1316e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1317e126ba97SEli Cohen 			     union ib_gid *gid)
1318e126ba97SEli Cohen {
13191b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
13201b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1321e126ba97SEli Cohen 
13221b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13231b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
13241b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1325e126ba97SEli Cohen 
13261b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
13271b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1328e126ba97SEli Cohen 
13291b5daf11SMajd Dibbiny 	default:
13301b5daf11SMajd Dibbiny 		return -EINVAL;
13311b5daf11SMajd Dibbiny 	}
1332e126ba97SEli Cohen 
1333e126ba97SEli Cohen }
1334e126ba97SEli Cohen 
1335b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1336b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1337b3cbd6f0SDaniel Jurgens {
1338b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1339b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1340b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1341b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1342b3cbd6f0SDaniel Jurgens 	int err;
1343b3cbd6f0SDaniel Jurgens 
1344b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1345b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1346b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1347b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1348b3cbd6f0SDaniel Jurgens 		 */
1349b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1350b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1351b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1352b3cbd6f0SDaniel Jurgens 	}
1353b3cbd6f0SDaniel Jurgens 
1354b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1355b3cbd6f0SDaniel Jurgens 					index, pkey);
1356b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1357b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1358b3cbd6f0SDaniel Jurgens 
1359b3cbd6f0SDaniel Jurgens 	return err;
1360b3cbd6f0SDaniel Jurgens }
1361b3cbd6f0SDaniel Jurgens 
1362e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1363e126ba97SEli Cohen 			      u16 *pkey)
1364e126ba97SEli Cohen {
13651b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13661b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
13671b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1368e126ba97SEli Cohen 
13691b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
13701b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1371b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
13721b5daf11SMajd Dibbiny 	default:
13731b5daf11SMajd Dibbiny 		return -EINVAL;
1374e126ba97SEli Cohen 	}
13751b5daf11SMajd Dibbiny }
1376e126ba97SEli Cohen 
1377e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1378e126ba97SEli Cohen 				 struct ib_device_modify *props)
1379e126ba97SEli Cohen {
1380e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1381e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1382e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1383e126ba97SEli Cohen 	int err;
1384e126ba97SEli Cohen 
1385e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1386e126ba97SEli Cohen 		return -EOPNOTSUPP;
1387e126ba97SEli Cohen 
1388e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1389e126ba97SEli Cohen 		return 0;
1390e126ba97SEli Cohen 
1391e126ba97SEli Cohen 	/*
1392e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1393e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1394e126ba97SEli Cohen 	 */
1395bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
13969603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1397e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1398e126ba97SEli Cohen 	if (err)
1399e126ba97SEli Cohen 		return err;
1400e126ba97SEli Cohen 
1401bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1402e126ba97SEli Cohen 
1403e126ba97SEli Cohen 	return err;
1404e126ba97SEli Cohen }
1405e126ba97SEli Cohen 
1406cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1407cdbe33d0SEli Cohen 				u32 value)
1408cdbe33d0SEli Cohen {
1409cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1410b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1411b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1412cdbe33d0SEli Cohen 	int err;
1413cdbe33d0SEli Cohen 
1414b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1415b3cbd6f0SDaniel Jurgens 	if (!mdev)
1416b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1417b3cbd6f0SDaniel Jurgens 
1418b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1419cdbe33d0SEli Cohen 	if (err)
1420b3cbd6f0SDaniel Jurgens 		goto out;
1421cdbe33d0SEli Cohen 
1422cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1423cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1424cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1425b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1426b3cbd6f0SDaniel Jurgens 		goto out;
1427cdbe33d0SEli Cohen 	}
1428cdbe33d0SEli Cohen 
1429cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1430cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1431b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1432b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1433b3cbd6f0SDaniel Jurgens 
1434b3cbd6f0SDaniel Jurgens out:
1435b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1436cdbe33d0SEli Cohen 
1437cdbe33d0SEli Cohen 	return err;
1438cdbe33d0SEli Cohen }
1439cdbe33d0SEli Cohen 
1440e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1441e126ba97SEli Cohen 			       struct ib_port_modify *props)
1442e126ba97SEli Cohen {
1443e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1444e126ba97SEli Cohen 	struct ib_port_attr attr;
1445e126ba97SEli Cohen 	u32 tmp;
1446e126ba97SEli Cohen 	int err;
1447cdbe33d0SEli Cohen 	u32 change_mask;
1448cdbe33d0SEli Cohen 	u32 value;
1449cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1450cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1451cdbe33d0SEli Cohen 
1452ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1453ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1454ec255879SMajd Dibbiny 	 */
1455ec255879SMajd Dibbiny 	if (!is_ib)
1456ec255879SMajd Dibbiny 		return 0;
1457ec255879SMajd Dibbiny 
1458cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1459cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1460cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1461cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1462cdbe33d0SEli Cohen 	}
1463e126ba97SEli Cohen 
1464e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1465e126ba97SEli Cohen 
1466c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1467e126ba97SEli Cohen 	if (err)
1468e126ba97SEli Cohen 		goto out;
1469e126ba97SEli Cohen 
1470e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1471e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1472e126ba97SEli Cohen 
14739603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1474e126ba97SEli Cohen 
1475e126ba97SEli Cohen out:
1476e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1477e126ba97SEli Cohen 	return err;
1478e126ba97SEli Cohen }
1479e126ba97SEli Cohen 
148030aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
148130aa60b3SEli Cohen {
148230aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
148330aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
148430aa60b3SEli Cohen }
148530aa60b3SEli Cohen 
148631a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
148731a78a5aSYishai Hadas {
148831a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
148931a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
149031a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
149131a78a5aSYishai Hadas 
149231a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
149331a78a5aSYishai Hadas }
149431a78a5aSYishai Hadas 
1495b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1496b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
149731a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1498b037c29aSEli Cohen {
1499b037c29aSEli Cohen 	int uars_per_sys_page;
1500b037c29aSEli Cohen 	int bfregs_per_sys_page;
1501b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1502b037c29aSEli Cohen 
1503b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1504b037c29aSEli Cohen 		return -EINVAL;
1505b037c29aSEli Cohen 
1506b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1507b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1508b037c29aSEli Cohen 
1509b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1510b037c29aSEli Cohen 		return -ENOMEM;
1511b037c29aSEli Cohen 
1512b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1513b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
151431a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1515b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1516b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1517b037c29aSEli Cohen 		return -EINVAL;
1518b037c29aSEli Cohen 
151931a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
152031a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
152131a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
152231a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
152331a78a5aSYishai Hadas 
152431a78a5aSYishai 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",
1525b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1526b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
152731a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
152831a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1529b037c29aSEli Cohen 
1530b037c29aSEli Cohen 	return 0;
1531b037c29aSEli Cohen }
1532b037c29aSEli Cohen 
1533b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1534b037c29aSEli Cohen {
1535b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1536b037c29aSEli Cohen 	int err;
1537b037c29aSEli Cohen 	int i;
1538b037c29aSEli Cohen 
1539b037c29aSEli Cohen 	bfregi = &context->bfregi;
154031a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1541b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1542b037c29aSEli Cohen 		if (err)
1543b037c29aSEli Cohen 			goto error;
1544b037c29aSEli Cohen 
1545b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1546b037c29aSEli Cohen 	}
15474ed131d0SYishai Hadas 
15484ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
15494ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
15504ed131d0SYishai Hadas 
1551b037c29aSEli Cohen 	return 0;
1552b037c29aSEli Cohen 
1553b037c29aSEli Cohen error:
1554b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1555b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1556b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1557b037c29aSEli Cohen 
1558b037c29aSEli Cohen 	return err;
1559b037c29aSEli Cohen }
1560b037c29aSEli Cohen 
156115177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
156215177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1563b037c29aSEli Cohen {
1564b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1565b037c29aSEli Cohen 	int i;
1566b037c29aSEli Cohen 
1567b037c29aSEli Cohen 	bfregi = &context->bfregi;
156815177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
15694ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
157015177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
157115177999SLeon Romanovsky 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1572b037c29aSEli Cohen }
1573b037c29aSEli Cohen 
1574c85023e1SHuy Nguyen static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1575c85023e1SHuy Nguyen {
1576c85023e1SHuy Nguyen 	int err;
1577c85023e1SHuy Nguyen 
1578cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1579cfdeb893SLeon Romanovsky 		return 0;
1580cfdeb893SLeon Romanovsky 
1581c85023e1SHuy Nguyen 	err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1582c85023e1SHuy Nguyen 	if (err)
1583c85023e1SHuy Nguyen 		return err;
1584c85023e1SHuy Nguyen 
1585c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
15868978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
15878978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1588c85023e1SHuy Nguyen 		return err;
1589c85023e1SHuy Nguyen 
1590c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1591c85023e1SHuy Nguyen 	dev->user_td++;
1592c85023e1SHuy Nguyen 
1593c85023e1SHuy Nguyen 	if (dev->user_td == 2)
1594c85023e1SHuy Nguyen 		err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1595c85023e1SHuy Nguyen 
1596c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1597c85023e1SHuy Nguyen 	return err;
1598c85023e1SHuy Nguyen }
1599c85023e1SHuy Nguyen 
1600c85023e1SHuy Nguyen static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1601c85023e1SHuy Nguyen {
1602cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1603cfdeb893SLeon Romanovsky 		return;
1604cfdeb893SLeon Romanovsky 
1605c85023e1SHuy Nguyen 	mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1606c85023e1SHuy Nguyen 
1607c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
16088978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
16098978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1610c85023e1SHuy Nguyen 		return;
1611c85023e1SHuy Nguyen 
1612c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1613c85023e1SHuy Nguyen 	dev->user_td--;
1614c85023e1SHuy Nguyen 
1615c85023e1SHuy Nguyen 	if (dev->user_td < 2)
1616c85023e1SHuy Nguyen 		mlx5_nic_vport_update_local_lb(dev->mdev, false);
1617c85023e1SHuy Nguyen 
1618c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1619c85023e1SHuy Nguyen }
1620c85023e1SHuy Nguyen 
1621e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1622e126ba97SEli Cohen 						  struct ib_udata *udata)
1623e126ba97SEli Cohen {
1624e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1625b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1626b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
16275c99eaecSFeras Daoud 	struct mlx5_core_dev *mdev = dev->mdev;
1628e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
16292f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
163078c0f98cSEli Cohen 	int ver;
1631e126ba97SEli Cohen 	int err;
1632a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1633a168a41cSMajd Dibbiny 				     max_cqe_version);
163425bb36e7SYonatan Cohen 	u32 dump_fill_mkey;
1635b037c29aSEli Cohen 	bool lib_uar_4k;
1636e126ba97SEli Cohen 
1637e126ba97SEli Cohen 	if (!dev->ib_active)
1638e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1639e126ba97SEli Cohen 
1640e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
164178c0f98cSEli Cohen 		ver = 0;
1642e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
164378c0f98cSEli Cohen 		ver = 2;
164478c0f98cSEli Cohen 	else
164578c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
164678c0f98cSEli Cohen 
1647e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1648e126ba97SEli Cohen 	if (err)
1649e126ba97SEli Cohen 		return ERR_PTR(err);
1650e126ba97SEli Cohen 
1651a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1652a8b92ca1SYishai Hadas 		return ERR_PTR(-EOPNOTSUPP);
165378c0f98cSEli Cohen 
1654f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1655b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1656b368d7cbSMatan Barak 
16572f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
16582f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
16592f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1660e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1661e126ba97SEli Cohen 
1662938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
16632cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1664938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1665b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1666938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1667938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1668938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1669938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1670938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1671f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1672f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1673f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
167430aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
167530aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
167630aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
167730aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1678b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1679b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1680e126ba97SEli Cohen 
1681c03faa56SMatan Barak 	if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1682c03faa56SMatan Barak 		if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1683c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1684c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1685c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1686c03faa56SMatan Barak 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1687c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1688c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1689c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1690c03faa56SMatan Barak 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1691c03faa56SMatan Barak 	}
1692c03faa56SMatan Barak 
1693e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1694e126ba97SEli Cohen 	if (!context)
1695e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1696e126ba97SEli Cohen 
169730aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
16982f5ff264SEli Cohen 	bfregi = &context->bfregi;
1699b037c29aSEli Cohen 
1700b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
170131a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1702b037c29aSEli Cohen 	if (err)
1703b037c29aSEli Cohen 		goto out_ctx;
1704b037c29aSEli Cohen 
17052f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1706b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
170731a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1708b037c29aSEli Cohen 				GFP_KERNEL);
1709b037c29aSEli Cohen 	if (!bfregi->count) {
1710e126ba97SEli Cohen 		err = -ENOMEM;
1711e126ba97SEli Cohen 		goto out_ctx;
1712e126ba97SEli Cohen 	}
1713e126ba97SEli Cohen 
1714b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1715b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1716e126ba97SEli Cohen 				    GFP_KERNEL);
1717b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1718e126ba97SEli Cohen 		err = -ENOMEM;
1719e126ba97SEli Cohen 		goto out_count;
1720e126ba97SEli Cohen 	}
1721e126ba97SEli Cohen 
1722b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1723b037c29aSEli Cohen 	if (err)
1724b037c29aSEli Cohen 		goto out_sys_pages;
1725b037c29aSEli Cohen 
1726b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1727b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1728b4cfe447SHaggai Eran #endif
1729b4cfe447SHaggai Eran 
1730c85023e1SHuy Nguyen 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
1731146d2f1aSmajd@mellanox.com 	if (err)
1732c44ef998SIlya Lesokhin 		goto out_uars;
1733146d2f1aSmajd@mellanox.com 
1734a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1735a8b92ca1SYishai Hadas 		/* Block DEVX on Infiniband as of SELinux */
1736a8b92ca1SYishai Hadas 		if (mlx5_ib_port_link_layer(ibdev, 1) != IB_LINK_LAYER_ETHERNET) {
1737a8b92ca1SYishai Hadas 			err = -EPERM;
1738a8b92ca1SYishai Hadas 			goto out_td;
1739a8b92ca1SYishai Hadas 		}
1740a8b92ca1SYishai Hadas 
1741a8b92ca1SYishai Hadas 		err = mlx5_ib_devx_create(dev, context);
1742a8b92ca1SYishai Hadas 		if (err)
1743a8b92ca1SYishai Hadas 			goto out_td;
1744a8b92ca1SYishai Hadas 	}
1745a8b92ca1SYishai Hadas 
174625bb36e7SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
174725bb36e7SYonatan Cohen 		err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
174825bb36e7SYonatan Cohen 		if (err)
17498193abb6SJason Gunthorpe 			goto out_mdev;
175025bb36e7SYonatan Cohen 	}
175125bb36e7SYonatan Cohen 
17527c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
1753ad9a3668SMajd Dibbiny 	mutex_init(&context->vma_private_list_mutex);
1754e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1755e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1756e126ba97SEli Cohen 
17572f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1758508562d6SDaniel Jurgens 	resp.num_ports = dev->num_ports;
1759b368d7cbSMatan Barak 
1760f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1761f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1762b368d7cbSMatan Barak 
1763402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
17646ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
17656ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1766402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1767402ca536SBodong Wang 	}
1768402ca536SBodong Wang 
176978984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
177078984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
177178984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
177278984898SOr Gerlitz 			resp.eth_min_inline++;
177378984898SOr Gerlitz 		}
177478984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
177578984898SOr Gerlitz 	}
177678984898SOr Gerlitz 
17775c99eaecSFeras Daoud 	if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
17785c99eaecSFeras Daoud 		if (mdev->clock_info)
17795c99eaecSFeras Daoud 			resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
17805c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.clock_info_versions);
17815c99eaecSFeras Daoud 	}
17825c99eaecSFeras Daoud 
1783bc5c6eedSNoa Osherovich 	/*
1784bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1785bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1786bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1787bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1788bc5c6eedSNoa Osherovich 	 */
1789de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1790de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1791b368d7cbSMatan Barak 			resp.comp_mask |=
1792b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1793b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1794de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1795de8d6e02SEli Cohen 		}
17965c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.hca_core_clock_offset);
1797b368d7cbSMatan Barak 	}
1798b368d7cbSMatan Barak 
179930aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
180030aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
180130aa60b3SEli Cohen 
180230aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
180330aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
180430aa60b3SEli Cohen 
180531a78a5aSYishai Hadas 	if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
180631a78a5aSYishai Hadas 		resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
180731a78a5aSYishai Hadas 		resp.response_length += sizeof(resp.num_dyn_bfregs);
180831a78a5aSYishai Hadas 	}
180931a78a5aSYishai Hadas 
181025bb36e7SYonatan Cohen 	if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
181125bb36e7SYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
181225bb36e7SYonatan Cohen 			resp.dump_fill_mkey = dump_fill_mkey;
181325bb36e7SYonatan Cohen 			resp.comp_mask |=
181425bb36e7SYonatan Cohen 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
181525bb36e7SYonatan Cohen 		}
181625bb36e7SYonatan Cohen 		resp.response_length += sizeof(resp.dump_fill_mkey);
181725bb36e7SYonatan Cohen 	}
181825bb36e7SYonatan Cohen 
1819b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1820e126ba97SEli Cohen 	if (err)
1821a8b92ca1SYishai Hadas 		goto out_mdev;
1822e126ba97SEli Cohen 
18232f5ff264SEli Cohen 	bfregi->ver = ver;
18242f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1825f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
182630aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
182730aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1828f72300c5SHaggai Abramovsky 
1829*c6a21c38SMajd Dibbiny 	if (mlx5_lag_is_active(dev->mdev)) {
1830*c6a21c38SMajd Dibbiny 		u8 port = mlx5_core_native_port_num(dev->mdev);
1831*c6a21c38SMajd Dibbiny 
1832*c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1833*c6a21c38SMajd Dibbiny 			   atomic_add_return(
1834*c6a21c38SMajd Dibbiny 				   1, &dev->roce[port].tx_port_affinity));
1835*c6a21c38SMajd Dibbiny 	}
1836*c6a21c38SMajd Dibbiny 
1837e126ba97SEli Cohen 	return &context->ibucontext;
1838e126ba97SEli Cohen 
1839a8b92ca1SYishai Hadas out_mdev:
1840a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
1841a8b92ca1SYishai Hadas 		mlx5_ib_devx_destroy(dev, context);
1842146d2f1aSmajd@mellanox.com out_td:
1843c85023e1SHuy Nguyen 	mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1844146d2f1aSmajd@mellanox.com 
1845e126ba97SEli Cohen out_uars:
1846b037c29aSEli Cohen 	deallocate_uars(dev, context);
1847b037c29aSEli Cohen 
1848b037c29aSEli Cohen out_sys_pages:
1849b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1850b037c29aSEli Cohen 
1851e126ba97SEli Cohen out_count:
18522f5ff264SEli Cohen 	kfree(bfregi->count);
1853e126ba97SEli Cohen 
1854e126ba97SEli Cohen out_ctx:
1855e126ba97SEli Cohen 	kfree(context);
1856b037c29aSEli Cohen 
1857e126ba97SEli Cohen 	return ERR_PTR(err);
1858e126ba97SEli Cohen }
1859e126ba97SEli Cohen 
1860e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1861e126ba97SEli Cohen {
1862e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1863e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1864b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1865e126ba97SEli Cohen 
1866a8b92ca1SYishai Hadas 	if (context->devx_uid)
1867a8b92ca1SYishai Hadas 		mlx5_ib_devx_destroy(dev, context);
1868a8b92ca1SYishai Hadas 
1869b037c29aSEli Cohen 	bfregi = &context->bfregi;
1870c85023e1SHuy Nguyen 	mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1871146d2f1aSmajd@mellanox.com 
1872b037c29aSEli Cohen 	deallocate_uars(dev, context);
1873b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
18742f5ff264SEli Cohen 	kfree(bfregi->count);
1875e126ba97SEli Cohen 	kfree(context);
1876e126ba97SEli Cohen 
1877e126ba97SEli Cohen 	return 0;
1878e126ba97SEli Cohen }
1879e126ba97SEli Cohen 
1880b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
18814ed131d0SYishai Hadas 				 int uar_idx)
1882e126ba97SEli Cohen {
1883b037c29aSEli Cohen 	int fw_uars_per_page;
1884b037c29aSEli Cohen 
1885b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1886b037c29aSEli Cohen 
18874ed131d0SYishai Hadas 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
1888e126ba97SEli Cohen }
1889e126ba97SEli Cohen 
1890e126ba97SEli Cohen static int get_command(unsigned long offset)
1891e126ba97SEli Cohen {
1892e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1893e126ba97SEli Cohen }
1894e126ba97SEli Cohen 
1895e126ba97SEli Cohen static int get_arg(unsigned long offset)
1896e126ba97SEli Cohen {
1897e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1898e126ba97SEli Cohen }
1899e126ba97SEli Cohen 
1900e126ba97SEli Cohen static int get_index(unsigned long offset)
1901e126ba97SEli Cohen {
1902e126ba97SEli Cohen 	return get_arg(offset);
1903e126ba97SEli Cohen }
1904e126ba97SEli Cohen 
19054ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
19064ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
19074ed131d0SYishai Hadas {
19084ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
19094ed131d0SYishai Hadas }
19104ed131d0SYishai Hadas 
19117c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
19127c2344c3SMaor Gottlieb {
19137c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
19147c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
19157c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
19167c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
19177c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
19187c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
19197c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
19207c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
19217c2344c3SMaor Gottlieb 	 */
19227c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
19237c2344c3SMaor Gottlieb }
19247c2344c3SMaor Gottlieb 
19257c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
19267c2344c3SMaor Gottlieb {
19277c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
19287c2344c3SMaor Gottlieb 
19297c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
19307c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
19317c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
19327c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
19337c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
19347c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
19357c2344c3SMaor Gottlieb 	 * process is exiting.
19367c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
19377c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
19387c2344c3SMaor Gottlieb 	 */
19397c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
19407c2344c3SMaor Gottlieb 
19417c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
19427c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
19437c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
19447c2344c3SMaor Gottlieb 	 */
19457c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
1946ad9a3668SMajd Dibbiny 	mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
19477c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
1948ad9a3668SMajd Dibbiny 	mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
19497c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
19507c2344c3SMaor Gottlieb }
19517c2344c3SMaor Gottlieb 
19527c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
19537c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
19547c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
19557c2344c3SMaor Gottlieb };
19567c2344c3SMaor Gottlieb 
19577c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
19587c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
19597c2344c3SMaor Gottlieb {
19607c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
19617c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
19627c2344c3SMaor Gottlieb 
19637c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
19647c2344c3SMaor Gottlieb 	if (!vma_prv)
19657c2344c3SMaor Gottlieb 		return -ENOMEM;
19667c2344c3SMaor Gottlieb 
19677c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
1968ad9a3668SMajd Dibbiny 	vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
19697c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
19707c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
19717c2344c3SMaor Gottlieb 
1972ad9a3668SMajd Dibbiny 	mutex_lock(&ctx->vma_private_list_mutex);
19737c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
1974ad9a3668SMajd Dibbiny 	mutex_unlock(&ctx->vma_private_list_mutex);
19757c2344c3SMaor Gottlieb 
19767c2344c3SMaor Gottlieb 	return 0;
19777c2344c3SMaor Gottlieb }
19787c2344c3SMaor Gottlieb 
19797c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
19807c2344c3SMaor Gottlieb {
19817c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
19827c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
19837c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
19847c2344c3SMaor Gottlieb 
1985ad9a3668SMajd Dibbiny 	mutex_lock(&context->vma_private_list_mutex);
19867c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
19877c2344c3SMaor Gottlieb 				 list) {
19887c2344c3SMaor Gottlieb 		vma = vma_private->vma;
19892cb40791SLeon Romanovsky 		zap_vma_ptes(vma, vma->vm_start, PAGE_SIZE);
19907c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
19917c2344c3SMaor Gottlieb 		 * not access ops any more.
19927c2344c3SMaor Gottlieb 		 */
199313776612SMaor Gottlieb 		vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
19947c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
19957c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
19967c2344c3SMaor Gottlieb 		kfree(vma_private);
19977c2344c3SMaor Gottlieb 	}
1998ad9a3668SMajd Dibbiny 	mutex_unlock(&context->vma_private_list_mutex);
19997c2344c3SMaor Gottlieb }
20007c2344c3SMaor Gottlieb 
200137aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2002e126ba97SEli Cohen {
200337aa5c36SGuy Levi 	switch (cmd) {
200437aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
200537aa5c36SGuy Levi 		return "WC";
2006e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
200737aa5c36SGuy Levi 		return "best effort WC";
200837aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
200937aa5c36SGuy Levi 		return "NC";
201024da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
201124da0016SAriel Levkovich 		return "Device Memory";
201237aa5c36SGuy Levi 	default:
201337aa5c36SGuy Levi 		return NULL;
201437aa5c36SGuy Levi 	}
201537aa5c36SGuy Levi }
201637aa5c36SGuy Levi 
20175c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20185c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20195c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20205c99eaecSFeras Daoud {
20215c99eaecSFeras Daoud 	phys_addr_t pfn;
20225c99eaecSFeras Daoud 	int err;
20235c99eaecSFeras Daoud 
20245c99eaecSFeras Daoud 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
20255c99eaecSFeras Daoud 		return -EINVAL;
20265c99eaecSFeras Daoud 
20275c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20285c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20295c99eaecSFeras Daoud 
20305c99eaecSFeras Daoud 	if (vma->vm_flags & VM_WRITE)
20315c99eaecSFeras Daoud 		return -EPERM;
20325c99eaecSFeras Daoud 
20335c99eaecSFeras Daoud 	if (!dev->mdev->clock_info_page)
20345c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20355c99eaecSFeras Daoud 
20365c99eaecSFeras Daoud 	pfn = page_to_pfn(dev->mdev->clock_info_page);
20375c99eaecSFeras Daoud 	err = remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE,
20385c99eaecSFeras Daoud 			      vma->vm_page_prot);
20395c99eaecSFeras Daoud 	if (err)
20405c99eaecSFeras Daoud 		return err;
20415c99eaecSFeras Daoud 
20425c99eaecSFeras Daoud 	return mlx5_ib_set_vma_data(vma, context);
20435c99eaecSFeras Daoud }
20445c99eaecSFeras Daoud 
204537aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
20467c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
20477c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
204837aa5c36SGuy Levi {
20492f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
205037aa5c36SGuy Levi 	int err;
205137aa5c36SGuy Levi 	unsigned long idx;
2052aa09ea6eSKamal Heib 	phys_addr_t pfn;
205337aa5c36SGuy Levi 	pgprot_t prot;
20544ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
20554ed131d0SYishai Hadas 	u32 uar_index;
20564ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
20574ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
20584ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2059b037c29aSEli Cohen 
2060b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2061b037c29aSEli Cohen 		return -EINVAL;
2062b037c29aSEli Cohen 
20634ed131d0SYishai Hadas 	if (dyn_uar)
20644ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
20654ed131d0SYishai Hadas 	else
2066b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
20674ed131d0SYishai Hadas 
20684ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
20694ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
20704ed131d0SYishai Hadas 			     idx, max_valid_idx);
2071b037c29aSEli Cohen 		return -EINVAL;
2072b037c29aSEli Cohen 	}
207337aa5c36SGuy Levi 
207437aa5c36SGuy Levi 	switch (cmd) {
207537aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
20764ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
207737aa5c36SGuy Levi /* Some architectures don't support WC memory */
207837aa5c36SGuy Levi #if defined(CONFIG_X86)
207937aa5c36SGuy Levi 		if (!pat_enabled())
208037aa5c36SGuy Levi 			return -EPERM;
208137aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
208237aa5c36SGuy Levi 			return -EPERM;
208337aa5c36SGuy Levi #endif
208437aa5c36SGuy Levi 	/* fall through */
208537aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
208637aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
208737aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
208837aa5c36SGuy Levi 		break;
208937aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
209037aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
209137aa5c36SGuy Levi 		break;
209237aa5c36SGuy Levi 	default:
209337aa5c36SGuy Levi 		return -EINVAL;
209437aa5c36SGuy Levi 	}
209537aa5c36SGuy Levi 
20964ed131d0SYishai Hadas 	if (dyn_uar) {
20974ed131d0SYishai Hadas 		int uars_per_page;
20984ed131d0SYishai Hadas 
20994ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21004ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21014ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21024ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21034ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21044ed131d0SYishai Hadas 			return -EINVAL;
21054ed131d0SYishai Hadas 		}
21064ed131d0SYishai Hadas 
21074ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21084ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21094ed131d0SYishai Hadas 		 * page holds its count.
21104ed131d0SYishai Hadas 		 */
21114ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
21124ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
21134ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
21144ed131d0SYishai Hadas 			return -EINVAL;
21154ed131d0SYishai Hadas 		}
21164ed131d0SYishai Hadas 
21174ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
21184ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
21194ed131d0SYishai Hadas 
21204ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
21214ed131d0SYishai Hadas 		if (err) {
21224ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
21234ed131d0SYishai Hadas 			goto free_bfreg;
21244ed131d0SYishai Hadas 		}
21254ed131d0SYishai Hadas 	} else {
21264ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
21274ed131d0SYishai Hadas 	}
21284ed131d0SYishai Hadas 
21294ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
213037aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2131e126ba97SEli Cohen 
213237aa5c36SGuy Levi 	vma->vm_page_prot = prot;
213337aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
213437aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
213537aa5c36SGuy Levi 	if (err) {
21368f062287SLeon Romanovsky 		mlx5_ib_err(dev,
21378f062287SLeon Romanovsky 			    "io_remap_pfn_range failed with error=%d, mmap_cmd=%s\n",
21388f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
21394ed131d0SYishai Hadas 		err = -EAGAIN;
21404ed131d0SYishai Hadas 		goto err;
214137aa5c36SGuy Levi 	}
2142e126ba97SEli Cohen 
21434ed131d0SYishai Hadas 	err = mlx5_ib_set_vma_data(vma, context);
21444ed131d0SYishai Hadas 	if (err)
21454ed131d0SYishai Hadas 		goto err;
21464ed131d0SYishai Hadas 
21474ed131d0SYishai Hadas 	if (dyn_uar)
21484ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
21494ed131d0SYishai Hadas 	return 0;
21504ed131d0SYishai Hadas 
21514ed131d0SYishai Hadas err:
21524ed131d0SYishai Hadas 	if (!dyn_uar)
21534ed131d0SYishai Hadas 		return err;
21544ed131d0SYishai Hadas 
21554ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
21564ed131d0SYishai Hadas 
21574ed131d0SYishai Hadas free_bfreg:
21584ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
21594ed131d0SYishai Hadas 
21604ed131d0SYishai Hadas 	return err;
216137aa5c36SGuy Levi }
216237aa5c36SGuy Levi 
216324da0016SAriel Levkovich static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
216424da0016SAriel Levkovich {
216524da0016SAriel Levkovich 	struct mlx5_ib_ucontext *mctx = to_mucontext(context);
216624da0016SAriel Levkovich 	struct mlx5_ib_dev *dev = to_mdev(context->device);
216724da0016SAriel Levkovich 	u16 page_idx = get_extended_index(vma->vm_pgoff);
216824da0016SAriel Levkovich 	size_t map_size = vma->vm_end - vma->vm_start;
216924da0016SAriel Levkovich 	u32 npages = map_size >> PAGE_SHIFT;
217024da0016SAriel Levkovich 	phys_addr_t pfn;
217124da0016SAriel Levkovich 	pgprot_t prot;
217224da0016SAriel Levkovich 
217324da0016SAriel Levkovich 	if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
217424da0016SAriel Levkovich 	    page_idx + npages)
217524da0016SAriel Levkovich 		return -EINVAL;
217624da0016SAriel Levkovich 
217724da0016SAriel Levkovich 	pfn = ((pci_resource_start(dev->mdev->pdev, 0) +
217824da0016SAriel Levkovich 	      MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
217924da0016SAriel Levkovich 	      PAGE_SHIFT) +
218024da0016SAriel Levkovich 	      page_idx;
218124da0016SAriel Levkovich 	prot = pgprot_writecombine(vma->vm_page_prot);
218224da0016SAriel Levkovich 	vma->vm_page_prot = prot;
218324da0016SAriel Levkovich 
218424da0016SAriel Levkovich 	if (io_remap_pfn_range(vma, vma->vm_start, pfn, map_size,
218524da0016SAriel Levkovich 			       vma->vm_page_prot))
218624da0016SAriel Levkovich 		return -EAGAIN;
218724da0016SAriel Levkovich 
218824da0016SAriel Levkovich 	return mlx5_ib_set_vma_data(vma, mctx);
218924da0016SAriel Levkovich }
219024da0016SAriel Levkovich 
219137aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
219237aa5c36SGuy Levi {
219337aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
219437aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
219537aa5c36SGuy Levi 	unsigned long command;
219637aa5c36SGuy Levi 	phys_addr_t pfn;
219737aa5c36SGuy Levi 
219837aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
219937aa5c36SGuy Levi 	switch (command) {
220037aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
220137aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
220237aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
22034ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
22047c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2205e126ba97SEli Cohen 
2206e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2207e126ba97SEli Cohen 		return -ENOSYS;
2208e126ba97SEli Cohen 
2209d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2210d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2211d69e3bcfSMatan Barak 			return -EINVAL;
2212d69e3bcfSMatan Barak 
22136cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2214d69e3bcfSMatan Barak 			return -EPERM;
2215d69e3bcfSMatan Barak 
2216d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2217d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2218d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2219d69e3bcfSMatan Barak 
2220d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2221d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2222d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2223d69e3bcfSMatan Barak 			PAGE_SHIFT;
2224d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2225d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
2226d69e3bcfSMatan Barak 			return -EAGAIN;
2227d69e3bcfSMatan Barak 		break;
22285c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
22295c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2230d69e3bcfSMatan Barak 
223124da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
223224da0016SAriel Levkovich 		return dm_mmap(ibcontext, vma);
223324da0016SAriel Levkovich 
2234e126ba97SEli Cohen 	default:
2235e126ba97SEli Cohen 		return -EINVAL;
2236e126ba97SEli Cohen 	}
2237e126ba97SEli Cohen 
2238e126ba97SEli Cohen 	return 0;
2239e126ba97SEli Cohen }
2240e126ba97SEli Cohen 
224124da0016SAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
224224da0016SAriel Levkovich 			       struct ib_ucontext *context,
224324da0016SAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
224424da0016SAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
224524da0016SAriel Levkovich {
224624da0016SAriel Levkovich 	u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
224724da0016SAriel Levkovich 	struct mlx5_memic *memic = &to_mdev(ibdev)->memic;
224824da0016SAriel Levkovich 	phys_addr_t memic_addr;
224924da0016SAriel Levkovich 	struct mlx5_ib_dm *dm;
225024da0016SAriel Levkovich 	u64 start_offset;
225124da0016SAriel Levkovich 	u32 page_idx;
225224da0016SAriel Levkovich 	int err;
225324da0016SAriel Levkovich 
225424da0016SAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
225524da0016SAriel Levkovich 	if (!dm)
225624da0016SAriel Levkovich 		return ERR_PTR(-ENOMEM);
225724da0016SAriel Levkovich 
225824da0016SAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
225924da0016SAriel Levkovich 		    attr->length, act_size, attr->alignment);
226024da0016SAriel Levkovich 
226124da0016SAriel Levkovich 	err = mlx5_cmd_alloc_memic(memic, &memic_addr,
226224da0016SAriel Levkovich 				   act_size, attr->alignment);
226324da0016SAriel Levkovich 	if (err)
226424da0016SAriel Levkovich 		goto err_free;
226524da0016SAriel Levkovich 
226624da0016SAriel Levkovich 	start_offset = memic_addr & ~PAGE_MASK;
226724da0016SAriel Levkovich 	page_idx = (memic_addr - pci_resource_start(memic->dev->pdev, 0) -
226824da0016SAriel Levkovich 		    MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
226924da0016SAriel Levkovich 		    PAGE_SHIFT;
227024da0016SAriel Levkovich 
227124da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
227224da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
227324da0016SAriel Levkovich 			     &start_offset, sizeof(start_offset));
227424da0016SAriel Levkovich 	if (err)
227524da0016SAriel Levkovich 		goto err_dealloc;
227624da0016SAriel Levkovich 
227724da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
227824da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
227924da0016SAriel Levkovich 			     &page_idx, sizeof(page_idx));
228024da0016SAriel Levkovich 	if (err)
228124da0016SAriel Levkovich 		goto err_dealloc;
228224da0016SAriel Levkovich 
228324da0016SAriel Levkovich 	bitmap_set(to_mucontext(context)->dm_pages, page_idx,
228424da0016SAriel Levkovich 		   DIV_ROUND_UP(act_size, PAGE_SIZE));
228524da0016SAriel Levkovich 
228624da0016SAriel Levkovich 	dm->dev_addr = memic_addr;
228724da0016SAriel Levkovich 
228824da0016SAriel Levkovich 	return &dm->ibdm;
228924da0016SAriel Levkovich 
229024da0016SAriel Levkovich err_dealloc:
229124da0016SAriel Levkovich 	mlx5_cmd_dealloc_memic(memic, memic_addr,
229224da0016SAriel Levkovich 			       act_size);
229324da0016SAriel Levkovich err_free:
229424da0016SAriel Levkovich 	kfree(dm);
229524da0016SAriel Levkovich 	return ERR_PTR(err);
229624da0016SAriel Levkovich }
229724da0016SAriel Levkovich 
229824da0016SAriel Levkovich int mlx5_ib_dealloc_dm(struct ib_dm *ibdm)
229924da0016SAriel Levkovich {
230024da0016SAriel Levkovich 	struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic;
230124da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
230224da0016SAriel Levkovich 	u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE);
230324da0016SAriel Levkovich 	u32 page_idx;
230424da0016SAriel Levkovich 	int ret;
230524da0016SAriel Levkovich 
230624da0016SAriel Levkovich 	ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size);
230724da0016SAriel Levkovich 	if (ret)
230824da0016SAriel Levkovich 		return ret;
230924da0016SAriel Levkovich 
231024da0016SAriel Levkovich 	page_idx = (dm->dev_addr - pci_resource_start(memic->dev->pdev, 0) -
231124da0016SAriel Levkovich 		    MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
231224da0016SAriel Levkovich 		    PAGE_SHIFT;
231324da0016SAriel Levkovich 	bitmap_clear(to_mucontext(ibdm->uobject->context)->dm_pages,
231424da0016SAriel Levkovich 		     page_idx,
231524da0016SAriel Levkovich 		     DIV_ROUND_UP(act_size, PAGE_SIZE));
231624da0016SAriel Levkovich 
231724da0016SAriel Levkovich 	kfree(dm);
231824da0016SAriel Levkovich 
231924da0016SAriel Levkovich 	return 0;
232024da0016SAriel Levkovich }
232124da0016SAriel Levkovich 
2322e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
2323e126ba97SEli Cohen 				      struct ib_ucontext *context,
2324e126ba97SEli Cohen 				      struct ib_udata *udata)
2325e126ba97SEli Cohen {
2326e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2327e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
2328e126ba97SEli Cohen 	int err;
2329e126ba97SEli Cohen 
2330e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
2331e126ba97SEli Cohen 	if (!pd)
2332e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
2333e126ba97SEli Cohen 
23349603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
2335e126ba97SEli Cohen 	if (err) {
2336e126ba97SEli Cohen 		kfree(pd);
2337e126ba97SEli Cohen 		return ERR_PTR(err);
2338e126ba97SEli Cohen 	}
2339e126ba97SEli Cohen 
2340e126ba97SEli Cohen 	if (context) {
2341e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2342e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
23439603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
2344e126ba97SEli Cohen 			kfree(pd);
2345e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
2346e126ba97SEli Cohen 		}
2347e126ba97SEli Cohen 	}
2348e126ba97SEli Cohen 
2349e126ba97SEli Cohen 	return &pd->ibpd;
2350e126ba97SEli Cohen }
2351e126ba97SEli Cohen 
2352e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
2353e126ba97SEli Cohen {
2354e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2355e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2356e126ba97SEli Cohen 
23579603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
2358e126ba97SEli Cohen 	kfree(mpd);
2359e126ba97SEli Cohen 
2360e126ba97SEli Cohen 	return 0;
2361e126ba97SEli Cohen }
2362e126ba97SEli Cohen 
2363466fa6d2SMaor Gottlieb enum {
2364466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2365466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
236671c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_INNER_BIT,
236771c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_MISC2_BIT
2368466fa6d2SMaor Gottlieb };
2369038d2ef8SMaor Gottlieb 
2370466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2371466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2372466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2373466fa6d2SMaor Gottlieb 
2374466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2375466fa6d2SMaor Gottlieb {
2376466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2377466fa6d2SMaor Gottlieb 
2378466fa6d2SMaor Gottlieb 	match_criteria_enable =
2379466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2380466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2381466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2382466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2383466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2384466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2385466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2386466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
238771c6e863SAriel Levkovich 	match_criteria_enable |=
238871c6e863SAriel Levkovich 		(!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
238971c6e863SAriel Levkovich 		MATCH_CRITERIA_ENABLE_MISC2_BIT;
2390466fa6d2SMaor Gottlieb 
2391466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2392038d2ef8SMaor Gottlieb }
2393038d2ef8SMaor Gottlieb 
2394ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2395ca0d4753SMaor Gottlieb {
2396ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2397ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2398ca0d4753SMaor Gottlieb }
2399ca0d4753SMaor Gottlieb 
240037da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
24012d1e697eSMoses Reuben 			   bool inner)
24022d1e697eSMoses Reuben {
24032d1e697eSMoses Reuben 	if (inner) {
24042d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24052d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
24062d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24072d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
24082d1e697eSMoses Reuben 	} else {
24092d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24102d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
24112d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24122d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
24132d1e697eSMoses Reuben 	}
24142d1e697eSMoses Reuben }
24152d1e697eSMoses Reuben 
2416ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2417ca0d4753SMaor Gottlieb {
2418ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2419ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2420ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2421ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2422ca0d4753SMaor Gottlieb }
2423ca0d4753SMaor Gottlieb 
242471c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
242571c6e863SAriel Levkovich {
242671c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
242771c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
242871c6e863SAriel Levkovich 		return -EOPNOTSUPP;
242971c6e863SAriel Levkovich 
243071c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
243171c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
243271c6e863SAriel Levkovich 		return -EOPNOTSUPP;
243371c6e863SAriel Levkovich 
243471c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
243571c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
243671c6e863SAriel Levkovich 		return -EOPNOTSUPP;
243771c6e863SAriel Levkovich 
243871c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
243971c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
244071c6e863SAriel Levkovich 		return -EOPNOTSUPP;
244171c6e863SAriel Levkovich 
244271c6e863SAriel Levkovich 	return 0;
244371c6e863SAriel Levkovich }
244471c6e863SAriel Levkovich 
2445c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2446c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2447ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2448466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2449c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2450ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
24512ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2452a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
24533b3233fbSRaed Salem #define LAST_COUNTERS_FIELD counters
2454c47ac6aeSMaor Gottlieb 
2455c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2456c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2457c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2458c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2459c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2460c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2461c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2462c47ac6aeSMaor Gottlieb 
2463802c2125SAviad Yehezkel static int parse_flow_flow_action(const union ib_flow_spec *ib_spec,
2464802c2125SAviad Yehezkel 				  const struct ib_flow_attr *flow_attr,
2465802c2125SAviad Yehezkel 				  struct mlx5_flow_act *action)
2466802c2125SAviad Yehezkel {
2467802c2125SAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(ib_spec->action.act);
2468802c2125SAviad Yehezkel 
2469802c2125SAviad Yehezkel 	switch (maction->ib_action.type) {
2470802c2125SAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
2471802c2125SAviad Yehezkel 		/* Currently only AES_GCM keymat is supported by the driver */
2472802c2125SAviad Yehezkel 		action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
2473802c2125SAviad Yehezkel 		action->action |= flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS ?
2474802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2475802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2476802c2125SAviad Yehezkel 		return 0;
2477802c2125SAviad Yehezkel 	default:
2478802c2125SAviad Yehezkel 		return -EOPNOTSUPP;
2479802c2125SAviad Yehezkel 	}
2480802c2125SAviad Yehezkel }
2481802c2125SAviad Yehezkel 
248219cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
248319cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
2484802c2125SAviad Yehezkel 			   const struct ib_flow_attr *flow_attr,
248571c6e863SAriel Levkovich 			   struct mlx5_flow_act *action, u32 prev_type)
2486038d2ef8SMaor Gottlieb {
2487466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2488466fa6d2SMaor Gottlieb 					   misc_parameters);
2489466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2490466fa6d2SMaor Gottlieb 					   misc_parameters);
249171c6e863SAriel Levkovich 	void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
249271c6e863SAriel Levkovich 					    misc_parameters_2);
249371c6e863SAriel Levkovich 	void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
249471c6e863SAriel Levkovich 					    misc_parameters_2);
24952d1e697eSMoses Reuben 	void *headers_c;
24962d1e697eSMoses Reuben 	void *headers_v;
249719cc7524SAriel Levkovich 	int match_ipv;
2498802c2125SAviad Yehezkel 	int ret;
2499466fa6d2SMaor Gottlieb 
25002d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
25012d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
25022d1e697eSMoses Reuben 					 inner_headers);
25032d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
25042d1e697eSMoses Reuben 					 inner_headers);
250519cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
250619cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
25072d1e697eSMoses Reuben 	} else {
25082d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
25092d1e697eSMoses Reuben 					 outer_headers);
25102d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
25112d1e697eSMoses Reuben 					 outer_headers);
251219cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
251319cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
25142d1e697eSMoses Reuben 	}
25152d1e697eSMoses Reuben 
25162d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2517038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2518c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
25191ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2520038d2ef8SMaor Gottlieb 
25212d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2522038d2ef8SMaor Gottlieb 					     dmac_47_16),
2523038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
25242d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2525038d2ef8SMaor Gottlieb 					     dmac_47_16),
2526038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2527038d2ef8SMaor Gottlieb 
25282d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2529ee3da804SMaor Gottlieb 					     smac_47_16),
2530ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
25312d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2532ee3da804SMaor Gottlieb 					     smac_47_16),
2533ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2534ee3da804SMaor Gottlieb 
2535038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
25362d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
253710543365SMohamad Haj Yahia 				 cvlan_tag, 1);
25382d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
253910543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2540038d2ef8SMaor Gottlieb 
25412d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2542038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
25432d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2544038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2545038d2ef8SMaor Gottlieb 
25462d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2547038d2ef8SMaor Gottlieb 				 first_cfi,
2548038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
25492d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2550038d2ef8SMaor Gottlieb 				 first_cfi,
2551038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2552038d2ef8SMaor Gottlieb 
25532d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2554038d2ef8SMaor Gottlieb 				 first_prio,
2555038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
25562d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2557038d2ef8SMaor Gottlieb 				 first_prio,
2558038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2559038d2ef8SMaor Gottlieb 		}
25602d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2561038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
25622d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2563038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2564038d2ef8SMaor Gottlieb 		break;
2565038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2566c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
25671ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2568038d2ef8SMaor Gottlieb 
256919cc7524SAriel Levkovich 		if (match_ipv) {
257019cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
257119cc7524SAriel Levkovich 				 ip_version, 0xf);
257219cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
25733346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV4_VERSION);
257419cc7524SAriel Levkovich 		} else {
25752d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2576038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
25772d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2578038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
257919cc7524SAriel Levkovich 		}
2580038d2ef8SMaor Gottlieb 
25812d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2582038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2583038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2584038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
25852d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2586038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2587038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2588038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
25892d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2590038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2591038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2592038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
25932d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2594038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2595038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2596038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2597ca0d4753SMaor Gottlieb 
25982d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2599ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2600ca0d4753SMaor Gottlieb 
26012d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2602ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
2603038d2ef8SMaor Gottlieb 		break;
2604026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2605c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
26061ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2607026bae0cSMaor Gottlieb 
260819cc7524SAriel Levkovich 		if (match_ipv) {
260919cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
261019cc7524SAriel Levkovich 				 ip_version, 0xf);
261119cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
26123346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV6_VERSION);
261319cc7524SAriel Levkovich 		} else {
26142d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2615026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
26162d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2617026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
261819cc7524SAriel Levkovich 		}
2619026bae0cSMaor Gottlieb 
26202d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2621026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2622026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2623026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
26242d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2625026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2626026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2627026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
26282d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2629026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2630026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2631026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
26322d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2633026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2634026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2635026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2636466fa6d2SMaor Gottlieb 
26372d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2638466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2639466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2640466fa6d2SMaor Gottlieb 
26412d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2642466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
2643466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
2644466fa6d2SMaor Gottlieb 
26452d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
26462d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
26472d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
26482d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
2649802c2125SAviad Yehezkel 		break;
2650802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ESP:
2651802c2125SAviad Yehezkel 		if (ib_spec->esp.mask.seq)
2652802c2125SAviad Yehezkel 			return -EOPNOTSUPP;
26532d1e697eSMoses Reuben 
2654802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2655802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.mask.spi));
2656802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2657802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.val.spi));
2658026bae0cSMaor Gottlieb 		break;
2659038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2660c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2661c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
26621ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2663038d2ef8SMaor Gottlieb 
26642d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2665038d2ef8SMaor Gottlieb 			 0xff);
26662d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2667038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
2668038d2ef8SMaor Gottlieb 
26692d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2670038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
26712d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2672038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2673038d2ef8SMaor Gottlieb 
26742d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2675038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
26762d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2677038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2678038d2ef8SMaor Gottlieb 		break;
2679038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2680c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2681c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
26821ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2683038d2ef8SMaor Gottlieb 
26842d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2685038d2ef8SMaor Gottlieb 			 0xff);
26862d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2687038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
2688038d2ef8SMaor Gottlieb 
26892d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2690038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
26912d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2692038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2693038d2ef8SMaor Gottlieb 
26942d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2695038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
26962d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2697038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2698038d2ef8SMaor Gottlieb 		break;
2699da2f22aeSAriel Levkovich 	case IB_FLOW_SPEC_GRE:
2700da2f22aeSAriel Levkovich 		if (ib_spec->gre.mask.c_ks_res0_ver)
2701da2f22aeSAriel Levkovich 			return -EOPNOTSUPP;
2702da2f22aeSAriel Levkovich 
2703da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2704da2f22aeSAriel Levkovich 			 0xff);
2705da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2706da2f22aeSAriel Levkovich 			 IPPROTO_GRE);
2707da2f22aeSAriel Levkovich 
2708da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
2709a93b632cSMaor Gottlieb 			 ntohs(ib_spec->gre.mask.protocol));
2710da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2711da2f22aeSAriel Levkovich 			 ntohs(ib_spec->gre.val.protocol));
2712da2f22aeSAriel Levkovich 
2713da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
2714da2f22aeSAriel Levkovich 				    gre_key_h),
2715da2f22aeSAriel Levkovich 		       &ib_spec->gre.mask.key,
2716da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.mask.key));
2717da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
2718da2f22aeSAriel Levkovich 				    gre_key_h),
2719da2f22aeSAriel Levkovich 		       &ib_spec->gre.val.key,
2720da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.val.key));
2721da2f22aeSAriel Levkovich 		break;
272271c6e863SAriel Levkovich 	case IB_FLOW_SPEC_MPLS:
272371c6e863SAriel Levkovich 		switch (prev_type) {
272471c6e863SAriel Levkovich 		case IB_FLOW_SPEC_UDP:
272571c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
272671c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_udp),
272771c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
272871c6e863SAriel Levkovich 				return -EOPNOTSUPP;
272971c6e863SAriel Levkovich 
273071c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
273171c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
273271c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
273371c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
273471c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
273571c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
273671c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
273771c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
273871c6e863SAriel Levkovich 			break;
273971c6e863SAriel Levkovich 		case IB_FLOW_SPEC_GRE:
274071c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
274171c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_gre),
274271c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
274371c6e863SAriel Levkovich 				return -EOPNOTSUPP;
274471c6e863SAriel Levkovich 
274571c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
274671c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
274771c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
274871c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
274971c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
275071c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
275171c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
275271c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
275371c6e863SAriel Levkovich 			break;
275471c6e863SAriel Levkovich 		default:
275571c6e863SAriel Levkovich 			if (ib_spec->type & IB_FLOW_SPEC_INNER) {
275671c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
275771c6e863SAriel Levkovich 							   ft_field_support.inner_first_mpls),
275871c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
275971c6e863SAriel Levkovich 					return -EOPNOTSUPP;
276071c6e863SAriel Levkovich 
276171c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
276271c6e863SAriel Levkovich 						    inner_first_mpls),
276371c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
276471c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
276571c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
276671c6e863SAriel Levkovich 						    inner_first_mpls),
276771c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
276871c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
276971c6e863SAriel Levkovich 			} else {
277071c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
277171c6e863SAriel Levkovich 							   ft_field_support.outer_first_mpls),
277271c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
277371c6e863SAriel Levkovich 					return -EOPNOTSUPP;
277471c6e863SAriel Levkovich 
277571c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
277671c6e863SAriel Levkovich 						    outer_first_mpls),
277771c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
277871c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
277971c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
278071c6e863SAriel Levkovich 						    outer_first_mpls),
278171c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
278271c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
278371c6e863SAriel Levkovich 			}
278471c6e863SAriel Levkovich 		}
278571c6e863SAriel Levkovich 		break;
2786ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2787ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2788ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
27891ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2790ffb30d8fSMoses Reuben 
2791ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2792ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2793ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2794ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2795ffb30d8fSMoses Reuben 		break;
27962ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
27972ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
27982ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
27992ac693f9SMoses Reuben 			return -EOPNOTSUPP;
28002ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
28012ac693f9SMoses Reuben 			return -EINVAL;
28022ac693f9SMoses Reuben 
2803075572d4SBoris Pismenny 		action->flow_tag = ib_spec->flow_tag.tag_id;
2804a9db0ecfSMatan Barak 		action->has_flow_tag = true;
28052ac693f9SMoses Reuben 		break;
2806a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2807a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2808a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2809a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2810075572d4SBoris Pismenny 		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
2811a22ed86cSSlava Shwartsman 		break;
2812802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ACTION_HANDLE:
2813802c2125SAviad Yehezkel 		ret = parse_flow_flow_action(ib_spec, flow_attr, action);
2814802c2125SAviad Yehezkel 		if (ret)
2815802c2125SAviad Yehezkel 			return ret;
2816802c2125SAviad Yehezkel 		break;
28173b3233fbSRaed Salem 	case IB_FLOW_SPEC_ACTION_COUNT:
28183b3233fbSRaed Salem 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
28193b3233fbSRaed Salem 					 LAST_COUNTERS_FIELD))
28203b3233fbSRaed Salem 			return -EOPNOTSUPP;
28213b3233fbSRaed Salem 
28223b3233fbSRaed Salem 		/* for now support only one counters spec per flow */
28233b3233fbSRaed Salem 		if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
28243b3233fbSRaed Salem 			return -EINVAL;
28253b3233fbSRaed Salem 
28263b3233fbSRaed Salem 		action->counters = ib_spec->flow_count.counters;
28273b3233fbSRaed Salem 		action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
28283b3233fbSRaed Salem 		break;
2829038d2ef8SMaor Gottlieb 	default:
2830038d2ef8SMaor Gottlieb 		return -EINVAL;
2831038d2ef8SMaor Gottlieb 	}
2832038d2ef8SMaor Gottlieb 
2833038d2ef8SMaor Gottlieb 	return 0;
2834038d2ef8SMaor Gottlieb }
2835038d2ef8SMaor Gottlieb 
2836038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
2837038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
2838038d2ef8SMaor Gottlieb  * could steal other multicast packets.
2839038d2ef8SMaor Gottlieb  */
2840a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2841038d2ef8SMaor Gottlieb {
284281e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
2843038d2ef8SMaor Gottlieb 
2844038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2845038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
2846038d2ef8SMaor Gottlieb 		return false;
2847038d2ef8SMaor Gottlieb 
284881e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
284981e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
285081e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
2851038d2ef8SMaor Gottlieb 
285281e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
285381e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
285481e30880SYishai Hadas 			return true;
285581e30880SYishai Hadas 
285681e30880SYishai Hadas 		return false;
285781e30880SYishai Hadas 	}
285881e30880SYishai Hadas 
285981e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
286081e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
286181e30880SYishai Hadas 
286281e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2863038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2864038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
2865038d2ef8SMaor Gottlieb 	}
2866038d2ef8SMaor Gottlieb 
286781e30880SYishai Hadas 	return false;
286881e30880SYishai Hadas }
286981e30880SYishai Hadas 
2870802c2125SAviad Yehezkel enum valid_spec {
2871802c2125SAviad Yehezkel 	VALID_SPEC_INVALID,
2872802c2125SAviad Yehezkel 	VALID_SPEC_VALID,
2873802c2125SAviad Yehezkel 	VALID_SPEC_NA,
2874802c2125SAviad Yehezkel };
2875802c2125SAviad Yehezkel 
2876802c2125SAviad Yehezkel static enum valid_spec
2877802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
2878802c2125SAviad Yehezkel 		     const struct mlx5_flow_spec *spec,
2879802c2125SAviad Yehezkel 		     const struct mlx5_flow_act *flow_act,
2880802c2125SAviad Yehezkel 		     bool egress)
2881802c2125SAviad Yehezkel {
2882802c2125SAviad Yehezkel 	const u32 *match_c = spec->match_criteria;
2883802c2125SAviad Yehezkel 	bool is_crypto =
2884802c2125SAviad Yehezkel 		(flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2885802c2125SAviad Yehezkel 				     MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
2886802c2125SAviad Yehezkel 	bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
2887802c2125SAviad Yehezkel 	bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
2888802c2125SAviad Yehezkel 
2889802c2125SAviad Yehezkel 	/*
2890802c2125SAviad Yehezkel 	 * Currently only crypto is supported in egress, when regular egress
2891802c2125SAviad Yehezkel 	 * rules would be supported, always return VALID_SPEC_NA.
2892802c2125SAviad Yehezkel 	 */
2893802c2125SAviad Yehezkel 	if (!is_crypto)
2894802c2125SAviad Yehezkel 		return egress ? VALID_SPEC_INVALID : VALID_SPEC_NA;
2895802c2125SAviad Yehezkel 
2896802c2125SAviad Yehezkel 	return is_crypto && is_ipsec &&
2897802c2125SAviad Yehezkel 		(!egress || (!is_drop && !flow_act->has_flow_tag)) ?
2898802c2125SAviad Yehezkel 		VALID_SPEC_VALID : VALID_SPEC_INVALID;
2899802c2125SAviad Yehezkel }
2900802c2125SAviad Yehezkel 
2901802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev,
2902802c2125SAviad Yehezkel 			  const struct mlx5_flow_spec *spec,
2903802c2125SAviad Yehezkel 			  const struct mlx5_flow_act *flow_act,
2904802c2125SAviad Yehezkel 			  bool egress)
2905802c2125SAviad Yehezkel {
2906802c2125SAviad Yehezkel 	/* We curretly only support ipsec egress flow */
2907802c2125SAviad Yehezkel 	return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
2908802c2125SAviad Yehezkel }
2909802c2125SAviad Yehezkel 
291019cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
291119cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
29120f750966SAriel Levkovich 			       bool check_inner)
2913038d2ef8SMaor Gottlieb {
2914038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
291519cc7524SAriel Levkovich 	int match_ipv = check_inner ?
291619cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
291719cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
291819cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
291919cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
29200f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
29210f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
29220f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
29230f750966SAriel Levkovich 	bool has_ethertype = false;
2924038d2ef8SMaor Gottlieb 	unsigned int spec_index;
29250f750966SAriel Levkovich 	bool mask_valid = true;
29260f750966SAriel Levkovich 	u16 eth_type = 0;
29270f750966SAriel Levkovich 	bool type_valid;
2928038d2ef8SMaor Gottlieb 
2929038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
2930038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
29310f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2932038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
29330f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
29340f750966SAriel Levkovich 				      htons(0xffff));
29350f750966SAriel Levkovich 			has_ethertype = true;
29360f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
29370f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
29380f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
29390f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
2940038d2ef8SMaor Gottlieb 		}
2941038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
2942038d2ef8SMaor Gottlieb 	}
29430f750966SAriel Levkovich 
29440f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
29450f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
29460f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
29470f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
29480f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
29490f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
295019cc7524SAriel Levkovich 
295119cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
295219cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
295319cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
29540f750966SAriel Levkovich 	}
29550f750966SAriel Levkovich 
29560f750966SAriel Levkovich 	return type_valid;
29570f750966SAriel Levkovich }
29580f750966SAriel Levkovich 
295919cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
296019cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
29610f750966SAriel Levkovich {
296219cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
296319cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
2964038d2ef8SMaor Gottlieb }
2965038d2ef8SMaor Gottlieb 
2966038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
2967038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
2968038d2ef8SMaor Gottlieb {
2969038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
2970038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
2971038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
2972038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
2973038d2ef8SMaor Gottlieb 	}
2974038d2ef8SMaor Gottlieb }
2975038d2ef8SMaor Gottlieb 
29763b3233fbSRaed Salem static void counters_clear_description(struct ib_counters *counters)
29773b3233fbSRaed Salem {
29783b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
29793b3233fbSRaed Salem 
29803b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
29813b3233fbSRaed Salem 	kfree(mcounters->counters_data);
29823b3233fbSRaed Salem 	mcounters->counters_data = NULL;
29833b3233fbSRaed Salem 	mcounters->cntrs_max_index = 0;
29843b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
29853b3233fbSRaed Salem }
29863b3233fbSRaed Salem 
2987038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2988038d2ef8SMaor Gottlieb {
2989038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2990038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2991038d2ef8SMaor Gottlieb 							  ibflow);
2992038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2993d4be3f44SYishai Hadas 	struct mlx5_ib_dev *dev = handler->dev;
2994038d2ef8SMaor Gottlieb 
29959a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
2996038d2ef8SMaor Gottlieb 
2997038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
299874491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2999cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
3000038d2ef8SMaor Gottlieb 		list_del(&iter->list);
3001038d2ef8SMaor Gottlieb 		kfree(iter);
3002038d2ef8SMaor Gottlieb 	}
3003038d2ef8SMaor Gottlieb 
300474491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
30055497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
30063b3233fbSRaed Salem 	if (handler->ibcounters &&
30073b3233fbSRaed Salem 	    atomic_read(&handler->ibcounters->usecnt) == 1)
30083b3233fbSRaed Salem 		counters_clear_description(handler->ibcounters);
3009038d2ef8SMaor Gottlieb 
30103b3233fbSRaed Salem 	mutex_unlock(&dev->flow_db->lock);
3011d4be3f44SYishai Hadas 	if (handler->flow_matcher)
3012d4be3f44SYishai Hadas 		atomic_dec(&handler->flow_matcher->usecnt);
3013038d2ef8SMaor Gottlieb 	kfree(handler);
3014038d2ef8SMaor Gottlieb 
3015038d2ef8SMaor Gottlieb 	return 0;
3016038d2ef8SMaor Gottlieb }
3017038d2ef8SMaor Gottlieb 
301835d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
301935d19011SMaor Gottlieb {
302035d19011SMaor Gottlieb 	priority *= 2;
302135d19011SMaor Gottlieb 	if (!dont_trap)
302235d19011SMaor Gottlieb 		priority++;
302335d19011SMaor Gottlieb 	return priority;
302435d19011SMaor Gottlieb }
302535d19011SMaor Gottlieb 
3026cc0e5d42SMaor Gottlieb enum flow_table_type {
3027cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
3028cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
3029cc0e5d42SMaor Gottlieb };
3030cc0e5d42SMaor Gottlieb 
303100b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
303200b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
3033d4be3f44SYishai Hadas 
3034d4be3f44SYishai Hadas static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3035d4be3f44SYishai Hadas 					   struct mlx5_ib_flow_prio *prio,
3036d4be3f44SYishai Hadas 					   int priority,
3037d4be3f44SYishai Hadas 					   int num_entries, int num_groups)
3038d4be3f44SYishai Hadas {
3039d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft;
3040d4be3f44SYishai Hadas 
3041d4be3f44SYishai Hadas 	ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3042d4be3f44SYishai Hadas 						 num_entries,
3043d4be3f44SYishai Hadas 						 num_groups,
3044d4be3f44SYishai Hadas 						 0, 0);
3045d4be3f44SYishai Hadas 	if (IS_ERR(ft))
3046d4be3f44SYishai Hadas 		return ERR_CAST(ft);
3047d4be3f44SYishai Hadas 
3048d4be3f44SYishai Hadas 	prio->flow_table = ft;
3049d4be3f44SYishai Hadas 	prio->refcount = 0;
3050d4be3f44SYishai Hadas 	return prio;
3051d4be3f44SYishai Hadas }
3052d4be3f44SYishai Hadas 
3053038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
3054cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
3055cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
3056038d2ef8SMaor Gottlieb {
305735d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
3058038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
3059038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
3060038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
3061dac388efSMaor Gottlieb 	int max_table_size;
3062038d2ef8SMaor Gottlieb 	int num_entries;
3063038d2ef8SMaor Gottlieb 	int num_groups;
3064038d2ef8SMaor Gottlieb 	int priority;
3065038d2ef8SMaor Gottlieb 
3066dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3067dac388efSMaor Gottlieb 						       log_max_ft_size));
3068038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
3069802c2125SAviad Yehezkel 		if (ft_type == MLX5_IB_FT_TX)
3070802c2125SAviad Yehezkel 			priority = 0;
3071802c2125SAviad Yehezkel 		else if (flow_is_multicast_only(flow_attr) &&
307235d19011SMaor Gottlieb 			 !dont_trap)
3073038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
3074038d2ef8SMaor Gottlieb 		else
307535d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
307635d19011SMaor Gottlieb 							dont_trap);
3077038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
3078802c2125SAviad Yehezkel 					     ft_type == MLX5_IB_FT_TX ?
3079802c2125SAviad Yehezkel 					     MLX5_FLOW_NAMESPACE_EGRESS :
3080038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
3081038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
3082038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
30839a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[priority];
3084038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3085038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3086038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
3087038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
3088038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
3089038d2ef8SMaor Gottlieb 					 &num_entries,
3090038d2ef8SMaor Gottlieb 					 &num_groups);
30919a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
3092cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3093cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3094cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
3095cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
3096cc0e5d42SMaor Gottlieb 
3097cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3098cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3099cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3100cc0e5d42SMaor Gottlieb 
31019a4ca38dSMark Bloch 		prio = &dev->flow_db->sniffer[ft_type];
3102cc0e5d42SMaor Gottlieb 		priority = 0;
3103cc0e5d42SMaor Gottlieb 		num_entries = 1;
3104cc0e5d42SMaor Gottlieb 		num_groups = 1;
3105038d2ef8SMaor Gottlieb 	}
3106038d2ef8SMaor Gottlieb 
3107038d2ef8SMaor Gottlieb 	if (!ns)
3108038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
3109038d2ef8SMaor Gottlieb 
3110dac388efSMaor Gottlieb 	if (num_entries > max_table_size)
3111dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
3112dac388efSMaor Gottlieb 
3113038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
3114d4be3f44SYishai Hadas 	if (!ft)
3115d4be3f44SYishai Hadas 		return _get_prio(ns, prio, priority, num_entries, num_groups);
3116038d2ef8SMaor Gottlieb 
3117d4be3f44SYishai Hadas 	return prio;
3118038d2ef8SMaor Gottlieb }
3119038d2ef8SMaor Gottlieb 
3120a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
3121a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
3122a550ddfcSYishai Hadas 			    u32 underlay_qpn)
3123a550ddfcSYishai Hadas {
3124a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3125a550ddfcSYishai Hadas 					   spec->match_criteria,
3126a550ddfcSYishai Hadas 					   misc_parameters);
3127a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3128a550ddfcSYishai Hadas 					   misc_parameters);
3129a550ddfcSYishai Hadas 
3130a550ddfcSYishai Hadas 	if (underlay_qpn &&
3131a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3132a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
3133a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3134a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
3135a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3136a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
3137a550ddfcSYishai Hadas 	}
3138a550ddfcSYishai Hadas }
3139a550ddfcSYishai Hadas 
31405e95af5fSRaed Salem static int read_flow_counters(struct ib_device *ibdev,
31415e95af5fSRaed Salem 			      struct mlx5_read_counters_attr *read_attr)
31425e95af5fSRaed Salem {
31435e95af5fSRaed Salem 	struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
31445e95af5fSRaed Salem 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
31455e95af5fSRaed Salem 
31465e95af5fSRaed Salem 	return mlx5_fc_query(dev->mdev, fc,
31475e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_PACKETS],
31485e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_BYTES]);
31495e95af5fSRaed Salem }
31505e95af5fSRaed Salem 
31515e95af5fSRaed Salem /* flow counters currently expose two counters packets and bytes */
31525e95af5fSRaed Salem #define FLOW_COUNTERS_NUM 2
31533b3233fbSRaed Salem static int counters_set_description(struct ib_counters *counters,
31543b3233fbSRaed Salem 				    enum mlx5_ib_counters_type counters_type,
31553b3233fbSRaed Salem 				    struct mlx5_ib_flow_counters_desc *desc_data,
31563b3233fbSRaed Salem 				    u32 ncounters)
31573b3233fbSRaed Salem {
31583b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
31593b3233fbSRaed Salem 	u32 cntrs_max_index = 0;
31603b3233fbSRaed Salem 	int i;
31613b3233fbSRaed Salem 
31623b3233fbSRaed Salem 	if (counters_type != MLX5_IB_COUNTERS_FLOW)
31633b3233fbSRaed Salem 		return -EINVAL;
31643b3233fbSRaed Salem 
31653b3233fbSRaed Salem 	/* init the fields for the object */
31663b3233fbSRaed Salem 	mcounters->type = counters_type;
31675e95af5fSRaed Salem 	mcounters->read_counters = read_flow_counters;
31685e95af5fSRaed Salem 	mcounters->counters_num = FLOW_COUNTERS_NUM;
31693b3233fbSRaed Salem 	mcounters->ncounters = ncounters;
31703b3233fbSRaed Salem 	/* each counter entry have both description and index pair */
31713b3233fbSRaed Salem 	for (i = 0; i < ncounters; i++) {
31723b3233fbSRaed Salem 		if (desc_data[i].description > IB_COUNTER_BYTES)
31733b3233fbSRaed Salem 			return -EINVAL;
31743b3233fbSRaed Salem 
31753b3233fbSRaed Salem 		if (cntrs_max_index <= desc_data[i].index)
31763b3233fbSRaed Salem 			cntrs_max_index = desc_data[i].index + 1;
31773b3233fbSRaed Salem 	}
31783b3233fbSRaed Salem 
31793b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
31803b3233fbSRaed Salem 	mcounters->counters_data = desc_data;
31813b3233fbSRaed Salem 	mcounters->cntrs_max_index = cntrs_max_index;
31823b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
31833b3233fbSRaed Salem 
31843b3233fbSRaed Salem 	return 0;
31853b3233fbSRaed Salem }
31863b3233fbSRaed Salem 
31873b3233fbSRaed Salem #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
31883b3233fbSRaed Salem static int flow_counters_set_data(struct ib_counters *ibcounters,
31893b3233fbSRaed Salem 				  struct mlx5_ib_create_flow *ucmd)
31903b3233fbSRaed Salem {
31913b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
31923b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
31933b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc_data = NULL;
31943b3233fbSRaed Salem 	bool hw_hndl = false;
31953b3233fbSRaed Salem 	int ret = 0;
31963b3233fbSRaed Salem 
31973b3233fbSRaed Salem 	if (ucmd && ucmd->ncounters_data != 0) {
31983b3233fbSRaed Salem 		cntrs_data = ucmd->data;
31993b3233fbSRaed Salem 		if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
32003b3233fbSRaed Salem 			return -EINVAL;
32013b3233fbSRaed Salem 
32023b3233fbSRaed Salem 		desc_data = kcalloc(cntrs_data->ncounters,
32033b3233fbSRaed Salem 				    sizeof(*desc_data),
32043b3233fbSRaed Salem 				    GFP_KERNEL);
32053b3233fbSRaed Salem 		if (!desc_data)
32063b3233fbSRaed Salem 			return  -ENOMEM;
32073b3233fbSRaed Salem 
32083b3233fbSRaed Salem 		if (copy_from_user(desc_data,
32093b3233fbSRaed Salem 				   u64_to_user_ptr(cntrs_data->counters_data),
32103b3233fbSRaed Salem 				   sizeof(*desc_data) * cntrs_data->ncounters)) {
32113b3233fbSRaed Salem 			ret = -EFAULT;
32123b3233fbSRaed Salem 			goto free;
32133b3233fbSRaed Salem 		}
32143b3233fbSRaed Salem 	}
32153b3233fbSRaed Salem 
32163b3233fbSRaed Salem 	if (!mcounters->hw_cntrs_hndl) {
32173b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = mlx5_fc_create(
32183b3233fbSRaed Salem 			to_mdev(ibcounters->device)->mdev, false);
3219e31abf76Sweiyongjun (A) 		if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3220e31abf76Sweiyongjun (A) 			ret = PTR_ERR(mcounters->hw_cntrs_hndl);
32213b3233fbSRaed Salem 			goto free;
32223b3233fbSRaed Salem 		}
32233b3233fbSRaed Salem 		hw_hndl = true;
32243b3233fbSRaed Salem 	}
32253b3233fbSRaed Salem 
32263b3233fbSRaed Salem 	if (desc_data) {
32273b3233fbSRaed Salem 		/* counters already bound to at least one flow */
32283b3233fbSRaed Salem 		if (mcounters->cntrs_max_index) {
32293b3233fbSRaed Salem 			ret = -EINVAL;
32303b3233fbSRaed Salem 			goto free_hndl;
32313b3233fbSRaed Salem 		}
32323b3233fbSRaed Salem 
32333b3233fbSRaed Salem 		ret = counters_set_description(ibcounters,
32343b3233fbSRaed Salem 					       MLX5_IB_COUNTERS_FLOW,
32353b3233fbSRaed Salem 					       desc_data,
32363b3233fbSRaed Salem 					       cntrs_data->ncounters);
32373b3233fbSRaed Salem 		if (ret)
32383b3233fbSRaed Salem 			goto free_hndl;
32393b3233fbSRaed Salem 
32403b3233fbSRaed Salem 	} else if (!mcounters->cntrs_max_index) {
32413b3233fbSRaed Salem 		/* counters not bound yet, must have udata passed */
32423b3233fbSRaed Salem 		ret = -EINVAL;
32433b3233fbSRaed Salem 		goto free_hndl;
32443b3233fbSRaed Salem 	}
32453b3233fbSRaed Salem 
32463b3233fbSRaed Salem 	return 0;
32473b3233fbSRaed Salem 
32483b3233fbSRaed Salem free_hndl:
32493b3233fbSRaed Salem 	if (hw_hndl) {
32503b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
32513b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
32523b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = NULL;
32533b3233fbSRaed Salem 	}
32543b3233fbSRaed Salem free:
32553b3233fbSRaed Salem 	kfree(desc_data);
32563b3233fbSRaed Salem 	return ret;
32573b3233fbSRaed Salem }
32583b3233fbSRaed Salem 
3259a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3260038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
3261dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
3262a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
32633b3233fbSRaed Salem 						      u32 underlay_qpn,
32643b3233fbSRaed Salem 						      struct mlx5_ib_create_flow *ucmd)
3265038d2ef8SMaor Gottlieb {
3266038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
3267038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
3268075572d4SBoris Pismenny 	struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
3269c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
32703b3233fbSRaed Salem 	struct mlx5_flow_destination dest_arr[2] = {};
32713b3233fbSRaed Salem 	struct mlx5_flow_destination *rule_dst = dest_arr;
3272dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3273038d2ef8SMaor Gottlieb 	unsigned int spec_index;
327471c6e863SAriel Levkovich 	u32 prev_type = 0;
3275038d2ef8SMaor Gottlieb 	int err = 0;
32763b3233fbSRaed Salem 	int dest_num = 0;
3277802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3278038d2ef8SMaor Gottlieb 
327919cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
3280038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3281038d2ef8SMaor Gottlieb 
32821b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3283038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3284c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
3285038d2ef8SMaor Gottlieb 		err = -ENOMEM;
3286038d2ef8SMaor Gottlieb 		goto free;
3287038d2ef8SMaor Gottlieb 	}
3288038d2ef8SMaor Gottlieb 
3289038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
32903b3233fbSRaed Salem 	if (dst) {
32913b3233fbSRaed Salem 		memcpy(&dest_arr[0], dst, sizeof(*dst));
32923b3233fbSRaed Salem 		dest_num++;
32933b3233fbSRaed Salem 	}
3294038d2ef8SMaor Gottlieb 
3295038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
329619cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
3297a22ed86cSSlava Shwartsman 				      spec->match_value,
329871c6e863SAriel Levkovich 				      ib_flow, flow_attr, &flow_act,
329971c6e863SAriel Levkovich 				      prev_type);
3300038d2ef8SMaor Gottlieb 		if (err < 0)
3301038d2ef8SMaor Gottlieb 			goto free;
3302038d2ef8SMaor Gottlieb 
330371c6e863SAriel Levkovich 		prev_type = ((union ib_flow_spec *)ib_flow)->type;
3304038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3305038d2ef8SMaor Gottlieb 	}
3306038d2ef8SMaor Gottlieb 
3307a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
3308a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
3309a550ddfcSYishai Hadas 
3310018a94eeSMark Bloch 	if (dev->rep) {
3311018a94eeSMark Bloch 		void *misc;
3312018a94eeSMark Bloch 
3313018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3314018a94eeSMark Bloch 				    misc_parameters);
3315018a94eeSMark Bloch 		MLX5_SET(fte_match_set_misc, misc, source_port,
3316018a94eeSMark Bloch 			 dev->rep->vport);
3317018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3318018a94eeSMark Bloch 				    misc_parameters);
3319018a94eeSMark Bloch 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3320018a94eeSMark Bloch 	}
3321018a94eeSMark Bloch 
3322466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3323802c2125SAviad Yehezkel 
3324802c2125SAviad Yehezkel 	if (is_egress &&
3325802c2125SAviad Yehezkel 	    !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3326802c2125SAviad Yehezkel 		err = -EINVAL;
3327802c2125SAviad Yehezkel 		goto free;
3328802c2125SAviad Yehezkel 	}
3329802c2125SAviad Yehezkel 
33303b3233fbSRaed Salem 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
33313b3233fbSRaed Salem 		err = flow_counters_set_data(flow_act.counters, ucmd);
33323b3233fbSRaed Salem 		if (err)
33333b3233fbSRaed Salem 			goto free;
33343b3233fbSRaed Salem 
33353b3233fbSRaed Salem 		handler->ibcounters = flow_act.counters;
33363b3233fbSRaed Salem 		dest_arr[dest_num].type =
33373b3233fbSRaed Salem 			MLX5_FLOW_DESTINATION_TYPE_COUNTER;
33383b3233fbSRaed Salem 		dest_arr[dest_num].counter =
33393b3233fbSRaed Salem 			to_mcounters(flow_act.counters)->hw_cntrs_hndl;
33403b3233fbSRaed Salem 		dest_num++;
33413b3233fbSRaed Salem 	}
33423b3233fbSRaed Salem 
3343075572d4SBoris Pismenny 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
33443b3233fbSRaed Salem 		if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3345a22ed86cSSlava Shwartsman 			rule_dst = NULL;
3346a22ed86cSSlava Shwartsman 			dest_num = 0;
33473b3233fbSRaed Salem 		}
3348a22ed86cSSlava Shwartsman 	} else {
3349802c2125SAviad Yehezkel 		if (is_egress)
3350802c2125SAviad Yehezkel 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3351802c2125SAviad Yehezkel 		else
3352802c2125SAviad Yehezkel 			flow_act.action |=
33533b3233fbSRaed Salem 				dest_num ?  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
335435d19011SMaor Gottlieb 					MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3355a22ed86cSSlava Shwartsman 	}
33562ac693f9SMoses Reuben 
3357a9db0ecfSMatan Barak 	if (flow_act.has_flow_tag &&
33582ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
33592ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
33602ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3361075572d4SBoris Pismenny 			     flow_act.flow_tag, flow_attr->type);
33622ac693f9SMoses Reuben 		err = -EINVAL;
33632ac693f9SMoses Reuben 		goto free;
33642ac693f9SMoses Reuben 	}
336574491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
336666958ed9SHadar Hen Zion 					    &flow_act,
3367a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
3368038d2ef8SMaor Gottlieb 
3369038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
3370038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
3371038d2ef8SMaor Gottlieb 		goto free;
3372038d2ef8SMaor Gottlieb 	}
3373038d2ef8SMaor Gottlieb 
3374d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
33755497adc6SMaor Gottlieb 	handler->prio = ft_prio;
3376d4be3f44SYishai Hadas 	handler->dev = dev;
3377038d2ef8SMaor Gottlieb 
3378038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
3379038d2ef8SMaor Gottlieb free:
33803b3233fbSRaed Salem 	if (err && handler) {
33813b3233fbSRaed Salem 		if (handler->ibcounters &&
33823b3233fbSRaed Salem 		    atomic_read(&handler->ibcounters->usecnt) == 1)
33833b3233fbSRaed Salem 			counters_clear_description(handler->ibcounters);
3384038d2ef8SMaor Gottlieb 		kfree(handler);
33853b3233fbSRaed Salem 	}
3386c5bb1730SMaor Gottlieb 	kvfree(spec);
3387038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
3388038d2ef8SMaor Gottlieb }
3389038d2ef8SMaor Gottlieb 
3390a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3391a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
3392a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
3393a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
3394a550ddfcSYishai Hadas {
33953b3233fbSRaed Salem 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
3396a550ddfcSYishai Hadas }
3397a550ddfcSYishai Hadas 
339835d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
339935d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
340035d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
340135d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
340235d19011SMaor Gottlieb {
340335d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
340435d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
340535d19011SMaor Gottlieb 
340635d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
340735d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
340835d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
340935d19011SMaor Gottlieb 					       flow_attr, dst);
341035d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
341174491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3412d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
341335d19011SMaor Gottlieb 			kfree(handler);
341435d19011SMaor Gottlieb 			handler = handler_dst;
341535d19011SMaor Gottlieb 		} else {
341635d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
341735d19011SMaor Gottlieb 		}
341835d19011SMaor Gottlieb 	}
341935d19011SMaor Gottlieb 
342035d19011SMaor Gottlieb 	return handler;
342135d19011SMaor Gottlieb }
3422038d2ef8SMaor Gottlieb enum {
3423038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
3424038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
3425038d2ef8SMaor Gottlieb };
3426038d2ef8SMaor Gottlieb 
3427038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3428038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
3429038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
3430038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
3431038d2ef8SMaor Gottlieb {
3432038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
3433038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3434038d2ef8SMaor Gottlieb 
3435038d2ef8SMaor Gottlieb 	static struct {
3436038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
3437038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
3438038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
3439038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
3440038d2ef8SMaor Gottlieb 			.flow_attr = {
3441038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3442038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3443038d2ef8SMaor Gottlieb 			},
3444038d2ef8SMaor Gottlieb 			.eth_flow = {
3445038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3446038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3447038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3448038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
3449038d2ef8SMaor Gottlieb 			}
3450038d2ef8SMaor Gottlieb 		},
3451038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
3452038d2ef8SMaor Gottlieb 			.flow_attr = {
3453038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3454038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3455038d2ef8SMaor Gottlieb 			},
3456038d2ef8SMaor Gottlieb 			.eth_flow = {
3457038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3458038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3459038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3460038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
3461038d2ef8SMaor Gottlieb 			}
3462038d2ef8SMaor Gottlieb 		}
3463038d2ef8SMaor Gottlieb 	};
3464038d2ef8SMaor Gottlieb 
3465038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
3466038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
3467038d2ef8SMaor Gottlieb 				   dst);
3468038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
3469038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3470038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
3471038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3472038d2ef8SMaor Gottlieb 						 dst);
3473038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
347474491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3475d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
3476038d2ef8SMaor Gottlieb 			kfree(handler);
3477038d2ef8SMaor Gottlieb 			handler = handler_ucast;
3478038d2ef8SMaor Gottlieb 		} else {
3479038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
3480038d2ef8SMaor Gottlieb 		}
3481038d2ef8SMaor Gottlieb 	}
3482038d2ef8SMaor Gottlieb 
3483038d2ef8SMaor Gottlieb 	return handler;
3484038d2ef8SMaor Gottlieb }
3485038d2ef8SMaor Gottlieb 
3486cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3487cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
3488cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
3489cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
3490cc0e5d42SMaor Gottlieb {
3491cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
3492cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
3493cc0e5d42SMaor Gottlieb 	int err;
3494cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
3495cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
3496cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
3497cc0e5d42SMaor Gottlieb 	};
3498cc0e5d42SMaor Gottlieb 
3499cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3500cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
3501cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
3502cc0e5d42SMaor Gottlieb 		goto err;
3503cc0e5d42SMaor Gottlieb 	}
3504cc0e5d42SMaor Gottlieb 
3505cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3506cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
3507cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
3508cc0e5d42SMaor Gottlieb 		goto err_tx;
3509cc0e5d42SMaor Gottlieb 	}
3510cc0e5d42SMaor Gottlieb 
3511cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
3512cc0e5d42SMaor Gottlieb 
3513cc0e5d42SMaor Gottlieb 	return handler_rx;
3514cc0e5d42SMaor Gottlieb 
3515cc0e5d42SMaor Gottlieb err_tx:
351674491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
3517cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
3518cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
3519cc0e5d42SMaor Gottlieb err:
3520cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
3521cc0e5d42SMaor Gottlieb }
3522cc0e5d42SMaor Gottlieb 
3523038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3524038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
352559082a32SMatan Barak 					   int domain,
352659082a32SMatan Barak 					   struct ib_udata *udata)
3527038d2ef8SMaor Gottlieb {
3528038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3529d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
3530038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3531038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
3532cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3533038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
3534802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
35353b3233fbSRaed Salem 	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
35363b3233fbSRaed Salem 	size_t min_ucmd_sz, required_ucmd_sz;
3537038d2ef8SMaor Gottlieb 	int err;
3538a550ddfcSYishai Hadas 	int underlay_qpn;
3539038d2ef8SMaor Gottlieb 
35403b3233fbSRaed Salem 	if (udata && udata->inlen) {
35413b3233fbSRaed Salem 		min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
35423b3233fbSRaed Salem 				sizeof(ucmd_hdr.reserved);
35433b3233fbSRaed Salem 		if (udata->inlen < min_ucmd_sz)
354459082a32SMatan Barak 			return ERR_PTR(-EOPNOTSUPP);
354559082a32SMatan Barak 
35463b3233fbSRaed Salem 		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
35473b3233fbSRaed Salem 		if (err)
35483b3233fbSRaed Salem 			return ERR_PTR(err);
35493b3233fbSRaed Salem 
35503b3233fbSRaed Salem 		/* currently supports only one counters data */
35513b3233fbSRaed Salem 		if (ucmd_hdr.ncounters_data > 1)
35523b3233fbSRaed Salem 			return ERR_PTR(-EINVAL);
35533b3233fbSRaed Salem 
35543b3233fbSRaed Salem 		required_ucmd_sz = min_ucmd_sz +
35553b3233fbSRaed Salem 			sizeof(struct mlx5_ib_flow_counters_data) *
35563b3233fbSRaed Salem 			ucmd_hdr.ncounters_data;
35573b3233fbSRaed Salem 		if (udata->inlen > required_ucmd_sz &&
35583b3233fbSRaed Salem 		    !ib_is_udata_cleared(udata, required_ucmd_sz,
35593b3233fbSRaed Salem 					 udata->inlen - required_ucmd_sz))
35603b3233fbSRaed Salem 			return ERR_PTR(-EOPNOTSUPP);
35613b3233fbSRaed Salem 
35623b3233fbSRaed Salem 		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
35633b3233fbSRaed Salem 		if (!ucmd)
35643b3233fbSRaed Salem 			return ERR_PTR(-ENOMEM);
35653b3233fbSRaed Salem 
35663b3233fbSRaed Salem 		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
3567299eafeeSGustavo A. R. Silva 		if (err)
3568299eafeeSGustavo A. R. Silva 			goto free_ucmd;
35693b3233fbSRaed Salem 	}
35703b3233fbSRaed Salem 
3571299eafeeSGustavo A. R. Silva 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3572299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3573299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3574299eafeeSGustavo A. R. Silva 	}
3575038d2ef8SMaor Gottlieb 
3576038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
3577508562d6SDaniel Jurgens 	    flow_attr->port > dev->num_ports ||
3578802c2125SAviad Yehezkel 	    (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3579299eafeeSGustavo A. R. Silva 				  IB_FLOW_ATTR_FLAGS_EGRESS))) {
3580299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3581299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3582299eafeeSGustavo A. R. Silva 	}
3583802c2125SAviad Yehezkel 
3584802c2125SAviad Yehezkel 	if (is_egress &&
3585802c2125SAviad Yehezkel 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3586299eafeeSGustavo A. R. Silva 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3587299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3588299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3589299eafeeSGustavo A. R. Silva 	}
3590038d2ef8SMaor Gottlieb 
3591038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3592299eafeeSGustavo A. R. Silva 	if (!dst) {
3593299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3594299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3595299eafeeSGustavo A. R. Silva 	}
3596038d2ef8SMaor Gottlieb 
35979a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3598038d2ef8SMaor Gottlieb 
3599802c2125SAviad Yehezkel 	ft_prio = get_flow_table(dev, flow_attr,
3600802c2125SAviad Yehezkel 				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3601038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
3602038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
3603038d2ef8SMaor Gottlieb 		goto unlock;
3604038d2ef8SMaor Gottlieb 	}
3605cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3606cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3607cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
3608cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
3609cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
3610cc0e5d42SMaor Gottlieb 			goto destroy_ft;
3611cc0e5d42SMaor Gottlieb 		}
3612cc0e5d42SMaor Gottlieb 	}
3613038d2ef8SMaor Gottlieb 
3614802c2125SAviad Yehezkel 	if (is_egress) {
3615802c2125SAviad Yehezkel 		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3616802c2125SAviad Yehezkel 	} else {
3617038d2ef8SMaor Gottlieb 		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3618d9f88e5aSYishai Hadas 		if (mqp->flags & MLX5_IB_QP_RSS)
3619d9f88e5aSYishai Hadas 			dst->tir_num = mqp->rss_qp.tirn;
3620d9f88e5aSYishai Hadas 		else
3621d9f88e5aSYishai Hadas 			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3622802c2125SAviad Yehezkel 	}
3623038d2ef8SMaor Gottlieb 
3624038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
362535d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
362635d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
362735d19011SMaor Gottlieb 							flow_attr, dst);
362835d19011SMaor Gottlieb 		} else {
3629a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3630a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
3631a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
36323b3233fbSRaed Salem 						    dst, underlay_qpn, ucmd);
363335d19011SMaor Gottlieb 		}
3634038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3635038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3636038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3637038d2ef8SMaor Gottlieb 						dst);
3638cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3639cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3640038d2ef8SMaor Gottlieb 	} else {
3641038d2ef8SMaor Gottlieb 		err = -EINVAL;
3642038d2ef8SMaor Gottlieb 		goto destroy_ft;
3643038d2ef8SMaor Gottlieb 	}
3644038d2ef8SMaor Gottlieb 
3645038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
3646038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
3647038d2ef8SMaor Gottlieb 		handler = NULL;
3648038d2ef8SMaor Gottlieb 		goto destroy_ft;
3649038d2ef8SMaor Gottlieb 	}
3650038d2ef8SMaor Gottlieb 
36519a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3652038d2ef8SMaor Gottlieb 	kfree(dst);
36533b3233fbSRaed Salem 	kfree(ucmd);
3654038d2ef8SMaor Gottlieb 
3655038d2ef8SMaor Gottlieb 	return &handler->ibflow;
3656038d2ef8SMaor Gottlieb 
3657038d2ef8SMaor Gottlieb destroy_ft:
3658038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
3659cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
3660cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
3661038d2ef8SMaor Gottlieb unlock:
36629a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3663038d2ef8SMaor Gottlieb 	kfree(dst);
3664299eafeeSGustavo A. R. Silva free_ucmd:
36653b3233fbSRaed Salem 	kfree(ucmd);
3666038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
3667038d2ef8SMaor Gottlieb }
3668038d2ef8SMaor Gottlieb 
3669d4be3f44SYishai Hadas static struct mlx5_ib_flow_prio *_get_flow_table(struct mlx5_ib_dev *dev,
3670d4be3f44SYishai Hadas 						 int priority, bool mcast)
3671d4be3f44SYishai Hadas {
3672d4be3f44SYishai Hadas 	int max_table_size;
3673d4be3f44SYishai Hadas 	struct mlx5_flow_namespace *ns = NULL;
3674d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *prio;
3675d4be3f44SYishai Hadas 
3676d4be3f44SYishai Hadas 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3677d4be3f44SYishai Hadas 			     log_max_ft_size));
3678d4be3f44SYishai Hadas 	if (max_table_size < MLX5_FS_MAX_ENTRIES)
3679d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
3680d4be3f44SYishai Hadas 
3681d4be3f44SYishai Hadas 	if (mcast)
3682d4be3f44SYishai Hadas 		priority = MLX5_IB_FLOW_MCAST_PRIO;
3683d4be3f44SYishai Hadas 	else
3684d4be3f44SYishai Hadas 		priority = ib_prio_to_core_prio(priority, false);
3685d4be3f44SYishai Hadas 
3686d4be3f44SYishai Hadas 	ns = mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS);
3687d4be3f44SYishai Hadas 	if (!ns)
3688d4be3f44SYishai Hadas 		return ERR_PTR(-ENOTSUPP);
3689d4be3f44SYishai Hadas 
3690d4be3f44SYishai Hadas 	prio = &dev->flow_db->prios[priority];
3691d4be3f44SYishai Hadas 
3692d4be3f44SYishai Hadas 	if (prio->flow_table)
3693d4be3f44SYishai Hadas 		return prio;
3694d4be3f44SYishai Hadas 
3695d4be3f44SYishai Hadas 	return _get_prio(ns, prio, priority, MLX5_FS_MAX_ENTRIES,
3696d4be3f44SYishai Hadas 			 MLX5_FS_MAX_TYPES);
3697d4be3f44SYishai Hadas }
3698d4be3f44SYishai Hadas 
3699d4be3f44SYishai Hadas static struct mlx5_ib_flow_handler *
3700d4be3f44SYishai Hadas _create_raw_flow_rule(struct mlx5_ib_dev *dev,
3701d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_prio *ft_prio,
3702d4be3f44SYishai Hadas 		      struct mlx5_flow_destination *dst,
3703d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_matcher  *fs_matcher,
3704d4be3f44SYishai Hadas 		      void *cmd_in, int inlen)
3705d4be3f44SYishai Hadas {
3706d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
3707d4be3f44SYishai Hadas 	struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
3708d4be3f44SYishai Hadas 	struct mlx5_flow_spec *spec;
3709d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft = ft_prio->flow_table;
3710d4be3f44SYishai Hadas 	int err = 0;
3711d4be3f44SYishai Hadas 
3712d4be3f44SYishai Hadas 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3713d4be3f44SYishai Hadas 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3714d4be3f44SYishai Hadas 	if (!handler || !spec) {
3715d4be3f44SYishai Hadas 		err = -ENOMEM;
3716d4be3f44SYishai Hadas 		goto free;
3717d4be3f44SYishai Hadas 	}
3718d4be3f44SYishai Hadas 
3719d4be3f44SYishai Hadas 	INIT_LIST_HEAD(&handler->list);
3720d4be3f44SYishai Hadas 
3721d4be3f44SYishai Hadas 	memcpy(spec->match_value, cmd_in, inlen);
3722d4be3f44SYishai Hadas 	memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
3723d4be3f44SYishai Hadas 	       fs_matcher->mask_len);
3724d4be3f44SYishai Hadas 	spec->match_criteria_enable = fs_matcher->match_criteria_enable;
3725d4be3f44SYishai Hadas 
3726d4be3f44SYishai Hadas 	flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3727d4be3f44SYishai Hadas 	handler->rule = mlx5_add_flow_rules(ft, spec,
3728d4be3f44SYishai Hadas 					    &flow_act, dst, 1);
3729d4be3f44SYishai Hadas 
3730d4be3f44SYishai Hadas 	if (IS_ERR(handler->rule)) {
3731d4be3f44SYishai Hadas 		err = PTR_ERR(handler->rule);
3732d4be3f44SYishai Hadas 		goto free;
3733d4be3f44SYishai Hadas 	}
3734d4be3f44SYishai Hadas 
3735d4be3f44SYishai Hadas 	ft_prio->refcount++;
3736d4be3f44SYishai Hadas 	handler->prio = ft_prio;
3737d4be3f44SYishai Hadas 	handler->dev = dev;
3738d4be3f44SYishai Hadas 	ft_prio->flow_table = ft;
3739d4be3f44SYishai Hadas 
3740d4be3f44SYishai Hadas free:
3741d4be3f44SYishai Hadas 	if (err)
3742d4be3f44SYishai Hadas 		kfree(handler);
3743d4be3f44SYishai Hadas 	kvfree(spec);
3744d4be3f44SYishai Hadas 	return err ? ERR_PTR(err) : handler;
3745d4be3f44SYishai Hadas }
3746d4be3f44SYishai Hadas 
3747d4be3f44SYishai Hadas static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
3748d4be3f44SYishai Hadas 				void *match_v)
3749d4be3f44SYishai Hadas {
3750d4be3f44SYishai Hadas 	void *match_c;
3751d4be3f44SYishai Hadas 	void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
3752d4be3f44SYishai Hadas 	void *dmac, *dmac_mask;
3753d4be3f44SYishai Hadas 	void *ipv4, *ipv4_mask;
3754d4be3f44SYishai Hadas 
3755d4be3f44SYishai Hadas 	if (!(fs_matcher->match_criteria_enable &
3756d4be3f44SYishai Hadas 	      (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
3757d4be3f44SYishai Hadas 		return false;
3758d4be3f44SYishai Hadas 
3759d4be3f44SYishai Hadas 	match_c = fs_matcher->matcher_mask.match_params;
3760d4be3f44SYishai Hadas 	match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
3761d4be3f44SYishai Hadas 					   outer_headers);
3762d4be3f44SYishai Hadas 	match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
3763d4be3f44SYishai Hadas 					   outer_headers);
3764d4be3f44SYishai Hadas 
3765d4be3f44SYishai Hadas 	dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3766d4be3f44SYishai Hadas 			    dmac_47_16);
3767d4be3f44SYishai Hadas 	dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3768d4be3f44SYishai Hadas 				 dmac_47_16);
3769d4be3f44SYishai Hadas 
3770d4be3f44SYishai Hadas 	if (is_multicast_ether_addr(dmac) &&
3771d4be3f44SYishai Hadas 	    is_multicast_ether_addr(dmac_mask))
3772d4be3f44SYishai Hadas 		return true;
3773d4be3f44SYishai Hadas 
3774d4be3f44SYishai Hadas 	ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3775d4be3f44SYishai Hadas 			    dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3776d4be3f44SYishai Hadas 
3777d4be3f44SYishai Hadas 	ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3778d4be3f44SYishai Hadas 				 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3779d4be3f44SYishai Hadas 
3780d4be3f44SYishai Hadas 	if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
3781d4be3f44SYishai Hadas 	    ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
3782d4be3f44SYishai Hadas 		return true;
3783d4be3f44SYishai Hadas 
3784d4be3f44SYishai Hadas 	return false;
3785d4be3f44SYishai Hadas }
3786d4be3f44SYishai Hadas 
378732269441SYishai Hadas struct mlx5_ib_flow_handler *
378832269441SYishai Hadas mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
378932269441SYishai Hadas 			struct mlx5_ib_flow_matcher *fs_matcher,
379032269441SYishai Hadas 			void *cmd_in, int inlen, int dest_id,
379132269441SYishai Hadas 			int dest_type)
379232269441SYishai Hadas {
3793d4be3f44SYishai Hadas 	struct mlx5_flow_destination *dst;
3794d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *ft_prio;
3795d4be3f44SYishai Hadas 	int priority = fs_matcher->priority;
3796d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
3797d4be3f44SYishai Hadas 	bool mcast;
3798d4be3f44SYishai Hadas 	int err;
3799d4be3f44SYishai Hadas 
3800d4be3f44SYishai Hadas 	if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
380132269441SYishai Hadas 		return ERR_PTR(-EOPNOTSUPP);
3802d4be3f44SYishai Hadas 
3803d4be3f44SYishai Hadas 	if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
3804d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
3805d4be3f44SYishai Hadas 
3806d4be3f44SYishai Hadas 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3807d4be3f44SYishai Hadas 	if (!dst)
3808d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
3809d4be3f44SYishai Hadas 
3810d4be3f44SYishai Hadas 	mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
3811d4be3f44SYishai Hadas 	mutex_lock(&dev->flow_db->lock);
3812d4be3f44SYishai Hadas 
3813d4be3f44SYishai Hadas 	ft_prio = _get_flow_table(dev, priority, mcast);
3814d4be3f44SYishai Hadas 	if (IS_ERR(ft_prio)) {
3815d4be3f44SYishai Hadas 		err = PTR_ERR(ft_prio);
3816d4be3f44SYishai Hadas 		goto unlock;
3817d4be3f44SYishai Hadas 	}
3818d4be3f44SYishai Hadas 
38196346f0bfSYishai Hadas 	if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
3820d4be3f44SYishai Hadas 		dst->type = dest_type;
3821d4be3f44SYishai Hadas 		dst->tir_num = dest_id;
38226346f0bfSYishai Hadas 	} else {
38236346f0bfSYishai Hadas 		dst->type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
38246346f0bfSYishai Hadas 		dst->ft_num = dest_id;
38256346f0bfSYishai Hadas 	}
38266346f0bfSYishai Hadas 
3827d4be3f44SYishai Hadas 	handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher, cmd_in,
3828d4be3f44SYishai Hadas 					inlen);
3829d4be3f44SYishai Hadas 
3830d4be3f44SYishai Hadas 	if (IS_ERR(handler)) {
3831d4be3f44SYishai Hadas 		err = PTR_ERR(handler);
3832d4be3f44SYishai Hadas 		goto destroy_ft;
3833d4be3f44SYishai Hadas 	}
3834d4be3f44SYishai Hadas 
3835d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
3836d4be3f44SYishai Hadas 	atomic_inc(&fs_matcher->usecnt);
3837d4be3f44SYishai Hadas 	handler->flow_matcher = fs_matcher;
3838d4be3f44SYishai Hadas 
3839d4be3f44SYishai Hadas 	kfree(dst);
3840d4be3f44SYishai Hadas 
3841d4be3f44SYishai Hadas 	return handler;
3842d4be3f44SYishai Hadas 
3843d4be3f44SYishai Hadas destroy_ft:
3844d4be3f44SYishai Hadas 	put_flow_table(dev, ft_prio, false);
3845d4be3f44SYishai Hadas unlock:
3846d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
3847d4be3f44SYishai Hadas 	kfree(dst);
3848d4be3f44SYishai Hadas 
3849d4be3f44SYishai Hadas 	return ERR_PTR(err);
385032269441SYishai Hadas }
385132269441SYishai Hadas 
3852c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
3853c6475a0bSAviad Yehezkel {
3854c6475a0bSAviad Yehezkel 	u32 flags = 0;
3855c6475a0bSAviad Yehezkel 
3856c6475a0bSAviad Yehezkel 	if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
3857c6475a0bSAviad Yehezkel 		flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
3858c6475a0bSAviad Yehezkel 
3859c6475a0bSAviad Yehezkel 	return flags;
3860c6475a0bSAviad Yehezkel }
3861c6475a0bSAviad Yehezkel 
3862c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED	MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
3863c6475a0bSAviad Yehezkel static struct ib_flow_action *
3864c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device,
3865c6475a0bSAviad Yehezkel 			       const struct ib_flow_action_attrs_esp *attr,
3866c6475a0bSAviad Yehezkel 			       struct uverbs_attr_bundle *attrs)
3867c6475a0bSAviad Yehezkel {
3868c6475a0bSAviad Yehezkel 	struct mlx5_ib_dev *mdev = to_mdev(device);
3869c6475a0bSAviad Yehezkel 	struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
3870c6475a0bSAviad Yehezkel 	struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
3871c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *action;
3872c6475a0bSAviad Yehezkel 	u64 action_flags;
3873c6475a0bSAviad Yehezkel 	u64 flags;
3874c6475a0bSAviad Yehezkel 	int err = 0;
3875c6475a0bSAviad Yehezkel 
3876bccd0622SJason Gunthorpe 	err = uverbs_get_flags64(
3877bccd0622SJason Gunthorpe 		&action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
3878bccd0622SJason Gunthorpe 		((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
3879bccd0622SJason Gunthorpe 	if (err)
3880bccd0622SJason Gunthorpe 		return ERR_PTR(err);
3881c6475a0bSAviad Yehezkel 
3882c6475a0bSAviad Yehezkel 	flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
3883c6475a0bSAviad Yehezkel 
3884c6475a0bSAviad Yehezkel 	/* We current only support a subset of the standard features. Only a
3885c6475a0bSAviad Yehezkel 	 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
3886c6475a0bSAviad Yehezkel 	 * (with overlap). Full offload mode isn't supported.
3887c6475a0bSAviad Yehezkel 	 */
3888c6475a0bSAviad Yehezkel 	if (!attr->keymat || attr->replay || attr->encap ||
3889c6475a0bSAviad Yehezkel 	    attr->spi || attr->seq || attr->tfc_pad ||
3890c6475a0bSAviad Yehezkel 	    attr->hard_limit_pkts ||
3891c6475a0bSAviad Yehezkel 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3892c6475a0bSAviad Yehezkel 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
3893c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3894c6475a0bSAviad Yehezkel 
3895c6475a0bSAviad Yehezkel 	if (attr->keymat->protocol !=
3896c6475a0bSAviad Yehezkel 	    IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
3897c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3898c6475a0bSAviad Yehezkel 
3899c6475a0bSAviad Yehezkel 	aes_gcm = &attr->keymat->keymat.aes_gcm;
3900c6475a0bSAviad Yehezkel 
3901c6475a0bSAviad Yehezkel 	if (aes_gcm->icv_len != 16 ||
3902c6475a0bSAviad Yehezkel 	    aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
3903c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3904c6475a0bSAviad Yehezkel 
3905c6475a0bSAviad Yehezkel 	action = kmalloc(sizeof(*action), GFP_KERNEL);
3906c6475a0bSAviad Yehezkel 	if (!action)
3907c6475a0bSAviad Yehezkel 		return ERR_PTR(-ENOMEM);
3908c6475a0bSAviad Yehezkel 
3909c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
3910c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
3911c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.aes_key));
3912c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
3913c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
3914c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.salt));
3915c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
3916c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
3917c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
3918c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
3919c6475a0bSAviad Yehezkel 	accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
3920c6475a0bSAviad Yehezkel 
3921c6475a0bSAviad Yehezkel 	accel_attrs.esn = attr->esn;
3922c6475a0bSAviad Yehezkel 	if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
3923c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
3924c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
3925c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3926c6475a0bSAviad Yehezkel 
3927c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
3928c6475a0bSAviad Yehezkel 		accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
3929c6475a0bSAviad Yehezkel 
3930c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ctx =
3931c6475a0bSAviad Yehezkel 		mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
3932c6475a0bSAviad Yehezkel 	if (IS_ERR(action->esp_aes_gcm.ctx)) {
3933c6475a0bSAviad Yehezkel 		err = PTR_ERR(action->esp_aes_gcm.ctx);
3934c6475a0bSAviad Yehezkel 		goto err_parse;
3935c6475a0bSAviad Yehezkel 	}
3936c6475a0bSAviad Yehezkel 
3937c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
3938c6475a0bSAviad Yehezkel 
3939c6475a0bSAviad Yehezkel 	return &action->ib_action;
3940c6475a0bSAviad Yehezkel 
3941c6475a0bSAviad Yehezkel err_parse:
3942c6475a0bSAviad Yehezkel 	kfree(action);
3943c6475a0bSAviad Yehezkel 	return ERR_PTR(err);
3944c6475a0bSAviad Yehezkel }
3945c6475a0bSAviad Yehezkel 
3946349705c1SMatan Barak static int
3947349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
3948349705c1SMatan Barak 			       const struct ib_flow_action_attrs_esp *attr,
3949349705c1SMatan Barak 			       struct uverbs_attr_bundle *attrs)
3950349705c1SMatan Barak {
3951349705c1SMatan Barak 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
3952349705c1SMatan Barak 	struct mlx5_accel_esp_xfrm_attrs accel_attrs;
3953349705c1SMatan Barak 	int err = 0;
3954349705c1SMatan Barak 
3955349705c1SMatan Barak 	if (attr->keymat || attr->replay || attr->encap ||
3956349705c1SMatan Barak 	    attr->spi || attr->seq || attr->tfc_pad ||
3957349705c1SMatan Barak 	    attr->hard_limit_pkts ||
3958349705c1SMatan Barak 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3959349705c1SMatan Barak 			     IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
3960349705c1SMatan Barak 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
3961349705c1SMatan Barak 		return -EOPNOTSUPP;
3962349705c1SMatan Barak 
3963349705c1SMatan Barak 	/* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
3964349705c1SMatan Barak 	 * be modified.
3965349705c1SMatan Barak 	 */
3966349705c1SMatan Barak 	if (!(maction->esp_aes_gcm.ib_flags &
3967349705c1SMatan Barak 	      IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
3968349705c1SMatan Barak 	    attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3969349705c1SMatan Barak 			   IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
3970349705c1SMatan Barak 		return -EINVAL;
3971349705c1SMatan Barak 
3972349705c1SMatan Barak 	memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
3973349705c1SMatan Barak 	       sizeof(accel_attrs));
3974349705c1SMatan Barak 
3975349705c1SMatan Barak 	accel_attrs.esn = attr->esn;
3976349705c1SMatan Barak 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
3977349705c1SMatan Barak 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3978349705c1SMatan Barak 	else
3979349705c1SMatan Barak 		accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3980349705c1SMatan Barak 
3981349705c1SMatan Barak 	err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
3982349705c1SMatan Barak 					 &accel_attrs);
3983349705c1SMatan Barak 	if (err)
3984349705c1SMatan Barak 		return err;
3985349705c1SMatan Barak 
3986349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags &=
3987349705c1SMatan Barak 		~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
3988349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags |=
3989349705c1SMatan Barak 		attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
3990349705c1SMatan Barak 
3991349705c1SMatan Barak 	return 0;
3992349705c1SMatan Barak }
3993349705c1SMatan Barak 
3994c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
3995c6475a0bSAviad Yehezkel {
3996c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
3997c6475a0bSAviad Yehezkel 
3998c6475a0bSAviad Yehezkel 	switch (action->type) {
3999c6475a0bSAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
4000c6475a0bSAviad Yehezkel 		/*
4001c6475a0bSAviad Yehezkel 		 * We only support aes_gcm by now, so we implicitly know this is
4002c6475a0bSAviad Yehezkel 		 * the underline crypto.
4003c6475a0bSAviad Yehezkel 		 */
4004c6475a0bSAviad Yehezkel 		mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4005c6475a0bSAviad Yehezkel 		break;
4006c6475a0bSAviad Yehezkel 	default:
4007c6475a0bSAviad Yehezkel 		WARN_ON(true);
4008c6475a0bSAviad Yehezkel 		break;
4009c6475a0bSAviad Yehezkel 	}
4010c6475a0bSAviad Yehezkel 
4011c6475a0bSAviad Yehezkel 	kfree(maction);
4012c6475a0bSAviad Yehezkel 	return 0;
4013c6475a0bSAviad Yehezkel }
4014c6475a0bSAviad Yehezkel 
4015e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4016e126ba97SEli Cohen {
4017e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
401881e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
4019e126ba97SEli Cohen 	int err;
4020e126ba97SEli Cohen 
402181e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
402281e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
402381e30880SYishai Hadas 		return -EOPNOTSUPP;
402481e30880SYishai Hadas 	}
402581e30880SYishai Hadas 
40269603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
4027e126ba97SEli Cohen 	if (err)
4028e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4029e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4030e126ba97SEli Cohen 
4031e126ba97SEli Cohen 	return err;
4032e126ba97SEli Cohen }
4033e126ba97SEli Cohen 
4034e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4035e126ba97SEli Cohen {
4036e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4037e126ba97SEli Cohen 	int err;
4038e126ba97SEli Cohen 
40399603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
4040e126ba97SEli Cohen 	if (err)
4041e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4042e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4043e126ba97SEli Cohen 
4044e126ba97SEli Cohen 	return err;
4045e126ba97SEli Cohen }
4046e126ba97SEli Cohen 
4047e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
4048e126ba97SEli Cohen {
40491b5daf11SMajd Dibbiny 	int err;
4050e126ba97SEli Cohen 
40511b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
4052e126ba97SEli Cohen 	if (err)
4053e126ba97SEli Cohen 		return err;
40541b5daf11SMajd Dibbiny 
40551b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
40561b5daf11SMajd Dibbiny 
40571b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
4058e126ba97SEli Cohen }
4059e126ba97SEli Cohen 
4060e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
4061e126ba97SEli Cohen 			     char *buf)
4062e126ba97SEli Cohen {
4063e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4064e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4065e126ba97SEli Cohen 
40669603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
4067e126ba97SEli Cohen }
4068e126ba97SEli Cohen 
4069e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
4070e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
4071e126ba97SEli Cohen {
4072e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4073e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4074e126ba97SEli Cohen 
40756aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
4076e126ba97SEli Cohen }
4077e126ba97SEli Cohen 
4078e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
4079e126ba97SEli Cohen 			char *buf)
4080e126ba97SEli Cohen {
4081e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4082e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
40839603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
4084e126ba97SEli Cohen }
4085e126ba97SEli Cohen 
4086e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
4087e126ba97SEli Cohen 			char *buf)
4088e126ba97SEli Cohen {
4089e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4090e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
40919603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
4092e126ba97SEli Cohen }
4093e126ba97SEli Cohen 
4094e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
4095e126ba97SEli Cohen 			  char *buf)
4096e126ba97SEli Cohen {
4097e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
4098e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4099e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
41009603b61dSJack Morgenstein 		       dev->mdev->board_id);
4101e126ba97SEli Cohen }
4102e126ba97SEli Cohen 
4103e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
4104e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
4105e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
4106e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
4107e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
4108e126ba97SEli Cohen 
4109e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
4110e126ba97SEli Cohen 	&dev_attr_hw_rev,
4111e126ba97SEli Cohen 	&dev_attr_hca_type,
4112e126ba97SEli Cohen 	&dev_attr_board_id,
4113e126ba97SEli Cohen 	&dev_attr_fw_pages,
4114e126ba97SEli Cohen 	&dev_attr_reg_pages,
4115e126ba97SEli Cohen };
4116e126ba97SEli Cohen 
41177722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
41187722f47eSHaggai Eran {
41197722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
41207722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
41217722f47eSHaggai Eran 			     pkey_change_work);
41227722f47eSHaggai Eran 
41237722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
41247722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
41257722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
41267722f47eSHaggai Eran }
41277722f47eSHaggai Eran 
412889ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
412989ea94a7SMaor Gottlieb {
413089ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
413189ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
413289ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
413389ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
413489ea94a7SMaor Gottlieb 	unsigned long flags_qp;
413589ea94a7SMaor Gottlieb 	unsigned long flags_cq;
413689ea94a7SMaor Gottlieb 	unsigned long flags;
413789ea94a7SMaor Gottlieb 
413889ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
413989ea94a7SMaor Gottlieb 
414089ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
414189ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
414289ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
414389ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
414489ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
414589ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
414689ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
414789ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
414889ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
414989ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
415089ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
415189ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
415289ea94a7SMaor Gottlieb 						      &cq_armed_list);
415389ea94a7SMaor Gottlieb 				}
415489ea94a7SMaor Gottlieb 			}
415589ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
415689ea94a7SMaor Gottlieb 		}
415789ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
415889ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
415989ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
416089ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
416189ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
416289ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
416389ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
416489ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
416589ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
416689ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
416789ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
416889ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
416989ea94a7SMaor Gottlieb 							      &cq_armed_list);
417089ea94a7SMaor Gottlieb 					}
417189ea94a7SMaor Gottlieb 				}
417289ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
417389ea94a7SMaor Gottlieb 						       flags_cq);
417489ea94a7SMaor Gottlieb 			}
417589ea94a7SMaor Gottlieb 		}
417689ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
417789ea94a7SMaor Gottlieb 	}
417889ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
417989ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
418089ea94a7SMaor Gottlieb 	 */
418189ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
418289ea94a7SMaor Gottlieb 		mcq->comp(mcq);
418389ea94a7SMaor Gottlieb 	}
418489ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
418589ea94a7SMaor Gottlieb }
418689ea94a7SMaor Gottlieb 
418703404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
418803404e8aSMaor Gottlieb {
418903404e8aSMaor Gottlieb 	int err;
419003404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
419103404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
419203404e8aSMaor Gottlieb 			     delay_drop_work);
419303404e8aSMaor Gottlieb 
4194fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
4195fe248c3aSMaor Gottlieb 
419603404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
419703404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
419803404e8aSMaor Gottlieb 				       delay_drop->timeout);
419903404e8aSMaor Gottlieb 	if (err) {
420003404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
420103404e8aSMaor Gottlieb 			     delay_drop->timeout);
420203404e8aSMaor Gottlieb 		delay_drop->activate = false;
420303404e8aSMaor Gottlieb 	}
420403404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
420503404e8aSMaor Gottlieb }
420603404e8aSMaor Gottlieb 
4207d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
4208e126ba97SEli Cohen {
4209d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
4210d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
4211d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
4212e126ba97SEli Cohen 	struct ib_event ibev;
4213dbaaff2aSEli Cohen 	bool fatal = false;
4214aba46213SDaniel Jurgens 	u8 port = (u8)work->param;
4215e126ba97SEli Cohen 
4216d69a24e0SDaniel Jurgens 	if (mlx5_core_is_mp_slave(work->dev)) {
4217d69a24e0SDaniel Jurgens 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->context);
4218d69a24e0SDaniel Jurgens 		if (!ibdev)
4219d69a24e0SDaniel Jurgens 			goto out;
4220d69a24e0SDaniel Jurgens 	} else {
4221d69a24e0SDaniel Jurgens 		ibdev = work->context;
4222d69a24e0SDaniel Jurgens 	}
4223d69a24e0SDaniel Jurgens 
4224d69a24e0SDaniel Jurgens 	switch (work->event) {
4225e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
4226e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
422789ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
4228dbaaff2aSEli Cohen 		fatal = true;
4229e126ba97SEli Cohen 		break;
4230e126ba97SEli Cohen 
4231e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
4232e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
42332788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
42345ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
42355ec8c83eSAviv Heller 		 * mlx5_netdev_event().
42365ec8c83eSAviv Heller 		 */
42375ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
42385ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
4239d69a24e0SDaniel Jurgens 			goto out;
42405ec8c83eSAviv Heller 
4241d69a24e0SDaniel Jurgens 		ibev.event = (work->event == MLX5_DEV_EVENT_PORT_UP) ?
42425ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4243e126ba97SEli Cohen 		break;
4244e126ba97SEli Cohen 
4245e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
4246e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
4247e126ba97SEli Cohen 		break;
4248e126ba97SEli Cohen 
4249e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
4250e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
42517722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4252e126ba97SEli Cohen 		break;
4253e126ba97SEli Cohen 
4254e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
4255e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
4256e126ba97SEli Cohen 		break;
4257e126ba97SEli Cohen 
4258e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
4259e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
4260e126ba97SEli Cohen 		break;
426103404e8aSMaor Gottlieb 	case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
426203404e8aSMaor Gottlieb 		schedule_work(&ibdev->delay_drop.delay_drop_work);
426303404e8aSMaor Gottlieb 		goto out;
4264bdc37924SSaeed Mahameed 	default:
426503404e8aSMaor Gottlieb 		goto out;
4266e126ba97SEli Cohen 	}
4267e126ba97SEli Cohen 
4268e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
4269e126ba97SEli Cohen 	ibev.element.port_num = port;
4270e126ba97SEli Cohen 
4271aba46213SDaniel Jurgens 	if (!rdma_is_port_valid(&ibdev->ib_dev, port)) {
4272a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
427303404e8aSMaor Gottlieb 		goto out;
4274a0c84c32SEli Cohen 	}
4275a0c84c32SEli Cohen 
4276e126ba97SEli Cohen 	if (ibdev->ib_active)
4277e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
4278dbaaff2aSEli Cohen 
4279dbaaff2aSEli Cohen 	if (fatal)
4280dbaaff2aSEli Cohen 		ibdev->ib_active = false;
428103404e8aSMaor Gottlieb out:
4282d69a24e0SDaniel Jurgens 	kfree(work);
4283d69a24e0SDaniel Jurgens }
4284d69a24e0SDaniel Jurgens 
4285d69a24e0SDaniel Jurgens static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
4286d69a24e0SDaniel Jurgens 			  enum mlx5_dev_event event, unsigned long param)
4287d69a24e0SDaniel Jurgens {
4288d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
4289d69a24e0SDaniel Jurgens 
4290d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
429110bea9c8SLeon Romanovsky 	if (!work)
429210bea9c8SLeon Romanovsky 		return;
429310bea9c8SLeon Romanovsky 
4294d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4295d69a24e0SDaniel Jurgens 	work->dev = dev;
4296d69a24e0SDaniel Jurgens 	work->param = param;
4297d69a24e0SDaniel Jurgens 	work->context = context;
4298d69a24e0SDaniel Jurgens 	work->event = event;
4299d69a24e0SDaniel Jurgens 
4300d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
4301d69a24e0SDaniel Jurgens }
4302d69a24e0SDaniel Jurgens 
4303c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4304c43f1112SMaor Gottlieb {
4305c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
4306c43f1112SMaor Gottlieb 	int err;
4307c43f1112SMaor Gottlieb 	int port;
4308c43f1112SMaor Gottlieb 
4309508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++) {
4310c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
4311c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4312c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
4313c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4314c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
4315c43f1112SMaor Gottlieb 								   port, 0,
4316c43f1112SMaor Gottlieb 								   &vport_ctx);
4317c43f1112SMaor Gottlieb 				if (err) {
4318c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4319c43f1112SMaor Gottlieb 						    port, err);
4320c43f1112SMaor Gottlieb 					return err;
4321c43f1112SMaor Gottlieb 				}
4322c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
4323c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
4324c43f1112SMaor Gottlieb 			} else {
4325c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
4326c43f1112SMaor Gottlieb 			}
4327c43f1112SMaor Gottlieb 		}
4328c43f1112SMaor Gottlieb 	}
4329c43f1112SMaor Gottlieb 	return 0;
4330c43f1112SMaor Gottlieb }
4331c43f1112SMaor Gottlieb 
4332e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4333e126ba97SEli Cohen {
4334e126ba97SEli Cohen 	int port;
4335e126ba97SEli Cohen 
4336508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
4337e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
4338e126ba97SEli Cohen }
4339e126ba97SEli Cohen 
434032f69e4bSDaniel Jurgens static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4341e126ba97SEli Cohen {
4342e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
4343e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
4344f614fc15SDan Carpenter 	int err = -ENOMEM;
43452528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
4346e126ba97SEli Cohen 
4347e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
4348e126ba97SEli Cohen 	if (!pprops)
4349e126ba97SEli Cohen 		goto out;
4350e126ba97SEli Cohen 
4351e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4352e126ba97SEli Cohen 	if (!dprops)
4353e126ba97SEli Cohen 		goto out;
4354e126ba97SEli Cohen 
4355c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
4356c43f1112SMaor Gottlieb 	if (err)
4357c43f1112SMaor Gottlieb 		goto out;
4358c43f1112SMaor Gottlieb 
43592528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
4360e126ba97SEli Cohen 	if (err) {
4361e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
4362e126ba97SEli Cohen 		goto out;
4363e126ba97SEli Cohen 	}
4364e126ba97SEli Cohen 
4365c4550c63SOr Gerlitz 	memset(pprops, 0, sizeof(*pprops));
4366e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4367e126ba97SEli Cohen 	if (err) {
4368938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
4369938fe83cSSaeed Mahameed 			     port, err);
437032f69e4bSDaniel Jurgens 		goto out;
4371e126ba97SEli Cohen 	}
437232f69e4bSDaniel Jurgens 
4373938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
4374938fe83cSSaeed Mahameed 					dprops->max_pkeys;
4375938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
4376938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
437732f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
437832f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
4379e126ba97SEli Cohen 
4380e126ba97SEli Cohen out:
4381e126ba97SEli Cohen 	kfree(pprops);
4382e126ba97SEli Cohen 	kfree(dprops);
4383e126ba97SEli Cohen 
4384e126ba97SEli Cohen 	return err;
4385e126ba97SEli Cohen }
4386e126ba97SEli Cohen 
4387e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4388e126ba97SEli Cohen {
4389e126ba97SEli Cohen 	int err;
4390e126ba97SEli Cohen 
4391e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
4392e126ba97SEli Cohen 	if (err)
4393e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4394e126ba97SEli Cohen 
439532927e28SMark Bloch 	if (dev->umrc.qp)
4396e126ba97SEli Cohen 		mlx5_ib_destroy_qp(dev->umrc.qp);
439732927e28SMark Bloch 	if (dev->umrc.cq)
4398add08d76SChristoph Hellwig 		ib_free_cq(dev->umrc.cq);
439932927e28SMark Bloch 	if (dev->umrc.pd)
4400e126ba97SEli Cohen 		ib_dealloc_pd(dev->umrc.pd);
4401e126ba97SEli Cohen }
4402e126ba97SEli Cohen 
4403e126ba97SEli Cohen enum {
4404e126ba97SEli Cohen 	MAX_UMR_WR = 128,
4405e126ba97SEli Cohen };
4406e126ba97SEli Cohen 
4407e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
4408e126ba97SEli Cohen {
4409e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
4410e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
4411e126ba97SEli Cohen 	struct ib_pd *pd;
4412e126ba97SEli Cohen 	struct ib_cq *cq;
4413e126ba97SEli Cohen 	struct ib_qp *qp;
4414e126ba97SEli Cohen 	int ret;
4415e126ba97SEli Cohen 
4416e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4417e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4418e126ba97SEli Cohen 	if (!attr || !init_attr) {
4419e126ba97SEli Cohen 		ret = -ENOMEM;
4420e126ba97SEli Cohen 		goto error_0;
4421e126ba97SEli Cohen 	}
4422e126ba97SEli Cohen 
4423ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
4424e126ba97SEli Cohen 	if (IS_ERR(pd)) {
4425e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4426e126ba97SEli Cohen 		ret = PTR_ERR(pd);
4427e126ba97SEli Cohen 		goto error_0;
4428e126ba97SEli Cohen 	}
4429e126ba97SEli Cohen 
4430add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
4431e126ba97SEli Cohen 	if (IS_ERR(cq)) {
4432e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4433e126ba97SEli Cohen 		ret = PTR_ERR(cq);
4434e126ba97SEli Cohen 		goto error_2;
4435e126ba97SEli Cohen 	}
4436e126ba97SEli Cohen 
4437e126ba97SEli Cohen 	init_attr->send_cq = cq;
4438e126ba97SEli Cohen 	init_attr->recv_cq = cq;
4439e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4440e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
4441e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
4442e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4443e126ba97SEli Cohen 	init_attr->port_num = 1;
4444e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4445e126ba97SEli Cohen 	if (IS_ERR(qp)) {
4446e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4447e126ba97SEli Cohen 		ret = PTR_ERR(qp);
4448e126ba97SEli Cohen 		goto error_3;
4449e126ba97SEli Cohen 	}
4450e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
4451e126ba97SEli Cohen 	qp->real_qp    = qp;
4452e126ba97SEli Cohen 	qp->uobject    = NULL;
4453e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
445431fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
445531fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
4456e126ba97SEli Cohen 
4457e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
4458e126ba97SEli Cohen 	attr->port_num = 1;
4459e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4460e126ba97SEli Cohen 				IB_QP_PORT, NULL);
4461e126ba97SEli Cohen 	if (ret) {
4462e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4463e126ba97SEli Cohen 		goto error_4;
4464e126ba97SEli Cohen 	}
4465e126ba97SEli Cohen 
4466e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4467e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
4468e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
4469e126ba97SEli Cohen 
4470e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4471e126ba97SEli Cohen 	if (ret) {
4472e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4473e126ba97SEli Cohen 		goto error_4;
4474e126ba97SEli Cohen 	}
4475e126ba97SEli Cohen 
4476e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4477e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
4478e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4479e126ba97SEli Cohen 	if (ret) {
4480e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4481e126ba97SEli Cohen 		goto error_4;
4482e126ba97SEli Cohen 	}
4483e126ba97SEli Cohen 
4484e126ba97SEli Cohen 	dev->umrc.qp = qp;
4485e126ba97SEli Cohen 	dev->umrc.cq = cq;
4486e126ba97SEli Cohen 	dev->umrc.pd = pd;
4487e126ba97SEli Cohen 
4488e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
4489e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
4490e126ba97SEli Cohen 	if (ret) {
4491e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4492e126ba97SEli Cohen 		goto error_4;
4493e126ba97SEli Cohen 	}
4494e126ba97SEli Cohen 
4495e126ba97SEli Cohen 	kfree(attr);
4496e126ba97SEli Cohen 	kfree(init_attr);
4497e126ba97SEli Cohen 
4498e126ba97SEli Cohen 	return 0;
4499e126ba97SEli Cohen 
4500e126ba97SEli Cohen error_4:
4501e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
450232927e28SMark Bloch 	dev->umrc.qp = NULL;
4503e126ba97SEli Cohen 
4504e126ba97SEli Cohen error_3:
4505add08d76SChristoph Hellwig 	ib_free_cq(cq);
450632927e28SMark Bloch 	dev->umrc.cq = NULL;
4507e126ba97SEli Cohen 
4508e126ba97SEli Cohen error_2:
4509e126ba97SEli Cohen 	ib_dealloc_pd(pd);
451032927e28SMark Bloch 	dev->umrc.pd = NULL;
4511e126ba97SEli Cohen 
4512e126ba97SEli Cohen error_0:
4513e126ba97SEli Cohen 	kfree(attr);
4514e126ba97SEli Cohen 	kfree(init_attr);
4515e126ba97SEli Cohen 	return ret;
4516e126ba97SEli Cohen }
4517e126ba97SEli Cohen 
45186e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
45196e8484c5SMax Gurtovoy {
45206e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
45216e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
45226e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
45236e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
45246e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
45256e8484c5SMax Gurtovoy 	default:
45266e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
45276e8484c5SMax Gurtovoy 	}
45286e8484c5SMax Gurtovoy }
45296e8484c5SMax Gurtovoy 
4530e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
4531e126ba97SEli Cohen {
4532e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
4533e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
4534bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
45357722f47eSHaggai Eran 	int port;
4536e126ba97SEli Cohen 	int ret = 0;
4537e126ba97SEli Cohen 
4538e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
4539e126ba97SEli Cohen 
4540d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
4541d16e91daSHaggai Eran 
4542e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
4543e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
4544e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
4545e126ba97SEli Cohen 		goto error0;
4546e126ba97SEli Cohen 	}
4547e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
4548e126ba97SEli Cohen 	devr->p0->uobject = NULL;
4549e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
4550e126ba97SEli Cohen 
4551bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
4552e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
4553e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
4554e126ba97SEli Cohen 		goto error1;
4555e126ba97SEli Cohen 	}
4556e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
4557e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
4558e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
4559e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
4560e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
4561e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
4562e126ba97SEli Cohen 
4563e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4564e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
4565e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
4566e126ba97SEli Cohen 		goto error2;
4567e126ba97SEli Cohen 	}
4568e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
4569e126ba97SEli Cohen 	devr->x0->inode = NULL;
4570e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
4571e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
4572e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4573e126ba97SEli Cohen 
4574e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4575e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
4576e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
4577e126ba97SEli Cohen 		goto error3;
4578e126ba97SEli Cohen 	}
4579e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
4580e126ba97SEli Cohen 	devr->x1->inode = NULL;
4581e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
4582e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
4583e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4584e126ba97SEli Cohen 
4585e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
4586e126ba97SEli Cohen 	attr.attr.max_sge = 1;
4587e126ba97SEli Cohen 	attr.attr.max_wr = 1;
4588e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
45891a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
4590e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
4591e126ba97SEli Cohen 
4592e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4593e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
4594e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
4595e126ba97SEli Cohen 		goto error4;
4596e126ba97SEli Cohen 	}
4597e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
4598e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
4599e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
4600e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
4601e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
4602e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
4603e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
46041a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
4605e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
46061a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
4607e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
4608e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
4609e126ba97SEli Cohen 
46104aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
46114aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
46124aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
46134aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
46144aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
46154aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
46164aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
46174aa17b28SHaggai Abramonvsky 		goto error5;
46184aa17b28SHaggai Abramonvsky 	}
46194aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
46204aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
46214aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
46224aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
46234aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
46244aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
46251a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
46264aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
46271a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
46284aa17b28SHaggai Abramonvsky 
46297722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
46307722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
46317722f47eSHaggai Eran 			  pkey_change_handler);
46327722f47eSHaggai Eran 		devr->ports[port].devr = devr;
46337722f47eSHaggai Eran 	}
46347722f47eSHaggai Eran 
4635e126ba97SEli Cohen 	return 0;
4636e126ba97SEli Cohen 
46374aa17b28SHaggai Abramonvsky error5:
46384aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
4639e126ba97SEli Cohen error4:
4640e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
4641e126ba97SEli Cohen error3:
4642e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
4643e126ba97SEli Cohen error2:
4644e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
4645e126ba97SEli Cohen error1:
4646e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
4647e126ba97SEli Cohen error0:
4648e126ba97SEli Cohen 	return ret;
4649e126ba97SEli Cohen }
4650e126ba97SEli Cohen 
4651e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
4652e126ba97SEli Cohen {
46537722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
46547722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
46557722f47eSHaggai Eran 	int port;
46567722f47eSHaggai Eran 
46574aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
4658e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
4659e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
4660e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
4661e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
4662e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
46637722f47eSHaggai Eran 
46647722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
46657722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
46667722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
4667e126ba97SEli Cohen }
4668e126ba97SEli Cohen 
4669b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
4670b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
4671e53505a8SAchiad Shochat {
4672e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4673e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
4674e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
4675e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
467685c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
4677e53505a8SAchiad Shochat 	u32 ret = 0;
4678e53505a8SAchiad Shochat 
4679b02289b3SArtemy Kovalyov 	if (rep->grh_required)
4680b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
4681b02289b3SArtemy Kovalyov 
4682e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
4683b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
4684e53505a8SAchiad Shochat 
468585c7c014SDaniel Jurgens 	if (raw_support)
4686b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
468772cd5717SOr Gerlitz 
4688e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
468972cd5717SOr Gerlitz 		return ret;
4690e53505a8SAchiad Shochat 
4691e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
469272cd5717SOr Gerlitz 		return ret;
4693e53505a8SAchiad Shochat 
4694e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
4695e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
4696e53505a8SAchiad Shochat 
4697e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
4698e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
4699e53505a8SAchiad Shochat 
4700e53505a8SAchiad Shochat 	return ret;
4701e53505a8SAchiad Shochat }
4702e53505a8SAchiad Shochat 
47037738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
47047738613eSIra Weiny 			       struct ib_port_immutable *immutable)
47057738613eSIra Weiny {
47067738613eSIra Weiny 	struct ib_port_attr attr;
4707ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4708ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
4709b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
47107738613eSIra Weiny 	int err;
47117738613eSIra Weiny 
4712c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
47137738613eSIra Weiny 	if (err)
47147738613eSIra Weiny 		return err;
47157738613eSIra Weiny 
4716b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
4717b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
4718b02289b3SArtemy Kovalyov 						   &rep);
4719b02289b3SArtemy Kovalyov 		if (err)
4720b02289b3SArtemy Kovalyov 			return err;
4721b02289b3SArtemy Kovalyov 	}
4722b02289b3SArtemy Kovalyov 
47237738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
47247738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
4725b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
4726ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
4727337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
47287738613eSIra Weiny 
47297738613eSIra Weiny 	return 0;
47307738613eSIra Weiny }
47317738613eSIra Weiny 
47328e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
47338e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
47348e6efa3aSMark Bloch {
47358e6efa3aSMark Bloch 	struct ib_port_attr attr;
47368e6efa3aSMark Bloch 	int err;
47378e6efa3aSMark Bloch 
47388e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
47398e6efa3aSMark Bloch 
47408e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
47418e6efa3aSMark Bloch 	if (err)
47428e6efa3aSMark Bloch 		return err;
47438e6efa3aSMark Bloch 
47448e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
47458e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
47468e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
47478e6efa3aSMark Bloch 
47488e6efa3aSMark Bloch 	return 0;
47498e6efa3aSMark Bloch }
47508e6efa3aSMark Bloch 
47519abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
4752c7342823SIra Weiny {
4753c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
4754c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
47559abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
47569abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
47579abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
4758c7342823SIra Weiny }
4759c7342823SIra Weiny 
476045f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
47619ef9c640SAviv Heller {
47629ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
47639ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
47649ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
47659ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
47669ef9c640SAviv Heller 	int err;
47679ef9c640SAviv Heller 
47689ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
47699ef9c640SAviv Heller 		return 0;
47709ef9c640SAviv Heller 
47719ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
47729ef9c640SAviv Heller 	if (err)
47739ef9c640SAviv Heller 		return err;
47749ef9c640SAviv Heller 
47759ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
47769ef9c640SAviv Heller 	if (IS_ERR(ft)) {
47779ef9c640SAviv Heller 		err = PTR_ERR(ft);
47789ef9c640SAviv Heller 		goto err_destroy_vport_lag;
47799ef9c640SAviv Heller 	}
47809ef9c640SAviv Heller 
47819a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
47829ef9c640SAviv Heller 	return 0;
47839ef9c640SAviv Heller 
47849ef9c640SAviv Heller err_destroy_vport_lag:
47859ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
47869ef9c640SAviv Heller 	return err;
47879ef9c640SAviv Heller }
47889ef9c640SAviv Heller 
478945f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
47909ef9c640SAviv Heller {
47919ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
47929ef9c640SAviv Heller 
47939a4ca38dSMark Bloch 	if (dev->flow_db->lag_demux_ft) {
47949a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
47959a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
47969ef9c640SAviv Heller 
47979ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
47989ef9c640SAviv Heller 	}
47999ef9c640SAviv Heller }
48009ef9c640SAviv Heller 
48017fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
4802fc24fc5eSAchiad Shochat {
4803e53505a8SAchiad Shochat 	int err;
4804e53505a8SAchiad Shochat 
48057fd8aefbSDaniel Jurgens 	dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
48067fd8aefbSDaniel Jurgens 	err = register_netdevice_notifier(&dev->roce[port_num].nb);
48075ec8c83eSAviv Heller 	if (err) {
48087fd8aefbSDaniel Jurgens 		dev->roce[port_num].nb.notifier_call = NULL;
4809e53505a8SAchiad Shochat 		return err;
48105ec8c83eSAviv Heller 	}
4811e53505a8SAchiad Shochat 
4812d012f5d6SOr Gerlitz 	return 0;
4813d012f5d6SOr Gerlitz }
4814d012f5d6SOr Gerlitz 
48157fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
4816e126ba97SEli Cohen {
48177fd8aefbSDaniel Jurgens 	if (dev->roce[port_num].nb.notifier_call) {
48187fd8aefbSDaniel Jurgens 		unregister_netdevice_notifier(&dev->roce[port_num].nb);
48197fd8aefbSDaniel Jurgens 		dev->roce[port_num].nb.notifier_call = NULL;
4820e126ba97SEli Cohen 	}
4821e126ba97SEli Cohen }
4822e126ba97SEli Cohen 
4823e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
4824e126ba97SEli Cohen {
4825e126ba97SEli Cohen 	int err;
4826e126ba97SEli Cohen 
4827ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
4828e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
4829e53505a8SAchiad Shochat 		if (err)
48308e6efa3aSMark Bloch 			return err;
4831ca5b91d6SOr Gerlitz 	}
4832e53505a8SAchiad Shochat 
483345f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
48349ef9c640SAviv Heller 	if (err)
48359ef9c640SAviv Heller 		goto err_disable_roce;
48369ef9c640SAviv Heller 
4837e53505a8SAchiad Shochat 	return 0;
4838e53505a8SAchiad Shochat 
48399ef9c640SAviv Heller err_disable_roce:
4840ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
48419ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
48429ef9c640SAviv Heller 
4843e53505a8SAchiad Shochat 	return err;
4844fc24fc5eSAchiad Shochat }
4845fc24fc5eSAchiad Shochat 
484645f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
4847fc24fc5eSAchiad Shochat {
484845f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
4849ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
4850e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
4851fc24fc5eSAchiad Shochat }
4852fc24fc5eSAchiad Shochat 
4853e1f24a79SParav Pandit struct mlx5_ib_counter {
48547c16f477SKamal Heib 	const char *name;
48557c16f477SKamal Heib 	size_t offset;
48567c16f477SKamal Heib };
48577c16f477SKamal Heib 
48587c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
48597c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
48607c16f477SKamal Heib 
4861e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
48627c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
48637c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
48647c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
48657c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
48667c16f477SKamal Heib };
48677c16f477SKamal Heib 
4868e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
48697c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
48707c16f477SKamal Heib };
48717c16f477SKamal Heib 
4872e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
48737c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
48747c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
48757c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
48767c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
48777c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
48787c16f477SKamal Heib };
48797c16f477SKamal Heib 
4880e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
4881e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
4882e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
4883e1f24a79SParav Pandit 
4884e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
4885e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
4886e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
4887e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
4888e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
4889e1f24a79SParav Pandit };
4890e1f24a79SParav Pandit 
489158dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
489258dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
489358dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
489458dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
489558dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
489658dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
489758dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
489858dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
489958dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
490058dcb60aSParav Pandit };
490158dcb60aSParav Pandit 
49029f876f3dSTalat Batheesh #define INIT_EXT_PPCNT_COUNTER(_name)		\
49039f876f3dSTalat Batheesh 	{ .name = #_name, .offset =	\
49049f876f3dSTalat Batheesh 	MLX5_BYTE_OFF(ppcnt_reg, \
49059f876f3dSTalat Batheesh 		      counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
49069f876f3dSTalat Batheesh 
49079f876f3dSTalat Batheesh static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
49089f876f3dSTalat Batheesh 	INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
49099f876f3dSTalat Batheesh };
49109f876f3dSTalat Batheesh 
4911e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
49120837e86aSMark Bloch {
4913aac4492eSDaniel Jurgens 	int i;
49140837e86aSMark Bloch 
49157c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
4916921c0f5bSParav Pandit 		if (dev->port[i].cnts.set_id_valid)
49170837e86aSMark Bloch 			mlx5_core_dealloc_q_counter(dev->mdev,
4918e1f24a79SParav Pandit 						    dev->port[i].cnts.set_id);
4919e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
4920e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
49217c16f477SKamal Heib 	}
49227c16f477SKamal Heib }
49237c16f477SKamal Heib 
4924e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
4925e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
49267c16f477SKamal Heib {
49277c16f477SKamal Heib 	u32 num_counters;
49287c16f477SKamal Heib 
49297c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
49307c16f477SKamal Heib 
49317c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
49327c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
49337c16f477SKamal Heib 
49347c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
49357c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
493658dcb60aSParav Pandit 
493758dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
493858dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
493958dcb60aSParav Pandit 
4940e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
49417c16f477SKamal Heib 
4942e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4943e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
4944e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
4945e1f24a79SParav Pandit 	}
49469f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
49479f876f3dSTalat Batheesh 		cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
49489f876f3dSTalat Batheesh 		num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
49499f876f3dSTalat Batheesh 	}
4950e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
4951e1f24a79SParav Pandit 	if (!cnts->names)
49527c16f477SKamal Heib 		return -ENOMEM;
49537c16f477SKamal Heib 
4954e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
4955e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
4956e1f24a79SParav Pandit 	if (!cnts->offsets)
49577c16f477SKamal Heib 		goto err_names;
49587c16f477SKamal Heib 
49597c16f477SKamal Heib 	return 0;
49607c16f477SKamal Heib 
49617c16f477SKamal Heib err_names:
4962e1f24a79SParav Pandit 	kfree(cnts->names);
4963aac4492eSDaniel Jurgens 	cnts->names = NULL;
49647c16f477SKamal Heib 	return -ENOMEM;
49657c16f477SKamal Heib }
49667c16f477SKamal Heib 
4967e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
49687c16f477SKamal Heib 				  const char **names,
49697c16f477SKamal Heib 				  size_t *offsets)
49707c16f477SKamal Heib {
49717c16f477SKamal Heib 	int i;
49727c16f477SKamal Heib 	int j = 0;
49737c16f477SKamal Heib 
49747c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
49757c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
49767c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
49777c16f477SKamal Heib 	}
49787c16f477SKamal Heib 
49797c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
49807c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
49817c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
49827c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
49837c16f477SKamal Heib 		}
49847c16f477SKamal Heib 	}
49857c16f477SKamal Heib 
49867c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
49877c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
49887c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
49897c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
49907c16f477SKamal Heib 		}
49917c16f477SKamal Heib 	}
4992e1f24a79SParav Pandit 
499358dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
499458dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
499558dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
499658dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
499758dcb60aSParav Pandit 		}
499858dcb60aSParav Pandit 	}
499958dcb60aSParav Pandit 
5000e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5001e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5002e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
5003e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
5004e1f24a79SParav Pandit 		}
5005e1f24a79SParav Pandit 	}
50069f876f3dSTalat Batheesh 
50079f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
50089f876f3dSTalat Batheesh 		for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
50099f876f3dSTalat Batheesh 			names[j] = ext_ppcnt_cnts[i].name;
50109f876f3dSTalat Batheesh 			offsets[j] = ext_ppcnt_cnts[i].offset;
50119f876f3dSTalat Batheesh 		}
50129f876f3dSTalat Batheesh 	}
50130837e86aSMark Bloch }
50140837e86aSMark Bloch 
5015e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
50160837e86aSMark Bloch {
5017aac4492eSDaniel Jurgens 	int err = 0;
50180837e86aSMark Bloch 	int i;
50190837e86aSMark Bloch 
50200837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
5021aac4492eSDaniel Jurgens 		err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5022aac4492eSDaniel Jurgens 		if (err)
5023aac4492eSDaniel Jurgens 			goto err_alloc;
50247c16f477SKamal Heib 
5025aac4492eSDaniel Jurgens 		mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5026aac4492eSDaniel Jurgens 				      dev->port[i].cnts.offsets);
5027aac4492eSDaniel Jurgens 
5028aac4492eSDaniel Jurgens 		err = mlx5_core_alloc_q_counter(dev->mdev,
5029aac4492eSDaniel Jurgens 						&dev->port[i].cnts.set_id);
5030aac4492eSDaniel Jurgens 		if (err) {
50310837e86aSMark Bloch 			mlx5_ib_warn(dev,
50320837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
5033aac4492eSDaniel Jurgens 				     i + 1, err);
5034aac4492eSDaniel Jurgens 			goto err_alloc;
50350837e86aSMark Bloch 		}
5036aac4492eSDaniel Jurgens 		dev->port[i].cnts.set_id_valid = true;
50370837e86aSMark Bloch 	}
50380837e86aSMark Bloch 
50390837e86aSMark Bloch 	return 0;
50400837e86aSMark Bloch 
5041aac4492eSDaniel Jurgens err_alloc:
5042aac4492eSDaniel Jurgens 	mlx5_ib_dealloc_counters(dev);
5043aac4492eSDaniel Jurgens 	return err;
50440837e86aSMark Bloch }
50450837e86aSMark Bloch 
50460ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
50470ad17a8fSMark Bloch 						    u8 port_num)
50480ad17a8fSMark Bloch {
50497c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
50507c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
50510ad17a8fSMark Bloch 
50520ad17a8fSMark Bloch 	/* We support only per port stats */
50530ad17a8fSMark Bloch 	if (port_num == 0)
50540ad17a8fSMark Bloch 		return NULL;
50550ad17a8fSMark Bloch 
5056e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
5057e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
50589f876f3dSTalat Batheesh 					  port->cnts.num_cong_counters +
50599f876f3dSTalat Batheesh 					  port->cnts.num_ext_ppcnt_counters,
50600ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
50610ad17a8fSMark Bloch }
50620ad17a8fSMark Bloch 
5063aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
5064e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
5065e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
5066e1f24a79SParav Pandit {
5067e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5068e1f24a79SParav Pandit 	void *out;
5069e1f24a79SParav Pandit 	__be32 val;
5070e1f24a79SParav Pandit 	int ret, i;
5071e1f24a79SParav Pandit 
50721b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
5073e1f24a79SParav Pandit 	if (!out)
5074e1f24a79SParav Pandit 		return -ENOMEM;
5075e1f24a79SParav Pandit 
5076aac4492eSDaniel Jurgens 	ret = mlx5_core_query_q_counter(mdev,
5077e1f24a79SParav Pandit 					port->cnts.set_id, 0,
5078e1f24a79SParav Pandit 					out, outlen);
5079e1f24a79SParav Pandit 	if (ret)
5080e1f24a79SParav Pandit 		goto free;
5081e1f24a79SParav Pandit 
5082e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
5083e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
5084e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
5085e1f24a79SParav Pandit 	}
5086e1f24a79SParav Pandit 
5087e1f24a79SParav Pandit free:
5088e1f24a79SParav Pandit 	kvfree(out);
5089e1f24a79SParav Pandit 	return ret;
5090e1f24a79SParav Pandit }
5091e1f24a79SParav Pandit 
50929f876f3dSTalat Batheesh static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
50939f876f3dSTalat Batheesh 					  struct mlx5_ib_port *port,
50949f876f3dSTalat Batheesh 					  struct rdma_hw_stats *stats)
50959f876f3dSTalat Batheesh {
50969f876f3dSTalat Batheesh 	int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
50979f876f3dSTalat Batheesh 	int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
50989f876f3dSTalat Batheesh 	int ret, i;
50999f876f3dSTalat Batheesh 	void *out;
51009f876f3dSTalat Batheesh 
51019f876f3dSTalat Batheesh 	out = kvzalloc(sz, GFP_KERNEL);
51029f876f3dSTalat Batheesh 	if (!out)
51039f876f3dSTalat Batheesh 		return -ENOMEM;
51049f876f3dSTalat Batheesh 
51059f876f3dSTalat Batheesh 	ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
51069f876f3dSTalat Batheesh 	if (ret)
51079f876f3dSTalat Batheesh 		goto free;
51089f876f3dSTalat Batheesh 
51099f876f3dSTalat Batheesh 	for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
51109f876f3dSTalat Batheesh 		stats->value[i + offset] =
51119f876f3dSTalat Batheesh 			be64_to_cpup((__be64 *)(out +
51129f876f3dSTalat Batheesh 				    port->cnts.offsets[i + offset]));
51139f876f3dSTalat Batheesh 	}
51149f876f3dSTalat Batheesh 
51159f876f3dSTalat Batheesh free:
51169f876f3dSTalat Batheesh 	kvfree(out);
51179f876f3dSTalat Batheesh 	return ret;
51189f876f3dSTalat Batheesh }
51199f876f3dSTalat Batheesh 
51200ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
51210ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
51227c16f477SKamal Heib 				u8 port_num, int index)
51230ad17a8fSMark Bloch {
51240ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
51257c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
5126aac4492eSDaniel Jurgens 	struct mlx5_core_dev *mdev;
5127e1f24a79SParav Pandit 	int ret, num_counters;
5128aac4492eSDaniel Jurgens 	u8 mdev_port_num;
51290ad17a8fSMark Bloch 
51307c16f477SKamal Heib 	if (!stats)
5131e1f24a79SParav Pandit 		return -EINVAL;
51320ad17a8fSMark Bloch 
51339f876f3dSTalat Batheesh 	num_counters = port->cnts.num_q_counters +
51349f876f3dSTalat Batheesh 		       port->cnts.num_cong_counters +
51359f876f3dSTalat Batheesh 		       port->cnts.num_ext_ppcnt_counters;
5136aac4492eSDaniel Jurgens 
5137aac4492eSDaniel Jurgens 	/* q_counters are per IB device, query the master mdev */
5138aac4492eSDaniel Jurgens 	ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
51390ad17a8fSMark Bloch 	if (ret)
5140e1f24a79SParav Pandit 		return ret;
51410ad17a8fSMark Bloch 
51429f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
51439f876f3dSTalat Batheesh 		ret =  mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
51449f876f3dSTalat Batheesh 		if (ret)
51459f876f3dSTalat Batheesh 			return ret;
51469f876f3dSTalat Batheesh 	}
51479f876f3dSTalat Batheesh 
5148e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5149aac4492eSDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5150aac4492eSDaniel Jurgens 						    &mdev_port_num);
5151aac4492eSDaniel Jurgens 		if (!mdev) {
5152aac4492eSDaniel Jurgens 			/* If port is not affiliated yet, its in down state
5153aac4492eSDaniel Jurgens 			 * which doesn't have any counters yet, so it would be
5154aac4492eSDaniel Jurgens 			 * zero. So no need to read from the HCA.
5155aac4492eSDaniel Jurgens 			 */
5156aac4492eSDaniel Jurgens 			goto done;
5157aac4492eSDaniel Jurgens 		}
515871a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
515971a0ff65SMajd Dibbiny 						   stats->value +
516071a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters,
516171a0ff65SMajd Dibbiny 						   port->cnts.num_cong_counters,
516271a0ff65SMajd Dibbiny 						   port->cnts.offsets +
516371a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters);
5164aac4492eSDaniel Jurgens 
5165aac4492eSDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
5166e1f24a79SParav Pandit 		if (ret)
5167e1f24a79SParav Pandit 			return ret;
51680ad17a8fSMark Bloch 	}
51697c16f477SKamal Heib 
5170aac4492eSDaniel Jurgens done:
5171e1f24a79SParav Pandit 	return num_counters;
51720ad17a8fSMark Bloch }
51730ad17a8fSMark Bloch 
5174693dfd5aSErez Shitrit static struct net_device*
5175693dfd5aSErez Shitrit mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
5176693dfd5aSErez Shitrit 			  u8 port_num,
5177693dfd5aSErez Shitrit 			  enum rdma_netdev_t type,
5178693dfd5aSErez Shitrit 			  const char *name,
5179693dfd5aSErez Shitrit 			  unsigned char name_assign_type,
5180693dfd5aSErez Shitrit 			  void (*setup)(struct net_device *))
5181693dfd5aSErez Shitrit {
51828e959601SNiranjana Vishwanathapura 	struct net_device *netdev;
51838e959601SNiranjana Vishwanathapura 
5184693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
5185693dfd5aSErez Shitrit 		return ERR_PTR(-EOPNOTSUPP);
5186693dfd5aSErez Shitrit 
51878e959601SNiranjana Vishwanathapura 	netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
5188693dfd5aSErez Shitrit 					name, setup);
51898e959601SNiranjana Vishwanathapura 	return netdev;
5190693dfd5aSErez Shitrit }
5191693dfd5aSErez Shitrit 
5192fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5193fe248c3aSMaor Gottlieb {
5194fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
5195fe248c3aSMaor Gottlieb 		return;
5196fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5197fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
5198fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
5199fe248c3aSMaor Gottlieb }
5200fe248c3aSMaor Gottlieb 
520103404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
520203404e8aSMaor Gottlieb {
520303404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
520403404e8aSMaor Gottlieb 		return;
520503404e8aSMaor Gottlieb 
520603404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
5207fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5208fe248c3aSMaor Gottlieb }
5209fe248c3aSMaor Gottlieb 
5210fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5211fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
5212fe248c3aSMaor Gottlieb {
5213fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5214fe248c3aSMaor Gottlieb 	char lbuf[20];
5215fe248c3aSMaor Gottlieb 	int len;
5216fe248c3aSMaor Gottlieb 
5217fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5218fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
5219fe248c3aSMaor Gottlieb }
5220fe248c3aSMaor Gottlieb 
5221fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5222fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
5223fe248c3aSMaor Gottlieb {
5224fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5225fe248c3aSMaor Gottlieb 	u32 timeout;
5226fe248c3aSMaor Gottlieb 	u32 var;
5227fe248c3aSMaor Gottlieb 
5228fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
5229fe248c3aSMaor Gottlieb 		return -EFAULT;
5230fe248c3aSMaor Gottlieb 
5231fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5232fe248c3aSMaor Gottlieb 			1000);
5233fe248c3aSMaor Gottlieb 	if (timeout != var)
5234fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5235fe248c3aSMaor Gottlieb 			    timeout);
5236fe248c3aSMaor Gottlieb 
5237fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
5238fe248c3aSMaor Gottlieb 
5239fe248c3aSMaor Gottlieb 	return count;
5240fe248c3aSMaor Gottlieb }
5241fe248c3aSMaor Gottlieb 
5242fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
5243fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
5244fe248c3aSMaor Gottlieb 	.open	= simple_open,
5245fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
5246fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
5247fe248c3aSMaor Gottlieb };
5248fe248c3aSMaor Gottlieb 
5249fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5250fe248c3aSMaor Gottlieb {
5251fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
5252fe248c3aSMaor Gottlieb 
5253fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
5254fe248c3aSMaor Gottlieb 		return 0;
5255fe248c3aSMaor Gottlieb 
5256fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5257fe248c3aSMaor Gottlieb 	if (!dbg)
5258fe248c3aSMaor Gottlieb 		return -ENOMEM;
5259fe248c3aSMaor Gottlieb 
5260cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
5261cbafad87SSudip Mukherjee 
5262fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
5263fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
5264fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
5265fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
5266cbafad87SSudip Mukherjee 		goto out_debugfs;
5267fe248c3aSMaor Gottlieb 
5268fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
5269fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
5270fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5271fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
5272fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
5273fe248c3aSMaor Gottlieb 		goto out_debugfs;
5274fe248c3aSMaor Gottlieb 
5275fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
5276fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
5277fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5278fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
5279fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
5280fe248c3aSMaor Gottlieb 		goto out_debugfs;
5281fe248c3aSMaor Gottlieb 
5282fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
5283fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
5284fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
5285fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
5286fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
5287fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
5288fe248c3aSMaor Gottlieb 		goto out_debugfs;
5289fe248c3aSMaor Gottlieb 
5290fe248c3aSMaor Gottlieb 	return 0;
5291fe248c3aSMaor Gottlieb 
5292fe248c3aSMaor Gottlieb out_debugfs:
5293fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5294fe248c3aSMaor Gottlieb 	return -ENOMEM;
529503404e8aSMaor Gottlieb }
529603404e8aSMaor Gottlieb 
529703404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
529803404e8aSMaor Gottlieb {
529903404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
530003404e8aSMaor Gottlieb 		return;
530103404e8aSMaor Gottlieb 
530203404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
530303404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
530403404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
530503404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
530603404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
5307fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
5308fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
5309fe248c3aSMaor Gottlieb 
5310fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
5311fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
531203404e8aSMaor Gottlieb }
531303404e8aSMaor Gottlieb 
531484305d71SLeon Romanovsky static const struct cpumask *
531584305d71SLeon Romanovsky mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
531640b24403SSagi Grimberg {
531740b24403SSagi Grimberg 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
531840b24403SSagi Grimberg 
53196082d9c9SIsrael Rukshin 	return mlx5_get_vector_affinity_hint(dev->mdev, comp_vector);
532040b24403SSagi Grimberg }
532140b24403SSagi Grimberg 
532232f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
532332f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
532432f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
532532f69e4bSDaniel Jurgens {
532632f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
532732f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
532832f69e4bSDaniel Jurgens 	int comps;
532932f69e4bSDaniel Jurgens 	int err;
533032f69e4bSDaniel Jurgens 	int i;
533132f69e4bSDaniel Jurgens 
5332a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5333a9e546e7SParav Pandit 
533432f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
533532f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
533632f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
533732f69e4bSDaniel Jurgens 		return;
533832f69e4bSDaniel Jurgens 	}
533932f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
534032f69e4bSDaniel Jurgens 
534132f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
534232f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
534332f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
534432f69e4bSDaniel Jurgens 
534532f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
534632f69e4bSDaniel Jurgens 	if (comps) {
534732f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
534832f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
534932f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
535032f69e4bSDaniel Jurgens 
535132f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
535232f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
535332f69e4bSDaniel Jurgens 
535432f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
535532f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
535632f69e4bSDaniel Jurgens 	}
535732f69e4bSDaniel Jurgens 
535832f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
535932f69e4bSDaniel Jurgens 
536032f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
536132f69e4bSDaniel Jurgens 
536232f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
536332f69e4bSDaniel Jurgens 
536432f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
536532f69e4bSDaniel Jurgens 
536632f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
536732f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
536832f69e4bSDaniel Jurgens 	 * it back to the list.
536932f69e4bSDaniel Jurgens 	 */
537032f69e4bSDaniel Jurgens 	if (err)
537132f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
537232f69e4bSDaniel Jurgens 			    port_num + 1);
537332f69e4bSDaniel Jurgens 
537432f69e4bSDaniel Jurgens 	ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
537532f69e4bSDaniel Jurgens }
537632f69e4bSDaniel Jurgens 
537732f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
537832f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
537932f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
538032f69e4bSDaniel Jurgens {
538132f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
538232f69e4bSDaniel Jurgens 	int err;
538332f69e4bSDaniel Jurgens 
538432f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
538532f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
53862577188eSQing Huang 		mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
538732f69e4bSDaniel Jurgens 			    port_num + 1);
538832f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
538932f69e4bSDaniel Jurgens 		return false;
539032f69e4bSDaniel Jurgens 	}
539132f69e4bSDaniel Jurgens 
539232f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
539332f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
539432f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
539532f69e4bSDaniel Jurgens 
539632f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
539732f69e4bSDaniel Jurgens 	if (err)
539832f69e4bSDaniel Jurgens 		goto unbind;
539932f69e4bSDaniel Jurgens 
540032f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
540132f69e4bSDaniel Jurgens 	if (err)
540232f69e4bSDaniel Jurgens 		goto unbind;
540332f69e4bSDaniel Jurgens 
540432f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
540532f69e4bSDaniel Jurgens 	if (err) {
540632f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
540732f69e4bSDaniel Jurgens 			    port_num + 1);
540832f69e4bSDaniel Jurgens 		goto unbind;
540932f69e4bSDaniel Jurgens 	}
541032f69e4bSDaniel Jurgens 
5411a9e546e7SParav Pandit 	err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
5412a9e546e7SParav Pandit 	if (err)
5413a9e546e7SParav Pandit 		goto unbind;
5414a9e546e7SParav Pandit 
541532f69e4bSDaniel Jurgens 	return true;
541632f69e4bSDaniel Jurgens 
541732f69e4bSDaniel Jurgens unbind:
541832f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
541932f69e4bSDaniel Jurgens 	return false;
542032f69e4bSDaniel Jurgens }
542132f69e4bSDaniel Jurgens 
542232f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
542332f69e4bSDaniel Jurgens {
542432f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
542532f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
542632f69e4bSDaniel Jurgens 							  port_num + 1);
542732f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
542832f69e4bSDaniel Jurgens 	int err;
542932f69e4bSDaniel Jurgens 	int i;
543032f69e4bSDaniel Jurgens 
543132f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
543232f69e4bSDaniel Jurgens 		return 0;
543332f69e4bSDaniel Jurgens 
543432f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
543532f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
543632f69e4bSDaniel Jurgens 	if (err)
543732f69e4bSDaniel Jurgens 		return err;
543832f69e4bSDaniel Jurgens 
543932f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
544032f69e4bSDaniel Jurgens 	if (err)
544132f69e4bSDaniel Jurgens 		return err;
544232f69e4bSDaniel Jurgens 
544332f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
544432f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
544532f69e4bSDaniel Jurgens 		bool bound = false;
544632f69e4bSDaniel Jurgens 
544732f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
544832f69e4bSDaniel Jurgens 		if (i == port_num) {
544932f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
545032f69e4bSDaniel Jurgens 			if (!mpi) {
545132f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
545232f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
545332f69e4bSDaniel Jurgens 				return -ENOMEM;
545432f69e4bSDaniel Jurgens 			}
545532f69e4bSDaniel Jurgens 
545632f69e4bSDaniel Jurgens 			mpi->is_master = true;
545732f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
545832f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
545932f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
546032f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
546132f69e4bSDaniel Jurgens 			mpi = NULL;
546232f69e4bSDaniel Jurgens 			continue;
546332f69e4bSDaniel Jurgens 		}
546432f69e4bSDaniel Jurgens 
546532f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
546632f69e4bSDaniel Jurgens 				    list) {
546732f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
546832f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
546932f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
547032f69e4bSDaniel Jurgens 			}
547132f69e4bSDaniel Jurgens 
547232f69e4bSDaniel Jurgens 			if (bound) {
547332f69e4bSDaniel Jurgens 				dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
547432f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
547532f69e4bSDaniel Jurgens 				list_del(&mpi->list);
547632f69e4bSDaniel Jurgens 				break;
547732f69e4bSDaniel Jurgens 			}
547832f69e4bSDaniel Jurgens 		}
547932f69e4bSDaniel Jurgens 		if (!bound) {
548032f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
548132f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
548232f69e4bSDaniel Jurgens 				    i + 1);
548332f69e4bSDaniel Jurgens 		}
548432f69e4bSDaniel Jurgens 	}
548532f69e4bSDaniel Jurgens 
548632f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
548732f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
548832f69e4bSDaniel Jurgens 	return err;
548932f69e4bSDaniel Jurgens }
549032f69e4bSDaniel Jurgens 
549132f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
549232f69e4bSDaniel Jurgens {
549332f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
549432f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
549532f69e4bSDaniel Jurgens 							  port_num + 1);
549632f69e4bSDaniel Jurgens 	int i;
549732f69e4bSDaniel Jurgens 
549832f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
549932f69e4bSDaniel Jurgens 		return;
550032f69e4bSDaniel Jurgens 
550132f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
550232f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
550332f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
550432f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
550532f69e4bSDaniel Jurgens 			if (i == port_num) {
550632f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
550732f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
550832f69e4bSDaniel Jurgens 			} else {
550932f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
551032f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
551132f69e4bSDaniel Jurgens 			}
551232f69e4bSDaniel Jurgens 		}
551332f69e4bSDaniel Jurgens 	}
551432f69e4bSDaniel Jurgens 
551532f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
551632f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
551732f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
551832f69e4bSDaniel Jurgens 
551932f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
552032f69e4bSDaniel Jurgens }
552132f69e4bSDaniel Jurgens 
55229a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
55239a119cd5SJason Gunthorpe 	mlx5_ib_dm,
55249a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
552524da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
55269a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
552724da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
552883bb4442SJason Gunthorpe 			    UA_MANDATORY),
55299a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
553024da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
553183bb4442SJason Gunthorpe 			    UA_MANDATORY));
553224da0016SAriel Levkovich 
55339a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
55349a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
55359a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
5536c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
5537bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
5538bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
5539c6475a0bSAviad Yehezkel 
55408c84660bSMatan Barak static int populate_specs_root(struct mlx5_ib_dev *dev)
55418c84660bSMatan Barak {
55427d96c9b1SJason Gunthorpe 	const struct uverbs_object_tree_def **trees = dev->driver_trees;
55437d96c9b1SJason Gunthorpe 	size_t num_trees = 0;
55448c84660bSMatan Barak 
55457d96c9b1SJason Gunthorpe 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
55467d96c9b1SJason Gunthorpe 	    MLX5_ACCEL_IPSEC_CAP_DEVICE)
55477d96c9b1SJason Gunthorpe 		trees[num_trees++] = &mlx5_ib_flow_action;
5548c6475a0bSAviad Yehezkel 
55497d96c9b1SJason Gunthorpe 	if (MLX5_CAP_DEV_MEM(dev->mdev, memic))
55507d96c9b1SJason Gunthorpe 		trees[num_trees++] = &mlx5_ib_dm;
555124da0016SAriel Levkovich 
5552c59450c4SYishai Hadas 	if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
55537d96c9b1SJason Gunthorpe 	    MLX5_GENERAL_OBJ_TYPES_CAP_UCTX)
55547d96c9b1SJason Gunthorpe 		trees[num_trees++] = mlx5_ib_get_devx_tree();
5555c59450c4SYishai Hadas 
55567d96c9b1SJason Gunthorpe 	num_trees += mlx5_ib_get_flow_trees(trees + num_trees);
5557cb80fb18SYishai Hadas 
55587d96c9b1SJason Gunthorpe 	WARN_ON(num_trees >= ARRAY_SIZE(dev->driver_trees));
55597d96c9b1SJason Gunthorpe 	trees[num_trees] = NULL;
55607d96c9b1SJason Gunthorpe 	dev->ib_dev.driver_specs = trees;
55618c84660bSMatan Barak 
55627d96c9b1SJason Gunthorpe 	return 0;
55638c84660bSMatan Barak }
55648c84660bSMatan Barak 
55651a1e03dcSRaed Salem static int mlx5_ib_read_counters(struct ib_counters *counters,
55661a1e03dcSRaed Salem 				 struct ib_counters_read_attr *read_attr,
55671a1e03dcSRaed Salem 				 struct uverbs_attr_bundle *attrs)
55681a1e03dcSRaed Salem {
55691a1e03dcSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
55701a1e03dcSRaed Salem 	struct mlx5_read_counters_attr mread_attr = {};
55711a1e03dcSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc;
55721a1e03dcSRaed Salem 	int ret, i;
55731a1e03dcSRaed Salem 
55741a1e03dcSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
55751a1e03dcSRaed Salem 	if (mcounters->cntrs_max_index > read_attr->ncounters) {
55761a1e03dcSRaed Salem 		ret = -EINVAL;
55771a1e03dcSRaed Salem 		goto err_bound;
55781a1e03dcSRaed Salem 	}
55791a1e03dcSRaed Salem 
55801a1e03dcSRaed Salem 	mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
55811a1e03dcSRaed Salem 				 GFP_KERNEL);
55821a1e03dcSRaed Salem 	if (!mread_attr.out) {
55831a1e03dcSRaed Salem 		ret = -ENOMEM;
55841a1e03dcSRaed Salem 		goto err_bound;
55851a1e03dcSRaed Salem 	}
55861a1e03dcSRaed Salem 
55871a1e03dcSRaed Salem 	mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
55881a1e03dcSRaed Salem 	mread_attr.flags = read_attr->flags;
55891a1e03dcSRaed Salem 	ret = mcounters->read_counters(counters->device, &mread_attr);
55901a1e03dcSRaed Salem 	if (ret)
55911a1e03dcSRaed Salem 		goto err_read;
55921a1e03dcSRaed Salem 
55931a1e03dcSRaed Salem 	/* do the pass over the counters data array to assign according to the
55941a1e03dcSRaed Salem 	 * descriptions and indexing pairs
55951a1e03dcSRaed Salem 	 */
55961a1e03dcSRaed Salem 	desc = mcounters->counters_data;
55971a1e03dcSRaed Salem 	for (i = 0; i < mcounters->ncounters; i++)
55981a1e03dcSRaed Salem 		read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
55991a1e03dcSRaed Salem 
56001a1e03dcSRaed Salem err_read:
56011a1e03dcSRaed Salem 	kfree(mread_attr.out);
56021a1e03dcSRaed Salem err_bound:
56031a1e03dcSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
56041a1e03dcSRaed Salem 	return ret;
56051a1e03dcSRaed Salem }
56061a1e03dcSRaed Salem 
5607b29e2a13SRaed Salem static int mlx5_ib_destroy_counters(struct ib_counters *counters)
5608b29e2a13SRaed Salem {
5609b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5610b29e2a13SRaed Salem 
56113b3233fbSRaed Salem 	counters_clear_description(counters);
56123b3233fbSRaed Salem 	if (mcounters->hw_cntrs_hndl)
56133b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(counters->device)->mdev,
56143b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
56153b3233fbSRaed Salem 
5616b29e2a13SRaed Salem 	kfree(mcounters);
5617b29e2a13SRaed Salem 
5618b29e2a13SRaed Salem 	return 0;
5619b29e2a13SRaed Salem }
5620b29e2a13SRaed Salem 
5621b29e2a13SRaed Salem static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
5622b29e2a13SRaed Salem 						   struct uverbs_attr_bundle *attrs)
5623b29e2a13SRaed Salem {
5624b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters;
5625b29e2a13SRaed Salem 
5626b29e2a13SRaed Salem 	mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
5627b29e2a13SRaed Salem 	if (!mcounters)
5628b29e2a13SRaed Salem 		return ERR_PTR(-ENOMEM);
5629b29e2a13SRaed Salem 
56303b3233fbSRaed Salem 	mutex_init(&mcounters->mcntrs_mutex);
56313b3233fbSRaed Salem 
5632b29e2a13SRaed Salem 	return &mcounters->ibcntrs;
5633b29e2a13SRaed Salem }
5634b29e2a13SRaed Salem 
5635b5ca15adSMark Bloch void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
5636e126ba97SEli Cohen {
563732f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
56383cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
56393cc297dbSMark Bloch 	cleanup_srcu_struct(&dev->mr_srcu);
56403cc297dbSMark Bloch #endif
564116c1975fSMark Bloch 	kfree(dev->port);
564216c1975fSMark Bloch }
564316c1975fSMark Bloch 
5644b5ca15adSMark Bloch int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
564516c1975fSMark Bloch {
564616c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
56474babcf97SAviv Heller 	const char *name;
5648e126ba97SEli Cohen 	int err;
564932f69e4bSDaniel Jurgens 	int i;
5650e126ba97SEli Cohen 
5651508562d6SDaniel Jurgens 	dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
56520837e86aSMark Bloch 			    GFP_KERNEL);
56530837e86aSMark Bloch 	if (!dev->port)
565416c1975fSMark Bloch 		return -ENOMEM;
56550837e86aSMark Bloch 
565632f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
565732f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
565832f69e4bSDaniel Jurgens 		rwlock_init(&dev->roce[i].netdev_lock);
565932f69e4bSDaniel Jurgens 	}
566032f69e4bSDaniel Jurgens 
566132f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
5662e126ba97SEli Cohen 	if (err)
56630837e86aSMark Bloch 		goto err_free_port;
5664e126ba97SEli Cohen 
566532f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
566632f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
566732f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
566832f69e4bSDaniel Jurgens 			if (err)
566932f69e4bSDaniel Jurgens 				break;
567032f69e4bSDaniel Jurgens 		}
567132f69e4bSDaniel Jurgens 	} else {
567232f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
567332f69e4bSDaniel Jurgens 	}
567432f69e4bSDaniel Jurgens 	if (err)
567532f69e4bSDaniel Jurgens 		goto err_mp;
567632f69e4bSDaniel Jurgens 
56771b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
5678e126ba97SEli Cohen 		get_ext_port_caps(dev);
5679e126ba97SEli Cohen 
56804babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
56814babcf97SAviv Heller 		name = "mlx5_%d";
56824babcf97SAviv Heller 	else
56834babcf97SAviv Heller 		name = "mlx5_bond_%d";
56844babcf97SAviv Heller 
56854babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
5686e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
5687e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
5688c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
5689e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
5690233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
5691233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
56929b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
5693e126ba97SEli Cohen 
56943cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
56953cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
56963cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
56973cc297dbSMark Bloch 
569824da0016SAriel Levkovich 	spin_lock_init(&dev->memic.memic_lock);
569924da0016SAriel Levkovich 	dev->memic.dev = mdev;
570024da0016SAriel Levkovich 
57013cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
57023cc297dbSMark Bloch 	err = init_srcu_struct(&dev->mr_srcu);
57033cc297dbSMark Bloch 	if (err)
57043cc297dbSMark Bloch 		goto err_free_port;
57053cc297dbSMark Bloch #endif
57063cc297dbSMark Bloch 
570716c1975fSMark Bloch 	return 0;
570832f69e4bSDaniel Jurgens err_mp:
570932f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
571016c1975fSMark Bloch 
571116c1975fSMark Bloch err_free_port:
571216c1975fSMark Bloch 	kfree(dev->port);
571316c1975fSMark Bloch 
571416c1975fSMark Bloch 	return -ENOMEM;
571516c1975fSMark Bloch }
571616c1975fSMark Bloch 
57179a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
57189a4ca38dSMark Bloch {
57199a4ca38dSMark Bloch 	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
57209a4ca38dSMark Bloch 
57219a4ca38dSMark Bloch 	if (!dev->flow_db)
57229a4ca38dSMark Bloch 		return -ENOMEM;
57239a4ca38dSMark Bloch 
57249a4ca38dSMark Bloch 	mutex_init(&dev->flow_db->lock);
57259a4ca38dSMark Bloch 
57269a4ca38dSMark Bloch 	return 0;
57279a4ca38dSMark Bloch }
57289a4ca38dSMark Bloch 
5729b5ca15adSMark Bloch int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
5730b5ca15adSMark Bloch {
5731b5ca15adSMark Bloch 	struct mlx5_ib_dev *nic_dev;
5732b5ca15adSMark Bloch 
5733b5ca15adSMark Bloch 	nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
5734b5ca15adSMark Bloch 
5735b5ca15adSMark Bloch 	if (!nic_dev)
5736b5ca15adSMark Bloch 		return -EINVAL;
5737b5ca15adSMark Bloch 
5738b5ca15adSMark Bloch 	dev->flow_db = nic_dev->flow_db;
5739b5ca15adSMark Bloch 
5740b5ca15adSMark Bloch 	return 0;
5741b5ca15adSMark Bloch }
5742b5ca15adSMark Bloch 
57439a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
57449a4ca38dSMark Bloch {
57459a4ca38dSMark Bloch 	kfree(dev->flow_db);
57469a4ca38dSMark Bloch }
57479a4ca38dSMark Bloch 
5748b5ca15adSMark Bloch int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
574916c1975fSMark Bloch {
575016c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
575116c1975fSMark Bloch 	int err;
575216c1975fSMark Bloch 
5753e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
5754e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
5755e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
5756e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
5757e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
5758e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
5759e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
576041c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
576141c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
5762e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
576356e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
5764e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
5765e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
5766e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
5767e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
5768e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
5769e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
5770e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
5771e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
5772e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
5773e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
5774e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
5775e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
5776e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
5777e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
5778e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
5779e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
5780e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
57811707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
5782d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
5783d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
57847d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
5785b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
5786b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
5787e126ba97SEli Cohen 
5788e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
5789ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
5790e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
57913cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
57923cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
5793e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
5794e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
5795e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
5796e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
5797e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
5798e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
5799e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
5800e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
5801e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
5802e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
5803e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
5804e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
5805e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
5806e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
5807e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
5808e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
5809e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
5810e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
5811e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
5812e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
5813d0e84c0aSYishai Hadas 	dev->ib_dev.drain_sq		= mlx5_ib_drain_sq;
5814d0e84c0aSYishai Hadas 	dev->ib_dev.drain_rq		= mlx5_ib_drain_rq;
5815e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
5816e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
5817e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
5818e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
5819e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
5820e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
5821e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
5822e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
5823e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
5824e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
582556e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
5826e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
5827e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
5828e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
5829e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
58309bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
58318a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
5832d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
5833c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
583440b24403SSagi Grimberg 	dev->ib_dev.get_vector_affinity	= mlx5_ib_get_vector_affinity;
58358e959601SNiranjana Vishwanathapura 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
5836693dfd5aSErez Shitrit 		dev->ib_dev.alloc_rdma_netdev	= mlx5_ib_alloc_rdma_netdev;
58378e959601SNiranjana Vishwanathapura 
5838eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
5839eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
5840eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
5841eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
5842eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
5843eff901d3SEli Cohen 	}
5844e126ba97SEli Cohen 
58457c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
58467c2344c3SMaor Gottlieb 
58476e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
58486e8484c5SMax Gurtovoy 
5849d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
5850d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
5851d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
5852d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
5853d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
5854d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
5855d2370e0aSMatan Barak 	}
5856d2370e0aSMatan Barak 
5857938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
5858e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
5859e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
5860e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
5861e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
5862e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
5863e126ba97SEli Cohen 	}
5864e126ba97SEli Cohen 
586524da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
586624da0016SAriel Levkovich 		dev->ib_dev.alloc_dm = mlx5_ib_alloc_dm;
586724da0016SAriel Levkovich 		dev->ib_dev.dealloc_dm = mlx5_ib_dealloc_dm;
58686c29f57eSAriel Levkovich 		dev->ib_dev.reg_dm_mr = mlx5_ib_reg_dm_mr;
586924da0016SAriel Levkovich 	}
587024da0016SAriel Levkovich 
5871038d2ef8SMaor Gottlieb 	dev->ib_dev.create_flow	= mlx5_ib_create_flow;
5872038d2ef8SMaor Gottlieb 	dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
587381e30880SYishai Hadas 	dev->ib_dev.uverbs_ex_cmd_mask |=
587481e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
587581e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
5876c6475a0bSAviad Yehezkel 	dev->ib_dev.create_flow_action_esp = mlx5_ib_create_flow_action_esp;
5877c6475a0bSAviad Yehezkel 	dev->ib_dev.destroy_flow_action = mlx5_ib_destroy_flow_action;
5878349705c1SMatan Barak 	dev->ib_dev.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp;
58790ede73bcSMatan Barak 	dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
5880b29e2a13SRaed Salem 	dev->ib_dev.create_counters = mlx5_ib_create_counters;
5881b29e2a13SRaed Salem 	dev->ib_dev.destroy_counters = mlx5_ib_destroy_counters;
58821a1e03dcSRaed Salem 	dev->ib_dev.read_counters = mlx5_ib_read_counters;
588381e30880SYishai Hadas 
5884e126ba97SEli Cohen 	err = init_node_data(dev);
5885e126ba97SEli Cohen 	if (err)
588616c1975fSMark Bloch 		return err;
5887e126ba97SEli Cohen 
5888c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
5889e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
5890e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
5891c8b89924SMark Bloch 		mutex_init(&dev->lb_mutex);
5892c8b89924SMark Bloch 
589316c1975fSMark Bloch 	return 0;
589416c1975fSMark Bloch }
589516c1975fSMark Bloch 
58968e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
589716c1975fSMark Bloch {
58988e6efa3aSMark Bloch 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
58998e6efa3aSMark Bloch 	dev->ib_dev.query_port		= mlx5_ib_query_port;
59008e6efa3aSMark Bloch 
59018e6efa3aSMark Bloch 	return 0;
59028e6efa3aSMark Bloch }
59038e6efa3aSMark Bloch 
5904b5ca15adSMark Bloch int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
59058e6efa3aSMark Bloch {
59068e6efa3aSMark Bloch 	dev->ib_dev.get_port_immutable  = mlx5_port_rep_immutable;
59078e6efa3aSMark Bloch 	dev->ib_dev.query_port		= mlx5_ib_rep_query_port;
59088e6efa3aSMark Bloch 
59098e6efa3aSMark Bloch 	return 0;
59108e6efa3aSMark Bloch }
59118e6efa3aSMark Bloch 
5912e3f1ed1fSLeon Romanovsky static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
59138e6efa3aSMark Bloch {
5914e3f1ed1fSLeon Romanovsky 	u8 port_num;
59157fd8aefbSDaniel Jurgens 	int i;
591616c1975fSMark Bloch 
59177fd8aefbSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
59187fd8aefbSDaniel Jurgens 		dev->roce[i].dev = dev;
59197fd8aefbSDaniel Jurgens 		dev->roce[i].native_port_num = i + 1;
59207fd8aefbSDaniel Jurgens 		dev->roce[i].last_port_state = IB_PORT_DOWN;
59217fd8aefbSDaniel Jurgens 	}
59227fd8aefbSDaniel Jurgens 
5923c11a226aSMark Bloch 	dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
5924c11a226aSMark Bloch 	dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
5925c11a226aSMark Bloch 	dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
5926c11a226aSMark Bloch 	dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
5927c11a226aSMark Bloch 	dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
5928c11a226aSMark Bloch 	dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
59298e6efa3aSMark Bloch 
5930c11a226aSMark Bloch 	dev->ib_dev.uverbs_ex_cmd_mask |=
5931c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
5932c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
5933c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
5934c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
5935c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
59368e6efa3aSMark Bloch 
5937e3f1ed1fSLeon Romanovsky 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5938e3f1ed1fSLeon Romanovsky 
59398e6efa3aSMark Bloch 	return mlx5_add_netdev_notifier(dev, port_num);
59408e6efa3aSMark Bloch }
59418e6efa3aSMark Bloch 
59428e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
59438e6efa3aSMark Bloch {
59448e6efa3aSMark Bloch 	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
59458e6efa3aSMark Bloch 
59468e6efa3aSMark Bloch 	mlx5_remove_netdev_notifier(dev, port_num);
59478e6efa3aSMark Bloch }
59488e6efa3aSMark Bloch 
59498e6efa3aSMark Bloch int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
59508e6efa3aSMark Bloch {
59518e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
59528e6efa3aSMark Bloch 	enum rdma_link_layer ll;
59538e6efa3aSMark Bloch 	int port_type_cap;
59548e6efa3aSMark Bloch 	int err = 0;
59558e6efa3aSMark Bloch 
59568e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
59578e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
59588e6efa3aSMark Bloch 
59598e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET)
5960e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
59618e6efa3aSMark Bloch 
596216c1975fSMark Bloch 	return err;
5963fc24fc5eSAchiad Shochat }
5964fc24fc5eSAchiad Shochat 
59658e6efa3aSMark Bloch void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
59668e6efa3aSMark Bloch {
59678e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
59688e6efa3aSMark Bloch }
59698e6efa3aSMark Bloch 
59708e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
59718e6efa3aSMark Bloch {
59728e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
59738e6efa3aSMark Bloch 	enum rdma_link_layer ll;
59748e6efa3aSMark Bloch 	int port_type_cap;
59758e6efa3aSMark Bloch 	int err;
59768e6efa3aSMark Bloch 
59778e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
59788e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
59798e6efa3aSMark Bloch 
59808e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
5981e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
59828e6efa3aSMark Bloch 		if (err)
59838e6efa3aSMark Bloch 			return err;
59848e6efa3aSMark Bloch 
5985e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
59868e6efa3aSMark Bloch 		if (err)
59878e6efa3aSMark Bloch 			goto cleanup;
59888e6efa3aSMark Bloch 	}
59898e6efa3aSMark Bloch 
599016c1975fSMark Bloch 	return 0;
59918e6efa3aSMark Bloch cleanup:
59928e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
59938e6efa3aSMark Bloch 
59948e6efa3aSMark Bloch 	return err;
599545bded2cSKamal Heib }
59966aec21f6SHaggai Eran 
599716c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
599816c1975fSMark Bloch {
599916c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
600016c1975fSMark Bloch 	enum rdma_link_layer ll;
600116c1975fSMark Bloch 	int port_type_cap;
60024a2da0b8SParav Pandit 
600316c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
600416c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
600516c1975fSMark Bloch 
600616c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
600716c1975fSMark Bloch 		mlx5_disable_eth(dev);
60088e6efa3aSMark Bloch 		mlx5_ib_stage_common_roce_cleanup(dev);
600916c1975fSMark Bloch 	}
601016c1975fSMark Bloch }
601116c1975fSMark Bloch 
6012b5ca15adSMark Bloch int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
601316c1975fSMark Bloch {
601416c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
601516c1975fSMark Bloch }
601616c1975fSMark Bloch 
6017b5ca15adSMark Bloch void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
601816c1975fSMark Bloch {
601916c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
602016c1975fSMark Bloch }
602116c1975fSMark Bloch 
602216c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
602316c1975fSMark Bloch {
602407321b3cSMark Bloch 	mlx5_ib_internal_fill_odp_caps(dev);
602507321b3cSMark Bloch 
602616c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
602716c1975fSMark Bloch }
602816c1975fSMark Bloch 
6029b5ca15adSMark Bloch int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
603016c1975fSMark Bloch {
60315e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
60325e1e7612SMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
60335e1e7612SMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
60345e1e7612SMark Bloch 
603516c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
60365e1e7612SMark Bloch 	}
603716c1975fSMark Bloch 
603816c1975fSMark Bloch 	return 0;
603916c1975fSMark Bloch }
604016c1975fSMark Bloch 
6041b5ca15adSMark Bloch void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
604216c1975fSMark Bloch {
604316c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
604416c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
604516c1975fSMark Bloch }
604616c1975fSMark Bloch 
604716c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
604816c1975fSMark Bloch {
6049a9e546e7SParav Pandit 	return mlx5_ib_init_cong_debugfs(dev,
6050a9e546e7SParav Pandit 					 mlx5_core_native_port_num(dev->mdev) - 1);
605116c1975fSMark Bloch }
605216c1975fSMark Bloch 
605316c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
605416c1975fSMark Bloch {
6055a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
6056a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
605716c1975fSMark Bloch }
605816c1975fSMark Bloch 
605916c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
606016c1975fSMark Bloch {
60615fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
6062444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
606316c1975fSMark Bloch }
606416c1975fSMark Bloch 
606516c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
606616c1975fSMark Bloch {
606716c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
606816c1975fSMark Bloch }
606916c1975fSMark Bloch 
6070b5ca15adSMark Bloch int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
607116c1975fSMark Bloch {
607216c1975fSMark Bloch 	int err;
60735fe9dec0SEli Cohen 
60745fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
60755fe9dec0SEli Cohen 	if (err)
607616c1975fSMark Bloch 		return err;
60775fe9dec0SEli Cohen 
60785fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
60795fe9dec0SEli Cohen 	if (err)
608016c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
60815fe9dec0SEli Cohen 
608216c1975fSMark Bloch 	return err;
608316c1975fSMark Bloch }
60840837e86aSMark Bloch 
6085b5ca15adSMark Bloch void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
608616c1975fSMark Bloch {
608716c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
608816c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
608916c1975fSMark Bloch }
6090e126ba97SEli Cohen 
60918c84660bSMatan Barak static int mlx5_ib_stage_populate_specs(struct mlx5_ib_dev *dev)
60928c84660bSMatan Barak {
60938c84660bSMatan Barak 	return populate_specs_root(dev);
60948c84660bSMatan Barak }
60958c84660bSMatan Barak 
6096b5ca15adSMark Bloch int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
609716c1975fSMark Bloch {
609816c1975fSMark Bloch 	return ib_register_device(&dev->ib_dev, NULL);
609916c1975fSMark Bloch }
610016c1975fSMark Bloch 
610103fe2debSDavid S. Miller void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
610242cea83fSMark Bloch {
610342cea83fSMark Bloch 	destroy_umrc_res(dev);
610442cea83fSMark Bloch }
610542cea83fSMark Bloch 
6106b5ca15adSMark Bloch void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
610716c1975fSMark Bloch {
610816c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
610916c1975fSMark Bloch }
611016c1975fSMark Bloch 
611103fe2debSDavid S. Miller int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
611216c1975fSMark Bloch {
611316c1975fSMark Bloch 	return create_umr_res(dev);
611416c1975fSMark Bloch }
611516c1975fSMark Bloch 
611616c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
611716c1975fSMark Bloch {
611803404e8aSMaor Gottlieb 	init_delay_drop(dev);
611903404e8aSMaor Gottlieb 
612016c1975fSMark Bloch 	return 0;
612116c1975fSMark Bloch }
612216c1975fSMark Bloch 
612316c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
612416c1975fSMark Bloch {
612516c1975fSMark Bloch 	cancel_delay_drop(dev);
612616c1975fSMark Bloch }
612716c1975fSMark Bloch 
6128b5ca15adSMark Bloch int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev)
612916c1975fSMark Bloch {
613016c1975fSMark Bloch 	int err;
613116c1975fSMark Bloch 	int i;
613216c1975fSMark Bloch 
6133e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
6134281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
6135281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
6136281d1a92SWei Yongjun 		if (err)
613716c1975fSMark Bloch 			return err;
6138e126ba97SEli Cohen 	}
6139e126ba97SEli Cohen 
614016c1975fSMark Bloch 	return 0;
614116c1975fSMark Bloch }
614216c1975fSMark Bloch 
6143fc385b7aSMark Bloch static int mlx5_ib_stage_rep_reg_init(struct mlx5_ib_dev *dev)
6144fc385b7aSMark Bloch {
6145fc385b7aSMark Bloch 	mlx5_ib_register_vport_reps(dev);
6146fc385b7aSMark Bloch 
6147fc385b7aSMark Bloch 	return 0;
6148fc385b7aSMark Bloch }
6149fc385b7aSMark Bloch 
6150fc385b7aSMark Bloch static void mlx5_ib_stage_rep_reg_cleanup(struct mlx5_ib_dev *dev)
6151fc385b7aSMark Bloch {
6152fc385b7aSMark Bloch 	mlx5_ib_unregister_vport_reps(dev);
6153fc385b7aSMark Bloch }
6154fc385b7aSMark Bloch 
6155b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
615616c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
615716c1975fSMark Bloch 		      int stage)
615816c1975fSMark Bloch {
615916c1975fSMark Bloch 	/* Number of stages to cleanup */
616016c1975fSMark Bloch 	while (stage) {
616116c1975fSMark Bloch 		stage--;
616216c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
616316c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
616416c1975fSMark Bloch 	}
616516c1975fSMark Bloch 
616616c1975fSMark Bloch 	ib_dealloc_device((struct ib_device *)dev);
616716c1975fSMark Bloch }
616816c1975fSMark Bloch 
6169b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
617016c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
617116c1975fSMark Bloch {
617216c1975fSMark Bloch 	int err;
617316c1975fSMark Bloch 	int i;
617416c1975fSMark Bloch 
617516c1975fSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
617616c1975fSMark Bloch 
617716c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
617816c1975fSMark Bloch 		if (profile->stage[i].init) {
617916c1975fSMark Bloch 			err = profile->stage[i].init(dev);
618016c1975fSMark Bloch 			if (err)
618116c1975fSMark Bloch 				goto err_out;
618216c1975fSMark Bloch 		}
618316c1975fSMark Bloch 	}
618416c1975fSMark Bloch 
618516c1975fSMark Bloch 	dev->profile = profile;
6186e126ba97SEli Cohen 	dev->ib_active = true;
6187e126ba97SEli Cohen 
61889603b61dSJack Morgenstein 	return dev;
6189e126ba97SEli Cohen 
619016c1975fSMark Bloch err_out:
619116c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
6192e126ba97SEli Cohen 
61939603b61dSJack Morgenstein 	return NULL;
6194e126ba97SEli Cohen }
6195e126ba97SEli Cohen 
619616c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
619716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
619816c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
619916c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
62009a4ca38dSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
62019a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_init,
62029a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
620316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
620416c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
620516c1975fSMark Bloch 		     NULL),
62068e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
62078e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
62088e6efa3aSMark Bloch 		     NULL),
620916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
621016c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
621116c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
621216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
621316c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
621416c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
621516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
621616c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
62173cc297dbSMark Bloch 		     NULL),
621816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
621916c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
622016c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
622116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
622216c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
622316c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
622416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
622516c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
622616c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
622716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
622816c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
622916c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
623042cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
623142cea83fSMark Bloch 		     NULL,
623242cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
62338c84660bSMatan Barak 	STAGE_CREATE(MLX5_IB_STAGE_SPECS,
62348c84660bSMatan Barak 		     mlx5_ib_stage_populate_specs,
62357d96c9b1SJason Gunthorpe 		     NULL),
623616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
623716c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
623816c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
623942cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
624042cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
624142cea83fSMark Bloch 		     NULL),
624216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
624316c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
624416c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
624516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
624616c1975fSMark Bloch 		     mlx5_ib_stage_class_attr_init,
624716c1975fSMark Bloch 		     NULL),
624816c1975fSMark Bloch };
624916c1975fSMark Bloch 
6250b5ca15adSMark Bloch static const struct mlx5_ib_profile nic_rep_profile = {
6251b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
6252b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
6253b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
6254b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6255b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_init,
6256b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
6257b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6258b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
6259b5ca15adSMark Bloch 		     NULL),
6260b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6261b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_non_default_cb,
6262b5ca15adSMark Bloch 		     NULL),
6263b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6264b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_init,
6265b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_cleanup),
6266b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6267b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_init,
6268b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
6269b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6270b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_init,
6271b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
6272b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
6273b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
6274b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
6275b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6276b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
6277b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
627803fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
627903fe2debSDavid S. Miller 		     NULL,
628003fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
62818c84660bSMatan Barak 	STAGE_CREATE(MLX5_IB_STAGE_SPECS,
62828c84660bSMatan Barak 		     mlx5_ib_stage_populate_specs,
62837d96c9b1SJason Gunthorpe 		     NULL),
6284b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6285b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
6286b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
628703fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
628803fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
628903fe2debSDavid S. Miller 		     NULL),
6290b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
6291b5ca15adSMark Bloch 		     mlx5_ib_stage_class_attr_init,
6292b5ca15adSMark Bloch 		     NULL),
6293b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_REP_REG,
6294b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_reg_init,
6295b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_reg_cleanup),
6296b5ca15adSMark Bloch };
6297b5ca15adSMark Bloch 
6298e3f1ed1fSLeon Romanovsky static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
629932f69e4bSDaniel Jurgens {
630032f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
630132f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
630232f69e4bSDaniel Jurgens 	bool bound = false;
630332f69e4bSDaniel Jurgens 	int err;
630432f69e4bSDaniel Jurgens 
630532f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
630632f69e4bSDaniel Jurgens 	if (!mpi)
630732f69e4bSDaniel Jurgens 		return NULL;
630832f69e4bSDaniel Jurgens 
630932f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
631032f69e4bSDaniel Jurgens 
631132f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
631232f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
631332f69e4bSDaniel Jurgens 	if (err) {
631432f69e4bSDaniel Jurgens 		kfree(mpi);
631532f69e4bSDaniel Jurgens 		return NULL;
631632f69e4bSDaniel Jurgens 	}
631732f69e4bSDaniel Jurgens 
631832f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
631932f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
632032f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
632132f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
632232f69e4bSDaniel Jurgens 
632332f69e4bSDaniel Jurgens 		if (bound) {
632432f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
632532f69e4bSDaniel Jurgens 			break;
632632f69e4bSDaniel Jurgens 		}
632732f69e4bSDaniel Jurgens 	}
632832f69e4bSDaniel Jurgens 
632932f69e4bSDaniel Jurgens 	if (!bound) {
633032f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
633132f69e4bSDaniel Jurgens 		dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
633232f69e4bSDaniel Jurgens 	}
633332f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
633432f69e4bSDaniel Jurgens 
633532f69e4bSDaniel Jurgens 	return mpi;
633632f69e4bSDaniel Jurgens }
633732f69e4bSDaniel Jurgens 
633816c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
633916c1975fSMark Bloch {
634032f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
6341b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
634232f69e4bSDaniel Jurgens 	int port_type_cap;
634332f69e4bSDaniel Jurgens 
6344b5ca15adSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
6345b5ca15adSMark Bloch 
634632f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
634732f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
634832f69e4bSDaniel Jurgens 
6349e3f1ed1fSLeon Romanovsky 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6350e3f1ed1fSLeon Romanovsky 		return mlx5_ib_add_slave_port(mdev);
635132f69e4bSDaniel Jurgens 
6352b5ca15adSMark Bloch 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
6353b5ca15adSMark Bloch 	if (!dev)
6354b5ca15adSMark Bloch 		return NULL;
6355b5ca15adSMark Bloch 
6356b5ca15adSMark Bloch 	dev->mdev = mdev;
6357b5ca15adSMark Bloch 	dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6358b5ca15adSMark Bloch 			     MLX5_CAP_GEN(mdev, num_vhca_ports));
6359b5ca15adSMark Bloch 
6360aff2252aSOr Gerlitz 	if (MLX5_ESWITCH_MANAGER(mdev) &&
6361b5ca15adSMark Bloch 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
6362b5ca15adSMark Bloch 		dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0);
6363b5ca15adSMark Bloch 
6364b5ca15adSMark Bloch 		return __mlx5_ib_add(dev, &nic_rep_profile);
6365b5ca15adSMark Bloch 	}
6366b5ca15adSMark Bloch 
6367b5ca15adSMark Bloch 	return __mlx5_ib_add(dev, &pf_profile);
636816c1975fSMark Bloch }
636916c1975fSMark Bloch 
63709603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
6371e126ba97SEli Cohen {
637232f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
637332f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
63746aec21f6SHaggai Eran 
637532f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
637632f69e4bSDaniel Jurgens 		mpi = context;
637732f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
637832f69e4bSDaniel Jurgens 		if (mpi->ibdev)
637932f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
638032f69e4bSDaniel Jurgens 		list_del(&mpi->list);
638132f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
638232f69e4bSDaniel Jurgens 		return;
638332f69e4bSDaniel Jurgens 	}
638432f69e4bSDaniel Jurgens 
638532f69e4bSDaniel Jurgens 	dev = context;
638616c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
6387e126ba97SEli Cohen }
6388e126ba97SEli Cohen 
63899603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
63909603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
63919603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
63929603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
6393d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
6394d9aaed83SArtemy Kovalyov 	.pfault		= mlx5_ib_pfault,
6395d9aaed83SArtemy Kovalyov #endif
639664613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
6397e126ba97SEli Cohen };
6398e126ba97SEli Cohen 
6399c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
6400c44ef998SIlya Lesokhin {
6401c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
6402c44ef998SIlya Lesokhin 	return xlt_emergency_page;
6403c44ef998SIlya Lesokhin }
6404c44ef998SIlya Lesokhin 
6405c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
6406c44ef998SIlya Lesokhin {
6407c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
6408c44ef998SIlya Lesokhin }
6409c44ef998SIlya Lesokhin 
6410e126ba97SEli Cohen static int __init mlx5_ib_init(void)
6411e126ba97SEli Cohen {
64126aec21f6SHaggai Eran 	int err;
64136aec21f6SHaggai Eran 
6414c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
6415c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
6416d69a24e0SDaniel Jurgens 		return -ENOMEM;
6417d69a24e0SDaniel Jurgens 
6418c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
6419c44ef998SIlya Lesokhin 
6420c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6421c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
6422c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
6423c44ef998SIlya Lesokhin 		return -ENOMEM;
6424c44ef998SIlya Lesokhin 	}
6425c44ef998SIlya Lesokhin 
642681713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
64279603b61dSJack Morgenstein 
64286aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
64296aec21f6SHaggai Eran 
64306aec21f6SHaggai Eran 	return err;
6431e126ba97SEli Cohen }
6432e126ba97SEli Cohen 
6433e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
6434e126ba97SEli Cohen {
64359603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
6436d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
6437c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
6438c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
6439e126ba97SEli Cohen }
6440e126ba97SEli Cohen 
6441e126ba97SEli Cohen module_init(mlx5_ib_init);
6442e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
6443