xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 333fbaa0255b8d471fc7ae767ef3a1766c732d6d)
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>
42e126ba97SEli Cohen #include <linux/sched.h>
436e84f315SIngo Molnar #include <linux/sched/mm.h>
440881e7bdSIngo Molnar #include <linux/sched/task.h>
457c2344c3SMaor Gottlieb #include <linux/delay.h>
46e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
473f89a643SAchiad Shochat #include <rdma/ib_addr.h>
482811ba51SAchiad Shochat #include <rdma/ib_cache.h>
49ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
501b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
5172c7fe90SPravin Shedge #include <linux/mlx5/fs.h>
52cecae747SMaor Gottlieb #include <linux/mlx5/eswitch.h>
537c2344c3SMaor Gottlieb #include <linux/list.h>
54e126ba97SEli Cohen #include <rdma/ib_smi.h>
55e126ba97SEli Cohen #include <rdma/ib_umem.h>
56038d2ef8SMaor Gottlieb #include <linux/in.h>
57038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
58e126ba97SEli Cohen #include "mlx5_ib.h"
59fc385b7aSMark Bloch #include "ib_rep.h"
60e1f24a79SParav Pandit #include "cmd.h"
61f3da6577SLeon Romanovsky #include "srq.h"
62*333fbaa0SLeon Romanovsky #include "qp.h"
633346c487SBoris Pismenny #include <linux/mlx5/fs_helpers.h>
64c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h>
658c84660bSMatan Barak #include <rdma/uverbs_std_types.h>
66c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h>
67c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h>
684061ff7aSErez Alfasi #include <rdma/ib_umem_odp.h>
698c84660bSMatan Barak 
708c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
718c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
72e126ba97SEli Cohen 
73e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
74b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0"
75e126ba97SEli Cohen 
76e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
77e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
78e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
79e126ba97SEli Cohen 
80e126ba97SEli Cohen static char mlx5_version[] =
81e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
82b359911dSTariq Toukan 	DRIVER_VERSION "\n";
83e126ba97SEli Cohen 
84d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
85d69a24e0SDaniel Jurgens 	struct work_struct	work;
86df097a27SSaeed Mahameed 	union {
87df097a27SSaeed Mahameed 		struct mlx5_ib_dev	      *dev;
88df097a27SSaeed Mahameed 		struct mlx5_ib_multiport_info *mpi;
89df097a27SSaeed Mahameed 	};
90df097a27SSaeed Mahameed 	bool			is_slave;
91134e9349SSaeed Mahameed 	unsigned int		event;
92df097a27SSaeed Mahameed 	void			*param;
93d69a24e0SDaniel Jurgens };
94d69a24e0SDaniel Jurgens 
95da7525d2SEran Ben Elisha enum {
96da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
97da7525d2SEran Ben Elisha };
981b5daf11SMajd Dibbiny 
99d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
10032f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
10132f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
10232f69e4bSDaniel Jurgens /*
10332f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
10432f69e4bSDaniel Jurgens  */
10532f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
10632f69e4bSDaniel Jurgens 
107c44ef998SIlya Lesokhin /* We can't use an array for xlt_emergency_page because dma_map_single
108c44ef998SIlya Lesokhin  * doesn't work on kernel modules memory
109c44ef998SIlya Lesokhin  */
110c44ef998SIlya Lesokhin static unsigned long xlt_emergency_page;
111c44ef998SIlya Lesokhin static struct mutex xlt_emergency_page_mutex;
112c44ef998SIlya Lesokhin 
11332f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
11432f69e4bSDaniel Jurgens {
11532f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
11632f69e4bSDaniel Jurgens 
11732f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
11832f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
11932f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
12032f69e4bSDaniel Jurgens 	return dev;
12132f69e4bSDaniel Jurgens }
12232f69e4bSDaniel Jurgens 
1231b5daf11SMajd Dibbiny static enum rdma_link_layer
124ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
1251b5daf11SMajd Dibbiny {
126ebd61f68SAchiad Shochat 	switch (port_type_cap) {
1271b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
1281b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
1291b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
1301b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
1311b5daf11SMajd Dibbiny 	default:
1321b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1331b5daf11SMajd Dibbiny 	}
1341b5daf11SMajd Dibbiny }
1351b5daf11SMajd Dibbiny 
136ebd61f68SAchiad Shochat static enum rdma_link_layer
137ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
138ebd61f68SAchiad Shochat {
139ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
140ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
141ebd61f68SAchiad Shochat 
142ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
143ebd61f68SAchiad Shochat }
144ebd61f68SAchiad Shochat 
145fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
146fd65f1b8SMoni Shoua 			  u8 port_num,
147fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
148fd65f1b8SMoni Shoua {
149fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
150fd65f1b8SMoni Shoua 	int ret;
151fd65f1b8SMoni Shoua 
152fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1533023a1e9SKamal Heib 	ret = ibdev->ops.query_port(ibdev, port_num, &attr);
154fd65f1b8SMoni Shoua 	if (!ret)
155fd65f1b8SMoni Shoua 		*state = attr.state;
156fd65f1b8SMoni Shoua 	return ret;
157fd65f1b8SMoni Shoua }
158fd65f1b8SMoni Shoua 
15935b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
16035b0aa67SMark Bloch 					   struct net_device *ndev,
16135b0aa67SMark Bloch 					   u8 *port_num)
16235b0aa67SMark Bloch {
16335b0aa67SMark Bloch 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
16435b0aa67SMark Bloch 	struct net_device *rep_ndev;
16535b0aa67SMark Bloch 	struct mlx5_ib_port *port;
16635b0aa67SMark Bloch 	int i;
16735b0aa67SMark Bloch 
16835b0aa67SMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
16935b0aa67SMark Bloch 		port  = &dev->port[i];
17035b0aa67SMark Bloch 		if (!port->rep)
17135b0aa67SMark Bloch 			continue;
17235b0aa67SMark Bloch 
17335b0aa67SMark Bloch 		read_lock(&port->roce.netdev_lock);
17435b0aa67SMark Bloch 		rep_ndev = mlx5_ib_get_rep_netdev(esw,
17535b0aa67SMark Bloch 						  port->rep->vport);
17635b0aa67SMark Bloch 		if (rep_ndev == ndev) {
17735b0aa67SMark Bloch 			read_unlock(&port->roce.netdev_lock);
17835b0aa67SMark Bloch 			*port_num = i + 1;
17935b0aa67SMark Bloch 			return &port->roce;
18035b0aa67SMark Bloch 		}
18135b0aa67SMark Bloch 		read_unlock(&port->roce.netdev_lock);
18235b0aa67SMark Bloch 	}
18335b0aa67SMark Bloch 
18435b0aa67SMark Bloch 	return NULL;
18535b0aa67SMark Bloch }
18635b0aa67SMark Bloch 
187fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
188fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
189fc24fc5eSAchiad Shochat {
1907fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
191fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1927fd8aefbSDaniel Jurgens 	u8 port_num = roce->native_port_num;
1937fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1947fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1957fd8aefbSDaniel Jurgens 
1967fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
19732f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
19832f69e4bSDaniel Jurgens 	if (!mdev)
19932f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
200fc24fc5eSAchiad Shochat 
2015ec8c83eSAviv Heller 	switch (event) {
2025ec8c83eSAviv Heller 	case NETDEV_REGISTER:
20335b0aa67SMark Bloch 		/* Should already be registered during the load */
20435b0aa67SMark Bloch 		if (ibdev->is_rep)
20535b0aa67SMark Bloch 			break;
2067fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
207dce45af5SLinus Torvalds 		if (ndev->dev.parent == mdev->device)
208842a9c83SOr Gerlitz 			roce->netdev = ndev;
2097fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
2105ec8c83eSAviv Heller 		break;
2115ec8c83eSAviv Heller 
212842a9c83SOr Gerlitz 	case NETDEV_UNREGISTER:
21335b0aa67SMark Bloch 		/* In case of reps, ib device goes away before the netdevs */
214842a9c83SOr Gerlitz 		write_lock(&roce->netdev_lock);
215842a9c83SOr Gerlitz 		if (roce->netdev == ndev)
216842a9c83SOr Gerlitz 			roce->netdev = NULL;
217842a9c83SOr Gerlitz 		write_unlock(&roce->netdev_lock);
218842a9c83SOr Gerlitz 		break;
219842a9c83SOr Gerlitz 
220fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
2215ec8c83eSAviv Heller 	case NETDEV_UP:
22288621dfeSAviv Heller 	case NETDEV_DOWN: {
2237fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
22488621dfeSAviv Heller 		struct net_device *upper = NULL;
22588621dfeSAviv Heller 
22688621dfeSAviv Heller 		if (lag_ndev) {
22788621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
22888621dfeSAviv Heller 			dev_put(lag_ndev);
22988621dfeSAviv Heller 		}
23088621dfeSAviv Heller 
23135b0aa67SMark Bloch 		if (ibdev->is_rep)
23235b0aa67SMark Bloch 			roce = mlx5_get_rep_roce(ibdev, ndev, &port_num);
23335b0aa67SMark Bloch 		if (!roce)
23435b0aa67SMark Bloch 			return NOTIFY_DONE;
2357fd8aefbSDaniel Jurgens 		if ((upper == ndev || (!upper && ndev == roce->netdev))
23688621dfeSAviv Heller 		    && ibdev->ib_active) {
237626bc02dSBart Van Assche 			struct ib_event ibev = { };
238fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2395ec8c83eSAviv Heller 
2407fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2417fd8aefbSDaniel Jurgens 					   &port_state))
2427fd8aefbSDaniel Jurgens 				goto done;
243fd65f1b8SMoni Shoua 
2447fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2457fd8aefbSDaniel Jurgens 				goto done;
246fd65f1b8SMoni Shoua 
2477fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2485ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
249fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
250fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
251fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
252fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
253fd65f1b8SMoni Shoua 			else
2547fd8aefbSDaniel Jurgens 				goto done;
255fd65f1b8SMoni Shoua 
2567fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2575ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2585ec8c83eSAviv Heller 		}
2595ec8c83eSAviv Heller 		break;
26088621dfeSAviv Heller 	}
2615ec8c83eSAviv Heller 
2625ec8c83eSAviv Heller 	default:
2635ec8c83eSAviv Heller 		break;
2645ec8c83eSAviv Heller 	}
2657fd8aefbSDaniel Jurgens done:
26632f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
267fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
268fc24fc5eSAchiad Shochat }
269fc24fc5eSAchiad Shochat 
270fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
271fc24fc5eSAchiad Shochat 					     u8 port_num)
272fc24fc5eSAchiad Shochat {
273fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
274fc24fc5eSAchiad Shochat 	struct net_device *ndev;
27532f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
276fc24fc5eSAchiad Shochat 
27732f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
27832f69e4bSDaniel Jurgens 	if (!mdev)
27932f69e4bSDaniel Jurgens 		return NULL;
28032f69e4bSDaniel Jurgens 
28132f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
28288621dfeSAviv Heller 	if (ndev)
28332f69e4bSDaniel Jurgens 		goto out;
28488621dfeSAviv Heller 
285fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
286fc24fc5eSAchiad Shochat 	 */
28795579e78SMark Bloch 	read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
28895579e78SMark Bloch 	ndev = ibdev->port[port_num - 1].roce.netdev;
289fc24fc5eSAchiad Shochat 	if (ndev)
290fc24fc5eSAchiad Shochat 		dev_hold(ndev);
29195579e78SMark Bloch 	read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
292fc24fc5eSAchiad Shochat 
29332f69e4bSDaniel Jurgens out:
29432f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
295fc24fc5eSAchiad Shochat 	return ndev;
296fc24fc5eSAchiad Shochat }
297fc24fc5eSAchiad Shochat 
29832f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
29932f69e4bSDaniel Jurgens 						   u8 ib_port_num,
30032f69e4bSDaniel Jurgens 						   u8 *native_port_num)
30132f69e4bSDaniel Jurgens {
30232f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
30332f69e4bSDaniel Jurgens 							  ib_port_num);
30432f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
30532f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
30632f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
30732f69e4bSDaniel Jurgens 
308210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
309210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
310210b1f78SMark Bloch 		if (native_port_num)
311210b1f78SMark Bloch 			*native_port_num = ib_port_num;
312210b1f78SMark Bloch 		return ibdev->mdev;
313210b1f78SMark Bloch 	}
314210b1f78SMark Bloch 
31532f69e4bSDaniel Jurgens 	if (native_port_num)
31632f69e4bSDaniel Jurgens 		*native_port_num = 1;
31732f69e4bSDaniel Jurgens 
31832f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
31932f69e4bSDaniel Jurgens 	if (!port)
32032f69e4bSDaniel Jurgens 		return NULL;
32132f69e4bSDaniel Jurgens 
32232f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
32332f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
32432f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
32532f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
32632f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
32732f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
32832f69e4bSDaniel Jurgens 		 */
32932f69e4bSDaniel Jurgens 		if (!mpi->is_master)
33032f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
33132f69e4bSDaniel Jurgens 	}
33232f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
33332f69e4bSDaniel Jurgens 
33432f69e4bSDaniel Jurgens 	return mdev;
33532f69e4bSDaniel Jurgens }
33632f69e4bSDaniel Jurgens 
33732f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
33832f69e4bSDaniel Jurgens {
33932f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
34032f69e4bSDaniel Jurgens 							  port_num);
34132f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
34232f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
34332f69e4bSDaniel Jurgens 
34432f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
34532f69e4bSDaniel Jurgens 		return;
34632f69e4bSDaniel Jurgens 
34732f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
34832f69e4bSDaniel Jurgens 
34932f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
35032f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
35132f69e4bSDaniel Jurgens 	if (mpi->is_master)
35232f69e4bSDaniel Jurgens 		goto out;
35332f69e4bSDaniel Jurgens 
35432f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
35532f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
35632f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
35732f69e4bSDaniel Jurgens out:
35832f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
35932f69e4bSDaniel Jurgens }
36032f69e4bSDaniel Jurgens 
36108e8676fSAya Levin static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, u8 *active_speed,
362f1b65df5SNoa Osherovich 					   u8 *active_width)
363f1b65df5SNoa Osherovich {
364f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
365f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
366f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
367f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
368f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
369f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
370f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
371f1b65df5SNoa Osherovich 		break;
372f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
373f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
374f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
375f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
376f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
377f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
378f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
379f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
380f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
381f1b65df5SNoa Osherovich 		break;
382f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
383f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
384f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
385f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
386f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
387f1b65df5SNoa Osherovich 		break;
388f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
389f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
390f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
391f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
392f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
393f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
394f1b65df5SNoa Osherovich 		break;
395f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
396f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
397f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
398f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
399f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
400f1b65df5SNoa Osherovich 		break;
401f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
402f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
403f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
404f1b65df5SNoa Osherovich 		break;
405f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
406f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
407f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
408f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
409f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
410f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
411f1b65df5SNoa Osherovich 		break;
412f1b65df5SNoa Osherovich 	default:
413f1b65df5SNoa Osherovich 		return -EINVAL;
414f1b65df5SNoa Osherovich 	}
415f1b65df5SNoa Osherovich 
416f1b65df5SNoa Osherovich 	return 0;
417f1b65df5SNoa Osherovich }
418f1b65df5SNoa Osherovich 
41908e8676fSAya Levin static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed,
42008e8676fSAya Levin 					u8 *active_width)
42108e8676fSAya Levin {
42208e8676fSAya Levin 	switch (eth_proto_oper) {
42308e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
42408e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
42508e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
42608e8676fSAya Levin 		*active_speed = IB_SPEED_SDR;
42708e8676fSAya Levin 		break;
42808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
42908e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
43008e8676fSAya Levin 		*active_speed = IB_SPEED_DDR;
43108e8676fSAya Levin 		break;
43208e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
43308e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
43408e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
43508e8676fSAya Levin 		break;
43608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
43708e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
43808e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
43908e8676fSAya Levin 		break;
44008e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
44108e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
44208e8676fSAya Levin 		*active_speed = IB_SPEED_EDR;
44308e8676fSAya Levin 		break;
44408e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
445cd272875SAya Levin 		*active_width = IB_WIDTH_2X;
446cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
447cd272875SAya Levin 		break;
44808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
44908e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
45008e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
45108e8676fSAya Levin 		break;
452cd272875SAya Levin 	case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
453cd272875SAya Levin 		*active_width = IB_WIDTH_4X;
454cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
455cd272875SAya Levin 		break;
45608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
45708e8676fSAya Levin 		*active_width = IB_WIDTH_2X;
45808e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
45908e8676fSAya Levin 		break;
46008e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
46108e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
46208e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
46308e8676fSAya Levin 		break;
46408e8676fSAya Levin 	default:
46508e8676fSAya Levin 		return -EINVAL;
46608e8676fSAya Levin 	}
46708e8676fSAya Levin 
46808e8676fSAya Levin 	return 0;
46908e8676fSAya Levin }
47008e8676fSAya Levin 
47108e8676fSAya Levin static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
47208e8676fSAya Levin 				    u8 *active_width, bool ext)
47308e8676fSAya Levin {
47408e8676fSAya Levin 	return ext ?
47508e8676fSAya Levin 		translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
47608e8676fSAya Levin 					     active_width) :
47708e8676fSAya Levin 		translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
47808e8676fSAya Levin 						active_width);
47908e8676fSAya Levin }
48008e8676fSAya Levin 
481095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
4823f89a643SAchiad Shochat 				struct ib_port_attr *props)
4833f89a643SAchiad Shochat {
4843f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
485bc4e12ffSAya Levin 	u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
486da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
48788621dfeSAviv Heller 	struct net_device *ndev, *upper;
4883f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
489b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
490c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
491f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
492b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
49308e8676fSAya Levin 	bool ext;
494095b0927SIlan Tayari 	int err;
4953f89a643SAchiad Shochat 
496b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
497b3cbd6f0SDaniel Jurgens 	if (!mdev) {
498b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
499b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
500b3cbd6f0SDaniel Jurgens 		 */
501b3cbd6f0SDaniel Jurgens 		put_mdev = false;
502b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
503b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
504b3cbd6f0SDaniel Jurgens 		port_num = 1;
505b3cbd6f0SDaniel Jurgens 	}
506b3cbd6f0SDaniel Jurgens 
507f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
508f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
50926628e2dSMark Bloch 	 * Use native port in case of reps
51050f22fd8SNoa Osherovich 	 */
51126628e2dSMark Bloch 	if (dev->is_rep)
51226628e2dSMark Bloch 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
51326628e2dSMark Bloch 					   1);
51426628e2dSMark Bloch 	else
515bc4e12ffSAya Levin 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
516b3cbd6f0SDaniel Jurgens 					   mdev_port_num);
517095b0927SIlan Tayari 	if (err)
518b3cbd6f0SDaniel Jurgens 		goto out;
51908e8676fSAya Levin 	ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet);
52008e8676fSAya Levin 	eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
521f1b65df5SNoa Osherovich 
5227672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
5237672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
5247672ed33SHonggang Li 
525f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
52608e8676fSAya Levin 				 &props->active_width, ext);
5273f89a643SAchiad Shochat 
5283f89a643SAchiad Shochat 	props->port_cap_flags |= IB_PORT_CM_SUP;
5292f944c0fSJason Gunthorpe 	props->ip_gids = true;
5303f89a643SAchiad Shochat 
5313f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
5323f89a643SAchiad Shochat 						roce_address_table_size);
5333f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
5343f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
5353f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
5363f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
53772a7720fSKamal Heib 	props->phys_state       = IB_PORT_PHYS_STATE_DISABLED;
5383f89a643SAchiad Shochat 
539b3cbd6f0SDaniel Jurgens 	mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
540c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
5413f89a643SAchiad Shochat 
542b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
543b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
544b3cbd6f0SDaniel Jurgens 		goto out;
545b3cbd6f0SDaniel Jurgens 
5463f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
5473f89a643SAchiad Shochat 	if (!ndev)
548b3cbd6f0SDaniel Jurgens 		goto out;
5493f89a643SAchiad Shochat 
5507c34ec19SAviv Heller 	if (dev->lag_active) {
55188621dfeSAviv Heller 		rcu_read_lock();
55288621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
55388621dfeSAviv Heller 		if (upper) {
55488621dfeSAviv Heller 			dev_put(ndev);
55588621dfeSAviv Heller 			ndev = upper;
55688621dfeSAviv Heller 			dev_hold(ndev);
55788621dfeSAviv Heller 		}
55888621dfeSAviv Heller 		rcu_read_unlock();
55988621dfeSAviv Heller 	}
56088621dfeSAviv Heller 
5613f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
5623f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
56372a7720fSKamal Heib 		props->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
5643f89a643SAchiad Shochat 	}
5653f89a643SAchiad Shochat 
5663f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
5673f89a643SAchiad Shochat 
5683f89a643SAchiad Shochat 	dev_put(ndev);
5693f89a643SAchiad Shochat 
5703f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
571b3cbd6f0SDaniel Jurgens out:
572b3cbd6f0SDaniel Jurgens 	if (put_mdev)
573b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
574b3cbd6f0SDaniel Jurgens 	return err;
5753f89a643SAchiad Shochat }
5763f89a643SAchiad Shochat 
577095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
578095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
579095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
5803cca2606SAchiad Shochat {
581095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
582a70c0739SParav Pandit 	u16 vlan_id = 0xffff;
583095b0927SIlan Tayari 	u8 roce_version = 0;
584095b0927SIlan Tayari 	u8 roce_l3_type = 0;
585095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
586a70c0739SParav Pandit 	int ret;
5873cca2606SAchiad Shochat 
588095b0927SIlan Tayari 	if (gid) {
589095b0927SIlan Tayari 		gid_type = attr->gid_type;
590a70c0739SParav Pandit 		ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
591a70c0739SParav Pandit 		if (ret)
592a70c0739SParav Pandit 			return ret;
5933cca2606SAchiad Shochat 	}
5943cca2606SAchiad Shochat 
595095b0927SIlan Tayari 	switch (gid_type) {
5963cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
597095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
5983cca2606SAchiad Shochat 		break;
5993cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
600095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
601095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
602095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
603095b0927SIlan Tayari 		else
604095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
6053cca2606SAchiad Shochat 		break;
6063cca2606SAchiad Shochat 
6073cca2606SAchiad Shochat 	default:
608095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
6093cca2606SAchiad Shochat 	}
6103cca2606SAchiad Shochat 
611095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
612cf34e1feSParav Pandit 				      roce_l3_type, gid->raw, mac,
613a70c0739SParav Pandit 				      vlan_id < VLAN_CFI_MASK, vlan_id,
614cf34e1feSParav Pandit 				      port_num);
6153cca2606SAchiad Shochat }
6163cca2606SAchiad Shochat 
617f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
6183cca2606SAchiad Shochat 			   __always_unused void **context)
6193cca2606SAchiad Shochat {
620414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
621f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
6223cca2606SAchiad Shochat }
6233cca2606SAchiad Shochat 
624414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
625414448d2SParav Pandit 			   __always_unused void **context)
6263cca2606SAchiad Shochat {
627414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
628414448d2SParav Pandit 			     attr->index, NULL, NULL);
6293cca2606SAchiad Shochat }
6303cca2606SAchiad Shochat 
63147ec3866SParav Pandit __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
63247ec3866SParav Pandit 			       const struct ib_gid_attr *attr)
6332811ba51SAchiad Shochat {
63447ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
6352811ba51SAchiad Shochat 		return 0;
6362811ba51SAchiad Shochat 
6372811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
6382811ba51SAchiad Shochat }
6392811ba51SAchiad Shochat 
6401b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
6411b5daf11SMajd Dibbiny {
6427fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
643d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
6447fae6655SNoa Osherovich 	return 0;
6451b5daf11SMajd Dibbiny }
6461b5daf11SMajd Dibbiny 
6471b5daf11SMajd Dibbiny enum {
6481b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
6491b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
6501b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
6511b5daf11SMajd Dibbiny };
6521b5daf11SMajd Dibbiny 
6531b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
6541b5daf11SMajd Dibbiny {
6551b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
6561b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
6571b5daf11SMajd Dibbiny 
658ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
6591b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
6601b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
6611b5daf11SMajd Dibbiny 
6621b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
6631b5daf11SMajd Dibbiny }
6641b5daf11SMajd Dibbiny 
665da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
666776a3906SMoni Shoua 			    u8 atomic_size_qp,
667da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
668da7525d2SEran Ben Elisha {
669da7525d2SEran Ben Elisha 	u8 tmp;
670da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
671da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
672bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
673da7525d2SEran Ben Elisha 
674da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
675da7525d2SEran Ben Elisha 	 * of host endianness respond
676da7525d2SEran Ben Elisha 	 */
677da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
678da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
679da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
680da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
681da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
682da7525d2SEran Ben Elisha 	} else {
683da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
684da7525d2SEran Ben Elisha 	}
685da7525d2SEran Ben Elisha }
686da7525d2SEran Ben Elisha 
687776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
688776a3906SMoni Shoua 			       struct ib_device_attr *props)
689776a3906SMoni Shoua {
690776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
691776a3906SMoni Shoua 
692776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
693776a3906SMoni Shoua }
694776a3906SMoni Shoua 
6951b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
6961b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
6971b5daf11SMajd Dibbiny {
6981b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6991b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7001b5daf11SMajd Dibbiny 	u64 tmp;
7011b5daf11SMajd Dibbiny 	int err;
7021b5daf11SMajd Dibbiny 
7031b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7041b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7051b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
7061b5daf11SMajd Dibbiny 							    sys_image_guid);
7071b5daf11SMajd Dibbiny 
7081b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7091b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
7103f89a643SAchiad Shochat 		break;
7113f89a643SAchiad Shochat 
7123f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7133f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
7143f89a643SAchiad Shochat 		break;
7151b5daf11SMajd Dibbiny 
7161b5daf11SMajd Dibbiny 	default:
7171b5daf11SMajd Dibbiny 		return -EINVAL;
7181b5daf11SMajd Dibbiny 	}
7193f89a643SAchiad Shochat 
7203f89a643SAchiad Shochat 	if (!err)
7213f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
7223f89a643SAchiad Shochat 
7233f89a643SAchiad Shochat 	return err;
7243f89a643SAchiad Shochat 
7251b5daf11SMajd Dibbiny }
7261b5daf11SMajd Dibbiny 
7271b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
7281b5daf11SMajd Dibbiny 				u16 *max_pkeys)
7291b5daf11SMajd Dibbiny {
7301b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7311b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7321b5daf11SMajd Dibbiny 
7331b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7341b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7351b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
7361b5daf11SMajd Dibbiny 
7371b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7381b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7391b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
7401b5daf11SMajd Dibbiny 						pkey_table_size));
7411b5daf11SMajd Dibbiny 		return 0;
7421b5daf11SMajd Dibbiny 
7431b5daf11SMajd Dibbiny 	default:
7441b5daf11SMajd Dibbiny 		return -EINVAL;
7451b5daf11SMajd Dibbiny 	}
7461b5daf11SMajd Dibbiny }
7471b5daf11SMajd Dibbiny 
7481b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
7491b5daf11SMajd Dibbiny 				u32 *vendor_id)
7501b5daf11SMajd Dibbiny {
7511b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7521b5daf11SMajd Dibbiny 
7531b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7541b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7551b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
7561b5daf11SMajd Dibbiny 
7571b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7581b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7591b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
7601b5daf11SMajd Dibbiny 
7611b5daf11SMajd Dibbiny 	default:
7621b5daf11SMajd Dibbiny 		return -EINVAL;
7631b5daf11SMajd Dibbiny 	}
7641b5daf11SMajd Dibbiny }
7651b5daf11SMajd Dibbiny 
7661b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
7671b5daf11SMajd Dibbiny 				__be64 *node_guid)
7681b5daf11SMajd Dibbiny {
7691b5daf11SMajd Dibbiny 	u64 tmp;
7701b5daf11SMajd Dibbiny 	int err;
7711b5daf11SMajd Dibbiny 
7721b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7731b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7741b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7751b5daf11SMajd Dibbiny 
7761b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7771b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7783f89a643SAchiad Shochat 		break;
7793f89a643SAchiad Shochat 
7803f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7813f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7823f89a643SAchiad Shochat 		break;
7831b5daf11SMajd Dibbiny 
7841b5daf11SMajd Dibbiny 	default:
7851b5daf11SMajd Dibbiny 		return -EINVAL;
7861b5daf11SMajd Dibbiny 	}
7873f89a643SAchiad Shochat 
7883f89a643SAchiad Shochat 	if (!err)
7893f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7903f89a643SAchiad Shochat 
7913f89a643SAchiad Shochat 	return err;
7921b5daf11SMajd Dibbiny }
7931b5daf11SMajd Dibbiny 
7941b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
795bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
7961b5daf11SMajd Dibbiny };
7971b5daf11SMajd Dibbiny 
7981b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
7991b5daf11SMajd Dibbiny {
8001b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
8011b5daf11SMajd Dibbiny 
8021b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
8031b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
8041b5daf11SMajd Dibbiny 
8051b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
8061b5daf11SMajd Dibbiny 
8071b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
8081b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
8091b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
8101b5daf11SMajd Dibbiny }
8111b5daf11SMajd Dibbiny 
812e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
8132528e33eSMatan Barak 				struct ib_device_attr *props,
8142528e33eSMatan Barak 				struct ib_udata *uhw)
815e126ba97SEli Cohen {
81648357091SLeon Romanovsky 	size_t uhw_outlen = (uhw) ? uhw->outlen : 0;
817e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
818938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
819e126ba97SEli Cohen 	int err = -ENOMEM;
820288c01b7SEli Cohen 	int max_sq_desc;
821e126ba97SEli Cohen 	int max_rq_sg;
822e126ba97SEli Cohen 	int max_sq_sg;
823e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
82485c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
825402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
826402ca536SBodong Wang 	size_t resp_len;
827402ca536SBodong Wang 	u64 max_tso;
828e126ba97SEli Cohen 
829402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
83048357091SLeon Romanovsky 	if (uhw_outlen && uhw_outlen < resp_len)
831402ca536SBodong Wang 		return -EINVAL;
8326f26b2acSErez Alfasi 
833402ca536SBodong Wang 	resp.response_length = resp_len;
834402ca536SBodong Wang 
83548357091SLeon Romanovsky 	if (uhw && uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
8362528e33eSMatan Barak 		return -EINVAL;
8372528e33eSMatan Barak 
838e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
8391b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
8401b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
8411b5daf11SMajd Dibbiny 	if (err)
8421b5daf11SMajd Dibbiny 		return err;
8431b5daf11SMajd Dibbiny 
8441b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
8451b5daf11SMajd Dibbiny 	if (err)
8461b5daf11SMajd Dibbiny 		return err;
8471b5daf11SMajd Dibbiny 
8481b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
8491b5daf11SMajd Dibbiny 	if (err)
8501b5daf11SMajd Dibbiny 		return err;
851e126ba97SEli Cohen 
8529603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
8539603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
8549603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
855e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
856e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
857e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
8581a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
859938fe83cSSaeed Mahameed 
860938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
861e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
862938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
863e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
864938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
865e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
866938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
867e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
868d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
869d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
870d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
871d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
872b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
873b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
874d2370e0aSMatan Barak 	}
875e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
876938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
877c0a6cbb9SIsrael Rukshin 		props->device_cap_flags |= IB_DEVICE_INTEGRITY_HANDOVER;
8782dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8792dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8802dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8812dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8822dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8832dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8842dea9094SSagi Grimberg 	}
885938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
886f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
887e126ba97SEli Cohen 
88885c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
889e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
890e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
89188115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
892e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
893e8161334SNoa Osherovich 		}
894e8161334SNoa Osherovich 
895e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
896e8161334SNoa Osherovich 			props->raw_packet_caps |=
897e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
89888115fe7SBodong Wang 
899a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) {
900402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
901402ca536SBodong Wang 			if (max_tso) {
902402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
903402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
904402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
905402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
906402ca536SBodong Wang 			}
907402ca536SBodong Wang 		}
90831f69a82SYishai Hadas 
909a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) {
91031f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
91131f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
91231f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
91331f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
91431f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
91531f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
91631f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
91731f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
91831f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
91931f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
9204e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
9214e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
9222d93fc85SMatan Barak 			if (mlx5_accel_ipsec_device_caps(dev->mdev) &
9232d93fc85SMatan Barak 			    MLX5_ACCEL_IPSEC_CAP_DEVICE)
9242d93fc85SMatan Barak 				resp.rss_caps.rx_hash_fields_mask |=
9252d93fc85SMatan Barak 					MLX5_RX_HASH_IPSEC_SPI;
92631f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
92731f69a82SYishai Hadas 		}
92831f69a82SYishai Hadas 	} else {
929a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen)
93031f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
931a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen)
93231f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
933402ca536SBodong Wang 	}
934402ca536SBodong Wang 
935f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
936f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
937f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
938f0313965SErez Shitrit 	}
939f0313965SErez Shitrit 
94003404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
94185c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
94285c7c014SDaniel Jurgens 	    raw_support)
94303404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
94403404e8aSMaor Gottlieb 
9451d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
9461d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
9471d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
9481d54f890SYishai Hadas 
949cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
95085c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
95185c7c014SDaniel Jurgens 	    raw_support) {
952e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
953cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
954e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
955e8161334SNoa Osherovich 	}
956cff5a0f3SMajd Dibbiny 
95724da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
95824da0016SAriel Levkovich 		props->max_dm_size =
95924da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
96024da0016SAriel Levkovich 	}
96124da0016SAriel Levkovich 
962da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
963da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
964da6d6ba3SMaor Gottlieb 
965b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
966b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
967b1383aa6SNoa Osherovich 
9681b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
9691b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
970e126ba97SEli Cohen 
971e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
972e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
973938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
974938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
975938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
976938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
977288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
978288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
979288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
980e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
98133023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
98233023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
983986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
984938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9859f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
986938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
987938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
988938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
989938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
990938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
991938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
992938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
993e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
994e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
995911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
996911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
99762e3c379SMax Gurtovoy 	props->max_pi_fast_reg_page_list_len =
99862e3c379SMax Gurtovoy 		props->max_fast_reg_page_list_len / 2;
99936609056SYamin Friedman 	props->max_sgl_rd =
100036609056SYamin Friedman 		MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance);
1001776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
100281bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
1003938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1004938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
1005e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1006e126ba97SEli Cohen 					   props->max_mcast_grp;
1007e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
100886695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
10097c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
10107c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
1011e126ba97SEli Cohen 
1012e502b8b0SLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
101300815752SMoni Shoua 		if (dev->odp_caps.general_caps & IB_ODP_SUPPORT)
10148cdd312cSHaggai Eran 			props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
10158cdd312cSHaggai Eran 		props->odp_caps = dev->odp_caps;
1016a73a8955SMoni Shoua 		if (!uhw) {
1017a73a8955SMoni Shoua 			/* ODP for kernel QPs is not implemented for receive
1018a73a8955SMoni Shoua 			 * WQEs and SRQ WQEs
1019a73a8955SMoni Shoua 			 */
1020a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.rc_odp_caps &=
1021a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1022a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1023a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.uc_odp_caps &=
1024a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1025a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1026a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.ud_odp_caps &=
1027a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1028a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1029a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.xrc_odp_caps &=
1030a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1031a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1032a73a8955SMoni Shoua 		}
1033e502b8b0SLeon Romanovsky 	}
10348cdd312cSHaggai Eran 
1035051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
1036051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1037051f2630SLeon Romanovsky 
1038e53a9d26SParav Pandit 	if (mlx5_core_is_vf(mdev))
1039eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1040eff901d3SEli Cohen 
104131f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
104285c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
104331f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
104431f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
104531f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
104631f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
104731f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
104831f69a82SYishai Hadas 		props->max_wq_type_rq =
104931f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
105031f69a82SYishai Hadas 	}
105131f69a82SYishai Hadas 
1052eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
105378b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
1054eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
105578b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
1056eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
105778b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1058eb761894SArtemy Kovalyov 	}
1059eb761894SArtemy Kovalyov 
106089705e92SDanit Goldberg 	if (MLX5_CAP_GEN(mdev, tag_matching) &&
106189705e92SDanit Goldberg 	    MLX5_CAP_GEN(mdev, rndv_offload_rc)) {
106289705e92SDanit Goldberg 		props->tm_caps.flags = IB_TM_CAP_RNDV_RC;
106389705e92SDanit Goldberg 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
106489705e92SDanit Goldberg 	}
106589705e92SDanit Goldberg 
106687ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
106787ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
106887ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
106987ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
107087ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
107187ab3f52SYonatan Cohen 	}
107287ab3f52SYonatan Cohen 
1073a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), cqe_comp_caps) <= uhw_outlen) {
1074572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
1075572f46bfSYonatan Cohen 
1076572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
10777e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
1078572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
1079572f46bfSYonatan Cohen 					     cqe_compression_max_num);
1080572f46bfSYonatan Cohen 
10817e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
10827e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
10837e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
10846f1006a4SYonatan Cohen 
10856f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
10866f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
10876f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1088572f46bfSYonatan Cohen 		}
10897e43a2a5SBodong Wang 	}
10907e43a2a5SBodong Wang 
1091a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), packet_pacing_caps) <= uhw_outlen &&
109285c7c014SDaniel Jurgens 	    raw_support) {
1093d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1094d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
1095d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
1096d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1097d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
1098d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1099d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
1100d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
110161147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
110261147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
110361147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
110461147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
1105d949167dSBodong Wang 		}
1106d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1107d949167dSBodong Wang 	}
1108d949167dSBodong Wang 
1109a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes) <=
1110a762d460SLeon Romanovsky 	    uhw_outlen) {
1111795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
11129f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1113795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1114050da902SBodong Wang 
1115050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1116050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1117050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1118050da902SBodong Wang 
11199f885201SLeon Romanovsky 		resp.response_length +=
11209f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
11219f885201SLeon Romanovsky 	}
11229f885201SLeon Romanovsky 
1123a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), flags) <= uhw_outlen) {
1124de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
11257a0c8f42SGuy Levi 
1126de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1127de57f2adSGuy Levi 			resp.flags |=
1128de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
11297a0c8f42SGuy Levi 
11307a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
11317a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
11327e11b911SDanit Goldberg 		if (MLX5_CAP_GEN(mdev, qp_packet_based))
11337e11b911SDanit Goldberg 			resp.flags |=
11347e11b911SDanit Goldberg 				MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
11357249c8eaSGuy Levi 
11367249c8eaSGuy Levi 		resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1137de57f2adSGuy Levi 	}
11389f885201SLeon Romanovsky 
1139a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), sw_parsing_caps) <= uhw_outlen) {
114096dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
114196dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
114296dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
114396dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
114496dc3fc5SNoa Osherovich 
114596dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
114696dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
114796dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
114896dc3fc5SNoa Osherovich 
114996dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
115096dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
115196dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
115296dc3fc5SNoa Osherovich 
115396dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
115496dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
115596dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
115696dc3fc5SNoa Osherovich 		}
115796dc3fc5SNoa Osherovich 	}
115896dc3fc5SNoa Osherovich 
1159a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), striding_rq_caps) <= uhw_outlen &&
116085c7c014SDaniel Jurgens 	    raw_support) {
1161b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1162b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1163b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1164b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1165b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1166b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1167c16339b6SMark Zhang 			if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range))
1168c16339b6SMark Zhang 				resp.striding_rq_caps
1169c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1170c16339b6SMark Zhang 					MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1171c16339b6SMark Zhang 			else
1172c16339b6SMark Zhang 				resp.striding_rq_caps
1173c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1174b4f34597SNoa Osherovich 					MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1175b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1176b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1177b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1178b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1179b4f34597SNoa Osherovich 		}
1180b4f34597SNoa Osherovich 	}
1181b4f34597SNoa Osherovich 
1182a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), tunnel_offloads_caps) <= uhw_outlen) {
1183f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1184f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1185f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1186f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1187f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1188f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1189f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1190f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1191f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1192f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
119341e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre))
1194e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1195e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
119641e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_udp))
1197e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1198e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1199f95ef6cbSMaor Gottlieb 	}
1200f95ef6cbSMaor Gottlieb 
120148357091SLeon Romanovsky 	if (uhw_outlen) {
1202402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1203402ca536SBodong Wang 
1204402ca536SBodong Wang 		if (err)
1205402ca536SBodong Wang 			return err;
1206402ca536SBodong Wang 	}
1207402ca536SBodong Wang 
12081b5daf11SMajd Dibbiny 	return 0;
12091b5daf11SMajd Dibbiny }
1210e126ba97SEli Cohen 
12111b5daf11SMajd Dibbiny enum mlx5_ib_width {
12121b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
12131b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
12141b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
12151b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
12161b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
12171b5daf11SMajd Dibbiny };
12181b5daf11SMajd Dibbiny 
1219db7a691aSMichael Guralnik static void translate_active_width(struct ib_device *ibdev, u8 active_width,
12201b5daf11SMajd Dibbiny 				  u8 *ib_width)
12211b5daf11SMajd Dibbiny {
12221b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12231b5daf11SMajd Dibbiny 
1224db7a691aSMichael Guralnik 	if (active_width & MLX5_IB_WIDTH_1X)
12251b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1226d764970bSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_2X)
1227d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1228db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_4X)
12291b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1230db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_8X)
12311b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1232db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_12X)
12331b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1234db7a691aSMichael Guralnik 	else {
1235db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
12361b5daf11SMajd Dibbiny 			    (int)active_width);
1237db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
12381b5daf11SMajd Dibbiny 	}
12391b5daf11SMajd Dibbiny 
1240db7a691aSMichael Guralnik 	return;
12411b5daf11SMajd Dibbiny }
12421b5daf11SMajd Dibbiny 
12431b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
12441b5daf11SMajd Dibbiny {
12451b5daf11SMajd Dibbiny 	switch (mtu) {
12461b5daf11SMajd Dibbiny 	case 256: return 1;
12471b5daf11SMajd Dibbiny 	case 512: return 2;
12481b5daf11SMajd Dibbiny 	case 1024: return 3;
12491b5daf11SMajd Dibbiny 	case 2048: return 4;
12501b5daf11SMajd Dibbiny 	case 4096: return 5;
12511b5daf11SMajd Dibbiny 	default:
12521b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
12531b5daf11SMajd Dibbiny 		return -1;
12541b5daf11SMajd Dibbiny 	}
12551b5daf11SMajd Dibbiny }
12561b5daf11SMajd Dibbiny 
12571b5daf11SMajd Dibbiny enum ib_max_vl_num {
12581b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
12591b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
12601b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
12611b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
12621b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
12631b5daf11SMajd Dibbiny };
12641b5daf11SMajd Dibbiny 
12651b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
12661b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
12671b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
12681b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
12691b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
12701b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
12711b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
12721b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
12731b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
12741b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
12751b5daf11SMajd Dibbiny };
12761b5daf11SMajd Dibbiny 
12771b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
12781b5daf11SMajd Dibbiny 				u8 *max_vl_num)
12791b5daf11SMajd Dibbiny {
12801b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
12811b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
12821b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
12831b5daf11SMajd Dibbiny 		break;
12841b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
12851b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
12861b5daf11SMajd Dibbiny 		break;
12871b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
12881b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
12891b5daf11SMajd Dibbiny 		break;
12901b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
12911b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
12921b5daf11SMajd Dibbiny 		break;
12931b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
12941b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
12951b5daf11SMajd Dibbiny 		break;
12961b5daf11SMajd Dibbiny 
12971b5daf11SMajd Dibbiny 	default:
12981b5daf11SMajd Dibbiny 		return -EINVAL;
12991b5daf11SMajd Dibbiny 	}
13001b5daf11SMajd Dibbiny 
13011b5daf11SMajd Dibbiny 	return 0;
13021b5daf11SMajd Dibbiny }
13031b5daf11SMajd Dibbiny 
13041b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
13051b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
13061b5daf11SMajd Dibbiny {
13071b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
13081b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
13091b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1310046339eaSSaeed Mahameed 	u16 max_mtu;
1311046339eaSSaeed Mahameed 	u16 oper_mtu;
13121b5daf11SMajd Dibbiny 	int err;
13131b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
13141b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
13151b5daf11SMajd Dibbiny 
13161b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
13171b5daf11SMajd Dibbiny 	if (!rep) {
13181b5daf11SMajd Dibbiny 		err = -ENOMEM;
13191b5daf11SMajd Dibbiny 		goto out;
13201b5daf11SMajd Dibbiny 	}
13211b5daf11SMajd Dibbiny 
1322c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
13231b5daf11SMajd Dibbiny 
13241b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
13251b5daf11SMajd Dibbiny 	if (err)
13261b5daf11SMajd Dibbiny 		goto out;
13271b5daf11SMajd Dibbiny 
13281b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
13291b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
13301b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
13311b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
13321b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
13331b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
13341b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
13351b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
13361b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
13371b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
13381b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
13391b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
13401b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
13411b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
13421b5daf11SMajd Dibbiny 
13434106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
13444106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
13454106a758SMichael Guralnik 
13461b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
13471b5daf11SMajd Dibbiny 	if (err)
13481b5daf11SMajd Dibbiny 		goto out;
13491b5daf11SMajd Dibbiny 
1350db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1351db7a691aSMichael Guralnik 
1352d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
13531b5daf11SMajd Dibbiny 	if (err)
13541b5daf11SMajd Dibbiny 		goto out;
13551b5daf11SMajd Dibbiny 
1356facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
13571b5daf11SMajd Dibbiny 
13581b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
13591b5daf11SMajd Dibbiny 
1360facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
13611b5daf11SMajd Dibbiny 
13621b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
13631b5daf11SMajd Dibbiny 
13641b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
13651b5daf11SMajd Dibbiny 	if (err)
13661b5daf11SMajd Dibbiny 		goto out;
13671b5daf11SMajd Dibbiny 
13681b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
13691b5daf11SMajd Dibbiny 				   &props->max_vl_num);
13701b5daf11SMajd Dibbiny out:
13711b5daf11SMajd Dibbiny 	kfree(rep);
1372e126ba97SEli Cohen 	return err;
1373e126ba97SEli Cohen }
1374e126ba97SEli Cohen 
1375e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1376e126ba97SEli Cohen 		       struct ib_port_attr *props)
1377e126ba97SEli Cohen {
1378095b0927SIlan Tayari 	unsigned int count;
1379095b0927SIlan Tayari 	int ret;
1380095b0927SIlan Tayari 
13811b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13821b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1383095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1384095b0927SIlan Tayari 		break;
1385e126ba97SEli Cohen 
13861b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1387095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1388095b0927SIlan Tayari 		break;
13891b5daf11SMajd Dibbiny 
13903f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1391095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1392095b0927SIlan Tayari 		break;
13933f89a643SAchiad Shochat 
13941b5daf11SMajd Dibbiny 	default:
1395095b0927SIlan Tayari 		ret = -EINVAL;
1396e126ba97SEli Cohen 	}
1397095b0927SIlan Tayari 
1398095b0927SIlan Tayari 	if (!ret && props) {
1399b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1400b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1401b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1402b3cbd6f0SDaniel Jurgens 
1403b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1404b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1405b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1406b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1407b3cbd6f0SDaniel Jurgens 			 */
1408b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1409b3cbd6f0SDaniel Jurgens 			port = 1;
1410b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1411b3cbd6f0SDaniel Jurgens 		}
1412b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1413b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1414b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1415095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1416095b0927SIlan Tayari 	}
1417095b0927SIlan Tayari 	return ret;
1418e126ba97SEli Cohen }
1419e126ba97SEli Cohen 
14208e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
14218e6efa3aSMark Bloch 				  struct ib_port_attr *props)
14228e6efa3aSMark Bloch {
14238e6efa3aSMark Bloch 	int ret;
14248e6efa3aSMark Bloch 
142526628e2dSMark Bloch 	/* Only link layer == ethernet is valid for representors
142626628e2dSMark Bloch 	 * and we always use port 1
142726628e2dSMark Bloch 	 */
14288e6efa3aSMark Bloch 	ret = mlx5_query_port_roce(ibdev, port, props);
14298e6efa3aSMark Bloch 	if (ret || !props)
14308e6efa3aSMark Bloch 		return ret;
14318e6efa3aSMark Bloch 
14328e6efa3aSMark Bloch 	/* We don't support GIDS */
14338e6efa3aSMark Bloch 	props->gid_tbl_len = 0;
14348e6efa3aSMark Bloch 
14358e6efa3aSMark Bloch 	return ret;
14368e6efa3aSMark Bloch }
14378e6efa3aSMark Bloch 
1438e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1439e126ba97SEli Cohen 			     union ib_gid *gid)
1440e126ba97SEli Cohen {
14411b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
14421b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1443e126ba97SEli Cohen 
14441b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14451b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14461b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1447e126ba97SEli Cohen 
14481b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14491b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1450e126ba97SEli Cohen 
14511b5daf11SMajd Dibbiny 	default:
14521b5daf11SMajd Dibbiny 		return -EINVAL;
14531b5daf11SMajd Dibbiny 	}
1454e126ba97SEli Cohen 
1455e126ba97SEli Cohen }
1456e126ba97SEli Cohen 
1457b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1458b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1459b3cbd6f0SDaniel Jurgens {
1460b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1461b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1462b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1463b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1464b3cbd6f0SDaniel Jurgens 	int err;
1465b3cbd6f0SDaniel Jurgens 
1466b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1467b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1468b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1469b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1470b3cbd6f0SDaniel Jurgens 		 */
1471b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1472b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1473b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1474b3cbd6f0SDaniel Jurgens 	}
1475b3cbd6f0SDaniel Jurgens 
1476b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1477b3cbd6f0SDaniel Jurgens 					index, pkey);
1478b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1479b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1480b3cbd6f0SDaniel Jurgens 
1481b3cbd6f0SDaniel Jurgens 	return err;
1482b3cbd6f0SDaniel Jurgens }
1483b3cbd6f0SDaniel Jurgens 
1484e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1485e126ba97SEli Cohen 			      u16 *pkey)
1486e126ba97SEli Cohen {
14871b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14881b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14891b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1490e126ba97SEli Cohen 
14911b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14921b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1493b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
14941b5daf11SMajd Dibbiny 	default:
14951b5daf11SMajd Dibbiny 		return -EINVAL;
1496e126ba97SEli Cohen 	}
14971b5daf11SMajd Dibbiny }
1498e126ba97SEli Cohen 
1499e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1500e126ba97SEli Cohen 				 struct ib_device_modify *props)
1501e126ba97SEli Cohen {
1502e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1503e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1504e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1505e126ba97SEli Cohen 	int err;
1506e126ba97SEli Cohen 
1507e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1508e126ba97SEli Cohen 		return -EOPNOTSUPP;
1509e126ba97SEli Cohen 
1510e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1511e126ba97SEli Cohen 		return 0;
1512e126ba97SEli Cohen 
1513e126ba97SEli Cohen 	/*
1514e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1515e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1516e126ba97SEli Cohen 	 */
1517bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
15189603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1519e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1520e126ba97SEli Cohen 	if (err)
1521e126ba97SEli Cohen 		return err;
1522e126ba97SEli Cohen 
1523bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1524e126ba97SEli Cohen 
1525e126ba97SEli Cohen 	return err;
1526e126ba97SEli Cohen }
1527e126ba97SEli Cohen 
1528cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1529cdbe33d0SEli Cohen 				u32 value)
1530cdbe33d0SEli Cohen {
1531cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1532b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1533b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1534cdbe33d0SEli Cohen 	int err;
1535cdbe33d0SEli Cohen 
1536b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1537b3cbd6f0SDaniel Jurgens 	if (!mdev)
1538b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1539b3cbd6f0SDaniel Jurgens 
1540b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1541cdbe33d0SEli Cohen 	if (err)
1542b3cbd6f0SDaniel Jurgens 		goto out;
1543cdbe33d0SEli Cohen 
1544cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1545cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1546cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1547b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1548b3cbd6f0SDaniel Jurgens 		goto out;
1549cdbe33d0SEli Cohen 	}
1550cdbe33d0SEli Cohen 
1551cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1552cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1553b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1554b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1555b3cbd6f0SDaniel Jurgens 
1556b3cbd6f0SDaniel Jurgens out:
1557b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1558cdbe33d0SEli Cohen 
1559cdbe33d0SEli Cohen 	return err;
1560cdbe33d0SEli Cohen }
1561cdbe33d0SEli Cohen 
1562e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1563e126ba97SEli Cohen 			       struct ib_port_modify *props)
1564e126ba97SEli Cohen {
1565e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1566e126ba97SEli Cohen 	struct ib_port_attr attr;
1567e126ba97SEli Cohen 	u32 tmp;
1568e126ba97SEli Cohen 	int err;
1569cdbe33d0SEli Cohen 	u32 change_mask;
1570cdbe33d0SEli Cohen 	u32 value;
1571cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1572cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1573cdbe33d0SEli Cohen 
1574ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1575ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1576ec255879SMajd Dibbiny 	 */
1577ec255879SMajd Dibbiny 	if (!is_ib)
1578ec255879SMajd Dibbiny 		return 0;
1579ec255879SMajd Dibbiny 
1580cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1581cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1582cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1583cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1584cdbe33d0SEli Cohen 	}
1585e126ba97SEli Cohen 
1586e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1587e126ba97SEli Cohen 
1588c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1589e126ba97SEli Cohen 	if (err)
1590e126ba97SEli Cohen 		goto out;
1591e126ba97SEli Cohen 
1592e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1593e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1594e126ba97SEli Cohen 
15959603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1596e126ba97SEli Cohen 
1597e126ba97SEli Cohen out:
1598e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1599e126ba97SEli Cohen 	return err;
1600e126ba97SEli Cohen }
1601e126ba97SEli Cohen 
160230aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
160330aa60b3SEli Cohen {
160430aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
160530aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
160630aa60b3SEli Cohen }
160730aa60b3SEli Cohen 
160831a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
160931a78a5aSYishai Hadas {
161031a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
161131a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
161231a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
161331a78a5aSYishai Hadas 
161431a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
161531a78a5aSYishai Hadas }
161631a78a5aSYishai Hadas 
1617b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1618b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
161931a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1620b037c29aSEli Cohen {
1621b037c29aSEli Cohen 	int uars_per_sys_page;
1622b037c29aSEli Cohen 	int bfregs_per_sys_page;
1623b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1624b037c29aSEli Cohen 
1625b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1626b037c29aSEli Cohen 		return -EINVAL;
1627b037c29aSEli Cohen 
1628b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1629b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1630b037c29aSEli Cohen 
1631b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1632b037c29aSEli Cohen 		return -ENOMEM;
1633b037c29aSEli Cohen 
1634b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1635b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
163631a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1637b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1638b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1639b037c29aSEli Cohen 		return -EINVAL;
1640b037c29aSEli Cohen 
164131a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
164231a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
164331a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
164431a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
164531a78a5aSYishai Hadas 
164631a78a5aSYishai 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",
1647b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1648b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
164931a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
165031a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1651b037c29aSEli Cohen 
1652b037c29aSEli Cohen 	return 0;
1653b037c29aSEli Cohen }
1654b037c29aSEli Cohen 
1655b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1656b037c29aSEli Cohen {
1657b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1658b037c29aSEli Cohen 	int err;
1659b037c29aSEli Cohen 	int i;
1660b037c29aSEli Cohen 
1661b037c29aSEli Cohen 	bfregi = &context->bfregi;
166231a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1663b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1664b037c29aSEli Cohen 		if (err)
1665b037c29aSEli Cohen 			goto error;
1666b037c29aSEli Cohen 
1667b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1668b037c29aSEli Cohen 	}
16694ed131d0SYishai Hadas 
16704ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
16714ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
16724ed131d0SYishai Hadas 
1673b037c29aSEli Cohen 	return 0;
1674b037c29aSEli Cohen 
1675b037c29aSEli Cohen error:
1676b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1677b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1678b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1679b037c29aSEli Cohen 
1680b037c29aSEli Cohen 	return err;
1681b037c29aSEli Cohen }
1682b037c29aSEli Cohen 
168315177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
168415177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1685b037c29aSEli Cohen {
1686b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1687b037c29aSEli Cohen 	int i;
1688b037c29aSEli Cohen 
1689b037c29aSEli Cohen 	bfregi = &context->bfregi;
169015177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
16914ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
169215177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
169315177999SLeon Romanovsky 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1694b037c29aSEli Cohen }
1695b037c29aSEli Cohen 
16960042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1697a560f1d9SMark Bloch {
1698a560f1d9SMark Bloch 	int err = 0;
1699a560f1d9SMark Bloch 
1700a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
17010042f9e4SMark Bloch 	if (td)
1702a560f1d9SMark Bloch 		dev->lb.user_td++;
17030042f9e4SMark Bloch 	if (qp)
17040042f9e4SMark Bloch 		dev->lb.qps++;
1705a560f1d9SMark Bloch 
17060042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
17070042f9e4SMark Bloch 	    dev->lb.qps == 1) {
17080042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1709a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
17100042f9e4SMark Bloch 			dev->lb.enabled = true;
17110042f9e4SMark Bloch 		}
17120042f9e4SMark Bloch 	}
1713a560f1d9SMark Bloch 
1714a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1715a560f1d9SMark Bloch 
1716a560f1d9SMark Bloch 	return err;
1717a560f1d9SMark Bloch }
1718a560f1d9SMark Bloch 
17190042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1720a560f1d9SMark Bloch {
1721a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
17220042f9e4SMark Bloch 	if (td)
1723a560f1d9SMark Bloch 		dev->lb.user_td--;
17240042f9e4SMark Bloch 	if (qp)
17250042f9e4SMark Bloch 		dev->lb.qps--;
1726a560f1d9SMark Bloch 
17270042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
17280042f9e4SMark Bloch 	    dev->lb.qps == 0) {
17290042f9e4SMark Bloch 		if (dev->lb.enabled) {
1730a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
17310042f9e4SMark Bloch 			dev->lb.enabled = false;
17320042f9e4SMark Bloch 		}
17330042f9e4SMark Bloch 	}
1734a560f1d9SMark Bloch 
1735a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1736a560f1d9SMark Bloch }
1737a560f1d9SMark Bloch 
1738d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1739d2d19121SYishai Hadas 					  u16 uid)
1740c85023e1SHuy Nguyen {
1741c85023e1SHuy Nguyen 	int err;
1742c85023e1SHuy Nguyen 
1743cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1744cfdeb893SLeon Romanovsky 		return 0;
1745cfdeb893SLeon Romanovsky 
1746d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1747c85023e1SHuy Nguyen 	if (err)
1748c85023e1SHuy Nguyen 		return err;
1749c85023e1SHuy Nguyen 
1750c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17518978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17528978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1753c85023e1SHuy Nguyen 		return err;
1754c85023e1SHuy Nguyen 
17550042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1756c85023e1SHuy Nguyen }
1757c85023e1SHuy Nguyen 
1758d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1759d2d19121SYishai Hadas 					     u16 uid)
1760c85023e1SHuy Nguyen {
1761cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1762cfdeb893SLeon Romanovsky 		return;
1763cfdeb893SLeon Romanovsky 
1764d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1765c85023e1SHuy Nguyen 
1766c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17678978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17688978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1769c85023e1SHuy Nguyen 		return;
1770c85023e1SHuy Nguyen 
17710042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1772c85023e1SHuy Nguyen }
1773c85023e1SHuy Nguyen 
1774a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1775e126ba97SEli Cohen 				  struct ib_udata *udata)
1776e126ba97SEli Cohen {
1777a2a074efSLeon Romanovsky 	struct ib_device *ibdev = uctx->device;
1778e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1779b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1780b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
17815c99eaecSFeras Daoud 	struct mlx5_core_dev *mdev = dev->mdev;
1782a2a074efSLeon Romanovsky 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
17832f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
178478c0f98cSEli Cohen 	int ver;
1785e126ba97SEli Cohen 	int err;
1786a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1787a168a41cSMajd Dibbiny 				     max_cqe_version);
178825bb36e7SYonatan Cohen 	u32 dump_fill_mkey;
1789b037c29aSEli Cohen 	bool lib_uar_4k;
17900a2fd01cSYishai Hadas 	bool lib_uar_dyn;
1791e126ba97SEli Cohen 
1792e126ba97SEli Cohen 	if (!dev->ib_active)
1793a2a074efSLeon Romanovsky 		return -EAGAIN;
1794e126ba97SEli Cohen 
1795e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
179678c0f98cSEli Cohen 		ver = 0;
1797e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
179878c0f98cSEli Cohen 		ver = 2;
179978c0f98cSEli Cohen 	else
1800a2a074efSLeon Romanovsky 		return -EINVAL;
180178c0f98cSEli Cohen 
1802e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1803e126ba97SEli Cohen 	if (err)
1804a2a074efSLeon Romanovsky 		return err;
1805e126ba97SEli Cohen 
1806a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1807a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
180878c0f98cSEli Cohen 
1809f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1810a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
1811b368d7cbSMatan Barak 
18122f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
18132f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
18142f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1815a2a074efSLeon Romanovsky 		return -EINVAL;
1816e126ba97SEli Cohen 
1817938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
181811f552e2SMichael Guralnik 	if (dev->wc_support)
1819938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1820b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1821938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1822938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1823938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1824938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1825938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1826f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1827f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1828f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
182930aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
183030aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
183130aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
183230aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1833b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1834b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1835e126ba97SEli Cohen 
1836c03faa56SMatan Barak 	if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1837c03faa56SMatan Barak 		if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1838c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1839c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1840c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1841c03faa56SMatan Barak 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1842c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1843c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1844c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1845c03faa56SMatan Barak 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1846c03faa56SMatan Barak 	}
1847c03faa56SMatan Barak 
184830aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
18490a2fd01cSYishai Hadas 	lib_uar_dyn = req.lib_caps & MLX5_LIB_CAP_DYN_UAR;
18502f5ff264SEli Cohen 	bfregi = &context->bfregi;
1851b037c29aSEli Cohen 
18520a2fd01cSYishai Hadas 	if (lib_uar_dyn) {
18530a2fd01cSYishai Hadas 		bfregi->lib_uar_dyn = lib_uar_dyn;
18540a2fd01cSYishai Hadas 		goto uar_done;
18550a2fd01cSYishai Hadas 	}
18560a2fd01cSYishai Hadas 
1857b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
185831a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1859b037c29aSEli Cohen 	if (err)
1860b037c29aSEli Cohen 		goto out_ctx;
1861b037c29aSEli Cohen 
18622f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1863b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
186431a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1865b037c29aSEli Cohen 				GFP_KERNEL);
1866b037c29aSEli Cohen 	if (!bfregi->count) {
1867e126ba97SEli Cohen 		err = -ENOMEM;
1868e126ba97SEli Cohen 		goto out_ctx;
1869e126ba97SEli Cohen 	}
1870e126ba97SEli Cohen 
1871b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1872b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1873e126ba97SEli Cohen 				    GFP_KERNEL);
1874b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1875e126ba97SEli Cohen 		err = -ENOMEM;
1876e126ba97SEli Cohen 		goto out_count;
1877e126ba97SEli Cohen 	}
1878e126ba97SEli Cohen 
1879b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1880b037c29aSEli Cohen 	if (err)
1881b037c29aSEli Cohen 		goto out_sys_pages;
1882b037c29aSEli Cohen 
18830a2fd01cSYishai Hadas uar_done:
1884a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1885fb98153bSYishai Hadas 		err = mlx5_ib_devx_create(dev, true);
188676dc5a84SYishai Hadas 		if (err < 0)
1887d2d19121SYishai Hadas 			goto out_uars;
188876dc5a84SYishai Hadas 		context->devx_uid = err;
1889a8b92ca1SYishai Hadas 	}
1890a8b92ca1SYishai Hadas 
1891d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1892d2d19121SYishai Hadas 					     context->devx_uid);
1893a8b92ca1SYishai Hadas 	if (err)
1894d2d19121SYishai Hadas 		goto out_devx;
1895a8b92ca1SYishai Hadas 
189625bb36e7SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
189725bb36e7SYonatan Cohen 		err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
189825bb36e7SYonatan Cohen 		if (err)
18998193abb6SJason Gunthorpe 			goto out_mdev;
190025bb36e7SYonatan Cohen 	}
190125bb36e7SYonatan Cohen 
1902e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1903e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1904e126ba97SEli Cohen 
19050a2fd01cSYishai Hadas 	resp.tot_bfregs = lib_uar_dyn ? 0 : req.total_num_bfregs;
1906508562d6SDaniel Jurgens 	resp.num_ports = dev->num_ports;
1907b368d7cbSMatan Barak 
1908a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), cqe_version) <= udata->outlen)
1909f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1910b368d7cbSMatan Barak 
1911a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), cmds_supp_uhw) <= udata->outlen) {
19126ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
19136ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1914402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1915402ca536SBodong Wang 	}
1916402ca536SBodong Wang 
1917a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), eth_min_inline) <= udata->outlen) {
191878984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
191978984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
192078984898SOr Gerlitz 			resp.eth_min_inline++;
192178984898SOr Gerlitz 		}
192278984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
192378984898SOr Gerlitz 	}
192478984898SOr Gerlitz 
1925a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), clock_info_versions) <= udata->outlen) {
19265c99eaecSFeras Daoud 		if (mdev->clock_info)
19275c99eaecSFeras Daoud 			resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
19285c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.clock_info_versions);
19295c99eaecSFeras Daoud 	}
19305c99eaecSFeras Daoud 
1931bc5c6eedSNoa Osherovich 	/*
1932bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1933bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1934bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1935bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1936bc5c6eedSNoa Osherovich 	 */
1937a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), hca_core_clock_offset) <= udata->outlen) {
1938de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1939b368d7cbSMatan Barak 			resp.comp_mask |=
1940b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1941b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1942de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1943de8d6e02SEli Cohen 		}
19445c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.hca_core_clock_offset);
1945b368d7cbSMatan Barak 	}
1946b368d7cbSMatan Barak 
1947a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), log_uar_size) <= udata->outlen)
194830aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
194930aa60b3SEli Cohen 
1950a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), num_uars_per_page) <= udata->outlen)
195130aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
195230aa60b3SEli Cohen 
1953a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), num_dyn_bfregs) <= udata->outlen) {
195431a78a5aSYishai Hadas 		resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
195531a78a5aSYishai Hadas 		resp.response_length += sizeof(resp.num_dyn_bfregs);
195631a78a5aSYishai Hadas 	}
195731a78a5aSYishai Hadas 
1958a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), dump_fill_mkey) <= udata->outlen) {
195925bb36e7SYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
196025bb36e7SYonatan Cohen 			resp.dump_fill_mkey = dump_fill_mkey;
196125bb36e7SYonatan Cohen 			resp.comp_mask |=
196225bb36e7SYonatan Cohen 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
196325bb36e7SYonatan Cohen 		}
196425bb36e7SYonatan Cohen 		resp.response_length += sizeof(resp.dump_fill_mkey);
196525bb36e7SYonatan Cohen 	}
196625bb36e7SYonatan Cohen 
1967b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1968e126ba97SEli Cohen 	if (err)
1969a8b92ca1SYishai Hadas 		goto out_mdev;
1970e126ba97SEli Cohen 
19712f5ff264SEli Cohen 	bfregi->ver = ver;
19722f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1973f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
197430aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
197530aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1976f72300c5SHaggai Abramovsky 
19777c34ec19SAviv Heller 	if (dev->lag_active) {
197895579e78SMark Bloch 		u8 port = mlx5_core_native_port_num(dev->mdev) - 1;
1979c6a21c38SMajd Dibbiny 
1980c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1981c6a21c38SMajd Dibbiny 			   atomic_add_return(
198295579e78SMark Bloch 				   1, &dev->port[port].roce.tx_port_affinity));
1983c6a21c38SMajd Dibbiny 	}
1984c6a21c38SMajd Dibbiny 
1985a2a074efSLeon Romanovsky 	return 0;
1986e126ba97SEli Cohen 
1987a8b92ca1SYishai Hadas out_mdev:
1988d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1989d2d19121SYishai Hadas out_devx:
1990a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
199176dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1992146d2f1aSmajd@mellanox.com 
1993e126ba97SEli Cohen out_uars:
1994b037c29aSEli Cohen 	deallocate_uars(dev, context);
1995b037c29aSEli Cohen 
1996b037c29aSEli Cohen out_sys_pages:
1997b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1998b037c29aSEli Cohen 
1999e126ba97SEli Cohen out_count:
20002f5ff264SEli Cohen 	kfree(bfregi->count);
2001e126ba97SEli Cohen 
2002e126ba97SEli Cohen out_ctx:
2003a2a074efSLeon Romanovsky 	return err;
2004e126ba97SEli Cohen }
2005e126ba97SEli Cohen 
2006a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
2007e126ba97SEli Cohen {
2008e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2009e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
2010b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
2011e126ba97SEli Cohen 
2012b037c29aSEli Cohen 	bfregi = &context->bfregi;
2013d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2014d2d19121SYishai Hadas 
2015b037c29aSEli Cohen 	if (context->devx_uid)
201676dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
2017e126ba97SEli Cohen 
2018e126ba97SEli Cohen 	deallocate_uars(dev, context);
20192f5ff264SEli Cohen 	kfree(bfregi->sys_pages);
20202f5ff264SEli Cohen 	kfree(bfregi->count);
2021e126ba97SEli Cohen }
2022e126ba97SEli Cohen 
2023e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2024e126ba97SEli Cohen 				 int uar_idx)
2025e126ba97SEli Cohen {
2026b037c29aSEli Cohen 	int fw_uars_per_page;
2027b037c29aSEli Cohen 
2028b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2029b037c29aSEli Cohen 
2030aa8106f1SHuy Nguyen 	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2031e126ba97SEli Cohen }
2032e126ba97SEli Cohen 
2033342ee59dSYishai Hadas static u64 uar_index2paddress(struct mlx5_ib_dev *dev,
2034342ee59dSYishai Hadas 				 int uar_idx)
2035342ee59dSYishai Hadas {
2036342ee59dSYishai Hadas 	unsigned int fw_uars_per_page;
2037342ee59dSYishai Hadas 
2038342ee59dSYishai Hadas 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
2039342ee59dSYishai Hadas 				MLX5_UARS_IN_PAGE : 1;
2040342ee59dSYishai Hadas 
2041342ee59dSYishai Hadas 	return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE);
2042342ee59dSYishai Hadas }
2043342ee59dSYishai Hadas 
2044e126ba97SEli Cohen static int get_command(unsigned long offset)
2045e126ba97SEli Cohen {
2046e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2047e126ba97SEli Cohen }
2048e126ba97SEli Cohen 
2049e126ba97SEli Cohen static int get_arg(unsigned long offset)
2050e126ba97SEli Cohen {
2051e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2052e126ba97SEli Cohen }
2053e126ba97SEli Cohen 
2054e126ba97SEli Cohen static int get_index(unsigned long offset)
2055e126ba97SEli Cohen {
2056e126ba97SEli Cohen 	return get_arg(offset);
2057e126ba97SEli Cohen }
2058e126ba97SEli Cohen 
20594ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
20604ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20614ed131d0SYishai Hadas {
20624ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20634ed131d0SYishai Hadas }
20644ed131d0SYishai Hadas 
20657c2344c3SMaor Gottlieb 
20667c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20677c2344c3SMaor Gottlieb {
20687c2344c3SMaor Gottlieb }
20697c2344c3SMaor Gottlieb 
207037aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2071e126ba97SEli Cohen {
207237aa5c36SGuy Levi 	switch (cmd) {
207337aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
207437aa5c36SGuy Levi 		return "WC";
2075e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
207637aa5c36SGuy Levi 		return "best effort WC";
207737aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
207837aa5c36SGuy Levi 		return "NC";
207924da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
208024da0016SAriel Levkovich 		return "Device Memory";
208137aa5c36SGuy Levi 	default:
208237aa5c36SGuy Levi 		return NULL;
208337aa5c36SGuy Levi 	}
208437aa5c36SGuy Levi }
208537aa5c36SGuy Levi 
20865c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20875c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20885c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20895c99eaecSFeras Daoud {
20904eb6ab13SJason Gunthorpe 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
20914eb6ab13SJason Gunthorpe 	    !(vma->vm_flags & VM_SHARED))
20925c99eaecSFeras Daoud 		return -EINVAL;
20935c99eaecSFeras Daoud 
20945c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20955c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20965c99eaecSFeras Daoud 
20974eb6ab13SJason Gunthorpe 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
20985c99eaecSFeras Daoud 		return -EPERM;
2099c660133cSJason Gunthorpe 	vma->vm_flags &= ~VM_MAYWRITE;
21005c99eaecSFeras Daoud 
2101ddcdc368SJason Gunthorpe 	if (!dev->mdev->clock_info)
21025c99eaecSFeras Daoud 		return -EOPNOTSUPP;
21035c99eaecSFeras Daoud 
21044eb6ab13SJason Gunthorpe 	return vm_insert_page(vma, vma->vm_start,
21054eb6ab13SJason Gunthorpe 			      virt_to_page(dev->mdev->clock_info));
21065c99eaecSFeras Daoud }
21075c99eaecSFeras Daoud 
2108dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
2109dc2316ebSYishai Hadas {
2110dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
2111dc2316ebSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device);
21127be76befSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
2113dc2316ebSYishai Hadas 	struct mlx5_ib_dm *mdm;
2114dc2316ebSYishai Hadas 
2115dc2316ebSYishai Hadas 	switch (mentry->mmap_flag) {
2116dc2316ebSYishai Hadas 	case MLX5_IB_MMAP_TYPE_MEMIC:
2117dc2316ebSYishai Hadas 		mdm = container_of(mentry, struct mlx5_ib_dm, mentry);
2118dc2316ebSYishai Hadas 		mlx5_cmd_dealloc_memic(&dev->dm, mdm->dev_addr,
2119dc2316ebSYishai Hadas 				       mdm->size);
2120dc2316ebSYishai Hadas 		kfree(mdm);
2121dc2316ebSYishai Hadas 		break;
21227be76befSYishai Hadas 	case MLX5_IB_MMAP_TYPE_VAR:
21237be76befSYishai Hadas 		mutex_lock(&var_table->bitmap_lock);
21247be76befSYishai Hadas 		clear_bit(mentry->page_idx, var_table->bitmap);
21257be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
21267be76befSYishai Hadas 		kfree(mentry);
21277be76befSYishai Hadas 		break;
2128342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_WC:
2129342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_NC:
2130342ee59dSYishai Hadas 		mlx5_cmd_free_uar(dev->mdev, mentry->page_idx);
2131342ee59dSYishai Hadas 		kfree(mentry);
2132342ee59dSYishai Hadas 		break;
2133dc2316ebSYishai Hadas 	default:
2134dc2316ebSYishai Hadas 		WARN_ON(true);
2135dc2316ebSYishai Hadas 	}
2136dc2316ebSYishai Hadas }
2137dc2316ebSYishai Hadas 
213837aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
21397c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
21407c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
214137aa5c36SGuy Levi {
21422f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
214337aa5c36SGuy Levi 	int err;
214437aa5c36SGuy Levi 	unsigned long idx;
2145aa09ea6eSKamal Heib 	phys_addr_t pfn;
214637aa5c36SGuy Levi 	pgprot_t prot;
21474ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
21484ed131d0SYishai Hadas 	u32 uar_index;
21494ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
21504ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
21514ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2152b037c29aSEli Cohen 
21530a2fd01cSYishai Hadas 	if (bfregi->lib_uar_dyn)
21540a2fd01cSYishai Hadas 		return -EINVAL;
21550a2fd01cSYishai Hadas 
2156b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2157b037c29aSEli Cohen 		return -EINVAL;
2158b037c29aSEli Cohen 
21594ed131d0SYishai Hadas 	if (dyn_uar)
21604ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
21614ed131d0SYishai Hadas 	else
2162b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
21634ed131d0SYishai Hadas 
21644ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21654ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21664ed131d0SYishai Hadas 			     idx, max_valid_idx);
2167b037c29aSEli Cohen 		return -EINVAL;
2168b037c29aSEli Cohen 	}
216937aa5c36SGuy Levi 
217037aa5c36SGuy Levi 	switch (cmd) {
217137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21724ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
217337aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
217437aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
217537aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
217637aa5c36SGuy Levi 		break;
217737aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
217837aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
217937aa5c36SGuy Levi 		break;
218037aa5c36SGuy Levi 	default:
218137aa5c36SGuy Levi 		return -EINVAL;
218237aa5c36SGuy Levi 	}
218337aa5c36SGuy Levi 
21844ed131d0SYishai Hadas 	if (dyn_uar) {
21854ed131d0SYishai Hadas 		int uars_per_page;
21864ed131d0SYishai Hadas 
21874ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21884ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21894ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21904ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21914ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21924ed131d0SYishai Hadas 			return -EINVAL;
21934ed131d0SYishai Hadas 		}
21944ed131d0SYishai Hadas 
21954ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21964ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21974ed131d0SYishai Hadas 		 * page holds its count.
21984ed131d0SYishai Hadas 		 */
21994ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
22004ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
22014ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
22024ed131d0SYishai Hadas 			return -EINVAL;
22034ed131d0SYishai Hadas 		}
22044ed131d0SYishai Hadas 
22054ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
22064ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
22074ed131d0SYishai Hadas 
22084ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
22094ed131d0SYishai Hadas 		if (err) {
22104ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
22114ed131d0SYishai Hadas 			goto free_bfreg;
22124ed131d0SYishai Hadas 		}
22134ed131d0SYishai Hadas 	} else {
22144ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
22154ed131d0SYishai Hadas 	}
22164ed131d0SYishai Hadas 
22174ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
221837aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2219e126ba97SEli Cohen 
2220e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2221c043ff2cSMichal Kalderon 				prot, NULL);
222237aa5c36SGuy Levi 	if (err) {
22238f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2224e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
22258f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
22264ed131d0SYishai Hadas 		goto err;
222737aa5c36SGuy Levi 	}
2228e126ba97SEli Cohen 
22294ed131d0SYishai Hadas 	if (dyn_uar)
22304ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
22314ed131d0SYishai Hadas 	return 0;
22324ed131d0SYishai Hadas 
22334ed131d0SYishai Hadas err:
22344ed131d0SYishai Hadas 	if (!dyn_uar)
22354ed131d0SYishai Hadas 		return err;
22364ed131d0SYishai Hadas 
22374ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
22384ed131d0SYishai Hadas 
22394ed131d0SYishai Hadas free_bfreg:
22404ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
22414ed131d0SYishai Hadas 
22424ed131d0SYishai Hadas 	return err;
224337aa5c36SGuy Levi }
224437aa5c36SGuy Levi 
2245dc2316ebSYishai Hadas static int add_dm_mmap_entry(struct ib_ucontext *context,
2246dc2316ebSYishai Hadas 			     struct mlx5_ib_dm *mdm,
2247dc2316ebSYishai Hadas 			     u64 address)
224824da0016SAriel Levkovich {
2249dc2316ebSYishai Hadas 	mdm->mentry.mmap_flag = MLX5_IB_MMAP_TYPE_MEMIC;
2250dc2316ebSYishai Hadas 	mdm->mentry.address = address;
2251dc2316ebSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
2252dc2316ebSYishai Hadas 			context, &mdm->mentry.rdma_entry,
2253dc2316ebSYishai Hadas 			mdm->size,
2254dc2316ebSYishai Hadas 			MLX5_IB_MMAP_DEVICE_MEM << 16,
2255dc2316ebSYishai Hadas 			(MLX5_IB_MMAP_DEVICE_MEM << 16) + (1UL << 16) - 1);
2256dc2316ebSYishai Hadas }
225724da0016SAriel Levkovich 
2258dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma)
2259dc2316ebSYishai Hadas {
2260dc2316ebSYishai Hadas 	unsigned long idx;
2261dc2316ebSYishai Hadas 	u8 command;
2262dc2316ebSYishai Hadas 
2263dc2316ebSYishai Hadas 	command = get_command(vma->vm_pgoff);
2264dc2316ebSYishai Hadas 	idx = get_extended_index(vma->vm_pgoff);
2265dc2316ebSYishai Hadas 
2266dc2316ebSYishai Hadas 	return (command << 16 | idx);
2267dc2316ebSYishai Hadas }
2268dc2316ebSYishai Hadas 
2269dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev,
2270dc2316ebSYishai Hadas 			       struct vm_area_struct *vma,
2271dc2316ebSYishai Hadas 			       struct ib_ucontext *ucontext)
2272dc2316ebSYishai Hadas {
2273dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry;
2274dc2316ebSYishai Hadas 	struct rdma_user_mmap_entry *entry;
2275dc2316ebSYishai Hadas 	unsigned long pgoff;
2276dc2316ebSYishai Hadas 	pgprot_t prot;
2277dc2316ebSYishai Hadas 	phys_addr_t pfn;
2278dc2316ebSYishai Hadas 	int ret;
2279dc2316ebSYishai Hadas 
2280dc2316ebSYishai Hadas 	pgoff = mlx5_vma_to_pgoff(vma);
2281dc2316ebSYishai Hadas 	entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff);
2282dc2316ebSYishai Hadas 	if (!entry)
228324da0016SAriel Levkovich 		return -EINVAL;
228424da0016SAriel Levkovich 
2285dc2316ebSYishai Hadas 	mentry = to_mmmap(entry);
2286dc2316ebSYishai Hadas 	pfn = (mentry->address >> PAGE_SHIFT);
2287342ee59dSYishai Hadas 	if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR ||
2288342ee59dSYishai Hadas 	    mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC)
22893f59b6c3SYishai Hadas 		prot = pgprot_noncached(vma->vm_page_prot);
22903f59b6c3SYishai Hadas 	else
2291dc2316ebSYishai Hadas 		prot = pgprot_writecombine(vma->vm_page_prot);
2292dc2316ebSYishai Hadas 	ret = rdma_user_mmap_io(ucontext, vma, pfn,
2293dc2316ebSYishai Hadas 				entry->npages * PAGE_SIZE,
2294dc2316ebSYishai Hadas 				prot,
2295dc2316ebSYishai Hadas 				entry);
2296dc2316ebSYishai Hadas 	rdma_user_mmap_entry_put(&mentry->rdma_entry);
2297dc2316ebSYishai Hadas 	return ret;
229824da0016SAriel Levkovich }
229924da0016SAriel Levkovich 
23007be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry)
23017be76befSYishai Hadas {
23029b6d3bbcSLeon Romanovsky 	u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF;
23039b6d3bbcSLeon Romanovsky 	u64 index = entry->rdma_entry.start_pgoff & 0xFFFF;
23047be76befSYishai Hadas 
23057be76befSYishai Hadas 	return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) |
23067be76befSYishai Hadas 		(index & 0xFF)) << PAGE_SHIFT;
23077be76befSYishai Hadas }
23087be76befSYishai Hadas 
230937aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
231037aa5c36SGuy Levi {
231137aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
231237aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
231337aa5c36SGuy Levi 	unsigned long command;
231437aa5c36SGuy Levi 	phys_addr_t pfn;
231537aa5c36SGuy Levi 
231637aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
231737aa5c36SGuy Levi 	switch (command) {
231837aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
23191f3db161SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
23201f3db161SYishai Hadas 		if (!dev->wc_support)
23211f3db161SYishai Hadas 			return -EPERM;
23221f3db161SYishai Hadas 		fallthrough;
232337aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
232437aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
23257c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2326e126ba97SEli Cohen 
2327e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2328e126ba97SEli Cohen 		return -ENOSYS;
2329e126ba97SEli Cohen 
2330d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2331d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2332d69e3bcfSMatan Barak 			return -EINVAL;
2333d69e3bcfSMatan Barak 
23346cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2335d69e3bcfSMatan Barak 			return -EPERM;
2336c660133cSJason Gunthorpe 		vma->vm_flags &= ~VM_MAYWRITE;
2337d69e3bcfSMatan Barak 
2338d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2339d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2340d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2341d69e3bcfSMatan Barak 
2342d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2343d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2344d69e3bcfSMatan Barak 			PAGE_SHIFT;
2345d5e560d3SJason Gunthorpe 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2346d5e560d3SJason Gunthorpe 					 PAGE_SIZE,
2347c043ff2cSMichal Kalderon 					 pgprot_noncached(vma->vm_page_prot),
2348c043ff2cSMichal Kalderon 					 NULL);
23495c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
23505c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2351d69e3bcfSMatan Barak 
2352e126ba97SEli Cohen 	default:
2353dc2316ebSYishai Hadas 		return mlx5_ib_mmap_offset(dev, vma, ibcontext);
2354e126ba97SEli Cohen 	}
2355e126ba97SEli Cohen 
2356e126ba97SEli Cohen 	return 0;
2357e126ba97SEli Cohen }
2358e126ba97SEli Cohen 
235925c13324SAriel Levkovich static inline int check_dm_type_support(struct mlx5_ib_dev *dev,
236025c13324SAriel Levkovich 					u32 type)
236125c13324SAriel Levkovich {
236225c13324SAriel Levkovich 	switch (type) {
236325c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
236425c13324SAriel Levkovich 		if (!MLX5_CAP_DEV_MEM(dev->mdev, memic))
236525c13324SAriel Levkovich 			return -EOPNOTSUPP;
236625c13324SAriel Levkovich 		break;
236725c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2368c9b9dcb4SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
236925c13324SAriel Levkovich 		if (!capable(CAP_SYS_RAWIO) ||
237025c13324SAriel Levkovich 		    !capable(CAP_NET_RAW))
237125c13324SAriel Levkovich 			return -EPERM;
237225c13324SAriel Levkovich 
237325c13324SAriel Levkovich 		if (!(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner) ||
237425c13324SAriel Levkovich 		      MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner)))
237525c13324SAriel Levkovich 			return -EOPNOTSUPP;
237625c13324SAriel Levkovich 		break;
237725c13324SAriel Levkovich 	}
237825c13324SAriel Levkovich 
237925c13324SAriel Levkovich 	return 0;
238025c13324SAriel Levkovich }
238125c13324SAriel Levkovich 
23823b113a1eSAriel Levkovich static int handle_alloc_dm_memic(struct ib_ucontext *ctx,
23833b113a1eSAriel Levkovich 				 struct mlx5_ib_dm *dm,
238424da0016SAriel Levkovich 				 struct ib_dm_alloc_attr *attr,
238524da0016SAriel Levkovich 				 struct uverbs_attr_bundle *attrs)
238624da0016SAriel Levkovich {
23873b113a1eSAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
238824da0016SAriel Levkovich 	u64 start_offset;
2389dc2316ebSYishai Hadas 	u16 page_idx;
239024da0016SAriel Levkovich 	int err;
2391dc2316ebSYishai Hadas 	u64 address;
239224da0016SAriel Levkovich 
23933b113a1eSAriel Levkovich 	dm->size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
239424da0016SAriel Levkovich 
23953b113a1eSAriel Levkovich 	err = mlx5_cmd_alloc_memic(dm_db, &dm->dev_addr,
23963b113a1eSAriel Levkovich 				   dm->size, attr->alignment);
239724da0016SAriel Levkovich 	if (err)
23983b113a1eSAriel Levkovich 		return err;
239924da0016SAriel Levkovich 
2400dc2316ebSYishai Hadas 	address = dm->dev_addr & PAGE_MASK;
2401dc2316ebSYishai Hadas 	err = add_dm_mmap_entry(ctx, dm, address);
240224da0016SAriel Levkovich 	if (err)
240324da0016SAriel Levkovich 		goto err_dealloc;
240424da0016SAriel Levkovich 
2405dc2316ebSYishai Hadas 	page_idx = dm->mentry.rdma_entry.start_pgoff & 0xFFFF;
2406dc2316ebSYishai Hadas 	err = uverbs_copy_to(attrs,
2407dc2316ebSYishai Hadas 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2408dc2316ebSYishai Hadas 			     &page_idx,
2409dc2316ebSYishai Hadas 			     sizeof(page_idx));
2410dc2316ebSYishai Hadas 	if (err)
2411dc2316ebSYishai Hadas 		goto err_copy;
2412dc2316ebSYishai Hadas 
24133b113a1eSAriel Levkovich 	start_offset = dm->dev_addr & ~PAGE_MASK;
24143b113a1eSAriel Levkovich 	err = uverbs_copy_to(attrs,
24153b113a1eSAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
24163b113a1eSAriel Levkovich 			     &start_offset, sizeof(start_offset));
24173b113a1eSAriel Levkovich 	if (err)
2418dc2316ebSYishai Hadas 		goto err_copy;
24193b113a1eSAriel Levkovich 
24203b113a1eSAriel Levkovich 	return 0;
24213b113a1eSAriel Levkovich 
2422dc2316ebSYishai Hadas err_copy:
2423dc2316ebSYishai Hadas 	rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
24243b113a1eSAriel Levkovich err_dealloc:
24253b113a1eSAriel Levkovich 	mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
24263b113a1eSAriel Levkovich 
24273b113a1eSAriel Levkovich 	return err;
24283b113a1eSAriel Levkovich }
24293b113a1eSAriel Levkovich 
243025c13324SAriel Levkovich static int handle_alloc_dm_sw_icm(struct ib_ucontext *ctx,
243125c13324SAriel Levkovich 				  struct mlx5_ib_dm *dm,
243225c13324SAriel Levkovich 				  struct ib_dm_alloc_attr *attr,
243325c13324SAriel Levkovich 				  struct uverbs_attr_bundle *attrs,
243425c13324SAriel Levkovich 				  int type)
243525c13324SAriel Levkovich {
2436c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ctx->device)->mdev;
243725c13324SAriel Levkovich 	u64 act_size;
243825c13324SAriel Levkovich 	int err;
243925c13324SAriel Levkovich 
244025c13324SAriel Levkovich 	/* Allocation size must a multiple of the basic block size
244125c13324SAriel Levkovich 	 * and a power of 2.
244225c13324SAriel Levkovich 	 */
2443c9b9dcb4SAriel Levkovich 	act_size = round_up(attr->length, MLX5_SW_ICM_BLOCK_SIZE(dev));
244425c13324SAriel Levkovich 	act_size = roundup_pow_of_two(act_size);
244525c13324SAriel Levkovich 
244625c13324SAriel Levkovich 	dm->size = act_size;
2447c9b9dcb4SAriel Levkovich 	err = mlx5_dm_sw_icm_alloc(dev, type, act_size,
244825c13324SAriel Levkovich 				   to_mucontext(ctx)->devx_uid, &dm->dev_addr,
244925c13324SAriel Levkovich 				   &dm->icm_dm.obj_id);
245025c13324SAriel Levkovich 	if (err)
245125c13324SAriel Levkovich 		return err;
245225c13324SAriel Levkovich 
245325c13324SAriel Levkovich 	err = uverbs_copy_to(attrs,
245425c13324SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
245525c13324SAriel Levkovich 			     &dm->dev_addr, sizeof(dm->dev_addr));
245625c13324SAriel Levkovich 	if (err)
2457c9b9dcb4SAriel Levkovich 		mlx5_dm_sw_icm_dealloc(dev, type, dm->size,
2458c9b9dcb4SAriel Levkovich 				       to_mucontext(ctx)->devx_uid, dm->dev_addr,
2459c9b9dcb4SAriel Levkovich 				       dm->icm_dm.obj_id);
246025c13324SAriel Levkovich 
246125c13324SAriel Levkovich 	return err;
246225c13324SAriel Levkovich }
246325c13324SAriel Levkovich 
24643b113a1eSAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
24653b113a1eSAriel Levkovich 			       struct ib_ucontext *context,
24663b113a1eSAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
24673b113a1eSAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
24683b113a1eSAriel Levkovich {
24693b113a1eSAriel Levkovich 	struct mlx5_ib_dm *dm;
24703b113a1eSAriel Levkovich 	enum mlx5_ib_uapi_dm_type type;
24713b113a1eSAriel Levkovich 	int err;
24723b113a1eSAriel Levkovich 
24733b113a1eSAriel Levkovich 	err = uverbs_get_const_default(&type, attrs,
24743b113a1eSAriel Levkovich 				       MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
24753b113a1eSAriel Levkovich 				       MLX5_IB_UAPI_DM_TYPE_MEMIC);
24763b113a1eSAriel Levkovich 	if (err)
24773b113a1eSAriel Levkovich 		return ERR_PTR(err);
24783b113a1eSAriel Levkovich 
24793b113a1eSAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_dm req: dm_type=%d user_length=0x%llx log_alignment=%d\n",
24803b113a1eSAriel Levkovich 		    type, attr->length, attr->alignment);
24813b113a1eSAriel Levkovich 
248225c13324SAriel Levkovich 	err = check_dm_type_support(to_mdev(ibdev), type);
248325c13324SAriel Levkovich 	if (err)
248425c13324SAriel Levkovich 		return ERR_PTR(err);
248525c13324SAriel Levkovich 
24863b113a1eSAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
24873b113a1eSAriel Levkovich 	if (!dm)
24883b113a1eSAriel Levkovich 		return ERR_PTR(-ENOMEM);
24893b113a1eSAriel Levkovich 
24903b113a1eSAriel Levkovich 	dm->type = type;
24913b113a1eSAriel Levkovich 
24923b113a1eSAriel Levkovich 	switch (type) {
24933b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
24943b113a1eSAriel Levkovich 		err = handle_alloc_dm_memic(context, dm,
24953b113a1eSAriel Levkovich 					    attr,
24963b113a1eSAriel Levkovich 					    attrs);
24973b113a1eSAriel Levkovich 		break;
249825c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2499c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2500c9b9dcb4SAriel Levkovich 					     attr, attrs,
2501c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_STEERING);
2502c9b9dcb4SAriel Levkovich 		break;
250325c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2504c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2505c9b9dcb4SAriel Levkovich 					     attr, attrs,
2506c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_HEADER_MODIFY);
250725c13324SAriel Levkovich 		break;
25083b113a1eSAriel Levkovich 	default:
25093b113a1eSAriel Levkovich 		err = -EOPNOTSUPP;
25103b113a1eSAriel Levkovich 	}
25113b113a1eSAriel Levkovich 
25123b113a1eSAriel Levkovich 	if (err)
25133b113a1eSAriel Levkovich 		goto err_free;
251424da0016SAriel Levkovich 
251524da0016SAriel Levkovich 	return &dm->ibdm;
251624da0016SAriel Levkovich 
251724da0016SAriel Levkovich err_free:
251824da0016SAriel Levkovich 	kfree(dm);
251924da0016SAriel Levkovich 	return ERR_PTR(err);
252024da0016SAriel Levkovich }
252124da0016SAriel Levkovich 
2522c4367a26SShamir Rabinovitch int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
252324da0016SAriel Levkovich {
252425c13324SAriel Levkovich 	struct mlx5_ib_ucontext *ctx = rdma_udata_to_drv_context(
252525c13324SAriel Levkovich 		&attrs->driver_udata, struct mlx5_ib_ucontext, ibucontext);
2526c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ibdm->device)->mdev;
252724da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
252824da0016SAriel Levkovich 	int ret;
252924da0016SAriel Levkovich 
25303b113a1eSAriel Levkovich 	switch (dm->type) {
25313b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
2532dc2316ebSYishai Hadas 		rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
2533dc2316ebSYishai Hadas 		return 0;
253425c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2535c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_STEERING,
2536c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
2537c9b9dcb4SAriel Levkovich 					     dm->icm_dm.obj_id);
2538c9b9dcb4SAriel Levkovich 		if (ret)
2539c9b9dcb4SAriel Levkovich 			return ret;
2540c9b9dcb4SAriel Levkovich 		break;
254125c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2542c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_HEADER_MODIFY,
2543c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
254425c13324SAriel Levkovich 					     dm->icm_dm.obj_id);
254525c13324SAriel Levkovich 		if (ret)
254625c13324SAriel Levkovich 			return ret;
25473b113a1eSAriel Levkovich 		break;
25483b113a1eSAriel Levkovich 	default:
25493b113a1eSAriel Levkovich 		return -EOPNOTSUPP;
25503b113a1eSAriel Levkovich 	}
255124da0016SAriel Levkovich 
255224da0016SAriel Levkovich 	kfree(dm);
255324da0016SAriel Levkovich 
255424da0016SAriel Levkovich 	return 0;
255524da0016SAriel Levkovich }
255624da0016SAriel Levkovich 
2557ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2558e126ba97SEli Cohen {
255921a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
256021a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2561e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2562e126ba97SEli Cohen 	int err;
2563a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2564a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)]   = {};
2565a1069c1cSYishai Hadas 	u16 uid = 0;
2566ff23dfa1SShamir Rabinovitch 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2567ff23dfa1SShamir Rabinovitch 		udata, struct mlx5_ib_ucontext, ibucontext);
2568e126ba97SEli Cohen 
2569ff23dfa1SShamir Rabinovitch 	uid = context ? context->devx_uid : 0;
2570a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2571a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
2572a1069c1cSYishai Hadas 	err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2573a1069c1cSYishai Hadas 			    out, sizeof(out));
257421a428a0SLeon Romanovsky 	if (err)
257521a428a0SLeon Romanovsky 		return err;
2576e126ba97SEli Cohen 
2577a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2578a1069c1cSYishai Hadas 	pd->uid = uid;
2579ff23dfa1SShamir Rabinovitch 	if (udata) {
2580e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2581e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2582a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
258321a428a0SLeon Romanovsky 			return -EFAULT;
2584e126ba97SEli Cohen 		}
2585e126ba97SEli Cohen 	}
2586e126ba97SEli Cohen 
258721a428a0SLeon Romanovsky 	return 0;
2588e126ba97SEli Cohen }
2589e126ba97SEli Cohen 
2590c4367a26SShamir Rabinovitch static void mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2591e126ba97SEli Cohen {
2592e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2593e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2594e126ba97SEli Cohen 
2595a1069c1cSYishai Hadas 	mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2596e126ba97SEli Cohen }
2597e126ba97SEli Cohen 
2598466fa6d2SMaor Gottlieb enum {
2599466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2600466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
260171c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_INNER_BIT,
260271c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_MISC2_BIT
2603466fa6d2SMaor Gottlieb };
2604038d2ef8SMaor Gottlieb 
2605466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2606466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2607466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2608466fa6d2SMaor Gottlieb 
2609466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2610466fa6d2SMaor Gottlieb {
2611466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2612466fa6d2SMaor Gottlieb 
2613466fa6d2SMaor Gottlieb 	match_criteria_enable =
2614466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2615466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2616466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2617466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2618466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2619466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2620466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2621466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
262271c6e863SAriel Levkovich 	match_criteria_enable |=
262371c6e863SAriel Levkovich 		(!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
262471c6e863SAriel Levkovich 		MATCH_CRITERIA_ENABLE_MISC2_BIT;
2625466fa6d2SMaor Gottlieb 
2626466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2627038d2ef8SMaor Gottlieb }
2628038d2ef8SMaor Gottlieb 
26296113cc44SMaor Gottlieb static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2630ca0d4753SMaor Gottlieb {
26316113cc44SMaor Gottlieb 	u8 entry_mask;
26326113cc44SMaor Gottlieb 	u8 entry_val;
26336113cc44SMaor Gottlieb 	int err = 0;
26346113cc44SMaor Gottlieb 
26356113cc44SMaor Gottlieb 	if (!mask)
26366113cc44SMaor Gottlieb 		goto out;
26376113cc44SMaor Gottlieb 
26386113cc44SMaor Gottlieb 	entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
26396113cc44SMaor Gottlieb 			      ip_protocol);
26406113cc44SMaor Gottlieb 	entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
26416113cc44SMaor Gottlieb 			     ip_protocol);
26426113cc44SMaor Gottlieb 	if (!entry_mask) {
2643ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2644ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
26456113cc44SMaor Gottlieb 		goto out;
26466113cc44SMaor Gottlieb 	}
26476113cc44SMaor Gottlieb 	/* Don't override existing ip protocol */
26486113cc44SMaor Gottlieb 	if (mask != entry_mask || val != entry_val)
26496113cc44SMaor Gottlieb 		err = -EINVAL;
26506113cc44SMaor Gottlieb out:
26516113cc44SMaor Gottlieb 	return err;
2652ca0d4753SMaor Gottlieb }
2653ca0d4753SMaor Gottlieb 
265437da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
26552d1e697eSMoses Reuben 			   bool inner)
26562d1e697eSMoses Reuben {
26572d1e697eSMoses Reuben 	if (inner) {
26582d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26592d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
26602d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26612d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
26622d1e697eSMoses Reuben 	} else {
26632d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26642d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
26652d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26662d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
26672d1e697eSMoses Reuben 	}
26682d1e697eSMoses Reuben }
26692d1e697eSMoses Reuben 
2670ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2671ca0d4753SMaor Gottlieb {
2672ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2673ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2674ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2675ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2676ca0d4753SMaor Gottlieb }
2677ca0d4753SMaor Gottlieb 
267871c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
267971c6e863SAriel Levkovich {
268071c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
268171c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
268271c6e863SAriel Levkovich 		return -EOPNOTSUPP;
268371c6e863SAriel Levkovich 
268471c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
268571c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
268671c6e863SAriel Levkovich 		return -EOPNOTSUPP;
268771c6e863SAriel Levkovich 
268871c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
268971c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
269071c6e863SAriel Levkovich 		return -EOPNOTSUPP;
269171c6e863SAriel Levkovich 
269271c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
269371c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
269471c6e863SAriel Levkovich 		return -EOPNOTSUPP;
269571c6e863SAriel Levkovich 
269671c6e863SAriel Levkovich 	return 0;
269771c6e863SAriel Levkovich }
269871c6e863SAriel Levkovich 
2699c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2700c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2701ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2702466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2703c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2704ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
27052ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2706a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
27073b3233fbSRaed Salem #define LAST_COUNTERS_FIELD counters
2708c47ac6aeSMaor Gottlieb 
2709c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2710c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2711c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2712c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2713c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2714c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2715c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2716c47ac6aeSMaor Gottlieb 
27172ea26203SMark Bloch int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
27182ea26203SMark Bloch 			   bool is_egress,
2719802c2125SAviad Yehezkel 			   struct mlx5_flow_act *action)
2720802c2125SAviad Yehezkel {
2721802c2125SAviad Yehezkel 
2722802c2125SAviad Yehezkel 	switch (maction->ib_action.type) {
2723802c2125SAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
2724501f14e3SMark Bloch 		if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2725501f14e3SMark Bloch 				      MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2726501f14e3SMark Bloch 			return -EINVAL;
2727802c2125SAviad Yehezkel 		/* Currently only AES_GCM keymat is supported by the driver */
2728802c2125SAviad Yehezkel 		action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
27292ea26203SMark Bloch 		action->action |= is_egress ?
2730802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2731802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2732802c2125SAviad Yehezkel 		return 0;
2733b1085be3SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
2734b1085be3SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2735b1085be3SMark Bloch 		    MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
2736501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2737501f14e3SMark Bloch 				return -EINVAL;
2738b1085be3SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
27392b688ea5SMaor Gottlieb 			action->modify_hdr =
27402b688ea5SMaor Gottlieb 				maction->flow_action_raw.modify_hdr;
2741b1085be3SMark Bloch 			return 0;
2742b1085be3SMark Bloch 		}
274310a30896SMark Bloch 		if (maction->flow_action_raw.sub_type ==
274410a30896SMark Bloch 		    MLX5_IB_FLOW_ACTION_DECAP) {
2745501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2746501f14e3SMark Bloch 				return -EINVAL;
274710a30896SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
274810a30896SMark Bloch 			return 0;
274910a30896SMark Bloch 		}
2750e806f932SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2751e806f932SMark Bloch 		    MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
2752501f14e3SMark Bloch 			if (action->action &
2753501f14e3SMark Bloch 			    MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2754501f14e3SMark Bloch 				return -EINVAL;
2755e806f932SMark Bloch 			action->action |=
2756e806f932SMark Bloch 				MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
27572b688ea5SMaor Gottlieb 			action->pkt_reformat =
27582b688ea5SMaor Gottlieb 				maction->flow_action_raw.pkt_reformat;
2759e806f932SMark Bloch 			return 0;
2760e806f932SMark Bloch 		}
2761b1085be3SMark Bloch 		/* fall through */
2762802c2125SAviad Yehezkel 	default:
2763802c2125SAviad Yehezkel 		return -EOPNOTSUPP;
2764802c2125SAviad Yehezkel 	}
2765802c2125SAviad Yehezkel }
2766802c2125SAviad Yehezkel 
2767bb0ee7dcSJianbo Liu static int parse_flow_attr(struct mlx5_core_dev *mdev,
2768bb0ee7dcSJianbo Liu 			   struct mlx5_flow_spec *spec,
2769bb0ee7dcSJianbo Liu 			   const union ib_flow_spec *ib_spec,
2770802c2125SAviad Yehezkel 			   const struct ib_flow_attr *flow_attr,
277171c6e863SAriel Levkovich 			   struct mlx5_flow_act *action, u32 prev_type)
2772038d2ef8SMaor Gottlieb {
2773bb0ee7dcSJianbo Liu 	struct mlx5_flow_context *flow_context = &spec->flow_context;
2774bb0ee7dcSJianbo Liu 	u32 *match_c = spec->match_criteria;
2775bb0ee7dcSJianbo Liu 	u32 *match_v = spec->match_value;
2776466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2777466fa6d2SMaor Gottlieb 					   misc_parameters);
2778466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2779466fa6d2SMaor Gottlieb 					   misc_parameters);
278071c6e863SAriel Levkovich 	void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
278171c6e863SAriel Levkovich 					    misc_parameters_2);
278271c6e863SAriel Levkovich 	void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
278371c6e863SAriel Levkovich 					    misc_parameters_2);
27842d1e697eSMoses Reuben 	void *headers_c;
27852d1e697eSMoses Reuben 	void *headers_v;
278619cc7524SAriel Levkovich 	int match_ipv;
2787802c2125SAviad Yehezkel 	int ret;
2788466fa6d2SMaor Gottlieb 
27892d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
27902d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
27912d1e697eSMoses Reuben 					 inner_headers);
27922d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
27932d1e697eSMoses Reuben 					 inner_headers);
279419cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
279519cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
27962d1e697eSMoses Reuben 	} else {
27972d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
27982d1e697eSMoses Reuben 					 outer_headers);
27992d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
28002d1e697eSMoses Reuben 					 outer_headers);
280119cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
280219cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
28032d1e697eSMoses Reuben 	}
28042d1e697eSMoses Reuben 
28052d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2806038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2807c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
28081ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2809038d2ef8SMaor Gottlieb 
28102d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2811038d2ef8SMaor Gottlieb 					     dmac_47_16),
2812038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
28132d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2814038d2ef8SMaor Gottlieb 					     dmac_47_16),
2815038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2816038d2ef8SMaor Gottlieb 
28172d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2818ee3da804SMaor Gottlieb 					     smac_47_16),
2819ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
28202d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2821ee3da804SMaor Gottlieb 					     smac_47_16),
2822ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2823ee3da804SMaor Gottlieb 
2824038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
28252d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
282610543365SMohamad Haj Yahia 				 cvlan_tag, 1);
28272d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
282810543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2829038d2ef8SMaor Gottlieb 
28302d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2831038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
28322d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2833038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2834038d2ef8SMaor Gottlieb 
28352d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2836038d2ef8SMaor Gottlieb 				 first_cfi,
2837038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
28382d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2839038d2ef8SMaor Gottlieb 				 first_cfi,
2840038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2841038d2ef8SMaor Gottlieb 
28422d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2843038d2ef8SMaor Gottlieb 				 first_prio,
2844038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
28452d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2846038d2ef8SMaor Gottlieb 				 first_prio,
2847038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2848038d2ef8SMaor Gottlieb 		}
28492d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2850038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
28512d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2852038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2853038d2ef8SMaor Gottlieb 		break;
2854038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2855c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
28561ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2857038d2ef8SMaor Gottlieb 
285819cc7524SAriel Levkovich 		if (match_ipv) {
285919cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
286019cc7524SAriel Levkovich 				 ip_version, 0xf);
286119cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
28623346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV4_VERSION);
286319cc7524SAriel Levkovich 		} else {
28642d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2865038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
28662d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2867038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
286819cc7524SAriel Levkovich 		}
2869038d2ef8SMaor Gottlieb 
28702d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2871038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2872038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2873038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
28742d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2875038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2876038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2877038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
28782d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2879038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2880038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2881038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
28822d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2883038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2884038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2885038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2886ca0d4753SMaor Gottlieb 
28872d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2888ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2889ca0d4753SMaor Gottlieb 
28906113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
28916113cc44SMaor Gottlieb 			      ib_spec->ipv4.mask.proto,
28926113cc44SMaor Gottlieb 			      ib_spec->ipv4.val.proto))
28936113cc44SMaor Gottlieb 			return -EINVAL;
2894038d2ef8SMaor Gottlieb 		break;
2895026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2896c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
28971ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2898026bae0cSMaor Gottlieb 
289919cc7524SAriel Levkovich 		if (match_ipv) {
290019cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
290119cc7524SAriel Levkovich 				 ip_version, 0xf);
290219cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
29033346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV6_VERSION);
290419cc7524SAriel Levkovich 		} else {
29052d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2906026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
29072d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2908026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
290919cc7524SAriel Levkovich 		}
2910026bae0cSMaor Gottlieb 
29112d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2912026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2913026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2914026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
29152d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2916026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2917026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2918026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
29192d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2920026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2921026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2922026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
29232d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2924026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2925026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2926026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2927466fa6d2SMaor Gottlieb 
29282d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2929466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2930466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2931466fa6d2SMaor Gottlieb 
29326113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
2933466fa6d2SMaor Gottlieb 			      ib_spec->ipv6.mask.next_hdr,
29346113cc44SMaor Gottlieb 			      ib_spec->ipv6.val.next_hdr))
29356113cc44SMaor Gottlieb 			return -EINVAL;
2936466fa6d2SMaor Gottlieb 
29372d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
29382d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
29392d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
29402d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
2941802c2125SAviad Yehezkel 		break;
2942802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ESP:
2943802c2125SAviad Yehezkel 		if (ib_spec->esp.mask.seq)
2944802c2125SAviad Yehezkel 			return -EOPNOTSUPP;
29452d1e697eSMoses Reuben 
2946802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2947802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.mask.spi));
2948802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2949802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.val.spi));
2950026bae0cSMaor Gottlieb 		break;
2951038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2952c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2953c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
29541ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2955038d2ef8SMaor Gottlieb 
29566113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
29576113cc44SMaor Gottlieb 			return -EINVAL;
2958038d2ef8SMaor Gottlieb 
29592d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2960038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
29612d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2962038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2963038d2ef8SMaor Gottlieb 
29642d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2965038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
29662d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2967038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2968038d2ef8SMaor Gottlieb 		break;
2969038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2970c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2971c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
29721ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2973038d2ef8SMaor Gottlieb 
29746113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
29756113cc44SMaor Gottlieb 			return -EINVAL;
2976038d2ef8SMaor Gottlieb 
29772d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2978038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
29792d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2980038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2981038d2ef8SMaor Gottlieb 
29822d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2983038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
29842d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2985038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2986038d2ef8SMaor Gottlieb 		break;
2987da2f22aeSAriel Levkovich 	case IB_FLOW_SPEC_GRE:
2988da2f22aeSAriel Levkovich 		if (ib_spec->gre.mask.c_ks_res0_ver)
2989da2f22aeSAriel Levkovich 			return -EOPNOTSUPP;
2990da2f22aeSAriel Levkovich 
29916113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
29926113cc44SMaor Gottlieb 			return -EINVAL;
29936113cc44SMaor Gottlieb 
2994da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2995da2f22aeSAriel Levkovich 			 0xff);
2996da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2997da2f22aeSAriel Levkovich 			 IPPROTO_GRE);
2998da2f22aeSAriel Levkovich 
2999da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
3000a93b632cSMaor Gottlieb 			 ntohs(ib_spec->gre.mask.protocol));
3001da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
3002da2f22aeSAriel Levkovich 			 ntohs(ib_spec->gre.val.protocol));
3003da2f22aeSAriel Levkovich 
3004da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
30055886a96aSOz Shlomo 				    gre_key.nvgre.hi),
3006da2f22aeSAriel Levkovich 		       &ib_spec->gre.mask.key,
3007da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.mask.key));
3008da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
30095886a96aSOz Shlomo 				    gre_key.nvgre.hi),
3010da2f22aeSAriel Levkovich 		       &ib_spec->gre.val.key,
3011da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.val.key));
3012da2f22aeSAriel Levkovich 		break;
301371c6e863SAriel Levkovich 	case IB_FLOW_SPEC_MPLS:
301471c6e863SAriel Levkovich 		switch (prev_type) {
301571c6e863SAriel Levkovich 		case IB_FLOW_SPEC_UDP:
301671c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
301771c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_udp),
301871c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
301971c6e863SAriel Levkovich 				return -EOPNOTSUPP;
302071c6e863SAriel Levkovich 
302171c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
302271c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
302371c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
302471c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
302571c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
302671c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
302771c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
302871c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
302971c6e863SAriel Levkovich 			break;
303071c6e863SAriel Levkovich 		case IB_FLOW_SPEC_GRE:
303171c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
303271c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_gre),
303371c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
303471c6e863SAriel Levkovich 				return -EOPNOTSUPP;
303571c6e863SAriel Levkovich 
303671c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
303771c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
303871c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
303971c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
304071c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
304171c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
304271c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
304371c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
304471c6e863SAriel Levkovich 			break;
304571c6e863SAriel Levkovich 		default:
304671c6e863SAriel Levkovich 			if (ib_spec->type & IB_FLOW_SPEC_INNER) {
304771c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
304871c6e863SAriel Levkovich 							   ft_field_support.inner_first_mpls),
304971c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
305071c6e863SAriel Levkovich 					return -EOPNOTSUPP;
305171c6e863SAriel Levkovich 
305271c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
305371c6e863SAriel Levkovich 						    inner_first_mpls),
305471c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
305571c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
305671c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
305771c6e863SAriel Levkovich 						    inner_first_mpls),
305871c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
305971c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
306071c6e863SAriel Levkovich 			} else {
306171c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
306271c6e863SAriel Levkovich 							   ft_field_support.outer_first_mpls),
306371c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
306471c6e863SAriel Levkovich 					return -EOPNOTSUPP;
306571c6e863SAriel Levkovich 
306671c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
306771c6e863SAriel Levkovich 						    outer_first_mpls),
306871c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
306971c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
307071c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
307171c6e863SAriel Levkovich 						    outer_first_mpls),
307271c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
307371c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
307471c6e863SAriel Levkovich 			}
307571c6e863SAriel Levkovich 		}
307671c6e863SAriel Levkovich 		break;
3077ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
3078ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
3079ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
30801ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
3081ffb30d8fSMoses Reuben 
3082ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
3083ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
3084ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
3085ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
3086ffb30d8fSMoses Reuben 		break;
30872ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
30882ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
30892ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
30902ac693f9SMoses Reuben 			return -EOPNOTSUPP;
30912ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
30922ac693f9SMoses Reuben 			return -EINVAL;
30932ac693f9SMoses Reuben 
3094bb0ee7dcSJianbo Liu 		flow_context->flow_tag = ib_spec->flow_tag.tag_id;
3095bb0ee7dcSJianbo Liu 		flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
30962ac693f9SMoses Reuben 		break;
3097a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
3098a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
3099a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
3100a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
3101075572d4SBoris Pismenny 		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
3102a22ed86cSSlava Shwartsman 		break;
3103802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ACTION_HANDLE:
31042ea26203SMark Bloch 		ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
31052ea26203SMark Bloch 			flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
3106802c2125SAviad Yehezkel 		if (ret)
3107802c2125SAviad Yehezkel 			return ret;
3108802c2125SAviad Yehezkel 		break;
31093b3233fbSRaed Salem 	case IB_FLOW_SPEC_ACTION_COUNT:
31103b3233fbSRaed Salem 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
31113b3233fbSRaed Salem 					 LAST_COUNTERS_FIELD))
31123b3233fbSRaed Salem 			return -EOPNOTSUPP;
31133b3233fbSRaed Salem 
31143b3233fbSRaed Salem 		/* for now support only one counters spec per flow */
31153b3233fbSRaed Salem 		if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
31163b3233fbSRaed Salem 			return -EINVAL;
31173b3233fbSRaed Salem 
31183b3233fbSRaed Salem 		action->counters = ib_spec->flow_count.counters;
31193b3233fbSRaed Salem 		action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
31203b3233fbSRaed Salem 		break;
3121038d2ef8SMaor Gottlieb 	default:
3122038d2ef8SMaor Gottlieb 		return -EINVAL;
3123038d2ef8SMaor Gottlieb 	}
3124038d2ef8SMaor Gottlieb 
3125038d2ef8SMaor Gottlieb 	return 0;
3126038d2ef8SMaor Gottlieb }
3127038d2ef8SMaor Gottlieb 
3128038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
3129038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
3130038d2ef8SMaor Gottlieb  * could steal other multicast packets.
3131038d2ef8SMaor Gottlieb  */
3132a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
3133038d2ef8SMaor Gottlieb {
313481e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
3135038d2ef8SMaor Gottlieb 
3136038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
3137038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
3138038d2ef8SMaor Gottlieb 		return false;
3139038d2ef8SMaor Gottlieb 
314081e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
314181e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
314281e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
3143038d2ef8SMaor Gottlieb 
314481e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
314581e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
314681e30880SYishai Hadas 			return true;
314781e30880SYishai Hadas 
314881e30880SYishai Hadas 		return false;
314981e30880SYishai Hadas 	}
315081e30880SYishai Hadas 
315181e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
315281e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
315381e30880SYishai Hadas 
315481e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
3155038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
3156038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
3157038d2ef8SMaor Gottlieb 	}
3158038d2ef8SMaor Gottlieb 
315981e30880SYishai Hadas 	return false;
316081e30880SYishai Hadas }
316181e30880SYishai Hadas 
3162802c2125SAviad Yehezkel enum valid_spec {
3163802c2125SAviad Yehezkel 	VALID_SPEC_INVALID,
3164802c2125SAviad Yehezkel 	VALID_SPEC_VALID,
3165802c2125SAviad Yehezkel 	VALID_SPEC_NA,
3166802c2125SAviad Yehezkel };
3167802c2125SAviad Yehezkel 
3168802c2125SAviad Yehezkel static enum valid_spec
3169802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
3170802c2125SAviad Yehezkel 		     const struct mlx5_flow_spec *spec,
3171802c2125SAviad Yehezkel 		     const struct mlx5_flow_act *flow_act,
3172802c2125SAviad Yehezkel 		     bool egress)
3173802c2125SAviad Yehezkel {
3174802c2125SAviad Yehezkel 	const u32 *match_c = spec->match_criteria;
3175802c2125SAviad Yehezkel 	bool is_crypto =
3176802c2125SAviad Yehezkel 		(flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
3177802c2125SAviad Yehezkel 				     MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
3178802c2125SAviad Yehezkel 	bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
3179802c2125SAviad Yehezkel 	bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
3180802c2125SAviad Yehezkel 
3181802c2125SAviad Yehezkel 	/*
3182802c2125SAviad Yehezkel 	 * Currently only crypto is supported in egress, when regular egress
3183802c2125SAviad Yehezkel 	 * rules would be supported, always return VALID_SPEC_NA.
3184802c2125SAviad Yehezkel 	 */
3185802c2125SAviad Yehezkel 	if (!is_crypto)
318678dd0c43SMark Bloch 		return VALID_SPEC_NA;
3187802c2125SAviad Yehezkel 
3188802c2125SAviad Yehezkel 	return is_crypto && is_ipsec &&
3189bb0ee7dcSJianbo Liu 		(!egress || (!is_drop &&
3190bb0ee7dcSJianbo Liu 			     !(spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG))) ?
3191802c2125SAviad Yehezkel 		VALID_SPEC_VALID : VALID_SPEC_INVALID;
3192802c2125SAviad Yehezkel }
3193802c2125SAviad Yehezkel 
3194802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev,
3195802c2125SAviad Yehezkel 			  const struct mlx5_flow_spec *spec,
3196802c2125SAviad Yehezkel 			  const struct mlx5_flow_act *flow_act,
3197802c2125SAviad Yehezkel 			  bool egress)
3198802c2125SAviad Yehezkel {
3199802c2125SAviad Yehezkel 	/* We curretly only support ipsec egress flow */
3200802c2125SAviad Yehezkel 	return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
3201802c2125SAviad Yehezkel }
3202802c2125SAviad Yehezkel 
320319cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
320419cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
32050f750966SAriel Levkovich 			       bool check_inner)
3206038d2ef8SMaor Gottlieb {
3207038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
320819cc7524SAriel Levkovich 	int match_ipv = check_inner ?
320919cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
321019cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
321119cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
321219cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
32130f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
32140f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
32150f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
32160f750966SAriel Levkovich 	bool has_ethertype = false;
3217038d2ef8SMaor Gottlieb 	unsigned int spec_index;
32180f750966SAriel Levkovich 	bool mask_valid = true;
32190f750966SAriel Levkovich 	u16 eth_type = 0;
32200f750966SAriel Levkovich 	bool type_valid;
3221038d2ef8SMaor Gottlieb 
3222038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
3223038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
32240f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
3225038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
32260f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
32270f750966SAriel Levkovich 				      htons(0xffff));
32280f750966SAriel Levkovich 			has_ethertype = true;
32290f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
32300f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
32310f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
32320f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
3233038d2ef8SMaor Gottlieb 		}
3234038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
3235038d2ef8SMaor Gottlieb 	}
32360f750966SAriel Levkovich 
32370f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
32380f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
32390f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
32400f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
32410f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
32420f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
324319cc7524SAriel Levkovich 
324419cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
324519cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
324619cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
32470f750966SAriel Levkovich 	}
32480f750966SAriel Levkovich 
32490f750966SAriel Levkovich 	return type_valid;
32500f750966SAriel Levkovich }
32510f750966SAriel Levkovich 
325219cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
325319cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
32540f750966SAriel Levkovich {
325519cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
325619cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
3257038d2ef8SMaor Gottlieb }
3258038d2ef8SMaor Gottlieb 
3259038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
3260038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
3261038d2ef8SMaor Gottlieb {
3262038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
3263038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
3264038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
3265038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
3266038d2ef8SMaor Gottlieb 	}
3267038d2ef8SMaor Gottlieb }
3268038d2ef8SMaor Gottlieb 
32693b3233fbSRaed Salem static void counters_clear_description(struct ib_counters *counters)
32703b3233fbSRaed Salem {
32713b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
32723b3233fbSRaed Salem 
32733b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
32743b3233fbSRaed Salem 	kfree(mcounters->counters_data);
32753b3233fbSRaed Salem 	mcounters->counters_data = NULL;
32763b3233fbSRaed Salem 	mcounters->cntrs_max_index = 0;
32773b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
32783b3233fbSRaed Salem }
32793b3233fbSRaed Salem 
3280038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3281038d2ef8SMaor Gottlieb {
3282038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3283038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
3284038d2ef8SMaor Gottlieb 							  ibflow);
3285038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
3286d4be3f44SYishai Hadas 	struct mlx5_ib_dev *dev = handler->dev;
3287038d2ef8SMaor Gottlieb 
32889a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3289038d2ef8SMaor Gottlieb 
3290038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
329174491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
3292cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
3293038d2ef8SMaor Gottlieb 		list_del(&iter->list);
3294038d2ef8SMaor Gottlieb 		kfree(iter);
3295038d2ef8SMaor Gottlieb 	}
3296038d2ef8SMaor Gottlieb 
329774491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
32985497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
32993b3233fbSRaed Salem 	if (handler->ibcounters &&
33003b3233fbSRaed Salem 	    atomic_read(&handler->ibcounters->usecnt) == 1)
33013b3233fbSRaed Salem 		counters_clear_description(handler->ibcounters);
3302038d2ef8SMaor Gottlieb 
33033b3233fbSRaed Salem 	mutex_unlock(&dev->flow_db->lock);
3304d4be3f44SYishai Hadas 	if (handler->flow_matcher)
3305d4be3f44SYishai Hadas 		atomic_dec(&handler->flow_matcher->usecnt);
3306038d2ef8SMaor Gottlieb 	kfree(handler);
3307038d2ef8SMaor Gottlieb 
3308038d2ef8SMaor Gottlieb 	return 0;
3309038d2ef8SMaor Gottlieb }
3310038d2ef8SMaor Gottlieb 
331135d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
331235d19011SMaor Gottlieb {
331335d19011SMaor Gottlieb 	priority *= 2;
331435d19011SMaor Gottlieb 	if (!dont_trap)
331535d19011SMaor Gottlieb 		priority++;
331635d19011SMaor Gottlieb 	return priority;
331735d19011SMaor Gottlieb }
331835d19011SMaor Gottlieb 
3319cc0e5d42SMaor Gottlieb enum flow_table_type {
3320cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
3321cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
3322cc0e5d42SMaor Gottlieb };
3323cc0e5d42SMaor Gottlieb 
332400b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
332500b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
3326d4be3f44SYishai Hadas 
3327d4be3f44SYishai Hadas static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3328d4be3f44SYishai Hadas 					   struct mlx5_ib_flow_prio *prio,
3329d4be3f44SYishai Hadas 					   int priority,
33304adda112SMark Bloch 					   int num_entries, int num_groups,
33314adda112SMark Bloch 					   u32 flags)
3332d4be3f44SYishai Hadas {
333361dc7b01SPaul Blakey 	struct mlx5_flow_table_attr ft_attr = {};
3334d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft;
3335d4be3f44SYishai Hadas 
333661dc7b01SPaul Blakey 	ft_attr.prio = priority;
333761dc7b01SPaul Blakey 	ft_attr.max_fte = num_entries;
333861dc7b01SPaul Blakey 	ft_attr.flags = flags;
333961dc7b01SPaul Blakey 	ft_attr.autogroup.max_num_groups = num_groups;
334061dc7b01SPaul Blakey 	ft = mlx5_create_auto_grouped_flow_table(ns, &ft_attr);
3341d4be3f44SYishai Hadas 	if (IS_ERR(ft))
3342d4be3f44SYishai Hadas 		return ERR_CAST(ft);
3343d4be3f44SYishai Hadas 
3344d4be3f44SYishai Hadas 	prio->flow_table = ft;
3345d4be3f44SYishai Hadas 	prio->refcount = 0;
3346d4be3f44SYishai Hadas 	return prio;
3347d4be3f44SYishai Hadas }
3348d4be3f44SYishai Hadas 
3349038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
3350cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
3351cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
3352038d2ef8SMaor Gottlieb {
335335d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
3354038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
3355038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
3356038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
3357dac388efSMaor Gottlieb 	int max_table_size;
3358038d2ef8SMaor Gottlieb 	int num_entries;
3359038d2ef8SMaor Gottlieb 	int num_groups;
3360cecae747SMaor Gottlieb 	bool esw_encap;
33614adda112SMark Bloch 	u32 flags = 0;
3362038d2ef8SMaor Gottlieb 	int priority;
3363038d2ef8SMaor Gottlieb 
3364dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3365dac388efSMaor Gottlieb 						       log_max_ft_size));
3366cecae747SMaor Gottlieb 	esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
3367cecae747SMaor Gottlieb 		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
3368038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
336978dd0c43SMark Bloch 		enum mlx5_flow_namespace_type fn_type;
337078dd0c43SMark Bloch 
337178dd0c43SMark Bloch 		if (flow_is_multicast_only(flow_attr) &&
337235d19011SMaor Gottlieb 		    !dont_trap)
3373038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
3374038d2ef8SMaor Gottlieb 		else
337535d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
337635d19011SMaor Gottlieb 							dont_trap);
337778dd0c43SMark Bloch 		if (ft_type == MLX5_IB_FT_RX) {
337878dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
337978dd0c43SMark Bloch 			prio = &dev->flow_db->prios[priority];
3380cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
33814adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
33824adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3383cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
33845c2db53fSMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
33855c2db53fSMark Bloch 					reformat_l3_tunnel_to_l2))
33865c2db53fSMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
338778dd0c43SMark Bloch 		} else {
338878dd0c43SMark Bloch 			max_table_size =
338978dd0c43SMark Bloch 				BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
339078dd0c43SMark Bloch 							      log_max_ft_size));
339178dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
339278dd0c43SMark Bloch 			prio = &dev->flow_db->egress_prios[priority];
3393cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
33944adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
33954adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
339678dd0c43SMark Bloch 		}
339778dd0c43SMark Bloch 		ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
3398038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
3399038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
3400038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3401038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3402038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
3403038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
3404038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
3405038d2ef8SMaor Gottlieb 					 &num_entries,
3406038d2ef8SMaor Gottlieb 					 &num_groups);
34079a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
3408cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3409cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3410cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
3411cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
3412cc0e5d42SMaor Gottlieb 
3413cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3414cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3415cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3416cc0e5d42SMaor Gottlieb 
34179a4ca38dSMark Bloch 		prio = &dev->flow_db->sniffer[ft_type];
3418cc0e5d42SMaor Gottlieb 		priority = 0;
3419cc0e5d42SMaor Gottlieb 		num_entries = 1;
3420cc0e5d42SMaor Gottlieb 		num_groups = 1;
3421038d2ef8SMaor Gottlieb 	}
3422038d2ef8SMaor Gottlieb 
3423038d2ef8SMaor Gottlieb 	if (!ns)
3424038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
3425038d2ef8SMaor Gottlieb 
34263b70508aSMark Bloch 	max_table_size = min_t(int, num_entries, max_table_size);
3427dac388efSMaor Gottlieb 
3428038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
3429d4be3f44SYishai Hadas 	if (!ft)
34303b70508aSMark Bloch 		return _get_prio(ns, prio, priority, max_table_size, num_groups,
34314adda112SMark Bloch 				 flags);
3432038d2ef8SMaor Gottlieb 
3433d4be3f44SYishai Hadas 	return prio;
3434038d2ef8SMaor Gottlieb }
3435038d2ef8SMaor Gottlieb 
3436a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
3437a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
3438a550ddfcSYishai Hadas 			    u32 underlay_qpn)
3439a550ddfcSYishai Hadas {
3440a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3441a550ddfcSYishai Hadas 					   spec->match_criteria,
3442a550ddfcSYishai Hadas 					   misc_parameters);
3443a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3444a550ddfcSYishai Hadas 					   misc_parameters);
3445a550ddfcSYishai Hadas 
3446a550ddfcSYishai Hadas 	if (underlay_qpn &&
3447a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3448a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
3449a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3450a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
3451a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3452a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
3453a550ddfcSYishai Hadas 	}
3454a550ddfcSYishai Hadas }
3455a550ddfcSYishai Hadas 
34565e95af5fSRaed Salem static int read_flow_counters(struct ib_device *ibdev,
34575e95af5fSRaed Salem 			      struct mlx5_read_counters_attr *read_attr)
34585e95af5fSRaed Salem {
34595e95af5fSRaed Salem 	struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
34605e95af5fSRaed Salem 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
34615e95af5fSRaed Salem 
34625e95af5fSRaed Salem 	return mlx5_fc_query(dev->mdev, fc,
34635e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_PACKETS],
34645e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_BYTES]);
34655e95af5fSRaed Salem }
34665e95af5fSRaed Salem 
34675e95af5fSRaed Salem /* flow counters currently expose two counters packets and bytes */
34685e95af5fSRaed Salem #define FLOW_COUNTERS_NUM 2
34693b3233fbSRaed Salem static int counters_set_description(struct ib_counters *counters,
34703b3233fbSRaed Salem 				    enum mlx5_ib_counters_type counters_type,
34713b3233fbSRaed Salem 				    struct mlx5_ib_flow_counters_desc *desc_data,
34723b3233fbSRaed Salem 				    u32 ncounters)
34733b3233fbSRaed Salem {
34743b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
34753b3233fbSRaed Salem 	u32 cntrs_max_index = 0;
34763b3233fbSRaed Salem 	int i;
34773b3233fbSRaed Salem 
34783b3233fbSRaed Salem 	if (counters_type != MLX5_IB_COUNTERS_FLOW)
34793b3233fbSRaed Salem 		return -EINVAL;
34803b3233fbSRaed Salem 
34813b3233fbSRaed Salem 	/* init the fields for the object */
34823b3233fbSRaed Salem 	mcounters->type = counters_type;
34835e95af5fSRaed Salem 	mcounters->read_counters = read_flow_counters;
34845e95af5fSRaed Salem 	mcounters->counters_num = FLOW_COUNTERS_NUM;
34853b3233fbSRaed Salem 	mcounters->ncounters = ncounters;
34863b3233fbSRaed Salem 	/* each counter entry have both description and index pair */
34873b3233fbSRaed Salem 	for (i = 0; i < ncounters; i++) {
34883b3233fbSRaed Salem 		if (desc_data[i].description > IB_COUNTER_BYTES)
34893b3233fbSRaed Salem 			return -EINVAL;
34903b3233fbSRaed Salem 
34913b3233fbSRaed Salem 		if (cntrs_max_index <= desc_data[i].index)
34923b3233fbSRaed Salem 			cntrs_max_index = desc_data[i].index + 1;
34933b3233fbSRaed Salem 	}
34943b3233fbSRaed Salem 
34953b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
34963b3233fbSRaed Salem 	mcounters->counters_data = desc_data;
34973b3233fbSRaed Salem 	mcounters->cntrs_max_index = cntrs_max_index;
34983b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
34993b3233fbSRaed Salem 
35003b3233fbSRaed Salem 	return 0;
35013b3233fbSRaed Salem }
35023b3233fbSRaed Salem 
35033b3233fbSRaed Salem #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
35043b3233fbSRaed Salem static int flow_counters_set_data(struct ib_counters *ibcounters,
35053b3233fbSRaed Salem 				  struct mlx5_ib_create_flow *ucmd)
35063b3233fbSRaed Salem {
35073b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
35083b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
35093b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc_data = NULL;
35103b3233fbSRaed Salem 	bool hw_hndl = false;
35113b3233fbSRaed Salem 	int ret = 0;
35123b3233fbSRaed Salem 
35133b3233fbSRaed Salem 	if (ucmd && ucmd->ncounters_data != 0) {
35143b3233fbSRaed Salem 		cntrs_data = ucmd->data;
35153b3233fbSRaed Salem 		if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
35163b3233fbSRaed Salem 			return -EINVAL;
35173b3233fbSRaed Salem 
35183b3233fbSRaed Salem 		desc_data = kcalloc(cntrs_data->ncounters,
35193b3233fbSRaed Salem 				    sizeof(*desc_data),
35203b3233fbSRaed Salem 				    GFP_KERNEL);
35213b3233fbSRaed Salem 		if (!desc_data)
35223b3233fbSRaed Salem 			return  -ENOMEM;
35233b3233fbSRaed Salem 
35243b3233fbSRaed Salem 		if (copy_from_user(desc_data,
35253b3233fbSRaed Salem 				   u64_to_user_ptr(cntrs_data->counters_data),
35263b3233fbSRaed Salem 				   sizeof(*desc_data) * cntrs_data->ncounters)) {
35273b3233fbSRaed Salem 			ret = -EFAULT;
35283b3233fbSRaed Salem 			goto free;
35293b3233fbSRaed Salem 		}
35303b3233fbSRaed Salem 	}
35313b3233fbSRaed Salem 
35323b3233fbSRaed Salem 	if (!mcounters->hw_cntrs_hndl) {
35333b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = mlx5_fc_create(
35343b3233fbSRaed Salem 			to_mdev(ibcounters->device)->mdev, false);
3535e31abf76Sweiyongjun (A) 		if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3536e31abf76Sweiyongjun (A) 			ret = PTR_ERR(mcounters->hw_cntrs_hndl);
35373b3233fbSRaed Salem 			goto free;
35383b3233fbSRaed Salem 		}
35393b3233fbSRaed Salem 		hw_hndl = true;
35403b3233fbSRaed Salem 	}
35413b3233fbSRaed Salem 
35423b3233fbSRaed Salem 	if (desc_data) {
35433b3233fbSRaed Salem 		/* counters already bound to at least one flow */
35443b3233fbSRaed Salem 		if (mcounters->cntrs_max_index) {
35453b3233fbSRaed Salem 			ret = -EINVAL;
35463b3233fbSRaed Salem 			goto free_hndl;
35473b3233fbSRaed Salem 		}
35483b3233fbSRaed Salem 
35493b3233fbSRaed Salem 		ret = counters_set_description(ibcounters,
35503b3233fbSRaed Salem 					       MLX5_IB_COUNTERS_FLOW,
35513b3233fbSRaed Salem 					       desc_data,
35523b3233fbSRaed Salem 					       cntrs_data->ncounters);
35533b3233fbSRaed Salem 		if (ret)
35543b3233fbSRaed Salem 			goto free_hndl;
35553b3233fbSRaed Salem 
35563b3233fbSRaed Salem 	} else if (!mcounters->cntrs_max_index) {
35573b3233fbSRaed Salem 		/* counters not bound yet, must have udata passed */
35583b3233fbSRaed Salem 		ret = -EINVAL;
35593b3233fbSRaed Salem 		goto free_hndl;
35603b3233fbSRaed Salem 	}
35613b3233fbSRaed Salem 
35623b3233fbSRaed Salem 	return 0;
35633b3233fbSRaed Salem 
35643b3233fbSRaed Salem free_hndl:
35653b3233fbSRaed Salem 	if (hw_hndl) {
35663b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
35673b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
35683b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = NULL;
35693b3233fbSRaed Salem 	}
35703b3233fbSRaed Salem free:
35713b3233fbSRaed Salem 	kfree(desc_data);
35723b3233fbSRaed Salem 	return ret;
35733b3233fbSRaed Salem }
35743b3233fbSRaed Salem 
3575669ff1e3SJianbo Liu static void mlx5_ib_set_rule_source_port(struct mlx5_ib_dev *dev,
3576669ff1e3SJianbo Liu 					 struct mlx5_flow_spec *spec,
3577669ff1e3SJianbo Liu 					 struct mlx5_eswitch_rep *rep)
3578669ff1e3SJianbo Liu {
3579669ff1e3SJianbo Liu 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
3580669ff1e3SJianbo Liu 	void *misc;
3581669ff1e3SJianbo Liu 
3582669ff1e3SJianbo Liu 	if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
3583669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3584669ff1e3SJianbo Liu 				    misc_parameters_2);
3585669ff1e3SJianbo Liu 
3586669ff1e3SJianbo Liu 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
3587669ff1e3SJianbo Liu 			 mlx5_eswitch_get_vport_metadata_for_match(esw,
3588669ff1e3SJianbo Liu 								   rep->vport));
3589669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3590669ff1e3SJianbo Liu 				    misc_parameters_2);
3591669ff1e3SJianbo Liu 
35920f0d3827SPaul Blakey 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
35930f0d3827SPaul Blakey 			 mlx5_eswitch_get_vport_metadata_mask());
3594669ff1e3SJianbo Liu 	} else {
3595669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3596669ff1e3SJianbo Liu 				    misc_parameters);
3597669ff1e3SJianbo Liu 
3598669ff1e3SJianbo Liu 		MLX5_SET(fte_match_set_misc, misc, source_port, rep->vport);
3599669ff1e3SJianbo Liu 
3600669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3601669ff1e3SJianbo Liu 				    misc_parameters);
3602669ff1e3SJianbo Liu 
3603669ff1e3SJianbo Liu 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3604669ff1e3SJianbo Liu 	}
3605669ff1e3SJianbo Liu }
3606669ff1e3SJianbo Liu 
3607a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3608038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
3609dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
3610a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
36113b3233fbSRaed Salem 						      u32 underlay_qpn,
36123b3233fbSRaed Salem 						      struct mlx5_ib_create_flow *ucmd)
3613038d2ef8SMaor Gottlieb {
3614038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
3615038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
3616bb0ee7dcSJianbo Liu 	struct mlx5_flow_act flow_act = {};
3617c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
36183b3233fbSRaed Salem 	struct mlx5_flow_destination dest_arr[2] = {};
36193b3233fbSRaed Salem 	struct mlx5_flow_destination *rule_dst = dest_arr;
3620dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3621038d2ef8SMaor Gottlieb 	unsigned int spec_index;
362271c6e863SAriel Levkovich 	u32 prev_type = 0;
3623038d2ef8SMaor Gottlieb 	int err = 0;
36243b3233fbSRaed Salem 	int dest_num = 0;
3625802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3626038d2ef8SMaor Gottlieb 
362719cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
3628038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3629038d2ef8SMaor Gottlieb 
36306a4d00beSMark Bloch 	if (dev->is_rep && is_egress)
363178dd0c43SMark Bloch 		return ERR_PTR(-EINVAL);
363278dd0c43SMark Bloch 
36331b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3634038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3635c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
3636038d2ef8SMaor Gottlieb 		err = -ENOMEM;
3637038d2ef8SMaor Gottlieb 		goto free;
3638038d2ef8SMaor Gottlieb 	}
3639038d2ef8SMaor Gottlieb 
3640038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
3641038d2ef8SMaor Gottlieb 
3642038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
3643bb0ee7dcSJianbo Liu 		err = parse_flow_attr(dev->mdev, spec,
364471c6e863SAriel Levkovich 				      ib_flow, flow_attr, &flow_act,
364571c6e863SAriel Levkovich 				      prev_type);
3646038d2ef8SMaor Gottlieb 		if (err < 0)
3647038d2ef8SMaor Gottlieb 			goto free;
3648038d2ef8SMaor Gottlieb 
364971c6e863SAriel Levkovich 		prev_type = ((union ib_flow_spec *)ib_flow)->type;
3650038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3651038d2ef8SMaor Gottlieb 	}
3652038d2ef8SMaor Gottlieb 
3653ed9085feSMaor Gottlieb 	if (dst && !(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP)) {
3654ed9085feSMaor Gottlieb 		memcpy(&dest_arr[0], dst, sizeof(*dst));
3655ed9085feSMaor Gottlieb 		dest_num++;
3656ed9085feSMaor Gottlieb 	}
3657ed9085feSMaor Gottlieb 
3658a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
3659a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
3660a550ddfcSYishai Hadas 
36616a4d00beSMark Bloch 	if (dev->is_rep) {
3662669ff1e3SJianbo Liu 		struct mlx5_eswitch_rep *rep;
3663018a94eeSMark Bloch 
3664669ff1e3SJianbo Liu 		rep = dev->port[flow_attr->port - 1].rep;
3665669ff1e3SJianbo Liu 		if (!rep) {
36666a4d00beSMark Bloch 			err = -EINVAL;
36676a4d00beSMark Bloch 			goto free;
36686a4d00beSMark Bloch 		}
3669669ff1e3SJianbo Liu 
3670669ff1e3SJianbo Liu 		mlx5_ib_set_rule_source_port(dev, spec, rep);
3671018a94eeSMark Bloch 	}
3672018a94eeSMark Bloch 
3673466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3674802c2125SAviad Yehezkel 
3675802c2125SAviad Yehezkel 	if (is_egress &&
3676802c2125SAviad Yehezkel 	    !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3677802c2125SAviad Yehezkel 		err = -EINVAL;
3678802c2125SAviad Yehezkel 		goto free;
3679802c2125SAviad Yehezkel 	}
3680802c2125SAviad Yehezkel 
36813b3233fbSRaed Salem 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3682171c7625SMark Bloch 		struct mlx5_ib_mcounters *mcounters;
3683171c7625SMark Bloch 
36843b3233fbSRaed Salem 		err = flow_counters_set_data(flow_act.counters, ucmd);
36853b3233fbSRaed Salem 		if (err)
36863b3233fbSRaed Salem 			goto free;
36873b3233fbSRaed Salem 
3688171c7625SMark Bloch 		mcounters = to_mcounters(flow_act.counters);
36893b3233fbSRaed Salem 		handler->ibcounters = flow_act.counters;
36903b3233fbSRaed Salem 		dest_arr[dest_num].type =
36913b3233fbSRaed Salem 			MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3692171c7625SMark Bloch 		dest_arr[dest_num].counter_id =
3693171c7625SMark Bloch 			mlx5_fc_id(mcounters->hw_cntrs_hndl);
36943b3233fbSRaed Salem 		dest_num++;
36953b3233fbSRaed Salem 	}
36963b3233fbSRaed Salem 
3697075572d4SBoris Pismenny 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
3698ed9085feSMaor Gottlieb 		if (!dest_num)
3699a22ed86cSSlava Shwartsman 			rule_dst = NULL;
3700a22ed86cSSlava Shwartsman 	} else {
3701802c2125SAviad Yehezkel 		if (is_egress)
3702802c2125SAviad Yehezkel 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3703802c2125SAviad Yehezkel 		else
3704802c2125SAviad Yehezkel 			flow_act.action |=
37053b3233fbSRaed Salem 				dest_num ?  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
370635d19011SMaor Gottlieb 					MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3707a22ed86cSSlava Shwartsman 	}
37082ac693f9SMoses Reuben 
3709bb0ee7dcSJianbo Liu 	if ((spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG)  &&
37102ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
37112ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
37122ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3713bb0ee7dcSJianbo Liu 			     spec->flow_context.flow_tag, flow_attr->type);
37142ac693f9SMoses Reuben 		err = -EINVAL;
37152ac693f9SMoses Reuben 		goto free;
37162ac693f9SMoses Reuben 	}
371774491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
371866958ed9SHadar Hen Zion 					    &flow_act,
3719a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
3720038d2ef8SMaor Gottlieb 
3721038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
3722038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
3723038d2ef8SMaor Gottlieb 		goto free;
3724038d2ef8SMaor Gottlieb 	}
3725038d2ef8SMaor Gottlieb 
3726d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
37275497adc6SMaor Gottlieb 	handler->prio = ft_prio;
3728d4be3f44SYishai Hadas 	handler->dev = dev;
3729038d2ef8SMaor Gottlieb 
3730038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
3731038d2ef8SMaor Gottlieb free:
37323b3233fbSRaed Salem 	if (err && handler) {
37333b3233fbSRaed Salem 		if (handler->ibcounters &&
37343b3233fbSRaed Salem 		    atomic_read(&handler->ibcounters->usecnt) == 1)
37353b3233fbSRaed Salem 			counters_clear_description(handler->ibcounters);
3736038d2ef8SMaor Gottlieb 		kfree(handler);
37373b3233fbSRaed Salem 	}
3738c5bb1730SMaor Gottlieb 	kvfree(spec);
3739038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
3740038d2ef8SMaor Gottlieb }
3741038d2ef8SMaor Gottlieb 
3742a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3743a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
3744a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
3745a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
3746a550ddfcSYishai Hadas {
37473b3233fbSRaed Salem 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
3748a550ddfcSYishai Hadas }
3749a550ddfcSYishai Hadas 
375035d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
375135d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
375235d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
375335d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
375435d19011SMaor Gottlieb {
375535d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
375635d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
375735d19011SMaor Gottlieb 
375835d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
375935d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
376035d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
376135d19011SMaor Gottlieb 					       flow_attr, dst);
376235d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
376374491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3764d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
376535d19011SMaor Gottlieb 			kfree(handler);
376635d19011SMaor Gottlieb 			handler = handler_dst;
376735d19011SMaor Gottlieb 		} else {
376835d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
376935d19011SMaor Gottlieb 		}
377035d19011SMaor Gottlieb 	}
377135d19011SMaor Gottlieb 
377235d19011SMaor Gottlieb 	return handler;
377335d19011SMaor Gottlieb }
3774038d2ef8SMaor Gottlieb enum {
3775038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
3776038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
3777038d2ef8SMaor Gottlieb };
3778038d2ef8SMaor Gottlieb 
3779038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3780038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
3781038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
3782038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
3783038d2ef8SMaor Gottlieb {
3784038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
3785038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3786038d2ef8SMaor Gottlieb 
3787038d2ef8SMaor Gottlieb 	static struct {
3788038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
3789038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
3790038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
3791038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
3792038d2ef8SMaor Gottlieb 			.flow_attr = {
3793038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3794038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3795038d2ef8SMaor Gottlieb 			},
3796038d2ef8SMaor Gottlieb 			.eth_flow = {
3797038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3798038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3799038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3800038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
3801038d2ef8SMaor Gottlieb 			}
3802038d2ef8SMaor Gottlieb 		},
3803038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
3804038d2ef8SMaor Gottlieb 			.flow_attr = {
3805038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3806038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3807038d2ef8SMaor Gottlieb 			},
3808038d2ef8SMaor Gottlieb 			.eth_flow = {
3809038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3810038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3811038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3812038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
3813038d2ef8SMaor Gottlieb 			}
3814038d2ef8SMaor Gottlieb 		}
3815038d2ef8SMaor Gottlieb 	};
3816038d2ef8SMaor Gottlieb 
3817038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
3818038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
3819038d2ef8SMaor Gottlieb 				   dst);
3820038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
3821038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3822038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
3823038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3824038d2ef8SMaor Gottlieb 						 dst);
3825038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
382674491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3827d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
3828038d2ef8SMaor Gottlieb 			kfree(handler);
3829038d2ef8SMaor Gottlieb 			handler = handler_ucast;
3830038d2ef8SMaor Gottlieb 		} else {
3831038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
3832038d2ef8SMaor Gottlieb 		}
3833038d2ef8SMaor Gottlieb 	}
3834038d2ef8SMaor Gottlieb 
3835038d2ef8SMaor Gottlieb 	return handler;
3836038d2ef8SMaor Gottlieb }
3837038d2ef8SMaor Gottlieb 
3838cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3839cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
3840cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
3841cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
3842cc0e5d42SMaor Gottlieb {
3843cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
3844cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
3845cc0e5d42SMaor Gottlieb 	int err;
3846cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
3847cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
3848cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
3849cc0e5d42SMaor Gottlieb 	};
3850cc0e5d42SMaor Gottlieb 
3851cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3852cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
3853cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
3854cc0e5d42SMaor Gottlieb 		goto err;
3855cc0e5d42SMaor Gottlieb 	}
3856cc0e5d42SMaor Gottlieb 
3857cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3858cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
3859cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
3860cc0e5d42SMaor Gottlieb 		goto err_tx;
3861cc0e5d42SMaor Gottlieb 	}
3862cc0e5d42SMaor Gottlieb 
3863cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
3864cc0e5d42SMaor Gottlieb 
3865cc0e5d42SMaor Gottlieb 	return handler_rx;
3866cc0e5d42SMaor Gottlieb 
3867cc0e5d42SMaor Gottlieb err_tx:
386874491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
3869cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
3870cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
3871cc0e5d42SMaor Gottlieb err:
3872cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
3873cc0e5d42SMaor Gottlieb }
3874cc0e5d42SMaor Gottlieb 
3875038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3876038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
387759082a32SMatan Barak 					   int domain,
387859082a32SMatan Barak 					   struct ib_udata *udata)
3879038d2ef8SMaor Gottlieb {
3880038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3881d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
3882038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3883038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
3884cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3885038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
3886802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
38873b3233fbSRaed Salem 	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
38883b3233fbSRaed Salem 	size_t min_ucmd_sz, required_ucmd_sz;
3889038d2ef8SMaor Gottlieb 	int err;
3890a550ddfcSYishai Hadas 	int underlay_qpn;
3891038d2ef8SMaor Gottlieb 
38923b3233fbSRaed Salem 	if (udata && udata->inlen) {
38933b3233fbSRaed Salem 		min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
38943b3233fbSRaed Salem 				sizeof(ucmd_hdr.reserved);
38953b3233fbSRaed Salem 		if (udata->inlen < min_ucmd_sz)
389659082a32SMatan Barak 			return ERR_PTR(-EOPNOTSUPP);
389759082a32SMatan Barak 
38983b3233fbSRaed Salem 		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
38993b3233fbSRaed Salem 		if (err)
39003b3233fbSRaed Salem 			return ERR_PTR(err);
39013b3233fbSRaed Salem 
39023b3233fbSRaed Salem 		/* currently supports only one counters data */
39033b3233fbSRaed Salem 		if (ucmd_hdr.ncounters_data > 1)
39043b3233fbSRaed Salem 			return ERR_PTR(-EINVAL);
39053b3233fbSRaed Salem 
39063b3233fbSRaed Salem 		required_ucmd_sz = min_ucmd_sz +
39073b3233fbSRaed Salem 			sizeof(struct mlx5_ib_flow_counters_data) *
39083b3233fbSRaed Salem 			ucmd_hdr.ncounters_data;
39093b3233fbSRaed Salem 		if (udata->inlen > required_ucmd_sz &&
39103b3233fbSRaed Salem 		    !ib_is_udata_cleared(udata, required_ucmd_sz,
39113b3233fbSRaed Salem 					 udata->inlen - required_ucmd_sz))
39123b3233fbSRaed Salem 			return ERR_PTR(-EOPNOTSUPP);
39133b3233fbSRaed Salem 
39143b3233fbSRaed Salem 		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
39153b3233fbSRaed Salem 		if (!ucmd)
39163b3233fbSRaed Salem 			return ERR_PTR(-ENOMEM);
39173b3233fbSRaed Salem 
39183b3233fbSRaed Salem 		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
3919299eafeeSGustavo A. R. Silva 		if (err)
3920299eafeeSGustavo A. R. Silva 			goto free_ucmd;
39213b3233fbSRaed Salem 	}
39223b3233fbSRaed Salem 
3923299eafeeSGustavo A. R. Silva 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3924299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3925299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3926299eafeeSGustavo A. R. Silva 	}
3927038d2ef8SMaor Gottlieb 
3928038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
3929508562d6SDaniel Jurgens 	    flow_attr->port > dev->num_ports ||
3930802c2125SAviad Yehezkel 	    (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3931299eafeeSGustavo A. R. Silva 				  IB_FLOW_ATTR_FLAGS_EGRESS))) {
3932299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3933299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3934299eafeeSGustavo A. R. Silva 	}
3935802c2125SAviad Yehezkel 
3936802c2125SAviad Yehezkel 	if (is_egress &&
3937802c2125SAviad Yehezkel 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3938299eafeeSGustavo A. R. Silva 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3939299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3940299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3941299eafeeSGustavo A. R. Silva 	}
3942038d2ef8SMaor Gottlieb 
3943038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3944299eafeeSGustavo A. R. Silva 	if (!dst) {
3945299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3946299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3947299eafeeSGustavo A. R. Silva 	}
3948038d2ef8SMaor Gottlieb 
39499a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3950038d2ef8SMaor Gottlieb 
3951802c2125SAviad Yehezkel 	ft_prio = get_flow_table(dev, flow_attr,
3952802c2125SAviad Yehezkel 				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3953038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
3954038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
3955038d2ef8SMaor Gottlieb 		goto unlock;
3956038d2ef8SMaor Gottlieb 	}
3957cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3958cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3959cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
3960cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
3961cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
3962cc0e5d42SMaor Gottlieb 			goto destroy_ft;
3963cc0e5d42SMaor Gottlieb 		}
3964cc0e5d42SMaor Gottlieb 	}
3965038d2ef8SMaor Gottlieb 
3966802c2125SAviad Yehezkel 	if (is_egress) {
3967802c2125SAviad Yehezkel 		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3968802c2125SAviad Yehezkel 	} else {
3969038d2ef8SMaor Gottlieb 		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3970d9f88e5aSYishai Hadas 		if (mqp->flags & MLX5_IB_QP_RSS)
3971d9f88e5aSYishai Hadas 			dst->tir_num = mqp->rss_qp.tirn;
3972d9f88e5aSYishai Hadas 		else
3973d9f88e5aSYishai Hadas 			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3974802c2125SAviad Yehezkel 	}
3975038d2ef8SMaor Gottlieb 
3976038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
397735d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
397835d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
397935d19011SMaor Gottlieb 							flow_attr, dst);
398035d19011SMaor Gottlieb 		} else {
3981a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3982a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
3983a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
39843b3233fbSRaed Salem 						    dst, underlay_qpn, ucmd);
398535d19011SMaor Gottlieb 		}
3986038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3987038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3988038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3989038d2ef8SMaor Gottlieb 						dst);
3990cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3991cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3992038d2ef8SMaor Gottlieb 	} else {
3993038d2ef8SMaor Gottlieb 		err = -EINVAL;
3994038d2ef8SMaor Gottlieb 		goto destroy_ft;
3995038d2ef8SMaor Gottlieb 	}
3996038d2ef8SMaor Gottlieb 
3997038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
3998038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
3999038d2ef8SMaor Gottlieb 		handler = NULL;
4000038d2ef8SMaor Gottlieb 		goto destroy_ft;
4001038d2ef8SMaor Gottlieb 	}
4002038d2ef8SMaor Gottlieb 
40039a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
4004038d2ef8SMaor Gottlieb 	kfree(dst);
40053b3233fbSRaed Salem 	kfree(ucmd);
4006038d2ef8SMaor Gottlieb 
4007038d2ef8SMaor Gottlieb 	return &handler->ibflow;
4008038d2ef8SMaor Gottlieb 
4009038d2ef8SMaor Gottlieb destroy_ft:
4010038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
4011cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
4012cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
4013038d2ef8SMaor Gottlieb unlock:
40149a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
4015038d2ef8SMaor Gottlieb 	kfree(dst);
4016299eafeeSGustavo A. R. Silva free_ucmd:
40173b3233fbSRaed Salem 	kfree(ucmd);
4018038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
4019038d2ef8SMaor Gottlieb }
4020038d2ef8SMaor Gottlieb 
4021b47fd4ffSMark Bloch static struct mlx5_ib_flow_prio *
4022b47fd4ffSMark Bloch _get_flow_table(struct mlx5_ib_dev *dev,
4023b47fd4ffSMark Bloch 		struct mlx5_ib_flow_matcher *fs_matcher,
4024b47fd4ffSMark Bloch 		bool mcast)
4025d4be3f44SYishai Hadas {
4026d4be3f44SYishai Hadas 	struct mlx5_flow_namespace *ns = NULL;
402713a43765SMark Bloch 	struct mlx5_ib_flow_prio *prio = NULL;
402813a43765SMark Bloch 	int max_table_size = 0;
4029cecae747SMaor Gottlieb 	bool esw_encap;
4030b47fd4ffSMark Bloch 	u32 flags = 0;
4031b47fd4ffSMark Bloch 	int priority;
4032d4be3f44SYishai Hadas 
403313a43765SMark Bloch 	if (mcast)
403413a43765SMark Bloch 		priority = MLX5_IB_FLOW_MCAST_PRIO;
403513a43765SMark Bloch 	else
403613a43765SMark Bloch 		priority = ib_prio_to_core_prio(fs_matcher->priority, false);
403713a43765SMark Bloch 
4038cecae747SMaor Gottlieb 	esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
4039cecae747SMaor Gottlieb 		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
4040b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
4041d4be3f44SYishai Hadas 		max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
4042d4be3f44SYishai Hadas 					log_max_ft_size));
4043cecae747SMaor Gottlieb 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap) && !esw_encap)
4044b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
4045b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
4046cecae747SMaor Gottlieb 					      reformat_l3_tunnel_to_l2) &&
4047cecae747SMaor Gottlieb 		    !esw_encap)
4048b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
404913a43765SMark Bloch 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS) {
405013a43765SMark Bloch 		max_table_size = BIT(
405113a43765SMark Bloch 			MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, log_max_ft_size));
4052cecae747SMaor Gottlieb 		if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat) && !esw_encap)
4053b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
405413a43765SMark Bloch 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB) {
405513a43765SMark Bloch 		max_table_size = BIT(
405613a43765SMark Bloch 			MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, log_max_ft_size));
405709d985beSMaor Gottlieb 		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, decap) && esw_encap)
405809d985beSMaor Gottlieb 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
405909d985beSMaor Gottlieb 		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, reformat_l3_tunnel_to_l2) &&
406009d985beSMaor Gottlieb 		    esw_encap)
406109d985beSMaor Gottlieb 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
406213a43765SMark Bloch 		priority = FDB_BYPASS_PATH;
4063d8abe884SMark Zhang 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX) {
4064d8abe884SMark Zhang 		max_table_size =
4065d8abe884SMark Zhang 			BIT(MLX5_CAP_FLOWTABLE_RDMA_RX(dev->mdev,
4066d8abe884SMark Zhang 						       log_max_ft_size));
4067d8abe884SMark Zhang 		priority = fs_matcher->priority;
4068af9c3841SMichael Guralnik 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_TX) {
4069af9c3841SMichael Guralnik 		max_table_size =
4070af9c3841SMichael Guralnik 			BIT(MLX5_CAP_FLOWTABLE_RDMA_TX(dev->mdev,
4071af9c3841SMichael Guralnik 						       log_max_ft_size));
4072af9c3841SMichael Guralnik 		priority = fs_matcher->priority;
4073b47fd4ffSMark Bloch 	}
4074b47fd4ffSMark Bloch 
40753b70508aSMark Bloch 	max_table_size = min_t(int, max_table_size, MLX5_FS_MAX_ENTRIES);
4076d4be3f44SYishai Hadas 
4077b47fd4ffSMark Bloch 	ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
4078d4be3f44SYishai Hadas 	if (!ns)
4079d4be3f44SYishai Hadas 		return ERR_PTR(-ENOTSUPP);
4080d4be3f44SYishai Hadas 
4081b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
4082d4be3f44SYishai Hadas 		prio = &dev->flow_db->prios[priority];
408313a43765SMark Bloch 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS)
4084b47fd4ffSMark Bloch 		prio = &dev->flow_db->egress_prios[priority];
408513a43765SMark Bloch 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB)
408613a43765SMark Bloch 		prio = &dev->flow_db->fdb;
4087d8abe884SMark Zhang 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX)
4088d8abe884SMark Zhang 		prio = &dev->flow_db->rdma_rx[priority];
4089af9c3841SMichael Guralnik 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_TX)
4090af9c3841SMichael Guralnik 		prio = &dev->flow_db->rdma_tx[priority];
409113a43765SMark Bloch 
409213a43765SMark Bloch 	if (!prio)
409313a43765SMark Bloch 		return ERR_PTR(-EINVAL);
4094d4be3f44SYishai Hadas 
4095d4be3f44SYishai Hadas 	if (prio->flow_table)
4096d4be3f44SYishai Hadas 		return prio;
4097d4be3f44SYishai Hadas 
40983b70508aSMark Bloch 	return _get_prio(ns, prio, priority, max_table_size,
4099b47fd4ffSMark Bloch 			 MLX5_FS_MAX_TYPES, flags);
4100d4be3f44SYishai Hadas }
4101d4be3f44SYishai Hadas 
4102d4be3f44SYishai Hadas static struct mlx5_ib_flow_handler *
4103d4be3f44SYishai Hadas _create_raw_flow_rule(struct mlx5_ib_dev *dev,
4104d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_prio *ft_prio,
4105d4be3f44SYishai Hadas 		      struct mlx5_flow_destination *dst,
4106d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_matcher  *fs_matcher,
4107bb0ee7dcSJianbo Liu 		      struct mlx5_flow_context *flow_context,
4108b823dd6dSMark Bloch 		      struct mlx5_flow_act *flow_act,
4109bfc5d839SMark Bloch 		      void *cmd_in, int inlen,
4110bfc5d839SMark Bloch 		      int dst_num)
4111d4be3f44SYishai Hadas {
4112d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
4113d4be3f44SYishai Hadas 	struct mlx5_flow_spec *spec;
4114d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft = ft_prio->flow_table;
4115d4be3f44SYishai Hadas 	int err = 0;
4116d4be3f44SYishai Hadas 
4117d4be3f44SYishai Hadas 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
4118d4be3f44SYishai Hadas 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
4119d4be3f44SYishai Hadas 	if (!handler || !spec) {
4120d4be3f44SYishai Hadas 		err = -ENOMEM;
4121d4be3f44SYishai Hadas 		goto free;
4122d4be3f44SYishai Hadas 	}
4123d4be3f44SYishai Hadas 
4124d4be3f44SYishai Hadas 	INIT_LIST_HEAD(&handler->list);
4125d4be3f44SYishai Hadas 
4126d4be3f44SYishai Hadas 	memcpy(spec->match_value, cmd_in, inlen);
4127d4be3f44SYishai Hadas 	memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
4128d4be3f44SYishai Hadas 	       fs_matcher->mask_len);
4129d4be3f44SYishai Hadas 	spec->match_criteria_enable = fs_matcher->match_criteria_enable;
4130bb0ee7dcSJianbo Liu 	spec->flow_context = *flow_context;
4131d4be3f44SYishai Hadas 
4132d4be3f44SYishai Hadas 	handler->rule = mlx5_add_flow_rules(ft, spec,
4133bfc5d839SMark Bloch 					    flow_act, dst, dst_num);
4134d4be3f44SYishai Hadas 
4135d4be3f44SYishai Hadas 	if (IS_ERR(handler->rule)) {
4136d4be3f44SYishai Hadas 		err = PTR_ERR(handler->rule);
4137d4be3f44SYishai Hadas 		goto free;
4138d4be3f44SYishai Hadas 	}
4139d4be3f44SYishai Hadas 
4140d4be3f44SYishai Hadas 	ft_prio->refcount++;
4141d4be3f44SYishai Hadas 	handler->prio = ft_prio;
4142d4be3f44SYishai Hadas 	handler->dev = dev;
4143d4be3f44SYishai Hadas 	ft_prio->flow_table = ft;
4144d4be3f44SYishai Hadas 
4145d4be3f44SYishai Hadas free:
4146d4be3f44SYishai Hadas 	if (err)
4147d4be3f44SYishai Hadas 		kfree(handler);
4148d4be3f44SYishai Hadas 	kvfree(spec);
4149d4be3f44SYishai Hadas 	return err ? ERR_PTR(err) : handler;
4150d4be3f44SYishai Hadas }
4151d4be3f44SYishai Hadas 
4152d4be3f44SYishai Hadas static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
4153d4be3f44SYishai Hadas 				void *match_v)
4154d4be3f44SYishai Hadas {
4155d4be3f44SYishai Hadas 	void *match_c;
4156d4be3f44SYishai Hadas 	void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
4157d4be3f44SYishai Hadas 	void *dmac, *dmac_mask;
4158d4be3f44SYishai Hadas 	void *ipv4, *ipv4_mask;
4159d4be3f44SYishai Hadas 
4160d4be3f44SYishai Hadas 	if (!(fs_matcher->match_criteria_enable &
4161d4be3f44SYishai Hadas 	      (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
4162d4be3f44SYishai Hadas 		return false;
4163d4be3f44SYishai Hadas 
4164d4be3f44SYishai Hadas 	match_c = fs_matcher->matcher_mask.match_params;
4165d4be3f44SYishai Hadas 	match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
4166d4be3f44SYishai Hadas 					   outer_headers);
4167d4be3f44SYishai Hadas 	match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
4168d4be3f44SYishai Hadas 					   outer_headers);
4169d4be3f44SYishai Hadas 
4170d4be3f44SYishai Hadas 	dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4171d4be3f44SYishai Hadas 			    dmac_47_16);
4172d4be3f44SYishai Hadas 	dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4173d4be3f44SYishai Hadas 				 dmac_47_16);
4174d4be3f44SYishai Hadas 
4175d4be3f44SYishai Hadas 	if (is_multicast_ether_addr(dmac) &&
4176d4be3f44SYishai Hadas 	    is_multicast_ether_addr(dmac_mask))
4177d4be3f44SYishai Hadas 		return true;
4178d4be3f44SYishai Hadas 
4179d4be3f44SYishai Hadas 	ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4180d4be3f44SYishai Hadas 			    dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4181d4be3f44SYishai Hadas 
4182d4be3f44SYishai Hadas 	ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4183d4be3f44SYishai Hadas 				 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4184d4be3f44SYishai Hadas 
4185d4be3f44SYishai Hadas 	if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
4186d4be3f44SYishai Hadas 	    ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
4187d4be3f44SYishai Hadas 		return true;
4188d4be3f44SYishai Hadas 
4189d4be3f44SYishai Hadas 	return false;
4190d4be3f44SYishai Hadas }
4191d4be3f44SYishai Hadas 
419232269441SYishai Hadas struct mlx5_ib_flow_handler *
419332269441SYishai Hadas mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
419432269441SYishai Hadas 			struct mlx5_ib_flow_matcher *fs_matcher,
4195bb0ee7dcSJianbo Liu 			struct mlx5_flow_context *flow_context,
4196b823dd6dSMark Bloch 			struct mlx5_flow_act *flow_act,
4197bfc5d839SMark Bloch 			u32 counter_id,
419832269441SYishai Hadas 			void *cmd_in, int inlen, int dest_id,
419932269441SYishai Hadas 			int dest_type)
420032269441SYishai Hadas {
4201d4be3f44SYishai Hadas 	struct mlx5_flow_destination *dst;
4202d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *ft_prio;
4203d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
4204bfc5d839SMark Bloch 	int dst_num = 0;
4205d4be3f44SYishai Hadas 	bool mcast;
4206d4be3f44SYishai Hadas 	int err;
4207d4be3f44SYishai Hadas 
4208d4be3f44SYishai Hadas 	if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
420932269441SYishai Hadas 		return ERR_PTR(-EOPNOTSUPP);
4210d4be3f44SYishai Hadas 
4211d4be3f44SYishai Hadas 	if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
4212d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
4213d4be3f44SYishai Hadas 
42148e8aa145SGustavo A. R. Silva 	dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
4215d4be3f44SYishai Hadas 	if (!dst)
4216d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
4217d4be3f44SYishai Hadas 
4218d4be3f44SYishai Hadas 	mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
4219d4be3f44SYishai Hadas 	mutex_lock(&dev->flow_db->lock);
4220d4be3f44SYishai Hadas 
4221b47fd4ffSMark Bloch 	ft_prio = _get_flow_table(dev, fs_matcher, mcast);
4222d4be3f44SYishai Hadas 	if (IS_ERR(ft_prio)) {
4223d4be3f44SYishai Hadas 		err = PTR_ERR(ft_prio);
4224d4be3f44SYishai Hadas 		goto unlock;
4225d4be3f44SYishai Hadas 	}
4226d4be3f44SYishai Hadas 
42276346f0bfSYishai Hadas 	if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
4228bfc5d839SMark Bloch 		dst[dst_num].type = dest_type;
4229bfc5d839SMark Bloch 		dst[dst_num].tir_num = dest_id;
4230b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
4231a7ee18bdSMark Bloch 	} else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
4232bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
4233bfc5d839SMark Bloch 		dst[dst_num].ft_num = dest_id;
4234b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
4235a7ee18bdSMark Bloch 	} else {
4236bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
4237a7ee18bdSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
42386346f0bfSYishai Hadas 	}
42396346f0bfSYishai Hadas 
4240bfc5d839SMark Bloch 	dst_num++;
4241bfc5d839SMark Bloch 
4242bfc5d839SMark Bloch 	if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
4243bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
4244bfc5d839SMark Bloch 		dst[dst_num].counter_id = counter_id;
4245bfc5d839SMark Bloch 		dst_num++;
4246bfc5d839SMark Bloch 	}
4247bfc5d839SMark Bloch 
4248bb0ee7dcSJianbo Liu 	handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher,
4249bb0ee7dcSJianbo Liu 					flow_context, flow_act,
4250bfc5d839SMark Bloch 					cmd_in, inlen, dst_num);
4251d4be3f44SYishai Hadas 
4252d4be3f44SYishai Hadas 	if (IS_ERR(handler)) {
4253d4be3f44SYishai Hadas 		err = PTR_ERR(handler);
4254d4be3f44SYishai Hadas 		goto destroy_ft;
4255d4be3f44SYishai Hadas 	}
4256d4be3f44SYishai Hadas 
4257d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
4258d4be3f44SYishai Hadas 	atomic_inc(&fs_matcher->usecnt);
4259d4be3f44SYishai Hadas 	handler->flow_matcher = fs_matcher;
4260d4be3f44SYishai Hadas 
4261d4be3f44SYishai Hadas 	kfree(dst);
4262d4be3f44SYishai Hadas 
4263d4be3f44SYishai Hadas 	return handler;
4264d4be3f44SYishai Hadas 
4265d4be3f44SYishai Hadas destroy_ft:
4266d4be3f44SYishai Hadas 	put_flow_table(dev, ft_prio, false);
4267d4be3f44SYishai Hadas unlock:
4268d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
4269d4be3f44SYishai Hadas 	kfree(dst);
4270d4be3f44SYishai Hadas 
4271d4be3f44SYishai Hadas 	return ERR_PTR(err);
427232269441SYishai Hadas }
427332269441SYishai Hadas 
4274c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
4275c6475a0bSAviad Yehezkel {
4276c6475a0bSAviad Yehezkel 	u32 flags = 0;
4277c6475a0bSAviad Yehezkel 
4278c6475a0bSAviad Yehezkel 	if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
4279c6475a0bSAviad Yehezkel 		flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
4280c6475a0bSAviad Yehezkel 
4281c6475a0bSAviad Yehezkel 	return flags;
4282c6475a0bSAviad Yehezkel }
4283c6475a0bSAviad Yehezkel 
4284c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED	MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
4285c6475a0bSAviad Yehezkel static struct ib_flow_action *
4286c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device,
4287c6475a0bSAviad Yehezkel 			       const struct ib_flow_action_attrs_esp *attr,
4288c6475a0bSAviad Yehezkel 			       struct uverbs_attr_bundle *attrs)
4289c6475a0bSAviad Yehezkel {
4290c6475a0bSAviad Yehezkel 	struct mlx5_ib_dev *mdev = to_mdev(device);
4291c6475a0bSAviad Yehezkel 	struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
4292c6475a0bSAviad Yehezkel 	struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
4293c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *action;
4294c6475a0bSAviad Yehezkel 	u64 action_flags;
4295c6475a0bSAviad Yehezkel 	u64 flags;
4296c6475a0bSAviad Yehezkel 	int err = 0;
4297c6475a0bSAviad Yehezkel 
4298bccd0622SJason Gunthorpe 	err = uverbs_get_flags64(
4299bccd0622SJason Gunthorpe 		&action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4300bccd0622SJason Gunthorpe 		((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
4301bccd0622SJason Gunthorpe 	if (err)
4302bccd0622SJason Gunthorpe 		return ERR_PTR(err);
4303c6475a0bSAviad Yehezkel 
4304c6475a0bSAviad Yehezkel 	flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
4305c6475a0bSAviad Yehezkel 
4306c6475a0bSAviad Yehezkel 	/* We current only support a subset of the standard features. Only a
4307c6475a0bSAviad Yehezkel 	 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
4308c6475a0bSAviad Yehezkel 	 * (with overlap). Full offload mode isn't supported.
4309c6475a0bSAviad Yehezkel 	 */
4310c6475a0bSAviad Yehezkel 	if (!attr->keymat || attr->replay || attr->encap ||
4311c6475a0bSAviad Yehezkel 	    attr->spi || attr->seq || attr->tfc_pad ||
4312c6475a0bSAviad Yehezkel 	    attr->hard_limit_pkts ||
4313c6475a0bSAviad Yehezkel 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4314c6475a0bSAviad Yehezkel 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
4315c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4316c6475a0bSAviad Yehezkel 
4317c6475a0bSAviad Yehezkel 	if (attr->keymat->protocol !=
4318c6475a0bSAviad Yehezkel 	    IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
4319c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4320c6475a0bSAviad Yehezkel 
4321c6475a0bSAviad Yehezkel 	aes_gcm = &attr->keymat->keymat.aes_gcm;
4322c6475a0bSAviad Yehezkel 
4323c6475a0bSAviad Yehezkel 	if (aes_gcm->icv_len != 16 ||
4324c6475a0bSAviad Yehezkel 	    aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
4325c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4326c6475a0bSAviad Yehezkel 
4327c6475a0bSAviad Yehezkel 	action = kmalloc(sizeof(*action), GFP_KERNEL);
4328c6475a0bSAviad Yehezkel 	if (!action)
4329c6475a0bSAviad Yehezkel 		return ERR_PTR(-ENOMEM);
4330c6475a0bSAviad Yehezkel 
4331c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4332c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
4333c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.aes_key));
4334c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
4335c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
4336c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.salt));
4337c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
4338c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
4339c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
4340c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
4341c6475a0bSAviad Yehezkel 	accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
4342c6475a0bSAviad Yehezkel 
4343c6475a0bSAviad Yehezkel 	accel_attrs.esn = attr->esn;
4344c6475a0bSAviad Yehezkel 	if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4345c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4346c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4347c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4348c6475a0bSAviad Yehezkel 
4349c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4350c6475a0bSAviad Yehezkel 		accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4351c6475a0bSAviad Yehezkel 
4352c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ctx =
4353c6475a0bSAviad Yehezkel 		mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4354c6475a0bSAviad Yehezkel 	if (IS_ERR(action->esp_aes_gcm.ctx)) {
4355c6475a0bSAviad Yehezkel 		err = PTR_ERR(action->esp_aes_gcm.ctx);
4356c6475a0bSAviad Yehezkel 		goto err_parse;
4357c6475a0bSAviad Yehezkel 	}
4358c6475a0bSAviad Yehezkel 
4359c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4360c6475a0bSAviad Yehezkel 
4361c6475a0bSAviad Yehezkel 	return &action->ib_action;
4362c6475a0bSAviad Yehezkel 
4363c6475a0bSAviad Yehezkel err_parse:
4364c6475a0bSAviad Yehezkel 	kfree(action);
4365c6475a0bSAviad Yehezkel 	return ERR_PTR(err);
4366c6475a0bSAviad Yehezkel }
4367c6475a0bSAviad Yehezkel 
4368349705c1SMatan Barak static int
4369349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4370349705c1SMatan Barak 			       const struct ib_flow_action_attrs_esp *attr,
4371349705c1SMatan Barak 			       struct uverbs_attr_bundle *attrs)
4372349705c1SMatan Barak {
4373349705c1SMatan Barak 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4374349705c1SMatan Barak 	struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4375349705c1SMatan Barak 	int err = 0;
4376349705c1SMatan Barak 
4377349705c1SMatan Barak 	if (attr->keymat || attr->replay || attr->encap ||
4378349705c1SMatan Barak 	    attr->spi || attr->seq || attr->tfc_pad ||
4379349705c1SMatan Barak 	    attr->hard_limit_pkts ||
4380349705c1SMatan Barak 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4381349705c1SMatan Barak 			     IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4382349705c1SMatan Barak 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4383349705c1SMatan Barak 		return -EOPNOTSUPP;
4384349705c1SMatan Barak 
4385349705c1SMatan Barak 	/* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4386349705c1SMatan Barak 	 * be modified.
4387349705c1SMatan Barak 	 */
4388349705c1SMatan Barak 	if (!(maction->esp_aes_gcm.ib_flags &
4389349705c1SMatan Barak 	      IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4390349705c1SMatan Barak 	    attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4391349705c1SMatan Barak 			   IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4392349705c1SMatan Barak 		return -EINVAL;
4393349705c1SMatan Barak 
4394349705c1SMatan Barak 	memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4395349705c1SMatan Barak 	       sizeof(accel_attrs));
4396349705c1SMatan Barak 
4397349705c1SMatan Barak 	accel_attrs.esn = attr->esn;
4398349705c1SMatan Barak 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4399349705c1SMatan Barak 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4400349705c1SMatan Barak 	else
4401349705c1SMatan Barak 		accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4402349705c1SMatan Barak 
4403349705c1SMatan Barak 	err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4404349705c1SMatan Barak 					 &accel_attrs);
4405349705c1SMatan Barak 	if (err)
4406349705c1SMatan Barak 		return err;
4407349705c1SMatan Barak 
4408349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags &=
4409349705c1SMatan Barak 		~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4410349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags |=
4411349705c1SMatan Barak 		attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4412349705c1SMatan Barak 
4413349705c1SMatan Barak 	return 0;
4414349705c1SMatan Barak }
4415349705c1SMatan Barak 
4416c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4417c6475a0bSAviad Yehezkel {
4418c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4419c6475a0bSAviad Yehezkel 
4420c6475a0bSAviad Yehezkel 	switch (action->type) {
4421c6475a0bSAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
4422c6475a0bSAviad Yehezkel 		/*
4423c6475a0bSAviad Yehezkel 		 * We only support aes_gcm by now, so we implicitly know this is
4424c6475a0bSAviad Yehezkel 		 * the underline crypto.
4425c6475a0bSAviad Yehezkel 		 */
4426c6475a0bSAviad Yehezkel 		mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4427c6475a0bSAviad Yehezkel 		break;
4428b4749bf2SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
4429b4749bf2SMark Bloch 		mlx5_ib_destroy_flow_action_raw(maction);
4430b4749bf2SMark Bloch 		break;
4431c6475a0bSAviad Yehezkel 	default:
4432c6475a0bSAviad Yehezkel 		WARN_ON(true);
4433c6475a0bSAviad Yehezkel 		break;
4434c6475a0bSAviad Yehezkel 	}
4435c6475a0bSAviad Yehezkel 
4436c6475a0bSAviad Yehezkel 	kfree(maction);
4437c6475a0bSAviad Yehezkel 	return 0;
4438c6475a0bSAviad Yehezkel }
4439c6475a0bSAviad Yehezkel 
4440e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4441e126ba97SEli Cohen {
4442e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
444381e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
4444e126ba97SEli Cohen 	int err;
4445539ec982SYishai Hadas 	u16 uid;
4446539ec982SYishai Hadas 
4447539ec982SYishai Hadas 	uid = ibqp->pd ?
4448539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4449e126ba97SEli Cohen 
445081e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
445181e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
445281e30880SYishai Hadas 		return -EOPNOTSUPP;
445381e30880SYishai Hadas 	}
445481e30880SYishai Hadas 
4455539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4456e126ba97SEli Cohen 	if (err)
4457e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4458e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4459e126ba97SEli Cohen 
4460e126ba97SEli Cohen 	return err;
4461e126ba97SEli Cohen }
4462e126ba97SEli Cohen 
4463e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4464e126ba97SEli Cohen {
4465e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4466e126ba97SEli Cohen 	int err;
4467539ec982SYishai Hadas 	u16 uid;
4468e126ba97SEli Cohen 
4469539ec982SYishai Hadas 	uid = ibqp->pd ?
4470539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4471539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4472e126ba97SEli Cohen 	if (err)
4473e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4474e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4475e126ba97SEli Cohen 
4476e126ba97SEli Cohen 	return err;
4477e126ba97SEli Cohen }
4478e126ba97SEli Cohen 
4479e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
4480e126ba97SEli Cohen {
44811b5daf11SMajd Dibbiny 	int err;
4482e126ba97SEli Cohen 
44831b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
4484e126ba97SEli Cohen 	if (err)
4485e126ba97SEli Cohen 		return err;
44861b5daf11SMajd Dibbiny 
44871b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
44881b5daf11SMajd Dibbiny 
44891b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
4490e126ba97SEli Cohen }
4491e126ba97SEli Cohen 
4492508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
4493508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4494e126ba97SEli Cohen {
4495e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
449654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4497e126ba97SEli Cohen 
44989603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
4499e126ba97SEli Cohen }
4500508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
4501e126ba97SEli Cohen 
4502508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
4503e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
4504e126ba97SEli Cohen {
4505e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
450654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4507e126ba97SEli Cohen 
45086aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
4509e126ba97SEli Cohen }
4510508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
4511e126ba97SEli Cohen 
4512508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
4513508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4514e126ba97SEli Cohen {
4515e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
451654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
451754747231SParav Pandit 
45189603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
4519e126ba97SEli Cohen }
4520508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
4521e126ba97SEli Cohen 
4522508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
4523508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
4524e126ba97SEli Cohen {
4525e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
452654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
452754747231SParav Pandit 
45289603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
4529e126ba97SEli Cohen }
4530508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
4531e126ba97SEli Cohen 
4532508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
4533508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4534e126ba97SEli Cohen {
4535e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
453654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
453754747231SParav Pandit 
4538e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
45399603b61dSJack Morgenstein 		       dev->mdev->board_id);
4540e126ba97SEli Cohen }
4541508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
4542e126ba97SEli Cohen 
4543508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
4544508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
4545508a523fSParav Pandit 	&dev_attr_hca_type.attr,
4546508a523fSParav Pandit 	&dev_attr_board_id.attr,
4547508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
4548508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
4549508a523fSParav Pandit 	NULL,
4550508a523fSParav Pandit };
4551e126ba97SEli Cohen 
4552508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
4553508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
4554e126ba97SEli Cohen };
4555e126ba97SEli Cohen 
45567722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
45577722f47eSHaggai Eran {
45587722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
45597722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
45607722f47eSHaggai Eran 			     pkey_change_work);
45617722f47eSHaggai Eran 
45627722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
45637722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
45647722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
45657722f47eSHaggai Eran }
45667722f47eSHaggai Eran 
456789ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
456889ea94a7SMaor Gottlieb {
456989ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
457089ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
457189ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
457289ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
457389ea94a7SMaor Gottlieb 	unsigned long flags_qp;
457489ea94a7SMaor Gottlieb 	unsigned long flags_cq;
457589ea94a7SMaor Gottlieb 	unsigned long flags;
457689ea94a7SMaor Gottlieb 
457789ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
457889ea94a7SMaor Gottlieb 
457989ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
458089ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
458189ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
458289ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
458389ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
458489ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
458589ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
458689ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
458789ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
458889ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
458989ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
459089ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
459189ea94a7SMaor Gottlieb 						      &cq_armed_list);
459289ea94a7SMaor Gottlieb 				}
459389ea94a7SMaor Gottlieb 			}
459489ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
459589ea94a7SMaor Gottlieb 		}
459689ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
459789ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
459889ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
459989ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
460089ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
460189ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
460289ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
460389ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
460489ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
460589ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
460689ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
460789ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
460889ea94a7SMaor Gottlieb 							      &cq_armed_list);
460989ea94a7SMaor Gottlieb 					}
461089ea94a7SMaor Gottlieb 				}
461189ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
461289ea94a7SMaor Gottlieb 						       flags_cq);
461389ea94a7SMaor Gottlieb 			}
461489ea94a7SMaor Gottlieb 		}
461589ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
461689ea94a7SMaor Gottlieb 	}
461789ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
461889ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
461989ea94a7SMaor Gottlieb 	 */
462089ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
46214e0e2ea1SYishai Hadas 		mcq->comp(mcq, NULL);
462289ea94a7SMaor Gottlieb 	}
462389ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
462489ea94a7SMaor Gottlieb }
462589ea94a7SMaor Gottlieb 
462603404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
462703404e8aSMaor Gottlieb {
462803404e8aSMaor Gottlieb 	int err;
462903404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
463003404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
463103404e8aSMaor Gottlieb 			     delay_drop_work);
463203404e8aSMaor Gottlieb 
4633fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
4634fe248c3aSMaor Gottlieb 
463503404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
4636*333fbaa0SLeon Romanovsky 	err = mlx5_core_set_delay_drop(delay_drop->dev, delay_drop->timeout);
463703404e8aSMaor Gottlieb 	if (err) {
463803404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
463903404e8aSMaor Gottlieb 			     delay_drop->timeout);
464003404e8aSMaor Gottlieb 		delay_drop->activate = false;
464103404e8aSMaor Gottlieb 	}
464203404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
464303404e8aSMaor Gottlieb }
464403404e8aSMaor Gottlieb 
464509e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
464609e574faSSaeed Mahameed 				 struct ib_event *ibev)
464709e574faSSaeed Mahameed {
46486cfdc7e4SAya Levin 	u8 port = (eqe->data.port.port >> 4) & 0xf;
46496cfdc7e4SAya Levin 
465009e574faSSaeed Mahameed 	switch (eqe->sub_type) {
465109e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
46526cfdc7e4SAya Levin 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
46536cfdc7e4SAya Levin 					    IB_LINK_LAYER_ETHERNET)
465409e574faSSaeed Mahameed 			schedule_work(&ibdev->delay_drop.delay_drop_work);
465509e574faSSaeed Mahameed 		break;
465609e574faSSaeed Mahameed 	default: /* do nothing */
465709e574faSSaeed Mahameed 		return;
465809e574faSSaeed Mahameed 	}
465909e574faSSaeed Mahameed }
466009e574faSSaeed Mahameed 
4661134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4662134e9349SSaeed Mahameed 			      struct ib_event *ibev)
4663134e9349SSaeed Mahameed {
4664134e9349SSaeed Mahameed 	u8 port = (eqe->data.port.port >> 4) & 0xf;
4665134e9349SSaeed Mahameed 
4666134e9349SSaeed Mahameed 	ibev->element.port_num = port;
4667134e9349SSaeed Mahameed 
4668134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
4669134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4670134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4671134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4672134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
4673134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
4674134e9349SSaeed Mahameed 		 */
4675134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4676134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
4677134e9349SSaeed Mahameed 			return -EINVAL;
4678134e9349SSaeed Mahameed 
4679134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4680134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4681134e9349SSaeed Mahameed 		break;
4682134e9349SSaeed Mahameed 
4683134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
4684134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
4685134e9349SSaeed Mahameed 		break;
4686134e9349SSaeed Mahameed 
4687134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4688134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
4689134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4690134e9349SSaeed Mahameed 		break;
4691134e9349SSaeed Mahameed 
4692134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4693134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
4694134e9349SSaeed Mahameed 		break;
4695134e9349SSaeed Mahameed 
4696134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4697134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
4698134e9349SSaeed Mahameed 		break;
4699134e9349SSaeed Mahameed 	default:
4700134e9349SSaeed Mahameed 		return -EINVAL;
4701134e9349SSaeed Mahameed 	}
4702134e9349SSaeed Mahameed 
4703134e9349SSaeed Mahameed 	return 0;
4704134e9349SSaeed Mahameed }
4705134e9349SSaeed Mahameed 
4706d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
4707e126ba97SEli Cohen {
4708d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
4709d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
4710d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
4711e126ba97SEli Cohen 	struct ib_event ibev;
4712dbaaff2aSEli Cohen 	bool fatal = false;
4713e126ba97SEli Cohen 
4714df097a27SSaeed Mahameed 	if (work->is_slave) {
4715df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
4716d69a24e0SDaniel Jurgens 		if (!ibdev)
4717d69a24e0SDaniel Jurgens 			goto out;
4718d69a24e0SDaniel Jurgens 	} else {
4719df097a27SSaeed Mahameed 		ibdev = work->dev;
4720d69a24e0SDaniel Jurgens 	}
4721d69a24e0SDaniel Jurgens 
4722d69a24e0SDaniel Jurgens 	switch (work->event) {
4723e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
4724e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
472589ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
4726134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
4727dbaaff2aSEli Cohen 		fatal = true;
4728e126ba97SEli Cohen 		break;
4729134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
4730134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
4731d69a24e0SDaniel Jurgens 			goto out;
4732e126ba97SEli Cohen 		break;
473309e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
473409e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
473509e574faSSaeed Mahameed 		/* fall through */
4736bdc37924SSaeed Mahameed 	default:
473703404e8aSMaor Gottlieb 		goto out;
4738e126ba97SEli Cohen 	}
4739e126ba97SEli Cohen 
4740e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
4741e126ba97SEli Cohen 
4742134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4743134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
474403404e8aSMaor Gottlieb 		goto out;
4745a0c84c32SEli Cohen 	}
4746a0c84c32SEli Cohen 
4747e126ba97SEli Cohen 	if (ibdev->ib_active)
4748e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
4749dbaaff2aSEli Cohen 
4750dbaaff2aSEli Cohen 	if (fatal)
4751dbaaff2aSEli Cohen 		ibdev->ib_active = false;
475203404e8aSMaor Gottlieb out:
4753d69a24e0SDaniel Jurgens 	kfree(work);
4754d69a24e0SDaniel Jurgens }
4755d69a24e0SDaniel Jurgens 
4756df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
4757df097a27SSaeed Mahameed 			 unsigned long event, void *param)
4758d69a24e0SDaniel Jurgens {
4759d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
4760d69a24e0SDaniel Jurgens 
4761d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
476210bea9c8SLeon Romanovsky 	if (!work)
4763df097a27SSaeed Mahameed 		return NOTIFY_DONE;
476410bea9c8SLeon Romanovsky 
4765d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4766df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4767df097a27SSaeed Mahameed 	work->is_slave = false;
4768d69a24e0SDaniel Jurgens 	work->param = param;
4769d69a24e0SDaniel Jurgens 	work->event = event;
4770d69a24e0SDaniel Jurgens 
4771d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
4772df097a27SSaeed Mahameed 
4773df097a27SSaeed Mahameed 	return NOTIFY_OK;
4774df097a27SSaeed Mahameed }
4775df097a27SSaeed Mahameed 
4776df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4777df097a27SSaeed Mahameed 				    unsigned long event, void *param)
4778df097a27SSaeed Mahameed {
4779df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
4780df097a27SSaeed Mahameed 
4781df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
4782df097a27SSaeed Mahameed 	if (!work)
4783df097a27SSaeed Mahameed 		return NOTIFY_DONE;
4784df097a27SSaeed Mahameed 
4785df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4786df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4787df097a27SSaeed Mahameed 	work->is_slave = true;
4788df097a27SSaeed Mahameed 	work->param = param;
4789df097a27SSaeed Mahameed 	work->event = event;
4790df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
4791df097a27SSaeed Mahameed 
4792df097a27SSaeed Mahameed 	return NOTIFY_OK;
4793d69a24e0SDaniel Jurgens }
4794d69a24e0SDaniel Jurgens 
4795c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4796c43f1112SMaor Gottlieb {
4797c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
4798c43f1112SMaor Gottlieb 	int err;
4799c43f1112SMaor Gottlieb 	int port;
4800c43f1112SMaor Gottlieb 
4801a989ea01SMark Bloch 	for (port = 1; port <= ARRAY_SIZE(dev->mdev->port_caps); port++) {
4802c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
4803c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4804c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
4805c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4806c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
4807c43f1112SMaor Gottlieb 								   port, 0,
4808c43f1112SMaor Gottlieb 								   &vport_ctx);
4809c43f1112SMaor Gottlieb 				if (err) {
4810c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4811c43f1112SMaor Gottlieb 						    port, err);
4812c43f1112SMaor Gottlieb 					return err;
4813c43f1112SMaor Gottlieb 				}
4814c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
4815c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
4816c43f1112SMaor Gottlieb 			} else {
4817c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
4818c43f1112SMaor Gottlieb 			}
4819c43f1112SMaor Gottlieb 		}
4820c43f1112SMaor Gottlieb 	}
4821c43f1112SMaor Gottlieb 	return 0;
4822c43f1112SMaor Gottlieb }
4823c43f1112SMaor Gottlieb 
4824e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4825e126ba97SEli Cohen {
4826e126ba97SEli Cohen 	int port;
4827e126ba97SEli Cohen 
4828508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
4829e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
4830e126ba97SEli Cohen }
4831e126ba97SEli Cohen 
483226628e2dSMark Bloch static int __get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4833e126ba97SEli Cohen {
4834e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
4835e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
4836f614fc15SDan Carpenter 	int err = -ENOMEM;
4837e126ba97SEli Cohen 
483850ba3c18SLeon Romanovsky 	pprops = kzalloc(sizeof(*pprops), GFP_KERNEL);
4839e126ba97SEli Cohen 	if (!pprops)
4840e126ba97SEli Cohen 		goto out;
4841e126ba97SEli Cohen 
4842e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4843e126ba97SEli Cohen 	if (!dprops)
4844e126ba97SEli Cohen 		goto out;
4845e126ba97SEli Cohen 
484648357091SLeon Romanovsky 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, NULL);
4847e126ba97SEli Cohen 	if (err) {
4848e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
4849e126ba97SEli Cohen 		goto out;
4850e126ba97SEli Cohen 	}
4851e126ba97SEli Cohen 
4852e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4853e126ba97SEli Cohen 	if (err) {
4854938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
4855938fe83cSSaeed Mahameed 			     port, err);
485632f69e4bSDaniel Jurgens 		goto out;
4857e126ba97SEli Cohen 	}
485832f69e4bSDaniel Jurgens 
4859938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
4860938fe83cSSaeed Mahameed 					dprops->max_pkeys;
4861938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
4862938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
486332f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
486432f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
4865e126ba97SEli Cohen 
4866e126ba97SEli Cohen out:
4867e126ba97SEli Cohen 	kfree(pprops);
4868e126ba97SEli Cohen 	kfree(dprops);
4869e126ba97SEli Cohen 
4870e126ba97SEli Cohen 	return err;
4871e126ba97SEli Cohen }
4872e126ba97SEli Cohen 
487326628e2dSMark Bloch static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
487426628e2dSMark Bloch {
487526628e2dSMark Bloch 	/* For representors use port 1, is this is the only native
487626628e2dSMark Bloch 	 * port
487726628e2dSMark Bloch 	 */
487826628e2dSMark Bloch 	if (dev->is_rep)
487926628e2dSMark Bloch 		return __get_port_caps(dev, 1);
488026628e2dSMark Bloch 	return __get_port_caps(dev, port);
488126628e2dSMark Bloch }
488226628e2dSMark Bloch 
4883e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4884e126ba97SEli Cohen {
4885e126ba97SEli Cohen 	int err;
4886e126ba97SEli Cohen 
4887e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
4888e126ba97SEli Cohen 	if (err)
4889e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4890e126ba97SEli Cohen 
489132927e28SMark Bloch 	if (dev->umrc.qp)
4892c4367a26SShamir Rabinovitch 		mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
489332927e28SMark Bloch 	if (dev->umrc.cq)
4894add08d76SChristoph Hellwig 		ib_free_cq(dev->umrc.cq);
489532927e28SMark Bloch 	if (dev->umrc.pd)
4896e126ba97SEli Cohen 		ib_dealloc_pd(dev->umrc.pd);
4897e126ba97SEli Cohen }
4898e126ba97SEli Cohen 
4899e126ba97SEli Cohen enum {
4900e126ba97SEli Cohen 	MAX_UMR_WR = 128,
4901e126ba97SEli Cohen };
4902e126ba97SEli Cohen 
4903e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
4904e126ba97SEli Cohen {
4905e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
4906e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
4907e126ba97SEli Cohen 	struct ib_pd *pd;
4908e126ba97SEli Cohen 	struct ib_cq *cq;
4909e126ba97SEli Cohen 	struct ib_qp *qp;
4910e126ba97SEli Cohen 	int ret;
4911e126ba97SEli Cohen 
4912e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4913e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4914e126ba97SEli Cohen 	if (!attr || !init_attr) {
4915e126ba97SEli Cohen 		ret = -ENOMEM;
4916e126ba97SEli Cohen 		goto error_0;
4917e126ba97SEli Cohen 	}
4918e126ba97SEli Cohen 
4919ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
4920e126ba97SEli Cohen 	if (IS_ERR(pd)) {
4921e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4922e126ba97SEli Cohen 		ret = PTR_ERR(pd);
4923e126ba97SEli Cohen 		goto error_0;
4924e126ba97SEli Cohen 	}
4925e126ba97SEli Cohen 
4926add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
4927e126ba97SEli Cohen 	if (IS_ERR(cq)) {
4928e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4929e126ba97SEli Cohen 		ret = PTR_ERR(cq);
4930e126ba97SEli Cohen 		goto error_2;
4931e126ba97SEli Cohen 	}
4932e126ba97SEli Cohen 
4933e126ba97SEli Cohen 	init_attr->send_cq = cq;
4934e126ba97SEli Cohen 	init_attr->recv_cq = cq;
4935e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4936e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
4937e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
4938e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4939e126ba97SEli Cohen 	init_attr->port_num = 1;
4940e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4941e126ba97SEli Cohen 	if (IS_ERR(qp)) {
4942e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4943e126ba97SEli Cohen 		ret = PTR_ERR(qp);
4944e126ba97SEli Cohen 		goto error_3;
4945e126ba97SEli Cohen 	}
4946e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
4947e126ba97SEli Cohen 	qp->real_qp    = qp;
4948e126ba97SEli Cohen 	qp->uobject    = NULL;
4949e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
495031fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
495131fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
4952e126ba97SEli Cohen 
4953e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
4954e126ba97SEli Cohen 	attr->port_num = 1;
4955e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4956e126ba97SEli Cohen 				IB_QP_PORT, NULL);
4957e126ba97SEli Cohen 	if (ret) {
4958e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4959e126ba97SEli Cohen 		goto error_4;
4960e126ba97SEli Cohen 	}
4961e126ba97SEli Cohen 
4962e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4963e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
4964e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
4965e126ba97SEli Cohen 
4966e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4967e126ba97SEli Cohen 	if (ret) {
4968e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4969e126ba97SEli Cohen 		goto error_4;
4970e126ba97SEli Cohen 	}
4971e126ba97SEli Cohen 
4972e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4973e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
4974e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4975e126ba97SEli Cohen 	if (ret) {
4976e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4977e126ba97SEli Cohen 		goto error_4;
4978e126ba97SEli Cohen 	}
4979e126ba97SEli Cohen 
4980e126ba97SEli Cohen 	dev->umrc.qp = qp;
4981e126ba97SEli Cohen 	dev->umrc.cq = cq;
4982e126ba97SEli Cohen 	dev->umrc.pd = pd;
4983e126ba97SEli Cohen 
4984e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
4985e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
4986e126ba97SEli Cohen 	if (ret) {
4987e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4988e126ba97SEli Cohen 		goto error_4;
4989e126ba97SEli Cohen 	}
4990e126ba97SEli Cohen 
4991e126ba97SEli Cohen 	kfree(attr);
4992e126ba97SEli Cohen 	kfree(init_attr);
4993e126ba97SEli Cohen 
4994e126ba97SEli Cohen 	return 0;
4995e126ba97SEli Cohen 
4996e126ba97SEli Cohen error_4:
4997c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_qp(qp, NULL);
499832927e28SMark Bloch 	dev->umrc.qp = NULL;
4999e126ba97SEli Cohen 
5000e126ba97SEli Cohen error_3:
5001add08d76SChristoph Hellwig 	ib_free_cq(cq);
500232927e28SMark Bloch 	dev->umrc.cq = NULL;
5003e126ba97SEli Cohen 
5004e126ba97SEli Cohen error_2:
5005e126ba97SEli Cohen 	ib_dealloc_pd(pd);
500632927e28SMark Bloch 	dev->umrc.pd = NULL;
5007e126ba97SEli Cohen 
5008e126ba97SEli Cohen error_0:
5009e126ba97SEli Cohen 	kfree(attr);
5010e126ba97SEli Cohen 	kfree(init_attr);
5011e126ba97SEli Cohen 	return ret;
5012e126ba97SEli Cohen }
5013e126ba97SEli Cohen 
50146e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
50156e8484c5SMax Gurtovoy {
50166e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
50176e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
50186e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
50196e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
50206e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
50216e8484c5SMax Gurtovoy 	default:
50226e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
50236e8484c5SMax Gurtovoy 	}
50246e8484c5SMax Gurtovoy }
50256e8484c5SMax Gurtovoy 
5026e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
5027e126ba97SEli Cohen {
5028e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
5029e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
503021a428a0SLeon Romanovsky 	struct ib_device *ibdev;
5031bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
50327722f47eSHaggai Eran 	int port;
5033e126ba97SEli Cohen 	int ret = 0;
5034e126ba97SEli Cohen 
5035e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
503621a428a0SLeon Romanovsky 	ibdev = &dev->ib_dev;
5037e126ba97SEli Cohen 
5038d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
5039d16e91daSHaggai Eran 
504021a428a0SLeon Romanovsky 	devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
504121a428a0SLeon Romanovsky 	if (!devr->p0)
504221a428a0SLeon Romanovsky 		return -ENOMEM;
504321a428a0SLeon Romanovsky 
504421a428a0SLeon Romanovsky 	devr->p0->device  = ibdev;
5045e126ba97SEli Cohen 	devr->p0->uobject = NULL;
5046e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
5047e126ba97SEli Cohen 
5048ff23dfa1SShamir Rabinovitch 	ret = mlx5_ib_alloc_pd(devr->p0, NULL);
504921a428a0SLeon Romanovsky 	if (ret)
505021a428a0SLeon Romanovsky 		goto error0;
505121a428a0SLeon Romanovsky 
5052e39afe3dSLeon Romanovsky 	devr->c0 = rdma_zalloc_drv_obj(ibdev, ib_cq);
5053e39afe3dSLeon Romanovsky 	if (!devr->c0) {
5054e39afe3dSLeon Romanovsky 		ret = -ENOMEM;
5055e126ba97SEli Cohen 		goto error1;
5056e126ba97SEli Cohen 	}
5057e39afe3dSLeon Romanovsky 
5058e126ba97SEli Cohen 	devr->c0->device = &dev->ib_dev;
5059e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
5060e126ba97SEli Cohen 
5061e39afe3dSLeon Romanovsky 	ret = mlx5_ib_create_cq(devr->c0, &cq_attr, NULL);
5062e39afe3dSLeon Romanovsky 	if (ret)
5063e39afe3dSLeon Romanovsky 		goto err_create_cq;
5064e39afe3dSLeon Romanovsky 
5065ff23dfa1SShamir Rabinovitch 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
5066e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
5067e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
5068e126ba97SEli Cohen 		goto error2;
5069e126ba97SEli Cohen 	}
5070e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
5071e126ba97SEli Cohen 	devr->x0->inode = NULL;
5072e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
5073e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
5074e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
5075e126ba97SEli Cohen 
5076ff23dfa1SShamir Rabinovitch 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
5077e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
5078e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
5079e126ba97SEli Cohen 		goto error3;
5080e126ba97SEli Cohen 	}
5081e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
5082e126ba97SEli Cohen 	devr->x1->inode = NULL;
5083e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
5084e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
5085e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
5086e126ba97SEli Cohen 
5087e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
5088e126ba97SEli Cohen 	attr.attr.max_sge = 1;
5089e126ba97SEli Cohen 	attr.attr.max_wr = 1;
5090e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
50911a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
5092e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
5093e126ba97SEli Cohen 
509468e326deSLeon Romanovsky 	devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
509568e326deSLeon Romanovsky 	if (!devr->s0) {
509668e326deSLeon Romanovsky 		ret = -ENOMEM;
5097e126ba97SEli Cohen 		goto error4;
5098e126ba97SEli Cohen 	}
509968e326deSLeon Romanovsky 
5100e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
5101e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
5102e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
5103e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
51041a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
510568e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
510668e326deSLeon Romanovsky 	if (ret)
510768e326deSLeon Romanovsky 		goto err_create;
510868e326deSLeon Romanovsky 
5109e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
51101a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
5111e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
5112e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
5113e126ba97SEli Cohen 
51144aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
51154aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
51164aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
51174aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
511868e326deSLeon Romanovsky 	devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
511968e326deSLeon Romanovsky 	if (!devr->s1) {
512068e326deSLeon Romanovsky 		ret = -ENOMEM;
51214aa17b28SHaggai Abramonvsky 		goto error5;
51224aa17b28SHaggai Abramonvsky 	}
512368e326deSLeon Romanovsky 
51244aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
51254aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
51264aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
51271a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
512868e326deSLeon Romanovsky 
512968e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
513068e326deSLeon Romanovsky 	if (ret)
513168e326deSLeon Romanovsky 		goto error6;
513268e326deSLeon Romanovsky 
51334aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
51341a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
51354aa17b28SHaggai Abramonvsky 
51367722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
51377722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
51387722f47eSHaggai Eran 			  pkey_change_handler);
51397722f47eSHaggai Eran 		devr->ports[port].devr = devr;
51407722f47eSHaggai Eran 	}
51417722f47eSHaggai Eran 
5142e126ba97SEli Cohen 	return 0;
5143e126ba97SEli Cohen 
514468e326deSLeon Romanovsky error6:
514568e326deSLeon Romanovsky 	kfree(devr->s1);
51464aa17b28SHaggai Abramonvsky error5:
5147c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
514868e326deSLeon Romanovsky err_create:
514968e326deSLeon Romanovsky 	kfree(devr->s0);
5150e126ba97SEli Cohen error4:
5151c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x1, NULL);
5152e126ba97SEli Cohen error3:
5153c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x0, NULL);
5154e126ba97SEli Cohen error2:
5155c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
5156e39afe3dSLeon Romanovsky err_create_cq:
5157e39afe3dSLeon Romanovsky 	kfree(devr->c0);
5158e126ba97SEli Cohen error1:
5159c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
5160e126ba97SEli Cohen error0:
516121a428a0SLeon Romanovsky 	kfree(devr->p0);
5162e126ba97SEli Cohen 	return ret;
5163e126ba97SEli Cohen }
5164e126ba97SEli Cohen 
5165e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
5166e126ba97SEli Cohen {
51677722f47eSHaggai Eran 	int port;
51687722f47eSHaggai Eran 
5169c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s1, NULL);
517068e326deSLeon Romanovsky 	kfree(devr->s1);
5171c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
517268e326deSLeon Romanovsky 	kfree(devr->s0);
5173c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x0, NULL);
5174c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x1, NULL);
5175c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
5176e39afe3dSLeon Romanovsky 	kfree(devr->c0);
5177c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
517821a428a0SLeon Romanovsky 	kfree(devr->p0);
51797722f47eSHaggai Eran 
51807722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
51815d8f6a0eSMark Bloch 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
51827722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
5183e126ba97SEli Cohen }
5184e126ba97SEli Cohen 
5185b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
5186b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
5187e53505a8SAchiad Shochat {
5188e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5189e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
5190e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
5191e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
519285c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
5193e53505a8SAchiad Shochat 	u32 ret = 0;
5194e53505a8SAchiad Shochat 
5195b02289b3SArtemy Kovalyov 	if (rep->grh_required)
5196b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
5197b02289b3SArtemy Kovalyov 
5198e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
5199b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
5200e53505a8SAchiad Shochat 
520185c7c014SDaniel Jurgens 	if (raw_support)
5202b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
520372cd5717SOr Gerlitz 
5204e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
520572cd5717SOr Gerlitz 		return ret;
5206e53505a8SAchiad Shochat 
5207e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
520872cd5717SOr Gerlitz 		return ret;
5209e53505a8SAchiad Shochat 
5210e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
5211e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
5212e53505a8SAchiad Shochat 
5213e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
5214e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
5215e53505a8SAchiad Shochat 
5216e53505a8SAchiad Shochat 	return ret;
5217e53505a8SAchiad Shochat }
5218e53505a8SAchiad Shochat 
52197738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
52207738613eSIra Weiny 			       struct ib_port_immutable *immutable)
52217738613eSIra Weiny {
52227738613eSIra Weiny 	struct ib_port_attr attr;
5223ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5224ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
5225b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
52267738613eSIra Weiny 	int err;
52277738613eSIra Weiny 
5228c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
52297738613eSIra Weiny 	if (err)
52307738613eSIra Weiny 		return err;
52317738613eSIra Weiny 
5232b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
5233b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
5234b02289b3SArtemy Kovalyov 						   &rep);
5235b02289b3SArtemy Kovalyov 		if (err)
5236b02289b3SArtemy Kovalyov 			return err;
5237b02289b3SArtemy Kovalyov 	}
5238b02289b3SArtemy Kovalyov 
52397738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
52407738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
5241b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
5242337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
52437738613eSIra Weiny 
52447738613eSIra Weiny 	return 0;
52457738613eSIra Weiny }
52467738613eSIra Weiny 
52478e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
52488e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
52498e6efa3aSMark Bloch {
52508e6efa3aSMark Bloch 	struct ib_port_attr attr;
52518e6efa3aSMark Bloch 	int err;
52528e6efa3aSMark Bloch 
52538e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
52548e6efa3aSMark Bloch 
52558e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
52568e6efa3aSMark Bloch 	if (err)
52578e6efa3aSMark Bloch 		return err;
52588e6efa3aSMark Bloch 
52598e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
52608e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
52618e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
52628e6efa3aSMark Bloch 
52638e6efa3aSMark Bloch 	return 0;
52648e6efa3aSMark Bloch }
52658e6efa3aSMark Bloch 
52669abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
5267c7342823SIra Weiny {
5268c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
5269c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
52709abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
52719abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
52729abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
5273c7342823SIra Weiny }
5274c7342823SIra Weiny 
527545f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
52769ef9c640SAviv Heller {
52779ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
52789ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
52799ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
52809ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
52819ef9c640SAviv Heller 	int err;
52829ef9c640SAviv Heller 
52837c34ec19SAviv Heller 	if (!ns || !mlx5_lag_is_roce(mdev))
52849ef9c640SAviv Heller 		return 0;
52859ef9c640SAviv Heller 
52869ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
52879ef9c640SAviv Heller 	if (err)
52889ef9c640SAviv Heller 		return err;
52899ef9c640SAviv Heller 
52909ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
52919ef9c640SAviv Heller 	if (IS_ERR(ft)) {
52929ef9c640SAviv Heller 		err = PTR_ERR(ft);
52939ef9c640SAviv Heller 		goto err_destroy_vport_lag;
52949ef9c640SAviv Heller 	}
52959ef9c640SAviv Heller 
52969a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
52977c34ec19SAviv Heller 	dev->lag_active = true;
52989ef9c640SAviv Heller 	return 0;
52999ef9c640SAviv Heller 
53009ef9c640SAviv Heller err_destroy_vport_lag:
53019ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
53029ef9c640SAviv Heller 	return err;
53039ef9c640SAviv Heller }
53049ef9c640SAviv Heller 
530545f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
53069ef9c640SAviv Heller {
53079ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
53089ef9c640SAviv Heller 
53097c34ec19SAviv Heller 	if (dev->lag_active) {
53107c34ec19SAviv Heller 		dev->lag_active = false;
53117c34ec19SAviv Heller 
53129a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
53139a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
53149ef9c640SAviv Heller 
53159ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
53169ef9c640SAviv Heller 	}
53179ef9c640SAviv Heller }
53189ef9c640SAviv Heller 
53197fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5320fc24fc5eSAchiad Shochat {
5321e53505a8SAchiad Shochat 	int err;
5322e53505a8SAchiad Shochat 
532395579e78SMark Bloch 	dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
532495579e78SMark Bloch 	err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
53255ec8c83eSAviv Heller 	if (err) {
532695579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
5327e53505a8SAchiad Shochat 		return err;
53285ec8c83eSAviv Heller 	}
5329e53505a8SAchiad Shochat 
5330d012f5d6SOr Gerlitz 	return 0;
5331d012f5d6SOr Gerlitz }
5332d012f5d6SOr Gerlitz 
53337fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5334e126ba97SEli Cohen {
533595579e78SMark Bloch 	if (dev->port[port_num].roce.nb.notifier_call) {
533695579e78SMark Bloch 		unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
533795579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
5338e126ba97SEli Cohen 	}
5339e126ba97SEli Cohen }
5340e126ba97SEli Cohen 
5341e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
5342e126ba97SEli Cohen {
5343e126ba97SEli Cohen 	int err;
5344e126ba97SEli Cohen 
5345e53505a8SAchiad Shochat 	err = mlx5_nic_vport_enable_roce(dev->mdev);
5346e53505a8SAchiad Shochat 	if (err)
53478e6efa3aSMark Bloch 		return err;
5348e53505a8SAchiad Shochat 
534945f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
53509ef9c640SAviv Heller 	if (err)
53519ef9c640SAviv Heller 		goto err_disable_roce;
53529ef9c640SAviv Heller 
5353e53505a8SAchiad Shochat 	return 0;
5354e53505a8SAchiad Shochat 
53559ef9c640SAviv Heller err_disable_roce:
53569ef9c640SAviv Heller 	mlx5_nic_vport_disable_roce(dev->mdev);
53579ef9c640SAviv Heller 
5358e53505a8SAchiad Shochat 	return err;
5359fc24fc5eSAchiad Shochat }
5360fc24fc5eSAchiad Shochat 
536145f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
5362fc24fc5eSAchiad Shochat {
536345f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
5364e53505a8SAchiad Shochat 	mlx5_nic_vport_disable_roce(dev->mdev);
5365fc24fc5eSAchiad Shochat }
5366fc24fc5eSAchiad Shochat 
5367e1f24a79SParav Pandit struct mlx5_ib_counter {
53687c16f477SKamal Heib 	const char *name;
53697c16f477SKamal Heib 	size_t offset;
53707c16f477SKamal Heib };
53717c16f477SKamal Heib 
53727c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
53737c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
53747c16f477SKamal Heib 
5375e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
53767c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
53777c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
53787c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
53797c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
53807c16f477SKamal Heib };
53817c16f477SKamal Heib 
5382e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
53837c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
53847c16f477SKamal Heib };
53857c16f477SKamal Heib 
5386e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
53877c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
53887c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
53897c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
53907c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
53917c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
53927c16f477SKamal Heib };
53937c16f477SKamal Heib 
5394e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
5395e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
5396e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
5397e1f24a79SParav Pandit 
5398e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
5399e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
5400e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
5401e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
5402e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
5403e1f24a79SParav Pandit };
5404e1f24a79SParav Pandit 
540558dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
540658dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
540758dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
540858dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
540958dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
541058dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
541158dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
541258dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
541358dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
541458dcb60aSParav Pandit };
541558dcb60aSParav Pandit 
5416d7fab916SAvihai Horon static const struct mlx5_ib_counter roce_accl_cnts[] = {
5417d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_adp_retrans),
5418d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_adp_retrans_to),
5419d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_slow_restart),
5420d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_slow_restart_cnps),
5421d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_slow_restart_trans),
5422d7fab916SAvihai Horon };
5423d7fab916SAvihai Horon 
54249f876f3dSTalat Batheesh #define INIT_EXT_PPCNT_COUNTER(_name)		\
54259f876f3dSTalat Batheesh 	{ .name = #_name, .offset =	\
54269f876f3dSTalat Batheesh 	MLX5_BYTE_OFF(ppcnt_reg, \
54279f876f3dSTalat Batheesh 		      counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
54289f876f3dSTalat Batheesh 
54299f876f3dSTalat Batheesh static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
54309f876f3dSTalat Batheesh 	INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
54319f876f3dSTalat Batheesh };
54329f876f3dSTalat Batheesh 
54333e1f000fSParav Pandit static bool is_mdev_switchdev_mode(const struct mlx5_core_dev *mdev)
54343e1f000fSParav Pandit {
54353e1f000fSParav Pandit 	return MLX5_ESWITCH_MANAGER(mdev) &&
54363e1f000fSParav Pandit 	       mlx5_ib_eswitch_mode(mdev->priv.eswitch) ==
54373e1f000fSParav Pandit 		       MLX5_ESWITCH_OFFLOADS;
54383e1f000fSParav Pandit }
54393e1f000fSParav Pandit 
5440e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
54410837e86aSMark Bloch {
544266247fbbSLeon Romanovsky 	u32 in[MLX5_ST_SZ_DW(dealloc_q_counter_in)] = {};
54433e1f000fSParav Pandit 	int num_cnt_ports;
5444aac4492eSDaniel Jurgens 	int i;
54450837e86aSMark Bloch 
54463e1f000fSParav Pandit 	num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports;
54473e1f000fSParav Pandit 
544866247fbbSLeon Romanovsky 	MLX5_SET(dealloc_q_counter_in, in, opcode,
544966247fbbSLeon Romanovsky 		 MLX5_CMD_OP_DEALLOC_Q_COUNTER);
545066247fbbSLeon Romanovsky 
54513e1f000fSParav Pandit 	for (i = 0; i < num_cnt_ports; i++) {
5452bfd745f8SLeon Romanovsky 		if (dev->port[i].cnts.set_id) {
545366247fbbSLeon Romanovsky 			MLX5_SET(dealloc_q_counter_in, in, counter_set_id,
5454e1f24a79SParav Pandit 				 dev->port[i].cnts.set_id);
545566247fbbSLeon Romanovsky 			mlx5_cmd_exec_in(dev->mdev, dealloc_q_counter, in);
545666247fbbSLeon Romanovsky 		}
5457e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
5458e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
54597c16f477SKamal Heib 	}
54607c16f477SKamal Heib }
54617c16f477SKamal Heib 
5462e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5463e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
54647c16f477SKamal Heib {
54657c16f477SKamal Heib 	u32 num_counters;
54667c16f477SKamal Heib 
54677c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
54687c16f477SKamal Heib 
54697c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
54707c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
54717c16f477SKamal Heib 
54727c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
54737c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
547458dcb60aSParav Pandit 
547558dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
547658dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
547758dcb60aSParav Pandit 
5478d7fab916SAvihai Horon 	if (MLX5_CAP_GEN(dev->mdev, roce_accl))
5479d7fab916SAvihai Horon 		num_counters += ARRAY_SIZE(roce_accl_cnts);
5480d7fab916SAvihai Horon 
5481e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
54827c16f477SKamal Heib 
5483e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5484e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5485e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
5486e1f24a79SParav Pandit 	}
54879f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
54889f876f3dSTalat Batheesh 		cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
54899f876f3dSTalat Batheesh 		num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
54909f876f3dSTalat Batheesh 	}
5491e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5492e1f24a79SParav Pandit 	if (!cnts->names)
54937c16f477SKamal Heib 		return -ENOMEM;
54947c16f477SKamal Heib 
5495e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
5496e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
5497e1f24a79SParav Pandit 	if (!cnts->offsets)
54987c16f477SKamal Heib 		goto err_names;
54997c16f477SKamal Heib 
55007c16f477SKamal Heib 	return 0;
55017c16f477SKamal Heib 
55027c16f477SKamal Heib err_names:
5503e1f24a79SParav Pandit 	kfree(cnts->names);
5504aac4492eSDaniel Jurgens 	cnts->names = NULL;
55057c16f477SKamal Heib 	return -ENOMEM;
55067c16f477SKamal Heib }
55077c16f477SKamal Heib 
5508e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
55097c16f477SKamal Heib 				  const char **names,
55107c16f477SKamal Heib 				  size_t *offsets)
55117c16f477SKamal Heib {
55127c16f477SKamal Heib 	int i;
55137c16f477SKamal Heib 	int j = 0;
55147c16f477SKamal Heib 
55157c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
55167c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
55177c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
55187c16f477SKamal Heib 	}
55197c16f477SKamal Heib 
55207c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
55217c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
55227c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
55237c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
55247c16f477SKamal Heib 		}
55257c16f477SKamal Heib 	}
55267c16f477SKamal Heib 
55277c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
55287c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
55297c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
55307c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
55317c16f477SKamal Heib 		}
55327c16f477SKamal Heib 	}
5533e1f24a79SParav Pandit 
553458dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
553558dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
553658dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
553758dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
553858dcb60aSParav Pandit 		}
553958dcb60aSParav Pandit 	}
554058dcb60aSParav Pandit 
5541d7fab916SAvihai Horon 	if (MLX5_CAP_GEN(dev->mdev, roce_accl)) {
5542d7fab916SAvihai Horon 		for (i = 0; i < ARRAY_SIZE(roce_accl_cnts); i++, j++) {
5543d7fab916SAvihai Horon 			names[j] = roce_accl_cnts[i].name;
5544d7fab916SAvihai Horon 			offsets[j] = roce_accl_cnts[i].offset;
5545d7fab916SAvihai Horon 		}
5546d7fab916SAvihai Horon 	}
5547d7fab916SAvihai Horon 
5548e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5549e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5550e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
5551e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
5552e1f24a79SParav Pandit 		}
5553e1f24a79SParav Pandit 	}
55549f876f3dSTalat Batheesh 
55559f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
55569f876f3dSTalat Batheesh 		for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
55579f876f3dSTalat Batheesh 			names[j] = ext_ppcnt_cnts[i].name;
55589f876f3dSTalat Batheesh 			offsets[j] = ext_ppcnt_cnts[i].offset;
55599f876f3dSTalat Batheesh 		}
55609f876f3dSTalat Batheesh 	}
55610837e86aSMark Bloch }
55620837e86aSMark Bloch 
5563e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
55640837e86aSMark Bloch {
5565bfd745f8SLeon Romanovsky 	u32 out[MLX5_ST_SZ_DW(alloc_q_counter_out)] = {};
5566bfd745f8SLeon Romanovsky 	u32 in[MLX5_ST_SZ_DW(alloc_q_counter_in)] = {};
55673e1f000fSParav Pandit 	int num_cnt_ports;
5568aac4492eSDaniel Jurgens 	int err = 0;
55690837e86aSMark Bloch 	int i;
5570aa74be6eSYishai Hadas 	bool is_shared;
5571aa74be6eSYishai Hadas 
5572bfd745f8SLeon Romanovsky 	MLX5_SET(alloc_q_counter_in, in, opcode, MLX5_CMD_OP_ALLOC_Q_COUNTER);
5573aa74be6eSYishai Hadas 	is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
55743e1f000fSParav Pandit 	num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports;
55750837e86aSMark Bloch 
55763e1f000fSParav Pandit 	for (i = 0; i < num_cnt_ports; i++) {
5577aac4492eSDaniel Jurgens 		err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5578aac4492eSDaniel Jurgens 		if (err)
5579aac4492eSDaniel Jurgens 			goto err_alloc;
55807c16f477SKamal Heib 
5581aac4492eSDaniel Jurgens 		mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5582aac4492eSDaniel Jurgens 				      dev->port[i].cnts.offsets);
5583aac4492eSDaniel Jurgens 
5584bfd745f8SLeon Romanovsky 		MLX5_SET(alloc_q_counter_in, in, uid,
5585bfd745f8SLeon Romanovsky 			 is_shared ? MLX5_SHARED_RESOURCE_UID : 0);
5586bfd745f8SLeon Romanovsky 
5587bfd745f8SLeon Romanovsky 		err = mlx5_cmd_exec_inout(dev->mdev, alloc_q_counter, in, out);
5588aac4492eSDaniel Jurgens 		if (err) {
55890837e86aSMark Bloch 			mlx5_ib_warn(dev,
55900837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
5591aac4492eSDaniel Jurgens 				     i + 1, err);
5592aac4492eSDaniel Jurgens 			goto err_alloc;
55930837e86aSMark Bloch 		}
5594bfd745f8SLeon Romanovsky 
5595bfd745f8SLeon Romanovsky 		dev->port[i].cnts.set_id =
5596bfd745f8SLeon Romanovsky 			MLX5_GET(alloc_q_counter_out, out, counter_set_id);
55970837e86aSMark Bloch 	}
55980837e86aSMark Bloch 	return 0;
55990837e86aSMark Bloch 
5600aac4492eSDaniel Jurgens err_alloc:
5601aac4492eSDaniel Jurgens 	mlx5_ib_dealloc_counters(dev);
5602aac4492eSDaniel Jurgens 	return err;
56030837e86aSMark Bloch }
56040837e86aSMark Bloch 
56053e1f000fSParav Pandit static const struct mlx5_ib_counters *get_counters(struct mlx5_ib_dev *dev,
56063e1f000fSParav Pandit 						   u8 port_num)
56073e1f000fSParav Pandit {
56083e1f000fSParav Pandit 	return is_mdev_switchdev_mode(dev->mdev) ? &dev->port[0].cnts :
56093e1f000fSParav Pandit 						   &dev->port[port_num].cnts;
56103e1f000fSParav Pandit }
56113e1f000fSParav Pandit 
56123e1f000fSParav Pandit /**
56133e1f000fSParav Pandit  * mlx5_ib_get_counters_id - Returns counters id to use for device+port
56143e1f000fSParav Pandit  * @dev:	Pointer to mlx5 IB device
56153e1f000fSParav Pandit  * @port_num:	Zero based port number
56163e1f000fSParav Pandit  *
56173e1f000fSParav Pandit  * mlx5_ib_get_counters_id() Returns counters set id to use for given
56183e1f000fSParav Pandit  * device port combination in switchdev and non switchdev mode of the
56193e1f000fSParav Pandit  * parent device.
56203e1f000fSParav Pandit  */
56213e1f000fSParav Pandit u16 mlx5_ib_get_counters_id(struct mlx5_ib_dev *dev, u8 port_num)
56223e1f000fSParav Pandit {
56233e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts = get_counters(dev, port_num);
56243e1f000fSParav Pandit 
56253e1f000fSParav Pandit 	return cnts->set_id;
56263e1f000fSParav Pandit }
56273e1f000fSParav Pandit 
56280ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
56290ad17a8fSMark Bloch 						    u8 port_num)
56300ad17a8fSMark Bloch {
56317c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
56323e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts;
56333e1f000fSParav Pandit 	bool is_switchdev = is_mdev_switchdev_mode(dev->mdev);
56340ad17a8fSMark Bloch 
56353e1f000fSParav Pandit 	if ((is_switchdev && port_num) || (!is_switchdev && !port_num))
56360ad17a8fSMark Bloch 		return NULL;
56370ad17a8fSMark Bloch 
56383e1f000fSParav Pandit 	cnts = get_counters(dev, port_num - 1);
56393e1f000fSParav Pandit 
56405dcecbc9SParav Pandit 	return rdma_alloc_hw_stats_struct(cnts->names,
56415dcecbc9SParav Pandit 					  cnts->num_q_counters +
56425dcecbc9SParav Pandit 					  cnts->num_cong_counters +
56435dcecbc9SParav Pandit 					  cnts->num_ext_ppcnt_counters,
56440ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
56450ad17a8fSMark Bloch }
56460ad17a8fSMark Bloch 
5647aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
56485dcecbc9SParav Pandit 				    const struct mlx5_ib_counters *cnts,
5649318d535cSMark Zhang 				    struct rdma_hw_stats *stats,
5650318d535cSMark Zhang 				    u16 set_id)
5651e1f24a79SParav Pandit {
565266247fbbSLeon Romanovsky 	u32 out[MLX5_ST_SZ_DW(query_q_counter_out)] = {};
565366247fbbSLeon Romanovsky 	u32 in[MLX5_ST_SZ_DW(query_q_counter_in)] = {};
5654e1f24a79SParav Pandit 	__be32 val;
5655e1f24a79SParav Pandit 	int ret, i;
5656e1f24a79SParav Pandit 
565766247fbbSLeon Romanovsky 	MLX5_SET(query_q_counter_in, in, opcode, MLX5_CMD_OP_QUERY_Q_COUNTER);
565866247fbbSLeon Romanovsky 	MLX5_SET(query_q_counter_in, in, counter_set_id, set_id);
565966247fbbSLeon Romanovsky 	ret = mlx5_cmd_exec_inout(mdev, query_q_counter, in, out);
5660e1f24a79SParav Pandit 	if (ret)
566166247fbbSLeon Romanovsky 		return ret;
5662e1f24a79SParav Pandit 
56635dcecbc9SParav Pandit 	for (i = 0; i < cnts->num_q_counters; i++) {
566466247fbbSLeon Romanovsky 		val = *(__be32 *)((void *)out + cnts->offsets[i]);
5665e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
5666e1f24a79SParav Pandit 	}
5667e1f24a79SParav Pandit 
566866247fbbSLeon Romanovsky 	return 0;
5669e1f24a79SParav Pandit }
5670e1f24a79SParav Pandit 
56719f876f3dSTalat Batheesh static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
56725dcecbc9SParav Pandit 					    const struct mlx5_ib_counters *cnts,
56739f876f3dSTalat Batheesh 					    struct rdma_hw_stats *stats)
56749f876f3dSTalat Batheesh {
56755dcecbc9SParav Pandit 	int offset = cnts->num_q_counters + cnts->num_cong_counters;
56769f876f3dSTalat Batheesh 	int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
56779f876f3dSTalat Batheesh 	int ret, i;
56789f876f3dSTalat Batheesh 	void *out;
56799f876f3dSTalat Batheesh 
56809f876f3dSTalat Batheesh 	out = kvzalloc(sz, GFP_KERNEL);
56819f876f3dSTalat Batheesh 	if (!out)
56829f876f3dSTalat Batheesh 		return -ENOMEM;
56839f876f3dSTalat Batheesh 
56849f876f3dSTalat Batheesh 	ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
56859f876f3dSTalat Batheesh 	if (ret)
56869f876f3dSTalat Batheesh 		goto free;
56879f876f3dSTalat Batheesh 
56885dcecbc9SParav Pandit 	for (i = 0; i < cnts->num_ext_ppcnt_counters; i++)
56899f876f3dSTalat Batheesh 		stats->value[i + offset] =
56909f876f3dSTalat Batheesh 			be64_to_cpup((__be64 *)(out +
56915dcecbc9SParav Pandit 				    cnts->offsets[i + offset]));
56929f876f3dSTalat Batheesh free:
56939f876f3dSTalat Batheesh 	kvfree(out);
56949f876f3dSTalat Batheesh 	return ret;
56959f876f3dSTalat Batheesh }
56969f876f3dSTalat Batheesh 
56970ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
56980ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
56997c16f477SKamal Heib 				u8 port_num, int index)
57000ad17a8fSMark Bloch {
57010ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
57023e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts = get_counters(dev, port_num - 1);
5703aac4492eSDaniel Jurgens 	struct mlx5_core_dev *mdev;
5704e1f24a79SParav Pandit 	int ret, num_counters;
5705aac4492eSDaniel Jurgens 	u8 mdev_port_num;
57060ad17a8fSMark Bloch 
57077c16f477SKamal Heib 	if (!stats)
5708e1f24a79SParav Pandit 		return -EINVAL;
57090ad17a8fSMark Bloch 
57105dcecbc9SParav Pandit 	num_counters = cnts->num_q_counters +
57115dcecbc9SParav Pandit 		       cnts->num_cong_counters +
57125dcecbc9SParav Pandit 		       cnts->num_ext_ppcnt_counters;
5713aac4492eSDaniel Jurgens 
5714aac4492eSDaniel Jurgens 	/* q_counters are per IB device, query the master mdev */
57155dcecbc9SParav Pandit 	ret = mlx5_ib_query_q_counters(dev->mdev, cnts, stats, cnts->set_id);
57160ad17a8fSMark Bloch 	if (ret)
5717e1f24a79SParav Pandit 		return ret;
57180ad17a8fSMark Bloch 
57199f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
57205dcecbc9SParav Pandit 		ret =  mlx5_ib_query_ext_ppcnt_counters(dev, cnts, stats);
57219f876f3dSTalat Batheesh 		if (ret)
57229f876f3dSTalat Batheesh 			return ret;
57239f876f3dSTalat Batheesh 	}
57249f876f3dSTalat Batheesh 
5725e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5726aac4492eSDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5727aac4492eSDaniel Jurgens 						    &mdev_port_num);
5728aac4492eSDaniel Jurgens 		if (!mdev) {
5729aac4492eSDaniel Jurgens 			/* If port is not affiliated yet, its in down state
5730aac4492eSDaniel Jurgens 			 * which doesn't have any counters yet, so it would be
5731aac4492eSDaniel Jurgens 			 * zero. So no need to read from the HCA.
5732aac4492eSDaniel Jurgens 			 */
5733aac4492eSDaniel Jurgens 			goto done;
5734aac4492eSDaniel Jurgens 		}
573571a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
573671a0ff65SMajd Dibbiny 						   stats->value +
57375dcecbc9SParav Pandit 						   cnts->num_q_counters,
57385dcecbc9SParav Pandit 						   cnts->num_cong_counters,
57395dcecbc9SParav Pandit 						   cnts->offsets +
57405dcecbc9SParav Pandit 						   cnts->num_q_counters);
5741aac4492eSDaniel Jurgens 
5742aac4492eSDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
5743e1f24a79SParav Pandit 		if (ret)
5744e1f24a79SParav Pandit 			return ret;
57450ad17a8fSMark Bloch 	}
57467c16f477SKamal Heib 
5747aac4492eSDaniel Jurgens done:
5748e1f24a79SParav Pandit 	return num_counters;
57490ad17a8fSMark Bloch }
57500ad17a8fSMark Bloch 
575118d422ceSMark Zhang static struct rdma_hw_stats *
575218d422ceSMark Zhang mlx5_ib_counter_alloc_stats(struct rdma_counter *counter)
575318d422ceSMark Zhang {
575418d422ceSMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
57555dcecbc9SParav Pandit 	const struct mlx5_ib_counters *cnts =
57563e1f000fSParav Pandit 		get_counters(dev, counter->port - 1);
575718d422ceSMark Zhang 
57585dcecbc9SParav Pandit 	return rdma_alloc_hw_stats_struct(cnts->names,
5759ec16b6bbSMark Zhang 					  cnts->num_q_counters +
5760ec16b6bbSMark Zhang 					  cnts->num_cong_counters +
5761ec16b6bbSMark Zhang 					  cnts->num_ext_ppcnt_counters,
576218d422ceSMark Zhang 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
576318d422ceSMark Zhang }
576418d422ceSMark Zhang 
576518d422ceSMark Zhang static int mlx5_ib_counter_update_stats(struct rdma_counter *counter)
576618d422ceSMark Zhang {
576718d422ceSMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
57683e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts =
57693e1f000fSParav Pandit 		get_counters(dev, counter->port - 1);
577018d422ceSMark Zhang 
57715dcecbc9SParav Pandit 	return mlx5_ib_query_q_counters(dev->mdev, cnts,
577218d422ceSMark Zhang 					counter->stats, counter->id);
577318d422ceSMark Zhang }
577418d422ceSMark Zhang 
577566247fbbSLeon Romanovsky static int mlx5_ib_counter_dealloc(struct rdma_counter *counter)
577666247fbbSLeon Romanovsky {
577766247fbbSLeon Romanovsky 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
577866247fbbSLeon Romanovsky 	u32 in[MLX5_ST_SZ_DW(dealloc_q_counter_in)] = {};
577966247fbbSLeon Romanovsky 
578066247fbbSLeon Romanovsky 	if (!counter->id)
578166247fbbSLeon Romanovsky 		return 0;
578266247fbbSLeon Romanovsky 
578366247fbbSLeon Romanovsky 	MLX5_SET(dealloc_q_counter_in, in, opcode,
578466247fbbSLeon Romanovsky 		 MLX5_CMD_OP_DEALLOC_Q_COUNTER);
578566247fbbSLeon Romanovsky 	MLX5_SET(dealloc_q_counter_in, in, counter_set_id, counter->id);
578666247fbbSLeon Romanovsky 	return mlx5_cmd_exec_in(dev->mdev, dealloc_q_counter, in);
578766247fbbSLeon Romanovsky }
578866247fbbSLeon Romanovsky 
578945842fc6SMark Zhang static int mlx5_ib_counter_bind_qp(struct rdma_counter *counter,
579045842fc6SMark Zhang 				   struct ib_qp *qp)
579145842fc6SMark Zhang {
579245842fc6SMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
579345842fc6SMark Zhang 	int err;
579445842fc6SMark Zhang 
579545842fc6SMark Zhang 	if (!counter->id) {
5796bfd745f8SLeon Romanovsky 		u32 out[MLX5_ST_SZ_DW(alloc_q_counter_out)] = {};
5797bfd745f8SLeon Romanovsky 		u32 in[MLX5_ST_SZ_DW(alloc_q_counter_in)] = {};
5798bfd745f8SLeon Romanovsky 
5799bfd745f8SLeon Romanovsky 		MLX5_SET(alloc_q_counter_in, in, opcode,
5800bfd745f8SLeon Romanovsky 			 MLX5_CMD_OP_ALLOC_Q_COUNTER);
5801bfd745f8SLeon Romanovsky 		MLX5_SET(alloc_q_counter_in, in, uid, MLX5_SHARED_RESOURCE_UID);
5802bfd745f8SLeon Romanovsky 		err = mlx5_cmd_exec_inout(dev->mdev, alloc_q_counter, in, out);
580345842fc6SMark Zhang 		if (err)
580445842fc6SMark Zhang 			return err;
5805bfd745f8SLeon Romanovsky 		counter->id =
5806bfd745f8SLeon Romanovsky 			MLX5_GET(alloc_q_counter_out, out, counter_set_id);
580745842fc6SMark Zhang 	}
580845842fc6SMark Zhang 
580945842fc6SMark Zhang 	err = mlx5_ib_qp_set_counter(qp, counter);
581045842fc6SMark Zhang 	if (err)
581145842fc6SMark Zhang 		goto fail_set_counter;
581245842fc6SMark Zhang 
581345842fc6SMark Zhang 	return 0;
581445842fc6SMark Zhang 
581545842fc6SMark Zhang fail_set_counter:
581666247fbbSLeon Romanovsky 	mlx5_ib_counter_dealloc(counter);
581745842fc6SMark Zhang 	counter->id = 0;
581845842fc6SMark Zhang 
581945842fc6SMark Zhang 	return err;
582045842fc6SMark Zhang }
582145842fc6SMark Zhang 
582245842fc6SMark Zhang static int mlx5_ib_counter_unbind_qp(struct ib_qp *qp)
582345842fc6SMark Zhang {
582445842fc6SMark Zhang 	return mlx5_ib_qp_set_counter(qp, NULL);
582545842fc6SMark Zhang }
582645842fc6SMark Zhang 
5827f6a8a19bSDenis Drozdov static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5828693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
5829f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
5830693dfd5aSErez Shitrit {
5831693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
5832f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
5833693dfd5aSErez Shitrit 
5834f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
5835693dfd5aSErez Shitrit }
5836693dfd5aSErez Shitrit 
5837fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5838fe248c3aSMaor Gottlieb {
583909b0965eSGreg Kroah-Hartman 	if (!dev->delay_drop.dir_debugfs)
5840fe248c3aSMaor Gottlieb 		return;
584109b0965eSGreg Kroah-Hartman 	debugfs_remove_recursive(dev->delay_drop.dir_debugfs);
584209b0965eSGreg Kroah-Hartman 	dev->delay_drop.dir_debugfs = NULL;
5843fe248c3aSMaor Gottlieb }
5844fe248c3aSMaor Gottlieb 
584503404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
584603404e8aSMaor Gottlieb {
584703404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
584803404e8aSMaor Gottlieb 		return;
584903404e8aSMaor Gottlieb 
585003404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
5851fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5852fe248c3aSMaor Gottlieb }
5853fe248c3aSMaor Gottlieb 
5854fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5855fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
5856fe248c3aSMaor Gottlieb {
5857fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5858fe248c3aSMaor Gottlieb 	char lbuf[20];
5859fe248c3aSMaor Gottlieb 	int len;
5860fe248c3aSMaor Gottlieb 
5861fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5862fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
5863fe248c3aSMaor Gottlieb }
5864fe248c3aSMaor Gottlieb 
5865fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5866fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
5867fe248c3aSMaor Gottlieb {
5868fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5869fe248c3aSMaor Gottlieb 	u32 timeout;
5870fe248c3aSMaor Gottlieb 	u32 var;
5871fe248c3aSMaor Gottlieb 
5872fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
5873fe248c3aSMaor Gottlieb 		return -EFAULT;
5874fe248c3aSMaor Gottlieb 
5875fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5876fe248c3aSMaor Gottlieb 			1000);
5877fe248c3aSMaor Gottlieb 	if (timeout != var)
5878fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5879fe248c3aSMaor Gottlieb 			    timeout);
5880fe248c3aSMaor Gottlieb 
5881fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
5882fe248c3aSMaor Gottlieb 
5883fe248c3aSMaor Gottlieb 	return count;
5884fe248c3aSMaor Gottlieb }
5885fe248c3aSMaor Gottlieb 
5886fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
5887fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
5888fe248c3aSMaor Gottlieb 	.open	= simple_open,
5889fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
5890fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
5891fe248c3aSMaor Gottlieb };
5892fe248c3aSMaor Gottlieb 
589309b0965eSGreg Kroah-Hartman static void delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5894fe248c3aSMaor Gottlieb {
589509b0965eSGreg Kroah-Hartman 	struct dentry *root;
5896fe248c3aSMaor Gottlieb 
5897fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
589809b0965eSGreg Kroah-Hartman 		return;
5899fe248c3aSMaor Gottlieb 
590009b0965eSGreg Kroah-Hartman 	root = debugfs_create_dir("delay_drop", dev->mdev->priv.dbg_root);
590109b0965eSGreg Kroah-Hartman 	dev->delay_drop.dir_debugfs = root;
5902fe248c3aSMaor Gottlieb 
590309b0965eSGreg Kroah-Hartman 	debugfs_create_atomic_t("num_timeout_events", 0400, root,
5904fe248c3aSMaor Gottlieb 				&dev->delay_drop.events_cnt);
590509b0965eSGreg Kroah-Hartman 	debugfs_create_atomic_t("num_rqs", 0400, root,
5906fe248c3aSMaor Gottlieb 				&dev->delay_drop.rqs_cnt);
590709b0965eSGreg Kroah-Hartman 	debugfs_create_file("timeout", 0600, root, &dev->delay_drop,
5908fe248c3aSMaor Gottlieb 			    &fops_delay_drop_timeout);
590903404e8aSMaor Gottlieb }
591003404e8aSMaor Gottlieb 
591103404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
591203404e8aSMaor Gottlieb {
591303404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
591403404e8aSMaor Gottlieb 		return;
591503404e8aSMaor Gottlieb 
591603404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
591703404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
591803404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
591903404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
592003404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
5921fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
5922fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
5923fe248c3aSMaor Gottlieb 
592409b0965eSGreg Kroah-Hartman 	delay_drop_debugfs_init(dev);
592503404e8aSMaor Gottlieb }
592603404e8aSMaor Gottlieb 
592732f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
592832f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
592932f69e4bSDaniel Jurgens {
593032f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
593132f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
593232f69e4bSDaniel Jurgens 	int comps;
593332f69e4bSDaniel Jurgens 	int err;
593432f69e4bSDaniel Jurgens 	int i;
593532f69e4bSDaniel Jurgens 
59369dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
59379dc4cfffSLeon Romanovsky 
5938a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5939a9e546e7SParav Pandit 
594032f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
594132f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
594232f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
594332f69e4bSDaniel Jurgens 		return;
594432f69e4bSDaniel Jurgens 	}
5945df097a27SSaeed Mahameed 
594632f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
594732f69e4bSDaniel Jurgens 
594832f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
594923eaf3b5SLeon Romanovsky 	if (mpi->mdev_events.notifier_call)
595023eaf3b5SLeon Romanovsky 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
595123eaf3b5SLeon Romanovsky 	mpi->mdev_events.notifier_call = NULL;
595232f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
595332f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
595432f69e4bSDaniel Jurgens 
595532f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
595632f69e4bSDaniel Jurgens 	if (comps) {
595732f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
595832f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
595932f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
596032f69e4bSDaniel Jurgens 
596132f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
596232f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
596332f69e4bSDaniel Jurgens 
596432f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
596532f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
596632f69e4bSDaniel Jurgens 	}
596732f69e4bSDaniel Jurgens 
596832f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
596932f69e4bSDaniel Jurgens 
597032f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
597132f69e4bSDaniel Jurgens 
597232f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
597332f69e4bSDaniel Jurgens 
597432f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
597532f69e4bSDaniel Jurgens 
597632f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
597732f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
597832f69e4bSDaniel Jurgens 	 * it back to the list.
597932f69e4bSDaniel Jurgens 	 */
598032f69e4bSDaniel Jurgens 	if (err)
598132f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
598232f69e4bSDaniel Jurgens 			    port_num + 1);
598332f69e4bSDaniel Jurgens 
598495579e78SMark Bloch 	ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
598532f69e4bSDaniel Jurgens }
598632f69e4bSDaniel Jurgens 
598732f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
598832f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
598932f69e4bSDaniel Jurgens {
599032f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
599132f69e4bSDaniel Jurgens 	int err;
599232f69e4bSDaniel Jurgens 
59939dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
59949dc4cfffSLeon Romanovsky 
599532f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
599632f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
59972577188eSQing Huang 		mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
599832f69e4bSDaniel Jurgens 			    port_num + 1);
599932f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
600032f69e4bSDaniel Jurgens 		return false;
600132f69e4bSDaniel Jurgens 	}
600232f69e4bSDaniel Jurgens 
600332f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
600432f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
6005df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
600632f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
600732f69e4bSDaniel Jurgens 
600832f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
600932f69e4bSDaniel Jurgens 	if (err)
601032f69e4bSDaniel Jurgens 		goto unbind;
601132f69e4bSDaniel Jurgens 
601232f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
601332f69e4bSDaniel Jurgens 	if (err)
601432f69e4bSDaniel Jurgens 		goto unbind;
601532f69e4bSDaniel Jurgens 
601632f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
601732f69e4bSDaniel Jurgens 	if (err) {
601832f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
601932f69e4bSDaniel Jurgens 			    port_num + 1);
602032f69e4bSDaniel Jurgens 		goto unbind;
602132f69e4bSDaniel Jurgens 	}
602232f69e4bSDaniel Jurgens 
6023df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
6024df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
6025df097a27SSaeed Mahameed 
602673eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
6027a9e546e7SParav Pandit 
602832f69e4bSDaniel Jurgens 	return true;
602932f69e4bSDaniel Jurgens 
603032f69e4bSDaniel Jurgens unbind:
603132f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
603232f69e4bSDaniel Jurgens 	return false;
603332f69e4bSDaniel Jurgens }
603432f69e4bSDaniel Jurgens 
603532f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
603632f69e4bSDaniel Jurgens {
603732f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
603832f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
603932f69e4bSDaniel Jurgens 							  port_num + 1);
604032f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
604132f69e4bSDaniel Jurgens 	int err;
604232f69e4bSDaniel Jurgens 	int i;
604332f69e4bSDaniel Jurgens 
604432f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
604532f69e4bSDaniel Jurgens 		return 0;
604632f69e4bSDaniel Jurgens 
604732f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
604832f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
604932f69e4bSDaniel Jurgens 	if (err)
605032f69e4bSDaniel Jurgens 		return err;
605132f69e4bSDaniel Jurgens 
605232f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
605332f69e4bSDaniel Jurgens 	if (err)
605432f69e4bSDaniel Jurgens 		return err;
605532f69e4bSDaniel Jurgens 
605632f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
605732f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
605832f69e4bSDaniel Jurgens 		bool bound = false;
605932f69e4bSDaniel Jurgens 
606032f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
606132f69e4bSDaniel Jurgens 		if (i == port_num) {
606232f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
606332f69e4bSDaniel Jurgens 			if (!mpi) {
606432f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
606532f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
606632f69e4bSDaniel Jurgens 				return -ENOMEM;
606732f69e4bSDaniel Jurgens 			}
606832f69e4bSDaniel Jurgens 
606932f69e4bSDaniel Jurgens 			mpi->is_master = true;
607032f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
607132f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
607232f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
607332f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
607432f69e4bSDaniel Jurgens 			mpi = NULL;
607532f69e4bSDaniel Jurgens 			continue;
607632f69e4bSDaniel Jurgens 		}
607732f69e4bSDaniel Jurgens 
607832f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
607932f69e4bSDaniel Jurgens 				    list) {
608032f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
608132f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
608232f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
608332f69e4bSDaniel Jurgens 			}
608432f69e4bSDaniel Jurgens 
608532f69e4bSDaniel Jurgens 			if (bound) {
6086c42260f1SVu Pham 				dev_dbg(mpi->mdev->device,
6087c42260f1SVu Pham 					"removing port from unaffiliated list.\n");
608832f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
608932f69e4bSDaniel Jurgens 				list_del(&mpi->list);
609032f69e4bSDaniel Jurgens 				break;
609132f69e4bSDaniel Jurgens 			}
609232f69e4bSDaniel Jurgens 		}
609332f69e4bSDaniel Jurgens 		if (!bound) {
609432f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
609532f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
609632f69e4bSDaniel Jurgens 				    i + 1);
609732f69e4bSDaniel Jurgens 		}
609832f69e4bSDaniel Jurgens 	}
609932f69e4bSDaniel Jurgens 
610032f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
610132f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
610232f69e4bSDaniel Jurgens 	return err;
610332f69e4bSDaniel Jurgens }
610432f69e4bSDaniel Jurgens 
610532f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
610632f69e4bSDaniel Jurgens {
610732f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
610832f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
610932f69e4bSDaniel Jurgens 							  port_num + 1);
611032f69e4bSDaniel Jurgens 	int i;
611132f69e4bSDaniel Jurgens 
611232f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
611332f69e4bSDaniel Jurgens 		return;
611432f69e4bSDaniel Jurgens 
611532f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
611632f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
611732f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
611832f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
611932f69e4bSDaniel Jurgens 			if (i == port_num) {
612032f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
612132f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
612232f69e4bSDaniel Jurgens 			} else {
612332f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
612432f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
612532f69e4bSDaniel Jurgens 			}
612632f69e4bSDaniel Jurgens 		}
612732f69e4bSDaniel Jurgens 	}
612832f69e4bSDaniel Jurgens 
612932f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
613032f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
613132f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
613232f69e4bSDaniel Jurgens 
613332f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
613432f69e4bSDaniel Jurgens }
613532f69e4bSDaniel Jurgens 
6136342ee59dSYishai Hadas static int mmap_obj_cleanup(struct ib_uobject *uobject,
61377be76befSYishai Hadas 			    enum rdma_remove_reason why,
61387be76befSYishai Hadas 			    struct uverbs_attr_bundle *attrs)
61397be76befSYishai Hadas {
61407be76befSYishai Hadas 	struct mlx5_user_mmap_entry *obj = uobject->object;
61417be76befSYishai Hadas 
61427be76befSYishai Hadas 	rdma_user_mmap_entry_remove(&obj->rdma_entry);
61437be76befSYishai Hadas 	return 0;
61447be76befSYishai Hadas }
61457be76befSYishai Hadas 
6146342ee59dSYishai Hadas static int mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext *c,
6147342ee59dSYishai Hadas 					    struct mlx5_user_mmap_entry *entry,
6148342ee59dSYishai Hadas 					    size_t length)
6149342ee59dSYishai Hadas {
6150342ee59dSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
6151342ee59dSYishai Hadas 		&c->ibucontext, &entry->rdma_entry, length,
6152342ee59dSYishai Hadas 		(MLX5_IB_MMAP_OFFSET_START << 16),
6153342ee59dSYishai Hadas 		((MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1));
6154342ee59dSYishai Hadas }
6155342ee59dSYishai Hadas 
61567be76befSYishai Hadas static struct mlx5_user_mmap_entry *
61577be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c)
61587be76befSYishai Hadas {
61597be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
61607be76befSYishai Hadas 	struct mlx5_var_table *var_table;
61617be76befSYishai Hadas 	u32 page_idx;
61627be76befSYishai Hadas 	int err;
61637be76befSYishai Hadas 
61647be76befSYishai Hadas 	var_table = &to_mdev(c->ibucontext.device)->var_table;
61657be76befSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
61667be76befSYishai Hadas 	if (!entry)
61677be76befSYishai Hadas 		return ERR_PTR(-ENOMEM);
61687be76befSYishai Hadas 
61697be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
61707be76befSYishai Hadas 	page_idx = find_first_zero_bit(var_table->bitmap,
61717be76befSYishai Hadas 				       var_table->num_var_hw_entries);
61727be76befSYishai Hadas 	if (page_idx >= var_table->num_var_hw_entries) {
61737be76befSYishai Hadas 		err = -ENOSPC;
61747be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
61757be76befSYishai Hadas 		goto end;
61767be76befSYishai Hadas 	}
61777be76befSYishai Hadas 
61787be76befSYishai Hadas 	set_bit(page_idx, var_table->bitmap);
61797be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
61807be76befSYishai Hadas 
61817be76befSYishai Hadas 	entry->address = var_table->hw_start_addr +
61827be76befSYishai Hadas 				(page_idx * var_table->stride_size);
61837be76befSYishai Hadas 	entry->page_idx = page_idx;
61847be76befSYishai Hadas 	entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR;
61857be76befSYishai Hadas 
6186342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry,
6187342ee59dSYishai Hadas 					       var_table->stride_size);
61887be76befSYishai Hadas 	if (err)
61897be76befSYishai Hadas 		goto err_insert;
61907be76befSYishai Hadas 
61917be76befSYishai Hadas 	return entry;
61927be76befSYishai Hadas 
61937be76befSYishai Hadas err_insert:
61947be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
61957be76befSYishai Hadas 	clear_bit(page_idx, var_table->bitmap);
61967be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
61977be76befSYishai Hadas end:
61987be76befSYishai Hadas 	kfree(entry);
61997be76befSYishai Hadas 	return ERR_PTR(err);
62007be76befSYishai Hadas }
62017be76befSYishai Hadas 
62027be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)(
62037be76befSYishai Hadas 	struct uverbs_attr_bundle *attrs)
62047be76befSYishai Hadas {
62057be76befSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
62067be76befSYishai Hadas 		attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
62077be76befSYishai Hadas 	struct mlx5_ib_ucontext *c;
62087be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
62097be76befSYishai Hadas 	u64 mmap_offset;
62107be76befSYishai Hadas 	u32 length;
62117be76befSYishai Hadas 	int err;
62127be76befSYishai Hadas 
62137be76befSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
62147be76befSYishai Hadas 	if (IS_ERR(c))
62157be76befSYishai Hadas 		return PTR_ERR(c);
62167be76befSYishai Hadas 
62177be76befSYishai Hadas 	entry = alloc_var_entry(c);
62187be76befSYishai Hadas 	if (IS_ERR(entry))
62197be76befSYishai Hadas 		return PTR_ERR(entry);
62207be76befSYishai Hadas 
62217be76befSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
62227be76befSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
62237be76befSYishai Hadas 	uobj->object = entry;
62247be76befSYishai Hadas 
62257be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
62267be76befSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
62277be76befSYishai Hadas 	if (err)
62287be76befSYishai Hadas 		goto err;
62297be76befSYishai Hadas 
62307be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
62317be76befSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
62327be76befSYishai Hadas 	if (err)
62337be76befSYishai Hadas 		goto err;
62347be76befSYishai Hadas 
62357be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
62367be76befSYishai Hadas 			     &length, sizeof(length));
62377be76befSYishai Hadas 	if (err)
62387be76befSYishai Hadas 		goto err;
62397be76befSYishai Hadas 
62407be76befSYishai Hadas 	return 0;
62417be76befSYishai Hadas 
62427be76befSYishai Hadas err:
62437be76befSYishai Hadas 	rdma_user_mmap_entry_remove(&entry->rdma_entry);
62447be76befSYishai Hadas 	return err;
62457be76befSYishai Hadas }
62467be76befSYishai Hadas 
62477be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
62487be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_ALLOC,
62497be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE,
62507be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
62517be76befSYishai Hadas 			UVERBS_ACCESS_NEW,
62527be76befSYishai Hadas 			UA_MANDATORY),
62537be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
62547be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
62557be76befSYishai Hadas 			   UA_MANDATORY),
62567be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
62577be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
62587be76befSYishai Hadas 			   UA_MANDATORY),
62597be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
62607be76befSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
62617be76befSYishai Hadas 			    UA_MANDATORY));
62627be76befSYishai Hadas 
62637be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
62647be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_DESTROY,
62657be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE,
62667be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
62677be76befSYishai Hadas 			UVERBS_ACCESS_DESTROY,
62687be76befSYishai Hadas 			UA_MANDATORY));
62697be76befSYishai Hadas 
62707be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR,
6271342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
62727be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC),
62737be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY));
62747be76befSYishai Hadas 
62757be76befSYishai Hadas static bool var_is_supported(struct ib_device *device)
62767be76befSYishai Hadas {
62777be76befSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(device);
62787be76befSYishai Hadas 
62797be76befSYishai Hadas 	return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
62807be76befSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q);
62817be76befSYishai Hadas }
62827be76befSYishai Hadas 
6283342ee59dSYishai Hadas static struct mlx5_user_mmap_entry *
6284342ee59dSYishai Hadas alloc_uar_entry(struct mlx5_ib_ucontext *c,
6285342ee59dSYishai Hadas 		enum mlx5_ib_uapi_uar_alloc_type alloc_type)
6286342ee59dSYishai Hadas {
6287342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
6288342ee59dSYishai Hadas 	struct mlx5_ib_dev *dev;
6289342ee59dSYishai Hadas 	u32 uar_index;
6290342ee59dSYishai Hadas 	int err;
6291342ee59dSYishai Hadas 
6292342ee59dSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
6293342ee59dSYishai Hadas 	if (!entry)
6294342ee59dSYishai Hadas 		return ERR_PTR(-ENOMEM);
6295342ee59dSYishai Hadas 
6296342ee59dSYishai Hadas 	dev = to_mdev(c->ibucontext.device);
6297342ee59dSYishai Hadas 	err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
6298342ee59dSYishai Hadas 	if (err)
6299342ee59dSYishai Hadas 		goto end;
6300342ee59dSYishai Hadas 
6301342ee59dSYishai Hadas 	entry->page_idx = uar_index;
6302342ee59dSYishai Hadas 	entry->address = uar_index2paddress(dev, uar_index);
6303342ee59dSYishai Hadas 	if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
6304342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC;
6305342ee59dSYishai Hadas 	else
6306342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_NC;
6307342ee59dSYishai Hadas 
6308342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry, PAGE_SIZE);
6309342ee59dSYishai Hadas 	if (err)
6310342ee59dSYishai Hadas 		goto err_insert;
6311342ee59dSYishai Hadas 
6312342ee59dSYishai Hadas 	return entry;
6313342ee59dSYishai Hadas 
6314342ee59dSYishai Hadas err_insert:
6315342ee59dSYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, uar_index);
6316342ee59dSYishai Hadas end:
6317342ee59dSYishai Hadas 	kfree(entry);
6318342ee59dSYishai Hadas 	return ERR_PTR(err);
6319342ee59dSYishai Hadas }
6320342ee59dSYishai Hadas 
6321342ee59dSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)(
6322342ee59dSYishai Hadas 	struct uverbs_attr_bundle *attrs)
6323342ee59dSYishai Hadas {
6324342ee59dSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
6325342ee59dSYishai Hadas 		attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
6326342ee59dSYishai Hadas 	enum mlx5_ib_uapi_uar_alloc_type alloc_type;
6327342ee59dSYishai Hadas 	struct mlx5_ib_ucontext *c;
6328342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
6329342ee59dSYishai Hadas 	u64 mmap_offset;
6330342ee59dSYishai Hadas 	u32 length;
6331342ee59dSYishai Hadas 	int err;
6332342ee59dSYishai Hadas 
6333342ee59dSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
6334342ee59dSYishai Hadas 	if (IS_ERR(c))
6335342ee59dSYishai Hadas 		return PTR_ERR(c);
6336342ee59dSYishai Hadas 
6337342ee59dSYishai Hadas 	err = uverbs_get_const(&alloc_type, attrs,
6338342ee59dSYishai Hadas 			       MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE);
6339342ee59dSYishai Hadas 	if (err)
6340342ee59dSYishai Hadas 		return err;
6341342ee59dSYishai Hadas 
6342342ee59dSYishai Hadas 	if (alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF &&
6343342ee59dSYishai Hadas 	    alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_NC)
6344342ee59dSYishai Hadas 		return -EOPNOTSUPP;
6345342ee59dSYishai Hadas 
6346342ee59dSYishai Hadas 	if (!to_mdev(c->ibucontext.device)->wc_support &&
6347342ee59dSYishai Hadas 	    alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
6348342ee59dSYishai Hadas 		return -EOPNOTSUPP;
6349342ee59dSYishai Hadas 
6350342ee59dSYishai Hadas 	entry = alloc_uar_entry(c, alloc_type);
6351342ee59dSYishai Hadas 	if (IS_ERR(entry))
6352342ee59dSYishai Hadas 		return PTR_ERR(entry);
6353342ee59dSYishai Hadas 
6354342ee59dSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
6355342ee59dSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
6356342ee59dSYishai Hadas 	uobj->object = entry;
6357342ee59dSYishai Hadas 
6358342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
6359342ee59dSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
6360342ee59dSYishai Hadas 	if (err)
6361342ee59dSYishai Hadas 		goto err;
6362342ee59dSYishai Hadas 
6363342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
6364342ee59dSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
6365342ee59dSYishai Hadas 	if (err)
6366342ee59dSYishai Hadas 		goto err;
6367342ee59dSYishai Hadas 
6368342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
6369342ee59dSYishai Hadas 			     &length, sizeof(length));
6370342ee59dSYishai Hadas 	if (err)
6371342ee59dSYishai Hadas 		goto err;
6372342ee59dSYishai Hadas 
6373342ee59dSYishai Hadas 	return 0;
6374342ee59dSYishai Hadas 
6375342ee59dSYishai Hadas err:
6376342ee59dSYishai Hadas 	rdma_user_mmap_entry_remove(&entry->rdma_entry);
6377342ee59dSYishai Hadas 	return err;
6378342ee59dSYishai Hadas }
6379342ee59dSYishai Hadas 
6380342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
6381342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_ALLOC,
6382342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE,
6383342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
6384342ee59dSYishai Hadas 			UVERBS_ACCESS_NEW,
6385342ee59dSYishai Hadas 			UA_MANDATORY),
6386342ee59dSYishai Hadas 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE,
6387342ee59dSYishai Hadas 			     enum mlx5_ib_uapi_uar_alloc_type,
6388342ee59dSYishai Hadas 			     UA_MANDATORY),
6389342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
6390342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
6391342ee59dSYishai Hadas 			   UA_MANDATORY),
6392342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
6393342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
6394342ee59dSYishai Hadas 			   UA_MANDATORY),
6395342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
6396342ee59dSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
6397342ee59dSYishai Hadas 			    UA_MANDATORY));
6398342ee59dSYishai Hadas 
6399342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
6400342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_DESTROY,
6401342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_DESTROY_HANDLE,
6402342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
6403342ee59dSYishai Hadas 			UVERBS_ACCESS_DESTROY,
6404342ee59dSYishai Hadas 			UA_MANDATORY));
6405342ee59dSYishai Hadas 
6406342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_UAR,
6407342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
6408342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_ALLOC),
6409342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_DESTROY));
6410342ee59dSYishai Hadas 
64119a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
64129a119cd5SJason Gunthorpe 	mlx5_ib_dm,
64139a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
641424da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
64159a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
641624da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
641783bb4442SJason Gunthorpe 			    UA_MANDATORY),
64189a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
641924da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
64203b113a1eSAriel Levkovich 			    UA_OPTIONAL),
64213b113a1eSAriel Levkovich 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
64223b113a1eSAriel Levkovich 			     enum mlx5_ib_uapi_dm_type,
64233b113a1eSAriel Levkovich 			     UA_OPTIONAL));
642424da0016SAriel Levkovich 
64259a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
64269a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
64279a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
6428c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
6429bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
6430bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
6431c6475a0bSAviad Yehezkel 
64320cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
643336e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
64340cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
643530f2fe40SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_qos_defs),
64368c84660bSMatan Barak 
64370cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
64380cbf432dSJason Gunthorpe 				&mlx5_ib_flow_action),
64390cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
64407be76befSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR,
64417be76befSYishai Hadas 				UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)),
6442342ee59dSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_UAR),
64430cbf432dSJason Gunthorpe 	{}
64440cbf432dSJason Gunthorpe };
64458c84660bSMatan Barak 
64461a1e03dcSRaed Salem static int mlx5_ib_read_counters(struct ib_counters *counters,
64471a1e03dcSRaed Salem 				 struct ib_counters_read_attr *read_attr,
64481a1e03dcSRaed Salem 				 struct uverbs_attr_bundle *attrs)
64491a1e03dcSRaed Salem {
64501a1e03dcSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
64511a1e03dcSRaed Salem 	struct mlx5_read_counters_attr mread_attr = {};
64521a1e03dcSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc;
64531a1e03dcSRaed Salem 	int ret, i;
64541a1e03dcSRaed Salem 
64551a1e03dcSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
64561a1e03dcSRaed Salem 	if (mcounters->cntrs_max_index > read_attr->ncounters) {
64571a1e03dcSRaed Salem 		ret = -EINVAL;
64581a1e03dcSRaed Salem 		goto err_bound;
64591a1e03dcSRaed Salem 	}
64601a1e03dcSRaed Salem 
64611a1e03dcSRaed Salem 	mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
64621a1e03dcSRaed Salem 				 GFP_KERNEL);
64631a1e03dcSRaed Salem 	if (!mread_attr.out) {
64641a1e03dcSRaed Salem 		ret = -ENOMEM;
64651a1e03dcSRaed Salem 		goto err_bound;
64661a1e03dcSRaed Salem 	}
64671a1e03dcSRaed Salem 
64681a1e03dcSRaed Salem 	mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
64691a1e03dcSRaed Salem 	mread_attr.flags = read_attr->flags;
64701a1e03dcSRaed Salem 	ret = mcounters->read_counters(counters->device, &mread_attr);
64711a1e03dcSRaed Salem 	if (ret)
64721a1e03dcSRaed Salem 		goto err_read;
64731a1e03dcSRaed Salem 
64741a1e03dcSRaed Salem 	/* do the pass over the counters data array to assign according to the
64751a1e03dcSRaed Salem 	 * descriptions and indexing pairs
64761a1e03dcSRaed Salem 	 */
64771a1e03dcSRaed Salem 	desc = mcounters->counters_data;
64781a1e03dcSRaed Salem 	for (i = 0; i < mcounters->ncounters; i++)
64791a1e03dcSRaed Salem 		read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
64801a1e03dcSRaed Salem 
64811a1e03dcSRaed Salem err_read:
64821a1e03dcSRaed Salem 	kfree(mread_attr.out);
64831a1e03dcSRaed Salem err_bound:
64841a1e03dcSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
64851a1e03dcSRaed Salem 	return ret;
64861a1e03dcSRaed Salem }
64871a1e03dcSRaed Salem 
6488b29e2a13SRaed Salem static int mlx5_ib_destroy_counters(struct ib_counters *counters)
6489b29e2a13SRaed Salem {
6490b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
6491b29e2a13SRaed Salem 
64923b3233fbSRaed Salem 	counters_clear_description(counters);
64933b3233fbSRaed Salem 	if (mcounters->hw_cntrs_hndl)
64943b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(counters->device)->mdev,
64953b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
64963b3233fbSRaed Salem 
6497b29e2a13SRaed Salem 	kfree(mcounters);
6498b29e2a13SRaed Salem 
6499b29e2a13SRaed Salem 	return 0;
6500b29e2a13SRaed Salem }
6501b29e2a13SRaed Salem 
6502b29e2a13SRaed Salem static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
6503b29e2a13SRaed Salem 						   struct uverbs_attr_bundle *attrs)
6504b29e2a13SRaed Salem {
6505b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters;
6506b29e2a13SRaed Salem 
6507b29e2a13SRaed Salem 	mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
6508b29e2a13SRaed Salem 	if (!mcounters)
6509b29e2a13SRaed Salem 		return ERR_PTR(-ENOMEM);
6510b29e2a13SRaed Salem 
65113b3233fbSRaed Salem 	mutex_init(&mcounters->mcntrs_mutex);
65123b3233fbSRaed Salem 
6513b29e2a13SRaed Salem 	return &mcounters->ibcntrs;
6514b29e2a13SRaed Salem }
6515b29e2a13SRaed Salem 
6516fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
6517e126ba97SEli Cohen {
651832f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
6519806b101bSJason Gunthorpe 	WARN_ON(!xa_empty(&dev->odp_mkeys));
6520806b101bSJason Gunthorpe 	cleanup_srcu_struct(&dev->odp_srcu);
65214056b12eSAriel Levkovich 
652250211ec9SJason Gunthorpe 	WARN_ON(!xa_empty(&dev->sig_mrs));
65234056b12eSAriel Levkovich 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
652416c1975fSMark Bloch }
652516c1975fSMark Bloch 
6526fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
652716c1975fSMark Bloch {
652816c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
6529e126ba97SEli Cohen 	int err;
653032f69e4bSDaniel Jurgens 	int i;
6531e126ba97SEli Cohen 
653232f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
653332f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
653495579e78SMark Bloch 		rwlock_init(&dev->port[i].roce.netdev_lock);
6535d3b5cc1cSMark Bloch 		dev->port[i].roce.dev = dev;
6536d3b5cc1cSMark Bloch 		dev->port[i].roce.native_port_num = i + 1;
6537d3b5cc1cSMark Bloch 		dev->port[i].roce.last_port_state = IB_PORT_DOWN;
653832f69e4bSDaniel Jurgens 	}
653932f69e4bSDaniel Jurgens 
654000815752SMoni Shoua 	mlx5_ib_internal_fill_odp_caps(dev);
654100815752SMoni Shoua 
654232f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
6543e126ba97SEli Cohen 	if (err)
6544da796ccbSMark Bloch 		return err;
6545e126ba97SEli Cohen 
6546a989ea01SMark Bloch 	err = set_has_smi_cap(dev);
6547a989ea01SMark Bloch 	if (err)
6548a989ea01SMark Bloch 		return err;
6549e126ba97SEli Cohen 
655032f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
655132f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
655232f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
655332f69e4bSDaniel Jurgens 			if (err)
655432f69e4bSDaniel Jurgens 				break;
655532f69e4bSDaniel Jurgens 		}
655632f69e4bSDaniel Jurgens 	} else {
655732f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
655832f69e4bSDaniel Jurgens 	}
655932f69e4bSDaniel Jurgens 	if (err)
656032f69e4bSDaniel Jurgens 		goto err_mp;
656132f69e4bSDaniel Jurgens 
65621b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
6563e126ba97SEli Cohen 		get_ext_port_caps(dev);
6564e126ba97SEli Cohen 
6565e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
6566c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
6567e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
6568f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
6569c42260f1SVu Pham 	dev->ib_dev.dev.parent		= mdev->device;
6570e126ba97SEli Cohen 
65713cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
65723cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
65733cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
6574806b101bSJason Gunthorpe 	xa_init(&dev->odp_mkeys);
657550211ec9SJason Gunthorpe 	xa_init(&dev->sig_mrs);
6576f743ff3bSSaeed Mahameed 	atomic_set(&dev->mkey_var, 0);
65773cc297dbSMark Bloch 
65783b113a1eSAriel Levkovich 	spin_lock_init(&dev->dm.lock);
65793b113a1eSAriel Levkovich 	dev->dm.dev = mdev;
658024da0016SAriel Levkovich 
6581806b101bSJason Gunthorpe 	err = init_srcu_struct(&dev->odp_srcu);
6582a6bc3875SMoni Shoua 	if (err)
6583c9b9dcb4SAriel Levkovich 		goto err_mp;
65843cc297dbSMark Bloch 
658516c1975fSMark Bloch 	return 0;
658625c13324SAriel Levkovich 
658732f69e4bSDaniel Jurgens err_mp:
658832f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
658916c1975fSMark Bloch 
659016c1975fSMark Bloch 	return -ENOMEM;
659116c1975fSMark Bloch }
659216c1975fSMark Bloch 
65939a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
65949a4ca38dSMark Bloch {
65959a4ca38dSMark Bloch 	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
65969a4ca38dSMark Bloch 
65979a4ca38dSMark Bloch 	if (!dev->flow_db)
65989a4ca38dSMark Bloch 		return -ENOMEM;
65999a4ca38dSMark Bloch 
66009a4ca38dSMark Bloch 	mutex_init(&dev->flow_db->lock);
66019a4ca38dSMark Bloch 
66029a4ca38dSMark Bloch 	return 0;
66039a4ca38dSMark Bloch }
66049a4ca38dSMark Bloch 
66059a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
66069a4ca38dSMark Bloch {
66079a4ca38dSMark Bloch 	kfree(dev->flow_db);
66089a4ca38dSMark Bloch }
66099a4ca38dSMark Bloch 
661096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
66117a154142SJason Gunthorpe 	.owner = THIS_MODULE,
6612b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_MLX5,
661372c6ec18SJason Gunthorpe 	.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION,
6614b9560a41SJason Gunthorpe 
661596458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
661696458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
66176c984472SMax Gurtovoy 	.alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
661896458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
661996458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
662096458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
662196458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
662296458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
662396458233SKamal Heib 	.create_counters = mlx5_ib_create_counters,
662496458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
662596458233SKamal Heib 	.create_flow = mlx5_ib_create_flow,
662696458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
662796458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
662896458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
662996458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
663096458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
663196458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
663296458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
663396458233SKamal Heib 	.destroy_counters = mlx5_ib_destroy_counters,
663496458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
663596458233SKamal Heib 	.destroy_flow = mlx5_ib_destroy_flow,
663696458233SKamal Heib 	.destroy_flow_action = mlx5_ib_destroy_flow_action,
663796458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
663896458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
663996458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
664096458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
664196458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
664296458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
664311f552e2SMichael Guralnik 	.enable_driver = mlx5_ib_enable_driver,
6644e1b95ae0SErez Alfasi 	.fill_res_entry = mlx5_ib_fill_res_entry,
66454061ff7aSErez Alfasi 	.fill_stat_entry = mlx5_ib_fill_stat_entry,
664696458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
664796458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
664896458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
664996458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
66506c984472SMax Gurtovoy 	.map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
665196458233SKamal Heib 	.mmap = mlx5_ib_mmap,
6652dc2316ebSYishai Hadas 	.mmap_free = mlx5_ib_mmap_free,
665396458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
665496458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
665596458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
665696458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
665796458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
665896458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
665996458233SKamal Heib 	.post_recv = mlx5_ib_post_recv,
666096458233SKamal Heib 	.post_send = mlx5_ib_post_send,
666196458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
666296458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
666396458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
666496458233SKamal Heib 	.query_device = mlx5_ib_query_device,
666596458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
666696458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
666796458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
666896458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
666996458233SKamal Heib 	.read_counters = mlx5_ib_read_counters,
667096458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
667196458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
667296458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
667396458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
6674d3456914SLeon Romanovsky 
6675d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
6676e39afe3dSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
667721a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
667868e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
6679a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
668096458233SKamal Heib };
668196458233SKamal Heib 
668296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
668396458233SKamal Heib 	.create_flow_action_esp = mlx5_ib_create_flow_action_esp,
668496458233SKamal Heib 	.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
668596458233SKamal Heib };
668696458233SKamal Heib 
668796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
668896458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
668996458233SKamal Heib };
669096458233SKamal Heib 
669196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
669296458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
66939c0015efSDanit Goldberg 	.get_vf_guid = mlx5_ib_get_vf_guid,
669496458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
669596458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
669696458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
669796458233SKamal Heib };
669896458233SKamal Heib 
669996458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
670096458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
670196458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
670296458233SKamal Heib };
670396458233SKamal Heib 
670496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
670596458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
670696458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
670796458233SKamal Heib };
670896458233SKamal Heib 
670996458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
671096458233SKamal Heib 	.alloc_dm = mlx5_ib_alloc_dm,
671196458233SKamal Heib 	.dealloc_dm = mlx5_ib_dealloc_dm,
671296458233SKamal Heib 	.reg_dm_mr = mlx5_ib_reg_dm_mr,
671396458233SKamal Heib };
671496458233SKamal Heib 
6715f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev)
6716f164be8cSYishai Hadas {
6717f164be8cSYishai Hadas 	struct mlx5_core_dev *mdev = dev->mdev;
6718f164be8cSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
6719f164be8cSYishai Hadas 	u8 log_doorbell_bar_size;
6720f164be8cSYishai Hadas 	u8 log_doorbell_stride;
6721f164be8cSYishai Hadas 	u64 bar_size;
6722f164be8cSYishai Hadas 
6723f164be8cSYishai Hadas 	log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
6724f164be8cSYishai Hadas 					log_doorbell_bar_size);
6725f164be8cSYishai Hadas 	log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
6726f164be8cSYishai Hadas 					log_doorbell_stride);
6727f164be8cSYishai Hadas 	var_table->hw_start_addr = dev->mdev->bar_addr +
6728f164be8cSYishai Hadas 				MLX5_CAP64_DEV_VDPA_EMULATION(mdev,
6729f164be8cSYishai Hadas 					doorbell_bar_offset);
6730f164be8cSYishai Hadas 	bar_size = (1ULL << log_doorbell_bar_size) * 4096;
6731f164be8cSYishai Hadas 	var_table->stride_size = 1ULL << log_doorbell_stride;
673291b74bf5SAlexander Lobakin 	var_table->num_var_hw_entries = div_u64(bar_size,
673391b74bf5SAlexander Lobakin 						var_table->stride_size);
6734f164be8cSYishai Hadas 	mutex_init(&var_table->bitmap_lock);
6735f164be8cSYishai Hadas 	var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries,
6736f164be8cSYishai Hadas 					  GFP_KERNEL);
6737f164be8cSYishai Hadas 	return (var_table->bitmap) ? 0 : -ENOMEM;
6738f164be8cSYishai Hadas }
6739f164be8cSYishai Hadas 
6740f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev)
6741f164be8cSYishai Hadas {
6742f164be8cSYishai Hadas 	bitmap_free(dev->var_table.bitmap);
6743f164be8cSYishai Hadas }
6744f164be8cSYishai Hadas 
6745fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
674616c1975fSMark Bloch {
674716c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
674816c1975fSMark Bloch 	int err;
674916c1975fSMark Bloch 
6750e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
6751e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
6752e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
6753e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
6754e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
6755e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
675641c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
675741c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
6758e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
675956e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
6760e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
6761e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
6762e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
6763e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
6764e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
6765e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
6766e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
6767e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
6768e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
6769e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
6770e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
6771e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
6772e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
6773e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
6774e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
6775e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
6776e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
67771707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
6778d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
6779d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
67807d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
6781b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
678296458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ)	|
678396458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW)	|
678496458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
6785e126ba97SEli Cohen 
6786f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
6787f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
678896458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
678996458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
67908e959601SNiranjana Vishwanathapura 
679196458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
679296458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
67937c2344c3SMaor Gottlieb 
67946e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
67956e8484c5SMax Gurtovoy 
6796d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
6797d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
6798d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
6799d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
680096458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
6801d2370e0aSMatan Barak 	}
6802d2370e0aSMatan Barak 
6803938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
6804e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
6805e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
6806e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
680796458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
6808e126ba97SEli Cohen 	}
6809e126ba97SEli Cohen 
681025c13324SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic) ||
681125c13324SAriel Levkovich 	    MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
681225c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
681396458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
681424da0016SAriel Levkovich 
6815dfb631a1SJason Gunthorpe 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
681696458233SKamal Heib 	    MLX5_ACCEL_IPSEC_CAP_DEVICE)
681796458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
681896458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
681981e30880SYishai Hadas 
682036e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
682136e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
682281e30880SYishai Hadas 
6823e126ba97SEli Cohen 	err = init_node_data(dev);
6824e126ba97SEli Cohen 	if (err)
682516c1975fSMark Bloch 		return err;
6826e126ba97SEli Cohen 
6827c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
6828e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6829e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
6830a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
6831c8b89924SMark Bloch 
6832f164be8cSYishai Hadas 	if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
6833f164be8cSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) {
6834f164be8cSYishai Hadas 		err = mlx5_ib_init_var_table(dev);
6835f164be8cSYishai Hadas 		if (err)
6836f164be8cSYishai Hadas 			return err;
6837f164be8cSYishai Hadas 	}
6838f164be8cSYishai Hadas 
683996e2fd73SLeon Romanovsky 	dev->ib_dev.use_cq_dim = true;
684096e2fd73SLeon Romanovsky 
684116c1975fSMark Bloch 	return 0;
684216c1975fSMark Bloch }
684316c1975fSMark Bloch 
684496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
684596458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
684696458233SKamal Heib 	.query_port = mlx5_ib_query_port,
684796458233SKamal Heib };
684896458233SKamal Heib 
68498e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
685016c1975fSMark Bloch {
685196458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
68528e6efa3aSMark Bloch 	return 0;
68538e6efa3aSMark Bloch }
68548e6efa3aSMark Bloch 
685596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
685696458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
685796458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
685896458233SKamal Heib };
685996458233SKamal Heib 
6860b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev)
68618e6efa3aSMark Bloch {
686296458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
68638e6efa3aSMark Bloch 	return 0;
68648e6efa3aSMark Bloch }
68658e6efa3aSMark Bloch 
686696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
686796458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
686896458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
686996458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
687096458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
687196458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
687296458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
687396458233SKamal Heib };
687496458233SKamal Heib 
6875e3f1ed1fSLeon Romanovsky static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
68768e6efa3aSMark Bloch {
6877e3f1ed1fSLeon Romanovsky 	u8 port_num;
68787fd8aefbSDaniel Jurgens 
6879c11a226aSMark Bloch 	dev->ib_dev.uverbs_ex_cmd_mask |=
6880c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6881c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6882c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6883c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6884c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
688596458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
68868e6efa3aSMark Bloch 
6887e3f1ed1fSLeon Romanovsky 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6888e3f1ed1fSLeon Romanovsky 
688926628e2dSMark Bloch 	/* Register only for native ports */
68908e6efa3aSMark Bloch 	return mlx5_add_netdev_notifier(dev, port_num);
68918e6efa3aSMark Bloch }
68928e6efa3aSMark Bloch 
68938e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
68948e6efa3aSMark Bloch {
68958e6efa3aSMark Bloch 	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
68968e6efa3aSMark Bloch 
68978e6efa3aSMark Bloch 	mlx5_remove_netdev_notifier(dev, port_num);
68988e6efa3aSMark Bloch }
68998e6efa3aSMark Bloch 
6900b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_roce_init(struct mlx5_ib_dev *dev)
69018e6efa3aSMark Bloch {
69028e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
69038e6efa3aSMark Bloch 	enum rdma_link_layer ll;
69048e6efa3aSMark Bloch 	int port_type_cap;
69058e6efa3aSMark Bloch 	int err = 0;
69068e6efa3aSMark Bloch 
69078e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
69088e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
69098e6efa3aSMark Bloch 
69108e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET)
6911e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
69128e6efa3aSMark Bloch 
691316c1975fSMark Bloch 	return err;
6914fc24fc5eSAchiad Shochat }
6915fc24fc5eSAchiad Shochat 
6916b5a498baSMichael Guralnik static void mlx5_ib_stage_raw_eth_roce_cleanup(struct mlx5_ib_dev *dev)
69178e6efa3aSMark Bloch {
69188e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
69198e6efa3aSMark Bloch }
69208e6efa3aSMark Bloch 
69218e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
69228e6efa3aSMark Bloch {
69238e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
69248e6efa3aSMark Bloch 	enum rdma_link_layer ll;
69258e6efa3aSMark Bloch 	int port_type_cap;
69268e6efa3aSMark Bloch 	int err;
69278e6efa3aSMark Bloch 
69288e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
69298e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
69308e6efa3aSMark Bloch 
69318e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
6932e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
69338e6efa3aSMark Bloch 		if (err)
69348e6efa3aSMark Bloch 			return err;
69358e6efa3aSMark Bloch 
6936e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
69378e6efa3aSMark Bloch 		if (err)
69388e6efa3aSMark Bloch 			goto cleanup;
69398e6efa3aSMark Bloch 	}
69408e6efa3aSMark Bloch 
694116c1975fSMark Bloch 	return 0;
69428e6efa3aSMark Bloch cleanup:
69438e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
69448e6efa3aSMark Bloch 
69458e6efa3aSMark Bloch 	return err;
694645bded2cSKamal Heib }
69476aec21f6SHaggai Eran 
694816c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
694916c1975fSMark Bloch {
695016c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
695116c1975fSMark Bloch 	enum rdma_link_layer ll;
695216c1975fSMark Bloch 	int port_type_cap;
69534a2da0b8SParav Pandit 
695416c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
695516c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
695616c1975fSMark Bloch 
695716c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
695816c1975fSMark Bloch 		mlx5_disable_eth(dev);
69598e6efa3aSMark Bloch 		mlx5_ib_stage_common_roce_cleanup(dev);
696016c1975fSMark Bloch 	}
696116c1975fSMark Bloch }
696216c1975fSMark Bloch 
6963fb652d32SMark Bloch static int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
696416c1975fSMark Bloch {
696516c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
696616c1975fSMark Bloch }
696716c1975fSMark Bloch 
6968fb652d32SMark Bloch static void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
696916c1975fSMark Bloch {
697016c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
697116c1975fSMark Bloch }
697216c1975fSMark Bloch 
697316c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
697416c1975fSMark Bloch {
697516c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
697616c1975fSMark Bloch }
697716c1975fSMark Bloch 
6978f3ffed0cSKamal Heib static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
6979d5d284b8SSaeed Mahameed {
6980d5d284b8SSaeed Mahameed 	mlx5_ib_odp_cleanup_one(dev);
6981d5d284b8SSaeed Mahameed }
6982d5d284b8SSaeed Mahameed 
698396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
698496458233SKamal Heib 	.alloc_hw_stats = mlx5_ib_alloc_hw_stats,
698596458233SKamal Heib 	.get_hw_stats = mlx5_ib_get_hw_stats,
698645842fc6SMark Zhang 	.counter_bind_qp = mlx5_ib_counter_bind_qp,
698745842fc6SMark Zhang 	.counter_unbind_qp = mlx5_ib_counter_unbind_qp,
698845842fc6SMark Zhang 	.counter_dealloc = mlx5_ib_counter_dealloc,
698918d422ceSMark Zhang 	.counter_alloc_stats = mlx5_ib_counter_alloc_stats,
699018d422ceSMark Zhang 	.counter_update_stats = mlx5_ib_counter_update_stats,
699196458233SKamal Heib };
699296458233SKamal Heib 
6993fb652d32SMark Bloch static int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
699416c1975fSMark Bloch {
69955e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
699696458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
69975e1e7612SMark Bloch 
699816c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
69995e1e7612SMark Bloch 	}
700016c1975fSMark Bloch 
700116c1975fSMark Bloch 	return 0;
700216c1975fSMark Bloch }
700316c1975fSMark Bloch 
7004fb652d32SMark Bloch static void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
700516c1975fSMark Bloch {
700616c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
700716c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
700816c1975fSMark Bloch }
700916c1975fSMark Bloch 
701016c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
701116c1975fSMark Bloch {
701273eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
7013a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
701473eb8f03SGreg Kroah-Hartman 	return 0;
701516c1975fSMark Bloch }
701616c1975fSMark Bloch 
701716c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
701816c1975fSMark Bloch {
7019a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
7020a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
702116c1975fSMark Bloch }
702216c1975fSMark Bloch 
702316c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
702416c1975fSMark Bloch {
70255fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
7026444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
702716c1975fSMark Bloch }
702816c1975fSMark Bloch 
702916c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
703016c1975fSMark Bloch {
703116c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
703216c1975fSMark Bloch }
703316c1975fSMark Bloch 
7034fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
703516c1975fSMark Bloch {
703616c1975fSMark Bloch 	int err;
70375fe9dec0SEli Cohen 
70385fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
70395fe9dec0SEli Cohen 	if (err)
704016c1975fSMark Bloch 		return err;
70415fe9dec0SEli Cohen 
70425fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
70435fe9dec0SEli Cohen 	if (err)
704416c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
70455fe9dec0SEli Cohen 
704616c1975fSMark Bloch 	return err;
704716c1975fSMark Bloch }
70480837e86aSMark Bloch 
7049fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
705016c1975fSMark Bloch {
705116c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
705216c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
705316c1975fSMark Bloch }
7054e126ba97SEli Cohen 
7055fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
705616c1975fSMark Bloch {
7057e349f858SJason Gunthorpe 	const char *name;
7058e349f858SJason Gunthorpe 
7059508a523fSParav Pandit 	rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
70607c34ec19SAviv Heller 	if (!mlx5_lag_is_roce(dev->mdev))
7061e349f858SJason Gunthorpe 		name = "mlx5_%d";
7062e349f858SJason Gunthorpe 	else
7063e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
7064ea4baf7fSParav Pandit 	return ib_register_device(&dev->ib_dev, name);
706516c1975fSMark Bloch }
706616c1975fSMark Bloch 
7067fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
706842cea83fSMark Bloch {
706942cea83fSMark Bloch 	destroy_umrc_res(dev);
707042cea83fSMark Bloch }
707142cea83fSMark Bloch 
7072fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
707316c1975fSMark Bloch {
707416c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
707516c1975fSMark Bloch }
707616c1975fSMark Bloch 
7077fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
707816c1975fSMark Bloch {
707916c1975fSMark Bloch 	return create_umr_res(dev);
708016c1975fSMark Bloch }
708116c1975fSMark Bloch 
708216c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
708316c1975fSMark Bloch {
708403404e8aSMaor Gottlieb 	init_delay_drop(dev);
708503404e8aSMaor Gottlieb 
708616c1975fSMark Bloch 	return 0;
708716c1975fSMark Bloch }
708816c1975fSMark Bloch 
708916c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
709016c1975fSMark Bloch {
709116c1975fSMark Bloch 	cancel_delay_drop(dev);
709216c1975fSMark Bloch }
709316c1975fSMark Bloch 
7094df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
7095df097a27SSaeed Mahameed {
7096df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
7097df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
7098df097a27SSaeed Mahameed 	return 0;
7099df097a27SSaeed Mahameed }
7100df097a27SSaeed Mahameed 
7101df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
7102df097a27SSaeed Mahameed {
7103df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
7104df097a27SSaeed Mahameed }
7105df097a27SSaeed Mahameed 
710681773ce5SLeon Romanovsky static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
710781773ce5SLeon Romanovsky {
710881773ce5SLeon Romanovsky 	int uid;
710981773ce5SLeon Romanovsky 
7110fb98153bSYishai Hadas 	uid = mlx5_ib_devx_create(dev, false);
7111e337dd53SYishai Hadas 	if (uid > 0) {
711281773ce5SLeon Romanovsky 		dev->devx_whitelist_uid = uid;
7113e337dd53SYishai Hadas 		mlx5_ib_devx_init_event_table(dev);
7114e337dd53SYishai Hadas 	}
711581773ce5SLeon Romanovsky 
711681773ce5SLeon Romanovsky 	return 0;
711781773ce5SLeon Romanovsky }
711881773ce5SLeon Romanovsky static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
711981773ce5SLeon Romanovsky {
7120e337dd53SYishai Hadas 	if (dev->devx_whitelist_uid) {
7121e337dd53SYishai Hadas 		mlx5_ib_devx_cleanup_event_table(dev);
712281773ce5SLeon Romanovsky 		mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
712381773ce5SLeon Romanovsky 	}
7124e337dd53SYishai Hadas }
712581773ce5SLeon Romanovsky 
712611f552e2SMichael Guralnik int mlx5_ib_enable_driver(struct ib_device *dev)
712711f552e2SMichael Guralnik {
712811f552e2SMichael Guralnik 	struct mlx5_ib_dev *mdev = to_mdev(dev);
712911f552e2SMichael Guralnik 	int ret;
713011f552e2SMichael Guralnik 
713111f552e2SMichael Guralnik 	ret = mlx5_ib_test_wc(mdev);
713211f552e2SMichael Guralnik 	mlx5_ib_dbg(mdev, "Write-Combining %s",
713311f552e2SMichael Guralnik 		    mdev->wc_support ? "supported" : "not supported");
713411f552e2SMichael Guralnik 
713511f552e2SMichael Guralnik 	return ret;
713611f552e2SMichael Guralnik }
713711f552e2SMichael Guralnik 
7138b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
713916c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
714016c1975fSMark Bloch 		      int stage)
714116c1975fSMark Bloch {
71424cca96a8SParav Pandit 	dev->ib_active = false;
71434cca96a8SParav Pandit 
714416c1975fSMark Bloch 	/* Number of stages to cleanup */
714516c1975fSMark Bloch 	while (stage) {
714616c1975fSMark Bloch 		stage--;
714716c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
714816c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
714916c1975fSMark Bloch 	}
71504a6dc855SMark Bloch 
7151da796ccbSMark Bloch 	kfree(dev->port);
71524a6dc855SMark Bloch 	ib_dealloc_device(&dev->ib_dev);
715316c1975fSMark Bloch }
715416c1975fSMark Bloch 
7155b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
715616c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
715716c1975fSMark Bloch {
715816c1975fSMark Bloch 	int err;
715916c1975fSMark Bloch 	int i;
716016c1975fSMark Bloch 
716116c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
716216c1975fSMark Bloch 		if (profile->stage[i].init) {
716316c1975fSMark Bloch 			err = profile->stage[i].init(dev);
716416c1975fSMark Bloch 			if (err)
716516c1975fSMark Bloch 				goto err_out;
716616c1975fSMark Bloch 		}
716716c1975fSMark Bloch 	}
716816c1975fSMark Bloch 
716916c1975fSMark Bloch 	dev->profile = profile;
7170e126ba97SEli Cohen 	dev->ib_active = true;
7171e126ba97SEli Cohen 
71729603b61dSJack Morgenstein 	return dev;
7173e126ba97SEli Cohen 
717416c1975fSMark Bloch err_out:
717516c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
7176e126ba97SEli Cohen 
71779603b61dSJack Morgenstein 	return NULL;
7178e126ba97SEli Cohen }
7179e126ba97SEli Cohen 
718016c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
718116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
718216c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
718316c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
71849a4ca38dSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
71859a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_init,
71869a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
718716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
718816c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
7189f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
71908e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
71918e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
71928e6efa3aSMark Bloch 		     NULL),
719316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
719416c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
719516c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
7196*333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
7197*333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
7198*333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
7199f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
7200f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
7201f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
720216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
720316c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
720416c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
7205df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
7206df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
7207df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
720816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
720916c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
7210d5d284b8SSaeed Mahameed 		     mlx5_ib_stage_odp_cleanup),
721116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
721216c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
721316c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
721416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
721516c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
721616c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
721716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
721816c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
721916c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
722016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
722116c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
722216c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
722342cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
722442cea83fSMark Bloch 		     NULL,
722542cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
722681773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
722781773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_init,
722881773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_cleanup),
722916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
723016c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
723116c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
723242cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
723342cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
723442cea83fSMark Bloch 		     NULL),
723516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
723616c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
723716c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
723816c1975fSMark Bloch };
723916c1975fSMark Bloch 
7240b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = {
7241b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
7242b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
7243b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
7244b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
7245b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_init,
7246b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
7247b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
7248b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
7249f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
7250b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
7251b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_non_default_cb,
7252b5ca15adSMark Bloch 		     NULL),
7253b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
7254b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_roce_init,
7255b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_roce_cleanup),
7256*333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
7257*333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
7258*333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
7259f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
7260f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
7261f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
7262b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
7263b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_init,
7264b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
7265df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
7266df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
7267df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
7268b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
7269b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_init,
7270b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
727179db784eSParav Pandit 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
727279db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_init,
727379db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_cleanup),
7274b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
7275b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
7276b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
7277b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
7278b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
7279b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
728003fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
728103fe2debSDavid S. Miller 		     NULL,
728203fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
72837f575103SMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
72847f575103SMark Bloch 		     mlx5_ib_stage_devx_init,
72857f575103SMark Bloch 		     mlx5_ib_stage_devx_cleanup),
7286b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
7287b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
7288b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
728903fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
729003fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
729103fe2debSDavid S. Miller 		     NULL),
7292b5ca15adSMark Bloch };
7293b5ca15adSMark Bloch 
7294e3f1ed1fSLeon Romanovsky static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
729532f69e4bSDaniel Jurgens {
729632f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
729732f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
729832f69e4bSDaniel Jurgens 	bool bound = false;
729932f69e4bSDaniel Jurgens 	int err;
730032f69e4bSDaniel Jurgens 
730132f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
730232f69e4bSDaniel Jurgens 	if (!mpi)
730332f69e4bSDaniel Jurgens 		return NULL;
730432f69e4bSDaniel Jurgens 
730532f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
730632f69e4bSDaniel Jurgens 
730732f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
730832f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
730932f69e4bSDaniel Jurgens 	if (err) {
731032f69e4bSDaniel Jurgens 		kfree(mpi);
731132f69e4bSDaniel Jurgens 		return NULL;
731232f69e4bSDaniel Jurgens 	}
731332f69e4bSDaniel Jurgens 
731432f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
731532f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
731632f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
731732f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
731832f69e4bSDaniel Jurgens 
731932f69e4bSDaniel Jurgens 		if (bound) {
732032f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
732132f69e4bSDaniel Jurgens 			break;
732232f69e4bSDaniel Jurgens 		}
732332f69e4bSDaniel Jurgens 	}
732432f69e4bSDaniel Jurgens 
732532f69e4bSDaniel Jurgens 	if (!bound) {
732632f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
7327c42260f1SVu Pham 		dev_dbg(mdev->device,
7328c42260f1SVu Pham 			"no suitable IB device found to bind to, added to unaffiliated list.\n");
732932f69e4bSDaniel Jurgens 	}
733032f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
733132f69e4bSDaniel Jurgens 
733232f69e4bSDaniel Jurgens 	return mpi;
733332f69e4bSDaniel Jurgens }
733432f69e4bSDaniel Jurgens 
733516c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
733616c1975fSMark Bloch {
733794de879cSMichael Guralnik 	const struct mlx5_ib_profile *profile;
733832f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
7339b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
734032f69e4bSDaniel Jurgens 	int port_type_cap;
7341da796ccbSMark Bloch 	int num_ports;
734232f69e4bSDaniel Jurgens 
7343b5ca15adSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
7344b5ca15adSMark Bloch 
7345f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) &&
7346f6455de0SBodong Wang 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == MLX5_ESWITCH_OFFLOADS) {
73475fb58c9eSMark Bloch 		if (!mlx5_core_mp_enabled(mdev))
7348f0666f1fSBodong Wang 			mlx5_ib_register_vport_reps(mdev);
7349f0666f1fSBodong Wang 		return mdev;
7350f0666f1fSBodong Wang 	}
7351f0666f1fSBodong Wang 
735232f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
735332f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
735432f69e4bSDaniel Jurgens 
7355e3f1ed1fSLeon Romanovsky 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
7356e3f1ed1fSLeon Romanovsky 		return mlx5_ib_add_slave_port(mdev);
735732f69e4bSDaniel Jurgens 
7358da796ccbSMark Bloch 	num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
7359da796ccbSMark Bloch 			MLX5_CAP_GEN(mdev, num_vhca_ports));
7360459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
7361b5ca15adSMark Bloch 	if (!dev)
7362b5ca15adSMark Bloch 		return NULL;
7363da796ccbSMark Bloch 	dev->port = kcalloc(num_ports, sizeof(*dev->port),
7364da796ccbSMark Bloch 			     GFP_KERNEL);
7365da796ccbSMark Bloch 	if (!dev->port) {
7366a5c9c299SParav Pandit 		ib_dealloc_device(&dev->ib_dev);
7367da796ccbSMark Bloch 		return NULL;
7368da796ccbSMark Bloch 	}
7369b5ca15adSMark Bloch 
7370b5ca15adSMark Bloch 	dev->mdev = mdev;
7371da796ccbSMark Bloch 	dev->num_ports = num_ports;
7372b5ca15adSMark Bloch 
737394de879cSMichael Guralnik 	if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_is_roce_enabled(mdev))
737494de879cSMichael Guralnik 		profile = &raw_eth_profile;
737594de879cSMichael Guralnik 	else
737694de879cSMichael Guralnik 		profile = &pf_profile;
737794de879cSMichael Guralnik 
737894de879cSMichael Guralnik 	return __mlx5_ib_add(dev, profile);
737916c1975fSMark Bloch }
738016c1975fSMark Bloch 
73819603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
7382e126ba97SEli Cohen {
738332f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
738432f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
73856aec21f6SHaggai Eran 
7386f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
7387f0666f1fSBodong Wang 		mlx5_ib_unregister_vport_reps(mdev);
7388f0666f1fSBodong Wang 		return;
7389f0666f1fSBodong Wang 	}
7390f0666f1fSBodong Wang 
739132f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
739232f69e4bSDaniel Jurgens 		mpi = context;
739332f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
739432f69e4bSDaniel Jurgens 		if (mpi->ibdev)
739532f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
739632f69e4bSDaniel Jurgens 		list_del(&mpi->list);
739732f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
73985d44adebSDanit Goldberg 		kfree(mpi);
739932f69e4bSDaniel Jurgens 		return;
740032f69e4bSDaniel Jurgens 	}
740132f69e4bSDaniel Jurgens 
740232f69e4bSDaniel Jurgens 	dev = context;
740316c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
7404e126ba97SEli Cohen }
7405e126ba97SEli Cohen 
74069603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
74079603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
74089603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
740964613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
7410e126ba97SEli Cohen };
7411e126ba97SEli Cohen 
7412c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
7413c44ef998SIlya Lesokhin {
7414c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
7415c44ef998SIlya Lesokhin 	return xlt_emergency_page;
7416c44ef998SIlya Lesokhin }
7417c44ef998SIlya Lesokhin 
7418c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
7419c44ef998SIlya Lesokhin {
7420c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
7421c44ef998SIlya Lesokhin }
7422c44ef998SIlya Lesokhin 
7423e126ba97SEli Cohen static int __init mlx5_ib_init(void)
7424e126ba97SEli Cohen {
74256aec21f6SHaggai Eran 	int err;
74266aec21f6SHaggai Eran 
7427c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
7428c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
7429d69a24e0SDaniel Jurgens 		return -ENOMEM;
7430d69a24e0SDaniel Jurgens 
7431c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
7432c44ef998SIlya Lesokhin 
7433c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
7434c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
7435c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
7436c44ef998SIlya Lesokhin 		return -ENOMEM;
7437c44ef998SIlya Lesokhin 	}
7438c44ef998SIlya Lesokhin 
743981713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
74409603b61dSJack Morgenstein 
74416aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
74426aec21f6SHaggai Eran 
74436aec21f6SHaggai Eran 	return err;
7444e126ba97SEli Cohen }
7445e126ba97SEli Cohen 
7446e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
7447e126ba97SEli Cohen {
74489603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
7449d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
7450c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
7451c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
7452e126ba97SEli Cohen }
7453e126ba97SEli Cohen 
7454e126ba97SEli Cohen module_init(mlx5_ib_init);
7455e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
7456