xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision c16339b69c0d58f456154b43fc396b1f1c3dc055)
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>
55cecae747SMaor Gottlieb #include <linux/mlx5/eswitch.h>
567c2344c3SMaor Gottlieb #include <linux/list.h>
57e126ba97SEli Cohen #include <rdma/ib_smi.h>
58e126ba97SEli Cohen #include <rdma/ib_umem.h>
59038d2ef8SMaor Gottlieb #include <linux/in.h>
60038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
61e126ba97SEli Cohen #include "mlx5_ib.h"
62fc385b7aSMark Bloch #include "ib_rep.h"
63e1f24a79SParav Pandit #include "cmd.h"
64f3da6577SLeon Romanovsky #include "srq.h"
653346c487SBoris Pismenny #include <linux/mlx5/fs_helpers.h>
66c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h>
678c84660bSMatan Barak #include <rdma/uverbs_std_types.h>
68c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h>
69c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h>
704061ff7aSErez Alfasi #include <rdma/ib_umem_odp.h>
718c84660bSMatan Barak 
728c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
738c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
74e126ba97SEli Cohen 
75e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
76b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0"
77e126ba97SEli Cohen 
78e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
79e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
80e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
81e126ba97SEli Cohen 
82e126ba97SEli Cohen static char mlx5_version[] =
83e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
84b359911dSTariq Toukan 	DRIVER_VERSION "\n";
85e126ba97SEli Cohen 
86d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
87d69a24e0SDaniel Jurgens 	struct work_struct	work;
88df097a27SSaeed Mahameed 	union {
89df097a27SSaeed Mahameed 		struct mlx5_ib_dev	      *dev;
90df097a27SSaeed Mahameed 		struct mlx5_ib_multiport_info *mpi;
91df097a27SSaeed Mahameed 	};
92df097a27SSaeed Mahameed 	bool			is_slave;
93134e9349SSaeed Mahameed 	unsigned int		event;
94df097a27SSaeed Mahameed 	void			*param;
95d69a24e0SDaniel Jurgens };
96d69a24e0SDaniel Jurgens 
97da7525d2SEran Ben Elisha enum {
98da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
99da7525d2SEran Ben Elisha };
1001b5daf11SMajd Dibbiny 
101d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
10232f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
10332f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
10432f69e4bSDaniel Jurgens /*
10532f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
10632f69e4bSDaniel Jurgens  */
10732f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
10832f69e4bSDaniel Jurgens 
109c44ef998SIlya Lesokhin /* We can't use an array for xlt_emergency_page because dma_map_single
110c44ef998SIlya Lesokhin  * doesn't work on kernel modules memory
111c44ef998SIlya Lesokhin  */
112c44ef998SIlya Lesokhin static unsigned long xlt_emergency_page;
113c44ef998SIlya Lesokhin static struct mutex xlt_emergency_page_mutex;
114c44ef998SIlya Lesokhin 
11532f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
11632f69e4bSDaniel Jurgens {
11732f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
11832f69e4bSDaniel Jurgens 
11932f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
12032f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
12132f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
12232f69e4bSDaniel Jurgens 	return dev;
12332f69e4bSDaniel Jurgens }
12432f69e4bSDaniel Jurgens 
1251b5daf11SMajd Dibbiny static enum rdma_link_layer
126ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
1271b5daf11SMajd Dibbiny {
128ebd61f68SAchiad Shochat 	switch (port_type_cap) {
1291b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
1301b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
1311b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
1321b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
1331b5daf11SMajd Dibbiny 	default:
1341b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1351b5daf11SMajd Dibbiny 	}
1361b5daf11SMajd Dibbiny }
1371b5daf11SMajd Dibbiny 
138ebd61f68SAchiad Shochat static enum rdma_link_layer
139ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
140ebd61f68SAchiad Shochat {
141ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
142ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
143ebd61f68SAchiad Shochat 
144ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
145ebd61f68SAchiad Shochat }
146ebd61f68SAchiad Shochat 
147fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
148fd65f1b8SMoni Shoua 			  u8 port_num,
149fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
150fd65f1b8SMoni Shoua {
151fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
152fd65f1b8SMoni Shoua 	int ret;
153fd65f1b8SMoni Shoua 
154fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1553023a1e9SKamal Heib 	ret = ibdev->ops.query_port(ibdev, port_num, &attr);
156fd65f1b8SMoni Shoua 	if (!ret)
157fd65f1b8SMoni Shoua 		*state = attr.state;
158fd65f1b8SMoni Shoua 	return ret;
159fd65f1b8SMoni Shoua }
160fd65f1b8SMoni Shoua 
16135b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
16235b0aa67SMark Bloch 					   struct net_device *ndev,
16335b0aa67SMark Bloch 					   u8 *port_num)
16435b0aa67SMark Bloch {
16535b0aa67SMark Bloch 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
16635b0aa67SMark Bloch 	struct net_device *rep_ndev;
16735b0aa67SMark Bloch 	struct mlx5_ib_port *port;
16835b0aa67SMark Bloch 	int i;
16935b0aa67SMark Bloch 
17035b0aa67SMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
17135b0aa67SMark Bloch 		port  = &dev->port[i];
17235b0aa67SMark Bloch 		if (!port->rep)
17335b0aa67SMark Bloch 			continue;
17435b0aa67SMark Bloch 
17535b0aa67SMark Bloch 		read_lock(&port->roce.netdev_lock);
17635b0aa67SMark Bloch 		rep_ndev = mlx5_ib_get_rep_netdev(esw,
17735b0aa67SMark Bloch 						  port->rep->vport);
17835b0aa67SMark Bloch 		if (rep_ndev == ndev) {
17935b0aa67SMark Bloch 			read_unlock(&port->roce.netdev_lock);
18035b0aa67SMark Bloch 			*port_num = i + 1;
18135b0aa67SMark Bloch 			return &port->roce;
18235b0aa67SMark Bloch 		}
18335b0aa67SMark Bloch 		read_unlock(&port->roce.netdev_lock);
18435b0aa67SMark Bloch 	}
18535b0aa67SMark Bloch 
18635b0aa67SMark Bloch 	return NULL;
18735b0aa67SMark Bloch }
18835b0aa67SMark Bloch 
189fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
190fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
191fc24fc5eSAchiad Shochat {
1927fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
193fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1947fd8aefbSDaniel Jurgens 	u8 port_num = roce->native_port_num;
1957fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1967fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1977fd8aefbSDaniel Jurgens 
1987fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
19932f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
20032f69e4bSDaniel Jurgens 	if (!mdev)
20132f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
202fc24fc5eSAchiad Shochat 
2035ec8c83eSAviv Heller 	switch (event) {
2045ec8c83eSAviv Heller 	case NETDEV_REGISTER:
20535b0aa67SMark Bloch 		/* Should already be registered during the load */
20635b0aa67SMark Bloch 		if (ibdev->is_rep)
20735b0aa67SMark Bloch 			break;
2087fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
209dce45af5SLinus Torvalds 		if (ndev->dev.parent == mdev->device)
210842a9c83SOr Gerlitz 			roce->netdev = ndev;
2117fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
2125ec8c83eSAviv Heller 		break;
2135ec8c83eSAviv Heller 
214842a9c83SOr Gerlitz 	case NETDEV_UNREGISTER:
21535b0aa67SMark Bloch 		/* In case of reps, ib device goes away before the netdevs */
216842a9c83SOr Gerlitz 		write_lock(&roce->netdev_lock);
217842a9c83SOr Gerlitz 		if (roce->netdev == ndev)
218842a9c83SOr Gerlitz 			roce->netdev = NULL;
219842a9c83SOr Gerlitz 		write_unlock(&roce->netdev_lock);
220842a9c83SOr Gerlitz 		break;
221842a9c83SOr Gerlitz 
222fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
2235ec8c83eSAviv Heller 	case NETDEV_UP:
22488621dfeSAviv Heller 	case NETDEV_DOWN: {
2257fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
22688621dfeSAviv Heller 		struct net_device *upper = NULL;
22788621dfeSAviv Heller 
22888621dfeSAviv Heller 		if (lag_ndev) {
22988621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
23088621dfeSAviv Heller 			dev_put(lag_ndev);
23188621dfeSAviv Heller 		}
23288621dfeSAviv Heller 
23335b0aa67SMark Bloch 		if (ibdev->is_rep)
23435b0aa67SMark Bloch 			roce = mlx5_get_rep_roce(ibdev, ndev, &port_num);
23535b0aa67SMark Bloch 		if (!roce)
23635b0aa67SMark Bloch 			return NOTIFY_DONE;
2377fd8aefbSDaniel Jurgens 		if ((upper == ndev || (!upper && ndev == roce->netdev))
23888621dfeSAviv Heller 		    && ibdev->ib_active) {
239626bc02dSBart Van Assche 			struct ib_event ibev = { };
240fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2415ec8c83eSAviv Heller 
2427fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2437fd8aefbSDaniel Jurgens 					   &port_state))
2447fd8aefbSDaniel Jurgens 				goto done;
245fd65f1b8SMoni Shoua 
2467fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2477fd8aefbSDaniel Jurgens 				goto done;
248fd65f1b8SMoni Shoua 
2497fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2505ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
251fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
252fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
253fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
254fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
255fd65f1b8SMoni Shoua 			else
2567fd8aefbSDaniel Jurgens 				goto done;
257fd65f1b8SMoni Shoua 
2587fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2595ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2605ec8c83eSAviv Heller 		}
2615ec8c83eSAviv Heller 		break;
26288621dfeSAviv Heller 	}
2635ec8c83eSAviv Heller 
2645ec8c83eSAviv Heller 	default:
2655ec8c83eSAviv Heller 		break;
2665ec8c83eSAviv Heller 	}
2677fd8aefbSDaniel Jurgens done:
26832f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
269fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
270fc24fc5eSAchiad Shochat }
271fc24fc5eSAchiad Shochat 
272fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
273fc24fc5eSAchiad Shochat 					     u8 port_num)
274fc24fc5eSAchiad Shochat {
275fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
276fc24fc5eSAchiad Shochat 	struct net_device *ndev;
27732f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
278fc24fc5eSAchiad Shochat 
27932f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
28032f69e4bSDaniel Jurgens 	if (!mdev)
28132f69e4bSDaniel Jurgens 		return NULL;
28232f69e4bSDaniel Jurgens 
28332f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
28488621dfeSAviv Heller 	if (ndev)
28532f69e4bSDaniel Jurgens 		goto out;
28688621dfeSAviv Heller 
287fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
288fc24fc5eSAchiad Shochat 	 */
28995579e78SMark Bloch 	read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
29095579e78SMark Bloch 	ndev = ibdev->port[port_num - 1].roce.netdev;
291fc24fc5eSAchiad Shochat 	if (ndev)
292fc24fc5eSAchiad Shochat 		dev_hold(ndev);
29395579e78SMark Bloch 	read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
294fc24fc5eSAchiad Shochat 
29532f69e4bSDaniel Jurgens out:
29632f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
297fc24fc5eSAchiad Shochat 	return ndev;
298fc24fc5eSAchiad Shochat }
299fc24fc5eSAchiad Shochat 
30032f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
30132f69e4bSDaniel Jurgens 						   u8 ib_port_num,
30232f69e4bSDaniel Jurgens 						   u8 *native_port_num)
30332f69e4bSDaniel Jurgens {
30432f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
30532f69e4bSDaniel Jurgens 							  ib_port_num);
30632f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
30732f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
30832f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
30932f69e4bSDaniel Jurgens 
310210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
311210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
312210b1f78SMark Bloch 		if (native_port_num)
313210b1f78SMark Bloch 			*native_port_num = ib_port_num;
314210b1f78SMark Bloch 		return ibdev->mdev;
315210b1f78SMark Bloch 	}
316210b1f78SMark Bloch 
31732f69e4bSDaniel Jurgens 	if (native_port_num)
31832f69e4bSDaniel Jurgens 		*native_port_num = 1;
31932f69e4bSDaniel Jurgens 
32032f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
32132f69e4bSDaniel Jurgens 	if (!port)
32232f69e4bSDaniel Jurgens 		return NULL;
32332f69e4bSDaniel Jurgens 
32432f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
32532f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
32632f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
32732f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
32832f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
32932f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
33032f69e4bSDaniel Jurgens 		 */
33132f69e4bSDaniel Jurgens 		if (!mpi->is_master)
33232f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
33332f69e4bSDaniel Jurgens 	}
33432f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
33532f69e4bSDaniel Jurgens 
33632f69e4bSDaniel Jurgens 	return mdev;
33732f69e4bSDaniel Jurgens }
33832f69e4bSDaniel Jurgens 
33932f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
34032f69e4bSDaniel Jurgens {
34132f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
34232f69e4bSDaniel Jurgens 							  port_num);
34332f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
34432f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
34532f69e4bSDaniel Jurgens 
34632f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
34732f69e4bSDaniel Jurgens 		return;
34832f69e4bSDaniel Jurgens 
34932f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
35032f69e4bSDaniel Jurgens 
35132f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
35232f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
35332f69e4bSDaniel Jurgens 	if (mpi->is_master)
35432f69e4bSDaniel Jurgens 		goto out;
35532f69e4bSDaniel Jurgens 
35632f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
35732f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
35832f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
35932f69e4bSDaniel Jurgens out:
36032f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
36132f69e4bSDaniel Jurgens }
36232f69e4bSDaniel Jurgens 
36308e8676fSAya Levin static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, u8 *active_speed,
364f1b65df5SNoa Osherovich 					   u8 *active_width)
365f1b65df5SNoa Osherovich {
366f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
367f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
368f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
369f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
370f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
371f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
372f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
373f1b65df5SNoa Osherovich 		break;
374f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
375f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
376f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
377f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
378f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
379f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
380f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
381f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
382f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
383f1b65df5SNoa Osherovich 		break;
384f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
385f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
386f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
387f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
388f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
389f1b65df5SNoa Osherovich 		break;
390f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
391f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
392f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
393f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
394f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
395f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
396f1b65df5SNoa Osherovich 		break;
397f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
398f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
399f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
400f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
401f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
402f1b65df5SNoa Osherovich 		break;
403f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
404f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
405f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
406f1b65df5SNoa Osherovich 		break;
407f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
408f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
409f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
410f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
411f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
412f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
413f1b65df5SNoa Osherovich 		break;
414f1b65df5SNoa Osherovich 	default:
415f1b65df5SNoa Osherovich 		return -EINVAL;
416f1b65df5SNoa Osherovich 	}
417f1b65df5SNoa Osherovich 
418f1b65df5SNoa Osherovich 	return 0;
419f1b65df5SNoa Osherovich }
420f1b65df5SNoa Osherovich 
42108e8676fSAya Levin static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed,
42208e8676fSAya Levin 					u8 *active_width)
42308e8676fSAya Levin {
42408e8676fSAya Levin 	switch (eth_proto_oper) {
42508e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
42608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
42708e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
42808e8676fSAya Levin 		*active_speed = IB_SPEED_SDR;
42908e8676fSAya Levin 		break;
43008e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
43108e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
43208e8676fSAya Levin 		*active_speed = IB_SPEED_DDR;
43308e8676fSAya Levin 		break;
43408e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
43508e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
43608e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
43708e8676fSAya Levin 		break;
43808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
43908e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
44008e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
44108e8676fSAya Levin 		break;
44208e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
44308e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
44408e8676fSAya Levin 		*active_speed = IB_SPEED_EDR;
44508e8676fSAya Levin 		break;
44608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
447cd272875SAya Levin 		*active_width = IB_WIDTH_2X;
448cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
449cd272875SAya Levin 		break;
45008e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
45108e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
45208e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
45308e8676fSAya Levin 		break;
454cd272875SAya Levin 	case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
455cd272875SAya Levin 		*active_width = IB_WIDTH_4X;
456cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
457cd272875SAya Levin 		break;
45808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
45908e8676fSAya Levin 		*active_width = IB_WIDTH_2X;
46008e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
46108e8676fSAya Levin 		break;
46208e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
46308e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
46408e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
46508e8676fSAya Levin 		break;
46608e8676fSAya Levin 	default:
46708e8676fSAya Levin 		return -EINVAL;
46808e8676fSAya Levin 	}
46908e8676fSAya Levin 
47008e8676fSAya Levin 	return 0;
47108e8676fSAya Levin }
47208e8676fSAya Levin 
47308e8676fSAya Levin static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
47408e8676fSAya Levin 				    u8 *active_width, bool ext)
47508e8676fSAya Levin {
47608e8676fSAya Levin 	return ext ?
47708e8676fSAya Levin 		translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
47808e8676fSAya Levin 					     active_width) :
47908e8676fSAya Levin 		translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
48008e8676fSAya Levin 						active_width);
48108e8676fSAya Levin }
48208e8676fSAya Levin 
483095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
4843f89a643SAchiad Shochat 				struct ib_port_attr *props)
4853f89a643SAchiad Shochat {
4863f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
487bc4e12ffSAya Levin 	u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
488da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
48988621dfeSAviv Heller 	struct net_device *ndev, *upper;
4903f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
491b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
492c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
493f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
494b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
49508e8676fSAya Levin 	bool ext;
496095b0927SIlan Tayari 	int err;
4973f89a643SAchiad Shochat 
498b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
499b3cbd6f0SDaniel Jurgens 	if (!mdev) {
500b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
501b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
502b3cbd6f0SDaniel Jurgens 		 */
503b3cbd6f0SDaniel Jurgens 		put_mdev = false;
504b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
505b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
506b3cbd6f0SDaniel Jurgens 		port_num = 1;
507b3cbd6f0SDaniel Jurgens 	}
508b3cbd6f0SDaniel Jurgens 
509f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
510f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
51126628e2dSMark Bloch 	 * Use native port in case of reps
51250f22fd8SNoa Osherovich 	 */
51326628e2dSMark Bloch 	if (dev->is_rep)
51426628e2dSMark Bloch 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
51526628e2dSMark Bloch 					   1);
51626628e2dSMark Bloch 	else
517bc4e12ffSAya Levin 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
518b3cbd6f0SDaniel Jurgens 					   mdev_port_num);
519095b0927SIlan Tayari 	if (err)
520b3cbd6f0SDaniel Jurgens 		goto out;
52108e8676fSAya Levin 	ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet);
52208e8676fSAya Levin 	eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
523f1b65df5SNoa Osherovich 
5247672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
5257672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
5267672ed33SHonggang Li 
527f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
52808e8676fSAya Levin 				 &props->active_width, ext);
5293f89a643SAchiad Shochat 
5303f89a643SAchiad Shochat 	props->port_cap_flags |= IB_PORT_CM_SUP;
5312f944c0fSJason Gunthorpe 	props->ip_gids = true;
5323f89a643SAchiad Shochat 
5333f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
5343f89a643SAchiad Shochat 						roce_address_table_size);
5353f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
5363f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
5373f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
5383f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
53972a7720fSKamal Heib 	props->phys_state       = IB_PORT_PHYS_STATE_DISABLED;
5403f89a643SAchiad Shochat 
541b3cbd6f0SDaniel Jurgens 	mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
542c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
5433f89a643SAchiad Shochat 
544b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
545b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
546b3cbd6f0SDaniel Jurgens 		goto out;
547b3cbd6f0SDaniel Jurgens 
5483f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
5493f89a643SAchiad Shochat 	if (!ndev)
550b3cbd6f0SDaniel Jurgens 		goto out;
5513f89a643SAchiad Shochat 
5527c34ec19SAviv Heller 	if (dev->lag_active) {
55388621dfeSAviv Heller 		rcu_read_lock();
55488621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
55588621dfeSAviv Heller 		if (upper) {
55688621dfeSAviv Heller 			dev_put(ndev);
55788621dfeSAviv Heller 			ndev = upper;
55888621dfeSAviv Heller 			dev_hold(ndev);
55988621dfeSAviv Heller 		}
56088621dfeSAviv Heller 		rcu_read_unlock();
56188621dfeSAviv Heller 	}
56288621dfeSAviv Heller 
5633f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
5643f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
56572a7720fSKamal Heib 		props->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
5663f89a643SAchiad Shochat 	}
5673f89a643SAchiad Shochat 
5683f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
5693f89a643SAchiad Shochat 
5703f89a643SAchiad Shochat 	dev_put(ndev);
5713f89a643SAchiad Shochat 
5723f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
573b3cbd6f0SDaniel Jurgens out:
574b3cbd6f0SDaniel Jurgens 	if (put_mdev)
575b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
576b3cbd6f0SDaniel Jurgens 	return err;
5773f89a643SAchiad Shochat }
5783f89a643SAchiad Shochat 
579095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
580095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
581095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
5823cca2606SAchiad Shochat {
583095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
584a70c0739SParav Pandit 	u16 vlan_id = 0xffff;
585095b0927SIlan Tayari 	u8 roce_version = 0;
586095b0927SIlan Tayari 	u8 roce_l3_type = 0;
587095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
588a70c0739SParav Pandit 	int ret;
5893cca2606SAchiad Shochat 
590095b0927SIlan Tayari 	if (gid) {
591095b0927SIlan Tayari 		gid_type = attr->gid_type;
592a70c0739SParav Pandit 		ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
593a70c0739SParav Pandit 		if (ret)
594a70c0739SParav Pandit 			return ret;
5953cca2606SAchiad Shochat 	}
5963cca2606SAchiad Shochat 
597095b0927SIlan Tayari 	switch (gid_type) {
5983cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
599095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
6003cca2606SAchiad Shochat 		break;
6013cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
602095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
603095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
604095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
605095b0927SIlan Tayari 		else
606095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
6073cca2606SAchiad Shochat 		break;
6083cca2606SAchiad Shochat 
6093cca2606SAchiad Shochat 	default:
610095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
6113cca2606SAchiad Shochat 	}
6123cca2606SAchiad Shochat 
613095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
614cf34e1feSParav Pandit 				      roce_l3_type, gid->raw, mac,
615a70c0739SParav Pandit 				      vlan_id < VLAN_CFI_MASK, vlan_id,
616cf34e1feSParav Pandit 				      port_num);
6173cca2606SAchiad Shochat }
6183cca2606SAchiad Shochat 
619f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
6203cca2606SAchiad Shochat 			   __always_unused void **context)
6213cca2606SAchiad Shochat {
622414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
623f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
6243cca2606SAchiad Shochat }
6253cca2606SAchiad Shochat 
626414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
627414448d2SParav Pandit 			   __always_unused void **context)
6283cca2606SAchiad Shochat {
629414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
630414448d2SParav Pandit 			     attr->index, NULL, NULL);
6313cca2606SAchiad Shochat }
6323cca2606SAchiad Shochat 
63347ec3866SParav Pandit __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
63447ec3866SParav Pandit 			       const struct ib_gid_attr *attr)
6352811ba51SAchiad Shochat {
63647ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
6372811ba51SAchiad Shochat 		return 0;
6382811ba51SAchiad Shochat 
6392811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
6402811ba51SAchiad Shochat }
6412811ba51SAchiad Shochat 
6421b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
6431b5daf11SMajd Dibbiny {
6447fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
645d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
6467fae6655SNoa Osherovich 	return 0;
6471b5daf11SMajd Dibbiny }
6481b5daf11SMajd Dibbiny 
6491b5daf11SMajd Dibbiny enum {
6501b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
6511b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
6521b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
6531b5daf11SMajd Dibbiny };
6541b5daf11SMajd Dibbiny 
6551b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
6561b5daf11SMajd Dibbiny {
6571b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
6581b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
6591b5daf11SMajd Dibbiny 
660ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
6611b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
6621b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
6631b5daf11SMajd Dibbiny 
6641b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
6651b5daf11SMajd Dibbiny }
6661b5daf11SMajd Dibbiny 
667da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
668776a3906SMoni Shoua 			    u8 atomic_size_qp,
669da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
670da7525d2SEran Ben Elisha {
671da7525d2SEran Ben Elisha 	u8 tmp;
672da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
673da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
674bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
675da7525d2SEran Ben Elisha 
676da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
677da7525d2SEran Ben Elisha 	 * of host endianness respond
678da7525d2SEran Ben Elisha 	 */
679da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
680da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
681da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
682da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
683da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
684da7525d2SEran Ben Elisha 	} else {
685da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
686da7525d2SEran Ben Elisha 	}
687da7525d2SEran Ben Elisha }
688da7525d2SEran Ben Elisha 
689776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
690776a3906SMoni Shoua 			       struct ib_device_attr *props)
691776a3906SMoni Shoua {
692776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
693776a3906SMoni Shoua 
694776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
695776a3906SMoni Shoua }
696776a3906SMoni Shoua 
6971b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
6981b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
6991b5daf11SMajd Dibbiny {
7001b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7011b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7021b5daf11SMajd Dibbiny 	u64 tmp;
7031b5daf11SMajd Dibbiny 	int err;
7041b5daf11SMajd Dibbiny 
7051b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7061b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7071b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
7081b5daf11SMajd Dibbiny 							    sys_image_guid);
7091b5daf11SMajd Dibbiny 
7101b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7111b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
7123f89a643SAchiad Shochat 		break;
7133f89a643SAchiad Shochat 
7143f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7153f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
7163f89a643SAchiad Shochat 		break;
7171b5daf11SMajd Dibbiny 
7181b5daf11SMajd Dibbiny 	default:
7191b5daf11SMajd Dibbiny 		return -EINVAL;
7201b5daf11SMajd Dibbiny 	}
7213f89a643SAchiad Shochat 
7223f89a643SAchiad Shochat 	if (!err)
7233f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
7243f89a643SAchiad Shochat 
7253f89a643SAchiad Shochat 	return err;
7263f89a643SAchiad Shochat 
7271b5daf11SMajd Dibbiny }
7281b5daf11SMajd Dibbiny 
7291b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
7301b5daf11SMajd Dibbiny 				u16 *max_pkeys)
7311b5daf11SMajd Dibbiny {
7321b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7331b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7341b5daf11SMajd Dibbiny 
7351b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7361b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7371b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
7381b5daf11SMajd Dibbiny 
7391b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7401b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7411b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
7421b5daf11SMajd Dibbiny 						pkey_table_size));
7431b5daf11SMajd Dibbiny 		return 0;
7441b5daf11SMajd Dibbiny 
7451b5daf11SMajd Dibbiny 	default:
7461b5daf11SMajd Dibbiny 		return -EINVAL;
7471b5daf11SMajd Dibbiny 	}
7481b5daf11SMajd Dibbiny }
7491b5daf11SMajd Dibbiny 
7501b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
7511b5daf11SMajd Dibbiny 				u32 *vendor_id)
7521b5daf11SMajd Dibbiny {
7531b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7541b5daf11SMajd Dibbiny 
7551b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7561b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7571b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
7581b5daf11SMajd Dibbiny 
7591b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7601b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7611b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
7621b5daf11SMajd Dibbiny 
7631b5daf11SMajd Dibbiny 	default:
7641b5daf11SMajd Dibbiny 		return -EINVAL;
7651b5daf11SMajd Dibbiny 	}
7661b5daf11SMajd Dibbiny }
7671b5daf11SMajd Dibbiny 
7681b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
7691b5daf11SMajd Dibbiny 				__be64 *node_guid)
7701b5daf11SMajd Dibbiny {
7711b5daf11SMajd Dibbiny 	u64 tmp;
7721b5daf11SMajd Dibbiny 	int err;
7731b5daf11SMajd Dibbiny 
7741b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7751b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7761b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7771b5daf11SMajd Dibbiny 
7781b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7791b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7803f89a643SAchiad Shochat 		break;
7813f89a643SAchiad Shochat 
7823f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7833f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7843f89a643SAchiad Shochat 		break;
7851b5daf11SMajd Dibbiny 
7861b5daf11SMajd Dibbiny 	default:
7871b5daf11SMajd Dibbiny 		return -EINVAL;
7881b5daf11SMajd Dibbiny 	}
7893f89a643SAchiad Shochat 
7903f89a643SAchiad Shochat 	if (!err)
7913f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7923f89a643SAchiad Shochat 
7933f89a643SAchiad Shochat 	return err;
7941b5daf11SMajd Dibbiny }
7951b5daf11SMajd Dibbiny 
7961b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
797bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
7981b5daf11SMajd Dibbiny };
7991b5daf11SMajd Dibbiny 
8001b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
8011b5daf11SMajd Dibbiny {
8021b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
8031b5daf11SMajd Dibbiny 
8041b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
8051b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
8061b5daf11SMajd Dibbiny 
8071b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
8081b5daf11SMajd Dibbiny 
8091b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
8101b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
8111b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
8121b5daf11SMajd Dibbiny }
8131b5daf11SMajd Dibbiny 
814e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
8152528e33eSMatan Barak 				struct ib_device_attr *props,
8162528e33eSMatan Barak 				struct ib_udata *uhw)
817e126ba97SEli Cohen {
818e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
819938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
820e126ba97SEli Cohen 	int err = -ENOMEM;
821288c01b7SEli Cohen 	int max_sq_desc;
822e126ba97SEli Cohen 	int max_rq_sg;
823e126ba97SEli Cohen 	int max_sq_sg;
824e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
82585c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
826402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
827402ca536SBodong Wang 	size_t resp_len;
828402ca536SBodong Wang 	u64 max_tso;
829e126ba97SEli Cohen 
830402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
831402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
832402ca536SBodong Wang 		return -EINVAL;
8336f26b2acSErez Alfasi 
834402ca536SBodong Wang 	resp.response_length = resp_len;
835402ca536SBodong Wang 
836402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
8372528e33eSMatan Barak 		return -EINVAL;
8382528e33eSMatan Barak 
839e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
8401b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
8411b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
8421b5daf11SMajd Dibbiny 	if (err)
8431b5daf11SMajd Dibbiny 		return err;
8441b5daf11SMajd Dibbiny 
8451b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
8461b5daf11SMajd Dibbiny 	if (err)
8471b5daf11SMajd Dibbiny 		return err;
8481b5daf11SMajd Dibbiny 
8491b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
8501b5daf11SMajd Dibbiny 	if (err)
8511b5daf11SMajd Dibbiny 		return err;
852e126ba97SEli Cohen 
8539603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
8549603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
8559603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
856e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
857e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
858e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
8591a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
860938fe83cSSaeed Mahameed 
861938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
862e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
863938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
864e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
865938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
866e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
867938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
868e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
869d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
870d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
871d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
872d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
873b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
874b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
875d2370e0aSMatan Barak 	}
876e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
877938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
878c0a6cbb9SIsrael Rukshin 		props->device_cap_flags |= IB_DEVICE_INTEGRITY_HANDOVER;
8792dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8802dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8812dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8822dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8832dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8842dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8852dea9094SSagi Grimberg 	}
886938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
887f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
888e126ba97SEli Cohen 
88985c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
890e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
891e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
89288115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
893e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
894e8161334SNoa Osherovich 		}
895e8161334SNoa Osherovich 
896e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
897e8161334SNoa Osherovich 			props->raw_packet_caps |=
898e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
89988115fe7SBodong Wang 
900402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
901402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
902402ca536SBodong Wang 			if (max_tso) {
903402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
904402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
905402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
906402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
907402ca536SBodong Wang 			}
908402ca536SBodong Wang 		}
90931f69a82SYishai Hadas 
91031f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
91131f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
91231f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
91331f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
91431f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
91531f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
91631f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
91731f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
91831f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
91931f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
92031f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
9214e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
9224e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
9232d93fc85SMatan Barak 			if (mlx5_accel_ipsec_device_caps(dev->mdev) &
9242d93fc85SMatan Barak 			    MLX5_ACCEL_IPSEC_CAP_DEVICE)
9252d93fc85SMatan Barak 				resp.rss_caps.rx_hash_fields_mask |=
9262d93fc85SMatan Barak 					MLX5_RX_HASH_IPSEC_SPI;
92731f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
92831f69a82SYishai Hadas 		}
92931f69a82SYishai Hadas 	} else {
93031f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
93131f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
93231f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
93331f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
934402ca536SBodong Wang 	}
935402ca536SBodong Wang 
936f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
937f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
938f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
939f0313965SErez Shitrit 	}
940f0313965SErez Shitrit 
94103404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
94285c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
94385c7c014SDaniel Jurgens 	    raw_support)
94403404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
94503404e8aSMaor Gottlieb 
9461d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
9471d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
9481d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
9491d54f890SYishai Hadas 
950cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
95185c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
95285c7c014SDaniel Jurgens 	    raw_support) {
953e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
954cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
955e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
956e8161334SNoa Osherovich 	}
957cff5a0f3SMajd Dibbiny 
95824da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
95924da0016SAriel Levkovich 		props->max_dm_size =
96024da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
96124da0016SAriel Levkovich 	}
96224da0016SAriel Levkovich 
963da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
964da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
965da6d6ba3SMaor Gottlieb 
966b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
967b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
968b1383aa6SNoa Osherovich 
9691b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
9701b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
971e126ba97SEli Cohen 
972e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
973e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
974938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
975938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
976938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
977938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
978288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
979288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
980288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
981e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
98233023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
98333023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
984986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
985938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9869f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
987938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
988938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
989938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
990938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
991938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
992938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
993938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
994e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
995e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
996911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
997911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
99862e3c379SMax Gurtovoy 	props->max_pi_fast_reg_page_list_len =
99962e3c379SMax Gurtovoy 		props->max_fast_reg_page_list_len / 2;
100036609056SYamin Friedman 	props->max_sgl_rd =
100136609056SYamin Friedman 		MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance);
1002776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
100381bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
1004938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1005938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
1006e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1007e126ba97SEli Cohen 					   props->max_mcast_grp;
1008e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
100986695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
10107c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
10117c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
1012e126ba97SEli Cohen 
1013e502b8b0SLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
101400815752SMoni Shoua 		if (dev->odp_caps.general_caps & IB_ODP_SUPPORT)
10158cdd312cSHaggai Eran 			props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
10168cdd312cSHaggai Eran 		props->odp_caps = dev->odp_caps;
1017e502b8b0SLeon Romanovsky 	}
10188cdd312cSHaggai Eran 
1019051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
1020051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1021051f2630SLeon Romanovsky 
1022eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
1023eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1024eff901d3SEli Cohen 
102531f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
102685c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
102731f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
102831f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
102931f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
103031f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
103131f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
103231f69a82SYishai Hadas 		props->max_wq_type_rq =
103331f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
103431f69a82SYishai Hadas 	}
103531f69a82SYishai Hadas 
1036eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
103778b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
1038eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
103978b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
1040eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
104178b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1042eb761894SArtemy Kovalyov 	}
1043eb761894SArtemy Kovalyov 
104489705e92SDanit Goldberg 	if (MLX5_CAP_GEN(mdev, tag_matching) &&
104589705e92SDanit Goldberg 	    MLX5_CAP_GEN(mdev, rndv_offload_rc)) {
104689705e92SDanit Goldberg 		props->tm_caps.flags = IB_TM_CAP_RNDV_RC;
104789705e92SDanit Goldberg 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
104889705e92SDanit Goldberg 	}
104989705e92SDanit Goldberg 
105087ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
105187ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
105287ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
105387ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
105487ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
105587ab3f52SYonatan Cohen 	}
105687ab3f52SYonatan Cohen 
10577e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
1058572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
1059572f46bfSYonatan Cohen 
1060572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
10617e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
1062572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
1063572f46bfSYonatan Cohen 					     cqe_compression_max_num);
1064572f46bfSYonatan Cohen 
10657e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
10667e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
10677e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
10686f1006a4SYonatan Cohen 
10696f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
10706f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
10716f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1072572f46bfSYonatan Cohen 		}
10737e43a2a5SBodong Wang 	}
10747e43a2a5SBodong Wang 
107585c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
107685c7c014SDaniel Jurgens 	    raw_support) {
1077d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1078d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
1079d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
1080d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1081d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
1082d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1083d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
1084d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
108561147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
108661147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
108761147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
108861147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
1089d949167dSBodong Wang 		}
1090d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1091d949167dSBodong Wang 	}
1092d949167dSBodong Wang 
10939f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
10949f885201SLeon Romanovsky 			uhw->outlen)) {
1095795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
10969f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1097795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1098050da902SBodong Wang 
1099050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1100050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1101050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1102050da902SBodong Wang 
11039f885201SLeon Romanovsky 		resp.response_length +=
11049f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
11059f885201SLeon Romanovsky 	}
11069f885201SLeon Romanovsky 
1107de57f2adSGuy Levi 	if (field_avail(typeof(resp), flags, uhw->outlen)) {
1108de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
11097a0c8f42SGuy Levi 
1110de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1111de57f2adSGuy Levi 			resp.flags |=
1112de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
11137a0c8f42SGuy Levi 
11147a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
11157a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
11167e11b911SDanit Goldberg 		if (MLX5_CAP_GEN(mdev, qp_packet_based))
11177e11b911SDanit Goldberg 			resp.flags |=
11187e11b911SDanit Goldberg 				MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
11197249c8eaSGuy Levi 
11207249c8eaSGuy Levi 		resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1121de57f2adSGuy Levi 	}
11229f885201SLeon Romanovsky 
112396dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
112496dc3fc5SNoa Osherovich 			uhw->outlen)) {
112596dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
112696dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
112796dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
112896dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
112996dc3fc5SNoa Osherovich 
113096dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
113196dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
113296dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
113396dc3fc5SNoa Osherovich 
113496dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
113596dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
113696dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
113796dc3fc5SNoa Osherovich 
113896dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
113996dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
114096dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
114196dc3fc5SNoa Osherovich 		}
114296dc3fc5SNoa Osherovich 	}
114396dc3fc5SNoa Osherovich 
114485c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
114585c7c014SDaniel Jurgens 	    raw_support) {
1146b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1147b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1148b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1149b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1150b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1151b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1152*c16339b6SMark Zhang 			if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range))
1153*c16339b6SMark Zhang 				resp.striding_rq_caps
1154*c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1155*c16339b6SMark Zhang 					MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1156*c16339b6SMark Zhang 			else
1157*c16339b6SMark Zhang 				resp.striding_rq_caps
1158*c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1159b4f34597SNoa Osherovich 					MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1160b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1161b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1162b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1163b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1164b4f34597SNoa Osherovich 		}
1165b4f34597SNoa Osherovich 	}
1166b4f34597SNoa Osherovich 
1167f95ef6cbSMaor Gottlieb 	if (field_avail(typeof(resp), tunnel_offloads_caps,
1168f95ef6cbSMaor Gottlieb 			uhw->outlen)) {
1169f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1170f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1171f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1172f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1173f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1174f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1175f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1176f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1177f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1178f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
1179e818e255SAriel Levkovich 		if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1180e818e255SAriel Levkovich 		    MLX5_FLEX_PROTO_CW_MPLS_GRE)
1181e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1182e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1183e818e255SAriel Levkovich 		if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1184e818e255SAriel Levkovich 		    MLX5_FLEX_PROTO_CW_MPLS_UDP)
1185e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1186e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1187f95ef6cbSMaor Gottlieb 	}
1188f95ef6cbSMaor Gottlieb 
1189402ca536SBodong Wang 	if (uhw->outlen) {
1190402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1191402ca536SBodong Wang 
1192402ca536SBodong Wang 		if (err)
1193402ca536SBodong Wang 			return err;
1194402ca536SBodong Wang 	}
1195402ca536SBodong Wang 
11961b5daf11SMajd Dibbiny 	return 0;
11971b5daf11SMajd Dibbiny }
1198e126ba97SEli Cohen 
11991b5daf11SMajd Dibbiny enum mlx5_ib_width {
12001b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
12011b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
12021b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
12031b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
12041b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
12051b5daf11SMajd Dibbiny };
12061b5daf11SMajd Dibbiny 
1207db7a691aSMichael Guralnik static void translate_active_width(struct ib_device *ibdev, u8 active_width,
12081b5daf11SMajd Dibbiny 				  u8 *ib_width)
12091b5daf11SMajd Dibbiny {
12101b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12111b5daf11SMajd Dibbiny 
1212db7a691aSMichael Guralnik 	if (active_width & MLX5_IB_WIDTH_1X)
12131b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1214d764970bSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_2X)
1215d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1216db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_4X)
12171b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1218db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_8X)
12191b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1220db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_12X)
12211b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1222db7a691aSMichael Guralnik 	else {
1223db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
12241b5daf11SMajd Dibbiny 			    (int)active_width);
1225db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
12261b5daf11SMajd Dibbiny 	}
12271b5daf11SMajd Dibbiny 
1228db7a691aSMichael Guralnik 	return;
12291b5daf11SMajd Dibbiny }
12301b5daf11SMajd Dibbiny 
12311b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
12321b5daf11SMajd Dibbiny {
12331b5daf11SMajd Dibbiny 	switch (mtu) {
12341b5daf11SMajd Dibbiny 	case 256: return 1;
12351b5daf11SMajd Dibbiny 	case 512: return 2;
12361b5daf11SMajd Dibbiny 	case 1024: return 3;
12371b5daf11SMajd Dibbiny 	case 2048: return 4;
12381b5daf11SMajd Dibbiny 	case 4096: return 5;
12391b5daf11SMajd Dibbiny 	default:
12401b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
12411b5daf11SMajd Dibbiny 		return -1;
12421b5daf11SMajd Dibbiny 	}
12431b5daf11SMajd Dibbiny }
12441b5daf11SMajd Dibbiny 
12451b5daf11SMajd Dibbiny enum ib_max_vl_num {
12461b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
12471b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
12481b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
12491b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
12501b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
12511b5daf11SMajd Dibbiny };
12521b5daf11SMajd Dibbiny 
12531b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
12541b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
12551b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
12561b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
12571b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
12581b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
12591b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
12601b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
12611b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
12621b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
12631b5daf11SMajd Dibbiny };
12641b5daf11SMajd Dibbiny 
12651b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
12661b5daf11SMajd Dibbiny 				u8 *max_vl_num)
12671b5daf11SMajd Dibbiny {
12681b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
12691b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
12701b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
12711b5daf11SMajd Dibbiny 		break;
12721b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
12731b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
12741b5daf11SMajd Dibbiny 		break;
12751b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
12761b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
12771b5daf11SMajd Dibbiny 		break;
12781b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
12791b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
12801b5daf11SMajd Dibbiny 		break;
12811b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
12821b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
12831b5daf11SMajd Dibbiny 		break;
12841b5daf11SMajd Dibbiny 
12851b5daf11SMajd Dibbiny 	default:
12861b5daf11SMajd Dibbiny 		return -EINVAL;
12871b5daf11SMajd Dibbiny 	}
12881b5daf11SMajd Dibbiny 
12891b5daf11SMajd Dibbiny 	return 0;
12901b5daf11SMajd Dibbiny }
12911b5daf11SMajd Dibbiny 
12921b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
12931b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
12941b5daf11SMajd Dibbiny {
12951b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12961b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
12971b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1298046339eaSSaeed Mahameed 	u16 max_mtu;
1299046339eaSSaeed Mahameed 	u16 oper_mtu;
13001b5daf11SMajd Dibbiny 	int err;
13011b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
13021b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
13031b5daf11SMajd Dibbiny 
13041b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
13051b5daf11SMajd Dibbiny 	if (!rep) {
13061b5daf11SMajd Dibbiny 		err = -ENOMEM;
13071b5daf11SMajd Dibbiny 		goto out;
13081b5daf11SMajd Dibbiny 	}
13091b5daf11SMajd Dibbiny 
1310c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
13111b5daf11SMajd Dibbiny 
13121b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
13131b5daf11SMajd Dibbiny 	if (err)
13141b5daf11SMajd Dibbiny 		goto out;
13151b5daf11SMajd Dibbiny 
13161b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
13171b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
13181b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
13191b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
13201b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
13211b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
13221b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
13231b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
13241b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
13251b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
13261b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
13271b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
13281b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
13291b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
13301b5daf11SMajd Dibbiny 
13314106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
13324106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
13334106a758SMichael Guralnik 
13341b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
13351b5daf11SMajd Dibbiny 	if (err)
13361b5daf11SMajd Dibbiny 		goto out;
13371b5daf11SMajd Dibbiny 
1338db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1339db7a691aSMichael Guralnik 
1340d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
13411b5daf11SMajd Dibbiny 	if (err)
13421b5daf11SMajd Dibbiny 		goto out;
13431b5daf11SMajd Dibbiny 
1344facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
13451b5daf11SMajd Dibbiny 
13461b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
13471b5daf11SMajd Dibbiny 
1348facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
13491b5daf11SMajd Dibbiny 
13501b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
13511b5daf11SMajd Dibbiny 
13521b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
13531b5daf11SMajd Dibbiny 	if (err)
13541b5daf11SMajd Dibbiny 		goto out;
13551b5daf11SMajd Dibbiny 
13561b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
13571b5daf11SMajd Dibbiny 				   &props->max_vl_num);
13581b5daf11SMajd Dibbiny out:
13591b5daf11SMajd Dibbiny 	kfree(rep);
1360e126ba97SEli Cohen 	return err;
1361e126ba97SEli Cohen }
1362e126ba97SEli Cohen 
1363e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1364e126ba97SEli Cohen 		       struct ib_port_attr *props)
1365e126ba97SEli Cohen {
1366095b0927SIlan Tayari 	unsigned int count;
1367095b0927SIlan Tayari 	int ret;
1368095b0927SIlan Tayari 
13691b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13701b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1371095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1372095b0927SIlan Tayari 		break;
1373e126ba97SEli Cohen 
13741b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1375095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1376095b0927SIlan Tayari 		break;
13771b5daf11SMajd Dibbiny 
13783f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1379095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1380095b0927SIlan Tayari 		break;
13813f89a643SAchiad Shochat 
13821b5daf11SMajd Dibbiny 	default:
1383095b0927SIlan Tayari 		ret = -EINVAL;
1384e126ba97SEli Cohen 	}
1385095b0927SIlan Tayari 
1386095b0927SIlan Tayari 	if (!ret && props) {
1387b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1388b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1389b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1390b3cbd6f0SDaniel Jurgens 
1391b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1392b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1393b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1394b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1395b3cbd6f0SDaniel Jurgens 			 */
1396b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1397b3cbd6f0SDaniel Jurgens 			port = 1;
1398b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1399b3cbd6f0SDaniel Jurgens 		}
1400b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1401b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1402b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1403095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1404095b0927SIlan Tayari 	}
1405095b0927SIlan Tayari 	return ret;
1406e126ba97SEli Cohen }
1407e126ba97SEli Cohen 
14088e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
14098e6efa3aSMark Bloch 				  struct ib_port_attr *props)
14108e6efa3aSMark Bloch {
14118e6efa3aSMark Bloch 	int ret;
14128e6efa3aSMark Bloch 
141326628e2dSMark Bloch 	/* Only link layer == ethernet is valid for representors
141426628e2dSMark Bloch 	 * and we always use port 1
141526628e2dSMark Bloch 	 */
14168e6efa3aSMark Bloch 	ret = mlx5_query_port_roce(ibdev, port, props);
14178e6efa3aSMark Bloch 	if (ret || !props)
14188e6efa3aSMark Bloch 		return ret;
14198e6efa3aSMark Bloch 
14208e6efa3aSMark Bloch 	/* We don't support GIDS */
14218e6efa3aSMark Bloch 	props->gid_tbl_len = 0;
14228e6efa3aSMark Bloch 
14238e6efa3aSMark Bloch 	return ret;
14248e6efa3aSMark Bloch }
14258e6efa3aSMark Bloch 
1426e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1427e126ba97SEli Cohen 			     union ib_gid *gid)
1428e126ba97SEli Cohen {
14291b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
14301b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1431e126ba97SEli Cohen 
14321b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14331b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14341b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1435e126ba97SEli Cohen 
14361b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14371b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1438e126ba97SEli Cohen 
14391b5daf11SMajd Dibbiny 	default:
14401b5daf11SMajd Dibbiny 		return -EINVAL;
14411b5daf11SMajd Dibbiny 	}
1442e126ba97SEli Cohen 
1443e126ba97SEli Cohen }
1444e126ba97SEli Cohen 
1445b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1446b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1447b3cbd6f0SDaniel Jurgens {
1448b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1449b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1450b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1451b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1452b3cbd6f0SDaniel Jurgens 	int err;
1453b3cbd6f0SDaniel Jurgens 
1454b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1455b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1456b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1457b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1458b3cbd6f0SDaniel Jurgens 		 */
1459b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1460b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1461b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1462b3cbd6f0SDaniel Jurgens 	}
1463b3cbd6f0SDaniel Jurgens 
1464b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1465b3cbd6f0SDaniel Jurgens 					index, pkey);
1466b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1467b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1468b3cbd6f0SDaniel Jurgens 
1469b3cbd6f0SDaniel Jurgens 	return err;
1470b3cbd6f0SDaniel Jurgens }
1471b3cbd6f0SDaniel Jurgens 
1472e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1473e126ba97SEli Cohen 			      u16 *pkey)
1474e126ba97SEli Cohen {
14751b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14761b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14771b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1478e126ba97SEli Cohen 
14791b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14801b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1481b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
14821b5daf11SMajd Dibbiny 	default:
14831b5daf11SMajd Dibbiny 		return -EINVAL;
1484e126ba97SEli Cohen 	}
14851b5daf11SMajd Dibbiny }
1486e126ba97SEli Cohen 
1487e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1488e126ba97SEli Cohen 				 struct ib_device_modify *props)
1489e126ba97SEli Cohen {
1490e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1491e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1492e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1493e126ba97SEli Cohen 	int err;
1494e126ba97SEli Cohen 
1495e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1496e126ba97SEli Cohen 		return -EOPNOTSUPP;
1497e126ba97SEli Cohen 
1498e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1499e126ba97SEli Cohen 		return 0;
1500e126ba97SEli Cohen 
1501e126ba97SEli Cohen 	/*
1502e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1503e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1504e126ba97SEli Cohen 	 */
1505bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
15069603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1507e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1508e126ba97SEli Cohen 	if (err)
1509e126ba97SEli Cohen 		return err;
1510e126ba97SEli Cohen 
1511bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1512e126ba97SEli Cohen 
1513e126ba97SEli Cohen 	return err;
1514e126ba97SEli Cohen }
1515e126ba97SEli Cohen 
1516cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1517cdbe33d0SEli Cohen 				u32 value)
1518cdbe33d0SEli Cohen {
1519cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1520b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1521b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1522cdbe33d0SEli Cohen 	int err;
1523cdbe33d0SEli Cohen 
1524b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1525b3cbd6f0SDaniel Jurgens 	if (!mdev)
1526b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1527b3cbd6f0SDaniel Jurgens 
1528b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1529cdbe33d0SEli Cohen 	if (err)
1530b3cbd6f0SDaniel Jurgens 		goto out;
1531cdbe33d0SEli Cohen 
1532cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1533cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1534cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1535b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1536b3cbd6f0SDaniel Jurgens 		goto out;
1537cdbe33d0SEli Cohen 	}
1538cdbe33d0SEli Cohen 
1539cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1540cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1541b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1542b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1543b3cbd6f0SDaniel Jurgens 
1544b3cbd6f0SDaniel Jurgens out:
1545b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1546cdbe33d0SEli Cohen 
1547cdbe33d0SEli Cohen 	return err;
1548cdbe33d0SEli Cohen }
1549cdbe33d0SEli Cohen 
1550e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1551e126ba97SEli Cohen 			       struct ib_port_modify *props)
1552e126ba97SEli Cohen {
1553e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1554e126ba97SEli Cohen 	struct ib_port_attr attr;
1555e126ba97SEli Cohen 	u32 tmp;
1556e126ba97SEli Cohen 	int err;
1557cdbe33d0SEli Cohen 	u32 change_mask;
1558cdbe33d0SEli Cohen 	u32 value;
1559cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1560cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1561cdbe33d0SEli Cohen 
1562ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1563ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1564ec255879SMajd Dibbiny 	 */
1565ec255879SMajd Dibbiny 	if (!is_ib)
1566ec255879SMajd Dibbiny 		return 0;
1567ec255879SMajd Dibbiny 
1568cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1569cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1570cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1571cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1572cdbe33d0SEli Cohen 	}
1573e126ba97SEli Cohen 
1574e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1575e126ba97SEli Cohen 
1576c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1577e126ba97SEli Cohen 	if (err)
1578e126ba97SEli Cohen 		goto out;
1579e126ba97SEli Cohen 
1580e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1581e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1582e126ba97SEli Cohen 
15839603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1584e126ba97SEli Cohen 
1585e126ba97SEli Cohen out:
1586e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1587e126ba97SEli Cohen 	return err;
1588e126ba97SEli Cohen }
1589e126ba97SEli Cohen 
159030aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
159130aa60b3SEli Cohen {
159230aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
159330aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
159430aa60b3SEli Cohen }
159530aa60b3SEli Cohen 
159631a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
159731a78a5aSYishai Hadas {
159831a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
159931a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
160031a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
160131a78a5aSYishai Hadas 
160231a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
160331a78a5aSYishai Hadas }
160431a78a5aSYishai Hadas 
1605b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1606b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
160731a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1608b037c29aSEli Cohen {
1609b037c29aSEli Cohen 	int uars_per_sys_page;
1610b037c29aSEli Cohen 	int bfregs_per_sys_page;
1611b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1612b037c29aSEli Cohen 
1613b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1614b037c29aSEli Cohen 		return -EINVAL;
1615b037c29aSEli Cohen 
1616b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1617b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1618b037c29aSEli Cohen 
1619b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1620b037c29aSEli Cohen 		return -ENOMEM;
1621b037c29aSEli Cohen 
1622b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1623b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
162431a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1625b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1626b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1627b037c29aSEli Cohen 		return -EINVAL;
1628b037c29aSEli Cohen 
162931a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
163031a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
163131a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
163231a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
163331a78a5aSYishai Hadas 
163431a78a5aSYishai 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",
1635b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1636b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
163731a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
163831a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1639b037c29aSEli Cohen 
1640b037c29aSEli Cohen 	return 0;
1641b037c29aSEli Cohen }
1642b037c29aSEli Cohen 
1643b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1644b037c29aSEli Cohen {
1645b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1646b037c29aSEli Cohen 	int err;
1647b037c29aSEli Cohen 	int i;
1648b037c29aSEli Cohen 
1649b037c29aSEli Cohen 	bfregi = &context->bfregi;
165031a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1651b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1652b037c29aSEli Cohen 		if (err)
1653b037c29aSEli Cohen 			goto error;
1654b037c29aSEli Cohen 
1655b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1656b037c29aSEli Cohen 	}
16574ed131d0SYishai Hadas 
16584ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
16594ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
16604ed131d0SYishai Hadas 
1661b037c29aSEli Cohen 	return 0;
1662b037c29aSEli Cohen 
1663b037c29aSEli Cohen error:
1664b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1665b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1666b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1667b037c29aSEli Cohen 
1668b037c29aSEli Cohen 	return err;
1669b037c29aSEli Cohen }
1670b037c29aSEli Cohen 
167115177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
167215177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1673b037c29aSEli Cohen {
1674b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1675b037c29aSEli Cohen 	int i;
1676b037c29aSEli Cohen 
1677b037c29aSEli Cohen 	bfregi = &context->bfregi;
167815177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
16794ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
168015177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
168115177999SLeon Romanovsky 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1682b037c29aSEli Cohen }
1683b037c29aSEli Cohen 
16840042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1685a560f1d9SMark Bloch {
1686a560f1d9SMark Bloch 	int err = 0;
1687a560f1d9SMark Bloch 
1688a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16890042f9e4SMark Bloch 	if (td)
1690a560f1d9SMark Bloch 		dev->lb.user_td++;
16910042f9e4SMark Bloch 	if (qp)
16920042f9e4SMark Bloch 		dev->lb.qps++;
1693a560f1d9SMark Bloch 
16940042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
16950042f9e4SMark Bloch 	    dev->lb.qps == 1) {
16960042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1697a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
16980042f9e4SMark Bloch 			dev->lb.enabled = true;
16990042f9e4SMark Bloch 		}
17000042f9e4SMark Bloch 	}
1701a560f1d9SMark Bloch 
1702a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1703a560f1d9SMark Bloch 
1704a560f1d9SMark Bloch 	return err;
1705a560f1d9SMark Bloch }
1706a560f1d9SMark Bloch 
17070042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1708a560f1d9SMark Bloch {
1709a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
17100042f9e4SMark Bloch 	if (td)
1711a560f1d9SMark Bloch 		dev->lb.user_td--;
17120042f9e4SMark Bloch 	if (qp)
17130042f9e4SMark Bloch 		dev->lb.qps--;
1714a560f1d9SMark Bloch 
17150042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
17160042f9e4SMark Bloch 	    dev->lb.qps == 0) {
17170042f9e4SMark Bloch 		if (dev->lb.enabled) {
1718a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
17190042f9e4SMark Bloch 			dev->lb.enabled = false;
17200042f9e4SMark Bloch 		}
17210042f9e4SMark Bloch 	}
1722a560f1d9SMark Bloch 
1723a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1724a560f1d9SMark Bloch }
1725a560f1d9SMark Bloch 
1726d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1727d2d19121SYishai Hadas 					  u16 uid)
1728c85023e1SHuy Nguyen {
1729c85023e1SHuy Nguyen 	int err;
1730c85023e1SHuy Nguyen 
1731cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1732cfdeb893SLeon Romanovsky 		return 0;
1733cfdeb893SLeon Romanovsky 
1734d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1735c85023e1SHuy Nguyen 	if (err)
1736c85023e1SHuy Nguyen 		return err;
1737c85023e1SHuy Nguyen 
1738c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17398978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17408978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1741c85023e1SHuy Nguyen 		return err;
1742c85023e1SHuy Nguyen 
17430042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1744c85023e1SHuy Nguyen }
1745c85023e1SHuy Nguyen 
1746d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1747d2d19121SYishai Hadas 					     u16 uid)
1748c85023e1SHuy Nguyen {
1749cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1750cfdeb893SLeon Romanovsky 		return;
1751cfdeb893SLeon Romanovsky 
1752d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1753c85023e1SHuy Nguyen 
1754c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17558978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17568978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1757c85023e1SHuy Nguyen 		return;
1758c85023e1SHuy Nguyen 
17590042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1760c85023e1SHuy Nguyen }
1761c85023e1SHuy Nguyen 
1762a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1763e126ba97SEli Cohen 				  struct ib_udata *udata)
1764e126ba97SEli Cohen {
1765a2a074efSLeon Romanovsky 	struct ib_device *ibdev = uctx->device;
1766e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1767b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1768b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
17695c99eaecSFeras Daoud 	struct mlx5_core_dev *mdev = dev->mdev;
1770a2a074efSLeon Romanovsky 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
17712f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
177278c0f98cSEli Cohen 	int ver;
1773e126ba97SEli Cohen 	int err;
1774a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1775a168a41cSMajd Dibbiny 				     max_cqe_version);
177625bb36e7SYonatan Cohen 	u32 dump_fill_mkey;
1777b037c29aSEli Cohen 	bool lib_uar_4k;
1778e126ba97SEli Cohen 
1779e126ba97SEli Cohen 	if (!dev->ib_active)
1780a2a074efSLeon Romanovsky 		return -EAGAIN;
1781e126ba97SEli Cohen 
1782e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
178378c0f98cSEli Cohen 		ver = 0;
1784e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
178578c0f98cSEli Cohen 		ver = 2;
178678c0f98cSEli Cohen 	else
1787a2a074efSLeon Romanovsky 		return -EINVAL;
178878c0f98cSEli Cohen 
1789e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1790e126ba97SEli Cohen 	if (err)
1791a2a074efSLeon Romanovsky 		return err;
1792e126ba97SEli Cohen 
1793a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1794a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
179578c0f98cSEli Cohen 
1796f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1797a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
1798b368d7cbSMatan Barak 
17992f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
18002f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
18012f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1802a2a074efSLeon Romanovsky 		return -EINVAL;
1803e126ba97SEli Cohen 
1804938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
180511f552e2SMichael Guralnik 	if (dev->wc_support)
1806938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1807b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1808938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1809938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1810938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1811938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1812938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1813f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1814f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1815f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
181630aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
181730aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
181830aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
181930aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1820b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1821b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1822e126ba97SEli Cohen 
1823c03faa56SMatan Barak 	if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1824c03faa56SMatan Barak 		if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1825c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1826c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1827c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1828c03faa56SMatan Barak 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1829c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1830c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1831c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1832c03faa56SMatan Barak 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1833c03faa56SMatan Barak 	}
1834c03faa56SMatan Barak 
183530aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
18362f5ff264SEli Cohen 	bfregi = &context->bfregi;
1837b037c29aSEli Cohen 
1838b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
183931a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1840b037c29aSEli Cohen 	if (err)
1841b037c29aSEli Cohen 		goto out_ctx;
1842b037c29aSEli Cohen 
18432f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1844b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
184531a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1846b037c29aSEli Cohen 				GFP_KERNEL);
1847b037c29aSEli Cohen 	if (!bfregi->count) {
1848e126ba97SEli Cohen 		err = -ENOMEM;
1849e126ba97SEli Cohen 		goto out_ctx;
1850e126ba97SEli Cohen 	}
1851e126ba97SEli Cohen 
1852b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1853b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1854e126ba97SEli Cohen 				    GFP_KERNEL);
1855b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1856e126ba97SEli Cohen 		err = -ENOMEM;
1857e126ba97SEli Cohen 		goto out_count;
1858e126ba97SEli Cohen 	}
1859e126ba97SEli Cohen 
1860b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1861b037c29aSEli Cohen 	if (err)
1862b037c29aSEli Cohen 		goto out_sys_pages;
1863b037c29aSEli Cohen 
1864a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1865fb98153bSYishai Hadas 		err = mlx5_ib_devx_create(dev, true);
186676dc5a84SYishai Hadas 		if (err < 0)
1867d2d19121SYishai Hadas 			goto out_uars;
186876dc5a84SYishai Hadas 		context->devx_uid = err;
1869a8b92ca1SYishai Hadas 	}
1870a8b92ca1SYishai Hadas 
1871d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1872d2d19121SYishai Hadas 					     context->devx_uid);
1873a8b92ca1SYishai Hadas 	if (err)
1874d2d19121SYishai Hadas 		goto out_devx;
1875a8b92ca1SYishai Hadas 
187625bb36e7SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
187725bb36e7SYonatan Cohen 		err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
187825bb36e7SYonatan Cohen 		if (err)
18798193abb6SJason Gunthorpe 			goto out_mdev;
188025bb36e7SYonatan Cohen 	}
188125bb36e7SYonatan Cohen 
1882e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1883e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1884e126ba97SEli Cohen 
18852f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1886508562d6SDaniel Jurgens 	resp.num_ports = dev->num_ports;
1887b368d7cbSMatan Barak 
1888f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1889f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1890b368d7cbSMatan Barak 
1891402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
18926ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
18936ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1894402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1895402ca536SBodong Wang 	}
1896402ca536SBodong Wang 
189778984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
189878984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
189978984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
190078984898SOr Gerlitz 			resp.eth_min_inline++;
190178984898SOr Gerlitz 		}
190278984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
190378984898SOr Gerlitz 	}
190478984898SOr Gerlitz 
19055c99eaecSFeras Daoud 	if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
19065c99eaecSFeras Daoud 		if (mdev->clock_info)
19075c99eaecSFeras Daoud 			resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
19085c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.clock_info_versions);
19095c99eaecSFeras Daoud 	}
19105c99eaecSFeras Daoud 
1911bc5c6eedSNoa Osherovich 	/*
1912bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1913bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1914bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1915bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1916bc5c6eedSNoa Osherovich 	 */
1917de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1918de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1919b368d7cbSMatan Barak 			resp.comp_mask |=
1920b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1921b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1922de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1923de8d6e02SEli Cohen 		}
19245c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.hca_core_clock_offset);
1925b368d7cbSMatan Barak 	}
1926b368d7cbSMatan Barak 
192730aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
192830aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
192930aa60b3SEli Cohen 
193030aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
193130aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
193230aa60b3SEli Cohen 
193331a78a5aSYishai Hadas 	if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
193431a78a5aSYishai Hadas 		resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
193531a78a5aSYishai Hadas 		resp.response_length += sizeof(resp.num_dyn_bfregs);
193631a78a5aSYishai Hadas 	}
193731a78a5aSYishai Hadas 
193825bb36e7SYonatan Cohen 	if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
193925bb36e7SYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
194025bb36e7SYonatan Cohen 			resp.dump_fill_mkey = dump_fill_mkey;
194125bb36e7SYonatan Cohen 			resp.comp_mask |=
194225bb36e7SYonatan Cohen 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
194325bb36e7SYonatan Cohen 		}
194425bb36e7SYonatan Cohen 		resp.response_length += sizeof(resp.dump_fill_mkey);
194525bb36e7SYonatan Cohen 	}
194625bb36e7SYonatan Cohen 
1947b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1948e126ba97SEli Cohen 	if (err)
1949a8b92ca1SYishai Hadas 		goto out_mdev;
1950e126ba97SEli Cohen 
19512f5ff264SEli Cohen 	bfregi->ver = ver;
19522f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1953f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
195430aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
195530aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1956f72300c5SHaggai Abramovsky 
19577c34ec19SAviv Heller 	if (dev->lag_active) {
195895579e78SMark Bloch 		u8 port = mlx5_core_native_port_num(dev->mdev) - 1;
1959c6a21c38SMajd Dibbiny 
1960c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1961c6a21c38SMajd Dibbiny 			   atomic_add_return(
196295579e78SMark Bloch 				   1, &dev->port[port].roce.tx_port_affinity));
1963c6a21c38SMajd Dibbiny 	}
1964c6a21c38SMajd Dibbiny 
1965a2a074efSLeon Romanovsky 	return 0;
1966e126ba97SEli Cohen 
1967a8b92ca1SYishai Hadas out_mdev:
1968d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1969d2d19121SYishai Hadas out_devx:
1970a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
197176dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1972146d2f1aSmajd@mellanox.com 
1973e126ba97SEli Cohen out_uars:
1974b037c29aSEli Cohen 	deallocate_uars(dev, context);
1975b037c29aSEli Cohen 
1976b037c29aSEli Cohen out_sys_pages:
1977b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1978b037c29aSEli Cohen 
1979e126ba97SEli Cohen out_count:
19802f5ff264SEli Cohen 	kfree(bfregi->count);
1981e126ba97SEli Cohen 
1982e126ba97SEli Cohen out_ctx:
1983a2a074efSLeon Romanovsky 	return err;
1984e126ba97SEli Cohen }
1985e126ba97SEli Cohen 
1986a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1987e126ba97SEli Cohen {
1988e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1989e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1990b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1991e126ba97SEli Cohen 
1992b037c29aSEli Cohen 	bfregi = &context->bfregi;
1993d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1994d2d19121SYishai Hadas 
1995b037c29aSEli Cohen 	if (context->devx_uid)
199676dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1997e126ba97SEli Cohen 
1998e126ba97SEli Cohen 	deallocate_uars(dev, context);
19992f5ff264SEli Cohen 	kfree(bfregi->sys_pages);
20002f5ff264SEli Cohen 	kfree(bfregi->count);
2001e126ba97SEli Cohen }
2002e126ba97SEli Cohen 
2003e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2004e126ba97SEli Cohen 				 int uar_idx)
2005e126ba97SEli Cohen {
2006b037c29aSEli Cohen 	int fw_uars_per_page;
2007b037c29aSEli Cohen 
2008b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2009b037c29aSEli Cohen 
2010aa8106f1SHuy Nguyen 	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2011e126ba97SEli Cohen }
2012e126ba97SEli Cohen 
2013e126ba97SEli Cohen static int get_command(unsigned long offset)
2014e126ba97SEli Cohen {
2015e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2016e126ba97SEli Cohen }
2017e126ba97SEli Cohen 
2018e126ba97SEli Cohen static int get_arg(unsigned long offset)
2019e126ba97SEli Cohen {
2020e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2021e126ba97SEli Cohen }
2022e126ba97SEli Cohen 
2023e126ba97SEli Cohen static int get_index(unsigned long offset)
2024e126ba97SEli Cohen {
2025e126ba97SEli Cohen 	return get_arg(offset);
2026e126ba97SEli Cohen }
2027e126ba97SEli Cohen 
20284ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
20294ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20304ed131d0SYishai Hadas {
20314ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20324ed131d0SYishai Hadas }
20334ed131d0SYishai Hadas 
20347c2344c3SMaor Gottlieb 
20357c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20367c2344c3SMaor Gottlieb {
20377c2344c3SMaor Gottlieb }
20387c2344c3SMaor Gottlieb 
203937aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2040e126ba97SEli Cohen {
204137aa5c36SGuy Levi 	switch (cmd) {
204237aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
204337aa5c36SGuy Levi 		return "WC";
2044e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
204537aa5c36SGuy Levi 		return "best effort WC";
204637aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
204737aa5c36SGuy Levi 		return "NC";
204824da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
204924da0016SAriel Levkovich 		return "Device Memory";
205037aa5c36SGuy Levi 	default:
205137aa5c36SGuy Levi 		return NULL;
205237aa5c36SGuy Levi 	}
205337aa5c36SGuy Levi }
205437aa5c36SGuy Levi 
20555c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20565c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20575c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20585c99eaecSFeras Daoud {
20594eb6ab13SJason Gunthorpe 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
20604eb6ab13SJason Gunthorpe 	    !(vma->vm_flags & VM_SHARED))
20615c99eaecSFeras Daoud 		return -EINVAL;
20625c99eaecSFeras Daoud 
20635c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20645c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20655c99eaecSFeras Daoud 
20664eb6ab13SJason Gunthorpe 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
20675c99eaecSFeras Daoud 		return -EPERM;
2068c660133cSJason Gunthorpe 	vma->vm_flags &= ~VM_MAYWRITE;
20695c99eaecSFeras Daoud 
2070ddcdc368SJason Gunthorpe 	if (!dev->mdev->clock_info)
20715c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20725c99eaecSFeras Daoud 
20734eb6ab13SJason Gunthorpe 	return vm_insert_page(vma, vma->vm_start,
20744eb6ab13SJason Gunthorpe 			      virt_to_page(dev->mdev->clock_info));
20755c99eaecSFeras Daoud }
20765c99eaecSFeras Daoud 
207737aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
20787c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
20797c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
208037aa5c36SGuy Levi {
20812f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
208237aa5c36SGuy Levi 	int err;
208337aa5c36SGuy Levi 	unsigned long idx;
2084aa09ea6eSKamal Heib 	phys_addr_t pfn;
208537aa5c36SGuy Levi 	pgprot_t prot;
20864ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
20874ed131d0SYishai Hadas 	u32 uar_index;
20884ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
20894ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
20904ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2091b037c29aSEli Cohen 
2092b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2093b037c29aSEli Cohen 		return -EINVAL;
2094b037c29aSEli Cohen 
20954ed131d0SYishai Hadas 	if (dyn_uar)
20964ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
20974ed131d0SYishai Hadas 	else
2098b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
20994ed131d0SYishai Hadas 
21004ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21014ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21024ed131d0SYishai Hadas 			     idx, max_valid_idx);
2103b037c29aSEli Cohen 		return -EINVAL;
2104b037c29aSEli Cohen 	}
210537aa5c36SGuy Levi 
210637aa5c36SGuy Levi 	switch (cmd) {
210737aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21084ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
210937aa5c36SGuy Levi /* Some architectures don't support WC memory */
211037aa5c36SGuy Levi #if defined(CONFIG_X86)
211137aa5c36SGuy Levi 		if (!pat_enabled())
211237aa5c36SGuy Levi 			return -EPERM;
211337aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
211437aa5c36SGuy Levi 			return -EPERM;
211537aa5c36SGuy Levi #endif
211637aa5c36SGuy Levi 	/* fall through */
211737aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
211837aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
211937aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
212037aa5c36SGuy Levi 		break;
212137aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
212237aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
212337aa5c36SGuy Levi 		break;
212437aa5c36SGuy Levi 	default:
212537aa5c36SGuy Levi 		return -EINVAL;
212637aa5c36SGuy Levi 	}
212737aa5c36SGuy Levi 
21284ed131d0SYishai Hadas 	if (dyn_uar) {
21294ed131d0SYishai Hadas 		int uars_per_page;
21304ed131d0SYishai Hadas 
21314ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21324ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21334ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21344ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21354ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21364ed131d0SYishai Hadas 			return -EINVAL;
21374ed131d0SYishai Hadas 		}
21384ed131d0SYishai Hadas 
21394ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21404ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21414ed131d0SYishai Hadas 		 * page holds its count.
21424ed131d0SYishai Hadas 		 */
21434ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
21444ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
21454ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
21464ed131d0SYishai Hadas 			return -EINVAL;
21474ed131d0SYishai Hadas 		}
21484ed131d0SYishai Hadas 
21494ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
21504ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
21514ed131d0SYishai Hadas 
21524ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
21534ed131d0SYishai Hadas 		if (err) {
21544ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
21554ed131d0SYishai Hadas 			goto free_bfreg;
21564ed131d0SYishai Hadas 		}
21574ed131d0SYishai Hadas 	} else {
21584ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
21594ed131d0SYishai Hadas 	}
21604ed131d0SYishai Hadas 
21614ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
216237aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2163e126ba97SEli Cohen 
2164e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2165c043ff2cSMichal Kalderon 				prot, NULL);
216637aa5c36SGuy Levi 	if (err) {
21678f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2168e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
21698f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
21704ed131d0SYishai Hadas 		goto err;
217137aa5c36SGuy Levi 	}
2172e126ba97SEli Cohen 
21734ed131d0SYishai Hadas 	if (dyn_uar)
21744ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
21754ed131d0SYishai Hadas 	return 0;
21764ed131d0SYishai Hadas 
21774ed131d0SYishai Hadas err:
21784ed131d0SYishai Hadas 	if (!dyn_uar)
21794ed131d0SYishai Hadas 		return err;
21804ed131d0SYishai Hadas 
21814ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
21824ed131d0SYishai Hadas 
21834ed131d0SYishai Hadas free_bfreg:
21844ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
21854ed131d0SYishai Hadas 
21864ed131d0SYishai Hadas 	return err;
218737aa5c36SGuy Levi }
218837aa5c36SGuy Levi 
218924da0016SAriel Levkovich static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
219024da0016SAriel Levkovich {
219124da0016SAriel Levkovich 	struct mlx5_ib_ucontext *mctx = to_mucontext(context);
219224da0016SAriel Levkovich 	struct mlx5_ib_dev *dev = to_mdev(context->device);
219324da0016SAriel Levkovich 	u16 page_idx = get_extended_index(vma->vm_pgoff);
219424da0016SAriel Levkovich 	size_t map_size = vma->vm_end - vma->vm_start;
219524da0016SAriel Levkovich 	u32 npages = map_size >> PAGE_SHIFT;
219624da0016SAriel Levkovich 	phys_addr_t pfn;
219724da0016SAriel Levkovich 
219824da0016SAriel Levkovich 	if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
219924da0016SAriel Levkovich 	    page_idx + npages)
220024da0016SAriel Levkovich 		return -EINVAL;
220124da0016SAriel Levkovich 
2202aa8106f1SHuy Nguyen 	pfn = ((dev->mdev->bar_addr +
220324da0016SAriel Levkovich 	      MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
220424da0016SAriel Levkovich 	      PAGE_SHIFT) +
220524da0016SAriel Levkovich 	      page_idx;
2206e2cd1d1aSJason Gunthorpe 	return rdma_user_mmap_io(context, vma, pfn, map_size,
2207c043ff2cSMichal Kalderon 				 pgprot_writecombine(vma->vm_page_prot),
2208c043ff2cSMichal Kalderon 				 NULL);
220924da0016SAriel Levkovich }
221024da0016SAriel Levkovich 
221137aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
221237aa5c36SGuy Levi {
221337aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
221437aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
221537aa5c36SGuy Levi 	unsigned long command;
221637aa5c36SGuy Levi 	phys_addr_t pfn;
221737aa5c36SGuy Levi 
221837aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
221937aa5c36SGuy Levi 	switch (command) {
222037aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
222137aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
222237aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
22234ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
22247c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2225e126ba97SEli Cohen 
2226e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2227e126ba97SEli Cohen 		return -ENOSYS;
2228e126ba97SEli Cohen 
2229d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2230d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2231d69e3bcfSMatan Barak 			return -EINVAL;
2232d69e3bcfSMatan Barak 
22336cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2234d69e3bcfSMatan Barak 			return -EPERM;
2235c660133cSJason Gunthorpe 		vma->vm_flags &= ~VM_MAYWRITE;
2236d69e3bcfSMatan Barak 
2237d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2238d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2239d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2240d69e3bcfSMatan Barak 
2241d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2242d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2243d69e3bcfSMatan Barak 			PAGE_SHIFT;
2244d5e560d3SJason Gunthorpe 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2245d5e560d3SJason Gunthorpe 					 PAGE_SIZE,
2246c043ff2cSMichal Kalderon 					 pgprot_noncached(vma->vm_page_prot),
2247c043ff2cSMichal Kalderon 					 NULL);
22485c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
22495c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2250d69e3bcfSMatan Barak 
225124da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
225224da0016SAriel Levkovich 		return dm_mmap(ibcontext, vma);
225324da0016SAriel Levkovich 
2254e126ba97SEli Cohen 	default:
2255e126ba97SEli Cohen 		return -EINVAL;
2256e126ba97SEli Cohen 	}
2257e126ba97SEli Cohen 
2258e126ba97SEli Cohen 	return 0;
2259e126ba97SEli Cohen }
2260e126ba97SEli Cohen 
226125c13324SAriel Levkovich static inline int check_dm_type_support(struct mlx5_ib_dev *dev,
226225c13324SAriel Levkovich 					u32 type)
226325c13324SAriel Levkovich {
226425c13324SAriel Levkovich 	switch (type) {
226525c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
226625c13324SAriel Levkovich 		if (!MLX5_CAP_DEV_MEM(dev->mdev, memic))
226725c13324SAriel Levkovich 			return -EOPNOTSUPP;
226825c13324SAriel Levkovich 		break;
226925c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2270c9b9dcb4SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
227125c13324SAriel Levkovich 		if (!capable(CAP_SYS_RAWIO) ||
227225c13324SAriel Levkovich 		    !capable(CAP_NET_RAW))
227325c13324SAriel Levkovich 			return -EPERM;
227425c13324SAriel Levkovich 
227525c13324SAriel Levkovich 		if (!(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner) ||
227625c13324SAriel Levkovich 		      MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner)))
227725c13324SAriel Levkovich 			return -EOPNOTSUPP;
227825c13324SAriel Levkovich 		break;
227925c13324SAriel Levkovich 	}
228025c13324SAriel Levkovich 
228125c13324SAriel Levkovich 	return 0;
228225c13324SAriel Levkovich }
228325c13324SAriel Levkovich 
22843b113a1eSAriel Levkovich static int handle_alloc_dm_memic(struct ib_ucontext *ctx,
22853b113a1eSAriel Levkovich 				 struct mlx5_ib_dm *dm,
228624da0016SAriel Levkovich 				 struct ib_dm_alloc_attr *attr,
228724da0016SAriel Levkovich 				 struct uverbs_attr_bundle *attrs)
228824da0016SAriel Levkovich {
22893b113a1eSAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
229024da0016SAriel Levkovich 	u64 start_offset;
229124da0016SAriel Levkovich 	u32 page_idx;
229224da0016SAriel Levkovich 	int err;
229324da0016SAriel Levkovich 
22943b113a1eSAriel Levkovich 	dm->size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
229524da0016SAriel Levkovich 
22963b113a1eSAriel Levkovich 	err = mlx5_cmd_alloc_memic(dm_db, &dm->dev_addr,
22973b113a1eSAriel Levkovich 				   dm->size, attr->alignment);
229824da0016SAriel Levkovich 	if (err)
22993b113a1eSAriel Levkovich 		return err;
230024da0016SAriel Levkovich 
23013b113a1eSAriel Levkovich 	page_idx = (dm->dev_addr - pci_resource_start(dm_db->dev->pdev, 0) -
23023b113a1eSAriel Levkovich 		    MLX5_CAP64_DEV_MEM(dm_db->dev, memic_bar_start_addr)) >>
230324da0016SAriel Levkovich 		    PAGE_SHIFT;
230424da0016SAriel Levkovich 
230524da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
230624da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
230724da0016SAriel Levkovich 			     &page_idx, sizeof(page_idx));
230824da0016SAriel Levkovich 	if (err)
230924da0016SAriel Levkovich 		goto err_dealloc;
231024da0016SAriel Levkovich 
23113b113a1eSAriel Levkovich 	start_offset = dm->dev_addr & ~PAGE_MASK;
23123b113a1eSAriel Levkovich 	err = uverbs_copy_to(attrs,
23133b113a1eSAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
23143b113a1eSAriel Levkovich 			     &start_offset, sizeof(start_offset));
23153b113a1eSAriel Levkovich 	if (err)
23163b113a1eSAriel Levkovich 		goto err_dealloc;
231724da0016SAriel Levkovich 
23183b113a1eSAriel Levkovich 	bitmap_set(to_mucontext(ctx)->dm_pages, page_idx,
23193b113a1eSAriel Levkovich 		   DIV_ROUND_UP(dm->size, PAGE_SIZE));
23203b113a1eSAriel Levkovich 
23213b113a1eSAriel Levkovich 	return 0;
23223b113a1eSAriel Levkovich 
23233b113a1eSAriel Levkovich err_dealloc:
23243b113a1eSAriel Levkovich 	mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
23253b113a1eSAriel Levkovich 
23263b113a1eSAriel Levkovich 	return err;
23273b113a1eSAriel Levkovich }
23283b113a1eSAriel Levkovich 
232925c13324SAriel Levkovich static int handle_alloc_dm_sw_icm(struct ib_ucontext *ctx,
233025c13324SAriel Levkovich 				  struct mlx5_ib_dm *dm,
233125c13324SAriel Levkovich 				  struct ib_dm_alloc_attr *attr,
233225c13324SAriel Levkovich 				  struct uverbs_attr_bundle *attrs,
233325c13324SAriel Levkovich 				  int type)
233425c13324SAriel Levkovich {
2335c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ctx->device)->mdev;
233625c13324SAriel Levkovich 	u64 act_size;
233725c13324SAriel Levkovich 	int err;
233825c13324SAriel Levkovich 
233925c13324SAriel Levkovich 	/* Allocation size must a multiple of the basic block size
234025c13324SAriel Levkovich 	 * and a power of 2.
234125c13324SAriel Levkovich 	 */
2342c9b9dcb4SAriel Levkovich 	act_size = round_up(attr->length, MLX5_SW_ICM_BLOCK_SIZE(dev));
234325c13324SAriel Levkovich 	act_size = roundup_pow_of_two(act_size);
234425c13324SAriel Levkovich 
234525c13324SAriel Levkovich 	dm->size = act_size;
2346c9b9dcb4SAriel Levkovich 	err = mlx5_dm_sw_icm_alloc(dev, type, act_size,
234725c13324SAriel Levkovich 				   to_mucontext(ctx)->devx_uid, &dm->dev_addr,
234825c13324SAriel Levkovich 				   &dm->icm_dm.obj_id);
234925c13324SAriel Levkovich 	if (err)
235025c13324SAriel Levkovich 		return err;
235125c13324SAriel Levkovich 
235225c13324SAriel Levkovich 	err = uverbs_copy_to(attrs,
235325c13324SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
235425c13324SAriel Levkovich 			     &dm->dev_addr, sizeof(dm->dev_addr));
235525c13324SAriel Levkovich 	if (err)
2356c9b9dcb4SAriel Levkovich 		mlx5_dm_sw_icm_dealloc(dev, type, dm->size,
2357c9b9dcb4SAriel Levkovich 				       to_mucontext(ctx)->devx_uid, dm->dev_addr,
2358c9b9dcb4SAriel Levkovich 				       dm->icm_dm.obj_id);
235925c13324SAriel Levkovich 
236025c13324SAriel Levkovich 	return err;
236125c13324SAriel Levkovich }
236225c13324SAriel Levkovich 
23633b113a1eSAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
23643b113a1eSAriel Levkovich 			       struct ib_ucontext *context,
23653b113a1eSAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
23663b113a1eSAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
23673b113a1eSAriel Levkovich {
23683b113a1eSAriel Levkovich 	struct mlx5_ib_dm *dm;
23693b113a1eSAriel Levkovich 	enum mlx5_ib_uapi_dm_type type;
23703b113a1eSAriel Levkovich 	int err;
23713b113a1eSAriel Levkovich 
23723b113a1eSAriel Levkovich 	err = uverbs_get_const_default(&type, attrs,
23733b113a1eSAriel Levkovich 				       MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
23743b113a1eSAriel Levkovich 				       MLX5_IB_UAPI_DM_TYPE_MEMIC);
23753b113a1eSAriel Levkovich 	if (err)
23763b113a1eSAriel Levkovich 		return ERR_PTR(err);
23773b113a1eSAriel Levkovich 
23783b113a1eSAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_dm req: dm_type=%d user_length=0x%llx log_alignment=%d\n",
23793b113a1eSAriel Levkovich 		    type, attr->length, attr->alignment);
23803b113a1eSAriel Levkovich 
238125c13324SAriel Levkovich 	err = check_dm_type_support(to_mdev(ibdev), type);
238225c13324SAriel Levkovich 	if (err)
238325c13324SAriel Levkovich 		return ERR_PTR(err);
238425c13324SAriel Levkovich 
23853b113a1eSAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
23863b113a1eSAriel Levkovich 	if (!dm)
23873b113a1eSAriel Levkovich 		return ERR_PTR(-ENOMEM);
23883b113a1eSAriel Levkovich 
23893b113a1eSAriel Levkovich 	dm->type = type;
23903b113a1eSAriel Levkovich 
23913b113a1eSAriel Levkovich 	switch (type) {
23923b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
23933b113a1eSAriel Levkovich 		err = handle_alloc_dm_memic(context, dm,
23943b113a1eSAriel Levkovich 					    attr,
23953b113a1eSAriel Levkovich 					    attrs);
23963b113a1eSAriel Levkovich 		break;
239725c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2398c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2399c9b9dcb4SAriel Levkovich 					     attr, attrs,
2400c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_STEERING);
2401c9b9dcb4SAriel Levkovich 		break;
240225c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2403c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2404c9b9dcb4SAriel Levkovich 					     attr, attrs,
2405c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_HEADER_MODIFY);
240625c13324SAriel Levkovich 		break;
24073b113a1eSAriel Levkovich 	default:
24083b113a1eSAriel Levkovich 		err = -EOPNOTSUPP;
24093b113a1eSAriel Levkovich 	}
24103b113a1eSAriel Levkovich 
24113b113a1eSAriel Levkovich 	if (err)
24123b113a1eSAriel Levkovich 		goto err_free;
241324da0016SAriel Levkovich 
241424da0016SAriel Levkovich 	return &dm->ibdm;
241524da0016SAriel Levkovich 
241624da0016SAriel Levkovich err_free:
241724da0016SAriel Levkovich 	kfree(dm);
241824da0016SAriel Levkovich 	return ERR_PTR(err);
241924da0016SAriel Levkovich }
242024da0016SAriel Levkovich 
2421c4367a26SShamir Rabinovitch int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
242224da0016SAriel Levkovich {
242325c13324SAriel Levkovich 	struct mlx5_ib_ucontext *ctx = rdma_udata_to_drv_context(
242425c13324SAriel Levkovich 		&attrs->driver_udata, struct mlx5_ib_ucontext, ibucontext);
2425c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ibdm->device)->mdev;
24263b113a1eSAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ibdm->device)->dm;
242724da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
242824da0016SAriel Levkovich 	u32 page_idx;
242924da0016SAriel Levkovich 	int ret;
243024da0016SAriel Levkovich 
24313b113a1eSAriel Levkovich 	switch (dm->type) {
24323b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
24333b113a1eSAriel Levkovich 		ret = mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
243424da0016SAriel Levkovich 		if (ret)
243524da0016SAriel Levkovich 			return ret;
243624da0016SAriel Levkovich 
2437c9b9dcb4SAriel Levkovich 		page_idx = (dm->dev_addr - pci_resource_start(dev->pdev, 0) -
2438c9b9dcb4SAriel Levkovich 			    MLX5_CAP64_DEV_MEM(dev, memic_bar_start_addr)) >>
243924da0016SAriel Levkovich 			    PAGE_SHIFT;
244025c13324SAriel Levkovich 		bitmap_clear(ctx->dm_pages, page_idx,
244125c13324SAriel Levkovich 			     DIV_ROUND_UP(dm->size, PAGE_SIZE));
244225c13324SAriel Levkovich 		break;
244325c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2444c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_STEERING,
2445c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
2446c9b9dcb4SAriel Levkovich 					     dm->icm_dm.obj_id);
2447c9b9dcb4SAriel Levkovich 		if (ret)
2448c9b9dcb4SAriel Levkovich 			return ret;
2449c9b9dcb4SAriel Levkovich 		break;
245025c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2451c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_HEADER_MODIFY,
2452c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
245325c13324SAriel Levkovich 					     dm->icm_dm.obj_id);
245425c13324SAriel Levkovich 		if (ret)
245525c13324SAriel Levkovich 			return ret;
24563b113a1eSAriel Levkovich 		break;
24573b113a1eSAriel Levkovich 	default:
24583b113a1eSAriel Levkovich 		return -EOPNOTSUPP;
24593b113a1eSAriel Levkovich 	}
246024da0016SAriel Levkovich 
246124da0016SAriel Levkovich 	kfree(dm);
246224da0016SAriel Levkovich 
246324da0016SAriel Levkovich 	return 0;
246424da0016SAriel Levkovich }
246524da0016SAriel Levkovich 
2466ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2467e126ba97SEli Cohen {
246821a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
246921a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2470e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2471e126ba97SEli Cohen 	int err;
2472a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2473a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)]   = {};
2474a1069c1cSYishai Hadas 	u16 uid = 0;
2475ff23dfa1SShamir Rabinovitch 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2476ff23dfa1SShamir Rabinovitch 		udata, struct mlx5_ib_ucontext, ibucontext);
2477e126ba97SEli Cohen 
2478ff23dfa1SShamir Rabinovitch 	uid = context ? context->devx_uid : 0;
2479a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2480a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
2481a1069c1cSYishai Hadas 	err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2482a1069c1cSYishai Hadas 			    out, sizeof(out));
248321a428a0SLeon Romanovsky 	if (err)
248421a428a0SLeon Romanovsky 		return err;
2485e126ba97SEli Cohen 
2486a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2487a1069c1cSYishai Hadas 	pd->uid = uid;
2488ff23dfa1SShamir Rabinovitch 	if (udata) {
2489e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2490e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2491a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
249221a428a0SLeon Romanovsky 			return -EFAULT;
2493e126ba97SEli Cohen 		}
2494e126ba97SEli Cohen 	}
2495e126ba97SEli Cohen 
249621a428a0SLeon Romanovsky 	return 0;
2497e126ba97SEli Cohen }
2498e126ba97SEli Cohen 
2499c4367a26SShamir Rabinovitch static void mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2500e126ba97SEli Cohen {
2501e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2502e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2503e126ba97SEli Cohen 
2504a1069c1cSYishai Hadas 	mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2505e126ba97SEli Cohen }
2506e126ba97SEli Cohen 
2507466fa6d2SMaor Gottlieb enum {
2508466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2509466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
251071c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_INNER_BIT,
251171c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_MISC2_BIT
2512466fa6d2SMaor Gottlieb };
2513038d2ef8SMaor Gottlieb 
2514466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2515466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2516466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2517466fa6d2SMaor Gottlieb 
2518466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2519466fa6d2SMaor Gottlieb {
2520466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2521466fa6d2SMaor Gottlieb 
2522466fa6d2SMaor Gottlieb 	match_criteria_enable =
2523466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2524466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2525466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2526466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2527466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2528466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2529466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2530466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
253171c6e863SAriel Levkovich 	match_criteria_enable |=
253271c6e863SAriel Levkovich 		(!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
253371c6e863SAriel Levkovich 		MATCH_CRITERIA_ENABLE_MISC2_BIT;
2534466fa6d2SMaor Gottlieb 
2535466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2536038d2ef8SMaor Gottlieb }
2537038d2ef8SMaor Gottlieb 
25386113cc44SMaor Gottlieb static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2539ca0d4753SMaor Gottlieb {
25406113cc44SMaor Gottlieb 	u8 entry_mask;
25416113cc44SMaor Gottlieb 	u8 entry_val;
25426113cc44SMaor Gottlieb 	int err = 0;
25436113cc44SMaor Gottlieb 
25446113cc44SMaor Gottlieb 	if (!mask)
25456113cc44SMaor Gottlieb 		goto out;
25466113cc44SMaor Gottlieb 
25476113cc44SMaor Gottlieb 	entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
25486113cc44SMaor Gottlieb 			      ip_protocol);
25496113cc44SMaor Gottlieb 	entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
25506113cc44SMaor Gottlieb 			     ip_protocol);
25516113cc44SMaor Gottlieb 	if (!entry_mask) {
2552ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2553ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
25546113cc44SMaor Gottlieb 		goto out;
25556113cc44SMaor Gottlieb 	}
25566113cc44SMaor Gottlieb 	/* Don't override existing ip protocol */
25576113cc44SMaor Gottlieb 	if (mask != entry_mask || val != entry_val)
25586113cc44SMaor Gottlieb 		err = -EINVAL;
25596113cc44SMaor Gottlieb out:
25606113cc44SMaor Gottlieb 	return err;
2561ca0d4753SMaor Gottlieb }
2562ca0d4753SMaor Gottlieb 
256337da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
25642d1e697eSMoses Reuben 			   bool inner)
25652d1e697eSMoses Reuben {
25662d1e697eSMoses Reuben 	if (inner) {
25672d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
25682d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
25692d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
25702d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
25712d1e697eSMoses Reuben 	} else {
25722d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
25732d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
25742d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
25752d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
25762d1e697eSMoses Reuben 	}
25772d1e697eSMoses Reuben }
25782d1e697eSMoses Reuben 
2579ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2580ca0d4753SMaor Gottlieb {
2581ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2582ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2583ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2584ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2585ca0d4753SMaor Gottlieb }
2586ca0d4753SMaor Gottlieb 
258771c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
258871c6e863SAriel Levkovich {
258971c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
259071c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
259171c6e863SAriel Levkovich 		return -EOPNOTSUPP;
259271c6e863SAriel Levkovich 
259371c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
259471c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
259571c6e863SAriel Levkovich 		return -EOPNOTSUPP;
259671c6e863SAriel Levkovich 
259771c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
259871c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
259971c6e863SAriel Levkovich 		return -EOPNOTSUPP;
260071c6e863SAriel Levkovich 
260171c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
260271c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
260371c6e863SAriel Levkovich 		return -EOPNOTSUPP;
260471c6e863SAriel Levkovich 
260571c6e863SAriel Levkovich 	return 0;
260671c6e863SAriel Levkovich }
260771c6e863SAriel Levkovich 
2608c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2609c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2610ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2611466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2612c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2613ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
26142ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2615a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
26163b3233fbSRaed Salem #define LAST_COUNTERS_FIELD counters
2617c47ac6aeSMaor Gottlieb 
2618c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2619c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2620c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2621c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2622c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2623c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2624c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2625c47ac6aeSMaor Gottlieb 
26262ea26203SMark Bloch int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
26272ea26203SMark Bloch 			   bool is_egress,
2628802c2125SAviad Yehezkel 			   struct mlx5_flow_act *action)
2629802c2125SAviad Yehezkel {
2630802c2125SAviad Yehezkel 
2631802c2125SAviad Yehezkel 	switch (maction->ib_action.type) {
2632802c2125SAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
2633501f14e3SMark Bloch 		if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2634501f14e3SMark Bloch 				      MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2635501f14e3SMark Bloch 			return -EINVAL;
2636802c2125SAviad Yehezkel 		/* Currently only AES_GCM keymat is supported by the driver */
2637802c2125SAviad Yehezkel 		action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
26382ea26203SMark Bloch 		action->action |= is_egress ?
2639802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2640802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2641802c2125SAviad Yehezkel 		return 0;
2642b1085be3SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
2643b1085be3SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2644b1085be3SMark Bloch 		    MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
2645501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2646501f14e3SMark Bloch 				return -EINVAL;
2647b1085be3SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
26482b688ea5SMaor Gottlieb 			action->modify_hdr =
26492b688ea5SMaor Gottlieb 				maction->flow_action_raw.modify_hdr;
2650b1085be3SMark Bloch 			return 0;
2651b1085be3SMark Bloch 		}
265210a30896SMark Bloch 		if (maction->flow_action_raw.sub_type ==
265310a30896SMark Bloch 		    MLX5_IB_FLOW_ACTION_DECAP) {
2654501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2655501f14e3SMark Bloch 				return -EINVAL;
265610a30896SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
265710a30896SMark Bloch 			return 0;
265810a30896SMark Bloch 		}
2659e806f932SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2660e806f932SMark Bloch 		    MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
2661501f14e3SMark Bloch 			if (action->action &
2662501f14e3SMark Bloch 			    MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2663501f14e3SMark Bloch 				return -EINVAL;
2664e806f932SMark Bloch 			action->action |=
2665e806f932SMark Bloch 				MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
26662b688ea5SMaor Gottlieb 			action->pkt_reformat =
26672b688ea5SMaor Gottlieb 				maction->flow_action_raw.pkt_reformat;
2668e806f932SMark Bloch 			return 0;
2669e806f932SMark Bloch 		}
2670b1085be3SMark Bloch 		/* fall through */
2671802c2125SAviad Yehezkel 	default:
2672802c2125SAviad Yehezkel 		return -EOPNOTSUPP;
2673802c2125SAviad Yehezkel 	}
2674802c2125SAviad Yehezkel }
2675802c2125SAviad Yehezkel 
2676bb0ee7dcSJianbo Liu static int parse_flow_attr(struct mlx5_core_dev *mdev,
2677bb0ee7dcSJianbo Liu 			   struct mlx5_flow_spec *spec,
2678bb0ee7dcSJianbo Liu 			   const union ib_flow_spec *ib_spec,
2679802c2125SAviad Yehezkel 			   const struct ib_flow_attr *flow_attr,
268071c6e863SAriel Levkovich 			   struct mlx5_flow_act *action, u32 prev_type)
2681038d2ef8SMaor Gottlieb {
2682bb0ee7dcSJianbo Liu 	struct mlx5_flow_context *flow_context = &spec->flow_context;
2683bb0ee7dcSJianbo Liu 	u32 *match_c = spec->match_criteria;
2684bb0ee7dcSJianbo Liu 	u32 *match_v = spec->match_value;
2685466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2686466fa6d2SMaor Gottlieb 					   misc_parameters);
2687466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2688466fa6d2SMaor Gottlieb 					   misc_parameters);
268971c6e863SAriel Levkovich 	void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
269071c6e863SAriel Levkovich 					    misc_parameters_2);
269171c6e863SAriel Levkovich 	void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
269271c6e863SAriel Levkovich 					    misc_parameters_2);
26932d1e697eSMoses Reuben 	void *headers_c;
26942d1e697eSMoses Reuben 	void *headers_v;
269519cc7524SAriel Levkovich 	int match_ipv;
2696802c2125SAviad Yehezkel 	int ret;
2697466fa6d2SMaor Gottlieb 
26982d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
26992d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
27002d1e697eSMoses Reuben 					 inner_headers);
27012d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
27022d1e697eSMoses Reuben 					 inner_headers);
270319cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
270419cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
27052d1e697eSMoses Reuben 	} else {
27062d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
27072d1e697eSMoses Reuben 					 outer_headers);
27082d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
27092d1e697eSMoses Reuben 					 outer_headers);
271019cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
271119cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
27122d1e697eSMoses Reuben 	}
27132d1e697eSMoses Reuben 
27142d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2715038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2716c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
27171ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2718038d2ef8SMaor Gottlieb 
27192d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2720038d2ef8SMaor Gottlieb 					     dmac_47_16),
2721038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
27222d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2723038d2ef8SMaor Gottlieb 					     dmac_47_16),
2724038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2725038d2ef8SMaor Gottlieb 
27262d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2727ee3da804SMaor Gottlieb 					     smac_47_16),
2728ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
27292d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2730ee3da804SMaor Gottlieb 					     smac_47_16),
2731ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2732ee3da804SMaor Gottlieb 
2733038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
27342d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
273510543365SMohamad Haj Yahia 				 cvlan_tag, 1);
27362d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
273710543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2738038d2ef8SMaor Gottlieb 
27392d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2740038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
27412d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2742038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2743038d2ef8SMaor Gottlieb 
27442d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2745038d2ef8SMaor Gottlieb 				 first_cfi,
2746038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
27472d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2748038d2ef8SMaor Gottlieb 				 first_cfi,
2749038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2750038d2ef8SMaor Gottlieb 
27512d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2752038d2ef8SMaor Gottlieb 				 first_prio,
2753038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
27542d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2755038d2ef8SMaor Gottlieb 				 first_prio,
2756038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2757038d2ef8SMaor Gottlieb 		}
27582d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2759038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
27602d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2761038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2762038d2ef8SMaor Gottlieb 		break;
2763038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2764c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
27651ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2766038d2ef8SMaor Gottlieb 
276719cc7524SAriel Levkovich 		if (match_ipv) {
276819cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
276919cc7524SAriel Levkovich 				 ip_version, 0xf);
277019cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
27713346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV4_VERSION);
277219cc7524SAriel Levkovich 		} else {
27732d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2774038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
27752d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2776038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
277719cc7524SAriel Levkovich 		}
2778038d2ef8SMaor Gottlieb 
27792d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2780038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2781038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2782038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
27832d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2784038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2785038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2786038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
27872d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2788038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2789038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2790038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
27912d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2792038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2793038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2794038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2795ca0d4753SMaor Gottlieb 
27962d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2797ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2798ca0d4753SMaor Gottlieb 
27996113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
28006113cc44SMaor Gottlieb 			      ib_spec->ipv4.mask.proto,
28016113cc44SMaor Gottlieb 			      ib_spec->ipv4.val.proto))
28026113cc44SMaor Gottlieb 			return -EINVAL;
2803038d2ef8SMaor Gottlieb 		break;
2804026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2805c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
28061ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2807026bae0cSMaor Gottlieb 
280819cc7524SAriel Levkovich 		if (match_ipv) {
280919cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
281019cc7524SAriel Levkovich 				 ip_version, 0xf);
281119cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
28123346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV6_VERSION);
281319cc7524SAriel Levkovich 		} else {
28142d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2815026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
28162d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2817026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
281819cc7524SAriel Levkovich 		}
2819026bae0cSMaor Gottlieb 
28202d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2821026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2822026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2823026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
28242d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2825026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2826026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2827026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
28282d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2829026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2830026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2831026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
28322d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2833026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2834026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2835026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2836466fa6d2SMaor Gottlieb 
28372d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2838466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2839466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2840466fa6d2SMaor Gottlieb 
28416113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
2842466fa6d2SMaor Gottlieb 			      ib_spec->ipv6.mask.next_hdr,
28436113cc44SMaor Gottlieb 			      ib_spec->ipv6.val.next_hdr))
28446113cc44SMaor Gottlieb 			return -EINVAL;
2845466fa6d2SMaor Gottlieb 
28462d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
28472d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
28482d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
28492d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
2850802c2125SAviad Yehezkel 		break;
2851802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ESP:
2852802c2125SAviad Yehezkel 		if (ib_spec->esp.mask.seq)
2853802c2125SAviad Yehezkel 			return -EOPNOTSUPP;
28542d1e697eSMoses Reuben 
2855802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2856802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.mask.spi));
2857802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2858802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.val.spi));
2859026bae0cSMaor Gottlieb 		break;
2860038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2861c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2862c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
28631ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2864038d2ef8SMaor Gottlieb 
28656113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
28666113cc44SMaor Gottlieb 			return -EINVAL;
2867038d2ef8SMaor Gottlieb 
28682d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2869038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
28702d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2871038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2872038d2ef8SMaor Gottlieb 
28732d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2874038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
28752d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2876038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2877038d2ef8SMaor Gottlieb 		break;
2878038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2879c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2880c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
28811ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2882038d2ef8SMaor Gottlieb 
28836113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
28846113cc44SMaor Gottlieb 			return -EINVAL;
2885038d2ef8SMaor Gottlieb 
28862d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2887038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
28882d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2889038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2890038d2ef8SMaor Gottlieb 
28912d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2892038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
28932d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2894038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2895038d2ef8SMaor Gottlieb 		break;
2896da2f22aeSAriel Levkovich 	case IB_FLOW_SPEC_GRE:
2897da2f22aeSAriel Levkovich 		if (ib_spec->gre.mask.c_ks_res0_ver)
2898da2f22aeSAriel Levkovich 			return -EOPNOTSUPP;
2899da2f22aeSAriel Levkovich 
29006113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
29016113cc44SMaor Gottlieb 			return -EINVAL;
29026113cc44SMaor Gottlieb 
2903da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2904da2f22aeSAriel Levkovich 			 0xff);
2905da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2906da2f22aeSAriel Levkovich 			 IPPROTO_GRE);
2907da2f22aeSAriel Levkovich 
2908da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
2909a93b632cSMaor Gottlieb 			 ntohs(ib_spec->gre.mask.protocol));
2910da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2911da2f22aeSAriel Levkovich 			 ntohs(ib_spec->gre.val.protocol));
2912da2f22aeSAriel Levkovich 
2913da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
29145886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2915da2f22aeSAriel Levkovich 		       &ib_spec->gre.mask.key,
2916da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.mask.key));
2917da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
29185886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2919da2f22aeSAriel Levkovich 		       &ib_spec->gre.val.key,
2920da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.val.key));
2921da2f22aeSAriel Levkovich 		break;
292271c6e863SAriel Levkovich 	case IB_FLOW_SPEC_MPLS:
292371c6e863SAriel Levkovich 		switch (prev_type) {
292471c6e863SAriel Levkovich 		case IB_FLOW_SPEC_UDP:
292571c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
292671c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_udp),
292771c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
292871c6e863SAriel Levkovich 				return -EOPNOTSUPP;
292971c6e863SAriel Levkovich 
293071c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
293171c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
293271c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
293371c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
293471c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
293571c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
293671c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
293771c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
293871c6e863SAriel Levkovich 			break;
293971c6e863SAriel Levkovich 		case IB_FLOW_SPEC_GRE:
294071c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
294171c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_gre),
294271c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
294371c6e863SAriel Levkovich 				return -EOPNOTSUPP;
294471c6e863SAriel Levkovich 
294571c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
294671c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
294771c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
294871c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
294971c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
295071c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
295171c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
295271c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
295371c6e863SAriel Levkovich 			break;
295471c6e863SAriel Levkovich 		default:
295571c6e863SAriel Levkovich 			if (ib_spec->type & IB_FLOW_SPEC_INNER) {
295671c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
295771c6e863SAriel Levkovich 							   ft_field_support.inner_first_mpls),
295871c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
295971c6e863SAriel Levkovich 					return -EOPNOTSUPP;
296071c6e863SAriel Levkovich 
296171c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
296271c6e863SAriel Levkovich 						    inner_first_mpls),
296371c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
296471c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
296571c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
296671c6e863SAriel Levkovich 						    inner_first_mpls),
296771c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
296871c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
296971c6e863SAriel Levkovich 			} else {
297071c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
297171c6e863SAriel Levkovich 							   ft_field_support.outer_first_mpls),
297271c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
297371c6e863SAriel Levkovich 					return -EOPNOTSUPP;
297471c6e863SAriel Levkovich 
297571c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
297671c6e863SAriel Levkovich 						    outer_first_mpls),
297771c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
297871c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
297971c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
298071c6e863SAriel Levkovich 						    outer_first_mpls),
298171c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
298271c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
298371c6e863SAriel Levkovich 			}
298471c6e863SAriel Levkovich 		}
298571c6e863SAriel Levkovich 		break;
2986ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2987ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2988ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
29891ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2990ffb30d8fSMoses Reuben 
2991ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2992ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2993ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2994ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2995ffb30d8fSMoses Reuben 		break;
29962ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
29972ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
29982ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
29992ac693f9SMoses Reuben 			return -EOPNOTSUPP;
30002ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
30012ac693f9SMoses Reuben 			return -EINVAL;
30022ac693f9SMoses Reuben 
3003bb0ee7dcSJianbo Liu 		flow_context->flow_tag = ib_spec->flow_tag.tag_id;
3004bb0ee7dcSJianbo Liu 		flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
30052ac693f9SMoses Reuben 		break;
3006a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
3007a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
3008a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
3009a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
3010075572d4SBoris Pismenny 		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
3011a22ed86cSSlava Shwartsman 		break;
3012802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ACTION_HANDLE:
30132ea26203SMark Bloch 		ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
30142ea26203SMark Bloch 			flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
3015802c2125SAviad Yehezkel 		if (ret)
3016802c2125SAviad Yehezkel 			return ret;
3017802c2125SAviad Yehezkel 		break;
30183b3233fbSRaed Salem 	case IB_FLOW_SPEC_ACTION_COUNT:
30193b3233fbSRaed Salem 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
30203b3233fbSRaed Salem 					 LAST_COUNTERS_FIELD))
30213b3233fbSRaed Salem 			return -EOPNOTSUPP;
30223b3233fbSRaed Salem 
30233b3233fbSRaed Salem 		/* for now support only one counters spec per flow */
30243b3233fbSRaed Salem 		if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
30253b3233fbSRaed Salem 			return -EINVAL;
30263b3233fbSRaed Salem 
30273b3233fbSRaed Salem 		action->counters = ib_spec->flow_count.counters;
30283b3233fbSRaed Salem 		action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
30293b3233fbSRaed Salem 		break;
3030038d2ef8SMaor Gottlieb 	default:
3031038d2ef8SMaor Gottlieb 		return -EINVAL;
3032038d2ef8SMaor Gottlieb 	}
3033038d2ef8SMaor Gottlieb 
3034038d2ef8SMaor Gottlieb 	return 0;
3035038d2ef8SMaor Gottlieb }
3036038d2ef8SMaor Gottlieb 
3037038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
3038038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
3039038d2ef8SMaor Gottlieb  * could steal other multicast packets.
3040038d2ef8SMaor Gottlieb  */
3041a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
3042038d2ef8SMaor Gottlieb {
304381e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
3044038d2ef8SMaor Gottlieb 
3045038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
3046038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
3047038d2ef8SMaor Gottlieb 		return false;
3048038d2ef8SMaor Gottlieb 
304981e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
305081e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
305181e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
3052038d2ef8SMaor Gottlieb 
305381e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
305481e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
305581e30880SYishai Hadas 			return true;
305681e30880SYishai Hadas 
305781e30880SYishai Hadas 		return false;
305881e30880SYishai Hadas 	}
305981e30880SYishai Hadas 
306081e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
306181e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
306281e30880SYishai Hadas 
306381e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
3064038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
3065038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
3066038d2ef8SMaor Gottlieb 	}
3067038d2ef8SMaor Gottlieb 
306881e30880SYishai Hadas 	return false;
306981e30880SYishai Hadas }
307081e30880SYishai Hadas 
3071802c2125SAviad Yehezkel enum valid_spec {
3072802c2125SAviad Yehezkel 	VALID_SPEC_INVALID,
3073802c2125SAviad Yehezkel 	VALID_SPEC_VALID,
3074802c2125SAviad Yehezkel 	VALID_SPEC_NA,
3075802c2125SAviad Yehezkel };
3076802c2125SAviad Yehezkel 
3077802c2125SAviad Yehezkel static enum valid_spec
3078802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
3079802c2125SAviad Yehezkel 		     const struct mlx5_flow_spec *spec,
3080802c2125SAviad Yehezkel 		     const struct mlx5_flow_act *flow_act,
3081802c2125SAviad Yehezkel 		     bool egress)
3082802c2125SAviad Yehezkel {
3083802c2125SAviad Yehezkel 	const u32 *match_c = spec->match_criteria;
3084802c2125SAviad Yehezkel 	bool is_crypto =
3085802c2125SAviad Yehezkel 		(flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
3086802c2125SAviad Yehezkel 				     MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
3087802c2125SAviad Yehezkel 	bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
3088802c2125SAviad Yehezkel 	bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
3089802c2125SAviad Yehezkel 
3090802c2125SAviad Yehezkel 	/*
3091802c2125SAviad Yehezkel 	 * Currently only crypto is supported in egress, when regular egress
3092802c2125SAviad Yehezkel 	 * rules would be supported, always return VALID_SPEC_NA.
3093802c2125SAviad Yehezkel 	 */
3094802c2125SAviad Yehezkel 	if (!is_crypto)
309578dd0c43SMark Bloch 		return VALID_SPEC_NA;
3096802c2125SAviad Yehezkel 
3097802c2125SAviad Yehezkel 	return is_crypto && is_ipsec &&
3098bb0ee7dcSJianbo Liu 		(!egress || (!is_drop &&
3099bb0ee7dcSJianbo Liu 			     !(spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG))) ?
3100802c2125SAviad Yehezkel 		VALID_SPEC_VALID : VALID_SPEC_INVALID;
3101802c2125SAviad Yehezkel }
3102802c2125SAviad Yehezkel 
3103802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev,
3104802c2125SAviad Yehezkel 			  const struct mlx5_flow_spec *spec,
3105802c2125SAviad Yehezkel 			  const struct mlx5_flow_act *flow_act,
3106802c2125SAviad Yehezkel 			  bool egress)
3107802c2125SAviad Yehezkel {
3108802c2125SAviad Yehezkel 	/* We curretly only support ipsec egress flow */
3109802c2125SAviad Yehezkel 	return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
3110802c2125SAviad Yehezkel }
3111802c2125SAviad Yehezkel 
311219cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
311319cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
31140f750966SAriel Levkovich 			       bool check_inner)
3115038d2ef8SMaor Gottlieb {
3116038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
311719cc7524SAriel Levkovich 	int match_ipv = check_inner ?
311819cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
311919cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
312019cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
312119cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
31220f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
31230f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
31240f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
31250f750966SAriel Levkovich 	bool has_ethertype = false;
3126038d2ef8SMaor Gottlieb 	unsigned int spec_index;
31270f750966SAriel Levkovich 	bool mask_valid = true;
31280f750966SAriel Levkovich 	u16 eth_type = 0;
31290f750966SAriel Levkovich 	bool type_valid;
3130038d2ef8SMaor Gottlieb 
3131038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
3132038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
31330f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
3134038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
31350f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
31360f750966SAriel Levkovich 				      htons(0xffff));
31370f750966SAriel Levkovich 			has_ethertype = true;
31380f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
31390f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
31400f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
31410f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
3142038d2ef8SMaor Gottlieb 		}
3143038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
3144038d2ef8SMaor Gottlieb 	}
31450f750966SAriel Levkovich 
31460f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
31470f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
31480f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
31490f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
31500f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
31510f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
315219cc7524SAriel Levkovich 
315319cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
315419cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
315519cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
31560f750966SAriel Levkovich 	}
31570f750966SAriel Levkovich 
31580f750966SAriel Levkovich 	return type_valid;
31590f750966SAriel Levkovich }
31600f750966SAriel Levkovich 
316119cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
316219cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
31630f750966SAriel Levkovich {
316419cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
316519cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
3166038d2ef8SMaor Gottlieb }
3167038d2ef8SMaor Gottlieb 
3168038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
3169038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
3170038d2ef8SMaor Gottlieb {
3171038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
3172038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
3173038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
3174038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
3175038d2ef8SMaor Gottlieb 	}
3176038d2ef8SMaor Gottlieb }
3177038d2ef8SMaor Gottlieb 
31783b3233fbSRaed Salem static void counters_clear_description(struct ib_counters *counters)
31793b3233fbSRaed Salem {
31803b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
31813b3233fbSRaed Salem 
31823b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
31833b3233fbSRaed Salem 	kfree(mcounters->counters_data);
31843b3233fbSRaed Salem 	mcounters->counters_data = NULL;
31853b3233fbSRaed Salem 	mcounters->cntrs_max_index = 0;
31863b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
31873b3233fbSRaed Salem }
31883b3233fbSRaed Salem 
3189038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3190038d2ef8SMaor Gottlieb {
3191038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3192038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
3193038d2ef8SMaor Gottlieb 							  ibflow);
3194038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
3195d4be3f44SYishai Hadas 	struct mlx5_ib_dev *dev = handler->dev;
3196038d2ef8SMaor Gottlieb 
31979a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3198038d2ef8SMaor Gottlieb 
3199038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
320074491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
3201cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
3202038d2ef8SMaor Gottlieb 		list_del(&iter->list);
3203038d2ef8SMaor Gottlieb 		kfree(iter);
3204038d2ef8SMaor Gottlieb 	}
3205038d2ef8SMaor Gottlieb 
320674491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
32075497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
32083b3233fbSRaed Salem 	if (handler->ibcounters &&
32093b3233fbSRaed Salem 	    atomic_read(&handler->ibcounters->usecnt) == 1)
32103b3233fbSRaed Salem 		counters_clear_description(handler->ibcounters);
3211038d2ef8SMaor Gottlieb 
32123b3233fbSRaed Salem 	mutex_unlock(&dev->flow_db->lock);
3213d4be3f44SYishai Hadas 	if (handler->flow_matcher)
3214d4be3f44SYishai Hadas 		atomic_dec(&handler->flow_matcher->usecnt);
3215038d2ef8SMaor Gottlieb 	kfree(handler);
3216038d2ef8SMaor Gottlieb 
3217038d2ef8SMaor Gottlieb 	return 0;
3218038d2ef8SMaor Gottlieb }
3219038d2ef8SMaor Gottlieb 
322035d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
322135d19011SMaor Gottlieb {
322235d19011SMaor Gottlieb 	priority *= 2;
322335d19011SMaor Gottlieb 	if (!dont_trap)
322435d19011SMaor Gottlieb 		priority++;
322535d19011SMaor Gottlieb 	return priority;
322635d19011SMaor Gottlieb }
322735d19011SMaor Gottlieb 
3228cc0e5d42SMaor Gottlieb enum flow_table_type {
3229cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
3230cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
3231cc0e5d42SMaor Gottlieb };
3232cc0e5d42SMaor Gottlieb 
323300b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
323400b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
3235d4be3f44SYishai Hadas 
3236d4be3f44SYishai Hadas static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3237d4be3f44SYishai Hadas 					   struct mlx5_ib_flow_prio *prio,
3238d4be3f44SYishai Hadas 					   int priority,
32394adda112SMark Bloch 					   int num_entries, int num_groups,
32404adda112SMark Bloch 					   u32 flags)
3241d4be3f44SYishai Hadas {
3242d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft;
3243d4be3f44SYishai Hadas 
3244d4be3f44SYishai Hadas 	ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3245d4be3f44SYishai Hadas 						 num_entries,
3246d4be3f44SYishai Hadas 						 num_groups,
32474adda112SMark Bloch 						 0, flags);
3248d4be3f44SYishai Hadas 	if (IS_ERR(ft))
3249d4be3f44SYishai Hadas 		return ERR_CAST(ft);
3250d4be3f44SYishai Hadas 
3251d4be3f44SYishai Hadas 	prio->flow_table = ft;
3252d4be3f44SYishai Hadas 	prio->refcount = 0;
3253d4be3f44SYishai Hadas 	return prio;
3254d4be3f44SYishai Hadas }
3255d4be3f44SYishai Hadas 
3256038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
3257cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
3258cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
3259038d2ef8SMaor Gottlieb {
326035d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
3261038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
3262038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
3263038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
3264dac388efSMaor Gottlieb 	int max_table_size;
3265038d2ef8SMaor Gottlieb 	int num_entries;
3266038d2ef8SMaor Gottlieb 	int num_groups;
3267cecae747SMaor Gottlieb 	bool esw_encap;
32684adda112SMark Bloch 	u32 flags = 0;
3269038d2ef8SMaor Gottlieb 	int priority;
3270038d2ef8SMaor Gottlieb 
3271dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3272dac388efSMaor Gottlieb 						       log_max_ft_size));
3273cecae747SMaor Gottlieb 	esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
3274cecae747SMaor Gottlieb 		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
3275038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
327678dd0c43SMark Bloch 		enum mlx5_flow_namespace_type fn_type;
327778dd0c43SMark Bloch 
327878dd0c43SMark Bloch 		if (flow_is_multicast_only(flow_attr) &&
327935d19011SMaor Gottlieb 		    !dont_trap)
3280038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
3281038d2ef8SMaor Gottlieb 		else
328235d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
328335d19011SMaor Gottlieb 							dont_trap);
328478dd0c43SMark Bloch 		if (ft_type == MLX5_IB_FT_RX) {
328578dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
328678dd0c43SMark Bloch 			prio = &dev->flow_db->prios[priority];
3287cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
32884adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
32894adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3290cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
32915c2db53fSMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
32925c2db53fSMark Bloch 					reformat_l3_tunnel_to_l2))
32935c2db53fSMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
329478dd0c43SMark Bloch 		} else {
329578dd0c43SMark Bloch 			max_table_size =
329678dd0c43SMark Bloch 				BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
329778dd0c43SMark Bloch 							      log_max_ft_size));
329878dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
329978dd0c43SMark Bloch 			prio = &dev->flow_db->egress_prios[priority];
3300cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
33014adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
33024adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
330378dd0c43SMark Bloch 		}
330478dd0c43SMark Bloch 		ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
3305038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
3306038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
3307038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3308038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3309038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
3310038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
3311038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
3312038d2ef8SMaor Gottlieb 					 &num_entries,
3313038d2ef8SMaor Gottlieb 					 &num_groups);
33149a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
3315cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3316cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3317cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
3318cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
3319cc0e5d42SMaor Gottlieb 
3320cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3321cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3322cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3323cc0e5d42SMaor Gottlieb 
33249a4ca38dSMark Bloch 		prio = &dev->flow_db->sniffer[ft_type];
3325cc0e5d42SMaor Gottlieb 		priority = 0;
3326cc0e5d42SMaor Gottlieb 		num_entries = 1;
3327cc0e5d42SMaor Gottlieb 		num_groups = 1;
3328038d2ef8SMaor Gottlieb 	}
3329038d2ef8SMaor Gottlieb 
3330038d2ef8SMaor Gottlieb 	if (!ns)
3331038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
3332038d2ef8SMaor Gottlieb 
33333b70508aSMark Bloch 	max_table_size = min_t(int, num_entries, max_table_size);
3334dac388efSMaor Gottlieb 
3335038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
3336d4be3f44SYishai Hadas 	if (!ft)
33373b70508aSMark Bloch 		return _get_prio(ns, prio, priority, max_table_size, num_groups,
33384adda112SMark Bloch 				 flags);
3339038d2ef8SMaor Gottlieb 
3340d4be3f44SYishai Hadas 	return prio;
3341038d2ef8SMaor Gottlieb }
3342038d2ef8SMaor Gottlieb 
3343a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
3344a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
3345a550ddfcSYishai Hadas 			    u32 underlay_qpn)
3346a550ddfcSYishai Hadas {
3347a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3348a550ddfcSYishai Hadas 					   spec->match_criteria,
3349a550ddfcSYishai Hadas 					   misc_parameters);
3350a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3351a550ddfcSYishai Hadas 					   misc_parameters);
3352a550ddfcSYishai Hadas 
3353a550ddfcSYishai Hadas 	if (underlay_qpn &&
3354a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3355a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
3356a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3357a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
3358a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3359a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
3360a550ddfcSYishai Hadas 	}
3361a550ddfcSYishai Hadas }
3362a550ddfcSYishai Hadas 
33635e95af5fSRaed Salem static int read_flow_counters(struct ib_device *ibdev,
33645e95af5fSRaed Salem 			      struct mlx5_read_counters_attr *read_attr)
33655e95af5fSRaed Salem {
33665e95af5fSRaed Salem 	struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
33675e95af5fSRaed Salem 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
33685e95af5fSRaed Salem 
33695e95af5fSRaed Salem 	return mlx5_fc_query(dev->mdev, fc,
33705e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_PACKETS],
33715e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_BYTES]);
33725e95af5fSRaed Salem }
33735e95af5fSRaed Salem 
33745e95af5fSRaed Salem /* flow counters currently expose two counters packets and bytes */
33755e95af5fSRaed Salem #define FLOW_COUNTERS_NUM 2
33763b3233fbSRaed Salem static int counters_set_description(struct ib_counters *counters,
33773b3233fbSRaed Salem 				    enum mlx5_ib_counters_type counters_type,
33783b3233fbSRaed Salem 				    struct mlx5_ib_flow_counters_desc *desc_data,
33793b3233fbSRaed Salem 				    u32 ncounters)
33803b3233fbSRaed Salem {
33813b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
33823b3233fbSRaed Salem 	u32 cntrs_max_index = 0;
33833b3233fbSRaed Salem 	int i;
33843b3233fbSRaed Salem 
33853b3233fbSRaed Salem 	if (counters_type != MLX5_IB_COUNTERS_FLOW)
33863b3233fbSRaed Salem 		return -EINVAL;
33873b3233fbSRaed Salem 
33883b3233fbSRaed Salem 	/* init the fields for the object */
33893b3233fbSRaed Salem 	mcounters->type = counters_type;
33905e95af5fSRaed Salem 	mcounters->read_counters = read_flow_counters;
33915e95af5fSRaed Salem 	mcounters->counters_num = FLOW_COUNTERS_NUM;
33923b3233fbSRaed Salem 	mcounters->ncounters = ncounters;
33933b3233fbSRaed Salem 	/* each counter entry have both description and index pair */
33943b3233fbSRaed Salem 	for (i = 0; i < ncounters; i++) {
33953b3233fbSRaed Salem 		if (desc_data[i].description > IB_COUNTER_BYTES)
33963b3233fbSRaed Salem 			return -EINVAL;
33973b3233fbSRaed Salem 
33983b3233fbSRaed Salem 		if (cntrs_max_index <= desc_data[i].index)
33993b3233fbSRaed Salem 			cntrs_max_index = desc_data[i].index + 1;
34003b3233fbSRaed Salem 	}
34013b3233fbSRaed Salem 
34023b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
34033b3233fbSRaed Salem 	mcounters->counters_data = desc_data;
34043b3233fbSRaed Salem 	mcounters->cntrs_max_index = cntrs_max_index;
34053b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
34063b3233fbSRaed Salem 
34073b3233fbSRaed Salem 	return 0;
34083b3233fbSRaed Salem }
34093b3233fbSRaed Salem 
34103b3233fbSRaed Salem #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
34113b3233fbSRaed Salem static int flow_counters_set_data(struct ib_counters *ibcounters,
34123b3233fbSRaed Salem 				  struct mlx5_ib_create_flow *ucmd)
34133b3233fbSRaed Salem {
34143b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
34153b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
34163b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc_data = NULL;
34173b3233fbSRaed Salem 	bool hw_hndl = false;
34183b3233fbSRaed Salem 	int ret = 0;
34193b3233fbSRaed Salem 
34203b3233fbSRaed Salem 	if (ucmd && ucmd->ncounters_data != 0) {
34213b3233fbSRaed Salem 		cntrs_data = ucmd->data;
34223b3233fbSRaed Salem 		if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
34233b3233fbSRaed Salem 			return -EINVAL;
34243b3233fbSRaed Salem 
34253b3233fbSRaed Salem 		desc_data = kcalloc(cntrs_data->ncounters,
34263b3233fbSRaed Salem 				    sizeof(*desc_data),
34273b3233fbSRaed Salem 				    GFP_KERNEL);
34283b3233fbSRaed Salem 		if (!desc_data)
34293b3233fbSRaed Salem 			return  -ENOMEM;
34303b3233fbSRaed Salem 
34313b3233fbSRaed Salem 		if (copy_from_user(desc_data,
34323b3233fbSRaed Salem 				   u64_to_user_ptr(cntrs_data->counters_data),
34333b3233fbSRaed Salem 				   sizeof(*desc_data) * cntrs_data->ncounters)) {
34343b3233fbSRaed Salem 			ret = -EFAULT;
34353b3233fbSRaed Salem 			goto free;
34363b3233fbSRaed Salem 		}
34373b3233fbSRaed Salem 	}
34383b3233fbSRaed Salem 
34393b3233fbSRaed Salem 	if (!mcounters->hw_cntrs_hndl) {
34403b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = mlx5_fc_create(
34413b3233fbSRaed Salem 			to_mdev(ibcounters->device)->mdev, false);
3442e31abf76Sweiyongjun (A) 		if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3443e31abf76Sweiyongjun (A) 			ret = PTR_ERR(mcounters->hw_cntrs_hndl);
34443b3233fbSRaed Salem 			goto free;
34453b3233fbSRaed Salem 		}
34463b3233fbSRaed Salem 		hw_hndl = true;
34473b3233fbSRaed Salem 	}
34483b3233fbSRaed Salem 
34493b3233fbSRaed Salem 	if (desc_data) {
34503b3233fbSRaed Salem 		/* counters already bound to at least one flow */
34513b3233fbSRaed Salem 		if (mcounters->cntrs_max_index) {
34523b3233fbSRaed Salem 			ret = -EINVAL;
34533b3233fbSRaed Salem 			goto free_hndl;
34543b3233fbSRaed Salem 		}
34553b3233fbSRaed Salem 
34563b3233fbSRaed Salem 		ret = counters_set_description(ibcounters,
34573b3233fbSRaed Salem 					       MLX5_IB_COUNTERS_FLOW,
34583b3233fbSRaed Salem 					       desc_data,
34593b3233fbSRaed Salem 					       cntrs_data->ncounters);
34603b3233fbSRaed Salem 		if (ret)
34613b3233fbSRaed Salem 			goto free_hndl;
34623b3233fbSRaed Salem 
34633b3233fbSRaed Salem 	} else if (!mcounters->cntrs_max_index) {
34643b3233fbSRaed Salem 		/* counters not bound yet, must have udata passed */
34653b3233fbSRaed Salem 		ret = -EINVAL;
34663b3233fbSRaed Salem 		goto free_hndl;
34673b3233fbSRaed Salem 	}
34683b3233fbSRaed Salem 
34693b3233fbSRaed Salem 	return 0;
34703b3233fbSRaed Salem 
34713b3233fbSRaed Salem free_hndl:
34723b3233fbSRaed Salem 	if (hw_hndl) {
34733b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
34743b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
34753b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = NULL;
34763b3233fbSRaed Salem 	}
34773b3233fbSRaed Salem free:
34783b3233fbSRaed Salem 	kfree(desc_data);
34793b3233fbSRaed Salem 	return ret;
34803b3233fbSRaed Salem }
34813b3233fbSRaed Salem 
3482669ff1e3SJianbo Liu static void mlx5_ib_set_rule_source_port(struct mlx5_ib_dev *dev,
3483669ff1e3SJianbo Liu 					 struct mlx5_flow_spec *spec,
3484669ff1e3SJianbo Liu 					 struct mlx5_eswitch_rep *rep)
3485669ff1e3SJianbo Liu {
3486669ff1e3SJianbo Liu 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
3487669ff1e3SJianbo Liu 	void *misc;
3488669ff1e3SJianbo Liu 
3489669ff1e3SJianbo Liu 	if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
3490669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3491669ff1e3SJianbo Liu 				    misc_parameters_2);
3492669ff1e3SJianbo Liu 
3493669ff1e3SJianbo Liu 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
3494669ff1e3SJianbo Liu 			 mlx5_eswitch_get_vport_metadata_for_match(esw,
3495669ff1e3SJianbo Liu 								   rep->vport));
3496669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3497669ff1e3SJianbo Liu 				    misc_parameters_2);
3498669ff1e3SJianbo Liu 
3499669ff1e3SJianbo Liu 		MLX5_SET_TO_ONES(fte_match_set_misc2, misc, metadata_reg_c_0);
3500669ff1e3SJianbo Liu 	} else {
3501669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3502669ff1e3SJianbo Liu 				    misc_parameters);
3503669ff1e3SJianbo Liu 
3504669ff1e3SJianbo Liu 		MLX5_SET(fte_match_set_misc, misc, source_port, rep->vport);
3505669ff1e3SJianbo Liu 
3506669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3507669ff1e3SJianbo Liu 				    misc_parameters);
3508669ff1e3SJianbo Liu 
3509669ff1e3SJianbo Liu 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3510669ff1e3SJianbo Liu 	}
3511669ff1e3SJianbo Liu }
3512669ff1e3SJianbo Liu 
3513a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3514038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
3515dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
3516a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
35173b3233fbSRaed Salem 						      u32 underlay_qpn,
35183b3233fbSRaed Salem 						      struct mlx5_ib_create_flow *ucmd)
3519038d2ef8SMaor Gottlieb {
3520038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
3521038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
3522bb0ee7dcSJianbo Liu 	struct mlx5_flow_act flow_act = {};
3523c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
35243b3233fbSRaed Salem 	struct mlx5_flow_destination dest_arr[2] = {};
35253b3233fbSRaed Salem 	struct mlx5_flow_destination *rule_dst = dest_arr;
3526dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3527038d2ef8SMaor Gottlieb 	unsigned int spec_index;
352871c6e863SAriel Levkovich 	u32 prev_type = 0;
3529038d2ef8SMaor Gottlieb 	int err = 0;
35303b3233fbSRaed Salem 	int dest_num = 0;
3531802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3532038d2ef8SMaor Gottlieb 
353319cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
3534038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3535038d2ef8SMaor Gottlieb 
35366a4d00beSMark Bloch 	if (dev->is_rep && is_egress)
353778dd0c43SMark Bloch 		return ERR_PTR(-EINVAL);
353878dd0c43SMark Bloch 
35391b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3540038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3541c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
3542038d2ef8SMaor Gottlieb 		err = -ENOMEM;
3543038d2ef8SMaor Gottlieb 		goto free;
3544038d2ef8SMaor Gottlieb 	}
3545038d2ef8SMaor Gottlieb 
3546038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
35473b3233fbSRaed Salem 	if (dst) {
35483b3233fbSRaed Salem 		memcpy(&dest_arr[0], dst, sizeof(*dst));
35493b3233fbSRaed Salem 		dest_num++;
35503b3233fbSRaed Salem 	}
3551038d2ef8SMaor Gottlieb 
3552038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
3553bb0ee7dcSJianbo Liu 		err = parse_flow_attr(dev->mdev, spec,
355471c6e863SAriel Levkovich 				      ib_flow, flow_attr, &flow_act,
355571c6e863SAriel Levkovich 				      prev_type);
3556038d2ef8SMaor Gottlieb 		if (err < 0)
3557038d2ef8SMaor Gottlieb 			goto free;
3558038d2ef8SMaor Gottlieb 
355971c6e863SAriel Levkovich 		prev_type = ((union ib_flow_spec *)ib_flow)->type;
3560038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3561038d2ef8SMaor Gottlieb 	}
3562038d2ef8SMaor Gottlieb 
3563a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
3564a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
3565a550ddfcSYishai Hadas 
35666a4d00beSMark Bloch 	if (dev->is_rep) {
3567669ff1e3SJianbo Liu 		struct mlx5_eswitch_rep *rep;
3568018a94eeSMark Bloch 
3569669ff1e3SJianbo Liu 		rep = dev->port[flow_attr->port - 1].rep;
3570669ff1e3SJianbo Liu 		if (!rep) {
35716a4d00beSMark Bloch 			err = -EINVAL;
35726a4d00beSMark Bloch 			goto free;
35736a4d00beSMark Bloch 		}
3574669ff1e3SJianbo Liu 
3575669ff1e3SJianbo Liu 		mlx5_ib_set_rule_source_port(dev, spec, rep);
3576018a94eeSMark Bloch 	}
3577018a94eeSMark Bloch 
3578466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3579802c2125SAviad Yehezkel 
3580802c2125SAviad Yehezkel 	if (is_egress &&
3581802c2125SAviad Yehezkel 	    !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3582802c2125SAviad Yehezkel 		err = -EINVAL;
3583802c2125SAviad Yehezkel 		goto free;
3584802c2125SAviad Yehezkel 	}
3585802c2125SAviad Yehezkel 
35863b3233fbSRaed Salem 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3587171c7625SMark Bloch 		struct mlx5_ib_mcounters *mcounters;
3588171c7625SMark Bloch 
35893b3233fbSRaed Salem 		err = flow_counters_set_data(flow_act.counters, ucmd);
35903b3233fbSRaed Salem 		if (err)
35913b3233fbSRaed Salem 			goto free;
35923b3233fbSRaed Salem 
3593171c7625SMark Bloch 		mcounters = to_mcounters(flow_act.counters);
35943b3233fbSRaed Salem 		handler->ibcounters = flow_act.counters;
35953b3233fbSRaed Salem 		dest_arr[dest_num].type =
35963b3233fbSRaed Salem 			MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3597171c7625SMark Bloch 		dest_arr[dest_num].counter_id =
3598171c7625SMark Bloch 			mlx5_fc_id(mcounters->hw_cntrs_hndl);
35993b3233fbSRaed Salem 		dest_num++;
36003b3233fbSRaed Salem 	}
36013b3233fbSRaed Salem 
3602075572d4SBoris Pismenny 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
36033b3233fbSRaed Salem 		if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3604a22ed86cSSlava Shwartsman 			rule_dst = NULL;
3605a22ed86cSSlava Shwartsman 			dest_num = 0;
36063b3233fbSRaed Salem 		}
3607a22ed86cSSlava Shwartsman 	} else {
3608802c2125SAviad Yehezkel 		if (is_egress)
3609802c2125SAviad Yehezkel 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3610802c2125SAviad Yehezkel 		else
3611802c2125SAviad Yehezkel 			flow_act.action |=
36123b3233fbSRaed Salem 				dest_num ?  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
361335d19011SMaor Gottlieb 					MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3614a22ed86cSSlava Shwartsman 	}
36152ac693f9SMoses Reuben 
3616bb0ee7dcSJianbo Liu 	if ((spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG)  &&
36172ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
36182ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
36192ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3620bb0ee7dcSJianbo Liu 			     spec->flow_context.flow_tag, flow_attr->type);
36212ac693f9SMoses Reuben 		err = -EINVAL;
36222ac693f9SMoses Reuben 		goto free;
36232ac693f9SMoses Reuben 	}
362474491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
362566958ed9SHadar Hen Zion 					    &flow_act,
3626a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
3627038d2ef8SMaor Gottlieb 
3628038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
3629038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
3630038d2ef8SMaor Gottlieb 		goto free;
3631038d2ef8SMaor Gottlieb 	}
3632038d2ef8SMaor Gottlieb 
3633d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
36345497adc6SMaor Gottlieb 	handler->prio = ft_prio;
3635d4be3f44SYishai Hadas 	handler->dev = dev;
3636038d2ef8SMaor Gottlieb 
3637038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
3638038d2ef8SMaor Gottlieb free:
36393b3233fbSRaed Salem 	if (err && handler) {
36403b3233fbSRaed Salem 		if (handler->ibcounters &&
36413b3233fbSRaed Salem 		    atomic_read(&handler->ibcounters->usecnt) == 1)
36423b3233fbSRaed Salem 			counters_clear_description(handler->ibcounters);
3643038d2ef8SMaor Gottlieb 		kfree(handler);
36443b3233fbSRaed Salem 	}
3645c5bb1730SMaor Gottlieb 	kvfree(spec);
3646038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
3647038d2ef8SMaor Gottlieb }
3648038d2ef8SMaor Gottlieb 
3649a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3650a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
3651a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
3652a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
3653a550ddfcSYishai Hadas {
36543b3233fbSRaed Salem 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
3655a550ddfcSYishai Hadas }
3656a550ddfcSYishai Hadas 
365735d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
365835d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
365935d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
366035d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
366135d19011SMaor Gottlieb {
366235d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
366335d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
366435d19011SMaor Gottlieb 
366535d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
366635d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
366735d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
366835d19011SMaor Gottlieb 					       flow_attr, dst);
366935d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
367074491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3671d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
367235d19011SMaor Gottlieb 			kfree(handler);
367335d19011SMaor Gottlieb 			handler = handler_dst;
367435d19011SMaor Gottlieb 		} else {
367535d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
367635d19011SMaor Gottlieb 		}
367735d19011SMaor Gottlieb 	}
367835d19011SMaor Gottlieb 
367935d19011SMaor Gottlieb 	return handler;
368035d19011SMaor Gottlieb }
3681038d2ef8SMaor Gottlieb enum {
3682038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
3683038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
3684038d2ef8SMaor Gottlieb };
3685038d2ef8SMaor Gottlieb 
3686038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3687038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
3688038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
3689038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
3690038d2ef8SMaor Gottlieb {
3691038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
3692038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3693038d2ef8SMaor Gottlieb 
3694038d2ef8SMaor Gottlieb 	static struct {
3695038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
3696038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
3697038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
3698038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
3699038d2ef8SMaor Gottlieb 			.flow_attr = {
3700038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3701038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3702038d2ef8SMaor Gottlieb 			},
3703038d2ef8SMaor Gottlieb 			.eth_flow = {
3704038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3705038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3706038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3707038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
3708038d2ef8SMaor Gottlieb 			}
3709038d2ef8SMaor Gottlieb 		},
3710038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
3711038d2ef8SMaor Gottlieb 			.flow_attr = {
3712038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3713038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3714038d2ef8SMaor Gottlieb 			},
3715038d2ef8SMaor Gottlieb 			.eth_flow = {
3716038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3717038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3718038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3719038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
3720038d2ef8SMaor Gottlieb 			}
3721038d2ef8SMaor Gottlieb 		}
3722038d2ef8SMaor Gottlieb 	};
3723038d2ef8SMaor Gottlieb 
3724038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
3725038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
3726038d2ef8SMaor Gottlieb 				   dst);
3727038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
3728038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3729038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
3730038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3731038d2ef8SMaor Gottlieb 						 dst);
3732038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
373374491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3734d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
3735038d2ef8SMaor Gottlieb 			kfree(handler);
3736038d2ef8SMaor Gottlieb 			handler = handler_ucast;
3737038d2ef8SMaor Gottlieb 		} else {
3738038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
3739038d2ef8SMaor Gottlieb 		}
3740038d2ef8SMaor Gottlieb 	}
3741038d2ef8SMaor Gottlieb 
3742038d2ef8SMaor Gottlieb 	return handler;
3743038d2ef8SMaor Gottlieb }
3744038d2ef8SMaor Gottlieb 
3745cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3746cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
3747cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
3748cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
3749cc0e5d42SMaor Gottlieb {
3750cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
3751cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
3752cc0e5d42SMaor Gottlieb 	int err;
3753cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
3754cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
3755cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
3756cc0e5d42SMaor Gottlieb 	};
3757cc0e5d42SMaor Gottlieb 
3758cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3759cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
3760cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
3761cc0e5d42SMaor Gottlieb 		goto err;
3762cc0e5d42SMaor Gottlieb 	}
3763cc0e5d42SMaor Gottlieb 
3764cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3765cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
3766cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
3767cc0e5d42SMaor Gottlieb 		goto err_tx;
3768cc0e5d42SMaor Gottlieb 	}
3769cc0e5d42SMaor Gottlieb 
3770cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
3771cc0e5d42SMaor Gottlieb 
3772cc0e5d42SMaor Gottlieb 	return handler_rx;
3773cc0e5d42SMaor Gottlieb 
3774cc0e5d42SMaor Gottlieb err_tx:
377574491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
3776cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
3777cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
3778cc0e5d42SMaor Gottlieb err:
3779cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
3780cc0e5d42SMaor Gottlieb }
3781cc0e5d42SMaor Gottlieb 
3782038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3783038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
378459082a32SMatan Barak 					   int domain,
378559082a32SMatan Barak 					   struct ib_udata *udata)
3786038d2ef8SMaor Gottlieb {
3787038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3788d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
3789038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3790038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
3791cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3792038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
3793802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
37943b3233fbSRaed Salem 	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
37953b3233fbSRaed Salem 	size_t min_ucmd_sz, required_ucmd_sz;
3796038d2ef8SMaor Gottlieb 	int err;
3797a550ddfcSYishai Hadas 	int underlay_qpn;
3798038d2ef8SMaor Gottlieb 
37993b3233fbSRaed Salem 	if (udata && udata->inlen) {
38003b3233fbSRaed Salem 		min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
38013b3233fbSRaed Salem 				sizeof(ucmd_hdr.reserved);
38023b3233fbSRaed Salem 		if (udata->inlen < min_ucmd_sz)
380359082a32SMatan Barak 			return ERR_PTR(-EOPNOTSUPP);
380459082a32SMatan Barak 
38053b3233fbSRaed Salem 		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
38063b3233fbSRaed Salem 		if (err)
38073b3233fbSRaed Salem 			return ERR_PTR(err);
38083b3233fbSRaed Salem 
38093b3233fbSRaed Salem 		/* currently supports only one counters data */
38103b3233fbSRaed Salem 		if (ucmd_hdr.ncounters_data > 1)
38113b3233fbSRaed Salem 			return ERR_PTR(-EINVAL);
38123b3233fbSRaed Salem 
38133b3233fbSRaed Salem 		required_ucmd_sz = min_ucmd_sz +
38143b3233fbSRaed Salem 			sizeof(struct mlx5_ib_flow_counters_data) *
38153b3233fbSRaed Salem 			ucmd_hdr.ncounters_data;
38163b3233fbSRaed Salem 		if (udata->inlen > required_ucmd_sz &&
38173b3233fbSRaed Salem 		    !ib_is_udata_cleared(udata, required_ucmd_sz,
38183b3233fbSRaed Salem 					 udata->inlen - required_ucmd_sz))
38193b3233fbSRaed Salem 			return ERR_PTR(-EOPNOTSUPP);
38203b3233fbSRaed Salem 
38213b3233fbSRaed Salem 		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
38223b3233fbSRaed Salem 		if (!ucmd)
38233b3233fbSRaed Salem 			return ERR_PTR(-ENOMEM);
38243b3233fbSRaed Salem 
38253b3233fbSRaed Salem 		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
3826299eafeeSGustavo A. R. Silva 		if (err)
3827299eafeeSGustavo A. R. Silva 			goto free_ucmd;
38283b3233fbSRaed Salem 	}
38293b3233fbSRaed Salem 
3830299eafeeSGustavo A. R. Silva 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3831299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3832299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3833299eafeeSGustavo A. R. Silva 	}
3834038d2ef8SMaor Gottlieb 
3835038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
3836508562d6SDaniel Jurgens 	    flow_attr->port > dev->num_ports ||
3837802c2125SAviad Yehezkel 	    (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3838299eafeeSGustavo A. R. Silva 				  IB_FLOW_ATTR_FLAGS_EGRESS))) {
3839299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3840299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3841299eafeeSGustavo A. R. Silva 	}
3842802c2125SAviad Yehezkel 
3843802c2125SAviad Yehezkel 	if (is_egress &&
3844802c2125SAviad Yehezkel 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3845299eafeeSGustavo A. R. Silva 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3846299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3847299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3848299eafeeSGustavo A. R. Silva 	}
3849038d2ef8SMaor Gottlieb 
3850038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3851299eafeeSGustavo A. R. Silva 	if (!dst) {
3852299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3853299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3854299eafeeSGustavo A. R. Silva 	}
3855038d2ef8SMaor Gottlieb 
38569a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3857038d2ef8SMaor Gottlieb 
3858802c2125SAviad Yehezkel 	ft_prio = get_flow_table(dev, flow_attr,
3859802c2125SAviad Yehezkel 				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3860038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
3861038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
3862038d2ef8SMaor Gottlieb 		goto unlock;
3863038d2ef8SMaor Gottlieb 	}
3864cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3865cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3866cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
3867cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
3868cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
3869cc0e5d42SMaor Gottlieb 			goto destroy_ft;
3870cc0e5d42SMaor Gottlieb 		}
3871cc0e5d42SMaor Gottlieb 	}
3872038d2ef8SMaor Gottlieb 
3873802c2125SAviad Yehezkel 	if (is_egress) {
3874802c2125SAviad Yehezkel 		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3875802c2125SAviad Yehezkel 	} else {
3876038d2ef8SMaor Gottlieb 		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3877d9f88e5aSYishai Hadas 		if (mqp->flags & MLX5_IB_QP_RSS)
3878d9f88e5aSYishai Hadas 			dst->tir_num = mqp->rss_qp.tirn;
3879d9f88e5aSYishai Hadas 		else
3880d9f88e5aSYishai Hadas 			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3881802c2125SAviad Yehezkel 	}
3882038d2ef8SMaor Gottlieb 
3883038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
388435d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
388535d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
388635d19011SMaor Gottlieb 							flow_attr, dst);
388735d19011SMaor Gottlieb 		} else {
3888a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3889a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
3890a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
38913b3233fbSRaed Salem 						    dst, underlay_qpn, ucmd);
389235d19011SMaor Gottlieb 		}
3893038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3894038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3895038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3896038d2ef8SMaor Gottlieb 						dst);
3897cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3898cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3899038d2ef8SMaor Gottlieb 	} else {
3900038d2ef8SMaor Gottlieb 		err = -EINVAL;
3901038d2ef8SMaor Gottlieb 		goto destroy_ft;
3902038d2ef8SMaor Gottlieb 	}
3903038d2ef8SMaor Gottlieb 
3904038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
3905038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
3906038d2ef8SMaor Gottlieb 		handler = NULL;
3907038d2ef8SMaor Gottlieb 		goto destroy_ft;
3908038d2ef8SMaor Gottlieb 	}
3909038d2ef8SMaor Gottlieb 
39109a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3911038d2ef8SMaor Gottlieb 	kfree(dst);
39123b3233fbSRaed Salem 	kfree(ucmd);
3913038d2ef8SMaor Gottlieb 
3914038d2ef8SMaor Gottlieb 	return &handler->ibflow;
3915038d2ef8SMaor Gottlieb 
3916038d2ef8SMaor Gottlieb destroy_ft:
3917038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
3918cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
3919cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
3920038d2ef8SMaor Gottlieb unlock:
39219a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3922038d2ef8SMaor Gottlieb 	kfree(dst);
3923299eafeeSGustavo A. R. Silva free_ucmd:
39243b3233fbSRaed Salem 	kfree(ucmd);
3925038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
3926038d2ef8SMaor Gottlieb }
3927038d2ef8SMaor Gottlieb 
3928b47fd4ffSMark Bloch static struct mlx5_ib_flow_prio *
3929b47fd4ffSMark Bloch _get_flow_table(struct mlx5_ib_dev *dev,
3930b47fd4ffSMark Bloch 		struct mlx5_ib_flow_matcher *fs_matcher,
3931b47fd4ffSMark Bloch 		bool mcast)
3932d4be3f44SYishai Hadas {
3933d4be3f44SYishai Hadas 	struct mlx5_flow_namespace *ns = NULL;
393413a43765SMark Bloch 	struct mlx5_ib_flow_prio *prio = NULL;
393513a43765SMark Bloch 	int max_table_size = 0;
3936cecae747SMaor Gottlieb 	bool esw_encap;
3937b47fd4ffSMark Bloch 	u32 flags = 0;
3938b47fd4ffSMark Bloch 	int priority;
3939d4be3f44SYishai Hadas 
394013a43765SMark Bloch 	if (mcast)
394113a43765SMark Bloch 		priority = MLX5_IB_FLOW_MCAST_PRIO;
394213a43765SMark Bloch 	else
394313a43765SMark Bloch 		priority = ib_prio_to_core_prio(fs_matcher->priority, false);
394413a43765SMark Bloch 
3945cecae747SMaor Gottlieb 	esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
3946cecae747SMaor Gottlieb 		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
3947b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
3948d4be3f44SYishai Hadas 		max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3949d4be3f44SYishai Hadas 					log_max_ft_size));
3950cecae747SMaor Gottlieb 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap) && !esw_encap)
3951b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3952b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3953cecae747SMaor Gottlieb 					      reformat_l3_tunnel_to_l2) &&
3954cecae747SMaor Gottlieb 		    !esw_encap)
3955b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
395613a43765SMark Bloch 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS) {
395713a43765SMark Bloch 		max_table_size = BIT(
395813a43765SMark Bloch 			MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, log_max_ft_size));
3959cecae747SMaor Gottlieb 		if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat) && !esw_encap)
3960b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
396113a43765SMark Bloch 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB) {
396213a43765SMark Bloch 		max_table_size = BIT(
396313a43765SMark Bloch 			MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, log_max_ft_size));
396409d985beSMaor Gottlieb 		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, decap) && esw_encap)
396509d985beSMaor Gottlieb 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
396609d985beSMaor Gottlieb 		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, reformat_l3_tunnel_to_l2) &&
396709d985beSMaor Gottlieb 		    esw_encap)
396809d985beSMaor Gottlieb 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
396913a43765SMark Bloch 		priority = FDB_BYPASS_PATH;
3970d8abe884SMark Zhang 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX) {
3971d8abe884SMark Zhang 		max_table_size =
3972d8abe884SMark Zhang 			BIT(MLX5_CAP_FLOWTABLE_RDMA_RX(dev->mdev,
3973d8abe884SMark Zhang 						       log_max_ft_size));
3974d8abe884SMark Zhang 		priority = fs_matcher->priority;
3975b47fd4ffSMark Bloch 	}
3976b47fd4ffSMark Bloch 
39773b70508aSMark Bloch 	max_table_size = min_t(int, max_table_size, MLX5_FS_MAX_ENTRIES);
3978d4be3f44SYishai Hadas 
3979b47fd4ffSMark Bloch 	ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
3980d4be3f44SYishai Hadas 	if (!ns)
3981d4be3f44SYishai Hadas 		return ERR_PTR(-ENOTSUPP);
3982d4be3f44SYishai Hadas 
3983b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
3984d4be3f44SYishai Hadas 		prio = &dev->flow_db->prios[priority];
398513a43765SMark Bloch 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS)
3986b47fd4ffSMark Bloch 		prio = &dev->flow_db->egress_prios[priority];
398713a43765SMark Bloch 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB)
398813a43765SMark Bloch 		prio = &dev->flow_db->fdb;
3989d8abe884SMark Zhang 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX)
3990d8abe884SMark Zhang 		prio = &dev->flow_db->rdma_rx[priority];
399113a43765SMark Bloch 
399213a43765SMark Bloch 	if (!prio)
399313a43765SMark Bloch 		return ERR_PTR(-EINVAL);
3994d4be3f44SYishai Hadas 
3995d4be3f44SYishai Hadas 	if (prio->flow_table)
3996d4be3f44SYishai Hadas 		return prio;
3997d4be3f44SYishai Hadas 
39983b70508aSMark Bloch 	return _get_prio(ns, prio, priority, max_table_size,
3999b47fd4ffSMark Bloch 			 MLX5_FS_MAX_TYPES, flags);
4000d4be3f44SYishai Hadas }
4001d4be3f44SYishai Hadas 
4002d4be3f44SYishai Hadas static struct mlx5_ib_flow_handler *
4003d4be3f44SYishai Hadas _create_raw_flow_rule(struct mlx5_ib_dev *dev,
4004d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_prio *ft_prio,
4005d4be3f44SYishai Hadas 		      struct mlx5_flow_destination *dst,
4006d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_matcher  *fs_matcher,
4007bb0ee7dcSJianbo Liu 		      struct mlx5_flow_context *flow_context,
4008b823dd6dSMark Bloch 		      struct mlx5_flow_act *flow_act,
4009bfc5d839SMark Bloch 		      void *cmd_in, int inlen,
4010bfc5d839SMark Bloch 		      int dst_num)
4011d4be3f44SYishai Hadas {
4012d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
4013d4be3f44SYishai Hadas 	struct mlx5_flow_spec *spec;
4014d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft = ft_prio->flow_table;
4015d4be3f44SYishai Hadas 	int err = 0;
4016d4be3f44SYishai Hadas 
4017d4be3f44SYishai Hadas 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
4018d4be3f44SYishai Hadas 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
4019d4be3f44SYishai Hadas 	if (!handler || !spec) {
4020d4be3f44SYishai Hadas 		err = -ENOMEM;
4021d4be3f44SYishai Hadas 		goto free;
4022d4be3f44SYishai Hadas 	}
4023d4be3f44SYishai Hadas 
4024d4be3f44SYishai Hadas 	INIT_LIST_HEAD(&handler->list);
4025d4be3f44SYishai Hadas 
4026d4be3f44SYishai Hadas 	memcpy(spec->match_value, cmd_in, inlen);
4027d4be3f44SYishai Hadas 	memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
4028d4be3f44SYishai Hadas 	       fs_matcher->mask_len);
4029d4be3f44SYishai Hadas 	spec->match_criteria_enable = fs_matcher->match_criteria_enable;
4030bb0ee7dcSJianbo Liu 	spec->flow_context = *flow_context;
4031d4be3f44SYishai Hadas 
4032d4be3f44SYishai Hadas 	handler->rule = mlx5_add_flow_rules(ft, spec,
4033bfc5d839SMark Bloch 					    flow_act, dst, dst_num);
4034d4be3f44SYishai Hadas 
4035d4be3f44SYishai Hadas 	if (IS_ERR(handler->rule)) {
4036d4be3f44SYishai Hadas 		err = PTR_ERR(handler->rule);
4037d4be3f44SYishai Hadas 		goto free;
4038d4be3f44SYishai Hadas 	}
4039d4be3f44SYishai Hadas 
4040d4be3f44SYishai Hadas 	ft_prio->refcount++;
4041d4be3f44SYishai Hadas 	handler->prio = ft_prio;
4042d4be3f44SYishai Hadas 	handler->dev = dev;
4043d4be3f44SYishai Hadas 	ft_prio->flow_table = ft;
4044d4be3f44SYishai Hadas 
4045d4be3f44SYishai Hadas free:
4046d4be3f44SYishai Hadas 	if (err)
4047d4be3f44SYishai Hadas 		kfree(handler);
4048d4be3f44SYishai Hadas 	kvfree(spec);
4049d4be3f44SYishai Hadas 	return err ? ERR_PTR(err) : handler;
4050d4be3f44SYishai Hadas }
4051d4be3f44SYishai Hadas 
4052d4be3f44SYishai Hadas static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
4053d4be3f44SYishai Hadas 				void *match_v)
4054d4be3f44SYishai Hadas {
4055d4be3f44SYishai Hadas 	void *match_c;
4056d4be3f44SYishai Hadas 	void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
4057d4be3f44SYishai Hadas 	void *dmac, *dmac_mask;
4058d4be3f44SYishai Hadas 	void *ipv4, *ipv4_mask;
4059d4be3f44SYishai Hadas 
4060d4be3f44SYishai Hadas 	if (!(fs_matcher->match_criteria_enable &
4061d4be3f44SYishai Hadas 	      (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
4062d4be3f44SYishai Hadas 		return false;
4063d4be3f44SYishai Hadas 
4064d4be3f44SYishai Hadas 	match_c = fs_matcher->matcher_mask.match_params;
4065d4be3f44SYishai Hadas 	match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
4066d4be3f44SYishai Hadas 					   outer_headers);
4067d4be3f44SYishai Hadas 	match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
4068d4be3f44SYishai Hadas 					   outer_headers);
4069d4be3f44SYishai Hadas 
4070d4be3f44SYishai Hadas 	dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4071d4be3f44SYishai Hadas 			    dmac_47_16);
4072d4be3f44SYishai Hadas 	dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4073d4be3f44SYishai Hadas 				 dmac_47_16);
4074d4be3f44SYishai Hadas 
4075d4be3f44SYishai Hadas 	if (is_multicast_ether_addr(dmac) &&
4076d4be3f44SYishai Hadas 	    is_multicast_ether_addr(dmac_mask))
4077d4be3f44SYishai Hadas 		return true;
4078d4be3f44SYishai Hadas 
4079d4be3f44SYishai Hadas 	ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4080d4be3f44SYishai Hadas 			    dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4081d4be3f44SYishai Hadas 
4082d4be3f44SYishai Hadas 	ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4083d4be3f44SYishai Hadas 				 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4084d4be3f44SYishai Hadas 
4085d4be3f44SYishai Hadas 	if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
4086d4be3f44SYishai Hadas 	    ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
4087d4be3f44SYishai Hadas 		return true;
4088d4be3f44SYishai Hadas 
4089d4be3f44SYishai Hadas 	return false;
4090d4be3f44SYishai Hadas }
4091d4be3f44SYishai Hadas 
409232269441SYishai Hadas struct mlx5_ib_flow_handler *
409332269441SYishai Hadas mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
409432269441SYishai Hadas 			struct mlx5_ib_flow_matcher *fs_matcher,
4095bb0ee7dcSJianbo Liu 			struct mlx5_flow_context *flow_context,
4096b823dd6dSMark Bloch 			struct mlx5_flow_act *flow_act,
4097bfc5d839SMark Bloch 			u32 counter_id,
409832269441SYishai Hadas 			void *cmd_in, int inlen, int dest_id,
409932269441SYishai Hadas 			int dest_type)
410032269441SYishai Hadas {
4101d4be3f44SYishai Hadas 	struct mlx5_flow_destination *dst;
4102d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *ft_prio;
4103d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
4104bfc5d839SMark Bloch 	int dst_num = 0;
4105d4be3f44SYishai Hadas 	bool mcast;
4106d4be3f44SYishai Hadas 	int err;
4107d4be3f44SYishai Hadas 
4108d4be3f44SYishai Hadas 	if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
410932269441SYishai Hadas 		return ERR_PTR(-EOPNOTSUPP);
4110d4be3f44SYishai Hadas 
4111d4be3f44SYishai Hadas 	if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
4112d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
4113d4be3f44SYishai Hadas 
41148e8aa145SGustavo A. R. Silva 	dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
4115d4be3f44SYishai Hadas 	if (!dst)
4116d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
4117d4be3f44SYishai Hadas 
4118d4be3f44SYishai Hadas 	mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
4119d4be3f44SYishai Hadas 	mutex_lock(&dev->flow_db->lock);
4120d4be3f44SYishai Hadas 
4121b47fd4ffSMark Bloch 	ft_prio = _get_flow_table(dev, fs_matcher, mcast);
4122d4be3f44SYishai Hadas 	if (IS_ERR(ft_prio)) {
4123d4be3f44SYishai Hadas 		err = PTR_ERR(ft_prio);
4124d4be3f44SYishai Hadas 		goto unlock;
4125d4be3f44SYishai Hadas 	}
4126d4be3f44SYishai Hadas 
41276346f0bfSYishai Hadas 	if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
4128bfc5d839SMark Bloch 		dst[dst_num].type = dest_type;
4129bfc5d839SMark Bloch 		dst[dst_num].tir_num = dest_id;
4130b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
4131a7ee18bdSMark Bloch 	} else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
4132bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
4133bfc5d839SMark Bloch 		dst[dst_num].ft_num = dest_id;
4134b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
4135a7ee18bdSMark Bloch 	} else {
4136bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
4137a7ee18bdSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
41386346f0bfSYishai Hadas 	}
41396346f0bfSYishai Hadas 
4140bfc5d839SMark Bloch 	dst_num++;
4141bfc5d839SMark Bloch 
4142bfc5d839SMark Bloch 	if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
4143bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
4144bfc5d839SMark Bloch 		dst[dst_num].counter_id = counter_id;
4145bfc5d839SMark Bloch 		dst_num++;
4146bfc5d839SMark Bloch 	}
4147bfc5d839SMark Bloch 
4148bb0ee7dcSJianbo Liu 	handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher,
4149bb0ee7dcSJianbo Liu 					flow_context, flow_act,
4150bfc5d839SMark Bloch 					cmd_in, inlen, dst_num);
4151d4be3f44SYishai Hadas 
4152d4be3f44SYishai Hadas 	if (IS_ERR(handler)) {
4153d4be3f44SYishai Hadas 		err = PTR_ERR(handler);
4154d4be3f44SYishai Hadas 		goto destroy_ft;
4155d4be3f44SYishai Hadas 	}
4156d4be3f44SYishai Hadas 
4157d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
4158d4be3f44SYishai Hadas 	atomic_inc(&fs_matcher->usecnt);
4159d4be3f44SYishai Hadas 	handler->flow_matcher = fs_matcher;
4160d4be3f44SYishai Hadas 
4161d4be3f44SYishai Hadas 	kfree(dst);
4162d4be3f44SYishai Hadas 
4163d4be3f44SYishai Hadas 	return handler;
4164d4be3f44SYishai Hadas 
4165d4be3f44SYishai Hadas destroy_ft:
4166d4be3f44SYishai Hadas 	put_flow_table(dev, ft_prio, false);
4167d4be3f44SYishai Hadas unlock:
4168d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
4169d4be3f44SYishai Hadas 	kfree(dst);
4170d4be3f44SYishai Hadas 
4171d4be3f44SYishai Hadas 	return ERR_PTR(err);
417232269441SYishai Hadas }
417332269441SYishai Hadas 
4174c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
4175c6475a0bSAviad Yehezkel {
4176c6475a0bSAviad Yehezkel 	u32 flags = 0;
4177c6475a0bSAviad Yehezkel 
4178c6475a0bSAviad Yehezkel 	if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
4179c6475a0bSAviad Yehezkel 		flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
4180c6475a0bSAviad Yehezkel 
4181c6475a0bSAviad Yehezkel 	return flags;
4182c6475a0bSAviad Yehezkel }
4183c6475a0bSAviad Yehezkel 
4184c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED	MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
4185c6475a0bSAviad Yehezkel static struct ib_flow_action *
4186c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device,
4187c6475a0bSAviad Yehezkel 			       const struct ib_flow_action_attrs_esp *attr,
4188c6475a0bSAviad Yehezkel 			       struct uverbs_attr_bundle *attrs)
4189c6475a0bSAviad Yehezkel {
4190c6475a0bSAviad Yehezkel 	struct mlx5_ib_dev *mdev = to_mdev(device);
4191c6475a0bSAviad Yehezkel 	struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
4192c6475a0bSAviad Yehezkel 	struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
4193c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *action;
4194c6475a0bSAviad Yehezkel 	u64 action_flags;
4195c6475a0bSAviad Yehezkel 	u64 flags;
4196c6475a0bSAviad Yehezkel 	int err = 0;
4197c6475a0bSAviad Yehezkel 
4198bccd0622SJason Gunthorpe 	err = uverbs_get_flags64(
4199bccd0622SJason Gunthorpe 		&action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4200bccd0622SJason Gunthorpe 		((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
4201bccd0622SJason Gunthorpe 	if (err)
4202bccd0622SJason Gunthorpe 		return ERR_PTR(err);
4203c6475a0bSAviad Yehezkel 
4204c6475a0bSAviad Yehezkel 	flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
4205c6475a0bSAviad Yehezkel 
4206c6475a0bSAviad Yehezkel 	/* We current only support a subset of the standard features. Only a
4207c6475a0bSAviad Yehezkel 	 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
4208c6475a0bSAviad Yehezkel 	 * (with overlap). Full offload mode isn't supported.
4209c6475a0bSAviad Yehezkel 	 */
4210c6475a0bSAviad Yehezkel 	if (!attr->keymat || attr->replay || attr->encap ||
4211c6475a0bSAviad Yehezkel 	    attr->spi || attr->seq || attr->tfc_pad ||
4212c6475a0bSAviad Yehezkel 	    attr->hard_limit_pkts ||
4213c6475a0bSAviad Yehezkel 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4214c6475a0bSAviad Yehezkel 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
4215c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4216c6475a0bSAviad Yehezkel 
4217c6475a0bSAviad Yehezkel 	if (attr->keymat->protocol !=
4218c6475a0bSAviad Yehezkel 	    IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
4219c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4220c6475a0bSAviad Yehezkel 
4221c6475a0bSAviad Yehezkel 	aes_gcm = &attr->keymat->keymat.aes_gcm;
4222c6475a0bSAviad Yehezkel 
4223c6475a0bSAviad Yehezkel 	if (aes_gcm->icv_len != 16 ||
4224c6475a0bSAviad Yehezkel 	    aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
4225c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4226c6475a0bSAviad Yehezkel 
4227c6475a0bSAviad Yehezkel 	action = kmalloc(sizeof(*action), GFP_KERNEL);
4228c6475a0bSAviad Yehezkel 	if (!action)
4229c6475a0bSAviad Yehezkel 		return ERR_PTR(-ENOMEM);
4230c6475a0bSAviad Yehezkel 
4231c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4232c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
4233c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.aes_key));
4234c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
4235c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
4236c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.salt));
4237c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
4238c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
4239c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
4240c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
4241c6475a0bSAviad Yehezkel 	accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
4242c6475a0bSAviad Yehezkel 
4243c6475a0bSAviad Yehezkel 	accel_attrs.esn = attr->esn;
4244c6475a0bSAviad Yehezkel 	if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4245c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4246c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4247c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4248c6475a0bSAviad Yehezkel 
4249c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4250c6475a0bSAviad Yehezkel 		accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4251c6475a0bSAviad Yehezkel 
4252c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ctx =
4253c6475a0bSAviad Yehezkel 		mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4254c6475a0bSAviad Yehezkel 	if (IS_ERR(action->esp_aes_gcm.ctx)) {
4255c6475a0bSAviad Yehezkel 		err = PTR_ERR(action->esp_aes_gcm.ctx);
4256c6475a0bSAviad Yehezkel 		goto err_parse;
4257c6475a0bSAviad Yehezkel 	}
4258c6475a0bSAviad Yehezkel 
4259c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4260c6475a0bSAviad Yehezkel 
4261c6475a0bSAviad Yehezkel 	return &action->ib_action;
4262c6475a0bSAviad Yehezkel 
4263c6475a0bSAviad Yehezkel err_parse:
4264c6475a0bSAviad Yehezkel 	kfree(action);
4265c6475a0bSAviad Yehezkel 	return ERR_PTR(err);
4266c6475a0bSAviad Yehezkel }
4267c6475a0bSAviad Yehezkel 
4268349705c1SMatan Barak static int
4269349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4270349705c1SMatan Barak 			       const struct ib_flow_action_attrs_esp *attr,
4271349705c1SMatan Barak 			       struct uverbs_attr_bundle *attrs)
4272349705c1SMatan Barak {
4273349705c1SMatan Barak 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4274349705c1SMatan Barak 	struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4275349705c1SMatan Barak 	int err = 0;
4276349705c1SMatan Barak 
4277349705c1SMatan Barak 	if (attr->keymat || attr->replay || attr->encap ||
4278349705c1SMatan Barak 	    attr->spi || attr->seq || attr->tfc_pad ||
4279349705c1SMatan Barak 	    attr->hard_limit_pkts ||
4280349705c1SMatan Barak 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4281349705c1SMatan Barak 			     IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4282349705c1SMatan Barak 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4283349705c1SMatan Barak 		return -EOPNOTSUPP;
4284349705c1SMatan Barak 
4285349705c1SMatan Barak 	/* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4286349705c1SMatan Barak 	 * be modified.
4287349705c1SMatan Barak 	 */
4288349705c1SMatan Barak 	if (!(maction->esp_aes_gcm.ib_flags &
4289349705c1SMatan Barak 	      IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4290349705c1SMatan Barak 	    attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4291349705c1SMatan Barak 			   IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4292349705c1SMatan Barak 		return -EINVAL;
4293349705c1SMatan Barak 
4294349705c1SMatan Barak 	memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4295349705c1SMatan Barak 	       sizeof(accel_attrs));
4296349705c1SMatan Barak 
4297349705c1SMatan Barak 	accel_attrs.esn = attr->esn;
4298349705c1SMatan Barak 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4299349705c1SMatan Barak 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4300349705c1SMatan Barak 	else
4301349705c1SMatan Barak 		accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4302349705c1SMatan Barak 
4303349705c1SMatan Barak 	err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4304349705c1SMatan Barak 					 &accel_attrs);
4305349705c1SMatan Barak 	if (err)
4306349705c1SMatan Barak 		return err;
4307349705c1SMatan Barak 
4308349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags &=
4309349705c1SMatan Barak 		~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4310349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags |=
4311349705c1SMatan Barak 		attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4312349705c1SMatan Barak 
4313349705c1SMatan Barak 	return 0;
4314349705c1SMatan Barak }
4315349705c1SMatan Barak 
4316c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4317c6475a0bSAviad Yehezkel {
4318c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4319c6475a0bSAviad Yehezkel 
4320c6475a0bSAviad Yehezkel 	switch (action->type) {
4321c6475a0bSAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
4322c6475a0bSAviad Yehezkel 		/*
4323c6475a0bSAviad Yehezkel 		 * We only support aes_gcm by now, so we implicitly know this is
4324c6475a0bSAviad Yehezkel 		 * the underline crypto.
4325c6475a0bSAviad Yehezkel 		 */
4326c6475a0bSAviad Yehezkel 		mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4327c6475a0bSAviad Yehezkel 		break;
4328b4749bf2SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
4329b4749bf2SMark Bloch 		mlx5_ib_destroy_flow_action_raw(maction);
4330b4749bf2SMark Bloch 		break;
4331c6475a0bSAviad Yehezkel 	default:
4332c6475a0bSAviad Yehezkel 		WARN_ON(true);
4333c6475a0bSAviad Yehezkel 		break;
4334c6475a0bSAviad Yehezkel 	}
4335c6475a0bSAviad Yehezkel 
4336c6475a0bSAviad Yehezkel 	kfree(maction);
4337c6475a0bSAviad Yehezkel 	return 0;
4338c6475a0bSAviad Yehezkel }
4339c6475a0bSAviad Yehezkel 
4340e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4341e126ba97SEli Cohen {
4342e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
434381e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
4344e126ba97SEli Cohen 	int err;
4345539ec982SYishai Hadas 	u16 uid;
4346539ec982SYishai Hadas 
4347539ec982SYishai Hadas 	uid = ibqp->pd ?
4348539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4349e126ba97SEli Cohen 
435081e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
435181e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
435281e30880SYishai Hadas 		return -EOPNOTSUPP;
435381e30880SYishai Hadas 	}
435481e30880SYishai Hadas 
4355539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4356e126ba97SEli Cohen 	if (err)
4357e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4358e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4359e126ba97SEli Cohen 
4360e126ba97SEli Cohen 	return err;
4361e126ba97SEli Cohen }
4362e126ba97SEli Cohen 
4363e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4364e126ba97SEli Cohen {
4365e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4366e126ba97SEli Cohen 	int err;
4367539ec982SYishai Hadas 	u16 uid;
4368e126ba97SEli Cohen 
4369539ec982SYishai Hadas 	uid = ibqp->pd ?
4370539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4371539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4372e126ba97SEli Cohen 	if (err)
4373e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4374e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4375e126ba97SEli Cohen 
4376e126ba97SEli Cohen 	return err;
4377e126ba97SEli Cohen }
4378e126ba97SEli Cohen 
4379e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
4380e126ba97SEli Cohen {
43811b5daf11SMajd Dibbiny 	int err;
4382e126ba97SEli Cohen 
43831b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
4384e126ba97SEli Cohen 	if (err)
4385e126ba97SEli Cohen 		return err;
43861b5daf11SMajd Dibbiny 
43871b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
43881b5daf11SMajd Dibbiny 
43891b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
4390e126ba97SEli Cohen }
4391e126ba97SEli Cohen 
4392508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
4393508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4394e126ba97SEli Cohen {
4395e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
439654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4397e126ba97SEli Cohen 
43989603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
4399e126ba97SEli Cohen }
4400508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
4401e126ba97SEli Cohen 
4402508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
4403e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
4404e126ba97SEli Cohen {
4405e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
440654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4407e126ba97SEli Cohen 
44086aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
4409e126ba97SEli Cohen }
4410508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
4411e126ba97SEli Cohen 
4412508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
4413508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4414e126ba97SEli Cohen {
4415e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
441654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
441754747231SParav Pandit 
44189603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
4419e126ba97SEli Cohen }
4420508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
4421e126ba97SEli Cohen 
4422508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
4423508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
4424e126ba97SEli Cohen {
4425e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
442654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
442754747231SParav Pandit 
44289603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
4429e126ba97SEli Cohen }
4430508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
4431e126ba97SEli Cohen 
4432508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
4433508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4434e126ba97SEli Cohen {
4435e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
443654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
443754747231SParav Pandit 
4438e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
44399603b61dSJack Morgenstein 		       dev->mdev->board_id);
4440e126ba97SEli Cohen }
4441508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
4442e126ba97SEli Cohen 
4443508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
4444508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
4445508a523fSParav Pandit 	&dev_attr_hca_type.attr,
4446508a523fSParav Pandit 	&dev_attr_board_id.attr,
4447508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
4448508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
4449508a523fSParav Pandit 	NULL,
4450508a523fSParav Pandit };
4451e126ba97SEli Cohen 
4452508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
4453508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
4454e126ba97SEli Cohen };
4455e126ba97SEli Cohen 
44567722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
44577722f47eSHaggai Eran {
44587722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
44597722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
44607722f47eSHaggai Eran 			     pkey_change_work);
44617722f47eSHaggai Eran 
44627722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
44637722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
44647722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
44657722f47eSHaggai Eran }
44667722f47eSHaggai Eran 
446789ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
446889ea94a7SMaor Gottlieb {
446989ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
447089ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
447189ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
447289ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
447389ea94a7SMaor Gottlieb 	unsigned long flags_qp;
447489ea94a7SMaor Gottlieb 	unsigned long flags_cq;
447589ea94a7SMaor Gottlieb 	unsigned long flags;
447689ea94a7SMaor Gottlieb 
447789ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
447889ea94a7SMaor Gottlieb 
447989ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
448089ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
448189ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
448289ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
448389ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
448489ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
448589ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
448689ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
448789ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
448889ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
448989ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
449089ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
449189ea94a7SMaor Gottlieb 						      &cq_armed_list);
449289ea94a7SMaor Gottlieb 				}
449389ea94a7SMaor Gottlieb 			}
449489ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
449589ea94a7SMaor Gottlieb 		}
449689ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
449789ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
449889ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
449989ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
450089ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
450189ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
450289ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
450389ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
450489ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
450589ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
450689ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
450789ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
450889ea94a7SMaor Gottlieb 							      &cq_armed_list);
450989ea94a7SMaor Gottlieb 					}
451089ea94a7SMaor Gottlieb 				}
451189ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
451289ea94a7SMaor Gottlieb 						       flags_cq);
451389ea94a7SMaor Gottlieb 			}
451489ea94a7SMaor Gottlieb 		}
451589ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
451689ea94a7SMaor Gottlieb 	}
451789ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
451889ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
451989ea94a7SMaor Gottlieb 	 */
452089ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
45214e0e2ea1SYishai Hadas 		mcq->comp(mcq, NULL);
452289ea94a7SMaor Gottlieb 	}
452389ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
452489ea94a7SMaor Gottlieb }
452589ea94a7SMaor Gottlieb 
452603404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
452703404e8aSMaor Gottlieb {
452803404e8aSMaor Gottlieb 	int err;
452903404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
453003404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
453103404e8aSMaor Gottlieb 			     delay_drop_work);
453203404e8aSMaor Gottlieb 
4533fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
4534fe248c3aSMaor Gottlieb 
453503404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
453603404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
453703404e8aSMaor Gottlieb 				       delay_drop->timeout);
453803404e8aSMaor Gottlieb 	if (err) {
453903404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
454003404e8aSMaor Gottlieb 			     delay_drop->timeout);
454103404e8aSMaor Gottlieb 		delay_drop->activate = false;
454203404e8aSMaor Gottlieb 	}
454303404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
454403404e8aSMaor Gottlieb }
454503404e8aSMaor Gottlieb 
454609e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
454709e574faSSaeed Mahameed 				 struct ib_event *ibev)
454809e574faSSaeed Mahameed {
45496cfdc7e4SAya Levin 	u8 port = (eqe->data.port.port >> 4) & 0xf;
45506cfdc7e4SAya Levin 
455109e574faSSaeed Mahameed 	switch (eqe->sub_type) {
455209e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
45536cfdc7e4SAya Levin 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
45546cfdc7e4SAya Levin 					    IB_LINK_LAYER_ETHERNET)
455509e574faSSaeed Mahameed 			schedule_work(&ibdev->delay_drop.delay_drop_work);
455609e574faSSaeed Mahameed 		break;
455709e574faSSaeed Mahameed 	default: /* do nothing */
455809e574faSSaeed Mahameed 		return;
455909e574faSSaeed Mahameed 	}
456009e574faSSaeed Mahameed }
456109e574faSSaeed Mahameed 
4562134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4563134e9349SSaeed Mahameed 			      struct ib_event *ibev)
4564134e9349SSaeed Mahameed {
4565134e9349SSaeed Mahameed 	u8 port = (eqe->data.port.port >> 4) & 0xf;
4566134e9349SSaeed Mahameed 
4567134e9349SSaeed Mahameed 	ibev->element.port_num = port;
4568134e9349SSaeed Mahameed 
4569134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
4570134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4571134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4572134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4573134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
4574134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
4575134e9349SSaeed Mahameed 		 */
4576134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4577134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
4578134e9349SSaeed Mahameed 			return -EINVAL;
4579134e9349SSaeed Mahameed 
4580134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4581134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4582134e9349SSaeed Mahameed 		break;
4583134e9349SSaeed Mahameed 
4584134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
4585134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
4586134e9349SSaeed Mahameed 		break;
4587134e9349SSaeed Mahameed 
4588134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4589134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
4590134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4591134e9349SSaeed Mahameed 		break;
4592134e9349SSaeed Mahameed 
4593134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4594134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
4595134e9349SSaeed Mahameed 		break;
4596134e9349SSaeed Mahameed 
4597134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4598134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
4599134e9349SSaeed Mahameed 		break;
4600134e9349SSaeed Mahameed 	default:
4601134e9349SSaeed Mahameed 		return -EINVAL;
4602134e9349SSaeed Mahameed 	}
4603134e9349SSaeed Mahameed 
4604134e9349SSaeed Mahameed 	return 0;
4605134e9349SSaeed Mahameed }
4606134e9349SSaeed Mahameed 
4607d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
4608e126ba97SEli Cohen {
4609d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
4610d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
4611d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
4612e126ba97SEli Cohen 	struct ib_event ibev;
4613dbaaff2aSEli Cohen 	bool fatal = false;
4614e126ba97SEli Cohen 
4615df097a27SSaeed Mahameed 	if (work->is_slave) {
4616df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
4617d69a24e0SDaniel Jurgens 		if (!ibdev)
4618d69a24e0SDaniel Jurgens 			goto out;
4619d69a24e0SDaniel Jurgens 	} else {
4620df097a27SSaeed Mahameed 		ibdev = work->dev;
4621d69a24e0SDaniel Jurgens 	}
4622d69a24e0SDaniel Jurgens 
4623d69a24e0SDaniel Jurgens 	switch (work->event) {
4624e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
4625e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
462689ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
4627134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
4628dbaaff2aSEli Cohen 		fatal = true;
4629e126ba97SEli Cohen 		break;
4630134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
4631134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
4632d69a24e0SDaniel Jurgens 			goto out;
4633e126ba97SEli Cohen 		break;
463409e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
463509e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
463609e574faSSaeed Mahameed 		/* fall through */
4637bdc37924SSaeed Mahameed 	default:
463803404e8aSMaor Gottlieb 		goto out;
4639e126ba97SEli Cohen 	}
4640e126ba97SEli Cohen 
4641e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
4642e126ba97SEli Cohen 
4643134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4644134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
464503404e8aSMaor Gottlieb 		goto out;
4646a0c84c32SEli Cohen 	}
4647a0c84c32SEli Cohen 
4648e126ba97SEli Cohen 	if (ibdev->ib_active)
4649e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
4650dbaaff2aSEli Cohen 
4651dbaaff2aSEli Cohen 	if (fatal)
4652dbaaff2aSEli Cohen 		ibdev->ib_active = false;
465303404e8aSMaor Gottlieb out:
4654d69a24e0SDaniel Jurgens 	kfree(work);
4655d69a24e0SDaniel Jurgens }
4656d69a24e0SDaniel Jurgens 
4657df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
4658df097a27SSaeed Mahameed 			 unsigned long event, void *param)
4659d69a24e0SDaniel Jurgens {
4660d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
4661d69a24e0SDaniel Jurgens 
4662d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
466310bea9c8SLeon Romanovsky 	if (!work)
4664df097a27SSaeed Mahameed 		return NOTIFY_DONE;
466510bea9c8SLeon Romanovsky 
4666d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4667df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4668df097a27SSaeed Mahameed 	work->is_slave = false;
4669d69a24e0SDaniel Jurgens 	work->param = param;
4670d69a24e0SDaniel Jurgens 	work->event = event;
4671d69a24e0SDaniel Jurgens 
4672d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
4673df097a27SSaeed Mahameed 
4674df097a27SSaeed Mahameed 	return NOTIFY_OK;
4675df097a27SSaeed Mahameed }
4676df097a27SSaeed Mahameed 
4677df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4678df097a27SSaeed Mahameed 				    unsigned long event, void *param)
4679df097a27SSaeed Mahameed {
4680df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
4681df097a27SSaeed Mahameed 
4682df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
4683df097a27SSaeed Mahameed 	if (!work)
4684df097a27SSaeed Mahameed 		return NOTIFY_DONE;
4685df097a27SSaeed Mahameed 
4686df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4687df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4688df097a27SSaeed Mahameed 	work->is_slave = true;
4689df097a27SSaeed Mahameed 	work->param = param;
4690df097a27SSaeed Mahameed 	work->event = event;
4691df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
4692df097a27SSaeed Mahameed 
4693df097a27SSaeed Mahameed 	return NOTIFY_OK;
4694d69a24e0SDaniel Jurgens }
4695d69a24e0SDaniel Jurgens 
4696c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4697c43f1112SMaor Gottlieb {
4698c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
4699c43f1112SMaor Gottlieb 	int err;
4700c43f1112SMaor Gottlieb 	int port;
4701c43f1112SMaor Gottlieb 
4702a989ea01SMark Bloch 	for (port = 1; port <= ARRAY_SIZE(dev->mdev->port_caps); port++) {
4703c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
4704c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4705c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
4706c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4707c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
4708c43f1112SMaor Gottlieb 								   port, 0,
4709c43f1112SMaor Gottlieb 								   &vport_ctx);
4710c43f1112SMaor Gottlieb 				if (err) {
4711c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4712c43f1112SMaor Gottlieb 						    port, err);
4713c43f1112SMaor Gottlieb 					return err;
4714c43f1112SMaor Gottlieb 				}
4715c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
4716c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
4717c43f1112SMaor Gottlieb 			} else {
4718c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
4719c43f1112SMaor Gottlieb 			}
4720c43f1112SMaor Gottlieb 		}
4721c43f1112SMaor Gottlieb 	}
4722c43f1112SMaor Gottlieb 	return 0;
4723c43f1112SMaor Gottlieb }
4724c43f1112SMaor Gottlieb 
4725e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4726e126ba97SEli Cohen {
4727e126ba97SEli Cohen 	int port;
4728e126ba97SEli Cohen 
4729508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
4730e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
4731e126ba97SEli Cohen }
4732e126ba97SEli Cohen 
473326628e2dSMark Bloch static int __get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4734e126ba97SEli Cohen {
4735e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
4736e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
4737f614fc15SDan Carpenter 	int err = -ENOMEM;
47382528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
4739e126ba97SEli Cohen 
474050ba3c18SLeon Romanovsky 	pprops = kzalloc(sizeof(*pprops), GFP_KERNEL);
4741e126ba97SEli Cohen 	if (!pprops)
4742e126ba97SEli Cohen 		goto out;
4743e126ba97SEli Cohen 
4744e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4745e126ba97SEli Cohen 	if (!dprops)
4746e126ba97SEli Cohen 		goto out;
4747e126ba97SEli Cohen 
47482528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
4749e126ba97SEli Cohen 	if (err) {
4750e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
4751e126ba97SEli Cohen 		goto out;
4752e126ba97SEli Cohen 	}
4753e126ba97SEli Cohen 
4754e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4755e126ba97SEli Cohen 	if (err) {
4756938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
4757938fe83cSSaeed Mahameed 			     port, err);
475832f69e4bSDaniel Jurgens 		goto out;
4759e126ba97SEli Cohen 	}
476032f69e4bSDaniel Jurgens 
4761938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
4762938fe83cSSaeed Mahameed 					dprops->max_pkeys;
4763938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
4764938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
476532f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
476632f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
4767e126ba97SEli Cohen 
4768e126ba97SEli Cohen out:
4769e126ba97SEli Cohen 	kfree(pprops);
4770e126ba97SEli Cohen 	kfree(dprops);
4771e126ba97SEli Cohen 
4772e126ba97SEli Cohen 	return err;
4773e126ba97SEli Cohen }
4774e126ba97SEli Cohen 
477526628e2dSMark Bloch static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
477626628e2dSMark Bloch {
477726628e2dSMark Bloch 	/* For representors use port 1, is this is the only native
477826628e2dSMark Bloch 	 * port
477926628e2dSMark Bloch 	 */
478026628e2dSMark Bloch 	if (dev->is_rep)
478126628e2dSMark Bloch 		return __get_port_caps(dev, 1);
478226628e2dSMark Bloch 	return __get_port_caps(dev, port);
478326628e2dSMark Bloch }
478426628e2dSMark Bloch 
4785e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4786e126ba97SEli Cohen {
4787e126ba97SEli Cohen 	int err;
4788e126ba97SEli Cohen 
4789e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
4790e126ba97SEli Cohen 	if (err)
4791e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4792e126ba97SEli Cohen 
479332927e28SMark Bloch 	if (dev->umrc.qp)
4794c4367a26SShamir Rabinovitch 		mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
479532927e28SMark Bloch 	if (dev->umrc.cq)
4796add08d76SChristoph Hellwig 		ib_free_cq(dev->umrc.cq);
479732927e28SMark Bloch 	if (dev->umrc.pd)
4798e126ba97SEli Cohen 		ib_dealloc_pd(dev->umrc.pd);
4799e126ba97SEli Cohen }
4800e126ba97SEli Cohen 
4801e126ba97SEli Cohen enum {
4802e126ba97SEli Cohen 	MAX_UMR_WR = 128,
4803e126ba97SEli Cohen };
4804e126ba97SEli Cohen 
4805e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
4806e126ba97SEli Cohen {
4807e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
4808e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
4809e126ba97SEli Cohen 	struct ib_pd *pd;
4810e126ba97SEli Cohen 	struct ib_cq *cq;
4811e126ba97SEli Cohen 	struct ib_qp *qp;
4812e126ba97SEli Cohen 	int ret;
4813e126ba97SEli Cohen 
4814e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4815e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4816e126ba97SEli Cohen 	if (!attr || !init_attr) {
4817e126ba97SEli Cohen 		ret = -ENOMEM;
4818e126ba97SEli Cohen 		goto error_0;
4819e126ba97SEli Cohen 	}
4820e126ba97SEli Cohen 
4821ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
4822e126ba97SEli Cohen 	if (IS_ERR(pd)) {
4823e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4824e126ba97SEli Cohen 		ret = PTR_ERR(pd);
4825e126ba97SEli Cohen 		goto error_0;
4826e126ba97SEli Cohen 	}
4827e126ba97SEli Cohen 
4828add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
4829e126ba97SEli Cohen 	if (IS_ERR(cq)) {
4830e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4831e126ba97SEli Cohen 		ret = PTR_ERR(cq);
4832e126ba97SEli Cohen 		goto error_2;
4833e126ba97SEli Cohen 	}
4834e126ba97SEli Cohen 
4835e126ba97SEli Cohen 	init_attr->send_cq = cq;
4836e126ba97SEli Cohen 	init_attr->recv_cq = cq;
4837e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4838e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
4839e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
4840e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4841e126ba97SEli Cohen 	init_attr->port_num = 1;
4842e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4843e126ba97SEli Cohen 	if (IS_ERR(qp)) {
4844e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4845e126ba97SEli Cohen 		ret = PTR_ERR(qp);
4846e126ba97SEli Cohen 		goto error_3;
4847e126ba97SEli Cohen 	}
4848e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
4849e126ba97SEli Cohen 	qp->real_qp    = qp;
4850e126ba97SEli Cohen 	qp->uobject    = NULL;
4851e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
485231fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
485331fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
4854e126ba97SEli Cohen 
4855e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
4856e126ba97SEli Cohen 	attr->port_num = 1;
4857e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4858e126ba97SEli Cohen 				IB_QP_PORT, NULL);
4859e126ba97SEli Cohen 	if (ret) {
4860e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4861e126ba97SEli Cohen 		goto error_4;
4862e126ba97SEli Cohen 	}
4863e126ba97SEli Cohen 
4864e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4865e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
4866e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
4867e126ba97SEli Cohen 
4868e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4869e126ba97SEli Cohen 	if (ret) {
4870e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4871e126ba97SEli Cohen 		goto error_4;
4872e126ba97SEli Cohen 	}
4873e126ba97SEli Cohen 
4874e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4875e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
4876e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4877e126ba97SEli Cohen 	if (ret) {
4878e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4879e126ba97SEli Cohen 		goto error_4;
4880e126ba97SEli Cohen 	}
4881e126ba97SEli Cohen 
4882e126ba97SEli Cohen 	dev->umrc.qp = qp;
4883e126ba97SEli Cohen 	dev->umrc.cq = cq;
4884e126ba97SEli Cohen 	dev->umrc.pd = pd;
4885e126ba97SEli Cohen 
4886e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
4887e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
4888e126ba97SEli Cohen 	if (ret) {
4889e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4890e126ba97SEli Cohen 		goto error_4;
4891e126ba97SEli Cohen 	}
4892e126ba97SEli Cohen 
4893e126ba97SEli Cohen 	kfree(attr);
4894e126ba97SEli Cohen 	kfree(init_attr);
4895e126ba97SEli Cohen 
4896e126ba97SEli Cohen 	return 0;
4897e126ba97SEli Cohen 
4898e126ba97SEli Cohen error_4:
4899c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_qp(qp, NULL);
490032927e28SMark Bloch 	dev->umrc.qp = NULL;
4901e126ba97SEli Cohen 
4902e126ba97SEli Cohen error_3:
4903add08d76SChristoph Hellwig 	ib_free_cq(cq);
490432927e28SMark Bloch 	dev->umrc.cq = NULL;
4905e126ba97SEli Cohen 
4906e126ba97SEli Cohen error_2:
4907e126ba97SEli Cohen 	ib_dealloc_pd(pd);
490832927e28SMark Bloch 	dev->umrc.pd = NULL;
4909e126ba97SEli Cohen 
4910e126ba97SEli Cohen error_0:
4911e126ba97SEli Cohen 	kfree(attr);
4912e126ba97SEli Cohen 	kfree(init_attr);
4913e126ba97SEli Cohen 	return ret;
4914e126ba97SEli Cohen }
4915e126ba97SEli Cohen 
49166e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
49176e8484c5SMax Gurtovoy {
49186e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
49196e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
49206e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
49216e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
49226e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
49236e8484c5SMax Gurtovoy 	default:
49246e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
49256e8484c5SMax Gurtovoy 	}
49266e8484c5SMax Gurtovoy }
49276e8484c5SMax Gurtovoy 
4928e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
4929e126ba97SEli Cohen {
4930e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
4931e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
493221a428a0SLeon Romanovsky 	struct ib_device *ibdev;
4933bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
49347722f47eSHaggai Eran 	int port;
4935e126ba97SEli Cohen 	int ret = 0;
4936e126ba97SEli Cohen 
4937e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
493821a428a0SLeon Romanovsky 	ibdev = &dev->ib_dev;
4939e126ba97SEli Cohen 
4940d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
4941d16e91daSHaggai Eran 
494221a428a0SLeon Romanovsky 	devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
494321a428a0SLeon Romanovsky 	if (!devr->p0)
494421a428a0SLeon Romanovsky 		return -ENOMEM;
494521a428a0SLeon Romanovsky 
494621a428a0SLeon Romanovsky 	devr->p0->device  = ibdev;
4947e126ba97SEli Cohen 	devr->p0->uobject = NULL;
4948e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
4949e126ba97SEli Cohen 
4950ff23dfa1SShamir Rabinovitch 	ret = mlx5_ib_alloc_pd(devr->p0, NULL);
495121a428a0SLeon Romanovsky 	if (ret)
495221a428a0SLeon Romanovsky 		goto error0;
495321a428a0SLeon Romanovsky 
4954e39afe3dSLeon Romanovsky 	devr->c0 = rdma_zalloc_drv_obj(ibdev, ib_cq);
4955e39afe3dSLeon Romanovsky 	if (!devr->c0) {
4956e39afe3dSLeon Romanovsky 		ret = -ENOMEM;
4957e126ba97SEli Cohen 		goto error1;
4958e126ba97SEli Cohen 	}
4959e39afe3dSLeon Romanovsky 
4960e126ba97SEli Cohen 	devr->c0->device = &dev->ib_dev;
4961e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
4962e126ba97SEli Cohen 
4963e39afe3dSLeon Romanovsky 	ret = mlx5_ib_create_cq(devr->c0, &cq_attr, NULL);
4964e39afe3dSLeon Romanovsky 	if (ret)
4965e39afe3dSLeon Romanovsky 		goto err_create_cq;
4966e39afe3dSLeon Romanovsky 
4967ff23dfa1SShamir Rabinovitch 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
4968e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
4969e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
4970e126ba97SEli Cohen 		goto error2;
4971e126ba97SEli Cohen 	}
4972e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
4973e126ba97SEli Cohen 	devr->x0->inode = NULL;
4974e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
4975e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
4976e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4977e126ba97SEli Cohen 
4978ff23dfa1SShamir Rabinovitch 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
4979e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
4980e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
4981e126ba97SEli Cohen 		goto error3;
4982e126ba97SEli Cohen 	}
4983e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
4984e126ba97SEli Cohen 	devr->x1->inode = NULL;
4985e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
4986e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
4987e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4988e126ba97SEli Cohen 
4989e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
4990e126ba97SEli Cohen 	attr.attr.max_sge = 1;
4991e126ba97SEli Cohen 	attr.attr.max_wr = 1;
4992e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
49931a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
4994e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
4995e126ba97SEli Cohen 
499668e326deSLeon Romanovsky 	devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
499768e326deSLeon Romanovsky 	if (!devr->s0) {
499868e326deSLeon Romanovsky 		ret = -ENOMEM;
4999e126ba97SEli Cohen 		goto error4;
5000e126ba97SEli Cohen 	}
500168e326deSLeon Romanovsky 
5002e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
5003e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
5004e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
5005e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
50061a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
500768e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
500868e326deSLeon Romanovsky 	if (ret)
500968e326deSLeon Romanovsky 		goto err_create;
501068e326deSLeon Romanovsky 
5011e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
50121a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
5013e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
5014e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
5015e126ba97SEli Cohen 
50164aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
50174aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
50184aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
50194aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
502068e326deSLeon Romanovsky 	devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
502168e326deSLeon Romanovsky 	if (!devr->s1) {
502268e326deSLeon Romanovsky 		ret = -ENOMEM;
50234aa17b28SHaggai Abramonvsky 		goto error5;
50244aa17b28SHaggai Abramonvsky 	}
502568e326deSLeon Romanovsky 
50264aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
50274aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
50284aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
50291a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
503068e326deSLeon Romanovsky 
503168e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
503268e326deSLeon Romanovsky 	if (ret)
503368e326deSLeon Romanovsky 		goto error6;
503468e326deSLeon Romanovsky 
50354aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
50361a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
50374aa17b28SHaggai Abramonvsky 
50387722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
50397722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
50407722f47eSHaggai Eran 			  pkey_change_handler);
50417722f47eSHaggai Eran 		devr->ports[port].devr = devr;
50427722f47eSHaggai Eran 	}
50437722f47eSHaggai Eran 
5044e126ba97SEli Cohen 	return 0;
5045e126ba97SEli Cohen 
504668e326deSLeon Romanovsky error6:
504768e326deSLeon Romanovsky 	kfree(devr->s1);
50484aa17b28SHaggai Abramonvsky error5:
5049c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
505068e326deSLeon Romanovsky err_create:
505168e326deSLeon Romanovsky 	kfree(devr->s0);
5052e126ba97SEli Cohen error4:
5053c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x1, NULL);
5054e126ba97SEli Cohen error3:
5055c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x0, NULL);
5056e126ba97SEli Cohen error2:
5057c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
5058e39afe3dSLeon Romanovsky err_create_cq:
5059e39afe3dSLeon Romanovsky 	kfree(devr->c0);
5060e126ba97SEli Cohen error1:
5061c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
5062e126ba97SEli Cohen error0:
506321a428a0SLeon Romanovsky 	kfree(devr->p0);
5064e126ba97SEli Cohen 	return ret;
5065e126ba97SEli Cohen }
5066e126ba97SEli Cohen 
5067e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
5068e126ba97SEli Cohen {
50697722f47eSHaggai Eran 	int port;
50707722f47eSHaggai Eran 
5071c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s1, NULL);
507268e326deSLeon Romanovsky 	kfree(devr->s1);
5073c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
507468e326deSLeon Romanovsky 	kfree(devr->s0);
5075c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x0, NULL);
5076c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x1, NULL);
5077c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
5078e39afe3dSLeon Romanovsky 	kfree(devr->c0);
5079c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
508021a428a0SLeon Romanovsky 	kfree(devr->p0);
50817722f47eSHaggai Eran 
50827722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
50835d8f6a0eSMark Bloch 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
50847722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
5085e126ba97SEli Cohen }
5086e126ba97SEli Cohen 
5087b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
5088b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
5089e53505a8SAchiad Shochat {
5090e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5091e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
5092e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
5093e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
509485c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
5095e53505a8SAchiad Shochat 	u32 ret = 0;
5096e53505a8SAchiad Shochat 
5097b02289b3SArtemy Kovalyov 	if (rep->grh_required)
5098b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
5099b02289b3SArtemy Kovalyov 
5100e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
5101b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
5102e53505a8SAchiad Shochat 
510385c7c014SDaniel Jurgens 	if (raw_support)
5104b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
510572cd5717SOr Gerlitz 
5106e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
510772cd5717SOr Gerlitz 		return ret;
5108e53505a8SAchiad Shochat 
5109e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
511072cd5717SOr Gerlitz 		return ret;
5111e53505a8SAchiad Shochat 
5112e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
5113e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
5114e53505a8SAchiad Shochat 
5115e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
5116e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
5117e53505a8SAchiad Shochat 
5118e53505a8SAchiad Shochat 	return ret;
5119e53505a8SAchiad Shochat }
5120e53505a8SAchiad Shochat 
51217738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
51227738613eSIra Weiny 			       struct ib_port_immutable *immutable)
51237738613eSIra Weiny {
51247738613eSIra Weiny 	struct ib_port_attr attr;
5125ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5126ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
5127b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
51287738613eSIra Weiny 	int err;
51297738613eSIra Weiny 
5130c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
51317738613eSIra Weiny 	if (err)
51327738613eSIra Weiny 		return err;
51337738613eSIra Weiny 
5134b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
5135b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
5136b02289b3SArtemy Kovalyov 						   &rep);
5137b02289b3SArtemy Kovalyov 		if (err)
5138b02289b3SArtemy Kovalyov 			return err;
5139b02289b3SArtemy Kovalyov 	}
5140b02289b3SArtemy Kovalyov 
51417738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
51427738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
5143b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
5144ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
5145337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
51467738613eSIra Weiny 
51477738613eSIra Weiny 	return 0;
51487738613eSIra Weiny }
51497738613eSIra Weiny 
51508e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
51518e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
51528e6efa3aSMark Bloch {
51538e6efa3aSMark Bloch 	struct ib_port_attr attr;
51548e6efa3aSMark Bloch 	int err;
51558e6efa3aSMark Bloch 
51568e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
51578e6efa3aSMark Bloch 
51588e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
51598e6efa3aSMark Bloch 	if (err)
51608e6efa3aSMark Bloch 		return err;
51618e6efa3aSMark Bloch 
51628e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
51638e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
51648e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
51658e6efa3aSMark Bloch 
51668e6efa3aSMark Bloch 	return 0;
51678e6efa3aSMark Bloch }
51688e6efa3aSMark Bloch 
51699abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
5170c7342823SIra Weiny {
5171c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
5172c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
51739abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
51749abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
51759abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
5176c7342823SIra Weiny }
5177c7342823SIra Weiny 
517845f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
51799ef9c640SAviv Heller {
51809ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
51819ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
51829ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
51839ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
51849ef9c640SAviv Heller 	int err;
51859ef9c640SAviv Heller 
51867c34ec19SAviv Heller 	if (!ns || !mlx5_lag_is_roce(mdev))
51879ef9c640SAviv Heller 		return 0;
51889ef9c640SAviv Heller 
51899ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
51909ef9c640SAviv Heller 	if (err)
51919ef9c640SAviv Heller 		return err;
51929ef9c640SAviv Heller 
51939ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
51949ef9c640SAviv Heller 	if (IS_ERR(ft)) {
51959ef9c640SAviv Heller 		err = PTR_ERR(ft);
51969ef9c640SAviv Heller 		goto err_destroy_vport_lag;
51979ef9c640SAviv Heller 	}
51989ef9c640SAviv Heller 
51999a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
52007c34ec19SAviv Heller 	dev->lag_active = true;
52019ef9c640SAviv Heller 	return 0;
52029ef9c640SAviv Heller 
52039ef9c640SAviv Heller err_destroy_vport_lag:
52049ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
52059ef9c640SAviv Heller 	return err;
52069ef9c640SAviv Heller }
52079ef9c640SAviv Heller 
520845f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
52099ef9c640SAviv Heller {
52109ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
52119ef9c640SAviv Heller 
52127c34ec19SAviv Heller 	if (dev->lag_active) {
52137c34ec19SAviv Heller 		dev->lag_active = false;
52147c34ec19SAviv Heller 
52159a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
52169a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
52179ef9c640SAviv Heller 
52189ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
52199ef9c640SAviv Heller 	}
52209ef9c640SAviv Heller }
52219ef9c640SAviv Heller 
52227fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5223fc24fc5eSAchiad Shochat {
5224e53505a8SAchiad Shochat 	int err;
5225e53505a8SAchiad Shochat 
522695579e78SMark Bloch 	dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
522795579e78SMark Bloch 	err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
52285ec8c83eSAviv Heller 	if (err) {
522995579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
5230e53505a8SAchiad Shochat 		return err;
52315ec8c83eSAviv Heller 	}
5232e53505a8SAchiad Shochat 
5233d012f5d6SOr Gerlitz 	return 0;
5234d012f5d6SOr Gerlitz }
5235d012f5d6SOr Gerlitz 
52367fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5237e126ba97SEli Cohen {
523895579e78SMark Bloch 	if (dev->port[port_num].roce.nb.notifier_call) {
523995579e78SMark Bloch 		unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
524095579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
5241e126ba97SEli Cohen 	}
5242e126ba97SEli Cohen }
5243e126ba97SEli Cohen 
5244e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
5245e126ba97SEli Cohen {
5246e126ba97SEli Cohen 	int err;
5247e126ba97SEli Cohen 
5248ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
5249e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
5250e53505a8SAchiad Shochat 		if (err)
52518e6efa3aSMark Bloch 			return err;
5252ca5b91d6SOr Gerlitz 	}
5253e53505a8SAchiad Shochat 
525445f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
52559ef9c640SAviv Heller 	if (err)
52569ef9c640SAviv Heller 		goto err_disable_roce;
52579ef9c640SAviv Heller 
5258e53505a8SAchiad Shochat 	return 0;
5259e53505a8SAchiad Shochat 
52609ef9c640SAviv Heller err_disable_roce:
5261ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
52629ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
52639ef9c640SAviv Heller 
5264e53505a8SAchiad Shochat 	return err;
5265fc24fc5eSAchiad Shochat }
5266fc24fc5eSAchiad Shochat 
526745f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
5268fc24fc5eSAchiad Shochat {
526945f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
5270ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
5271e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
5272fc24fc5eSAchiad Shochat }
5273fc24fc5eSAchiad Shochat 
5274e1f24a79SParav Pandit struct mlx5_ib_counter {
52757c16f477SKamal Heib 	const char *name;
52767c16f477SKamal Heib 	size_t offset;
52777c16f477SKamal Heib };
52787c16f477SKamal Heib 
52797c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
52807c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
52817c16f477SKamal Heib 
5282e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
52837c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
52847c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
52857c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
52867c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
52877c16f477SKamal Heib };
52887c16f477SKamal Heib 
5289e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
52907c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
52917c16f477SKamal Heib };
52927c16f477SKamal Heib 
5293e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
52947c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
52957c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
52967c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
52977c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
52987c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
52997c16f477SKamal Heib };
53007c16f477SKamal Heib 
5301e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
5302e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
5303e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
5304e1f24a79SParav Pandit 
5305e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
5306e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
5307e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
5308e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
5309e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
5310e1f24a79SParav Pandit };
5311e1f24a79SParav Pandit 
531258dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
531358dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
531458dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
531558dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
531658dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
531758dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
531858dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
531958dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
532058dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
532158dcb60aSParav Pandit };
532258dcb60aSParav Pandit 
53239f876f3dSTalat Batheesh #define INIT_EXT_PPCNT_COUNTER(_name)		\
53249f876f3dSTalat Batheesh 	{ .name = #_name, .offset =	\
53259f876f3dSTalat Batheesh 	MLX5_BYTE_OFF(ppcnt_reg, \
53269f876f3dSTalat Batheesh 		      counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
53279f876f3dSTalat Batheesh 
53289f876f3dSTalat Batheesh static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
53299f876f3dSTalat Batheesh 	INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
53309f876f3dSTalat Batheesh };
53319f876f3dSTalat Batheesh 
53323e1f000fSParav Pandit static bool is_mdev_switchdev_mode(const struct mlx5_core_dev *mdev)
53333e1f000fSParav Pandit {
53343e1f000fSParav Pandit 	return MLX5_ESWITCH_MANAGER(mdev) &&
53353e1f000fSParav Pandit 	       mlx5_ib_eswitch_mode(mdev->priv.eswitch) ==
53363e1f000fSParav Pandit 		       MLX5_ESWITCH_OFFLOADS;
53373e1f000fSParav Pandit }
53383e1f000fSParav Pandit 
5339e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
53400837e86aSMark Bloch {
53413e1f000fSParav Pandit 	int num_cnt_ports;
5342aac4492eSDaniel Jurgens 	int i;
53430837e86aSMark Bloch 
53443e1f000fSParav Pandit 	num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports;
53453e1f000fSParav Pandit 
53463e1f000fSParav Pandit 	for (i = 0; i < num_cnt_ports; i++) {
5347921c0f5bSParav Pandit 		if (dev->port[i].cnts.set_id_valid)
53480837e86aSMark Bloch 			mlx5_core_dealloc_q_counter(dev->mdev,
5349e1f24a79SParav Pandit 						    dev->port[i].cnts.set_id);
5350e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
5351e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
53527c16f477SKamal Heib 	}
53537c16f477SKamal Heib }
53547c16f477SKamal Heib 
5355e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5356e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
53577c16f477SKamal Heib {
53587c16f477SKamal Heib 	u32 num_counters;
53597c16f477SKamal Heib 
53607c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
53617c16f477SKamal Heib 
53627c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
53637c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
53647c16f477SKamal Heib 
53657c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
53667c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
536758dcb60aSParav Pandit 
536858dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
536958dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
537058dcb60aSParav Pandit 
5371e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
53727c16f477SKamal Heib 
5373e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5374e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5375e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
5376e1f24a79SParav Pandit 	}
53779f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
53789f876f3dSTalat Batheesh 		cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
53799f876f3dSTalat Batheesh 		num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
53809f876f3dSTalat Batheesh 	}
5381e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5382e1f24a79SParav Pandit 	if (!cnts->names)
53837c16f477SKamal Heib 		return -ENOMEM;
53847c16f477SKamal Heib 
5385e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
5386e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
5387e1f24a79SParav Pandit 	if (!cnts->offsets)
53887c16f477SKamal Heib 		goto err_names;
53897c16f477SKamal Heib 
53907c16f477SKamal Heib 	return 0;
53917c16f477SKamal Heib 
53927c16f477SKamal Heib err_names:
5393e1f24a79SParav Pandit 	kfree(cnts->names);
5394aac4492eSDaniel Jurgens 	cnts->names = NULL;
53957c16f477SKamal Heib 	return -ENOMEM;
53967c16f477SKamal Heib }
53977c16f477SKamal Heib 
5398e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
53997c16f477SKamal Heib 				  const char **names,
54007c16f477SKamal Heib 				  size_t *offsets)
54017c16f477SKamal Heib {
54027c16f477SKamal Heib 	int i;
54037c16f477SKamal Heib 	int j = 0;
54047c16f477SKamal Heib 
54057c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
54067c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
54077c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
54087c16f477SKamal Heib 	}
54097c16f477SKamal Heib 
54107c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
54117c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
54127c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
54137c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
54147c16f477SKamal Heib 		}
54157c16f477SKamal Heib 	}
54167c16f477SKamal Heib 
54177c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
54187c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
54197c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
54207c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
54217c16f477SKamal Heib 		}
54227c16f477SKamal Heib 	}
5423e1f24a79SParav Pandit 
542458dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
542558dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
542658dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
542758dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
542858dcb60aSParav Pandit 		}
542958dcb60aSParav Pandit 	}
543058dcb60aSParav Pandit 
5431e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5432e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5433e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
5434e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
5435e1f24a79SParav Pandit 		}
5436e1f24a79SParav Pandit 	}
54379f876f3dSTalat Batheesh 
54389f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
54399f876f3dSTalat Batheesh 		for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
54409f876f3dSTalat Batheesh 			names[j] = ext_ppcnt_cnts[i].name;
54419f876f3dSTalat Batheesh 			offsets[j] = ext_ppcnt_cnts[i].offset;
54429f876f3dSTalat Batheesh 		}
54439f876f3dSTalat Batheesh 	}
54440837e86aSMark Bloch }
54450837e86aSMark Bloch 
5446e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
54470837e86aSMark Bloch {
54483e1f000fSParav Pandit 	int num_cnt_ports;
5449aac4492eSDaniel Jurgens 	int err = 0;
54500837e86aSMark Bloch 	int i;
5451aa74be6eSYishai Hadas 	bool is_shared;
5452aa74be6eSYishai Hadas 
5453aa74be6eSYishai Hadas 	is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
54543e1f000fSParav Pandit 	num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports;
54550837e86aSMark Bloch 
54563e1f000fSParav Pandit 	for (i = 0; i < num_cnt_ports; i++) {
5457aac4492eSDaniel Jurgens 		err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5458aac4492eSDaniel Jurgens 		if (err)
5459aac4492eSDaniel Jurgens 			goto err_alloc;
54607c16f477SKamal Heib 
5461aac4492eSDaniel Jurgens 		mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5462aac4492eSDaniel Jurgens 				      dev->port[i].cnts.offsets);
5463aac4492eSDaniel Jurgens 
5464aa74be6eSYishai Hadas 		err = mlx5_cmd_alloc_q_counter(dev->mdev,
5465aa74be6eSYishai Hadas 					       &dev->port[i].cnts.set_id,
5466aa74be6eSYishai Hadas 					       is_shared ?
5467aa74be6eSYishai Hadas 					       MLX5_SHARED_RESOURCE_UID : 0);
5468aac4492eSDaniel Jurgens 		if (err) {
54690837e86aSMark Bloch 			mlx5_ib_warn(dev,
54700837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
5471aac4492eSDaniel Jurgens 				     i + 1, err);
5472aac4492eSDaniel Jurgens 			goto err_alloc;
54730837e86aSMark Bloch 		}
5474aac4492eSDaniel Jurgens 		dev->port[i].cnts.set_id_valid = true;
54750837e86aSMark Bloch 	}
54760837e86aSMark Bloch 	return 0;
54770837e86aSMark Bloch 
5478aac4492eSDaniel Jurgens err_alloc:
5479aac4492eSDaniel Jurgens 	mlx5_ib_dealloc_counters(dev);
5480aac4492eSDaniel Jurgens 	return err;
54810837e86aSMark Bloch }
54820837e86aSMark Bloch 
54833e1f000fSParav Pandit static const struct mlx5_ib_counters *get_counters(struct mlx5_ib_dev *dev,
54843e1f000fSParav Pandit 						   u8 port_num)
54853e1f000fSParav Pandit {
54863e1f000fSParav Pandit 	return is_mdev_switchdev_mode(dev->mdev) ? &dev->port[0].cnts :
54873e1f000fSParav Pandit 						   &dev->port[port_num].cnts;
54883e1f000fSParav Pandit }
54893e1f000fSParav Pandit 
54903e1f000fSParav Pandit /**
54913e1f000fSParav Pandit  * mlx5_ib_get_counters_id - Returns counters id to use for device+port
54923e1f000fSParav Pandit  * @dev:	Pointer to mlx5 IB device
54933e1f000fSParav Pandit  * @port_num:	Zero based port number
54943e1f000fSParav Pandit  *
54953e1f000fSParav Pandit  * mlx5_ib_get_counters_id() Returns counters set id to use for given
54963e1f000fSParav Pandit  * device port combination in switchdev and non switchdev mode of the
54973e1f000fSParav Pandit  * parent device.
54983e1f000fSParav Pandit  */
54993e1f000fSParav Pandit u16 mlx5_ib_get_counters_id(struct mlx5_ib_dev *dev, u8 port_num)
55003e1f000fSParav Pandit {
55013e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts = get_counters(dev, port_num);
55023e1f000fSParav Pandit 
55033e1f000fSParav Pandit 	return cnts->set_id;
55043e1f000fSParav Pandit }
55053e1f000fSParav Pandit 
55060ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
55070ad17a8fSMark Bloch 						    u8 port_num)
55080ad17a8fSMark Bloch {
55097c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
55103e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts;
55113e1f000fSParav Pandit 	bool is_switchdev = is_mdev_switchdev_mode(dev->mdev);
55120ad17a8fSMark Bloch 
55133e1f000fSParav Pandit 	if ((is_switchdev && port_num) || (!is_switchdev && !port_num))
55140ad17a8fSMark Bloch 		return NULL;
55150ad17a8fSMark Bloch 
55163e1f000fSParav Pandit 	cnts = get_counters(dev, port_num - 1);
55173e1f000fSParav Pandit 
55185dcecbc9SParav Pandit 	return rdma_alloc_hw_stats_struct(cnts->names,
55195dcecbc9SParav Pandit 					  cnts->num_q_counters +
55205dcecbc9SParav Pandit 					  cnts->num_cong_counters +
55215dcecbc9SParav Pandit 					  cnts->num_ext_ppcnt_counters,
55220ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
55230ad17a8fSMark Bloch }
55240ad17a8fSMark Bloch 
5525aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
55265dcecbc9SParav Pandit 				    const struct mlx5_ib_counters *cnts,
5527318d535cSMark Zhang 				    struct rdma_hw_stats *stats,
5528318d535cSMark Zhang 				    u16 set_id)
5529e1f24a79SParav Pandit {
5530e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5531e1f24a79SParav Pandit 	void *out;
5532e1f24a79SParav Pandit 	__be32 val;
5533e1f24a79SParav Pandit 	int ret, i;
5534e1f24a79SParav Pandit 
55351b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
5536e1f24a79SParav Pandit 	if (!out)
5537e1f24a79SParav Pandit 		return -ENOMEM;
5538e1f24a79SParav Pandit 
5539318d535cSMark Zhang 	ret = mlx5_core_query_q_counter(mdev, set_id, 0, out, outlen);
5540e1f24a79SParav Pandit 	if (ret)
5541e1f24a79SParav Pandit 		goto free;
5542e1f24a79SParav Pandit 
55435dcecbc9SParav Pandit 	for (i = 0; i < cnts->num_q_counters; i++) {
55445dcecbc9SParav Pandit 		val = *(__be32 *)(out + cnts->offsets[i]);
5545e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
5546e1f24a79SParav Pandit 	}
5547e1f24a79SParav Pandit 
5548e1f24a79SParav Pandit free:
5549e1f24a79SParav Pandit 	kvfree(out);
5550e1f24a79SParav Pandit 	return ret;
5551e1f24a79SParav Pandit }
5552e1f24a79SParav Pandit 
55539f876f3dSTalat Batheesh static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
55545dcecbc9SParav Pandit 					    const struct mlx5_ib_counters *cnts,
55559f876f3dSTalat Batheesh 					    struct rdma_hw_stats *stats)
55569f876f3dSTalat Batheesh {
55575dcecbc9SParav Pandit 	int offset = cnts->num_q_counters + cnts->num_cong_counters;
55589f876f3dSTalat Batheesh 	int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
55599f876f3dSTalat Batheesh 	int ret, i;
55609f876f3dSTalat Batheesh 	void *out;
55619f876f3dSTalat Batheesh 
55629f876f3dSTalat Batheesh 	out = kvzalloc(sz, GFP_KERNEL);
55639f876f3dSTalat Batheesh 	if (!out)
55649f876f3dSTalat Batheesh 		return -ENOMEM;
55659f876f3dSTalat Batheesh 
55669f876f3dSTalat Batheesh 	ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
55679f876f3dSTalat Batheesh 	if (ret)
55689f876f3dSTalat Batheesh 		goto free;
55699f876f3dSTalat Batheesh 
55705dcecbc9SParav Pandit 	for (i = 0; i < cnts->num_ext_ppcnt_counters; i++)
55719f876f3dSTalat Batheesh 		stats->value[i + offset] =
55729f876f3dSTalat Batheesh 			be64_to_cpup((__be64 *)(out +
55735dcecbc9SParav Pandit 				    cnts->offsets[i + offset]));
55749f876f3dSTalat Batheesh free:
55759f876f3dSTalat Batheesh 	kvfree(out);
55769f876f3dSTalat Batheesh 	return ret;
55779f876f3dSTalat Batheesh }
55789f876f3dSTalat Batheesh 
55790ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
55800ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
55817c16f477SKamal Heib 				u8 port_num, int index)
55820ad17a8fSMark Bloch {
55830ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
55843e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts = get_counters(dev, port_num - 1);
5585aac4492eSDaniel Jurgens 	struct mlx5_core_dev *mdev;
5586e1f24a79SParav Pandit 	int ret, num_counters;
5587aac4492eSDaniel Jurgens 	u8 mdev_port_num;
55880ad17a8fSMark Bloch 
55897c16f477SKamal Heib 	if (!stats)
5590e1f24a79SParav Pandit 		return -EINVAL;
55910ad17a8fSMark Bloch 
55925dcecbc9SParav Pandit 	num_counters = cnts->num_q_counters +
55935dcecbc9SParav Pandit 		       cnts->num_cong_counters +
55945dcecbc9SParav Pandit 		       cnts->num_ext_ppcnt_counters;
5595aac4492eSDaniel Jurgens 
5596aac4492eSDaniel Jurgens 	/* q_counters are per IB device, query the master mdev */
55975dcecbc9SParav Pandit 	ret = mlx5_ib_query_q_counters(dev->mdev, cnts, stats, cnts->set_id);
55980ad17a8fSMark Bloch 	if (ret)
5599e1f24a79SParav Pandit 		return ret;
56000ad17a8fSMark Bloch 
56019f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
56025dcecbc9SParav Pandit 		ret =  mlx5_ib_query_ext_ppcnt_counters(dev, cnts, stats);
56039f876f3dSTalat Batheesh 		if (ret)
56049f876f3dSTalat Batheesh 			return ret;
56059f876f3dSTalat Batheesh 	}
56069f876f3dSTalat Batheesh 
5607e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5608aac4492eSDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5609aac4492eSDaniel Jurgens 						    &mdev_port_num);
5610aac4492eSDaniel Jurgens 		if (!mdev) {
5611aac4492eSDaniel Jurgens 			/* If port is not affiliated yet, its in down state
5612aac4492eSDaniel Jurgens 			 * which doesn't have any counters yet, so it would be
5613aac4492eSDaniel Jurgens 			 * zero. So no need to read from the HCA.
5614aac4492eSDaniel Jurgens 			 */
5615aac4492eSDaniel Jurgens 			goto done;
5616aac4492eSDaniel Jurgens 		}
561771a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
561871a0ff65SMajd Dibbiny 						   stats->value +
56195dcecbc9SParav Pandit 						   cnts->num_q_counters,
56205dcecbc9SParav Pandit 						   cnts->num_cong_counters,
56215dcecbc9SParav Pandit 						   cnts->offsets +
56225dcecbc9SParav Pandit 						   cnts->num_q_counters);
5623aac4492eSDaniel Jurgens 
5624aac4492eSDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
5625e1f24a79SParav Pandit 		if (ret)
5626e1f24a79SParav Pandit 			return ret;
56270ad17a8fSMark Bloch 	}
56287c16f477SKamal Heib 
5629aac4492eSDaniel Jurgens done:
5630e1f24a79SParav Pandit 	return num_counters;
56310ad17a8fSMark Bloch }
56320ad17a8fSMark Bloch 
563318d422ceSMark Zhang static struct rdma_hw_stats *
563418d422ceSMark Zhang mlx5_ib_counter_alloc_stats(struct rdma_counter *counter)
563518d422ceSMark Zhang {
563618d422ceSMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
56375dcecbc9SParav Pandit 	const struct mlx5_ib_counters *cnts =
56383e1f000fSParav Pandit 		get_counters(dev, counter->port - 1);
563918d422ceSMark Zhang 
564018d422ceSMark Zhang 	/* Q counters are in the beginning of all counters */
56415dcecbc9SParav Pandit 	return rdma_alloc_hw_stats_struct(cnts->names,
56425dcecbc9SParav Pandit 					  cnts->num_q_counters,
564318d422ceSMark Zhang 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
564418d422ceSMark Zhang }
564518d422ceSMark Zhang 
564618d422ceSMark Zhang static int mlx5_ib_counter_update_stats(struct rdma_counter *counter)
564718d422ceSMark Zhang {
564818d422ceSMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
56493e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts =
56503e1f000fSParav Pandit 		get_counters(dev, counter->port - 1);
565118d422ceSMark Zhang 
56525dcecbc9SParav Pandit 	return mlx5_ib_query_q_counters(dev->mdev, cnts,
565318d422ceSMark Zhang 					counter->stats, counter->id);
565418d422ceSMark Zhang }
565518d422ceSMark Zhang 
565645842fc6SMark Zhang static int mlx5_ib_counter_bind_qp(struct rdma_counter *counter,
565745842fc6SMark Zhang 				   struct ib_qp *qp)
565845842fc6SMark Zhang {
565945842fc6SMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
566045842fc6SMark Zhang 	u16 cnt_set_id = 0;
566145842fc6SMark Zhang 	int err;
566245842fc6SMark Zhang 
566345842fc6SMark Zhang 	if (!counter->id) {
566445842fc6SMark Zhang 		err = mlx5_cmd_alloc_q_counter(dev->mdev,
566545842fc6SMark Zhang 					       &cnt_set_id,
566645842fc6SMark Zhang 					       MLX5_SHARED_RESOURCE_UID);
566745842fc6SMark Zhang 		if (err)
566845842fc6SMark Zhang 			return err;
566945842fc6SMark Zhang 		counter->id = cnt_set_id;
567045842fc6SMark Zhang 	}
567145842fc6SMark Zhang 
567245842fc6SMark Zhang 	err = mlx5_ib_qp_set_counter(qp, counter);
567345842fc6SMark Zhang 	if (err)
567445842fc6SMark Zhang 		goto fail_set_counter;
567545842fc6SMark Zhang 
567645842fc6SMark Zhang 	return 0;
567745842fc6SMark Zhang 
567845842fc6SMark Zhang fail_set_counter:
567945842fc6SMark Zhang 	mlx5_core_dealloc_q_counter(dev->mdev, cnt_set_id);
568045842fc6SMark Zhang 	counter->id = 0;
568145842fc6SMark Zhang 
568245842fc6SMark Zhang 	return err;
568345842fc6SMark Zhang }
568445842fc6SMark Zhang 
568545842fc6SMark Zhang static int mlx5_ib_counter_unbind_qp(struct ib_qp *qp)
568645842fc6SMark Zhang {
568745842fc6SMark Zhang 	return mlx5_ib_qp_set_counter(qp, NULL);
568845842fc6SMark Zhang }
568945842fc6SMark Zhang 
569045842fc6SMark Zhang static int mlx5_ib_counter_dealloc(struct rdma_counter *counter)
569145842fc6SMark Zhang {
569245842fc6SMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
569345842fc6SMark Zhang 
569445842fc6SMark Zhang 	return mlx5_core_dealloc_q_counter(dev->mdev, counter->id);
569545842fc6SMark Zhang }
569645842fc6SMark Zhang 
5697f6a8a19bSDenis Drozdov static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5698693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
5699f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
5700693dfd5aSErez Shitrit {
5701693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
5702f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
5703693dfd5aSErez Shitrit 
5704f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
5705693dfd5aSErez Shitrit }
5706693dfd5aSErez Shitrit 
5707fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5708fe248c3aSMaor Gottlieb {
5709fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
5710fe248c3aSMaor Gottlieb 		return;
5711fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5712fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
5713fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
5714fe248c3aSMaor Gottlieb }
5715fe248c3aSMaor Gottlieb 
571603404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
571703404e8aSMaor Gottlieb {
571803404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
571903404e8aSMaor Gottlieb 		return;
572003404e8aSMaor Gottlieb 
572103404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
5722fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5723fe248c3aSMaor Gottlieb }
5724fe248c3aSMaor Gottlieb 
5725fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5726fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
5727fe248c3aSMaor Gottlieb {
5728fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5729fe248c3aSMaor Gottlieb 	char lbuf[20];
5730fe248c3aSMaor Gottlieb 	int len;
5731fe248c3aSMaor Gottlieb 
5732fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5733fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
5734fe248c3aSMaor Gottlieb }
5735fe248c3aSMaor Gottlieb 
5736fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5737fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
5738fe248c3aSMaor Gottlieb {
5739fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5740fe248c3aSMaor Gottlieb 	u32 timeout;
5741fe248c3aSMaor Gottlieb 	u32 var;
5742fe248c3aSMaor Gottlieb 
5743fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
5744fe248c3aSMaor Gottlieb 		return -EFAULT;
5745fe248c3aSMaor Gottlieb 
5746fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5747fe248c3aSMaor Gottlieb 			1000);
5748fe248c3aSMaor Gottlieb 	if (timeout != var)
5749fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5750fe248c3aSMaor Gottlieb 			    timeout);
5751fe248c3aSMaor Gottlieb 
5752fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
5753fe248c3aSMaor Gottlieb 
5754fe248c3aSMaor Gottlieb 	return count;
5755fe248c3aSMaor Gottlieb }
5756fe248c3aSMaor Gottlieb 
5757fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
5758fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
5759fe248c3aSMaor Gottlieb 	.open	= simple_open,
5760fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
5761fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
5762fe248c3aSMaor Gottlieb };
5763fe248c3aSMaor Gottlieb 
5764fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5765fe248c3aSMaor Gottlieb {
5766fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
5767fe248c3aSMaor Gottlieb 
5768fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
5769fe248c3aSMaor Gottlieb 		return 0;
5770fe248c3aSMaor Gottlieb 
5771fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5772fe248c3aSMaor Gottlieb 	if (!dbg)
5773fe248c3aSMaor Gottlieb 		return -ENOMEM;
5774fe248c3aSMaor Gottlieb 
5775cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
5776cbafad87SSudip Mukherjee 
5777fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
5778fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
5779fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
5780fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
5781cbafad87SSudip Mukherjee 		goto out_debugfs;
5782fe248c3aSMaor Gottlieb 
5783fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
5784fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
5785fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5786fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
5787fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
5788fe248c3aSMaor Gottlieb 		goto out_debugfs;
5789fe248c3aSMaor Gottlieb 
5790fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
5791fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
5792fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5793fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
5794fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
5795fe248c3aSMaor Gottlieb 		goto out_debugfs;
5796fe248c3aSMaor Gottlieb 
5797fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
5798fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
5799fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
5800fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
5801fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
5802fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
5803fe248c3aSMaor Gottlieb 		goto out_debugfs;
5804fe248c3aSMaor Gottlieb 
5805fe248c3aSMaor Gottlieb 	return 0;
5806fe248c3aSMaor Gottlieb 
5807fe248c3aSMaor Gottlieb out_debugfs:
5808fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5809fe248c3aSMaor Gottlieb 	return -ENOMEM;
581003404e8aSMaor Gottlieb }
581103404e8aSMaor Gottlieb 
581203404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
581303404e8aSMaor Gottlieb {
581403404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
581503404e8aSMaor Gottlieb 		return;
581603404e8aSMaor Gottlieb 
581703404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
581803404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
581903404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
582003404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
582103404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
5822fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
5823fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
5824fe248c3aSMaor Gottlieb 
5825fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
5826fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
582703404e8aSMaor Gottlieb }
582803404e8aSMaor Gottlieb 
582932f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
583032f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
583132f69e4bSDaniel Jurgens {
583232f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
583332f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
583432f69e4bSDaniel Jurgens 	int comps;
583532f69e4bSDaniel Jurgens 	int err;
583632f69e4bSDaniel Jurgens 	int i;
583732f69e4bSDaniel Jurgens 
58389dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
58399dc4cfffSLeon Romanovsky 
5840a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5841a9e546e7SParav Pandit 
584232f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
584332f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
584432f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
584532f69e4bSDaniel Jurgens 		return;
584632f69e4bSDaniel Jurgens 	}
5847df097a27SSaeed Mahameed 
584832f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
584932f69e4bSDaniel Jurgens 
585032f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
585123eaf3b5SLeon Romanovsky 	if (mpi->mdev_events.notifier_call)
585223eaf3b5SLeon Romanovsky 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
585323eaf3b5SLeon Romanovsky 	mpi->mdev_events.notifier_call = NULL;
585432f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
585532f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
585632f69e4bSDaniel Jurgens 
585732f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
585832f69e4bSDaniel Jurgens 	if (comps) {
585932f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
586032f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
586132f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
586232f69e4bSDaniel Jurgens 
586332f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
586432f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
586532f69e4bSDaniel Jurgens 
586632f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
586732f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
586832f69e4bSDaniel Jurgens 	}
586932f69e4bSDaniel Jurgens 
587032f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
587132f69e4bSDaniel Jurgens 
587232f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
587332f69e4bSDaniel Jurgens 
587432f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
587532f69e4bSDaniel Jurgens 
587632f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
587732f69e4bSDaniel Jurgens 
587832f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
587932f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
588032f69e4bSDaniel Jurgens 	 * it back to the list.
588132f69e4bSDaniel Jurgens 	 */
588232f69e4bSDaniel Jurgens 	if (err)
588332f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
588432f69e4bSDaniel Jurgens 			    port_num + 1);
588532f69e4bSDaniel Jurgens 
588695579e78SMark Bloch 	ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
588732f69e4bSDaniel Jurgens }
588832f69e4bSDaniel Jurgens 
588932f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
589032f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
589132f69e4bSDaniel Jurgens {
589232f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
589332f69e4bSDaniel Jurgens 	int err;
589432f69e4bSDaniel Jurgens 
58959dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
58969dc4cfffSLeon Romanovsky 
589732f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
589832f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
58992577188eSQing Huang 		mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
590032f69e4bSDaniel Jurgens 			    port_num + 1);
590132f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
590232f69e4bSDaniel Jurgens 		return false;
590332f69e4bSDaniel Jurgens 	}
590432f69e4bSDaniel Jurgens 
590532f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
590632f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
5907df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
590832f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
590932f69e4bSDaniel Jurgens 
591032f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
591132f69e4bSDaniel Jurgens 	if (err)
591232f69e4bSDaniel Jurgens 		goto unbind;
591332f69e4bSDaniel Jurgens 
591432f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
591532f69e4bSDaniel Jurgens 	if (err)
591632f69e4bSDaniel Jurgens 		goto unbind;
591732f69e4bSDaniel Jurgens 
591832f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
591932f69e4bSDaniel Jurgens 	if (err) {
592032f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
592132f69e4bSDaniel Jurgens 			    port_num + 1);
592232f69e4bSDaniel Jurgens 		goto unbind;
592332f69e4bSDaniel Jurgens 	}
592432f69e4bSDaniel Jurgens 
5925df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5926df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5927df097a27SSaeed Mahameed 
592873eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
5929a9e546e7SParav Pandit 
593032f69e4bSDaniel Jurgens 	return true;
593132f69e4bSDaniel Jurgens 
593232f69e4bSDaniel Jurgens unbind:
593332f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
593432f69e4bSDaniel Jurgens 	return false;
593532f69e4bSDaniel Jurgens }
593632f69e4bSDaniel Jurgens 
593732f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
593832f69e4bSDaniel Jurgens {
593932f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
594032f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
594132f69e4bSDaniel Jurgens 							  port_num + 1);
594232f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
594332f69e4bSDaniel Jurgens 	int err;
594432f69e4bSDaniel Jurgens 	int i;
594532f69e4bSDaniel Jurgens 
594632f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
594732f69e4bSDaniel Jurgens 		return 0;
594832f69e4bSDaniel Jurgens 
594932f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
595032f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
595132f69e4bSDaniel Jurgens 	if (err)
595232f69e4bSDaniel Jurgens 		return err;
595332f69e4bSDaniel Jurgens 
595432f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
595532f69e4bSDaniel Jurgens 	if (err)
595632f69e4bSDaniel Jurgens 		return err;
595732f69e4bSDaniel Jurgens 
595832f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
595932f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
596032f69e4bSDaniel Jurgens 		bool bound = false;
596132f69e4bSDaniel Jurgens 
596232f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
596332f69e4bSDaniel Jurgens 		if (i == port_num) {
596432f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
596532f69e4bSDaniel Jurgens 			if (!mpi) {
596632f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
596732f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
596832f69e4bSDaniel Jurgens 				return -ENOMEM;
596932f69e4bSDaniel Jurgens 			}
597032f69e4bSDaniel Jurgens 
597132f69e4bSDaniel Jurgens 			mpi->is_master = true;
597232f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
597332f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
597432f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
597532f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
597632f69e4bSDaniel Jurgens 			mpi = NULL;
597732f69e4bSDaniel Jurgens 			continue;
597832f69e4bSDaniel Jurgens 		}
597932f69e4bSDaniel Jurgens 
598032f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
598132f69e4bSDaniel Jurgens 				    list) {
598232f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
598332f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
598432f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
598532f69e4bSDaniel Jurgens 			}
598632f69e4bSDaniel Jurgens 
598732f69e4bSDaniel Jurgens 			if (bound) {
5988c42260f1SVu Pham 				dev_dbg(mpi->mdev->device,
5989c42260f1SVu Pham 					"removing port from unaffiliated list.\n");
599032f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
599132f69e4bSDaniel Jurgens 				list_del(&mpi->list);
599232f69e4bSDaniel Jurgens 				break;
599332f69e4bSDaniel Jurgens 			}
599432f69e4bSDaniel Jurgens 		}
599532f69e4bSDaniel Jurgens 		if (!bound) {
599632f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
599732f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
599832f69e4bSDaniel Jurgens 				    i + 1);
599932f69e4bSDaniel Jurgens 		}
600032f69e4bSDaniel Jurgens 	}
600132f69e4bSDaniel Jurgens 
600232f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
600332f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
600432f69e4bSDaniel Jurgens 	return err;
600532f69e4bSDaniel Jurgens }
600632f69e4bSDaniel Jurgens 
600732f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
600832f69e4bSDaniel Jurgens {
600932f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
601032f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
601132f69e4bSDaniel Jurgens 							  port_num + 1);
601232f69e4bSDaniel Jurgens 	int i;
601332f69e4bSDaniel Jurgens 
601432f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
601532f69e4bSDaniel Jurgens 		return;
601632f69e4bSDaniel Jurgens 
601732f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
601832f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
601932f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
602032f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
602132f69e4bSDaniel Jurgens 			if (i == port_num) {
602232f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
602332f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
602432f69e4bSDaniel Jurgens 			} else {
602532f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
602632f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
602732f69e4bSDaniel Jurgens 			}
602832f69e4bSDaniel Jurgens 		}
602932f69e4bSDaniel Jurgens 	}
603032f69e4bSDaniel Jurgens 
603132f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
603232f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
603332f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
603432f69e4bSDaniel Jurgens 
603532f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
603632f69e4bSDaniel Jurgens }
603732f69e4bSDaniel Jurgens 
60389a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
60399a119cd5SJason Gunthorpe 	mlx5_ib_dm,
60409a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
604124da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
60429a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
604324da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
604483bb4442SJason Gunthorpe 			    UA_MANDATORY),
60459a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
604624da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
60473b113a1eSAriel Levkovich 			    UA_OPTIONAL),
60483b113a1eSAriel Levkovich 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
60493b113a1eSAriel Levkovich 			     enum mlx5_ib_uapi_dm_type,
60503b113a1eSAriel Levkovich 			     UA_OPTIONAL));
605124da0016SAriel Levkovich 
60529a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
60539a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
60549a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
6055c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
6056bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
6057bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
6058c6475a0bSAviad Yehezkel 
60590cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
60600cbf432dSJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
606136e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
60620cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
60630cbf432dSJason Gunthorpe #endif
60648c84660bSMatan Barak 
60650cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
60660cbf432dSJason Gunthorpe 				&mlx5_ib_flow_action),
60670cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
60680cbf432dSJason Gunthorpe 	{}
60690cbf432dSJason Gunthorpe };
60708c84660bSMatan Barak 
60711a1e03dcSRaed Salem static int mlx5_ib_read_counters(struct ib_counters *counters,
60721a1e03dcSRaed Salem 				 struct ib_counters_read_attr *read_attr,
60731a1e03dcSRaed Salem 				 struct uverbs_attr_bundle *attrs)
60741a1e03dcSRaed Salem {
60751a1e03dcSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
60761a1e03dcSRaed Salem 	struct mlx5_read_counters_attr mread_attr = {};
60771a1e03dcSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc;
60781a1e03dcSRaed Salem 	int ret, i;
60791a1e03dcSRaed Salem 
60801a1e03dcSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
60811a1e03dcSRaed Salem 	if (mcounters->cntrs_max_index > read_attr->ncounters) {
60821a1e03dcSRaed Salem 		ret = -EINVAL;
60831a1e03dcSRaed Salem 		goto err_bound;
60841a1e03dcSRaed Salem 	}
60851a1e03dcSRaed Salem 
60861a1e03dcSRaed Salem 	mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
60871a1e03dcSRaed Salem 				 GFP_KERNEL);
60881a1e03dcSRaed Salem 	if (!mread_attr.out) {
60891a1e03dcSRaed Salem 		ret = -ENOMEM;
60901a1e03dcSRaed Salem 		goto err_bound;
60911a1e03dcSRaed Salem 	}
60921a1e03dcSRaed Salem 
60931a1e03dcSRaed Salem 	mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
60941a1e03dcSRaed Salem 	mread_attr.flags = read_attr->flags;
60951a1e03dcSRaed Salem 	ret = mcounters->read_counters(counters->device, &mread_attr);
60961a1e03dcSRaed Salem 	if (ret)
60971a1e03dcSRaed Salem 		goto err_read;
60981a1e03dcSRaed Salem 
60991a1e03dcSRaed Salem 	/* do the pass over the counters data array to assign according to the
61001a1e03dcSRaed Salem 	 * descriptions and indexing pairs
61011a1e03dcSRaed Salem 	 */
61021a1e03dcSRaed Salem 	desc = mcounters->counters_data;
61031a1e03dcSRaed Salem 	for (i = 0; i < mcounters->ncounters; i++)
61041a1e03dcSRaed Salem 		read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
61051a1e03dcSRaed Salem 
61061a1e03dcSRaed Salem err_read:
61071a1e03dcSRaed Salem 	kfree(mread_attr.out);
61081a1e03dcSRaed Salem err_bound:
61091a1e03dcSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
61101a1e03dcSRaed Salem 	return ret;
61111a1e03dcSRaed Salem }
61121a1e03dcSRaed Salem 
6113b29e2a13SRaed Salem static int mlx5_ib_destroy_counters(struct ib_counters *counters)
6114b29e2a13SRaed Salem {
6115b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
6116b29e2a13SRaed Salem 
61173b3233fbSRaed Salem 	counters_clear_description(counters);
61183b3233fbSRaed Salem 	if (mcounters->hw_cntrs_hndl)
61193b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(counters->device)->mdev,
61203b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
61213b3233fbSRaed Salem 
6122b29e2a13SRaed Salem 	kfree(mcounters);
6123b29e2a13SRaed Salem 
6124b29e2a13SRaed Salem 	return 0;
6125b29e2a13SRaed Salem }
6126b29e2a13SRaed Salem 
6127b29e2a13SRaed Salem static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
6128b29e2a13SRaed Salem 						   struct uverbs_attr_bundle *attrs)
6129b29e2a13SRaed Salem {
6130b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters;
6131b29e2a13SRaed Salem 
6132b29e2a13SRaed Salem 	mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
6133b29e2a13SRaed Salem 	if (!mcounters)
6134b29e2a13SRaed Salem 		return ERR_PTR(-ENOMEM);
6135b29e2a13SRaed Salem 
61363b3233fbSRaed Salem 	mutex_init(&mcounters->mcntrs_mutex);
61373b3233fbSRaed Salem 
6138b29e2a13SRaed Salem 	return &mcounters->ibcntrs;
6139b29e2a13SRaed Salem }
6140b29e2a13SRaed Salem 
6141fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
6142e126ba97SEli Cohen {
614332f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
6144806b101bSJason Gunthorpe 	WARN_ON(!xa_empty(&dev->odp_mkeys));
6145806b101bSJason Gunthorpe 	cleanup_srcu_struct(&dev->odp_srcu);
61464056b12eSAriel Levkovich 
614750211ec9SJason Gunthorpe 	WARN_ON(!xa_empty(&dev->sig_mrs));
61484056b12eSAriel Levkovich 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
614916c1975fSMark Bloch }
615016c1975fSMark Bloch 
6151fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
615216c1975fSMark Bloch {
615316c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
6154e126ba97SEli Cohen 	int err;
615532f69e4bSDaniel Jurgens 	int i;
6156e126ba97SEli Cohen 
615732f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
615832f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
615995579e78SMark Bloch 		rwlock_init(&dev->port[i].roce.netdev_lock);
6160d3b5cc1cSMark Bloch 		dev->port[i].roce.dev = dev;
6161d3b5cc1cSMark Bloch 		dev->port[i].roce.native_port_num = i + 1;
6162d3b5cc1cSMark Bloch 		dev->port[i].roce.last_port_state = IB_PORT_DOWN;
616332f69e4bSDaniel Jurgens 	}
616432f69e4bSDaniel Jurgens 
616500815752SMoni Shoua 	mlx5_ib_internal_fill_odp_caps(dev);
616600815752SMoni Shoua 
616732f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
6168e126ba97SEli Cohen 	if (err)
6169da796ccbSMark Bloch 		return err;
6170e126ba97SEli Cohen 
6171a989ea01SMark Bloch 	err = set_has_smi_cap(dev);
6172a989ea01SMark Bloch 	if (err)
6173a989ea01SMark Bloch 		return err;
6174e126ba97SEli Cohen 
617532f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
617632f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
617732f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
617832f69e4bSDaniel Jurgens 			if (err)
617932f69e4bSDaniel Jurgens 				break;
618032f69e4bSDaniel Jurgens 		}
618132f69e4bSDaniel Jurgens 	} else {
618232f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
618332f69e4bSDaniel Jurgens 	}
618432f69e4bSDaniel Jurgens 	if (err)
618532f69e4bSDaniel Jurgens 		goto err_mp;
618632f69e4bSDaniel Jurgens 
61871b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
6188e126ba97SEli Cohen 		get_ext_port_caps(dev);
6189e126ba97SEli Cohen 
6190e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
6191c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
6192e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
6193f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
6194c42260f1SVu Pham 	dev->ib_dev.dev.parent		= mdev->device;
6195e126ba97SEli Cohen 
61963cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
61973cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
61983cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
6199806b101bSJason Gunthorpe 	xa_init(&dev->odp_mkeys);
620050211ec9SJason Gunthorpe 	xa_init(&dev->sig_mrs);
62013cc297dbSMark Bloch 
62023b113a1eSAriel Levkovich 	spin_lock_init(&dev->dm.lock);
62033b113a1eSAriel Levkovich 	dev->dm.dev = mdev;
620424da0016SAriel Levkovich 
6205806b101bSJason Gunthorpe 	err = init_srcu_struct(&dev->odp_srcu);
6206a6bc3875SMoni Shoua 	if (err)
6207c9b9dcb4SAriel Levkovich 		goto err_mp;
62083cc297dbSMark Bloch 
620916c1975fSMark Bloch 	return 0;
621025c13324SAriel Levkovich 
621132f69e4bSDaniel Jurgens err_mp:
621232f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
621316c1975fSMark Bloch 
621416c1975fSMark Bloch 	return -ENOMEM;
621516c1975fSMark Bloch }
621616c1975fSMark Bloch 
62179a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
62189a4ca38dSMark Bloch {
62199a4ca38dSMark Bloch 	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
62209a4ca38dSMark Bloch 
62219a4ca38dSMark Bloch 	if (!dev->flow_db)
62229a4ca38dSMark Bloch 		return -ENOMEM;
62239a4ca38dSMark Bloch 
62249a4ca38dSMark Bloch 	mutex_init(&dev->flow_db->lock);
62259a4ca38dSMark Bloch 
62269a4ca38dSMark Bloch 	return 0;
62279a4ca38dSMark Bloch }
62289a4ca38dSMark Bloch 
62299a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
62309a4ca38dSMark Bloch {
62319a4ca38dSMark Bloch 	kfree(dev->flow_db);
62329a4ca38dSMark Bloch }
62339a4ca38dSMark Bloch 
623496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
62357a154142SJason Gunthorpe 	.owner = THIS_MODULE,
6236b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_MLX5,
623772c6ec18SJason Gunthorpe 	.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION,
6238b9560a41SJason Gunthorpe 
623996458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
624096458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
62416c984472SMax Gurtovoy 	.alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
624296458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
624396458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
624496458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
624596458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
624696458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
624796458233SKamal Heib 	.create_counters = mlx5_ib_create_counters,
624896458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
624996458233SKamal Heib 	.create_flow = mlx5_ib_create_flow,
625096458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
625196458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
625296458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
625396458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
625496458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
625596458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
625696458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
625796458233SKamal Heib 	.destroy_counters = mlx5_ib_destroy_counters,
625896458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
625996458233SKamal Heib 	.destroy_flow = mlx5_ib_destroy_flow,
626096458233SKamal Heib 	.destroy_flow_action = mlx5_ib_destroy_flow_action,
626196458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
626296458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
626396458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
626496458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
626596458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
626696458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
626711f552e2SMichael Guralnik 	.enable_driver = mlx5_ib_enable_driver,
6268e1b95ae0SErez Alfasi 	.fill_res_entry = mlx5_ib_fill_res_entry,
62694061ff7aSErez Alfasi 	.fill_stat_entry = mlx5_ib_fill_stat_entry,
627096458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
627196458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
627296458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
627396458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
62746c984472SMax Gurtovoy 	.map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
627596458233SKamal Heib 	.mmap = mlx5_ib_mmap,
627696458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
627796458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
627896458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
627996458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
628096458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
628196458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
628296458233SKamal Heib 	.post_recv = mlx5_ib_post_recv,
628396458233SKamal Heib 	.post_send = mlx5_ib_post_send,
628496458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
628596458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
628696458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
628796458233SKamal Heib 	.query_device = mlx5_ib_query_device,
628896458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
628996458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
629096458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
629196458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
629296458233SKamal Heib 	.read_counters = mlx5_ib_read_counters,
629396458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
629496458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
629596458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
629696458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
6297d3456914SLeon Romanovsky 
6298d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
6299e39afe3dSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
630021a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
630168e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
6302a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
630396458233SKamal Heib };
630496458233SKamal Heib 
630596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
630696458233SKamal Heib 	.create_flow_action_esp = mlx5_ib_create_flow_action_esp,
630796458233SKamal Heib 	.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
630896458233SKamal Heib };
630996458233SKamal Heib 
631096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
631196458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
631296458233SKamal Heib };
631396458233SKamal Heib 
631496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
631596458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
631696458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
631796458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
631896458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
631996458233SKamal Heib };
632096458233SKamal Heib 
632196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
632296458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
632396458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
632496458233SKamal Heib };
632596458233SKamal Heib 
632696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
632796458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
632896458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
632996458233SKamal Heib };
633096458233SKamal Heib 
633196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
633296458233SKamal Heib 	.alloc_dm = mlx5_ib_alloc_dm,
633396458233SKamal Heib 	.dealloc_dm = mlx5_ib_dealloc_dm,
633496458233SKamal Heib 	.reg_dm_mr = mlx5_ib_reg_dm_mr,
633596458233SKamal Heib };
633696458233SKamal Heib 
6337fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
633816c1975fSMark Bloch {
633916c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
634016c1975fSMark Bloch 	int err;
634116c1975fSMark Bloch 
6342e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
6343e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
6344e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
6345e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
6346e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
6347e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
634841c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
634941c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
6350e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
635156e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
6352e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
6353e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
6354e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
6355e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
6356e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
6357e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
6358e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
6359e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
6360e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
6361e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
6362e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
6363e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
6364e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
6365e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
6366e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
6367e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
6368e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
63691707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
6370d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
6371d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
63727d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
6373b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
637496458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ)	|
637596458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW)	|
637696458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
6377e126ba97SEli Cohen 
6378f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
6379f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
638096458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
638196458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
63828e959601SNiranjana Vishwanathapura 
638396458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
638496458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
63857c2344c3SMaor Gottlieb 
63866e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
63876e8484c5SMax Gurtovoy 
6388d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
6389d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
6390d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
6391d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
639296458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
6393d2370e0aSMatan Barak 	}
6394d2370e0aSMatan Barak 
6395938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
6396e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
6397e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
6398e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
639996458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
6400e126ba97SEli Cohen 	}
6401e126ba97SEli Cohen 
640225c13324SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic) ||
640325c13324SAriel Levkovich 	    MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
640425c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
640596458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
640624da0016SAriel Levkovich 
6407dfb631a1SJason Gunthorpe 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
640896458233SKamal Heib 	    MLX5_ACCEL_IPSEC_CAP_DEVICE)
640996458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
641096458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
641181e30880SYishai Hadas 
641236e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
641336e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
641481e30880SYishai Hadas 
6415e126ba97SEli Cohen 	err = init_node_data(dev);
6416e126ba97SEli Cohen 	if (err)
641716c1975fSMark Bloch 		return err;
6418e126ba97SEli Cohen 
6419c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
6420e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6421e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
6422a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
6423c8b89924SMark Bloch 
642496e2fd73SLeon Romanovsky 	dev->ib_dev.use_cq_dim = true;
642596e2fd73SLeon Romanovsky 
642616c1975fSMark Bloch 	return 0;
642716c1975fSMark Bloch }
642816c1975fSMark Bloch 
642996458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
643096458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
643196458233SKamal Heib 	.query_port = mlx5_ib_query_port,
643296458233SKamal Heib };
643396458233SKamal Heib 
64348e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
643516c1975fSMark Bloch {
643696458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
64378e6efa3aSMark Bloch 	return 0;
64388e6efa3aSMark Bloch }
64398e6efa3aSMark Bloch 
644096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
644196458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
644296458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
644396458233SKamal Heib };
644496458233SKamal Heib 
6445fb652d32SMark Bloch static int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
64468e6efa3aSMark Bloch {
644796458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
64488e6efa3aSMark Bloch 	return 0;
64498e6efa3aSMark Bloch }
64508e6efa3aSMark Bloch 
645196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
645296458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
645396458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
645496458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
645596458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
645696458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
645796458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
645896458233SKamal Heib };
645996458233SKamal Heib 
6460e3f1ed1fSLeon Romanovsky static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
64618e6efa3aSMark Bloch {
6462e3f1ed1fSLeon Romanovsky 	u8 port_num;
64637fd8aefbSDaniel Jurgens 
6464c11a226aSMark Bloch 	dev->ib_dev.uverbs_ex_cmd_mask |=
6465c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6466c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6467c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6468c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6469c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
647096458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
64718e6efa3aSMark Bloch 
6472e3f1ed1fSLeon Romanovsky 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6473e3f1ed1fSLeon Romanovsky 
647426628e2dSMark Bloch 	/* Register only for native ports */
64758e6efa3aSMark Bloch 	return mlx5_add_netdev_notifier(dev, port_num);
64768e6efa3aSMark Bloch }
64778e6efa3aSMark Bloch 
64788e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
64798e6efa3aSMark Bloch {
64808e6efa3aSMark Bloch 	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
64818e6efa3aSMark Bloch 
64828e6efa3aSMark Bloch 	mlx5_remove_netdev_notifier(dev, port_num);
64838e6efa3aSMark Bloch }
64848e6efa3aSMark Bloch 
6485fb652d32SMark Bloch static int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
64868e6efa3aSMark Bloch {
64878e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
64888e6efa3aSMark Bloch 	enum rdma_link_layer ll;
64898e6efa3aSMark Bloch 	int port_type_cap;
64908e6efa3aSMark Bloch 	int err = 0;
64918e6efa3aSMark Bloch 
64928e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
64938e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
64948e6efa3aSMark Bloch 
64958e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET)
6496e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
64978e6efa3aSMark Bloch 
649816c1975fSMark Bloch 	return err;
6499fc24fc5eSAchiad Shochat }
6500fc24fc5eSAchiad Shochat 
6501fb652d32SMark Bloch static void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
65028e6efa3aSMark Bloch {
65038e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
65048e6efa3aSMark Bloch }
65058e6efa3aSMark Bloch 
65068e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
65078e6efa3aSMark Bloch {
65088e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
65098e6efa3aSMark Bloch 	enum rdma_link_layer ll;
65108e6efa3aSMark Bloch 	int port_type_cap;
65118e6efa3aSMark Bloch 	int err;
65128e6efa3aSMark Bloch 
65138e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
65148e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
65158e6efa3aSMark Bloch 
65168e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
6517e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
65188e6efa3aSMark Bloch 		if (err)
65198e6efa3aSMark Bloch 			return err;
65208e6efa3aSMark Bloch 
6521e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
65228e6efa3aSMark Bloch 		if (err)
65238e6efa3aSMark Bloch 			goto cleanup;
65248e6efa3aSMark Bloch 	}
65258e6efa3aSMark Bloch 
652616c1975fSMark Bloch 	return 0;
65278e6efa3aSMark Bloch cleanup:
65288e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
65298e6efa3aSMark Bloch 
65308e6efa3aSMark Bloch 	return err;
653145bded2cSKamal Heib }
65326aec21f6SHaggai Eran 
653316c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
653416c1975fSMark Bloch {
653516c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
653616c1975fSMark Bloch 	enum rdma_link_layer ll;
653716c1975fSMark Bloch 	int port_type_cap;
65384a2da0b8SParav Pandit 
653916c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
654016c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
654116c1975fSMark Bloch 
654216c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
654316c1975fSMark Bloch 		mlx5_disable_eth(dev);
65448e6efa3aSMark Bloch 		mlx5_ib_stage_common_roce_cleanup(dev);
654516c1975fSMark Bloch 	}
654616c1975fSMark Bloch }
654716c1975fSMark Bloch 
6548fb652d32SMark Bloch static int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
654916c1975fSMark Bloch {
655016c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
655116c1975fSMark Bloch }
655216c1975fSMark Bloch 
6553fb652d32SMark Bloch static void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
655416c1975fSMark Bloch {
655516c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
655616c1975fSMark Bloch }
655716c1975fSMark Bloch 
655816c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
655916c1975fSMark Bloch {
656016c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
656116c1975fSMark Bloch }
656216c1975fSMark Bloch 
6563f3ffed0cSKamal Heib static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
6564d5d284b8SSaeed Mahameed {
6565d5d284b8SSaeed Mahameed 	mlx5_ib_odp_cleanup_one(dev);
6566d5d284b8SSaeed Mahameed }
6567d5d284b8SSaeed Mahameed 
656896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
656996458233SKamal Heib 	.alloc_hw_stats = mlx5_ib_alloc_hw_stats,
657096458233SKamal Heib 	.get_hw_stats = mlx5_ib_get_hw_stats,
657145842fc6SMark Zhang 	.counter_bind_qp = mlx5_ib_counter_bind_qp,
657245842fc6SMark Zhang 	.counter_unbind_qp = mlx5_ib_counter_unbind_qp,
657345842fc6SMark Zhang 	.counter_dealloc = mlx5_ib_counter_dealloc,
657418d422ceSMark Zhang 	.counter_alloc_stats = mlx5_ib_counter_alloc_stats,
657518d422ceSMark Zhang 	.counter_update_stats = mlx5_ib_counter_update_stats,
657696458233SKamal Heib };
657796458233SKamal Heib 
6578fb652d32SMark Bloch static int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
657916c1975fSMark Bloch {
65805e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
658196458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
65825e1e7612SMark Bloch 
658316c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
65845e1e7612SMark Bloch 	}
658516c1975fSMark Bloch 
658616c1975fSMark Bloch 	return 0;
658716c1975fSMark Bloch }
658816c1975fSMark Bloch 
6589fb652d32SMark Bloch static void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
659016c1975fSMark Bloch {
659116c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
659216c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
659316c1975fSMark Bloch }
659416c1975fSMark Bloch 
659516c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
659616c1975fSMark Bloch {
659773eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
6598a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
659973eb8f03SGreg Kroah-Hartman 	return 0;
660016c1975fSMark Bloch }
660116c1975fSMark Bloch 
660216c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
660316c1975fSMark Bloch {
6604a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
6605a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
660616c1975fSMark Bloch }
660716c1975fSMark Bloch 
660816c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
660916c1975fSMark Bloch {
66105fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
6611444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
661216c1975fSMark Bloch }
661316c1975fSMark Bloch 
661416c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
661516c1975fSMark Bloch {
661616c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
661716c1975fSMark Bloch }
661816c1975fSMark Bloch 
6619fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
662016c1975fSMark Bloch {
662116c1975fSMark Bloch 	int err;
66225fe9dec0SEli Cohen 
66235fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
66245fe9dec0SEli Cohen 	if (err)
662516c1975fSMark Bloch 		return err;
66265fe9dec0SEli Cohen 
66275fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
66285fe9dec0SEli Cohen 	if (err)
662916c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
66305fe9dec0SEli Cohen 
663116c1975fSMark Bloch 	return err;
663216c1975fSMark Bloch }
66330837e86aSMark Bloch 
6634fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
663516c1975fSMark Bloch {
663616c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
663716c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
663816c1975fSMark Bloch }
6639e126ba97SEli Cohen 
6640fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
664116c1975fSMark Bloch {
6642e349f858SJason Gunthorpe 	const char *name;
6643e349f858SJason Gunthorpe 
6644508a523fSParav Pandit 	rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
66457c34ec19SAviv Heller 	if (!mlx5_lag_is_roce(dev->mdev))
6646e349f858SJason Gunthorpe 		name = "mlx5_%d";
6647e349f858SJason Gunthorpe 	else
6648e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
6649ea4baf7fSParav Pandit 	return ib_register_device(&dev->ib_dev, name);
665016c1975fSMark Bloch }
665116c1975fSMark Bloch 
6652fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
665342cea83fSMark Bloch {
665442cea83fSMark Bloch 	destroy_umrc_res(dev);
665542cea83fSMark Bloch }
665642cea83fSMark Bloch 
6657fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
665816c1975fSMark Bloch {
665916c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
666016c1975fSMark Bloch }
666116c1975fSMark Bloch 
6662fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
666316c1975fSMark Bloch {
666416c1975fSMark Bloch 	return create_umr_res(dev);
666516c1975fSMark Bloch }
666616c1975fSMark Bloch 
666716c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
666816c1975fSMark Bloch {
666903404e8aSMaor Gottlieb 	init_delay_drop(dev);
667003404e8aSMaor Gottlieb 
667116c1975fSMark Bloch 	return 0;
667216c1975fSMark Bloch }
667316c1975fSMark Bloch 
667416c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
667516c1975fSMark Bloch {
667616c1975fSMark Bloch 	cancel_delay_drop(dev);
667716c1975fSMark Bloch }
667816c1975fSMark Bloch 
6679df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
6680df097a27SSaeed Mahameed {
6681df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
6682df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
6683df097a27SSaeed Mahameed 	return 0;
6684df097a27SSaeed Mahameed }
6685df097a27SSaeed Mahameed 
6686df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
6687df097a27SSaeed Mahameed {
6688df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
6689df097a27SSaeed Mahameed }
6690df097a27SSaeed Mahameed 
669181773ce5SLeon Romanovsky static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
669281773ce5SLeon Romanovsky {
669381773ce5SLeon Romanovsky 	int uid;
669481773ce5SLeon Romanovsky 
6695fb98153bSYishai Hadas 	uid = mlx5_ib_devx_create(dev, false);
6696e337dd53SYishai Hadas 	if (uid > 0) {
669781773ce5SLeon Romanovsky 		dev->devx_whitelist_uid = uid;
6698e337dd53SYishai Hadas 		mlx5_ib_devx_init_event_table(dev);
6699e337dd53SYishai Hadas 	}
670081773ce5SLeon Romanovsky 
670181773ce5SLeon Romanovsky 	return 0;
670281773ce5SLeon Romanovsky }
670381773ce5SLeon Romanovsky static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
670481773ce5SLeon Romanovsky {
6705e337dd53SYishai Hadas 	if (dev->devx_whitelist_uid) {
6706e337dd53SYishai Hadas 		mlx5_ib_devx_cleanup_event_table(dev);
670781773ce5SLeon Romanovsky 		mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
670881773ce5SLeon Romanovsky 	}
6709e337dd53SYishai Hadas }
671081773ce5SLeon Romanovsky 
671111f552e2SMichael Guralnik int mlx5_ib_enable_driver(struct ib_device *dev)
671211f552e2SMichael Guralnik {
671311f552e2SMichael Guralnik 	struct mlx5_ib_dev *mdev = to_mdev(dev);
671411f552e2SMichael Guralnik 	int ret;
671511f552e2SMichael Guralnik 
671611f552e2SMichael Guralnik 	ret = mlx5_ib_test_wc(mdev);
671711f552e2SMichael Guralnik 	mlx5_ib_dbg(mdev, "Write-Combining %s",
671811f552e2SMichael Guralnik 		    mdev->wc_support ? "supported" : "not supported");
671911f552e2SMichael Guralnik 
672011f552e2SMichael Guralnik 	return ret;
672111f552e2SMichael Guralnik }
672211f552e2SMichael Guralnik 
6723b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
672416c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
672516c1975fSMark Bloch 		      int stage)
672616c1975fSMark Bloch {
672716c1975fSMark Bloch 	/* Number of stages to cleanup */
672816c1975fSMark Bloch 	while (stage) {
672916c1975fSMark Bloch 		stage--;
673016c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
673116c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
673216c1975fSMark Bloch 	}
67334a6dc855SMark Bloch 
6734da796ccbSMark Bloch 	kfree(dev->port);
67354a6dc855SMark Bloch 	ib_dealloc_device(&dev->ib_dev);
673616c1975fSMark Bloch }
673716c1975fSMark Bloch 
6738b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
673916c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
674016c1975fSMark Bloch {
674116c1975fSMark Bloch 	int err;
674216c1975fSMark Bloch 	int i;
674316c1975fSMark Bloch 
674416c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
674516c1975fSMark Bloch 		if (profile->stage[i].init) {
674616c1975fSMark Bloch 			err = profile->stage[i].init(dev);
674716c1975fSMark Bloch 			if (err)
674816c1975fSMark Bloch 				goto err_out;
674916c1975fSMark Bloch 		}
675016c1975fSMark Bloch 	}
675116c1975fSMark Bloch 
675216c1975fSMark Bloch 	dev->profile = profile;
6753e126ba97SEli Cohen 	dev->ib_active = true;
6754e126ba97SEli Cohen 
67559603b61dSJack Morgenstein 	return dev;
6756e126ba97SEli Cohen 
675716c1975fSMark Bloch err_out:
675816c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
6759e126ba97SEli Cohen 
67609603b61dSJack Morgenstein 	return NULL;
6761e126ba97SEli Cohen }
6762e126ba97SEli Cohen 
676316c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
676416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
676516c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
676616c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
67679a4ca38dSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
67689a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_init,
67699a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
677016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
677116c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
677216c1975fSMark Bloch 		     NULL),
67738e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
67748e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
67758e6efa3aSMark Bloch 		     NULL),
677616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
677716c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
677816c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
6779f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6780f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
6781f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
678216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
678316c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
678416c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
6785df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6786df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
6787df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
678816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
678916c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
6790d5d284b8SSaeed Mahameed 		     mlx5_ib_stage_odp_cleanup),
679116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
679216c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
679316c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
679416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
679516c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
679616c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
679716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
679816c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
679916c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
680016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
680116c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
680216c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
680342cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
680442cea83fSMark Bloch 		     NULL,
680542cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
680681773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
680781773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_init,
680881773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_cleanup),
680916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
681016c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
681116c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
681242cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
681342cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
681442cea83fSMark Bloch 		     NULL),
681516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
681616c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
681716c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
681816c1975fSMark Bloch };
681916c1975fSMark Bloch 
6820f0666f1fSBodong Wang const struct mlx5_ib_profile uplink_rep_profile = {
6821b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
6822b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
6823b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
6824b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6825b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_init,
6826b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
6827b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6828b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
6829b5ca15adSMark Bloch 		     NULL),
6830b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6831b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_non_default_cb,
6832b5ca15adSMark Bloch 		     NULL),
6833b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6834b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_init,
6835b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_cleanup),
6836f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6837f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
6838f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
6839b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6840b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_init,
6841b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
6842df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6843df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
6844df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
6845b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6846b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_init,
6847b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
6848b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
6849b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
6850b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
6851b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6852b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
6853b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
685403fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
685503fe2debSDavid S. Miller 		     NULL,
685603fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
68577f575103SMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
68587f575103SMark Bloch 		     mlx5_ib_stage_devx_init,
68597f575103SMark Bloch 		     mlx5_ib_stage_devx_cleanup),
6860b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6861b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
6862b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
686303fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
686403fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
686503fe2debSDavid S. Miller 		     NULL),
6866b5ca15adSMark Bloch };
6867b5ca15adSMark Bloch 
6868e3f1ed1fSLeon Romanovsky static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
686932f69e4bSDaniel Jurgens {
687032f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
687132f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
687232f69e4bSDaniel Jurgens 	bool bound = false;
687332f69e4bSDaniel Jurgens 	int err;
687432f69e4bSDaniel Jurgens 
687532f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
687632f69e4bSDaniel Jurgens 	if (!mpi)
687732f69e4bSDaniel Jurgens 		return NULL;
687832f69e4bSDaniel Jurgens 
687932f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
688032f69e4bSDaniel Jurgens 
688132f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
688232f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
688332f69e4bSDaniel Jurgens 	if (err) {
688432f69e4bSDaniel Jurgens 		kfree(mpi);
688532f69e4bSDaniel Jurgens 		return NULL;
688632f69e4bSDaniel Jurgens 	}
688732f69e4bSDaniel Jurgens 
688832f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
688932f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
689032f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
689132f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
689232f69e4bSDaniel Jurgens 
689332f69e4bSDaniel Jurgens 		if (bound) {
689432f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
689532f69e4bSDaniel Jurgens 			break;
689632f69e4bSDaniel Jurgens 		}
689732f69e4bSDaniel Jurgens 	}
689832f69e4bSDaniel Jurgens 
689932f69e4bSDaniel Jurgens 	if (!bound) {
690032f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
6901c42260f1SVu Pham 		dev_dbg(mdev->device,
6902c42260f1SVu Pham 			"no suitable IB device found to bind to, added to unaffiliated list.\n");
690332f69e4bSDaniel Jurgens 	}
690432f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
690532f69e4bSDaniel Jurgens 
690632f69e4bSDaniel Jurgens 	return mpi;
690732f69e4bSDaniel Jurgens }
690832f69e4bSDaniel Jurgens 
690916c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
691016c1975fSMark Bloch {
691132f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
6912b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
691332f69e4bSDaniel Jurgens 	int port_type_cap;
6914da796ccbSMark Bloch 	int num_ports;
691532f69e4bSDaniel Jurgens 
6916b5ca15adSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
6917b5ca15adSMark Bloch 
6918f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) &&
6919f6455de0SBodong Wang 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == MLX5_ESWITCH_OFFLOADS) {
69205fb58c9eSMark Bloch 		if (!mlx5_core_mp_enabled(mdev))
6921f0666f1fSBodong Wang 			mlx5_ib_register_vport_reps(mdev);
6922f0666f1fSBodong Wang 		return mdev;
6923f0666f1fSBodong Wang 	}
6924f0666f1fSBodong Wang 
692532f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
692632f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
692732f69e4bSDaniel Jurgens 
6928e3f1ed1fSLeon Romanovsky 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6929e3f1ed1fSLeon Romanovsky 		return mlx5_ib_add_slave_port(mdev);
693032f69e4bSDaniel Jurgens 
6931da796ccbSMark Bloch 	num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6932da796ccbSMark Bloch 			MLX5_CAP_GEN(mdev, num_vhca_ports));
6933459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
6934b5ca15adSMark Bloch 	if (!dev)
6935b5ca15adSMark Bloch 		return NULL;
6936da796ccbSMark Bloch 	dev->port = kcalloc(num_ports, sizeof(*dev->port),
6937da796ccbSMark Bloch 			     GFP_KERNEL);
6938da796ccbSMark Bloch 	if (!dev->port) {
6939a5c9c299SParav Pandit 		ib_dealloc_device(&dev->ib_dev);
6940da796ccbSMark Bloch 		return NULL;
6941da796ccbSMark Bloch 	}
6942b5ca15adSMark Bloch 
6943b5ca15adSMark Bloch 	dev->mdev = mdev;
6944da796ccbSMark Bloch 	dev->num_ports = num_ports;
6945b5ca15adSMark Bloch 
6946b5ca15adSMark Bloch 	return __mlx5_ib_add(dev, &pf_profile);
694716c1975fSMark Bloch }
694816c1975fSMark Bloch 
69499603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
6950e126ba97SEli Cohen {
695132f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
695232f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
69536aec21f6SHaggai Eran 
6954f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
6955f0666f1fSBodong Wang 		mlx5_ib_unregister_vport_reps(mdev);
6956f0666f1fSBodong Wang 		return;
6957f0666f1fSBodong Wang 	}
6958f0666f1fSBodong Wang 
695932f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
696032f69e4bSDaniel Jurgens 		mpi = context;
696132f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
696232f69e4bSDaniel Jurgens 		if (mpi->ibdev)
696332f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
696432f69e4bSDaniel Jurgens 		list_del(&mpi->list);
696532f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
69665d44adebSDanit Goldberg 		kfree(mpi);
696732f69e4bSDaniel Jurgens 		return;
696832f69e4bSDaniel Jurgens 	}
696932f69e4bSDaniel Jurgens 
697032f69e4bSDaniel Jurgens 	dev = context;
697116c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
6972e126ba97SEli Cohen }
6973e126ba97SEli Cohen 
69749603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
69759603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
69769603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
697764613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
6978e126ba97SEli Cohen };
6979e126ba97SEli Cohen 
6980c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
6981c44ef998SIlya Lesokhin {
6982c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
6983c44ef998SIlya Lesokhin 	return xlt_emergency_page;
6984c44ef998SIlya Lesokhin }
6985c44ef998SIlya Lesokhin 
6986c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
6987c44ef998SIlya Lesokhin {
6988c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
6989c44ef998SIlya Lesokhin }
6990c44ef998SIlya Lesokhin 
6991e126ba97SEli Cohen static int __init mlx5_ib_init(void)
6992e126ba97SEli Cohen {
69936aec21f6SHaggai Eran 	int err;
69946aec21f6SHaggai Eran 
6995c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
6996c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
6997d69a24e0SDaniel Jurgens 		return -ENOMEM;
6998d69a24e0SDaniel Jurgens 
6999c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
7000c44ef998SIlya Lesokhin 
7001c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
7002c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
7003c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
7004c44ef998SIlya Lesokhin 		return -ENOMEM;
7005c44ef998SIlya Lesokhin 	}
7006c44ef998SIlya Lesokhin 
700781713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
70089603b61dSJack Morgenstein 
70096aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
70106aec21f6SHaggai Eran 
70116aec21f6SHaggai Eran 	return err;
7012e126ba97SEli Cohen }
7013e126ba97SEli Cohen 
7014e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
7015e126ba97SEli Cohen {
70169603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
7017d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
7018c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
7019c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
7020e126ba97SEli Cohen }
7021e126ba97SEli Cohen 
7022e126ba97SEli Cohen module_init(mlx5_ib_init);
7023e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
7024