xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision b02289b3d60f79ba0831051a7743d8fdb4110355)
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 
1829e126ba97SEli Cohen 	return &context->ibucontext;
1830e126ba97SEli Cohen 
1831a8b92ca1SYishai Hadas out_mdev:
1832a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
1833a8b92ca1SYishai Hadas 		mlx5_ib_devx_destroy(dev, context);
1834146d2f1aSmajd@mellanox.com out_td:
1835c85023e1SHuy Nguyen 	mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1836146d2f1aSmajd@mellanox.com 
1837e126ba97SEli Cohen out_uars:
1838b037c29aSEli Cohen 	deallocate_uars(dev, context);
1839b037c29aSEli Cohen 
1840b037c29aSEli Cohen out_sys_pages:
1841b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1842b037c29aSEli Cohen 
1843e126ba97SEli Cohen out_count:
18442f5ff264SEli Cohen 	kfree(bfregi->count);
1845e126ba97SEli Cohen 
1846e126ba97SEli Cohen out_ctx:
1847e126ba97SEli Cohen 	kfree(context);
1848b037c29aSEli Cohen 
1849e126ba97SEli Cohen 	return ERR_PTR(err);
1850e126ba97SEli Cohen }
1851e126ba97SEli Cohen 
1852e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1853e126ba97SEli Cohen {
1854e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1855e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1856b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1857e126ba97SEli Cohen 
1858a8b92ca1SYishai Hadas 	if (context->devx_uid)
1859a8b92ca1SYishai Hadas 		mlx5_ib_devx_destroy(dev, context);
1860a8b92ca1SYishai Hadas 
1861b037c29aSEli Cohen 	bfregi = &context->bfregi;
1862c85023e1SHuy Nguyen 	mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1863146d2f1aSmajd@mellanox.com 
1864b037c29aSEli Cohen 	deallocate_uars(dev, context);
1865b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
18662f5ff264SEli Cohen 	kfree(bfregi->count);
1867e126ba97SEli Cohen 	kfree(context);
1868e126ba97SEli Cohen 
1869e126ba97SEli Cohen 	return 0;
1870e126ba97SEli Cohen }
1871e126ba97SEli Cohen 
1872b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
18734ed131d0SYishai Hadas 				 int uar_idx)
1874e126ba97SEli Cohen {
1875b037c29aSEli Cohen 	int fw_uars_per_page;
1876b037c29aSEli Cohen 
1877b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1878b037c29aSEli Cohen 
18794ed131d0SYishai Hadas 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
1880e126ba97SEli Cohen }
1881e126ba97SEli Cohen 
1882e126ba97SEli Cohen static int get_command(unsigned long offset)
1883e126ba97SEli Cohen {
1884e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1885e126ba97SEli Cohen }
1886e126ba97SEli Cohen 
1887e126ba97SEli Cohen static int get_arg(unsigned long offset)
1888e126ba97SEli Cohen {
1889e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1890e126ba97SEli Cohen }
1891e126ba97SEli Cohen 
1892e126ba97SEli Cohen static int get_index(unsigned long offset)
1893e126ba97SEli Cohen {
1894e126ba97SEli Cohen 	return get_arg(offset);
1895e126ba97SEli Cohen }
1896e126ba97SEli Cohen 
18974ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
18984ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
18994ed131d0SYishai Hadas {
19004ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
19014ed131d0SYishai Hadas }
19024ed131d0SYishai Hadas 
19037c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
19047c2344c3SMaor Gottlieb {
19057c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
19067c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
19077c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
19087c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
19097c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
19107c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
19117c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
19127c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
19137c2344c3SMaor Gottlieb 	 */
19147c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
19157c2344c3SMaor Gottlieb }
19167c2344c3SMaor Gottlieb 
19177c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
19187c2344c3SMaor Gottlieb {
19197c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
19207c2344c3SMaor Gottlieb 
19217c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
19227c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
19237c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
19247c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
19257c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
19267c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
19277c2344c3SMaor Gottlieb 	 * process is exiting.
19287c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
19297c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
19307c2344c3SMaor Gottlieb 	 */
19317c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
19327c2344c3SMaor Gottlieb 
19337c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
19347c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
19357c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
19367c2344c3SMaor Gottlieb 	 */
19377c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
1938ad9a3668SMajd Dibbiny 	mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
19397c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
1940ad9a3668SMajd Dibbiny 	mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
19417c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
19427c2344c3SMaor Gottlieb }
19437c2344c3SMaor Gottlieb 
19447c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
19457c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
19467c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
19477c2344c3SMaor Gottlieb };
19487c2344c3SMaor Gottlieb 
19497c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
19507c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
19517c2344c3SMaor Gottlieb {
19527c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
19537c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
19547c2344c3SMaor Gottlieb 
19557c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
19567c2344c3SMaor Gottlieb 	if (!vma_prv)
19577c2344c3SMaor Gottlieb 		return -ENOMEM;
19587c2344c3SMaor Gottlieb 
19597c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
1960ad9a3668SMajd Dibbiny 	vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
19617c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
19627c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
19637c2344c3SMaor Gottlieb 
1964ad9a3668SMajd Dibbiny 	mutex_lock(&ctx->vma_private_list_mutex);
19657c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
1966ad9a3668SMajd Dibbiny 	mutex_unlock(&ctx->vma_private_list_mutex);
19677c2344c3SMaor Gottlieb 
19687c2344c3SMaor Gottlieb 	return 0;
19697c2344c3SMaor Gottlieb }
19707c2344c3SMaor Gottlieb 
19717c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
19727c2344c3SMaor Gottlieb {
19737c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
19747c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
19757c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
19767c2344c3SMaor Gottlieb 
1977ad9a3668SMajd Dibbiny 	mutex_lock(&context->vma_private_list_mutex);
19787c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
19797c2344c3SMaor Gottlieb 				 list) {
19807c2344c3SMaor Gottlieb 		vma = vma_private->vma;
19812cb40791SLeon Romanovsky 		zap_vma_ptes(vma, vma->vm_start, PAGE_SIZE);
19827c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
19837c2344c3SMaor Gottlieb 		 * not access ops any more.
19847c2344c3SMaor Gottlieb 		 */
198513776612SMaor Gottlieb 		vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
19867c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
19877c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
19887c2344c3SMaor Gottlieb 		kfree(vma_private);
19897c2344c3SMaor Gottlieb 	}
1990ad9a3668SMajd Dibbiny 	mutex_unlock(&context->vma_private_list_mutex);
19917c2344c3SMaor Gottlieb }
19927c2344c3SMaor Gottlieb 
199337aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1994e126ba97SEli Cohen {
199537aa5c36SGuy Levi 	switch (cmd) {
199637aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
199737aa5c36SGuy Levi 		return "WC";
1998e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
199937aa5c36SGuy Levi 		return "best effort WC";
200037aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
200137aa5c36SGuy Levi 		return "NC";
200224da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
200324da0016SAriel Levkovich 		return "Device Memory";
200437aa5c36SGuy Levi 	default:
200537aa5c36SGuy Levi 		return NULL;
200637aa5c36SGuy Levi 	}
200737aa5c36SGuy Levi }
200837aa5c36SGuy Levi 
20095c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20105c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20115c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20125c99eaecSFeras Daoud {
20135c99eaecSFeras Daoud 	phys_addr_t pfn;
20145c99eaecSFeras Daoud 	int err;
20155c99eaecSFeras Daoud 
20165c99eaecSFeras Daoud 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
20175c99eaecSFeras Daoud 		return -EINVAL;
20185c99eaecSFeras Daoud 
20195c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20205c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20215c99eaecSFeras Daoud 
20225c99eaecSFeras Daoud 	if (vma->vm_flags & VM_WRITE)
20235c99eaecSFeras Daoud 		return -EPERM;
20245c99eaecSFeras Daoud 
20255c99eaecSFeras Daoud 	if (!dev->mdev->clock_info_page)
20265c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20275c99eaecSFeras Daoud 
20285c99eaecSFeras Daoud 	pfn = page_to_pfn(dev->mdev->clock_info_page);
20295c99eaecSFeras Daoud 	err = remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE,
20305c99eaecSFeras Daoud 			      vma->vm_page_prot);
20315c99eaecSFeras Daoud 	if (err)
20325c99eaecSFeras Daoud 		return err;
20335c99eaecSFeras Daoud 
20345c99eaecSFeras Daoud 	return mlx5_ib_set_vma_data(vma, context);
20355c99eaecSFeras Daoud }
20365c99eaecSFeras Daoud 
203737aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
20387c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
20397c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
204037aa5c36SGuy Levi {
20412f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
204237aa5c36SGuy Levi 	int err;
204337aa5c36SGuy Levi 	unsigned long idx;
204437aa5c36SGuy Levi 	phys_addr_t pfn, pa;
204537aa5c36SGuy Levi 	pgprot_t prot;
20464ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
20474ed131d0SYishai Hadas 	u32 uar_index;
20484ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
20494ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
20504ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2051b037c29aSEli Cohen 
2052b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2053b037c29aSEli Cohen 		return -EINVAL;
2054b037c29aSEli Cohen 
20554ed131d0SYishai Hadas 	if (dyn_uar)
20564ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
20574ed131d0SYishai Hadas 	else
2058b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
20594ed131d0SYishai Hadas 
20604ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
20614ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
20624ed131d0SYishai Hadas 			     idx, max_valid_idx);
2063b037c29aSEli Cohen 		return -EINVAL;
2064b037c29aSEli Cohen 	}
206537aa5c36SGuy Levi 
206637aa5c36SGuy Levi 	switch (cmd) {
206737aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
20684ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
206937aa5c36SGuy Levi /* Some architectures don't support WC memory */
207037aa5c36SGuy Levi #if defined(CONFIG_X86)
207137aa5c36SGuy Levi 		if (!pat_enabled())
207237aa5c36SGuy Levi 			return -EPERM;
207337aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
207437aa5c36SGuy Levi 			return -EPERM;
207537aa5c36SGuy Levi #endif
207637aa5c36SGuy Levi 	/* fall through */
207737aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
207837aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
207937aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
208037aa5c36SGuy Levi 		break;
208137aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
208237aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
208337aa5c36SGuy Levi 		break;
208437aa5c36SGuy Levi 	default:
208537aa5c36SGuy Levi 		return -EINVAL;
208637aa5c36SGuy Levi 	}
208737aa5c36SGuy Levi 
20884ed131d0SYishai Hadas 	if (dyn_uar) {
20894ed131d0SYishai Hadas 		int uars_per_page;
20904ed131d0SYishai Hadas 
20914ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
20924ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
20934ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
20944ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
20954ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
20964ed131d0SYishai Hadas 			return -EINVAL;
20974ed131d0SYishai Hadas 		}
20984ed131d0SYishai Hadas 
20994ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21004ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21014ed131d0SYishai Hadas 		 * page holds its count.
21024ed131d0SYishai Hadas 		 */
21034ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
21044ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
21054ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
21064ed131d0SYishai Hadas 			return -EINVAL;
21074ed131d0SYishai Hadas 		}
21084ed131d0SYishai Hadas 
21094ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
21104ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
21114ed131d0SYishai Hadas 
21124ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
21134ed131d0SYishai Hadas 		if (err) {
21144ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
21154ed131d0SYishai Hadas 			goto free_bfreg;
21164ed131d0SYishai Hadas 		}
21174ed131d0SYishai Hadas 	} else {
21184ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
21194ed131d0SYishai Hadas 	}
21204ed131d0SYishai Hadas 
21214ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
212237aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2123e126ba97SEli Cohen 
212437aa5c36SGuy Levi 	vma->vm_page_prot = prot;
212537aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
212637aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
212737aa5c36SGuy Levi 	if (err) {
21288f062287SLeon Romanovsky 		mlx5_ib_err(dev,
21298f062287SLeon Romanovsky 			    "io_remap_pfn_range failed with error=%d, mmap_cmd=%s\n",
21308f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
21314ed131d0SYishai Hadas 		err = -EAGAIN;
21324ed131d0SYishai Hadas 		goto err;
213337aa5c36SGuy Levi 	}
2134e126ba97SEli Cohen 
213537aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
213637aa5c36SGuy Levi 
21374ed131d0SYishai Hadas 	err = mlx5_ib_set_vma_data(vma, context);
21384ed131d0SYishai Hadas 	if (err)
21394ed131d0SYishai Hadas 		goto err;
21404ed131d0SYishai Hadas 
21414ed131d0SYishai Hadas 	if (dyn_uar)
21424ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
21434ed131d0SYishai Hadas 	return 0;
21444ed131d0SYishai Hadas 
21454ed131d0SYishai Hadas err:
21464ed131d0SYishai Hadas 	if (!dyn_uar)
21474ed131d0SYishai Hadas 		return err;
21484ed131d0SYishai Hadas 
21494ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
21504ed131d0SYishai Hadas 
21514ed131d0SYishai Hadas free_bfreg:
21524ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
21534ed131d0SYishai Hadas 
21544ed131d0SYishai Hadas 	return err;
215537aa5c36SGuy Levi }
215637aa5c36SGuy Levi 
215724da0016SAriel Levkovich static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
215824da0016SAriel Levkovich {
215924da0016SAriel Levkovich 	struct mlx5_ib_ucontext *mctx = to_mucontext(context);
216024da0016SAriel Levkovich 	struct mlx5_ib_dev *dev = to_mdev(context->device);
216124da0016SAriel Levkovich 	u16 page_idx = get_extended_index(vma->vm_pgoff);
216224da0016SAriel Levkovich 	size_t map_size = vma->vm_end - vma->vm_start;
216324da0016SAriel Levkovich 	u32 npages = map_size >> PAGE_SHIFT;
216424da0016SAriel Levkovich 	phys_addr_t pfn;
216524da0016SAriel Levkovich 	pgprot_t prot;
216624da0016SAriel Levkovich 
216724da0016SAriel Levkovich 	if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
216824da0016SAriel Levkovich 	    page_idx + npages)
216924da0016SAriel Levkovich 		return -EINVAL;
217024da0016SAriel Levkovich 
217124da0016SAriel Levkovich 	pfn = ((pci_resource_start(dev->mdev->pdev, 0) +
217224da0016SAriel Levkovich 	      MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
217324da0016SAriel Levkovich 	      PAGE_SHIFT) +
217424da0016SAriel Levkovich 	      page_idx;
217524da0016SAriel Levkovich 	prot = pgprot_writecombine(vma->vm_page_prot);
217624da0016SAriel Levkovich 	vma->vm_page_prot = prot;
217724da0016SAriel Levkovich 
217824da0016SAriel Levkovich 	if (io_remap_pfn_range(vma, vma->vm_start, pfn, map_size,
217924da0016SAriel Levkovich 			       vma->vm_page_prot))
218024da0016SAriel Levkovich 		return -EAGAIN;
218124da0016SAriel Levkovich 
218224da0016SAriel Levkovich 	return mlx5_ib_set_vma_data(vma, mctx);
218324da0016SAriel Levkovich }
218424da0016SAriel Levkovich 
218537aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
218637aa5c36SGuy Levi {
218737aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
218837aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
218937aa5c36SGuy Levi 	unsigned long command;
219037aa5c36SGuy Levi 	phys_addr_t pfn;
219137aa5c36SGuy Levi 
219237aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
219337aa5c36SGuy Levi 	switch (command) {
219437aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
219537aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
219637aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
21974ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
21987c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2199e126ba97SEli Cohen 
2200e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2201e126ba97SEli Cohen 		return -ENOSYS;
2202e126ba97SEli Cohen 
2203d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2204d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2205d69e3bcfSMatan Barak 			return -EINVAL;
2206d69e3bcfSMatan Barak 
22076cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2208d69e3bcfSMatan Barak 			return -EPERM;
2209d69e3bcfSMatan Barak 
2210d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2211d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2212d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2213d69e3bcfSMatan Barak 
2214d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2215d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2216d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2217d69e3bcfSMatan Barak 			PAGE_SHIFT;
2218d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2219d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
2220d69e3bcfSMatan Barak 			return -EAGAIN;
2221d69e3bcfSMatan Barak 		break;
22225c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
22235c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2224d69e3bcfSMatan Barak 
222524da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
222624da0016SAriel Levkovich 		return dm_mmap(ibcontext, vma);
222724da0016SAriel Levkovich 
2228e126ba97SEli Cohen 	default:
2229e126ba97SEli Cohen 		return -EINVAL;
2230e126ba97SEli Cohen 	}
2231e126ba97SEli Cohen 
2232e126ba97SEli Cohen 	return 0;
2233e126ba97SEli Cohen }
2234e126ba97SEli Cohen 
223524da0016SAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
223624da0016SAriel Levkovich 			       struct ib_ucontext *context,
223724da0016SAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
223824da0016SAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
223924da0016SAriel Levkovich {
224024da0016SAriel Levkovich 	u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
224124da0016SAriel Levkovich 	struct mlx5_memic *memic = &to_mdev(ibdev)->memic;
224224da0016SAriel Levkovich 	phys_addr_t memic_addr;
224324da0016SAriel Levkovich 	struct mlx5_ib_dm *dm;
224424da0016SAriel Levkovich 	u64 start_offset;
224524da0016SAriel Levkovich 	u32 page_idx;
224624da0016SAriel Levkovich 	int err;
224724da0016SAriel Levkovich 
224824da0016SAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
224924da0016SAriel Levkovich 	if (!dm)
225024da0016SAriel Levkovich 		return ERR_PTR(-ENOMEM);
225124da0016SAriel Levkovich 
225224da0016SAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
225324da0016SAriel Levkovich 		    attr->length, act_size, attr->alignment);
225424da0016SAriel Levkovich 
225524da0016SAriel Levkovich 	err = mlx5_cmd_alloc_memic(memic, &memic_addr,
225624da0016SAriel Levkovich 				   act_size, attr->alignment);
225724da0016SAriel Levkovich 	if (err)
225824da0016SAriel Levkovich 		goto err_free;
225924da0016SAriel Levkovich 
226024da0016SAriel Levkovich 	start_offset = memic_addr & ~PAGE_MASK;
226124da0016SAriel Levkovich 	page_idx = (memic_addr - pci_resource_start(memic->dev->pdev, 0) -
226224da0016SAriel Levkovich 		    MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
226324da0016SAriel Levkovich 		    PAGE_SHIFT;
226424da0016SAriel Levkovich 
226524da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
226624da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
226724da0016SAriel Levkovich 			     &start_offset, sizeof(start_offset));
226824da0016SAriel Levkovich 	if (err)
226924da0016SAriel Levkovich 		goto err_dealloc;
227024da0016SAriel Levkovich 
227124da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
227224da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
227324da0016SAriel Levkovich 			     &page_idx, sizeof(page_idx));
227424da0016SAriel Levkovich 	if (err)
227524da0016SAriel Levkovich 		goto err_dealloc;
227624da0016SAriel Levkovich 
227724da0016SAriel Levkovich 	bitmap_set(to_mucontext(context)->dm_pages, page_idx,
227824da0016SAriel Levkovich 		   DIV_ROUND_UP(act_size, PAGE_SIZE));
227924da0016SAriel Levkovich 
228024da0016SAriel Levkovich 	dm->dev_addr = memic_addr;
228124da0016SAriel Levkovich 
228224da0016SAriel Levkovich 	return &dm->ibdm;
228324da0016SAriel Levkovich 
228424da0016SAriel Levkovich err_dealloc:
228524da0016SAriel Levkovich 	mlx5_cmd_dealloc_memic(memic, memic_addr,
228624da0016SAriel Levkovich 			       act_size);
228724da0016SAriel Levkovich err_free:
228824da0016SAriel Levkovich 	kfree(dm);
228924da0016SAriel Levkovich 	return ERR_PTR(err);
229024da0016SAriel Levkovich }
229124da0016SAriel Levkovich 
229224da0016SAriel Levkovich int mlx5_ib_dealloc_dm(struct ib_dm *ibdm)
229324da0016SAriel Levkovich {
229424da0016SAriel Levkovich 	struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic;
229524da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
229624da0016SAriel Levkovich 	u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE);
229724da0016SAriel Levkovich 	u32 page_idx;
229824da0016SAriel Levkovich 	int ret;
229924da0016SAriel Levkovich 
230024da0016SAriel Levkovich 	ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size);
230124da0016SAriel Levkovich 	if (ret)
230224da0016SAriel Levkovich 		return ret;
230324da0016SAriel Levkovich 
230424da0016SAriel Levkovich 	page_idx = (dm->dev_addr - pci_resource_start(memic->dev->pdev, 0) -
230524da0016SAriel Levkovich 		    MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
230624da0016SAriel Levkovich 		    PAGE_SHIFT;
230724da0016SAriel Levkovich 	bitmap_clear(to_mucontext(ibdm->uobject->context)->dm_pages,
230824da0016SAriel Levkovich 		     page_idx,
230924da0016SAriel Levkovich 		     DIV_ROUND_UP(act_size, PAGE_SIZE));
231024da0016SAriel Levkovich 
231124da0016SAriel Levkovich 	kfree(dm);
231224da0016SAriel Levkovich 
231324da0016SAriel Levkovich 	return 0;
231424da0016SAriel Levkovich }
231524da0016SAriel Levkovich 
2316e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
2317e126ba97SEli Cohen 				      struct ib_ucontext *context,
2318e126ba97SEli Cohen 				      struct ib_udata *udata)
2319e126ba97SEli Cohen {
2320e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2321e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
2322e126ba97SEli Cohen 	int err;
2323e126ba97SEli Cohen 
2324e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
2325e126ba97SEli Cohen 	if (!pd)
2326e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
2327e126ba97SEli Cohen 
23289603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
2329e126ba97SEli Cohen 	if (err) {
2330e126ba97SEli Cohen 		kfree(pd);
2331e126ba97SEli Cohen 		return ERR_PTR(err);
2332e126ba97SEli Cohen 	}
2333e126ba97SEli Cohen 
2334e126ba97SEli Cohen 	if (context) {
2335e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2336e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
23379603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
2338e126ba97SEli Cohen 			kfree(pd);
2339e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
2340e126ba97SEli Cohen 		}
2341e126ba97SEli Cohen 	}
2342e126ba97SEli Cohen 
2343e126ba97SEli Cohen 	return &pd->ibpd;
2344e126ba97SEli Cohen }
2345e126ba97SEli Cohen 
2346e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
2347e126ba97SEli Cohen {
2348e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2349e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2350e126ba97SEli Cohen 
23519603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
2352e126ba97SEli Cohen 	kfree(mpd);
2353e126ba97SEli Cohen 
2354e126ba97SEli Cohen 	return 0;
2355e126ba97SEli Cohen }
2356e126ba97SEli Cohen 
2357466fa6d2SMaor Gottlieb enum {
2358466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2359466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
236071c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_INNER_BIT,
236171c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_MISC2_BIT
2362466fa6d2SMaor Gottlieb };
2363038d2ef8SMaor Gottlieb 
2364466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2365466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2366466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2367466fa6d2SMaor Gottlieb 
2368466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2369466fa6d2SMaor Gottlieb {
2370466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2371466fa6d2SMaor Gottlieb 
2372466fa6d2SMaor Gottlieb 	match_criteria_enable =
2373466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2374466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2375466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2376466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2377466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2378466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2379466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2380466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
238171c6e863SAriel Levkovich 	match_criteria_enable |=
238271c6e863SAriel Levkovich 		(!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
238371c6e863SAriel Levkovich 		MATCH_CRITERIA_ENABLE_MISC2_BIT;
2384466fa6d2SMaor Gottlieb 
2385466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2386038d2ef8SMaor Gottlieb }
2387038d2ef8SMaor Gottlieb 
2388ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2389ca0d4753SMaor Gottlieb {
2390ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2391ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2392ca0d4753SMaor Gottlieb }
2393ca0d4753SMaor Gottlieb 
239437da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
23952d1e697eSMoses Reuben 			   bool inner)
23962d1e697eSMoses Reuben {
23972d1e697eSMoses Reuben 	if (inner) {
23982d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
23992d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
24002d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24012d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
24022d1e697eSMoses Reuben 	} else {
24032d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24042d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
24052d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
24062d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
24072d1e697eSMoses Reuben 	}
24082d1e697eSMoses Reuben }
24092d1e697eSMoses Reuben 
2410ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2411ca0d4753SMaor Gottlieb {
2412ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2413ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2414ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2415ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2416ca0d4753SMaor Gottlieb }
2417ca0d4753SMaor Gottlieb 
241871c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
241971c6e863SAriel Levkovich {
242071c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
242171c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
242271c6e863SAriel Levkovich 		return -EOPNOTSUPP;
242371c6e863SAriel Levkovich 
242471c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
242571c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
242671c6e863SAriel Levkovich 		return -EOPNOTSUPP;
242771c6e863SAriel Levkovich 
242871c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
242971c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
243071c6e863SAriel Levkovich 		return -EOPNOTSUPP;
243171c6e863SAriel Levkovich 
243271c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
243371c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
243471c6e863SAriel Levkovich 		return -EOPNOTSUPP;
243571c6e863SAriel Levkovich 
243671c6e863SAriel Levkovich 	return 0;
243771c6e863SAriel Levkovich }
243871c6e863SAriel Levkovich 
2439c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2440c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2441ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2442466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2443c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2444ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
24452ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2446a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
24473b3233fbSRaed Salem #define LAST_COUNTERS_FIELD counters
2448c47ac6aeSMaor Gottlieb 
2449c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2450c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2451c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2452c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2453c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2454c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2455c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2456c47ac6aeSMaor Gottlieb 
2457802c2125SAviad Yehezkel static int parse_flow_flow_action(const union ib_flow_spec *ib_spec,
2458802c2125SAviad Yehezkel 				  const struct ib_flow_attr *flow_attr,
2459802c2125SAviad Yehezkel 				  struct mlx5_flow_act *action)
2460802c2125SAviad Yehezkel {
2461802c2125SAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(ib_spec->action.act);
2462802c2125SAviad Yehezkel 
2463802c2125SAviad Yehezkel 	switch (maction->ib_action.type) {
2464802c2125SAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
2465802c2125SAviad Yehezkel 		/* Currently only AES_GCM keymat is supported by the driver */
2466802c2125SAviad Yehezkel 		action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
2467802c2125SAviad Yehezkel 		action->action |= flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS ?
2468802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2469802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2470802c2125SAviad Yehezkel 		return 0;
2471802c2125SAviad Yehezkel 	default:
2472802c2125SAviad Yehezkel 		return -EOPNOTSUPP;
2473802c2125SAviad Yehezkel 	}
2474802c2125SAviad Yehezkel }
2475802c2125SAviad Yehezkel 
247619cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
247719cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
2478802c2125SAviad Yehezkel 			   const struct ib_flow_attr *flow_attr,
247971c6e863SAriel Levkovich 			   struct mlx5_flow_act *action, u32 prev_type)
2480038d2ef8SMaor Gottlieb {
2481466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2482466fa6d2SMaor Gottlieb 					   misc_parameters);
2483466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2484466fa6d2SMaor Gottlieb 					   misc_parameters);
248571c6e863SAriel Levkovich 	void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
248671c6e863SAriel Levkovich 					    misc_parameters_2);
248771c6e863SAriel Levkovich 	void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
248871c6e863SAriel Levkovich 					    misc_parameters_2);
24892d1e697eSMoses Reuben 	void *headers_c;
24902d1e697eSMoses Reuben 	void *headers_v;
249119cc7524SAriel Levkovich 	int match_ipv;
2492802c2125SAviad Yehezkel 	int ret;
2493466fa6d2SMaor Gottlieb 
24942d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
24952d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
24962d1e697eSMoses Reuben 					 inner_headers);
24972d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
24982d1e697eSMoses Reuben 					 inner_headers);
249919cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
250019cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
25012d1e697eSMoses Reuben 	} else {
25022d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
25032d1e697eSMoses Reuben 					 outer_headers);
25042d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
25052d1e697eSMoses Reuben 					 outer_headers);
250619cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
250719cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
25082d1e697eSMoses Reuben 	}
25092d1e697eSMoses Reuben 
25102d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2511038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2512c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
25131ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2514038d2ef8SMaor Gottlieb 
25152d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2516038d2ef8SMaor Gottlieb 					     dmac_47_16),
2517038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
25182d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2519038d2ef8SMaor Gottlieb 					     dmac_47_16),
2520038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2521038d2ef8SMaor Gottlieb 
25222d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2523ee3da804SMaor Gottlieb 					     smac_47_16),
2524ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
25252d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2526ee3da804SMaor Gottlieb 					     smac_47_16),
2527ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2528ee3da804SMaor Gottlieb 
2529038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
25302d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
253110543365SMohamad Haj Yahia 				 cvlan_tag, 1);
25322d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
253310543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2534038d2ef8SMaor Gottlieb 
25352d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2536038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
25372d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2538038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2539038d2ef8SMaor Gottlieb 
25402d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2541038d2ef8SMaor Gottlieb 				 first_cfi,
2542038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
25432d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2544038d2ef8SMaor Gottlieb 				 first_cfi,
2545038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2546038d2ef8SMaor Gottlieb 
25472d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2548038d2ef8SMaor Gottlieb 				 first_prio,
2549038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
25502d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2551038d2ef8SMaor Gottlieb 				 first_prio,
2552038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2553038d2ef8SMaor Gottlieb 		}
25542d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2555038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
25562d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2557038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2558038d2ef8SMaor Gottlieb 		break;
2559038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2560c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
25611ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2562038d2ef8SMaor Gottlieb 
256319cc7524SAriel Levkovich 		if (match_ipv) {
256419cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
256519cc7524SAriel Levkovich 				 ip_version, 0xf);
256619cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
25673346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV4_VERSION);
256819cc7524SAriel Levkovich 		} else {
25692d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2570038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
25712d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2572038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
257319cc7524SAriel Levkovich 		}
2574038d2ef8SMaor Gottlieb 
25752d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2576038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2577038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2578038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
25792d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2580038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2581038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2582038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
25832d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2584038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2585038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2586038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
25872d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2588038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2589038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2590038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2591ca0d4753SMaor Gottlieb 
25922d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2593ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2594ca0d4753SMaor Gottlieb 
25952d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2596ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
2597038d2ef8SMaor Gottlieb 		break;
2598026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2599c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
26001ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2601026bae0cSMaor Gottlieb 
260219cc7524SAriel Levkovich 		if (match_ipv) {
260319cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
260419cc7524SAriel Levkovich 				 ip_version, 0xf);
260519cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
26063346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV6_VERSION);
260719cc7524SAriel Levkovich 		} else {
26082d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2609026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
26102d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2611026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
261219cc7524SAriel Levkovich 		}
2613026bae0cSMaor Gottlieb 
26142d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2615026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2616026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2617026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
26182d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2619026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2620026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2621026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
26222d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2623026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2624026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2625026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
26262d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2627026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2628026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2629026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2630466fa6d2SMaor Gottlieb 
26312d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2632466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2633466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2634466fa6d2SMaor Gottlieb 
26352d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2636466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
2637466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
2638466fa6d2SMaor Gottlieb 
26392d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
26402d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
26412d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
26422d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
2643802c2125SAviad Yehezkel 		break;
2644802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ESP:
2645802c2125SAviad Yehezkel 		if (ib_spec->esp.mask.seq)
2646802c2125SAviad Yehezkel 			return -EOPNOTSUPP;
26472d1e697eSMoses Reuben 
2648802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2649802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.mask.spi));
2650802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2651802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.val.spi));
2652026bae0cSMaor Gottlieb 		break;
2653038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2654c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2655c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
26561ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2657038d2ef8SMaor Gottlieb 
26582d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2659038d2ef8SMaor Gottlieb 			 0xff);
26602d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2661038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
2662038d2ef8SMaor Gottlieb 
26632d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2664038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
26652d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2666038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2667038d2ef8SMaor Gottlieb 
26682d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2669038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
26702d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2671038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2672038d2ef8SMaor Gottlieb 		break;
2673038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2674c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2675c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
26761ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2677038d2ef8SMaor Gottlieb 
26782d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2679038d2ef8SMaor Gottlieb 			 0xff);
26802d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2681038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
2682038d2ef8SMaor Gottlieb 
26832d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2684038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
26852d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2686038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2687038d2ef8SMaor Gottlieb 
26882d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2689038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
26902d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2691038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2692038d2ef8SMaor Gottlieb 		break;
2693da2f22aeSAriel Levkovich 	case IB_FLOW_SPEC_GRE:
2694da2f22aeSAriel Levkovich 		if (ib_spec->gre.mask.c_ks_res0_ver)
2695da2f22aeSAriel Levkovich 			return -EOPNOTSUPP;
2696da2f22aeSAriel Levkovich 
2697da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2698da2f22aeSAriel Levkovich 			 0xff);
2699da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2700da2f22aeSAriel Levkovich 			 IPPROTO_GRE);
2701da2f22aeSAriel Levkovich 
2702da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
2703a93b632cSMaor Gottlieb 			 ntohs(ib_spec->gre.mask.protocol));
2704da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2705da2f22aeSAriel Levkovich 			 ntohs(ib_spec->gre.val.protocol));
2706da2f22aeSAriel Levkovich 
2707da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
2708da2f22aeSAriel Levkovich 				    gre_key_h),
2709da2f22aeSAriel Levkovich 		       &ib_spec->gre.mask.key,
2710da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.mask.key));
2711da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
2712da2f22aeSAriel Levkovich 				    gre_key_h),
2713da2f22aeSAriel Levkovich 		       &ib_spec->gre.val.key,
2714da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.val.key));
2715da2f22aeSAriel Levkovich 		break;
271671c6e863SAriel Levkovich 	case IB_FLOW_SPEC_MPLS:
271771c6e863SAriel Levkovich 		switch (prev_type) {
271871c6e863SAriel Levkovich 		case IB_FLOW_SPEC_UDP:
271971c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
272071c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_udp),
272171c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
272271c6e863SAriel Levkovich 				return -EOPNOTSUPP;
272371c6e863SAriel Levkovich 
272471c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
272571c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
272671c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
272771c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
272871c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
272971c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
273071c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
273171c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
273271c6e863SAriel Levkovich 			break;
273371c6e863SAriel Levkovich 		case IB_FLOW_SPEC_GRE:
273471c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
273571c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_gre),
273671c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
273771c6e863SAriel Levkovich 				return -EOPNOTSUPP;
273871c6e863SAriel Levkovich 
273971c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
274071c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
274171c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
274271c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
274371c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
274471c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
274571c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
274671c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
274771c6e863SAriel Levkovich 			break;
274871c6e863SAriel Levkovich 		default:
274971c6e863SAriel Levkovich 			if (ib_spec->type & IB_FLOW_SPEC_INNER) {
275071c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
275171c6e863SAriel Levkovich 							   ft_field_support.inner_first_mpls),
275271c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
275371c6e863SAriel Levkovich 					return -EOPNOTSUPP;
275471c6e863SAriel Levkovich 
275571c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
275671c6e863SAriel Levkovich 						    inner_first_mpls),
275771c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
275871c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
275971c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
276071c6e863SAriel Levkovich 						    inner_first_mpls),
276171c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
276271c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
276371c6e863SAriel Levkovich 			} else {
276471c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
276571c6e863SAriel Levkovich 							   ft_field_support.outer_first_mpls),
276671c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
276771c6e863SAriel Levkovich 					return -EOPNOTSUPP;
276871c6e863SAriel Levkovich 
276971c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
277071c6e863SAriel Levkovich 						    outer_first_mpls),
277171c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
277271c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
277371c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
277471c6e863SAriel Levkovich 						    outer_first_mpls),
277571c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
277671c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
277771c6e863SAriel Levkovich 			}
277871c6e863SAriel Levkovich 		}
277971c6e863SAriel Levkovich 		break;
2780ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2781ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2782ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
27831ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2784ffb30d8fSMoses Reuben 
2785ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2786ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2787ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2788ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2789ffb30d8fSMoses Reuben 		break;
27902ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
27912ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
27922ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
27932ac693f9SMoses Reuben 			return -EOPNOTSUPP;
27942ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
27952ac693f9SMoses Reuben 			return -EINVAL;
27962ac693f9SMoses Reuben 
2797075572d4SBoris Pismenny 		action->flow_tag = ib_spec->flow_tag.tag_id;
2798a9db0ecfSMatan Barak 		action->has_flow_tag = true;
27992ac693f9SMoses Reuben 		break;
2800a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2801a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2802a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2803a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2804075572d4SBoris Pismenny 		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
2805a22ed86cSSlava Shwartsman 		break;
2806802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ACTION_HANDLE:
2807802c2125SAviad Yehezkel 		ret = parse_flow_flow_action(ib_spec, flow_attr, action);
2808802c2125SAviad Yehezkel 		if (ret)
2809802c2125SAviad Yehezkel 			return ret;
2810802c2125SAviad Yehezkel 		break;
28113b3233fbSRaed Salem 	case IB_FLOW_SPEC_ACTION_COUNT:
28123b3233fbSRaed Salem 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
28133b3233fbSRaed Salem 					 LAST_COUNTERS_FIELD))
28143b3233fbSRaed Salem 			return -EOPNOTSUPP;
28153b3233fbSRaed Salem 
28163b3233fbSRaed Salem 		/* for now support only one counters spec per flow */
28173b3233fbSRaed Salem 		if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
28183b3233fbSRaed Salem 			return -EINVAL;
28193b3233fbSRaed Salem 
28203b3233fbSRaed Salem 		action->counters = ib_spec->flow_count.counters;
28213b3233fbSRaed Salem 		action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
28223b3233fbSRaed Salem 		break;
2823038d2ef8SMaor Gottlieb 	default:
2824038d2ef8SMaor Gottlieb 		return -EINVAL;
2825038d2ef8SMaor Gottlieb 	}
2826038d2ef8SMaor Gottlieb 
2827038d2ef8SMaor Gottlieb 	return 0;
2828038d2ef8SMaor Gottlieb }
2829038d2ef8SMaor Gottlieb 
2830038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
2831038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
2832038d2ef8SMaor Gottlieb  * could steal other multicast packets.
2833038d2ef8SMaor Gottlieb  */
2834a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2835038d2ef8SMaor Gottlieb {
283681e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
2837038d2ef8SMaor Gottlieb 
2838038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2839038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
2840038d2ef8SMaor Gottlieb 		return false;
2841038d2ef8SMaor Gottlieb 
284281e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
284381e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
284481e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
2845038d2ef8SMaor Gottlieb 
284681e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
284781e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
284881e30880SYishai Hadas 			return true;
284981e30880SYishai Hadas 
285081e30880SYishai Hadas 		return false;
285181e30880SYishai Hadas 	}
285281e30880SYishai Hadas 
285381e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
285481e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
285581e30880SYishai Hadas 
285681e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2857038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2858038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
2859038d2ef8SMaor Gottlieb 	}
2860038d2ef8SMaor Gottlieb 
286181e30880SYishai Hadas 	return false;
286281e30880SYishai Hadas }
286381e30880SYishai Hadas 
2864802c2125SAviad Yehezkel enum valid_spec {
2865802c2125SAviad Yehezkel 	VALID_SPEC_INVALID,
2866802c2125SAviad Yehezkel 	VALID_SPEC_VALID,
2867802c2125SAviad Yehezkel 	VALID_SPEC_NA,
2868802c2125SAviad Yehezkel };
2869802c2125SAviad Yehezkel 
2870802c2125SAviad Yehezkel static enum valid_spec
2871802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
2872802c2125SAviad Yehezkel 		     const struct mlx5_flow_spec *spec,
2873802c2125SAviad Yehezkel 		     const struct mlx5_flow_act *flow_act,
2874802c2125SAviad Yehezkel 		     bool egress)
2875802c2125SAviad Yehezkel {
2876802c2125SAviad Yehezkel 	const u32 *match_c = spec->match_criteria;
2877802c2125SAviad Yehezkel 	bool is_crypto =
2878802c2125SAviad Yehezkel 		(flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2879802c2125SAviad Yehezkel 				     MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
2880802c2125SAviad Yehezkel 	bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
2881802c2125SAviad Yehezkel 	bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
2882802c2125SAviad Yehezkel 
2883802c2125SAviad Yehezkel 	/*
2884802c2125SAviad Yehezkel 	 * Currently only crypto is supported in egress, when regular egress
2885802c2125SAviad Yehezkel 	 * rules would be supported, always return VALID_SPEC_NA.
2886802c2125SAviad Yehezkel 	 */
2887802c2125SAviad Yehezkel 	if (!is_crypto)
2888802c2125SAviad Yehezkel 		return egress ? VALID_SPEC_INVALID : VALID_SPEC_NA;
2889802c2125SAviad Yehezkel 
2890802c2125SAviad Yehezkel 	return is_crypto && is_ipsec &&
2891802c2125SAviad Yehezkel 		(!egress || (!is_drop && !flow_act->has_flow_tag)) ?
2892802c2125SAviad Yehezkel 		VALID_SPEC_VALID : VALID_SPEC_INVALID;
2893802c2125SAviad Yehezkel }
2894802c2125SAviad Yehezkel 
2895802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev,
2896802c2125SAviad Yehezkel 			  const struct mlx5_flow_spec *spec,
2897802c2125SAviad Yehezkel 			  const struct mlx5_flow_act *flow_act,
2898802c2125SAviad Yehezkel 			  bool egress)
2899802c2125SAviad Yehezkel {
2900802c2125SAviad Yehezkel 	/* We curretly only support ipsec egress flow */
2901802c2125SAviad Yehezkel 	return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
2902802c2125SAviad Yehezkel }
2903802c2125SAviad Yehezkel 
290419cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
290519cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
29060f750966SAriel Levkovich 			       bool check_inner)
2907038d2ef8SMaor Gottlieb {
2908038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
290919cc7524SAriel Levkovich 	int match_ipv = check_inner ?
291019cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
291119cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
291219cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
291319cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
29140f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
29150f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
29160f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
29170f750966SAriel Levkovich 	bool has_ethertype = false;
2918038d2ef8SMaor Gottlieb 	unsigned int spec_index;
29190f750966SAriel Levkovich 	bool mask_valid = true;
29200f750966SAriel Levkovich 	u16 eth_type = 0;
29210f750966SAriel Levkovich 	bool type_valid;
2922038d2ef8SMaor Gottlieb 
2923038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
2924038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
29250f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2926038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
29270f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
29280f750966SAriel Levkovich 				      htons(0xffff));
29290f750966SAriel Levkovich 			has_ethertype = true;
29300f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
29310f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
29320f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
29330f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
2934038d2ef8SMaor Gottlieb 		}
2935038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
2936038d2ef8SMaor Gottlieb 	}
29370f750966SAriel Levkovich 
29380f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
29390f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
29400f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
29410f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
29420f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
29430f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
294419cc7524SAriel Levkovich 
294519cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
294619cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
294719cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
29480f750966SAriel Levkovich 	}
29490f750966SAriel Levkovich 
29500f750966SAriel Levkovich 	return type_valid;
29510f750966SAriel Levkovich }
29520f750966SAriel Levkovich 
295319cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
295419cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
29550f750966SAriel Levkovich {
295619cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
295719cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
2958038d2ef8SMaor Gottlieb }
2959038d2ef8SMaor Gottlieb 
2960038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
2961038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
2962038d2ef8SMaor Gottlieb {
2963038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
2964038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
2965038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
2966038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
2967038d2ef8SMaor Gottlieb 	}
2968038d2ef8SMaor Gottlieb }
2969038d2ef8SMaor Gottlieb 
29703b3233fbSRaed Salem static void counters_clear_description(struct ib_counters *counters)
29713b3233fbSRaed Salem {
29723b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
29733b3233fbSRaed Salem 
29743b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
29753b3233fbSRaed Salem 	kfree(mcounters->counters_data);
29763b3233fbSRaed Salem 	mcounters->counters_data = NULL;
29773b3233fbSRaed Salem 	mcounters->cntrs_max_index = 0;
29783b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
29793b3233fbSRaed Salem }
29803b3233fbSRaed Salem 
2981038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2982038d2ef8SMaor Gottlieb {
2983038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2984038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2985038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2986038d2ef8SMaor Gottlieb 							  ibflow);
2987038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2988038d2ef8SMaor Gottlieb 
29899a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
2990038d2ef8SMaor Gottlieb 
2991038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
299274491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2993cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
2994038d2ef8SMaor Gottlieb 		list_del(&iter->list);
2995038d2ef8SMaor Gottlieb 		kfree(iter);
2996038d2ef8SMaor Gottlieb 	}
2997038d2ef8SMaor Gottlieb 
299874491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
29995497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
30003b3233fbSRaed Salem 	if (handler->ibcounters &&
30013b3233fbSRaed Salem 	    atomic_read(&handler->ibcounters->usecnt) == 1)
30023b3233fbSRaed Salem 		counters_clear_description(handler->ibcounters);
3003038d2ef8SMaor Gottlieb 
30043b3233fbSRaed Salem 	mutex_unlock(&dev->flow_db->lock);
3005038d2ef8SMaor Gottlieb 	kfree(handler);
3006038d2ef8SMaor Gottlieb 
3007038d2ef8SMaor Gottlieb 	return 0;
3008038d2ef8SMaor Gottlieb }
3009038d2ef8SMaor Gottlieb 
301035d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
301135d19011SMaor Gottlieb {
301235d19011SMaor Gottlieb 	priority *= 2;
301335d19011SMaor Gottlieb 	if (!dont_trap)
301435d19011SMaor Gottlieb 		priority++;
301535d19011SMaor Gottlieb 	return priority;
301635d19011SMaor Gottlieb }
301735d19011SMaor Gottlieb 
3018cc0e5d42SMaor Gottlieb enum flow_table_type {
3019cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
3020cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
3021cc0e5d42SMaor Gottlieb };
3022cc0e5d42SMaor Gottlieb 
302300b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
302400b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
3025038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
3026cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
3027cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
3028038d2ef8SMaor Gottlieb {
302935d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
3030038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
3031038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
3032038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
3033dac388efSMaor Gottlieb 	int max_table_size;
3034038d2ef8SMaor Gottlieb 	int num_entries;
3035038d2ef8SMaor Gottlieb 	int num_groups;
3036038d2ef8SMaor Gottlieb 	int priority;
3037038d2ef8SMaor Gottlieb 	int err = 0;
3038038d2ef8SMaor Gottlieb 
3039dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3040dac388efSMaor Gottlieb 						       log_max_ft_size));
3041038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
3042802c2125SAviad Yehezkel 		if (ft_type == MLX5_IB_FT_TX)
3043802c2125SAviad Yehezkel 			priority = 0;
3044802c2125SAviad Yehezkel 		else if (flow_is_multicast_only(flow_attr) &&
304535d19011SMaor Gottlieb 			 !dont_trap)
3046038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
3047038d2ef8SMaor Gottlieb 		else
304835d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
304935d19011SMaor Gottlieb 							dont_trap);
3050038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
3051802c2125SAviad Yehezkel 					     ft_type == MLX5_IB_FT_TX ?
3052802c2125SAviad Yehezkel 					     MLX5_FLOW_NAMESPACE_EGRESS :
3053038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
3054038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
3055038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
30569a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[priority];
3057038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3058038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3059038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
3060038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
3061038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
3062038d2ef8SMaor Gottlieb 					 &num_entries,
3063038d2ef8SMaor Gottlieb 					 &num_groups);
30649a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
3065cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3066cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3067cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
3068cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
3069cc0e5d42SMaor Gottlieb 
3070cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3071cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3072cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3073cc0e5d42SMaor Gottlieb 
30749a4ca38dSMark Bloch 		prio = &dev->flow_db->sniffer[ft_type];
3075cc0e5d42SMaor Gottlieb 		priority = 0;
3076cc0e5d42SMaor Gottlieb 		num_entries = 1;
3077cc0e5d42SMaor Gottlieb 		num_groups = 1;
3078038d2ef8SMaor Gottlieb 	}
3079038d2ef8SMaor Gottlieb 
3080038d2ef8SMaor Gottlieb 	if (!ns)
3081038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
3082038d2ef8SMaor Gottlieb 
3083dac388efSMaor Gottlieb 	if (num_entries > max_table_size)
3084dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
3085dac388efSMaor Gottlieb 
3086038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
3087038d2ef8SMaor Gottlieb 	if (!ft) {
3088038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3089038d2ef8SMaor Gottlieb 							 num_entries,
3090d63cd286SMaor Gottlieb 							 num_groups,
3091c9f1b073SHadar Hen Zion 							 0, 0);
3092038d2ef8SMaor Gottlieb 
3093038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
3094038d2ef8SMaor Gottlieb 			prio->refcount = 0;
3095038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
3096038d2ef8SMaor Gottlieb 		} else {
3097038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
3098038d2ef8SMaor Gottlieb 		}
3099038d2ef8SMaor Gottlieb 	}
3100038d2ef8SMaor Gottlieb 
3101038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
3102038d2ef8SMaor Gottlieb }
3103038d2ef8SMaor Gottlieb 
3104a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
3105a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
3106a550ddfcSYishai Hadas 			    u32 underlay_qpn)
3107a550ddfcSYishai Hadas {
3108a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3109a550ddfcSYishai Hadas 					   spec->match_criteria,
3110a550ddfcSYishai Hadas 					   misc_parameters);
3111a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3112a550ddfcSYishai Hadas 					   misc_parameters);
3113a550ddfcSYishai Hadas 
3114a550ddfcSYishai Hadas 	if (underlay_qpn &&
3115a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3116a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
3117a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3118a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
3119a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3120a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
3121a550ddfcSYishai Hadas 	}
3122a550ddfcSYishai Hadas }
3123a550ddfcSYishai Hadas 
31245e95af5fSRaed Salem static int read_flow_counters(struct ib_device *ibdev,
31255e95af5fSRaed Salem 			      struct mlx5_read_counters_attr *read_attr)
31265e95af5fSRaed Salem {
31275e95af5fSRaed Salem 	struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
31285e95af5fSRaed Salem 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
31295e95af5fSRaed Salem 
31305e95af5fSRaed Salem 	return mlx5_fc_query(dev->mdev, fc,
31315e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_PACKETS],
31325e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_BYTES]);
31335e95af5fSRaed Salem }
31345e95af5fSRaed Salem 
31355e95af5fSRaed Salem /* flow counters currently expose two counters packets and bytes */
31365e95af5fSRaed Salem #define FLOW_COUNTERS_NUM 2
31373b3233fbSRaed Salem static int counters_set_description(struct ib_counters *counters,
31383b3233fbSRaed Salem 				    enum mlx5_ib_counters_type counters_type,
31393b3233fbSRaed Salem 				    struct mlx5_ib_flow_counters_desc *desc_data,
31403b3233fbSRaed Salem 				    u32 ncounters)
31413b3233fbSRaed Salem {
31423b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
31433b3233fbSRaed Salem 	u32 cntrs_max_index = 0;
31443b3233fbSRaed Salem 	int i;
31453b3233fbSRaed Salem 
31463b3233fbSRaed Salem 	if (counters_type != MLX5_IB_COUNTERS_FLOW)
31473b3233fbSRaed Salem 		return -EINVAL;
31483b3233fbSRaed Salem 
31493b3233fbSRaed Salem 	/* init the fields for the object */
31503b3233fbSRaed Salem 	mcounters->type = counters_type;
31515e95af5fSRaed Salem 	mcounters->read_counters = read_flow_counters;
31525e95af5fSRaed Salem 	mcounters->counters_num = FLOW_COUNTERS_NUM;
31533b3233fbSRaed Salem 	mcounters->ncounters = ncounters;
31543b3233fbSRaed Salem 	/* each counter entry have both description and index pair */
31553b3233fbSRaed Salem 	for (i = 0; i < ncounters; i++) {
31563b3233fbSRaed Salem 		if (desc_data[i].description > IB_COUNTER_BYTES)
31573b3233fbSRaed Salem 			return -EINVAL;
31583b3233fbSRaed Salem 
31593b3233fbSRaed Salem 		if (cntrs_max_index <= desc_data[i].index)
31603b3233fbSRaed Salem 			cntrs_max_index = desc_data[i].index + 1;
31613b3233fbSRaed Salem 	}
31623b3233fbSRaed Salem 
31633b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
31643b3233fbSRaed Salem 	mcounters->counters_data = desc_data;
31653b3233fbSRaed Salem 	mcounters->cntrs_max_index = cntrs_max_index;
31663b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
31673b3233fbSRaed Salem 
31683b3233fbSRaed Salem 	return 0;
31693b3233fbSRaed Salem }
31703b3233fbSRaed Salem 
31713b3233fbSRaed Salem #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
31723b3233fbSRaed Salem static int flow_counters_set_data(struct ib_counters *ibcounters,
31733b3233fbSRaed Salem 				  struct mlx5_ib_create_flow *ucmd)
31743b3233fbSRaed Salem {
31753b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
31763b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
31773b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc_data = NULL;
31783b3233fbSRaed Salem 	bool hw_hndl = false;
31793b3233fbSRaed Salem 	int ret = 0;
31803b3233fbSRaed Salem 
31813b3233fbSRaed Salem 	if (ucmd && ucmd->ncounters_data != 0) {
31823b3233fbSRaed Salem 		cntrs_data = ucmd->data;
31833b3233fbSRaed Salem 		if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
31843b3233fbSRaed Salem 			return -EINVAL;
31853b3233fbSRaed Salem 
31863b3233fbSRaed Salem 		desc_data = kcalloc(cntrs_data->ncounters,
31873b3233fbSRaed Salem 				    sizeof(*desc_data),
31883b3233fbSRaed Salem 				    GFP_KERNEL);
31893b3233fbSRaed Salem 		if (!desc_data)
31903b3233fbSRaed Salem 			return  -ENOMEM;
31913b3233fbSRaed Salem 
31923b3233fbSRaed Salem 		if (copy_from_user(desc_data,
31933b3233fbSRaed Salem 				   u64_to_user_ptr(cntrs_data->counters_data),
31943b3233fbSRaed Salem 				   sizeof(*desc_data) * cntrs_data->ncounters)) {
31953b3233fbSRaed Salem 			ret = -EFAULT;
31963b3233fbSRaed Salem 			goto free;
31973b3233fbSRaed Salem 		}
31983b3233fbSRaed Salem 	}
31993b3233fbSRaed Salem 
32003b3233fbSRaed Salem 	if (!mcounters->hw_cntrs_hndl) {
32013b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = mlx5_fc_create(
32023b3233fbSRaed Salem 			to_mdev(ibcounters->device)->mdev, false);
32033b3233fbSRaed Salem 		if (!mcounters->hw_cntrs_hndl) {
32043b3233fbSRaed Salem 			ret = -ENOMEM;
32053b3233fbSRaed Salem 			goto free;
32063b3233fbSRaed Salem 		}
32073b3233fbSRaed Salem 		hw_hndl = true;
32083b3233fbSRaed Salem 	}
32093b3233fbSRaed Salem 
32103b3233fbSRaed Salem 	if (desc_data) {
32113b3233fbSRaed Salem 		/* counters already bound to at least one flow */
32123b3233fbSRaed Salem 		if (mcounters->cntrs_max_index) {
32133b3233fbSRaed Salem 			ret = -EINVAL;
32143b3233fbSRaed Salem 			goto free_hndl;
32153b3233fbSRaed Salem 		}
32163b3233fbSRaed Salem 
32173b3233fbSRaed Salem 		ret = counters_set_description(ibcounters,
32183b3233fbSRaed Salem 					       MLX5_IB_COUNTERS_FLOW,
32193b3233fbSRaed Salem 					       desc_data,
32203b3233fbSRaed Salem 					       cntrs_data->ncounters);
32213b3233fbSRaed Salem 		if (ret)
32223b3233fbSRaed Salem 			goto free_hndl;
32233b3233fbSRaed Salem 
32243b3233fbSRaed Salem 	} else if (!mcounters->cntrs_max_index) {
32253b3233fbSRaed Salem 		/* counters not bound yet, must have udata passed */
32263b3233fbSRaed Salem 		ret = -EINVAL;
32273b3233fbSRaed Salem 		goto free_hndl;
32283b3233fbSRaed Salem 	}
32293b3233fbSRaed Salem 
32303b3233fbSRaed Salem 	return 0;
32313b3233fbSRaed Salem 
32323b3233fbSRaed Salem free_hndl:
32333b3233fbSRaed Salem 	if (hw_hndl) {
32343b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
32353b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
32363b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = NULL;
32373b3233fbSRaed Salem 	}
32383b3233fbSRaed Salem free:
32393b3233fbSRaed Salem 	kfree(desc_data);
32403b3233fbSRaed Salem 	return ret;
32413b3233fbSRaed Salem }
32423b3233fbSRaed Salem 
3243a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3244038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
3245dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
3246a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
32473b3233fbSRaed Salem 						      u32 underlay_qpn,
32483b3233fbSRaed Salem 						      struct mlx5_ib_create_flow *ucmd)
3249038d2ef8SMaor Gottlieb {
3250038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
3251038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
3252075572d4SBoris Pismenny 	struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
3253c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
32543b3233fbSRaed Salem 	struct mlx5_flow_destination dest_arr[2] = {};
32553b3233fbSRaed Salem 	struct mlx5_flow_destination *rule_dst = dest_arr;
3256dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3257038d2ef8SMaor Gottlieb 	unsigned int spec_index;
325871c6e863SAriel Levkovich 	u32 prev_type = 0;
3259038d2ef8SMaor Gottlieb 	int err = 0;
32603b3233fbSRaed Salem 	int dest_num = 0;
3261802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3262038d2ef8SMaor Gottlieb 
326319cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
3264038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3265038d2ef8SMaor Gottlieb 
32661b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3267038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3268c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
3269038d2ef8SMaor Gottlieb 		err = -ENOMEM;
3270038d2ef8SMaor Gottlieb 		goto free;
3271038d2ef8SMaor Gottlieb 	}
3272038d2ef8SMaor Gottlieb 
3273038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
32743b3233fbSRaed Salem 	if (dst) {
32753b3233fbSRaed Salem 		memcpy(&dest_arr[0], dst, sizeof(*dst));
32763b3233fbSRaed Salem 		dest_num++;
32773b3233fbSRaed Salem 	}
3278038d2ef8SMaor Gottlieb 
3279038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
328019cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
3281a22ed86cSSlava Shwartsman 				      spec->match_value,
328271c6e863SAriel Levkovich 				      ib_flow, flow_attr, &flow_act,
328371c6e863SAriel Levkovich 				      prev_type);
3284038d2ef8SMaor Gottlieb 		if (err < 0)
3285038d2ef8SMaor Gottlieb 			goto free;
3286038d2ef8SMaor Gottlieb 
328771c6e863SAriel Levkovich 		prev_type = ((union ib_flow_spec *)ib_flow)->type;
3288038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3289038d2ef8SMaor Gottlieb 	}
3290038d2ef8SMaor Gottlieb 
3291a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
3292a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
3293a550ddfcSYishai Hadas 
3294018a94eeSMark Bloch 	if (dev->rep) {
3295018a94eeSMark Bloch 		void *misc;
3296018a94eeSMark Bloch 
3297018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3298018a94eeSMark Bloch 				    misc_parameters);
3299018a94eeSMark Bloch 		MLX5_SET(fte_match_set_misc, misc, source_port,
3300018a94eeSMark Bloch 			 dev->rep->vport);
3301018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3302018a94eeSMark Bloch 				    misc_parameters);
3303018a94eeSMark Bloch 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3304018a94eeSMark Bloch 	}
3305018a94eeSMark Bloch 
3306466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3307802c2125SAviad Yehezkel 
3308802c2125SAviad Yehezkel 	if (is_egress &&
3309802c2125SAviad Yehezkel 	    !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3310802c2125SAviad Yehezkel 		err = -EINVAL;
3311802c2125SAviad Yehezkel 		goto free;
3312802c2125SAviad Yehezkel 	}
3313802c2125SAviad Yehezkel 
33143b3233fbSRaed Salem 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
33153b3233fbSRaed Salem 		err = flow_counters_set_data(flow_act.counters, ucmd);
33163b3233fbSRaed Salem 		if (err)
33173b3233fbSRaed Salem 			goto free;
33183b3233fbSRaed Salem 
33193b3233fbSRaed Salem 		handler->ibcounters = flow_act.counters;
33203b3233fbSRaed Salem 		dest_arr[dest_num].type =
33213b3233fbSRaed Salem 			MLX5_FLOW_DESTINATION_TYPE_COUNTER;
33223b3233fbSRaed Salem 		dest_arr[dest_num].counter =
33233b3233fbSRaed Salem 			to_mcounters(flow_act.counters)->hw_cntrs_hndl;
33243b3233fbSRaed Salem 		dest_num++;
33253b3233fbSRaed Salem 	}
33263b3233fbSRaed Salem 
3327075572d4SBoris Pismenny 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
33283b3233fbSRaed Salem 		if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3329a22ed86cSSlava Shwartsman 			rule_dst = NULL;
3330a22ed86cSSlava Shwartsman 			dest_num = 0;
33313b3233fbSRaed Salem 		}
3332a22ed86cSSlava Shwartsman 	} else {
3333802c2125SAviad Yehezkel 		if (is_egress)
3334802c2125SAviad Yehezkel 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3335802c2125SAviad Yehezkel 		else
3336802c2125SAviad Yehezkel 			flow_act.action |=
33373b3233fbSRaed Salem 				dest_num ?  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
333835d19011SMaor Gottlieb 					MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3339a22ed86cSSlava Shwartsman 	}
33402ac693f9SMoses Reuben 
3341a9db0ecfSMatan Barak 	if (flow_act.has_flow_tag &&
33422ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
33432ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
33442ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3345075572d4SBoris Pismenny 			     flow_act.flow_tag, flow_attr->type);
33462ac693f9SMoses Reuben 		err = -EINVAL;
33472ac693f9SMoses Reuben 		goto free;
33482ac693f9SMoses Reuben 	}
334974491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
335066958ed9SHadar Hen Zion 					    &flow_act,
3351a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
3352038d2ef8SMaor Gottlieb 
3353038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
3354038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
3355038d2ef8SMaor Gottlieb 		goto free;
3356038d2ef8SMaor Gottlieb 	}
3357038d2ef8SMaor Gottlieb 
3358d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
33595497adc6SMaor Gottlieb 	handler->prio = ft_prio;
3360038d2ef8SMaor Gottlieb 
3361038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
3362038d2ef8SMaor Gottlieb free:
33633b3233fbSRaed Salem 	if (err && handler) {
33643b3233fbSRaed Salem 		if (handler->ibcounters &&
33653b3233fbSRaed Salem 		    atomic_read(&handler->ibcounters->usecnt) == 1)
33663b3233fbSRaed Salem 			counters_clear_description(handler->ibcounters);
3367038d2ef8SMaor Gottlieb 		kfree(handler);
33683b3233fbSRaed Salem 	}
3369c5bb1730SMaor Gottlieb 	kvfree(spec);
3370038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
3371038d2ef8SMaor Gottlieb }
3372038d2ef8SMaor Gottlieb 
3373a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3374a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
3375a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
3376a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
3377a550ddfcSYishai Hadas {
33783b3233fbSRaed Salem 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
3379a550ddfcSYishai Hadas }
3380a550ddfcSYishai Hadas 
338135d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
338235d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
338335d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
338435d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
338535d19011SMaor Gottlieb {
338635d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
338735d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
338835d19011SMaor Gottlieb 
338935d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
339035d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
339135d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
339235d19011SMaor Gottlieb 					       flow_attr, dst);
339335d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
339474491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3395d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
339635d19011SMaor Gottlieb 			kfree(handler);
339735d19011SMaor Gottlieb 			handler = handler_dst;
339835d19011SMaor Gottlieb 		} else {
339935d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
340035d19011SMaor Gottlieb 		}
340135d19011SMaor Gottlieb 	}
340235d19011SMaor Gottlieb 
340335d19011SMaor Gottlieb 	return handler;
340435d19011SMaor Gottlieb }
3405038d2ef8SMaor Gottlieb enum {
3406038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
3407038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
3408038d2ef8SMaor Gottlieb };
3409038d2ef8SMaor Gottlieb 
3410038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3411038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
3412038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
3413038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
3414038d2ef8SMaor Gottlieb {
3415038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
3416038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3417038d2ef8SMaor Gottlieb 
3418038d2ef8SMaor Gottlieb 	static struct {
3419038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
3420038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
3421038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
3422038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
3423038d2ef8SMaor Gottlieb 			.flow_attr = {
3424038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3425038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3426038d2ef8SMaor Gottlieb 			},
3427038d2ef8SMaor Gottlieb 			.eth_flow = {
3428038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3429038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3430038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3431038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
3432038d2ef8SMaor Gottlieb 			}
3433038d2ef8SMaor Gottlieb 		},
3434038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
3435038d2ef8SMaor Gottlieb 			.flow_attr = {
3436038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3437038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3438038d2ef8SMaor Gottlieb 			},
3439038d2ef8SMaor Gottlieb 			.eth_flow = {
3440038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3441038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3442038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3443038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
3444038d2ef8SMaor Gottlieb 			}
3445038d2ef8SMaor Gottlieb 		}
3446038d2ef8SMaor Gottlieb 	};
3447038d2ef8SMaor Gottlieb 
3448038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
3449038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
3450038d2ef8SMaor Gottlieb 				   dst);
3451038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
3452038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3453038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
3454038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3455038d2ef8SMaor Gottlieb 						 dst);
3456038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
345774491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3458d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
3459038d2ef8SMaor Gottlieb 			kfree(handler);
3460038d2ef8SMaor Gottlieb 			handler = handler_ucast;
3461038d2ef8SMaor Gottlieb 		} else {
3462038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
3463038d2ef8SMaor Gottlieb 		}
3464038d2ef8SMaor Gottlieb 	}
3465038d2ef8SMaor Gottlieb 
3466038d2ef8SMaor Gottlieb 	return handler;
3467038d2ef8SMaor Gottlieb }
3468038d2ef8SMaor Gottlieb 
3469cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3470cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
3471cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
3472cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
3473cc0e5d42SMaor Gottlieb {
3474cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
3475cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
3476cc0e5d42SMaor Gottlieb 	int err;
3477cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
3478cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
3479cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
3480cc0e5d42SMaor Gottlieb 	};
3481cc0e5d42SMaor Gottlieb 
3482cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3483cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
3484cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
3485cc0e5d42SMaor Gottlieb 		goto err;
3486cc0e5d42SMaor Gottlieb 	}
3487cc0e5d42SMaor Gottlieb 
3488cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3489cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
3490cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
3491cc0e5d42SMaor Gottlieb 		goto err_tx;
3492cc0e5d42SMaor Gottlieb 	}
3493cc0e5d42SMaor Gottlieb 
3494cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
3495cc0e5d42SMaor Gottlieb 
3496cc0e5d42SMaor Gottlieb 	return handler_rx;
3497cc0e5d42SMaor Gottlieb 
3498cc0e5d42SMaor Gottlieb err_tx:
349974491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
3500cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
3501cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
3502cc0e5d42SMaor Gottlieb err:
3503cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
3504cc0e5d42SMaor Gottlieb }
3505cc0e5d42SMaor Gottlieb 
3506038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3507038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
350859082a32SMatan Barak 					   int domain,
350959082a32SMatan Barak 					   struct ib_udata *udata)
3510038d2ef8SMaor Gottlieb {
3511038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3512d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
3513038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3514038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
3515cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3516038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
3517802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
35183b3233fbSRaed Salem 	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
35193b3233fbSRaed Salem 	size_t min_ucmd_sz, required_ucmd_sz;
3520038d2ef8SMaor Gottlieb 	int err;
3521a550ddfcSYishai Hadas 	int underlay_qpn;
3522038d2ef8SMaor Gottlieb 
35233b3233fbSRaed Salem 	if (udata && udata->inlen) {
35243b3233fbSRaed Salem 		min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
35253b3233fbSRaed Salem 				sizeof(ucmd_hdr.reserved);
35263b3233fbSRaed Salem 		if (udata->inlen < min_ucmd_sz)
352759082a32SMatan Barak 			return ERR_PTR(-EOPNOTSUPP);
352859082a32SMatan Barak 
35293b3233fbSRaed Salem 		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
35303b3233fbSRaed Salem 		if (err)
35313b3233fbSRaed Salem 			return ERR_PTR(err);
35323b3233fbSRaed Salem 
35333b3233fbSRaed Salem 		/* currently supports only one counters data */
35343b3233fbSRaed Salem 		if (ucmd_hdr.ncounters_data > 1)
35353b3233fbSRaed Salem 			return ERR_PTR(-EINVAL);
35363b3233fbSRaed Salem 
35373b3233fbSRaed Salem 		required_ucmd_sz = min_ucmd_sz +
35383b3233fbSRaed Salem 			sizeof(struct mlx5_ib_flow_counters_data) *
35393b3233fbSRaed Salem 			ucmd_hdr.ncounters_data;
35403b3233fbSRaed Salem 		if (udata->inlen > required_ucmd_sz &&
35413b3233fbSRaed Salem 		    !ib_is_udata_cleared(udata, required_ucmd_sz,
35423b3233fbSRaed Salem 					 udata->inlen - required_ucmd_sz))
35433b3233fbSRaed Salem 			return ERR_PTR(-EOPNOTSUPP);
35443b3233fbSRaed Salem 
35453b3233fbSRaed Salem 		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
35463b3233fbSRaed Salem 		if (!ucmd)
35473b3233fbSRaed Salem 			return ERR_PTR(-ENOMEM);
35483b3233fbSRaed Salem 
35493b3233fbSRaed Salem 		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
35503b3233fbSRaed Salem 		if (err) {
35513b3233fbSRaed Salem 			kfree(ucmd);
35523b3233fbSRaed Salem 			return ERR_PTR(err);
35533b3233fbSRaed Salem 		}
35543b3233fbSRaed Salem 	}
35553b3233fbSRaed Salem 
3556038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
3557dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
3558038d2ef8SMaor Gottlieb 
3559038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
3560508562d6SDaniel Jurgens 	    flow_attr->port > dev->num_ports ||
3561802c2125SAviad Yehezkel 	    (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3562802c2125SAviad Yehezkel 				  IB_FLOW_ATTR_FLAGS_EGRESS)))
3563802c2125SAviad Yehezkel 		return ERR_PTR(-EINVAL);
3564802c2125SAviad Yehezkel 
3565802c2125SAviad Yehezkel 	if (is_egress &&
3566802c2125SAviad Yehezkel 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3567802c2125SAviad Yehezkel 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT))
3568038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3569038d2ef8SMaor Gottlieb 
3570038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3571038d2ef8SMaor Gottlieb 	if (!dst)
3572038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
3573038d2ef8SMaor Gottlieb 
35749a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3575038d2ef8SMaor Gottlieb 
3576802c2125SAviad Yehezkel 	ft_prio = get_flow_table(dev, flow_attr,
3577802c2125SAviad Yehezkel 				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3578038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
3579038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
3580038d2ef8SMaor Gottlieb 		goto unlock;
3581038d2ef8SMaor Gottlieb 	}
3582cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3583cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3584cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
3585cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
3586cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
3587cc0e5d42SMaor Gottlieb 			goto destroy_ft;
3588cc0e5d42SMaor Gottlieb 		}
3589cc0e5d42SMaor Gottlieb 	}
3590038d2ef8SMaor Gottlieb 
3591802c2125SAviad Yehezkel 	if (is_egress) {
3592802c2125SAviad Yehezkel 		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3593802c2125SAviad Yehezkel 	} else {
3594038d2ef8SMaor Gottlieb 		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3595d9f88e5aSYishai Hadas 		if (mqp->flags & MLX5_IB_QP_RSS)
3596d9f88e5aSYishai Hadas 			dst->tir_num = mqp->rss_qp.tirn;
3597d9f88e5aSYishai Hadas 		else
3598d9f88e5aSYishai Hadas 			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3599802c2125SAviad Yehezkel 	}
3600038d2ef8SMaor Gottlieb 
3601038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
360235d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
360335d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
360435d19011SMaor Gottlieb 							flow_attr, dst);
360535d19011SMaor Gottlieb 		} else {
3606a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3607a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
3608a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
36093b3233fbSRaed Salem 						    dst, underlay_qpn, ucmd);
361035d19011SMaor Gottlieb 		}
3611038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3612038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3613038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3614038d2ef8SMaor Gottlieb 						dst);
3615cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3616cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3617038d2ef8SMaor Gottlieb 	} else {
3618038d2ef8SMaor Gottlieb 		err = -EINVAL;
3619038d2ef8SMaor Gottlieb 		goto destroy_ft;
3620038d2ef8SMaor Gottlieb 	}
3621038d2ef8SMaor Gottlieb 
3622038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
3623038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
3624038d2ef8SMaor Gottlieb 		handler = NULL;
3625038d2ef8SMaor Gottlieb 		goto destroy_ft;
3626038d2ef8SMaor Gottlieb 	}
3627038d2ef8SMaor Gottlieb 
36289a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3629038d2ef8SMaor Gottlieb 	kfree(dst);
36303b3233fbSRaed Salem 	kfree(ucmd);
3631038d2ef8SMaor Gottlieb 
3632038d2ef8SMaor Gottlieb 	return &handler->ibflow;
3633038d2ef8SMaor Gottlieb 
3634038d2ef8SMaor Gottlieb destroy_ft:
3635038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
3636cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
3637cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
3638038d2ef8SMaor Gottlieb unlock:
36399a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3640038d2ef8SMaor Gottlieb 	kfree(dst);
36413b3233fbSRaed Salem 	kfree(ucmd);
3642038d2ef8SMaor Gottlieb 	kfree(handler);
3643038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
3644038d2ef8SMaor Gottlieb }
3645038d2ef8SMaor Gottlieb 
3646c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
3647c6475a0bSAviad Yehezkel {
3648c6475a0bSAviad Yehezkel 	u32 flags = 0;
3649c6475a0bSAviad Yehezkel 
3650c6475a0bSAviad Yehezkel 	if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
3651c6475a0bSAviad Yehezkel 		flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
3652c6475a0bSAviad Yehezkel 
3653c6475a0bSAviad Yehezkel 	return flags;
3654c6475a0bSAviad Yehezkel }
3655c6475a0bSAviad Yehezkel 
3656c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED	MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
3657c6475a0bSAviad Yehezkel static struct ib_flow_action *
3658c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device,
3659c6475a0bSAviad Yehezkel 			       const struct ib_flow_action_attrs_esp *attr,
3660c6475a0bSAviad Yehezkel 			       struct uverbs_attr_bundle *attrs)
3661c6475a0bSAviad Yehezkel {
3662c6475a0bSAviad Yehezkel 	struct mlx5_ib_dev *mdev = to_mdev(device);
3663c6475a0bSAviad Yehezkel 	struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
3664c6475a0bSAviad Yehezkel 	struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
3665c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *action;
3666c6475a0bSAviad Yehezkel 	u64 action_flags;
3667c6475a0bSAviad Yehezkel 	u64 flags;
3668c6475a0bSAviad Yehezkel 	int err = 0;
3669c6475a0bSAviad Yehezkel 
3670c6475a0bSAviad Yehezkel 	if (IS_UVERBS_COPY_ERR(uverbs_copy_from(&action_flags, attrs,
3671c6475a0bSAviad Yehezkel 						MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS)))
3672c6475a0bSAviad Yehezkel 		return ERR_PTR(-EFAULT);
3673c6475a0bSAviad Yehezkel 
3674c6475a0bSAviad Yehezkel 	if (action_flags >= (MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1))
3675c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3676c6475a0bSAviad Yehezkel 
3677c6475a0bSAviad Yehezkel 	flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
3678c6475a0bSAviad Yehezkel 
3679c6475a0bSAviad Yehezkel 	/* We current only support a subset of the standard features. Only a
3680c6475a0bSAviad Yehezkel 	 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
3681c6475a0bSAviad Yehezkel 	 * (with overlap). Full offload mode isn't supported.
3682c6475a0bSAviad Yehezkel 	 */
3683c6475a0bSAviad Yehezkel 	if (!attr->keymat || attr->replay || attr->encap ||
3684c6475a0bSAviad Yehezkel 	    attr->spi || attr->seq || attr->tfc_pad ||
3685c6475a0bSAviad Yehezkel 	    attr->hard_limit_pkts ||
3686c6475a0bSAviad Yehezkel 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3687c6475a0bSAviad Yehezkel 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
3688c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3689c6475a0bSAviad Yehezkel 
3690c6475a0bSAviad Yehezkel 	if (attr->keymat->protocol !=
3691c6475a0bSAviad Yehezkel 	    IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
3692c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3693c6475a0bSAviad Yehezkel 
3694c6475a0bSAviad Yehezkel 	aes_gcm = &attr->keymat->keymat.aes_gcm;
3695c6475a0bSAviad Yehezkel 
3696c6475a0bSAviad Yehezkel 	if (aes_gcm->icv_len != 16 ||
3697c6475a0bSAviad Yehezkel 	    aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
3698c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
3699c6475a0bSAviad Yehezkel 
3700c6475a0bSAviad Yehezkel 	action = kmalloc(sizeof(*action), GFP_KERNEL);
3701c6475a0bSAviad Yehezkel 	if (!action)
3702c6475a0bSAviad Yehezkel 		return ERR_PTR(-ENOMEM);
3703c6475a0bSAviad Yehezkel 
3704c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
3705c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
3706c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.aes_key));
3707c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
3708c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
3709c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.salt));
3710c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
3711c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
3712c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
3713c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
3714c6475a0bSAviad Yehezkel 	accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
3715c6475a0bSAviad Yehezkel 
3716c6475a0bSAviad Yehezkel 	accel_attrs.esn = attr->esn;
3717c6475a0bSAviad Yehezkel 	if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
3718c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
3719c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
3720c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3721c6475a0bSAviad Yehezkel 
3722c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
3723c6475a0bSAviad Yehezkel 		accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
3724c6475a0bSAviad Yehezkel 
3725c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ctx =
3726c6475a0bSAviad Yehezkel 		mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
3727c6475a0bSAviad Yehezkel 	if (IS_ERR(action->esp_aes_gcm.ctx)) {
3728c6475a0bSAviad Yehezkel 		err = PTR_ERR(action->esp_aes_gcm.ctx);
3729c6475a0bSAviad Yehezkel 		goto err_parse;
3730c6475a0bSAviad Yehezkel 	}
3731c6475a0bSAviad Yehezkel 
3732c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
3733c6475a0bSAviad Yehezkel 
3734c6475a0bSAviad Yehezkel 	return &action->ib_action;
3735c6475a0bSAviad Yehezkel 
3736c6475a0bSAviad Yehezkel err_parse:
3737c6475a0bSAviad Yehezkel 	kfree(action);
3738c6475a0bSAviad Yehezkel 	return ERR_PTR(err);
3739c6475a0bSAviad Yehezkel }
3740c6475a0bSAviad Yehezkel 
3741349705c1SMatan Barak static int
3742349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
3743349705c1SMatan Barak 			       const struct ib_flow_action_attrs_esp *attr,
3744349705c1SMatan Barak 			       struct uverbs_attr_bundle *attrs)
3745349705c1SMatan Barak {
3746349705c1SMatan Barak 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
3747349705c1SMatan Barak 	struct mlx5_accel_esp_xfrm_attrs accel_attrs;
3748349705c1SMatan Barak 	int err = 0;
3749349705c1SMatan Barak 
3750349705c1SMatan Barak 	if (attr->keymat || attr->replay || attr->encap ||
3751349705c1SMatan Barak 	    attr->spi || attr->seq || attr->tfc_pad ||
3752349705c1SMatan Barak 	    attr->hard_limit_pkts ||
3753349705c1SMatan Barak 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3754349705c1SMatan Barak 			     IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
3755349705c1SMatan Barak 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
3756349705c1SMatan Barak 		return -EOPNOTSUPP;
3757349705c1SMatan Barak 
3758349705c1SMatan Barak 	/* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
3759349705c1SMatan Barak 	 * be modified.
3760349705c1SMatan Barak 	 */
3761349705c1SMatan Barak 	if (!(maction->esp_aes_gcm.ib_flags &
3762349705c1SMatan Barak 	      IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
3763349705c1SMatan Barak 	    attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3764349705c1SMatan Barak 			   IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
3765349705c1SMatan Barak 		return -EINVAL;
3766349705c1SMatan Barak 
3767349705c1SMatan Barak 	memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
3768349705c1SMatan Barak 	       sizeof(accel_attrs));
3769349705c1SMatan Barak 
3770349705c1SMatan Barak 	accel_attrs.esn = attr->esn;
3771349705c1SMatan Barak 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
3772349705c1SMatan Barak 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3773349705c1SMatan Barak 	else
3774349705c1SMatan Barak 		accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3775349705c1SMatan Barak 
3776349705c1SMatan Barak 	err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
3777349705c1SMatan Barak 					 &accel_attrs);
3778349705c1SMatan Barak 	if (err)
3779349705c1SMatan Barak 		return err;
3780349705c1SMatan Barak 
3781349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags &=
3782349705c1SMatan Barak 		~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
3783349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags |=
3784349705c1SMatan Barak 		attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
3785349705c1SMatan Barak 
3786349705c1SMatan Barak 	return 0;
3787349705c1SMatan Barak }
3788349705c1SMatan Barak 
3789c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
3790c6475a0bSAviad Yehezkel {
3791c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
3792c6475a0bSAviad Yehezkel 
3793c6475a0bSAviad Yehezkel 	switch (action->type) {
3794c6475a0bSAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
3795c6475a0bSAviad Yehezkel 		/*
3796c6475a0bSAviad Yehezkel 		 * We only support aes_gcm by now, so we implicitly know this is
3797c6475a0bSAviad Yehezkel 		 * the underline crypto.
3798c6475a0bSAviad Yehezkel 		 */
3799c6475a0bSAviad Yehezkel 		mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
3800c6475a0bSAviad Yehezkel 		break;
3801c6475a0bSAviad Yehezkel 	default:
3802c6475a0bSAviad Yehezkel 		WARN_ON(true);
3803c6475a0bSAviad Yehezkel 		break;
3804c6475a0bSAviad Yehezkel 	}
3805c6475a0bSAviad Yehezkel 
3806c6475a0bSAviad Yehezkel 	kfree(maction);
3807c6475a0bSAviad Yehezkel 	return 0;
3808c6475a0bSAviad Yehezkel }
3809c6475a0bSAviad Yehezkel 
3810e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3811e126ba97SEli Cohen {
3812e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
381381e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
3814e126ba97SEli Cohen 	int err;
3815e126ba97SEli Cohen 
381681e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
381781e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
381881e30880SYishai Hadas 		return -EOPNOTSUPP;
381981e30880SYishai Hadas 	}
382081e30880SYishai Hadas 
38219603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
3822e126ba97SEli Cohen 	if (err)
3823e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
3824e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
3825e126ba97SEli Cohen 
3826e126ba97SEli Cohen 	return err;
3827e126ba97SEli Cohen }
3828e126ba97SEli Cohen 
3829e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3830e126ba97SEli Cohen {
3831e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3832e126ba97SEli Cohen 	int err;
3833e126ba97SEli Cohen 
38349603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
3835e126ba97SEli Cohen 	if (err)
3836e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
3837e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
3838e126ba97SEli Cohen 
3839e126ba97SEli Cohen 	return err;
3840e126ba97SEli Cohen }
3841e126ba97SEli Cohen 
3842e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
3843e126ba97SEli Cohen {
38441b5daf11SMajd Dibbiny 	int err;
3845e126ba97SEli Cohen 
38461b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
3847e126ba97SEli Cohen 	if (err)
3848e126ba97SEli Cohen 		return err;
38491b5daf11SMajd Dibbiny 
38501b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
38511b5daf11SMajd Dibbiny 
38521b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
3853e126ba97SEli Cohen }
3854e126ba97SEli Cohen 
3855e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
3856e126ba97SEli Cohen 			     char *buf)
3857e126ba97SEli Cohen {
3858e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3859e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3860e126ba97SEli Cohen 
38619603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
3862e126ba97SEli Cohen }
3863e126ba97SEli Cohen 
3864e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
3865e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
3866e126ba97SEli Cohen {
3867e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3868e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3869e126ba97SEli Cohen 
38706aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
3871e126ba97SEli Cohen }
3872e126ba97SEli Cohen 
3873e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
3874e126ba97SEli Cohen 			char *buf)
3875e126ba97SEli Cohen {
3876e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3877e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
38789603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
3879e126ba97SEli Cohen }
3880e126ba97SEli Cohen 
3881e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
3882e126ba97SEli Cohen 			char *buf)
3883e126ba97SEli Cohen {
3884e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3885e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
38869603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
3887e126ba97SEli Cohen }
3888e126ba97SEli Cohen 
3889e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
3890e126ba97SEli Cohen 			  char *buf)
3891e126ba97SEli Cohen {
3892e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
3893e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3894e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
38959603b61dSJack Morgenstein 		       dev->mdev->board_id);
3896e126ba97SEli Cohen }
3897e126ba97SEli Cohen 
3898e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
3899e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
3900e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
3901e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
3902e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
3903e126ba97SEli Cohen 
3904e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
3905e126ba97SEli Cohen 	&dev_attr_hw_rev,
3906e126ba97SEli Cohen 	&dev_attr_hca_type,
3907e126ba97SEli Cohen 	&dev_attr_board_id,
3908e126ba97SEli Cohen 	&dev_attr_fw_pages,
3909e126ba97SEli Cohen 	&dev_attr_reg_pages,
3910e126ba97SEli Cohen };
3911e126ba97SEli Cohen 
39127722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
39137722f47eSHaggai Eran {
39147722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
39157722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
39167722f47eSHaggai Eran 			     pkey_change_work);
39177722f47eSHaggai Eran 
39187722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
39197722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
39207722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
39217722f47eSHaggai Eran }
39227722f47eSHaggai Eran 
392389ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
392489ea94a7SMaor Gottlieb {
392589ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
392689ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
392789ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
392889ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
392989ea94a7SMaor Gottlieb 	unsigned long flags_qp;
393089ea94a7SMaor Gottlieb 	unsigned long flags_cq;
393189ea94a7SMaor Gottlieb 	unsigned long flags;
393289ea94a7SMaor Gottlieb 
393389ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
393489ea94a7SMaor Gottlieb 
393589ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
393689ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
393789ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
393889ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
393989ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
394089ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
394189ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
394289ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
394389ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
394489ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
394589ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
394689ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
394789ea94a7SMaor Gottlieb 						      &cq_armed_list);
394889ea94a7SMaor Gottlieb 				}
394989ea94a7SMaor Gottlieb 			}
395089ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
395189ea94a7SMaor Gottlieb 		}
395289ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
395389ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
395489ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
395589ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
395689ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
395789ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
395889ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
395989ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
396089ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
396189ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
396289ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
396389ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
396489ea94a7SMaor Gottlieb 							      &cq_armed_list);
396589ea94a7SMaor Gottlieb 					}
396689ea94a7SMaor Gottlieb 				}
396789ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
396889ea94a7SMaor Gottlieb 						       flags_cq);
396989ea94a7SMaor Gottlieb 			}
397089ea94a7SMaor Gottlieb 		}
397189ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
397289ea94a7SMaor Gottlieb 	}
397389ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
397489ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
397589ea94a7SMaor Gottlieb 	 */
397689ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
397789ea94a7SMaor Gottlieb 		mcq->comp(mcq);
397889ea94a7SMaor Gottlieb 	}
397989ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
398089ea94a7SMaor Gottlieb }
398189ea94a7SMaor Gottlieb 
398203404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
398303404e8aSMaor Gottlieb {
398403404e8aSMaor Gottlieb 	int err;
398503404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
398603404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
398703404e8aSMaor Gottlieb 			     delay_drop_work);
398803404e8aSMaor Gottlieb 
3989fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
3990fe248c3aSMaor Gottlieb 
399103404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
399203404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
399303404e8aSMaor Gottlieb 				       delay_drop->timeout);
399403404e8aSMaor Gottlieb 	if (err) {
399503404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
399603404e8aSMaor Gottlieb 			     delay_drop->timeout);
399703404e8aSMaor Gottlieb 		delay_drop->activate = false;
399803404e8aSMaor Gottlieb 	}
399903404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
400003404e8aSMaor Gottlieb }
400103404e8aSMaor Gottlieb 
4002d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
4003e126ba97SEli Cohen {
4004d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
4005d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
4006d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
4007e126ba97SEli Cohen 	struct ib_event ibev;
4008dbaaff2aSEli Cohen 	bool fatal = false;
4009aba46213SDaniel Jurgens 	u8 port = (u8)work->param;
4010e126ba97SEli Cohen 
4011d69a24e0SDaniel Jurgens 	if (mlx5_core_is_mp_slave(work->dev)) {
4012d69a24e0SDaniel Jurgens 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->context);
4013d69a24e0SDaniel Jurgens 		if (!ibdev)
4014d69a24e0SDaniel Jurgens 			goto out;
4015d69a24e0SDaniel Jurgens 	} else {
4016d69a24e0SDaniel Jurgens 		ibdev = work->context;
4017d69a24e0SDaniel Jurgens 	}
4018d69a24e0SDaniel Jurgens 
4019d69a24e0SDaniel Jurgens 	switch (work->event) {
4020e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
4021e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
402289ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
4023dbaaff2aSEli Cohen 		fatal = true;
4024e126ba97SEli Cohen 		break;
4025e126ba97SEli Cohen 
4026e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
4027e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
40282788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
40295ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
40305ec8c83eSAviv Heller 		 * mlx5_netdev_event().
40315ec8c83eSAviv Heller 		 */
40325ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
40335ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
4034d69a24e0SDaniel Jurgens 			goto out;
40355ec8c83eSAviv Heller 
4036d69a24e0SDaniel Jurgens 		ibev.event = (work->event == MLX5_DEV_EVENT_PORT_UP) ?
40375ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4038e126ba97SEli Cohen 		break;
4039e126ba97SEli Cohen 
4040e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
4041e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
4042e126ba97SEli Cohen 		break;
4043e126ba97SEli Cohen 
4044e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
4045e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
40467722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4047e126ba97SEli Cohen 		break;
4048e126ba97SEli Cohen 
4049e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
4050e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
4051e126ba97SEli Cohen 		break;
4052e126ba97SEli Cohen 
4053e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
4054e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
4055e126ba97SEli Cohen 		break;
405603404e8aSMaor Gottlieb 	case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
405703404e8aSMaor Gottlieb 		schedule_work(&ibdev->delay_drop.delay_drop_work);
405803404e8aSMaor Gottlieb 		goto out;
4059bdc37924SSaeed Mahameed 	default:
406003404e8aSMaor Gottlieb 		goto out;
4061e126ba97SEli Cohen 	}
4062e126ba97SEli Cohen 
4063e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
4064e126ba97SEli Cohen 	ibev.element.port_num = port;
4065e126ba97SEli Cohen 
4066aba46213SDaniel Jurgens 	if (!rdma_is_port_valid(&ibdev->ib_dev, port)) {
4067a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
406803404e8aSMaor Gottlieb 		goto out;
4069a0c84c32SEli Cohen 	}
4070a0c84c32SEli Cohen 
4071e126ba97SEli Cohen 	if (ibdev->ib_active)
4072e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
4073dbaaff2aSEli Cohen 
4074dbaaff2aSEli Cohen 	if (fatal)
4075dbaaff2aSEli Cohen 		ibdev->ib_active = false;
407603404e8aSMaor Gottlieb out:
4077d69a24e0SDaniel Jurgens 	kfree(work);
4078d69a24e0SDaniel Jurgens }
4079d69a24e0SDaniel Jurgens 
4080d69a24e0SDaniel Jurgens static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
4081d69a24e0SDaniel Jurgens 			  enum mlx5_dev_event event, unsigned long param)
4082d69a24e0SDaniel Jurgens {
4083d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
4084d69a24e0SDaniel Jurgens 
4085d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
408610bea9c8SLeon Romanovsky 	if (!work)
408710bea9c8SLeon Romanovsky 		return;
408810bea9c8SLeon Romanovsky 
4089d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4090d69a24e0SDaniel Jurgens 	work->dev = dev;
4091d69a24e0SDaniel Jurgens 	work->param = param;
4092d69a24e0SDaniel Jurgens 	work->context = context;
4093d69a24e0SDaniel Jurgens 	work->event = event;
4094d69a24e0SDaniel Jurgens 
4095d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
4096d69a24e0SDaniel Jurgens }
4097d69a24e0SDaniel Jurgens 
4098c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4099c43f1112SMaor Gottlieb {
4100c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
4101c43f1112SMaor Gottlieb 	int err;
4102c43f1112SMaor Gottlieb 	int port;
4103c43f1112SMaor Gottlieb 
4104508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++) {
4105c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
4106c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4107c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
4108c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4109c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
4110c43f1112SMaor Gottlieb 								   port, 0,
4111c43f1112SMaor Gottlieb 								   &vport_ctx);
4112c43f1112SMaor Gottlieb 				if (err) {
4113c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4114c43f1112SMaor Gottlieb 						    port, err);
4115c43f1112SMaor Gottlieb 					return err;
4116c43f1112SMaor Gottlieb 				}
4117c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
4118c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
4119c43f1112SMaor Gottlieb 			} else {
4120c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
4121c43f1112SMaor Gottlieb 			}
4122c43f1112SMaor Gottlieb 		}
4123c43f1112SMaor Gottlieb 	}
4124c43f1112SMaor Gottlieb 	return 0;
4125c43f1112SMaor Gottlieb }
4126c43f1112SMaor Gottlieb 
4127e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4128e126ba97SEli Cohen {
4129e126ba97SEli Cohen 	int port;
4130e126ba97SEli Cohen 
4131508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
4132e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
4133e126ba97SEli Cohen }
4134e126ba97SEli Cohen 
413532f69e4bSDaniel Jurgens static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4136e126ba97SEli Cohen {
4137e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
4138e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
4139f614fc15SDan Carpenter 	int err = -ENOMEM;
41402528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
4141e126ba97SEli Cohen 
4142e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
4143e126ba97SEli Cohen 	if (!pprops)
4144e126ba97SEli Cohen 		goto out;
4145e126ba97SEli Cohen 
4146e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4147e126ba97SEli Cohen 	if (!dprops)
4148e126ba97SEli Cohen 		goto out;
4149e126ba97SEli Cohen 
4150c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
4151c43f1112SMaor Gottlieb 	if (err)
4152c43f1112SMaor Gottlieb 		goto out;
4153c43f1112SMaor Gottlieb 
41542528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
4155e126ba97SEli Cohen 	if (err) {
4156e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
4157e126ba97SEli Cohen 		goto out;
4158e126ba97SEli Cohen 	}
4159e126ba97SEli Cohen 
4160c4550c63SOr Gerlitz 	memset(pprops, 0, sizeof(*pprops));
4161e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4162e126ba97SEli Cohen 	if (err) {
4163938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
4164938fe83cSSaeed Mahameed 			     port, err);
416532f69e4bSDaniel Jurgens 		goto out;
4166e126ba97SEli Cohen 	}
416732f69e4bSDaniel Jurgens 
4168938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
4169938fe83cSSaeed Mahameed 					dprops->max_pkeys;
4170938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
4171938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
417232f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
417332f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
4174e126ba97SEli Cohen 
4175e126ba97SEli Cohen out:
4176e126ba97SEli Cohen 	kfree(pprops);
4177e126ba97SEli Cohen 	kfree(dprops);
4178e126ba97SEli Cohen 
4179e126ba97SEli Cohen 	return err;
4180e126ba97SEli Cohen }
4181e126ba97SEli Cohen 
4182e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4183e126ba97SEli Cohen {
4184e126ba97SEli Cohen 	int err;
4185e126ba97SEli Cohen 
4186e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
4187e126ba97SEli Cohen 	if (err)
4188e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4189e126ba97SEli Cohen 
419032927e28SMark Bloch 	if (dev->umrc.qp)
4191e126ba97SEli Cohen 		mlx5_ib_destroy_qp(dev->umrc.qp);
419232927e28SMark Bloch 	if (dev->umrc.cq)
4193add08d76SChristoph Hellwig 		ib_free_cq(dev->umrc.cq);
419432927e28SMark Bloch 	if (dev->umrc.pd)
4195e126ba97SEli Cohen 		ib_dealloc_pd(dev->umrc.pd);
4196e126ba97SEli Cohen }
4197e126ba97SEli Cohen 
4198e126ba97SEli Cohen enum {
4199e126ba97SEli Cohen 	MAX_UMR_WR = 128,
4200e126ba97SEli Cohen };
4201e126ba97SEli Cohen 
4202e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
4203e126ba97SEli Cohen {
4204e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
4205e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
4206e126ba97SEli Cohen 	struct ib_pd *pd;
4207e126ba97SEli Cohen 	struct ib_cq *cq;
4208e126ba97SEli Cohen 	struct ib_qp *qp;
4209e126ba97SEli Cohen 	int ret;
4210e126ba97SEli Cohen 
4211e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4212e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4213e126ba97SEli Cohen 	if (!attr || !init_attr) {
4214e126ba97SEli Cohen 		ret = -ENOMEM;
4215e126ba97SEli Cohen 		goto error_0;
4216e126ba97SEli Cohen 	}
4217e126ba97SEli Cohen 
4218ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
4219e126ba97SEli Cohen 	if (IS_ERR(pd)) {
4220e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4221e126ba97SEli Cohen 		ret = PTR_ERR(pd);
4222e126ba97SEli Cohen 		goto error_0;
4223e126ba97SEli Cohen 	}
4224e126ba97SEli Cohen 
4225add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
4226e126ba97SEli Cohen 	if (IS_ERR(cq)) {
4227e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4228e126ba97SEli Cohen 		ret = PTR_ERR(cq);
4229e126ba97SEli Cohen 		goto error_2;
4230e126ba97SEli Cohen 	}
4231e126ba97SEli Cohen 
4232e126ba97SEli Cohen 	init_attr->send_cq = cq;
4233e126ba97SEli Cohen 	init_attr->recv_cq = cq;
4234e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4235e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
4236e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
4237e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4238e126ba97SEli Cohen 	init_attr->port_num = 1;
4239e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4240e126ba97SEli Cohen 	if (IS_ERR(qp)) {
4241e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4242e126ba97SEli Cohen 		ret = PTR_ERR(qp);
4243e126ba97SEli Cohen 		goto error_3;
4244e126ba97SEli Cohen 	}
4245e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
4246e126ba97SEli Cohen 	qp->real_qp    = qp;
4247e126ba97SEli Cohen 	qp->uobject    = NULL;
4248e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
424931fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
425031fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
4251e126ba97SEli Cohen 
4252e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
4253e126ba97SEli Cohen 	attr->port_num = 1;
4254e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4255e126ba97SEli Cohen 				IB_QP_PORT, NULL);
4256e126ba97SEli Cohen 	if (ret) {
4257e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4258e126ba97SEli Cohen 		goto error_4;
4259e126ba97SEli Cohen 	}
4260e126ba97SEli Cohen 
4261e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4262e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
4263e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
4264e126ba97SEli Cohen 
4265e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4266e126ba97SEli Cohen 	if (ret) {
4267e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4268e126ba97SEli Cohen 		goto error_4;
4269e126ba97SEli Cohen 	}
4270e126ba97SEli Cohen 
4271e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4272e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
4273e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4274e126ba97SEli Cohen 	if (ret) {
4275e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4276e126ba97SEli Cohen 		goto error_4;
4277e126ba97SEli Cohen 	}
4278e126ba97SEli Cohen 
4279e126ba97SEli Cohen 	dev->umrc.qp = qp;
4280e126ba97SEli Cohen 	dev->umrc.cq = cq;
4281e126ba97SEli Cohen 	dev->umrc.pd = pd;
4282e126ba97SEli Cohen 
4283e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
4284e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
4285e126ba97SEli Cohen 	if (ret) {
4286e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4287e126ba97SEli Cohen 		goto error_4;
4288e126ba97SEli Cohen 	}
4289e126ba97SEli Cohen 
4290e126ba97SEli Cohen 	kfree(attr);
4291e126ba97SEli Cohen 	kfree(init_attr);
4292e126ba97SEli Cohen 
4293e126ba97SEli Cohen 	return 0;
4294e126ba97SEli Cohen 
4295e126ba97SEli Cohen error_4:
4296e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
429732927e28SMark Bloch 	dev->umrc.qp = NULL;
4298e126ba97SEli Cohen 
4299e126ba97SEli Cohen error_3:
4300add08d76SChristoph Hellwig 	ib_free_cq(cq);
430132927e28SMark Bloch 	dev->umrc.cq = NULL;
4302e126ba97SEli Cohen 
4303e126ba97SEli Cohen error_2:
4304e126ba97SEli Cohen 	ib_dealloc_pd(pd);
430532927e28SMark Bloch 	dev->umrc.pd = NULL;
4306e126ba97SEli Cohen 
4307e126ba97SEli Cohen error_0:
4308e126ba97SEli Cohen 	kfree(attr);
4309e126ba97SEli Cohen 	kfree(init_attr);
4310e126ba97SEli Cohen 	return ret;
4311e126ba97SEli Cohen }
4312e126ba97SEli Cohen 
43136e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
43146e8484c5SMax Gurtovoy {
43156e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
43166e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
43176e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
43186e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
43196e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
43206e8484c5SMax Gurtovoy 	default:
43216e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
43226e8484c5SMax Gurtovoy 	}
43236e8484c5SMax Gurtovoy }
43246e8484c5SMax Gurtovoy 
4325e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
4326e126ba97SEli Cohen {
4327e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
4328e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
4329bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
43307722f47eSHaggai Eran 	int port;
4331e126ba97SEli Cohen 	int ret = 0;
4332e126ba97SEli Cohen 
4333e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
4334e126ba97SEli Cohen 
4335d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
4336d16e91daSHaggai Eran 
4337e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
4338e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
4339e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
4340e126ba97SEli Cohen 		goto error0;
4341e126ba97SEli Cohen 	}
4342e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
4343e126ba97SEli Cohen 	devr->p0->uobject = NULL;
4344e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
4345e126ba97SEli Cohen 
4346bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
4347e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
4348e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
4349e126ba97SEli Cohen 		goto error1;
4350e126ba97SEli Cohen 	}
4351e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
4352e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
4353e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
4354e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
4355e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
4356e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
4357e126ba97SEli Cohen 
4358e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4359e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
4360e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
4361e126ba97SEli Cohen 		goto error2;
4362e126ba97SEli Cohen 	}
4363e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
4364e126ba97SEli Cohen 	devr->x0->inode = NULL;
4365e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
4366e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
4367e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4368e126ba97SEli Cohen 
4369e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4370e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
4371e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
4372e126ba97SEli Cohen 		goto error3;
4373e126ba97SEli Cohen 	}
4374e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
4375e126ba97SEli Cohen 	devr->x1->inode = NULL;
4376e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
4377e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
4378e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4379e126ba97SEli Cohen 
4380e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
4381e126ba97SEli Cohen 	attr.attr.max_sge = 1;
4382e126ba97SEli Cohen 	attr.attr.max_wr = 1;
4383e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
43841a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
4385e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
4386e126ba97SEli Cohen 
4387e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4388e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
4389e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
4390e126ba97SEli Cohen 		goto error4;
4391e126ba97SEli Cohen 	}
4392e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
4393e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
4394e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
4395e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
4396e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
4397e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
4398e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
43991a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
4400e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
44011a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
4402e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
4403e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
4404e126ba97SEli Cohen 
44054aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
44064aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
44074aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
44084aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
44094aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
44104aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
44114aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
44124aa17b28SHaggai Abramonvsky 		goto error5;
44134aa17b28SHaggai Abramonvsky 	}
44144aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
44154aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
44164aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
44174aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
44184aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
44194aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
44201a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
44214aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
44221a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
44234aa17b28SHaggai Abramonvsky 
44247722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
44257722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
44267722f47eSHaggai Eran 			  pkey_change_handler);
44277722f47eSHaggai Eran 		devr->ports[port].devr = devr;
44287722f47eSHaggai Eran 	}
44297722f47eSHaggai Eran 
4430e126ba97SEli Cohen 	return 0;
4431e126ba97SEli Cohen 
44324aa17b28SHaggai Abramonvsky error5:
44334aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
4434e126ba97SEli Cohen error4:
4435e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
4436e126ba97SEli Cohen error3:
4437e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
4438e126ba97SEli Cohen error2:
4439e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
4440e126ba97SEli Cohen error1:
4441e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
4442e126ba97SEli Cohen error0:
4443e126ba97SEli Cohen 	return ret;
4444e126ba97SEli Cohen }
4445e126ba97SEli Cohen 
4446e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
4447e126ba97SEli Cohen {
44487722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
44497722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
44507722f47eSHaggai Eran 	int port;
44517722f47eSHaggai Eran 
44524aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
4453e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
4454e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
4455e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
4456e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
4457e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
44587722f47eSHaggai Eran 
44597722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
44607722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
44617722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
4462e126ba97SEli Cohen }
4463e126ba97SEli Cohen 
4464*b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
4465*b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
4466e53505a8SAchiad Shochat {
4467e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4468e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
4469e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
4470e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
447185c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
4472e53505a8SAchiad Shochat 	u32 ret = 0;
4473e53505a8SAchiad Shochat 
4474*b02289b3SArtemy Kovalyov 	if (rep->grh_required)
4475*b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
4476*b02289b3SArtemy Kovalyov 
4477e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
4478*b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
4479e53505a8SAchiad Shochat 
448085c7c014SDaniel Jurgens 	if (raw_support)
4481*b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
448272cd5717SOr Gerlitz 
4483e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
448472cd5717SOr Gerlitz 		return ret;
4485e53505a8SAchiad Shochat 
4486e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
448772cd5717SOr Gerlitz 		return ret;
4488e53505a8SAchiad Shochat 
4489e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
4490e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
4491e53505a8SAchiad Shochat 
4492e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
4493e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
4494e53505a8SAchiad Shochat 
4495e53505a8SAchiad Shochat 	return ret;
4496e53505a8SAchiad Shochat }
4497e53505a8SAchiad Shochat 
44987738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
44997738613eSIra Weiny 			       struct ib_port_immutable *immutable)
45007738613eSIra Weiny {
45017738613eSIra Weiny 	struct ib_port_attr attr;
4502ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4503ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
4504*b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
45057738613eSIra Weiny 	int err;
45067738613eSIra Weiny 
4507c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
45087738613eSIra Weiny 	if (err)
45097738613eSIra Weiny 		return err;
45107738613eSIra Weiny 
4511*b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
4512*b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
4513*b02289b3SArtemy Kovalyov 						   &rep);
4514*b02289b3SArtemy Kovalyov 		if (err)
4515*b02289b3SArtemy Kovalyov 			return err;
4516*b02289b3SArtemy Kovalyov 	}
4517*b02289b3SArtemy Kovalyov 
45187738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
45197738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
4520*b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
4521ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
4522337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
45237738613eSIra Weiny 
45247738613eSIra Weiny 	return 0;
45257738613eSIra Weiny }
45267738613eSIra Weiny 
45278e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
45288e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
45298e6efa3aSMark Bloch {
45308e6efa3aSMark Bloch 	struct ib_port_attr attr;
45318e6efa3aSMark Bloch 	int err;
45328e6efa3aSMark Bloch 
45338e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
45348e6efa3aSMark Bloch 
45358e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
45368e6efa3aSMark Bloch 	if (err)
45378e6efa3aSMark Bloch 		return err;
45388e6efa3aSMark Bloch 
45398e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
45408e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
45418e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
45428e6efa3aSMark Bloch 
45438e6efa3aSMark Bloch 	return 0;
45448e6efa3aSMark Bloch }
45458e6efa3aSMark Bloch 
45469abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
4547c7342823SIra Weiny {
4548c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
4549c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
45509abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
45519abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
45529abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
4553c7342823SIra Weiny }
4554c7342823SIra Weiny 
455545f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
45569ef9c640SAviv Heller {
45579ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
45589ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
45599ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
45609ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
45619ef9c640SAviv Heller 	int err;
45629ef9c640SAviv Heller 
45639ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
45649ef9c640SAviv Heller 		return 0;
45659ef9c640SAviv Heller 
45669ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
45679ef9c640SAviv Heller 	if (err)
45689ef9c640SAviv Heller 		return err;
45699ef9c640SAviv Heller 
45709ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
45719ef9c640SAviv Heller 	if (IS_ERR(ft)) {
45729ef9c640SAviv Heller 		err = PTR_ERR(ft);
45739ef9c640SAviv Heller 		goto err_destroy_vport_lag;
45749ef9c640SAviv Heller 	}
45759ef9c640SAviv Heller 
45769a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
45779ef9c640SAviv Heller 	return 0;
45789ef9c640SAviv Heller 
45799ef9c640SAviv Heller err_destroy_vport_lag:
45809ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
45819ef9c640SAviv Heller 	return err;
45829ef9c640SAviv Heller }
45839ef9c640SAviv Heller 
458445f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
45859ef9c640SAviv Heller {
45869ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
45879ef9c640SAviv Heller 
45889a4ca38dSMark Bloch 	if (dev->flow_db->lag_demux_ft) {
45899a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
45909a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
45919ef9c640SAviv Heller 
45929ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
45939ef9c640SAviv Heller 	}
45949ef9c640SAviv Heller }
45959ef9c640SAviv Heller 
45967fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
4597fc24fc5eSAchiad Shochat {
4598e53505a8SAchiad Shochat 	int err;
4599e53505a8SAchiad Shochat 
46007fd8aefbSDaniel Jurgens 	dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
46017fd8aefbSDaniel Jurgens 	err = register_netdevice_notifier(&dev->roce[port_num].nb);
46025ec8c83eSAviv Heller 	if (err) {
46037fd8aefbSDaniel Jurgens 		dev->roce[port_num].nb.notifier_call = NULL;
4604e53505a8SAchiad Shochat 		return err;
46055ec8c83eSAviv Heller 	}
4606e53505a8SAchiad Shochat 
4607d012f5d6SOr Gerlitz 	return 0;
4608d012f5d6SOr Gerlitz }
4609d012f5d6SOr Gerlitz 
46107fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
4611e126ba97SEli Cohen {
46127fd8aefbSDaniel Jurgens 	if (dev->roce[port_num].nb.notifier_call) {
46137fd8aefbSDaniel Jurgens 		unregister_netdevice_notifier(&dev->roce[port_num].nb);
46147fd8aefbSDaniel Jurgens 		dev->roce[port_num].nb.notifier_call = NULL;
4615e126ba97SEli Cohen 	}
4616e126ba97SEli Cohen }
4617e126ba97SEli Cohen 
4618e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
4619e126ba97SEli Cohen {
4620e126ba97SEli Cohen 	int err;
4621e126ba97SEli Cohen 
4622ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
4623e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
4624e53505a8SAchiad Shochat 		if (err)
46258e6efa3aSMark Bloch 			return err;
4626ca5b91d6SOr Gerlitz 	}
4627e53505a8SAchiad Shochat 
462845f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
46299ef9c640SAviv Heller 	if (err)
46309ef9c640SAviv Heller 		goto err_disable_roce;
46319ef9c640SAviv Heller 
4632e53505a8SAchiad Shochat 	return 0;
4633e53505a8SAchiad Shochat 
46349ef9c640SAviv Heller err_disable_roce:
4635ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
46369ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
46379ef9c640SAviv Heller 
4638e53505a8SAchiad Shochat 	return err;
4639fc24fc5eSAchiad Shochat }
4640fc24fc5eSAchiad Shochat 
464145f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
4642fc24fc5eSAchiad Shochat {
464345f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
4644ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
4645e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
4646fc24fc5eSAchiad Shochat }
4647fc24fc5eSAchiad Shochat 
4648e1f24a79SParav Pandit struct mlx5_ib_counter {
46497c16f477SKamal Heib 	const char *name;
46507c16f477SKamal Heib 	size_t offset;
46517c16f477SKamal Heib };
46527c16f477SKamal Heib 
46537c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
46547c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
46557c16f477SKamal Heib 
4656e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
46577c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
46587c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
46597c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
46607c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
46617c16f477SKamal Heib };
46627c16f477SKamal Heib 
4663e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
46647c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
46657c16f477SKamal Heib };
46667c16f477SKamal Heib 
4667e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
46687c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
46697c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
46707c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
46717c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
46727c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
46737c16f477SKamal Heib };
46747c16f477SKamal Heib 
4675e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
4676e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
4677e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
4678e1f24a79SParav Pandit 
4679e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
4680e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
4681e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
4682e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
4683e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
4684e1f24a79SParav Pandit };
4685e1f24a79SParav Pandit 
468658dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
468758dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
468858dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
468958dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
469058dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
469158dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
469258dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
469358dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
469458dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
469558dcb60aSParav Pandit };
469658dcb60aSParav Pandit 
46979f876f3dSTalat Batheesh #define INIT_EXT_PPCNT_COUNTER(_name)		\
46989f876f3dSTalat Batheesh 	{ .name = #_name, .offset =	\
46999f876f3dSTalat Batheesh 	MLX5_BYTE_OFF(ppcnt_reg, \
47009f876f3dSTalat Batheesh 		      counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
47019f876f3dSTalat Batheesh 
47029f876f3dSTalat Batheesh static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
47039f876f3dSTalat Batheesh 	INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
47049f876f3dSTalat Batheesh };
47059f876f3dSTalat Batheesh 
4706e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
47070837e86aSMark Bloch {
4708aac4492eSDaniel Jurgens 	int i;
47090837e86aSMark Bloch 
47107c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
4711921c0f5bSParav Pandit 		if (dev->port[i].cnts.set_id_valid)
47120837e86aSMark Bloch 			mlx5_core_dealloc_q_counter(dev->mdev,
4713e1f24a79SParav Pandit 						    dev->port[i].cnts.set_id);
4714e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
4715e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
47167c16f477SKamal Heib 	}
47177c16f477SKamal Heib }
47187c16f477SKamal Heib 
4719e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
4720e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
47217c16f477SKamal Heib {
47227c16f477SKamal Heib 	u32 num_counters;
47237c16f477SKamal Heib 
47247c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
47257c16f477SKamal Heib 
47267c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
47277c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
47287c16f477SKamal Heib 
47297c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
47307c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
473158dcb60aSParav Pandit 
473258dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
473358dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
473458dcb60aSParav Pandit 
4735e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
47367c16f477SKamal Heib 
4737e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4738e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
4739e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
4740e1f24a79SParav Pandit 	}
47419f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
47429f876f3dSTalat Batheesh 		cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
47439f876f3dSTalat Batheesh 		num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
47449f876f3dSTalat Batheesh 	}
4745e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
4746e1f24a79SParav Pandit 	if (!cnts->names)
47477c16f477SKamal Heib 		return -ENOMEM;
47487c16f477SKamal Heib 
4749e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
4750e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
4751e1f24a79SParav Pandit 	if (!cnts->offsets)
47527c16f477SKamal Heib 		goto err_names;
47537c16f477SKamal Heib 
47547c16f477SKamal Heib 	return 0;
47557c16f477SKamal Heib 
47567c16f477SKamal Heib err_names:
4757e1f24a79SParav Pandit 	kfree(cnts->names);
4758aac4492eSDaniel Jurgens 	cnts->names = NULL;
47597c16f477SKamal Heib 	return -ENOMEM;
47607c16f477SKamal Heib }
47617c16f477SKamal Heib 
4762e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
47637c16f477SKamal Heib 				  const char **names,
47647c16f477SKamal Heib 				  size_t *offsets)
47657c16f477SKamal Heib {
47667c16f477SKamal Heib 	int i;
47677c16f477SKamal Heib 	int j = 0;
47687c16f477SKamal Heib 
47697c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
47707c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
47717c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
47727c16f477SKamal Heib 	}
47737c16f477SKamal Heib 
47747c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
47757c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
47767c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
47777c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
47787c16f477SKamal Heib 		}
47797c16f477SKamal Heib 	}
47807c16f477SKamal Heib 
47817c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
47827c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
47837c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
47847c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
47857c16f477SKamal Heib 		}
47867c16f477SKamal Heib 	}
4787e1f24a79SParav Pandit 
478858dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
478958dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
479058dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
479158dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
479258dcb60aSParav Pandit 		}
479358dcb60aSParav Pandit 	}
479458dcb60aSParav Pandit 
4795e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4796e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
4797e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
4798e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
4799e1f24a79SParav Pandit 		}
4800e1f24a79SParav Pandit 	}
48019f876f3dSTalat Batheesh 
48029f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
48039f876f3dSTalat Batheesh 		for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
48049f876f3dSTalat Batheesh 			names[j] = ext_ppcnt_cnts[i].name;
48059f876f3dSTalat Batheesh 			offsets[j] = ext_ppcnt_cnts[i].offset;
48069f876f3dSTalat Batheesh 		}
48079f876f3dSTalat Batheesh 	}
48080837e86aSMark Bloch }
48090837e86aSMark Bloch 
4810e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
48110837e86aSMark Bloch {
4812aac4492eSDaniel Jurgens 	int err = 0;
48130837e86aSMark Bloch 	int i;
48140837e86aSMark Bloch 
48150837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
4816aac4492eSDaniel Jurgens 		err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
4817aac4492eSDaniel Jurgens 		if (err)
4818aac4492eSDaniel Jurgens 			goto err_alloc;
48197c16f477SKamal Heib 
4820aac4492eSDaniel Jurgens 		mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
4821aac4492eSDaniel Jurgens 				      dev->port[i].cnts.offsets);
4822aac4492eSDaniel Jurgens 
4823aac4492eSDaniel Jurgens 		err = mlx5_core_alloc_q_counter(dev->mdev,
4824aac4492eSDaniel Jurgens 						&dev->port[i].cnts.set_id);
4825aac4492eSDaniel Jurgens 		if (err) {
48260837e86aSMark Bloch 			mlx5_ib_warn(dev,
48270837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
4828aac4492eSDaniel Jurgens 				     i + 1, err);
4829aac4492eSDaniel Jurgens 			goto err_alloc;
48300837e86aSMark Bloch 		}
4831aac4492eSDaniel Jurgens 		dev->port[i].cnts.set_id_valid = true;
48320837e86aSMark Bloch 	}
48330837e86aSMark Bloch 
48340837e86aSMark Bloch 	return 0;
48350837e86aSMark Bloch 
4836aac4492eSDaniel Jurgens err_alloc:
4837aac4492eSDaniel Jurgens 	mlx5_ib_dealloc_counters(dev);
4838aac4492eSDaniel Jurgens 	return err;
48390837e86aSMark Bloch }
48400837e86aSMark Bloch 
48410ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
48420ad17a8fSMark Bloch 						    u8 port_num)
48430ad17a8fSMark Bloch {
48447c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
48457c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
48460ad17a8fSMark Bloch 
48470ad17a8fSMark Bloch 	/* We support only per port stats */
48480ad17a8fSMark Bloch 	if (port_num == 0)
48490ad17a8fSMark Bloch 		return NULL;
48500ad17a8fSMark Bloch 
4851e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
4852e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
48539f876f3dSTalat Batheesh 					  port->cnts.num_cong_counters +
48549f876f3dSTalat Batheesh 					  port->cnts.num_ext_ppcnt_counters,
48550ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
48560ad17a8fSMark Bloch }
48570ad17a8fSMark Bloch 
4858aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
4859e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
4860e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
4861e1f24a79SParav Pandit {
4862e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
4863e1f24a79SParav Pandit 	void *out;
4864e1f24a79SParav Pandit 	__be32 val;
4865e1f24a79SParav Pandit 	int ret, i;
4866e1f24a79SParav Pandit 
48671b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
4868e1f24a79SParav Pandit 	if (!out)
4869e1f24a79SParav Pandit 		return -ENOMEM;
4870e1f24a79SParav Pandit 
4871aac4492eSDaniel Jurgens 	ret = mlx5_core_query_q_counter(mdev,
4872e1f24a79SParav Pandit 					port->cnts.set_id, 0,
4873e1f24a79SParav Pandit 					out, outlen);
4874e1f24a79SParav Pandit 	if (ret)
4875e1f24a79SParav Pandit 		goto free;
4876e1f24a79SParav Pandit 
4877e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
4878e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
4879e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
4880e1f24a79SParav Pandit 	}
4881e1f24a79SParav Pandit 
4882e1f24a79SParav Pandit free:
4883e1f24a79SParav Pandit 	kvfree(out);
4884e1f24a79SParav Pandit 	return ret;
4885e1f24a79SParav Pandit }
4886e1f24a79SParav Pandit 
48879f876f3dSTalat Batheesh static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
48889f876f3dSTalat Batheesh 					  struct mlx5_ib_port *port,
48899f876f3dSTalat Batheesh 					  struct rdma_hw_stats *stats)
48909f876f3dSTalat Batheesh {
48919f876f3dSTalat Batheesh 	int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
48929f876f3dSTalat Batheesh 	int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
48939f876f3dSTalat Batheesh 	int ret, i;
48949f876f3dSTalat Batheesh 	void *out;
48959f876f3dSTalat Batheesh 
48969f876f3dSTalat Batheesh 	out = kvzalloc(sz, GFP_KERNEL);
48979f876f3dSTalat Batheesh 	if (!out)
48989f876f3dSTalat Batheesh 		return -ENOMEM;
48999f876f3dSTalat Batheesh 
49009f876f3dSTalat Batheesh 	ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
49019f876f3dSTalat Batheesh 	if (ret)
49029f876f3dSTalat Batheesh 		goto free;
49039f876f3dSTalat Batheesh 
49049f876f3dSTalat Batheesh 	for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
49059f876f3dSTalat Batheesh 		stats->value[i + offset] =
49069f876f3dSTalat Batheesh 			be64_to_cpup((__be64 *)(out +
49079f876f3dSTalat Batheesh 				    port->cnts.offsets[i + offset]));
49089f876f3dSTalat Batheesh 	}
49099f876f3dSTalat Batheesh 
49109f876f3dSTalat Batheesh free:
49119f876f3dSTalat Batheesh 	kvfree(out);
49129f876f3dSTalat Batheesh 	return ret;
49139f876f3dSTalat Batheesh }
49149f876f3dSTalat Batheesh 
49150ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
49160ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
49177c16f477SKamal Heib 				u8 port_num, int index)
49180ad17a8fSMark Bloch {
49190ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
49207c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
4921aac4492eSDaniel Jurgens 	struct mlx5_core_dev *mdev;
4922e1f24a79SParav Pandit 	int ret, num_counters;
4923aac4492eSDaniel Jurgens 	u8 mdev_port_num;
49240ad17a8fSMark Bloch 
49257c16f477SKamal Heib 	if (!stats)
4926e1f24a79SParav Pandit 		return -EINVAL;
49270ad17a8fSMark Bloch 
49289f876f3dSTalat Batheesh 	num_counters = port->cnts.num_q_counters +
49299f876f3dSTalat Batheesh 		       port->cnts.num_cong_counters +
49309f876f3dSTalat Batheesh 		       port->cnts.num_ext_ppcnt_counters;
4931aac4492eSDaniel Jurgens 
4932aac4492eSDaniel Jurgens 	/* q_counters are per IB device, query the master mdev */
4933aac4492eSDaniel Jurgens 	ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
49340ad17a8fSMark Bloch 	if (ret)
4935e1f24a79SParav Pandit 		return ret;
49360ad17a8fSMark Bloch 
49379f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
49389f876f3dSTalat Batheesh 		ret =  mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
49399f876f3dSTalat Batheesh 		if (ret)
49409f876f3dSTalat Batheesh 			return ret;
49419f876f3dSTalat Batheesh 	}
49429f876f3dSTalat Batheesh 
4943e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4944aac4492eSDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
4945aac4492eSDaniel Jurgens 						    &mdev_port_num);
4946aac4492eSDaniel Jurgens 		if (!mdev) {
4947aac4492eSDaniel Jurgens 			/* If port is not affiliated yet, its in down state
4948aac4492eSDaniel Jurgens 			 * which doesn't have any counters yet, so it would be
4949aac4492eSDaniel Jurgens 			 * zero. So no need to read from the HCA.
4950aac4492eSDaniel Jurgens 			 */
4951aac4492eSDaniel Jurgens 			goto done;
4952aac4492eSDaniel Jurgens 		}
495371a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
495471a0ff65SMajd Dibbiny 						   stats->value +
495571a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters,
495671a0ff65SMajd Dibbiny 						   port->cnts.num_cong_counters,
495771a0ff65SMajd Dibbiny 						   port->cnts.offsets +
495871a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters);
4959aac4492eSDaniel Jurgens 
4960aac4492eSDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
4961e1f24a79SParav Pandit 		if (ret)
4962e1f24a79SParav Pandit 			return ret;
49630ad17a8fSMark Bloch 	}
49647c16f477SKamal Heib 
4965aac4492eSDaniel Jurgens done:
4966e1f24a79SParav Pandit 	return num_counters;
49670ad17a8fSMark Bloch }
49680ad17a8fSMark Bloch 
49698e959601SNiranjana Vishwanathapura static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
49708e959601SNiranjana Vishwanathapura {
49718e959601SNiranjana Vishwanathapura 	return mlx5_rdma_netdev_free(netdev);
49728e959601SNiranjana Vishwanathapura }
49738e959601SNiranjana Vishwanathapura 
4974693dfd5aSErez Shitrit static struct net_device*
4975693dfd5aSErez Shitrit mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
4976693dfd5aSErez Shitrit 			  u8 port_num,
4977693dfd5aSErez Shitrit 			  enum rdma_netdev_t type,
4978693dfd5aSErez Shitrit 			  const char *name,
4979693dfd5aSErez Shitrit 			  unsigned char name_assign_type,
4980693dfd5aSErez Shitrit 			  void (*setup)(struct net_device *))
4981693dfd5aSErez Shitrit {
49828e959601SNiranjana Vishwanathapura 	struct net_device *netdev;
49838e959601SNiranjana Vishwanathapura 	struct rdma_netdev *rn;
49848e959601SNiranjana Vishwanathapura 
4985693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
4986693dfd5aSErez Shitrit 		return ERR_PTR(-EOPNOTSUPP);
4987693dfd5aSErez Shitrit 
49888e959601SNiranjana Vishwanathapura 	netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
4989693dfd5aSErez Shitrit 					name, setup);
49908e959601SNiranjana Vishwanathapura 	if (likely(!IS_ERR_OR_NULL(netdev))) {
49918e959601SNiranjana Vishwanathapura 		rn = netdev_priv(netdev);
49928e959601SNiranjana Vishwanathapura 		rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
4993693dfd5aSErez Shitrit 	}
49948e959601SNiranjana Vishwanathapura 	return netdev;
4995693dfd5aSErez Shitrit }
4996693dfd5aSErez Shitrit 
4997fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
4998fe248c3aSMaor Gottlieb {
4999fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
5000fe248c3aSMaor Gottlieb 		return;
5001fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5002fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
5003fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
5004fe248c3aSMaor Gottlieb }
5005fe248c3aSMaor Gottlieb 
500603404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
500703404e8aSMaor Gottlieb {
500803404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
500903404e8aSMaor Gottlieb 		return;
501003404e8aSMaor Gottlieb 
501103404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
5012fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5013fe248c3aSMaor Gottlieb }
5014fe248c3aSMaor Gottlieb 
5015fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5016fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
5017fe248c3aSMaor Gottlieb {
5018fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5019fe248c3aSMaor Gottlieb 	char lbuf[20];
5020fe248c3aSMaor Gottlieb 	int len;
5021fe248c3aSMaor Gottlieb 
5022fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5023fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
5024fe248c3aSMaor Gottlieb }
5025fe248c3aSMaor Gottlieb 
5026fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5027fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
5028fe248c3aSMaor Gottlieb {
5029fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5030fe248c3aSMaor Gottlieb 	u32 timeout;
5031fe248c3aSMaor Gottlieb 	u32 var;
5032fe248c3aSMaor Gottlieb 
5033fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
5034fe248c3aSMaor Gottlieb 		return -EFAULT;
5035fe248c3aSMaor Gottlieb 
5036fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5037fe248c3aSMaor Gottlieb 			1000);
5038fe248c3aSMaor Gottlieb 	if (timeout != var)
5039fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5040fe248c3aSMaor Gottlieb 			    timeout);
5041fe248c3aSMaor Gottlieb 
5042fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
5043fe248c3aSMaor Gottlieb 
5044fe248c3aSMaor Gottlieb 	return count;
5045fe248c3aSMaor Gottlieb }
5046fe248c3aSMaor Gottlieb 
5047fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
5048fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
5049fe248c3aSMaor Gottlieb 	.open	= simple_open,
5050fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
5051fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
5052fe248c3aSMaor Gottlieb };
5053fe248c3aSMaor Gottlieb 
5054fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5055fe248c3aSMaor Gottlieb {
5056fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
5057fe248c3aSMaor Gottlieb 
5058fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
5059fe248c3aSMaor Gottlieb 		return 0;
5060fe248c3aSMaor Gottlieb 
5061fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5062fe248c3aSMaor Gottlieb 	if (!dbg)
5063fe248c3aSMaor Gottlieb 		return -ENOMEM;
5064fe248c3aSMaor Gottlieb 
5065cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
5066cbafad87SSudip Mukherjee 
5067fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
5068fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
5069fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
5070fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
5071cbafad87SSudip Mukherjee 		goto out_debugfs;
5072fe248c3aSMaor Gottlieb 
5073fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
5074fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
5075fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5076fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
5077fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
5078fe248c3aSMaor Gottlieb 		goto out_debugfs;
5079fe248c3aSMaor Gottlieb 
5080fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
5081fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
5082fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5083fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
5084fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
5085fe248c3aSMaor Gottlieb 		goto out_debugfs;
5086fe248c3aSMaor Gottlieb 
5087fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
5088fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
5089fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
5090fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
5091fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
5092fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
5093fe248c3aSMaor Gottlieb 		goto out_debugfs;
5094fe248c3aSMaor Gottlieb 
5095fe248c3aSMaor Gottlieb 	return 0;
5096fe248c3aSMaor Gottlieb 
5097fe248c3aSMaor Gottlieb out_debugfs:
5098fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5099fe248c3aSMaor Gottlieb 	return -ENOMEM;
510003404e8aSMaor Gottlieb }
510103404e8aSMaor Gottlieb 
510203404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
510303404e8aSMaor Gottlieb {
510403404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
510503404e8aSMaor Gottlieb 		return;
510603404e8aSMaor Gottlieb 
510703404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
510803404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
510903404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
511003404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
511103404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
5112fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
5113fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
5114fe248c3aSMaor Gottlieb 
5115fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
5116fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
511703404e8aSMaor Gottlieb }
511803404e8aSMaor Gottlieb 
511984305d71SLeon Romanovsky static const struct cpumask *
512084305d71SLeon Romanovsky mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
512140b24403SSagi Grimberg {
512240b24403SSagi Grimberg 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
512340b24403SSagi Grimberg 
51246082d9c9SIsrael Rukshin 	return mlx5_get_vector_affinity_hint(dev->mdev, comp_vector);
512540b24403SSagi Grimberg }
512640b24403SSagi Grimberg 
512732f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
512832f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
512932f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
513032f69e4bSDaniel Jurgens {
513132f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
513232f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
513332f69e4bSDaniel Jurgens 	int comps;
513432f69e4bSDaniel Jurgens 	int err;
513532f69e4bSDaniel Jurgens 	int i;
513632f69e4bSDaniel Jurgens 
5137a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5138a9e546e7SParav Pandit 
513932f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
514032f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
514132f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
514232f69e4bSDaniel Jurgens 		return;
514332f69e4bSDaniel Jurgens 	}
514432f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
514532f69e4bSDaniel Jurgens 
514632f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
514732f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
514832f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
514932f69e4bSDaniel Jurgens 
515032f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
515132f69e4bSDaniel Jurgens 	if (comps) {
515232f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
515332f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
515432f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
515532f69e4bSDaniel Jurgens 
515632f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
515732f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
515832f69e4bSDaniel Jurgens 
515932f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
516032f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
516132f69e4bSDaniel Jurgens 	}
516232f69e4bSDaniel Jurgens 
516332f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
516432f69e4bSDaniel Jurgens 
516532f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
516632f69e4bSDaniel Jurgens 
516732f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
516832f69e4bSDaniel Jurgens 
516932f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
517032f69e4bSDaniel Jurgens 
517132f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
517232f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
517332f69e4bSDaniel Jurgens 	 * it back to the list.
517432f69e4bSDaniel Jurgens 	 */
517532f69e4bSDaniel Jurgens 	if (err)
517632f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
517732f69e4bSDaniel Jurgens 			    port_num + 1);
517832f69e4bSDaniel Jurgens 
517932f69e4bSDaniel Jurgens 	ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
518032f69e4bSDaniel Jurgens }
518132f69e4bSDaniel Jurgens 
518232f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
518332f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
518432f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
518532f69e4bSDaniel Jurgens {
518632f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
518732f69e4bSDaniel Jurgens 	int err;
518832f69e4bSDaniel Jurgens 
518932f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
519032f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
519132f69e4bSDaniel Jurgens 		mlx5_ib_warn(ibdev, "port %d already affiliated.\n",
519232f69e4bSDaniel Jurgens 			     port_num + 1);
519332f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
519432f69e4bSDaniel Jurgens 		return false;
519532f69e4bSDaniel Jurgens 	}
519632f69e4bSDaniel Jurgens 
519732f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
519832f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
519932f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
520032f69e4bSDaniel Jurgens 
520132f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
520232f69e4bSDaniel Jurgens 	if (err)
520332f69e4bSDaniel Jurgens 		goto unbind;
520432f69e4bSDaniel Jurgens 
520532f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
520632f69e4bSDaniel Jurgens 	if (err)
520732f69e4bSDaniel Jurgens 		goto unbind;
520832f69e4bSDaniel Jurgens 
520932f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
521032f69e4bSDaniel Jurgens 	if (err) {
521132f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
521232f69e4bSDaniel Jurgens 			    port_num + 1);
521332f69e4bSDaniel Jurgens 		goto unbind;
521432f69e4bSDaniel Jurgens 	}
521532f69e4bSDaniel Jurgens 
5216a9e546e7SParav Pandit 	err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
5217a9e546e7SParav Pandit 	if (err)
5218a9e546e7SParav Pandit 		goto unbind;
5219a9e546e7SParav Pandit 
522032f69e4bSDaniel Jurgens 	return true;
522132f69e4bSDaniel Jurgens 
522232f69e4bSDaniel Jurgens unbind:
522332f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
522432f69e4bSDaniel Jurgens 	return false;
522532f69e4bSDaniel Jurgens }
522632f69e4bSDaniel Jurgens 
522732f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
522832f69e4bSDaniel Jurgens {
522932f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
523032f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
523132f69e4bSDaniel Jurgens 							  port_num + 1);
523232f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
523332f69e4bSDaniel Jurgens 	int err;
523432f69e4bSDaniel Jurgens 	int i;
523532f69e4bSDaniel Jurgens 
523632f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
523732f69e4bSDaniel Jurgens 		return 0;
523832f69e4bSDaniel Jurgens 
523932f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
524032f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
524132f69e4bSDaniel Jurgens 	if (err)
524232f69e4bSDaniel Jurgens 		return err;
524332f69e4bSDaniel Jurgens 
524432f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
524532f69e4bSDaniel Jurgens 	if (err)
524632f69e4bSDaniel Jurgens 		return err;
524732f69e4bSDaniel Jurgens 
524832f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
524932f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
525032f69e4bSDaniel Jurgens 		bool bound = false;
525132f69e4bSDaniel Jurgens 
525232f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
525332f69e4bSDaniel Jurgens 		if (i == port_num) {
525432f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
525532f69e4bSDaniel Jurgens 			if (!mpi) {
525632f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
525732f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
525832f69e4bSDaniel Jurgens 				return -ENOMEM;
525932f69e4bSDaniel Jurgens 			}
526032f69e4bSDaniel Jurgens 
526132f69e4bSDaniel Jurgens 			mpi->is_master = true;
526232f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
526332f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
526432f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
526532f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
526632f69e4bSDaniel Jurgens 			mpi = NULL;
526732f69e4bSDaniel Jurgens 			continue;
526832f69e4bSDaniel Jurgens 		}
526932f69e4bSDaniel Jurgens 
527032f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
527132f69e4bSDaniel Jurgens 				    list) {
527232f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
527332f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
527432f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
527532f69e4bSDaniel Jurgens 			}
527632f69e4bSDaniel Jurgens 
527732f69e4bSDaniel Jurgens 			if (bound) {
527832f69e4bSDaniel Jurgens 				dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
527932f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
528032f69e4bSDaniel Jurgens 				list_del(&mpi->list);
528132f69e4bSDaniel Jurgens 				break;
528232f69e4bSDaniel Jurgens 			}
528332f69e4bSDaniel Jurgens 		}
528432f69e4bSDaniel Jurgens 		if (!bound) {
528532f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
528632f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
528732f69e4bSDaniel Jurgens 				    i + 1);
528832f69e4bSDaniel Jurgens 		}
528932f69e4bSDaniel Jurgens 	}
529032f69e4bSDaniel Jurgens 
529132f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
529232f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
529332f69e4bSDaniel Jurgens 	return err;
529432f69e4bSDaniel Jurgens }
529532f69e4bSDaniel Jurgens 
529632f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
529732f69e4bSDaniel Jurgens {
529832f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
529932f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
530032f69e4bSDaniel Jurgens 							  port_num + 1);
530132f69e4bSDaniel Jurgens 	int i;
530232f69e4bSDaniel Jurgens 
530332f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
530432f69e4bSDaniel Jurgens 		return;
530532f69e4bSDaniel Jurgens 
530632f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
530732f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
530832f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
530932f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
531032f69e4bSDaniel Jurgens 			if (i == port_num) {
531132f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
531232f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
531332f69e4bSDaniel Jurgens 			} else {
531432f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
531532f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
531632f69e4bSDaniel Jurgens 			}
531732f69e4bSDaniel Jurgens 		}
531832f69e4bSDaniel Jurgens 	}
531932f69e4bSDaniel Jurgens 
532032f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
532132f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
532232f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
532332f69e4bSDaniel Jurgens 
532432f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
532532f69e4bSDaniel Jurgens }
532632f69e4bSDaniel Jurgens 
53279a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
53289a119cd5SJason Gunthorpe 	mlx5_ib_dm,
53299a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
533024da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
53319a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
533224da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
533383bb4442SJason Gunthorpe 			    UA_MANDATORY),
53349a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
533524da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
533683bb4442SJason Gunthorpe 			    UA_MANDATORY));
533724da0016SAriel Levkovich 
53389a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
53399a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
53409a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
5341c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
53429a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
5343c6475a0bSAviad Yehezkel 			   UVERBS_ATTR_TYPE(u64),
534483bb4442SJason Gunthorpe 			   UA_MANDATORY));
5345c6475a0bSAviad Yehezkel 
5346c59450c4SYishai Hadas #define NUM_TREES	3
53478c84660bSMatan Barak static int populate_specs_root(struct mlx5_ib_dev *dev)
53488c84660bSMatan Barak {
53498c84660bSMatan Barak 	const struct uverbs_object_tree_def *default_root[NUM_TREES + 1] = {
53508c84660bSMatan Barak 		uverbs_default_get_objects()};
53518c84660bSMatan Barak 	size_t num_trees = 1;
53528c84660bSMatan Barak 
5353c6475a0bSAviad Yehezkel 	if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
5354c6475a0bSAviad Yehezkel 	    !WARN_ON(num_trees >= ARRAY_SIZE(default_root)))
5355c6475a0bSAviad Yehezkel 		default_root[num_trees++] = &mlx5_ib_flow_action;
5356c6475a0bSAviad Yehezkel 
535724da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(dev->mdev, memic) &&
535824da0016SAriel Levkovich 	    !WARN_ON(num_trees >= ARRAY_SIZE(default_root)))
535924da0016SAriel Levkovich 		default_root[num_trees++] = &mlx5_ib_dm;
536024da0016SAriel Levkovich 
5361c59450c4SYishai Hadas 	if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
5362c59450c4SYishai Hadas 			    MLX5_GENERAL_OBJ_TYPES_CAP_UCTX &&
5363c59450c4SYishai Hadas 	    !WARN_ON(num_trees >= ARRAY_SIZE(default_root)))
5364c59450c4SYishai Hadas 		default_root[num_trees++] = mlx5_ib_get_devx_tree();
5365c59450c4SYishai Hadas 
536687fc2a62SJason Gunthorpe 	dev->ib_dev.driver_specs_root =
53678c84660bSMatan Barak 		uverbs_alloc_spec_tree(num_trees, default_root);
53688c84660bSMatan Barak 
536987fc2a62SJason Gunthorpe 	return PTR_ERR_OR_ZERO(dev->ib_dev.driver_specs_root);
53708c84660bSMatan Barak }
53718c84660bSMatan Barak 
53728c84660bSMatan Barak static void depopulate_specs_root(struct mlx5_ib_dev *dev)
53738c84660bSMatan Barak {
537487fc2a62SJason Gunthorpe 	uverbs_free_spec_tree(dev->ib_dev.driver_specs_root);
53758c84660bSMatan Barak }
53768c84660bSMatan Barak 
53771a1e03dcSRaed Salem static int mlx5_ib_read_counters(struct ib_counters *counters,
53781a1e03dcSRaed Salem 				 struct ib_counters_read_attr *read_attr,
53791a1e03dcSRaed Salem 				 struct uverbs_attr_bundle *attrs)
53801a1e03dcSRaed Salem {
53811a1e03dcSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
53821a1e03dcSRaed Salem 	struct mlx5_read_counters_attr mread_attr = {};
53831a1e03dcSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc;
53841a1e03dcSRaed Salem 	int ret, i;
53851a1e03dcSRaed Salem 
53861a1e03dcSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
53871a1e03dcSRaed Salem 	if (mcounters->cntrs_max_index > read_attr->ncounters) {
53881a1e03dcSRaed Salem 		ret = -EINVAL;
53891a1e03dcSRaed Salem 		goto err_bound;
53901a1e03dcSRaed Salem 	}
53911a1e03dcSRaed Salem 
53921a1e03dcSRaed Salem 	mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
53931a1e03dcSRaed Salem 				 GFP_KERNEL);
53941a1e03dcSRaed Salem 	if (!mread_attr.out) {
53951a1e03dcSRaed Salem 		ret = -ENOMEM;
53961a1e03dcSRaed Salem 		goto err_bound;
53971a1e03dcSRaed Salem 	}
53981a1e03dcSRaed Salem 
53991a1e03dcSRaed Salem 	mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
54001a1e03dcSRaed Salem 	mread_attr.flags = read_attr->flags;
54011a1e03dcSRaed Salem 	ret = mcounters->read_counters(counters->device, &mread_attr);
54021a1e03dcSRaed Salem 	if (ret)
54031a1e03dcSRaed Salem 		goto err_read;
54041a1e03dcSRaed Salem 
54051a1e03dcSRaed Salem 	/* do the pass over the counters data array to assign according to the
54061a1e03dcSRaed Salem 	 * descriptions and indexing pairs
54071a1e03dcSRaed Salem 	 */
54081a1e03dcSRaed Salem 	desc = mcounters->counters_data;
54091a1e03dcSRaed Salem 	for (i = 0; i < mcounters->ncounters; i++)
54101a1e03dcSRaed Salem 		read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
54111a1e03dcSRaed Salem 
54121a1e03dcSRaed Salem err_read:
54131a1e03dcSRaed Salem 	kfree(mread_attr.out);
54141a1e03dcSRaed Salem err_bound:
54151a1e03dcSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
54161a1e03dcSRaed Salem 	return ret;
54171a1e03dcSRaed Salem }
54181a1e03dcSRaed Salem 
5419b29e2a13SRaed Salem static int mlx5_ib_destroy_counters(struct ib_counters *counters)
5420b29e2a13SRaed Salem {
5421b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5422b29e2a13SRaed Salem 
54233b3233fbSRaed Salem 	counters_clear_description(counters);
54243b3233fbSRaed Salem 	if (mcounters->hw_cntrs_hndl)
54253b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(counters->device)->mdev,
54263b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
54273b3233fbSRaed Salem 
5428b29e2a13SRaed Salem 	kfree(mcounters);
5429b29e2a13SRaed Salem 
5430b29e2a13SRaed Salem 	return 0;
5431b29e2a13SRaed Salem }
5432b29e2a13SRaed Salem 
5433b29e2a13SRaed Salem static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
5434b29e2a13SRaed Salem 						   struct uverbs_attr_bundle *attrs)
5435b29e2a13SRaed Salem {
5436b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters;
5437b29e2a13SRaed Salem 
5438b29e2a13SRaed Salem 	mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
5439b29e2a13SRaed Salem 	if (!mcounters)
5440b29e2a13SRaed Salem 		return ERR_PTR(-ENOMEM);
5441b29e2a13SRaed Salem 
54423b3233fbSRaed Salem 	mutex_init(&mcounters->mcntrs_mutex);
54433b3233fbSRaed Salem 
5444b29e2a13SRaed Salem 	return &mcounters->ibcntrs;
5445b29e2a13SRaed Salem }
5446b29e2a13SRaed Salem 
5447b5ca15adSMark Bloch void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
5448e126ba97SEli Cohen {
544932f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
54503cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
54513cc297dbSMark Bloch 	cleanup_srcu_struct(&dev->mr_srcu);
54523cc297dbSMark Bloch #endif
545316c1975fSMark Bloch 	kfree(dev->port);
545416c1975fSMark Bloch }
545516c1975fSMark Bloch 
5456b5ca15adSMark Bloch int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
545716c1975fSMark Bloch {
545816c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
54594babcf97SAviv Heller 	const char *name;
5460e126ba97SEli Cohen 	int err;
546132f69e4bSDaniel Jurgens 	int i;
5462e126ba97SEli Cohen 
5463508562d6SDaniel Jurgens 	dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
54640837e86aSMark Bloch 			    GFP_KERNEL);
54650837e86aSMark Bloch 	if (!dev->port)
546616c1975fSMark Bloch 		return -ENOMEM;
54670837e86aSMark Bloch 
546832f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
546932f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
547032f69e4bSDaniel Jurgens 		rwlock_init(&dev->roce[i].netdev_lock);
547132f69e4bSDaniel Jurgens 	}
547232f69e4bSDaniel Jurgens 
547332f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
5474e126ba97SEli Cohen 	if (err)
54750837e86aSMark Bloch 		goto err_free_port;
5476e126ba97SEli Cohen 
547732f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
547832f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
547932f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
548032f69e4bSDaniel Jurgens 			if (err)
548132f69e4bSDaniel Jurgens 				break;
548232f69e4bSDaniel Jurgens 		}
548332f69e4bSDaniel Jurgens 	} else {
548432f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
548532f69e4bSDaniel Jurgens 	}
548632f69e4bSDaniel Jurgens 	if (err)
548732f69e4bSDaniel Jurgens 		goto err_mp;
548832f69e4bSDaniel Jurgens 
54891b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
5490e126ba97SEli Cohen 		get_ext_port_caps(dev);
5491e126ba97SEli Cohen 
54924babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
54934babcf97SAviv Heller 		name = "mlx5_%d";
54944babcf97SAviv Heller 	else
54954babcf97SAviv Heller 		name = "mlx5_bond_%d";
54964babcf97SAviv Heller 
54974babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
5498e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
5499e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
5500c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
5501e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
5502233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
5503233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
55049b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
5505e126ba97SEli Cohen 
55063cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
55073cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
55083cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
55093cc297dbSMark Bloch 
551024da0016SAriel Levkovich 	spin_lock_init(&dev->memic.memic_lock);
551124da0016SAriel Levkovich 	dev->memic.dev = mdev;
551224da0016SAriel Levkovich 
55133cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
55143cc297dbSMark Bloch 	err = init_srcu_struct(&dev->mr_srcu);
55153cc297dbSMark Bloch 	if (err)
55163cc297dbSMark Bloch 		goto err_free_port;
55173cc297dbSMark Bloch #endif
55183cc297dbSMark Bloch 
551916c1975fSMark Bloch 	return 0;
552032f69e4bSDaniel Jurgens err_mp:
552132f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
552216c1975fSMark Bloch 
552316c1975fSMark Bloch err_free_port:
552416c1975fSMark Bloch 	kfree(dev->port);
552516c1975fSMark Bloch 
552616c1975fSMark Bloch 	return -ENOMEM;
552716c1975fSMark Bloch }
552816c1975fSMark Bloch 
55299a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
55309a4ca38dSMark Bloch {
55319a4ca38dSMark Bloch 	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
55329a4ca38dSMark Bloch 
55339a4ca38dSMark Bloch 	if (!dev->flow_db)
55349a4ca38dSMark Bloch 		return -ENOMEM;
55359a4ca38dSMark Bloch 
55369a4ca38dSMark Bloch 	mutex_init(&dev->flow_db->lock);
55379a4ca38dSMark Bloch 
55389a4ca38dSMark Bloch 	return 0;
55399a4ca38dSMark Bloch }
55409a4ca38dSMark Bloch 
5541b5ca15adSMark Bloch int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
5542b5ca15adSMark Bloch {
5543b5ca15adSMark Bloch 	struct mlx5_ib_dev *nic_dev;
5544b5ca15adSMark Bloch 
5545b5ca15adSMark Bloch 	nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
5546b5ca15adSMark Bloch 
5547b5ca15adSMark Bloch 	if (!nic_dev)
5548b5ca15adSMark Bloch 		return -EINVAL;
5549b5ca15adSMark Bloch 
5550b5ca15adSMark Bloch 	dev->flow_db = nic_dev->flow_db;
5551b5ca15adSMark Bloch 
5552b5ca15adSMark Bloch 	return 0;
5553b5ca15adSMark Bloch }
5554b5ca15adSMark Bloch 
55559a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
55569a4ca38dSMark Bloch {
55579a4ca38dSMark Bloch 	kfree(dev->flow_db);
55589a4ca38dSMark Bloch }
55599a4ca38dSMark Bloch 
5560b5ca15adSMark Bloch int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
556116c1975fSMark Bloch {
556216c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
556316c1975fSMark Bloch 	int err;
556416c1975fSMark Bloch 
5565e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
5566e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
5567e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
5568e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
5569e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
5570e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
5571e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
557241c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
557341c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
5574e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
557556e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
5576e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
5577e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
5578e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
5579e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
5580e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
5581e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
5582e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
5583e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
5584e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
5585e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
5586e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
5587e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
5588e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
5589e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
5590e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
5591e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
5592e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
55931707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
5594d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
5595d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
55967d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
5597b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
5598b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
5599e126ba97SEli Cohen 
5600e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
5601ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
5602e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
56033cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
56043cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
5605e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
5606e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
5607e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
5608e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
5609e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
5610e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
5611e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
5612e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
5613e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
5614e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
5615e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
5616e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
5617e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
5618e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
5619e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
5620e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
5621e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
5622e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
5623e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
5624e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
5625d0e84c0aSYishai Hadas 	dev->ib_dev.drain_sq		= mlx5_ib_drain_sq;
5626d0e84c0aSYishai Hadas 	dev->ib_dev.drain_rq		= mlx5_ib_drain_rq;
5627e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
5628e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
5629e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
5630e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
5631e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
5632e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
5633e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
5634e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
5635e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
5636e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
563756e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
5638e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
5639e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
5640e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
5641e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
56429bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
56438a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
5644d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
5645c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
564640b24403SSagi Grimberg 	dev->ib_dev.get_vector_affinity	= mlx5_ib_get_vector_affinity;
56478e959601SNiranjana Vishwanathapura 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
5648693dfd5aSErez Shitrit 		dev->ib_dev.alloc_rdma_netdev	= mlx5_ib_alloc_rdma_netdev;
56498e959601SNiranjana Vishwanathapura 
5650eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
5651eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
5652eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
5653eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
5654eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
5655eff901d3SEli Cohen 	}
5656e126ba97SEli Cohen 
56577c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
56587c2344c3SMaor Gottlieb 
56596e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
56606e8484c5SMax Gurtovoy 
5661d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
5662d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
5663d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
5664d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
5665d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
5666d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
5667d2370e0aSMatan Barak 	}
5668d2370e0aSMatan Barak 
5669938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
5670e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
5671e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
5672e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
5673e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
5674e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
5675e126ba97SEli Cohen 	}
5676e126ba97SEli Cohen 
567724da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
567824da0016SAriel Levkovich 		dev->ib_dev.alloc_dm = mlx5_ib_alloc_dm;
567924da0016SAriel Levkovich 		dev->ib_dev.dealloc_dm = mlx5_ib_dealloc_dm;
56806c29f57eSAriel Levkovich 		dev->ib_dev.reg_dm_mr = mlx5_ib_reg_dm_mr;
568124da0016SAriel Levkovich 	}
568224da0016SAriel Levkovich 
5683038d2ef8SMaor Gottlieb 	dev->ib_dev.create_flow	= mlx5_ib_create_flow;
5684038d2ef8SMaor Gottlieb 	dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
568581e30880SYishai Hadas 	dev->ib_dev.uverbs_ex_cmd_mask |=
568681e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
568781e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
5688c6475a0bSAviad Yehezkel 	dev->ib_dev.create_flow_action_esp = mlx5_ib_create_flow_action_esp;
5689c6475a0bSAviad Yehezkel 	dev->ib_dev.destroy_flow_action = mlx5_ib_destroy_flow_action;
5690349705c1SMatan Barak 	dev->ib_dev.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp;
56910ede73bcSMatan Barak 	dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
5692b29e2a13SRaed Salem 	dev->ib_dev.create_counters = mlx5_ib_create_counters;
5693b29e2a13SRaed Salem 	dev->ib_dev.destroy_counters = mlx5_ib_destroy_counters;
56941a1e03dcSRaed Salem 	dev->ib_dev.read_counters = mlx5_ib_read_counters;
569581e30880SYishai Hadas 
5696e126ba97SEli Cohen 	err = init_node_data(dev);
5697e126ba97SEli Cohen 	if (err)
569816c1975fSMark Bloch 		return err;
5699e126ba97SEli Cohen 
5700c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
5701e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
5702e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
5703c8b89924SMark Bloch 		mutex_init(&dev->lb_mutex);
5704c8b89924SMark Bloch 
570516c1975fSMark Bloch 	return 0;
570616c1975fSMark Bloch }
570716c1975fSMark Bloch 
57088e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
570916c1975fSMark Bloch {
57108e6efa3aSMark Bloch 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
57118e6efa3aSMark Bloch 	dev->ib_dev.query_port		= mlx5_ib_query_port;
57128e6efa3aSMark Bloch 
57138e6efa3aSMark Bloch 	return 0;
57148e6efa3aSMark Bloch }
57158e6efa3aSMark Bloch 
5716b5ca15adSMark Bloch int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
57178e6efa3aSMark Bloch {
57188e6efa3aSMark Bloch 	dev->ib_dev.get_port_immutable  = mlx5_port_rep_immutable;
57198e6efa3aSMark Bloch 	dev->ib_dev.query_port		= mlx5_ib_rep_query_port;
57208e6efa3aSMark Bloch 
57218e6efa3aSMark Bloch 	return 0;
57228e6efa3aSMark Bloch }
57238e6efa3aSMark Bloch 
5724e3f1ed1fSLeon Romanovsky static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
57258e6efa3aSMark Bloch {
5726e3f1ed1fSLeon Romanovsky 	u8 port_num;
57277fd8aefbSDaniel Jurgens 	int i;
572816c1975fSMark Bloch 
57297fd8aefbSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
57307fd8aefbSDaniel Jurgens 		dev->roce[i].dev = dev;
57317fd8aefbSDaniel Jurgens 		dev->roce[i].native_port_num = i + 1;
57327fd8aefbSDaniel Jurgens 		dev->roce[i].last_port_state = IB_PORT_DOWN;
57337fd8aefbSDaniel Jurgens 	}
57347fd8aefbSDaniel Jurgens 
5735c11a226aSMark Bloch 	dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
5736c11a226aSMark Bloch 	dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
5737c11a226aSMark Bloch 	dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
5738c11a226aSMark Bloch 	dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
5739c11a226aSMark Bloch 	dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
5740c11a226aSMark Bloch 	dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
57418e6efa3aSMark Bloch 
5742c11a226aSMark Bloch 	dev->ib_dev.uverbs_ex_cmd_mask |=
5743c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
5744c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
5745c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
5746c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
5747c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
57488e6efa3aSMark Bloch 
5749e3f1ed1fSLeon Romanovsky 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5750e3f1ed1fSLeon Romanovsky 
57518e6efa3aSMark Bloch 	return mlx5_add_netdev_notifier(dev, port_num);
57528e6efa3aSMark Bloch }
57538e6efa3aSMark Bloch 
57548e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
57558e6efa3aSMark Bloch {
57568e6efa3aSMark Bloch 	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
57578e6efa3aSMark Bloch 
57588e6efa3aSMark Bloch 	mlx5_remove_netdev_notifier(dev, port_num);
57598e6efa3aSMark Bloch }
57608e6efa3aSMark Bloch 
57618e6efa3aSMark Bloch int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
57628e6efa3aSMark Bloch {
57638e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
57648e6efa3aSMark Bloch 	enum rdma_link_layer ll;
57658e6efa3aSMark Bloch 	int port_type_cap;
57668e6efa3aSMark Bloch 	int err = 0;
57678e6efa3aSMark Bloch 
57688e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
57698e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
57708e6efa3aSMark Bloch 
57718e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET)
5772e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
57738e6efa3aSMark Bloch 
577416c1975fSMark Bloch 	return err;
5775fc24fc5eSAchiad Shochat }
5776fc24fc5eSAchiad Shochat 
57778e6efa3aSMark Bloch void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
57788e6efa3aSMark Bloch {
57798e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
57808e6efa3aSMark Bloch }
57818e6efa3aSMark Bloch 
57828e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
57838e6efa3aSMark Bloch {
57848e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
57858e6efa3aSMark Bloch 	enum rdma_link_layer ll;
57868e6efa3aSMark Bloch 	int port_type_cap;
57878e6efa3aSMark Bloch 	int err;
57888e6efa3aSMark Bloch 
57898e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
57908e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
57918e6efa3aSMark Bloch 
57928e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
5793e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
57948e6efa3aSMark Bloch 		if (err)
57958e6efa3aSMark Bloch 			return err;
57968e6efa3aSMark Bloch 
5797e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
57988e6efa3aSMark Bloch 		if (err)
57998e6efa3aSMark Bloch 			goto cleanup;
58008e6efa3aSMark Bloch 	}
58018e6efa3aSMark Bloch 
580216c1975fSMark Bloch 	return 0;
58038e6efa3aSMark Bloch cleanup:
58048e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
58058e6efa3aSMark Bloch 
58068e6efa3aSMark Bloch 	return err;
580745bded2cSKamal Heib }
58086aec21f6SHaggai Eran 
580916c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
581016c1975fSMark Bloch {
581116c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
581216c1975fSMark Bloch 	enum rdma_link_layer ll;
581316c1975fSMark Bloch 	int port_type_cap;
58144a2da0b8SParav Pandit 
581516c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
581616c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
581716c1975fSMark Bloch 
581816c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
581916c1975fSMark Bloch 		mlx5_disable_eth(dev);
58208e6efa3aSMark Bloch 		mlx5_ib_stage_common_roce_cleanup(dev);
582116c1975fSMark Bloch 	}
582216c1975fSMark Bloch }
582316c1975fSMark Bloch 
5824b5ca15adSMark Bloch int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
582516c1975fSMark Bloch {
582616c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
582716c1975fSMark Bloch }
582816c1975fSMark Bloch 
5829b5ca15adSMark Bloch void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
583016c1975fSMark Bloch {
583116c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
583216c1975fSMark Bloch }
583316c1975fSMark Bloch 
583416c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
583516c1975fSMark Bloch {
583607321b3cSMark Bloch 	mlx5_ib_internal_fill_odp_caps(dev);
583707321b3cSMark Bloch 
583816c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
583916c1975fSMark Bloch }
584016c1975fSMark Bloch 
5841b5ca15adSMark Bloch int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
584216c1975fSMark Bloch {
58435e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
58445e1e7612SMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
58455e1e7612SMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
58465e1e7612SMark Bloch 
584716c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
58485e1e7612SMark Bloch 	}
584916c1975fSMark Bloch 
585016c1975fSMark Bloch 	return 0;
585116c1975fSMark Bloch }
585216c1975fSMark Bloch 
5853b5ca15adSMark Bloch void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
585416c1975fSMark Bloch {
585516c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
585616c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
585716c1975fSMark Bloch }
585816c1975fSMark Bloch 
585916c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
586016c1975fSMark Bloch {
5861a9e546e7SParav Pandit 	return mlx5_ib_init_cong_debugfs(dev,
5862a9e546e7SParav Pandit 					 mlx5_core_native_port_num(dev->mdev) - 1);
586316c1975fSMark Bloch }
586416c1975fSMark Bloch 
586516c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
586616c1975fSMark Bloch {
5867a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
5868a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
586916c1975fSMark Bloch }
587016c1975fSMark Bloch 
587116c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
587216c1975fSMark Bloch {
58735fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
5874444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
587516c1975fSMark Bloch }
587616c1975fSMark Bloch 
587716c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
587816c1975fSMark Bloch {
587916c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
588016c1975fSMark Bloch }
588116c1975fSMark Bloch 
5882b5ca15adSMark Bloch int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
588316c1975fSMark Bloch {
588416c1975fSMark Bloch 	int err;
58855fe9dec0SEli Cohen 
58865fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
58875fe9dec0SEli Cohen 	if (err)
588816c1975fSMark Bloch 		return err;
58895fe9dec0SEli Cohen 
58905fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
58915fe9dec0SEli Cohen 	if (err)
589216c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
58935fe9dec0SEli Cohen 
589416c1975fSMark Bloch 	return err;
589516c1975fSMark Bloch }
58960837e86aSMark Bloch 
5897b5ca15adSMark Bloch void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
589816c1975fSMark Bloch {
589916c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
590016c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
590116c1975fSMark Bloch }
5902e126ba97SEli Cohen 
59038c84660bSMatan Barak static int mlx5_ib_stage_populate_specs(struct mlx5_ib_dev *dev)
59048c84660bSMatan Barak {
59058c84660bSMatan Barak 	return populate_specs_root(dev);
59068c84660bSMatan Barak }
59078c84660bSMatan Barak 
5908b5ca15adSMark Bloch int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
590916c1975fSMark Bloch {
591016c1975fSMark Bloch 	return ib_register_device(&dev->ib_dev, NULL);
591116c1975fSMark Bloch }
591216c1975fSMark Bloch 
59138c84660bSMatan Barak static void mlx5_ib_stage_depopulate_specs(struct mlx5_ib_dev *dev)
59148c84660bSMatan Barak {
59158c84660bSMatan Barak 	depopulate_specs_root(dev);
59168c84660bSMatan Barak }
59178c84660bSMatan Barak 
591803fe2debSDavid S. Miller void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
591942cea83fSMark Bloch {
592042cea83fSMark Bloch 	destroy_umrc_res(dev);
592142cea83fSMark Bloch }
592242cea83fSMark Bloch 
5923b5ca15adSMark Bloch void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
592416c1975fSMark Bloch {
592516c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
592616c1975fSMark Bloch }
592716c1975fSMark Bloch 
592803fe2debSDavid S. Miller int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
592916c1975fSMark Bloch {
593016c1975fSMark Bloch 	return create_umr_res(dev);
593116c1975fSMark Bloch }
593216c1975fSMark Bloch 
593316c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
593416c1975fSMark Bloch {
593503404e8aSMaor Gottlieb 	init_delay_drop(dev);
593603404e8aSMaor Gottlieb 
593716c1975fSMark Bloch 	return 0;
593816c1975fSMark Bloch }
593916c1975fSMark Bloch 
594016c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
594116c1975fSMark Bloch {
594216c1975fSMark Bloch 	cancel_delay_drop(dev);
594316c1975fSMark Bloch }
594416c1975fSMark Bloch 
5945b5ca15adSMark Bloch int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev)
594616c1975fSMark Bloch {
594716c1975fSMark Bloch 	int err;
594816c1975fSMark Bloch 	int i;
594916c1975fSMark Bloch 
5950e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
5951281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
5952281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
5953281d1a92SWei Yongjun 		if (err)
595416c1975fSMark Bloch 			return err;
5955e126ba97SEli Cohen 	}
5956e126ba97SEli Cohen 
595716c1975fSMark Bloch 	return 0;
595816c1975fSMark Bloch }
595916c1975fSMark Bloch 
5960fc385b7aSMark Bloch static int mlx5_ib_stage_rep_reg_init(struct mlx5_ib_dev *dev)
5961fc385b7aSMark Bloch {
5962fc385b7aSMark Bloch 	mlx5_ib_register_vport_reps(dev);
5963fc385b7aSMark Bloch 
5964fc385b7aSMark Bloch 	return 0;
5965fc385b7aSMark Bloch }
5966fc385b7aSMark Bloch 
5967fc385b7aSMark Bloch static void mlx5_ib_stage_rep_reg_cleanup(struct mlx5_ib_dev *dev)
5968fc385b7aSMark Bloch {
5969fc385b7aSMark Bloch 	mlx5_ib_unregister_vport_reps(dev);
5970fc385b7aSMark Bloch }
5971fc385b7aSMark Bloch 
5972b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
597316c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
597416c1975fSMark Bloch 		      int stage)
597516c1975fSMark Bloch {
597616c1975fSMark Bloch 	/* Number of stages to cleanup */
597716c1975fSMark Bloch 	while (stage) {
597816c1975fSMark Bloch 		stage--;
597916c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
598016c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
598116c1975fSMark Bloch 	}
598216c1975fSMark Bloch 
598316c1975fSMark Bloch 	ib_dealloc_device((struct ib_device *)dev);
598416c1975fSMark Bloch }
598516c1975fSMark Bloch 
5986b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
598716c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
598816c1975fSMark Bloch {
598916c1975fSMark Bloch 	int err;
599016c1975fSMark Bloch 	int i;
599116c1975fSMark Bloch 
599216c1975fSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
599316c1975fSMark Bloch 
599416c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
599516c1975fSMark Bloch 		if (profile->stage[i].init) {
599616c1975fSMark Bloch 			err = profile->stage[i].init(dev);
599716c1975fSMark Bloch 			if (err)
599816c1975fSMark Bloch 				goto err_out;
599916c1975fSMark Bloch 		}
600016c1975fSMark Bloch 	}
600116c1975fSMark Bloch 
600216c1975fSMark Bloch 	dev->profile = profile;
6003e126ba97SEli Cohen 	dev->ib_active = true;
6004e126ba97SEli Cohen 
60059603b61dSJack Morgenstein 	return dev;
6006e126ba97SEli Cohen 
600716c1975fSMark Bloch err_out:
600816c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
6009e126ba97SEli Cohen 
60109603b61dSJack Morgenstein 	return NULL;
6011e126ba97SEli Cohen }
6012e126ba97SEli Cohen 
601316c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
601416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
601516c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
601616c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
60179a4ca38dSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
60189a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_init,
60199a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
602016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
602116c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
602216c1975fSMark Bloch 		     NULL),
60238e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
60248e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
60258e6efa3aSMark Bloch 		     NULL),
602616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
602716c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
602816c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
602916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
603016c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
603116c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
603216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
603316c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
60343cc297dbSMark Bloch 		     NULL),
603516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
603616c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
603716c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
603816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
603916c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
604016c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
604116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
604216c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
604316c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
604416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
604516c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
604616c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
604742cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
604842cea83fSMark Bloch 		     NULL,
604942cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
60508c84660bSMatan Barak 	STAGE_CREATE(MLX5_IB_STAGE_SPECS,
60518c84660bSMatan Barak 		     mlx5_ib_stage_populate_specs,
60528c84660bSMatan Barak 		     mlx5_ib_stage_depopulate_specs),
605316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
605416c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
605516c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
605642cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
605742cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
605842cea83fSMark Bloch 		     NULL),
605916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
606016c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
606116c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
606216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
606316c1975fSMark Bloch 		     mlx5_ib_stage_class_attr_init,
606416c1975fSMark Bloch 		     NULL),
606516c1975fSMark Bloch };
606616c1975fSMark Bloch 
6067b5ca15adSMark Bloch static const struct mlx5_ib_profile nic_rep_profile = {
6068b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
6069b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
6070b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
6071b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6072b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_init,
6073b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
6074b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6075b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
6076b5ca15adSMark Bloch 		     NULL),
6077b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6078b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_non_default_cb,
6079b5ca15adSMark Bloch 		     NULL),
6080b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6081b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_init,
6082b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_cleanup),
6083b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6084b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_init,
6085b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
6086b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6087b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_init,
6088b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
6089b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
6090b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
6091b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
6092b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6093b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
6094b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
609503fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
609603fe2debSDavid S. Miller 		     NULL,
609703fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
60988c84660bSMatan Barak 	STAGE_CREATE(MLX5_IB_STAGE_SPECS,
60998c84660bSMatan Barak 		     mlx5_ib_stage_populate_specs,
61008c84660bSMatan Barak 		     mlx5_ib_stage_depopulate_specs),
6101b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6102b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
6103b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
610403fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
610503fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
610603fe2debSDavid S. Miller 		     NULL),
6107b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
6108b5ca15adSMark Bloch 		     mlx5_ib_stage_class_attr_init,
6109b5ca15adSMark Bloch 		     NULL),
6110b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_REP_REG,
6111b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_reg_init,
6112b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_reg_cleanup),
6113b5ca15adSMark Bloch };
6114b5ca15adSMark Bloch 
6115e3f1ed1fSLeon Romanovsky static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
611632f69e4bSDaniel Jurgens {
611732f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
611832f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
611932f69e4bSDaniel Jurgens 	bool bound = false;
612032f69e4bSDaniel Jurgens 	int err;
612132f69e4bSDaniel Jurgens 
612232f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
612332f69e4bSDaniel Jurgens 	if (!mpi)
612432f69e4bSDaniel Jurgens 		return NULL;
612532f69e4bSDaniel Jurgens 
612632f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
612732f69e4bSDaniel Jurgens 
612832f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
612932f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
613032f69e4bSDaniel Jurgens 	if (err) {
613132f69e4bSDaniel Jurgens 		kfree(mpi);
613232f69e4bSDaniel Jurgens 		return NULL;
613332f69e4bSDaniel Jurgens 	}
613432f69e4bSDaniel Jurgens 
613532f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
613632f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
613732f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
613832f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
613932f69e4bSDaniel Jurgens 
614032f69e4bSDaniel Jurgens 		if (bound) {
614132f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
614232f69e4bSDaniel Jurgens 			break;
614332f69e4bSDaniel Jurgens 		}
614432f69e4bSDaniel Jurgens 	}
614532f69e4bSDaniel Jurgens 
614632f69e4bSDaniel Jurgens 	if (!bound) {
614732f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
614832f69e4bSDaniel Jurgens 		dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
614932f69e4bSDaniel Jurgens 	}
615032f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
615132f69e4bSDaniel Jurgens 
615232f69e4bSDaniel Jurgens 	return mpi;
615332f69e4bSDaniel Jurgens }
615432f69e4bSDaniel Jurgens 
615516c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
615616c1975fSMark Bloch {
615732f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
6158b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
615932f69e4bSDaniel Jurgens 	int port_type_cap;
616032f69e4bSDaniel Jurgens 
6161b5ca15adSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
6162b5ca15adSMark Bloch 
616332f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
616432f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
616532f69e4bSDaniel Jurgens 
6166e3f1ed1fSLeon Romanovsky 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6167e3f1ed1fSLeon Romanovsky 		return mlx5_ib_add_slave_port(mdev);
616832f69e4bSDaniel Jurgens 
6169b5ca15adSMark Bloch 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
6170b5ca15adSMark Bloch 	if (!dev)
6171b5ca15adSMark Bloch 		return NULL;
6172b5ca15adSMark Bloch 
6173b5ca15adSMark Bloch 	dev->mdev = mdev;
6174b5ca15adSMark Bloch 	dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6175b5ca15adSMark Bloch 			     MLX5_CAP_GEN(mdev, num_vhca_ports));
6176b5ca15adSMark Bloch 
6177b5ca15adSMark Bloch 	if (MLX5_VPORT_MANAGER(mdev) &&
6178b5ca15adSMark Bloch 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
6179b5ca15adSMark Bloch 		dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0);
6180b5ca15adSMark Bloch 
6181b5ca15adSMark Bloch 		return __mlx5_ib_add(dev, &nic_rep_profile);
6182b5ca15adSMark Bloch 	}
6183b5ca15adSMark Bloch 
6184b5ca15adSMark Bloch 	return __mlx5_ib_add(dev, &pf_profile);
618516c1975fSMark Bloch }
618616c1975fSMark Bloch 
61879603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
6188e126ba97SEli Cohen {
618932f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
619032f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
61916aec21f6SHaggai Eran 
619232f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
619332f69e4bSDaniel Jurgens 		mpi = context;
619432f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
619532f69e4bSDaniel Jurgens 		if (mpi->ibdev)
619632f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
619732f69e4bSDaniel Jurgens 		list_del(&mpi->list);
619832f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
619932f69e4bSDaniel Jurgens 		return;
620032f69e4bSDaniel Jurgens 	}
620132f69e4bSDaniel Jurgens 
620232f69e4bSDaniel Jurgens 	dev = context;
620316c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
6204e126ba97SEli Cohen }
6205e126ba97SEli Cohen 
62069603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
62079603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
62089603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
62099603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
6210d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
6211d9aaed83SArtemy Kovalyov 	.pfault		= mlx5_ib_pfault,
6212d9aaed83SArtemy Kovalyov #endif
621364613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
6214e126ba97SEli Cohen };
6215e126ba97SEli Cohen 
6216c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
6217c44ef998SIlya Lesokhin {
6218c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
6219c44ef998SIlya Lesokhin 	return xlt_emergency_page;
6220c44ef998SIlya Lesokhin }
6221c44ef998SIlya Lesokhin 
6222c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
6223c44ef998SIlya Lesokhin {
6224c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
6225c44ef998SIlya Lesokhin }
6226c44ef998SIlya Lesokhin 
6227e126ba97SEli Cohen static int __init mlx5_ib_init(void)
6228e126ba97SEli Cohen {
62296aec21f6SHaggai Eran 	int err;
62306aec21f6SHaggai Eran 
6231c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
6232c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
6233d69a24e0SDaniel Jurgens 		return -ENOMEM;
6234d69a24e0SDaniel Jurgens 
6235c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
6236c44ef998SIlya Lesokhin 
6237c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6238c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
6239c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
6240c44ef998SIlya Lesokhin 		return -ENOMEM;
6241c44ef998SIlya Lesokhin 	}
6242c44ef998SIlya Lesokhin 
624381713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
62449603b61dSJack Morgenstein 
62456aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
62466aec21f6SHaggai Eran 
62476aec21f6SHaggai Eran 	return err;
6248e126ba97SEli Cohen }
6249e126ba97SEli Cohen 
6250e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
6251e126ba97SEli Cohen {
62529603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
6253d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
6254c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
6255c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
6256e126ba97SEli Cohen }
6257e126ba97SEli Cohen 
6258e126ba97SEli Cohen module_init(mlx5_ib_init);
6259e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
6260