xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 1f3db161881b7e21efb149e0ae8152b79a571a8f)
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"
623346c487SBoris Pismenny #include <linux/mlx5/fs_helpers.h>
63c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h>
648c84660bSMatan Barak #include <rdma/uverbs_std_types.h>
65c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h>
66c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h>
674061ff7aSErez Alfasi #include <rdma/ib_umem_odp.h>
688c84660bSMatan Barak 
698c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
708c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
71e126ba97SEli Cohen 
72e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
73b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0"
74e126ba97SEli Cohen 
75e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
76e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
77e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
78e126ba97SEli Cohen 
79e126ba97SEli Cohen static char mlx5_version[] =
80e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
81b359911dSTariq Toukan 	DRIVER_VERSION "\n";
82e126ba97SEli Cohen 
83d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
84d69a24e0SDaniel Jurgens 	struct work_struct	work;
85df097a27SSaeed Mahameed 	union {
86df097a27SSaeed Mahameed 		struct mlx5_ib_dev	      *dev;
87df097a27SSaeed Mahameed 		struct mlx5_ib_multiport_info *mpi;
88df097a27SSaeed Mahameed 	};
89df097a27SSaeed Mahameed 	bool			is_slave;
90134e9349SSaeed Mahameed 	unsigned int		event;
91df097a27SSaeed Mahameed 	void			*param;
92d69a24e0SDaniel Jurgens };
93d69a24e0SDaniel Jurgens 
94da7525d2SEran Ben Elisha enum {
95da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
96da7525d2SEran Ben Elisha };
971b5daf11SMajd Dibbiny 
98d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
9932f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
10032f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
10132f69e4bSDaniel Jurgens /*
10232f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
10332f69e4bSDaniel Jurgens  */
10432f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
10532f69e4bSDaniel Jurgens 
106c44ef998SIlya Lesokhin /* We can't use an array for xlt_emergency_page because dma_map_single
107c44ef998SIlya Lesokhin  * doesn't work on kernel modules memory
108c44ef998SIlya Lesokhin  */
109c44ef998SIlya Lesokhin static unsigned long xlt_emergency_page;
110c44ef998SIlya Lesokhin static struct mutex xlt_emergency_page_mutex;
111c44ef998SIlya Lesokhin 
11232f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
11332f69e4bSDaniel Jurgens {
11432f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
11532f69e4bSDaniel Jurgens 
11632f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
11732f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
11832f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
11932f69e4bSDaniel Jurgens 	return dev;
12032f69e4bSDaniel Jurgens }
12132f69e4bSDaniel Jurgens 
1221b5daf11SMajd Dibbiny static enum rdma_link_layer
123ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
1241b5daf11SMajd Dibbiny {
125ebd61f68SAchiad Shochat 	switch (port_type_cap) {
1261b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
1271b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
1281b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
1291b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
1301b5daf11SMajd Dibbiny 	default:
1311b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1321b5daf11SMajd Dibbiny 	}
1331b5daf11SMajd Dibbiny }
1341b5daf11SMajd Dibbiny 
135ebd61f68SAchiad Shochat static enum rdma_link_layer
136ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
137ebd61f68SAchiad Shochat {
138ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
139ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
140ebd61f68SAchiad Shochat 
141ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
142ebd61f68SAchiad Shochat }
143ebd61f68SAchiad Shochat 
144fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
145fd65f1b8SMoni Shoua 			  u8 port_num,
146fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
147fd65f1b8SMoni Shoua {
148fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
149fd65f1b8SMoni Shoua 	int ret;
150fd65f1b8SMoni Shoua 
151fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1523023a1e9SKamal Heib 	ret = ibdev->ops.query_port(ibdev, port_num, &attr);
153fd65f1b8SMoni Shoua 	if (!ret)
154fd65f1b8SMoni Shoua 		*state = attr.state;
155fd65f1b8SMoni Shoua 	return ret;
156fd65f1b8SMoni Shoua }
157fd65f1b8SMoni Shoua 
15835b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
15935b0aa67SMark Bloch 					   struct net_device *ndev,
16035b0aa67SMark Bloch 					   u8 *port_num)
16135b0aa67SMark Bloch {
16235b0aa67SMark Bloch 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
16335b0aa67SMark Bloch 	struct net_device *rep_ndev;
16435b0aa67SMark Bloch 	struct mlx5_ib_port *port;
16535b0aa67SMark Bloch 	int i;
16635b0aa67SMark Bloch 
16735b0aa67SMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
16835b0aa67SMark Bloch 		port  = &dev->port[i];
16935b0aa67SMark Bloch 		if (!port->rep)
17035b0aa67SMark Bloch 			continue;
17135b0aa67SMark Bloch 
17235b0aa67SMark Bloch 		read_lock(&port->roce.netdev_lock);
17335b0aa67SMark Bloch 		rep_ndev = mlx5_ib_get_rep_netdev(esw,
17435b0aa67SMark Bloch 						  port->rep->vport);
17535b0aa67SMark Bloch 		if (rep_ndev == ndev) {
17635b0aa67SMark Bloch 			read_unlock(&port->roce.netdev_lock);
17735b0aa67SMark Bloch 			*port_num = i + 1;
17835b0aa67SMark Bloch 			return &port->roce;
17935b0aa67SMark Bloch 		}
18035b0aa67SMark Bloch 		read_unlock(&port->roce.netdev_lock);
18135b0aa67SMark Bloch 	}
18235b0aa67SMark Bloch 
18335b0aa67SMark Bloch 	return NULL;
18435b0aa67SMark Bloch }
18535b0aa67SMark Bloch 
186fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
187fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
188fc24fc5eSAchiad Shochat {
1897fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
190fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1917fd8aefbSDaniel Jurgens 	u8 port_num = roce->native_port_num;
1927fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1937fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1947fd8aefbSDaniel Jurgens 
1957fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
19632f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
19732f69e4bSDaniel Jurgens 	if (!mdev)
19832f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
199fc24fc5eSAchiad Shochat 
2005ec8c83eSAviv Heller 	switch (event) {
2015ec8c83eSAviv Heller 	case NETDEV_REGISTER:
20235b0aa67SMark Bloch 		/* Should already be registered during the load */
20335b0aa67SMark Bloch 		if (ibdev->is_rep)
20435b0aa67SMark Bloch 			break;
2057fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
206dce45af5SLinus Torvalds 		if (ndev->dev.parent == mdev->device)
207842a9c83SOr Gerlitz 			roce->netdev = ndev;
2087fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
2095ec8c83eSAviv Heller 		break;
2105ec8c83eSAviv Heller 
211842a9c83SOr Gerlitz 	case NETDEV_UNREGISTER:
21235b0aa67SMark Bloch 		/* In case of reps, ib device goes away before the netdevs */
213842a9c83SOr Gerlitz 		write_lock(&roce->netdev_lock);
214842a9c83SOr Gerlitz 		if (roce->netdev == ndev)
215842a9c83SOr Gerlitz 			roce->netdev = NULL;
216842a9c83SOr Gerlitz 		write_unlock(&roce->netdev_lock);
217842a9c83SOr Gerlitz 		break;
218842a9c83SOr Gerlitz 
219fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
2205ec8c83eSAviv Heller 	case NETDEV_UP:
22188621dfeSAviv Heller 	case NETDEV_DOWN: {
2227fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
22388621dfeSAviv Heller 		struct net_device *upper = NULL;
22488621dfeSAviv Heller 
22588621dfeSAviv Heller 		if (lag_ndev) {
22688621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
22788621dfeSAviv Heller 			dev_put(lag_ndev);
22888621dfeSAviv Heller 		}
22988621dfeSAviv Heller 
23035b0aa67SMark Bloch 		if (ibdev->is_rep)
23135b0aa67SMark Bloch 			roce = mlx5_get_rep_roce(ibdev, ndev, &port_num);
23235b0aa67SMark Bloch 		if (!roce)
23335b0aa67SMark Bloch 			return NOTIFY_DONE;
2347fd8aefbSDaniel Jurgens 		if ((upper == ndev || (!upper && ndev == roce->netdev))
23588621dfeSAviv Heller 		    && ibdev->ib_active) {
236626bc02dSBart Van Assche 			struct ib_event ibev = { };
237fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2385ec8c83eSAviv Heller 
2397fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2407fd8aefbSDaniel Jurgens 					   &port_state))
2417fd8aefbSDaniel Jurgens 				goto done;
242fd65f1b8SMoni Shoua 
2437fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2447fd8aefbSDaniel Jurgens 				goto done;
245fd65f1b8SMoni Shoua 
2467fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2475ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
248fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
249fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
250fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
251fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
252fd65f1b8SMoni Shoua 			else
2537fd8aefbSDaniel Jurgens 				goto done;
254fd65f1b8SMoni Shoua 
2557fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2565ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2575ec8c83eSAviv Heller 		}
2585ec8c83eSAviv Heller 		break;
25988621dfeSAviv Heller 	}
2605ec8c83eSAviv Heller 
2615ec8c83eSAviv Heller 	default:
2625ec8c83eSAviv Heller 		break;
2635ec8c83eSAviv Heller 	}
2647fd8aefbSDaniel Jurgens done:
26532f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
266fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
267fc24fc5eSAchiad Shochat }
268fc24fc5eSAchiad Shochat 
269fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
270fc24fc5eSAchiad Shochat 					     u8 port_num)
271fc24fc5eSAchiad Shochat {
272fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
273fc24fc5eSAchiad Shochat 	struct net_device *ndev;
27432f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
275fc24fc5eSAchiad Shochat 
27632f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
27732f69e4bSDaniel Jurgens 	if (!mdev)
27832f69e4bSDaniel Jurgens 		return NULL;
27932f69e4bSDaniel Jurgens 
28032f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
28188621dfeSAviv Heller 	if (ndev)
28232f69e4bSDaniel Jurgens 		goto out;
28388621dfeSAviv Heller 
284fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
285fc24fc5eSAchiad Shochat 	 */
28695579e78SMark Bloch 	read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
28795579e78SMark Bloch 	ndev = ibdev->port[port_num - 1].roce.netdev;
288fc24fc5eSAchiad Shochat 	if (ndev)
289fc24fc5eSAchiad Shochat 		dev_hold(ndev);
29095579e78SMark Bloch 	read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
291fc24fc5eSAchiad Shochat 
29232f69e4bSDaniel Jurgens out:
29332f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
294fc24fc5eSAchiad Shochat 	return ndev;
295fc24fc5eSAchiad Shochat }
296fc24fc5eSAchiad Shochat 
29732f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
29832f69e4bSDaniel Jurgens 						   u8 ib_port_num,
29932f69e4bSDaniel Jurgens 						   u8 *native_port_num)
30032f69e4bSDaniel Jurgens {
30132f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
30232f69e4bSDaniel Jurgens 							  ib_port_num);
30332f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
30432f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
30532f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
30632f69e4bSDaniel Jurgens 
307210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
308210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
309210b1f78SMark Bloch 		if (native_port_num)
310210b1f78SMark Bloch 			*native_port_num = ib_port_num;
311210b1f78SMark Bloch 		return ibdev->mdev;
312210b1f78SMark Bloch 	}
313210b1f78SMark Bloch 
31432f69e4bSDaniel Jurgens 	if (native_port_num)
31532f69e4bSDaniel Jurgens 		*native_port_num = 1;
31632f69e4bSDaniel Jurgens 
31732f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
31832f69e4bSDaniel Jurgens 	if (!port)
31932f69e4bSDaniel Jurgens 		return NULL;
32032f69e4bSDaniel Jurgens 
32132f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
32232f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
32332f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
32432f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
32532f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
32632f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
32732f69e4bSDaniel Jurgens 		 */
32832f69e4bSDaniel Jurgens 		if (!mpi->is_master)
32932f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
33032f69e4bSDaniel Jurgens 	}
33132f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
33232f69e4bSDaniel Jurgens 
33332f69e4bSDaniel Jurgens 	return mdev;
33432f69e4bSDaniel Jurgens }
33532f69e4bSDaniel Jurgens 
33632f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
33732f69e4bSDaniel Jurgens {
33832f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
33932f69e4bSDaniel Jurgens 							  port_num);
34032f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
34132f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
34232f69e4bSDaniel Jurgens 
34332f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
34432f69e4bSDaniel Jurgens 		return;
34532f69e4bSDaniel Jurgens 
34632f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
34732f69e4bSDaniel Jurgens 
34832f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
34932f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
35032f69e4bSDaniel Jurgens 	if (mpi->is_master)
35132f69e4bSDaniel Jurgens 		goto out;
35232f69e4bSDaniel Jurgens 
35332f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
35432f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
35532f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
35632f69e4bSDaniel Jurgens out:
35732f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
35832f69e4bSDaniel Jurgens }
35932f69e4bSDaniel Jurgens 
36008e8676fSAya Levin static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, u8 *active_speed,
361f1b65df5SNoa Osherovich 					   u8 *active_width)
362f1b65df5SNoa Osherovich {
363f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
364f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
365f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
366f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
367f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
368f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
369f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
370f1b65df5SNoa Osherovich 		break;
371f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
372f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
373f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
374f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
375f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
376f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
377f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
378f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
379f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
380f1b65df5SNoa Osherovich 		break;
381f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
382f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
383f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
384f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
385f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
386f1b65df5SNoa Osherovich 		break;
387f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
388f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
389f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
390f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
391f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
392f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
393f1b65df5SNoa Osherovich 		break;
394f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
395f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
396f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
397f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
398f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
399f1b65df5SNoa Osherovich 		break;
400f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
401f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
402f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
403f1b65df5SNoa Osherovich 		break;
404f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
405f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
406f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
407f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
408f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
409f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
410f1b65df5SNoa Osherovich 		break;
411f1b65df5SNoa Osherovich 	default:
412f1b65df5SNoa Osherovich 		return -EINVAL;
413f1b65df5SNoa Osherovich 	}
414f1b65df5SNoa Osherovich 
415f1b65df5SNoa Osherovich 	return 0;
416f1b65df5SNoa Osherovich }
417f1b65df5SNoa Osherovich 
41808e8676fSAya Levin static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed,
41908e8676fSAya Levin 					u8 *active_width)
42008e8676fSAya Levin {
42108e8676fSAya Levin 	switch (eth_proto_oper) {
42208e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
42308e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
42408e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
42508e8676fSAya Levin 		*active_speed = IB_SPEED_SDR;
42608e8676fSAya Levin 		break;
42708e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
42808e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
42908e8676fSAya Levin 		*active_speed = IB_SPEED_DDR;
43008e8676fSAya Levin 		break;
43108e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
43208e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
43308e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
43408e8676fSAya Levin 		break;
43508e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
43608e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
43708e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
43808e8676fSAya Levin 		break;
43908e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
44008e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
44108e8676fSAya Levin 		*active_speed = IB_SPEED_EDR;
44208e8676fSAya Levin 		break;
44308e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
444cd272875SAya Levin 		*active_width = IB_WIDTH_2X;
445cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
446cd272875SAya Levin 		break;
44708e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
44808e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
44908e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
45008e8676fSAya Levin 		break;
451cd272875SAya Levin 	case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
452cd272875SAya Levin 		*active_width = IB_WIDTH_4X;
453cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
454cd272875SAya Levin 		break;
45508e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
45608e8676fSAya Levin 		*active_width = IB_WIDTH_2X;
45708e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
45808e8676fSAya Levin 		break;
45908e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
46008e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
46108e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
46208e8676fSAya Levin 		break;
46308e8676fSAya Levin 	default:
46408e8676fSAya Levin 		return -EINVAL;
46508e8676fSAya Levin 	}
46608e8676fSAya Levin 
46708e8676fSAya Levin 	return 0;
46808e8676fSAya Levin }
46908e8676fSAya Levin 
47008e8676fSAya Levin static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
47108e8676fSAya Levin 				    u8 *active_width, bool ext)
47208e8676fSAya Levin {
47308e8676fSAya Levin 	return ext ?
47408e8676fSAya Levin 		translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
47508e8676fSAya Levin 					     active_width) :
47608e8676fSAya Levin 		translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
47708e8676fSAya Levin 						active_width);
47808e8676fSAya Levin }
47908e8676fSAya Levin 
480095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
4813f89a643SAchiad Shochat 				struct ib_port_attr *props)
4823f89a643SAchiad Shochat {
4833f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
484bc4e12ffSAya Levin 	u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
485da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
48688621dfeSAviv Heller 	struct net_device *ndev, *upper;
4873f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
488b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
489c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
490f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
491b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
49208e8676fSAya Levin 	bool ext;
493095b0927SIlan Tayari 	int err;
4943f89a643SAchiad Shochat 
495b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
496b3cbd6f0SDaniel Jurgens 	if (!mdev) {
497b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
498b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
499b3cbd6f0SDaniel Jurgens 		 */
500b3cbd6f0SDaniel Jurgens 		put_mdev = false;
501b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
502b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
503b3cbd6f0SDaniel Jurgens 		port_num = 1;
504b3cbd6f0SDaniel Jurgens 	}
505b3cbd6f0SDaniel Jurgens 
506f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
507f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
50826628e2dSMark Bloch 	 * Use native port in case of reps
50950f22fd8SNoa Osherovich 	 */
51026628e2dSMark Bloch 	if (dev->is_rep)
51126628e2dSMark Bloch 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
51226628e2dSMark Bloch 					   1);
51326628e2dSMark Bloch 	else
514bc4e12ffSAya Levin 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
515b3cbd6f0SDaniel Jurgens 					   mdev_port_num);
516095b0927SIlan Tayari 	if (err)
517b3cbd6f0SDaniel Jurgens 		goto out;
51808e8676fSAya Levin 	ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet);
51908e8676fSAya Levin 	eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
520f1b65df5SNoa Osherovich 
5217672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
5227672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
5237672ed33SHonggang Li 
524f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
52508e8676fSAya Levin 				 &props->active_width, ext);
5263f89a643SAchiad Shochat 
5273f89a643SAchiad Shochat 	props->port_cap_flags |= IB_PORT_CM_SUP;
5282f944c0fSJason Gunthorpe 	props->ip_gids = true;
5293f89a643SAchiad Shochat 
5303f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
5313f89a643SAchiad Shochat 						roce_address_table_size);
5323f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
5333f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
5343f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
5353f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
53672a7720fSKamal Heib 	props->phys_state       = IB_PORT_PHYS_STATE_DISABLED;
5373f89a643SAchiad Shochat 
538b3cbd6f0SDaniel Jurgens 	mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
539c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
5403f89a643SAchiad Shochat 
541b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
542b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
543b3cbd6f0SDaniel Jurgens 		goto out;
544b3cbd6f0SDaniel Jurgens 
5453f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
5463f89a643SAchiad Shochat 	if (!ndev)
547b3cbd6f0SDaniel Jurgens 		goto out;
5483f89a643SAchiad Shochat 
5497c34ec19SAviv Heller 	if (dev->lag_active) {
55088621dfeSAviv Heller 		rcu_read_lock();
55188621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
55288621dfeSAviv Heller 		if (upper) {
55388621dfeSAviv Heller 			dev_put(ndev);
55488621dfeSAviv Heller 			ndev = upper;
55588621dfeSAviv Heller 			dev_hold(ndev);
55688621dfeSAviv Heller 		}
55788621dfeSAviv Heller 		rcu_read_unlock();
55888621dfeSAviv Heller 	}
55988621dfeSAviv Heller 
5603f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
5613f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
56272a7720fSKamal Heib 		props->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
5633f89a643SAchiad Shochat 	}
5643f89a643SAchiad Shochat 
5653f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
5663f89a643SAchiad Shochat 
5673f89a643SAchiad Shochat 	dev_put(ndev);
5683f89a643SAchiad Shochat 
5693f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
570b3cbd6f0SDaniel Jurgens out:
571b3cbd6f0SDaniel Jurgens 	if (put_mdev)
572b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
573b3cbd6f0SDaniel Jurgens 	return err;
5743f89a643SAchiad Shochat }
5753f89a643SAchiad Shochat 
576095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
577095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
578095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
5793cca2606SAchiad Shochat {
580095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
581a70c0739SParav Pandit 	u16 vlan_id = 0xffff;
582095b0927SIlan Tayari 	u8 roce_version = 0;
583095b0927SIlan Tayari 	u8 roce_l3_type = 0;
584095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
585a70c0739SParav Pandit 	int ret;
5863cca2606SAchiad Shochat 
587095b0927SIlan Tayari 	if (gid) {
588095b0927SIlan Tayari 		gid_type = attr->gid_type;
589a70c0739SParav Pandit 		ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
590a70c0739SParav Pandit 		if (ret)
591a70c0739SParav Pandit 			return ret;
5923cca2606SAchiad Shochat 	}
5933cca2606SAchiad Shochat 
594095b0927SIlan Tayari 	switch (gid_type) {
5953cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
596095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
5973cca2606SAchiad Shochat 		break;
5983cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
599095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
600095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
601095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
602095b0927SIlan Tayari 		else
603095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
6043cca2606SAchiad Shochat 		break;
6053cca2606SAchiad Shochat 
6063cca2606SAchiad Shochat 	default:
607095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
6083cca2606SAchiad Shochat 	}
6093cca2606SAchiad Shochat 
610095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
611cf34e1feSParav Pandit 				      roce_l3_type, gid->raw, mac,
612a70c0739SParav Pandit 				      vlan_id < VLAN_CFI_MASK, vlan_id,
613cf34e1feSParav Pandit 				      port_num);
6143cca2606SAchiad Shochat }
6153cca2606SAchiad Shochat 
616f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
6173cca2606SAchiad Shochat 			   __always_unused void **context)
6183cca2606SAchiad Shochat {
619414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
620f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
6213cca2606SAchiad Shochat }
6223cca2606SAchiad Shochat 
623414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
624414448d2SParav Pandit 			   __always_unused void **context)
6253cca2606SAchiad Shochat {
626414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
627414448d2SParav Pandit 			     attr->index, NULL, NULL);
6283cca2606SAchiad Shochat }
6293cca2606SAchiad Shochat 
63047ec3866SParav Pandit __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
63147ec3866SParav Pandit 			       const struct ib_gid_attr *attr)
6322811ba51SAchiad Shochat {
63347ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
6342811ba51SAchiad Shochat 		return 0;
6352811ba51SAchiad Shochat 
6362811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
6372811ba51SAchiad Shochat }
6382811ba51SAchiad Shochat 
6391b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
6401b5daf11SMajd Dibbiny {
6417fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
642d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
6437fae6655SNoa Osherovich 	return 0;
6441b5daf11SMajd Dibbiny }
6451b5daf11SMajd Dibbiny 
6461b5daf11SMajd Dibbiny enum {
6471b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
6481b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
6491b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
6501b5daf11SMajd Dibbiny };
6511b5daf11SMajd Dibbiny 
6521b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
6531b5daf11SMajd Dibbiny {
6541b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
6551b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
6561b5daf11SMajd Dibbiny 
657ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
6581b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
6591b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
6601b5daf11SMajd Dibbiny 
6611b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
6621b5daf11SMajd Dibbiny }
6631b5daf11SMajd Dibbiny 
664da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
665776a3906SMoni Shoua 			    u8 atomic_size_qp,
666da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
667da7525d2SEran Ben Elisha {
668da7525d2SEran Ben Elisha 	u8 tmp;
669da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
670da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
671bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
672da7525d2SEran Ben Elisha 
673da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
674da7525d2SEran Ben Elisha 	 * of host endianness respond
675da7525d2SEran Ben Elisha 	 */
676da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
677da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
678da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
679da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
680da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
681da7525d2SEran Ben Elisha 	} else {
682da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
683da7525d2SEran Ben Elisha 	}
684da7525d2SEran Ben Elisha }
685da7525d2SEran Ben Elisha 
686776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
687776a3906SMoni Shoua 			       struct ib_device_attr *props)
688776a3906SMoni Shoua {
689776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
690776a3906SMoni Shoua 
691776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
692776a3906SMoni Shoua }
693776a3906SMoni Shoua 
6941b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
6951b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
6961b5daf11SMajd Dibbiny {
6971b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6981b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6991b5daf11SMajd Dibbiny 	u64 tmp;
7001b5daf11SMajd Dibbiny 	int err;
7011b5daf11SMajd Dibbiny 
7021b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7031b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7041b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
7051b5daf11SMajd Dibbiny 							    sys_image_guid);
7061b5daf11SMajd Dibbiny 
7071b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7081b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
7093f89a643SAchiad Shochat 		break;
7103f89a643SAchiad Shochat 
7113f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7123f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
7133f89a643SAchiad Shochat 		break;
7141b5daf11SMajd Dibbiny 
7151b5daf11SMajd Dibbiny 	default:
7161b5daf11SMajd Dibbiny 		return -EINVAL;
7171b5daf11SMajd Dibbiny 	}
7183f89a643SAchiad Shochat 
7193f89a643SAchiad Shochat 	if (!err)
7203f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
7213f89a643SAchiad Shochat 
7223f89a643SAchiad Shochat 	return err;
7233f89a643SAchiad Shochat 
7241b5daf11SMajd Dibbiny }
7251b5daf11SMajd Dibbiny 
7261b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
7271b5daf11SMajd Dibbiny 				u16 *max_pkeys)
7281b5daf11SMajd Dibbiny {
7291b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7301b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7311b5daf11SMajd Dibbiny 
7321b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7331b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7341b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
7351b5daf11SMajd Dibbiny 
7361b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7371b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7381b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
7391b5daf11SMajd Dibbiny 						pkey_table_size));
7401b5daf11SMajd Dibbiny 		return 0;
7411b5daf11SMajd Dibbiny 
7421b5daf11SMajd Dibbiny 	default:
7431b5daf11SMajd Dibbiny 		return -EINVAL;
7441b5daf11SMajd Dibbiny 	}
7451b5daf11SMajd Dibbiny }
7461b5daf11SMajd Dibbiny 
7471b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
7481b5daf11SMajd Dibbiny 				u32 *vendor_id)
7491b5daf11SMajd Dibbiny {
7501b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7511b5daf11SMajd Dibbiny 
7521b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7531b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7541b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
7551b5daf11SMajd Dibbiny 
7561b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7571b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7581b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
7591b5daf11SMajd Dibbiny 
7601b5daf11SMajd Dibbiny 	default:
7611b5daf11SMajd Dibbiny 		return -EINVAL;
7621b5daf11SMajd Dibbiny 	}
7631b5daf11SMajd Dibbiny }
7641b5daf11SMajd Dibbiny 
7651b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
7661b5daf11SMajd Dibbiny 				__be64 *node_guid)
7671b5daf11SMajd Dibbiny {
7681b5daf11SMajd Dibbiny 	u64 tmp;
7691b5daf11SMajd Dibbiny 	int err;
7701b5daf11SMajd Dibbiny 
7711b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7721b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7731b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7741b5daf11SMajd Dibbiny 
7751b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7761b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7773f89a643SAchiad Shochat 		break;
7783f89a643SAchiad Shochat 
7793f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7803f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7813f89a643SAchiad Shochat 		break;
7821b5daf11SMajd Dibbiny 
7831b5daf11SMajd Dibbiny 	default:
7841b5daf11SMajd Dibbiny 		return -EINVAL;
7851b5daf11SMajd Dibbiny 	}
7863f89a643SAchiad Shochat 
7873f89a643SAchiad Shochat 	if (!err)
7883f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7893f89a643SAchiad Shochat 
7903f89a643SAchiad Shochat 	return err;
7911b5daf11SMajd Dibbiny }
7921b5daf11SMajd Dibbiny 
7931b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
794bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
7951b5daf11SMajd Dibbiny };
7961b5daf11SMajd Dibbiny 
7971b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
7981b5daf11SMajd Dibbiny {
7991b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
8001b5daf11SMajd Dibbiny 
8011b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
8021b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
8031b5daf11SMajd Dibbiny 
8041b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
8051b5daf11SMajd Dibbiny 
8061b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
8071b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
8081b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
8091b5daf11SMajd Dibbiny }
8101b5daf11SMajd Dibbiny 
811e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
8122528e33eSMatan Barak 				struct ib_device_attr *props,
8132528e33eSMatan Barak 				struct ib_udata *uhw)
814e126ba97SEli Cohen {
81548357091SLeon Romanovsky 	size_t uhw_outlen = (uhw) ? uhw->outlen : 0;
816e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
817938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
818e126ba97SEli Cohen 	int err = -ENOMEM;
819288c01b7SEli Cohen 	int max_sq_desc;
820e126ba97SEli Cohen 	int max_rq_sg;
821e126ba97SEli Cohen 	int max_sq_sg;
822e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
82385c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
824402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
825402ca536SBodong Wang 	size_t resp_len;
826402ca536SBodong Wang 	u64 max_tso;
827e126ba97SEli Cohen 
828402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
82948357091SLeon Romanovsky 	if (uhw_outlen && uhw_outlen < resp_len)
830402ca536SBodong Wang 		return -EINVAL;
8316f26b2acSErez Alfasi 
832402ca536SBodong Wang 	resp.response_length = resp_len;
833402ca536SBodong Wang 
83448357091SLeon Romanovsky 	if (uhw && uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
8352528e33eSMatan Barak 		return -EINVAL;
8362528e33eSMatan Barak 
837e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
8381b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
8391b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
8401b5daf11SMajd Dibbiny 	if (err)
8411b5daf11SMajd Dibbiny 		return err;
8421b5daf11SMajd Dibbiny 
8431b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
8441b5daf11SMajd Dibbiny 	if (err)
8451b5daf11SMajd Dibbiny 		return err;
8461b5daf11SMajd Dibbiny 
8471b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
8481b5daf11SMajd Dibbiny 	if (err)
8491b5daf11SMajd Dibbiny 		return err;
850e126ba97SEli Cohen 
8519603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
8529603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
8539603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
854e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
855e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
856e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
8571a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
858938fe83cSSaeed Mahameed 
859938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
860e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
861938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
862e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
863938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
864e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
865938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
866e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
867d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
868d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
869d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
870d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
871b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
872b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
873d2370e0aSMatan Barak 	}
874e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
875938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
876c0a6cbb9SIsrael Rukshin 		props->device_cap_flags |= IB_DEVICE_INTEGRITY_HANDOVER;
8772dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8782dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8792dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8802dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8812dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8822dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8832dea9094SSagi Grimberg 	}
884938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
885f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
886e126ba97SEli Cohen 
88785c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
888e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
889e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
89088115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
891e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
892e8161334SNoa Osherovich 		}
893e8161334SNoa Osherovich 
894e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
895e8161334SNoa Osherovich 			props->raw_packet_caps |=
896e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
89788115fe7SBodong Wang 
898a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) {
899402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
900402ca536SBodong Wang 			if (max_tso) {
901402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
902402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
903402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
904402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
905402ca536SBodong Wang 			}
906402ca536SBodong Wang 		}
90731f69a82SYishai Hadas 
908a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) {
90931f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
91031f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
91131f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
91231f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
91331f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
91431f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
91531f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
91631f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
91731f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
91831f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
9194e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
9204e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
9212d93fc85SMatan Barak 			if (mlx5_accel_ipsec_device_caps(dev->mdev) &
9222d93fc85SMatan Barak 			    MLX5_ACCEL_IPSEC_CAP_DEVICE)
9232d93fc85SMatan Barak 				resp.rss_caps.rx_hash_fields_mask |=
9242d93fc85SMatan Barak 					MLX5_RX_HASH_IPSEC_SPI;
92531f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
92631f69a82SYishai Hadas 		}
92731f69a82SYishai Hadas 	} else {
928a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen)
92931f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
930a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen)
93131f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
932402ca536SBodong Wang 	}
933402ca536SBodong Wang 
934f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
935f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
936f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
937f0313965SErez Shitrit 	}
938f0313965SErez Shitrit 
93903404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
94085c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
94185c7c014SDaniel Jurgens 	    raw_support)
94203404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
94303404e8aSMaor Gottlieb 
9441d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
9451d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
9461d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
9471d54f890SYishai Hadas 
948cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
94985c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
95085c7c014SDaniel Jurgens 	    raw_support) {
951e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
952cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
953e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
954e8161334SNoa Osherovich 	}
955cff5a0f3SMajd Dibbiny 
95624da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
95724da0016SAriel Levkovich 		props->max_dm_size =
95824da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
95924da0016SAriel Levkovich 	}
96024da0016SAriel Levkovich 
961da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
962da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
963da6d6ba3SMaor Gottlieb 
964b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
965b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
966b1383aa6SNoa Osherovich 
9671b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
9681b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
969e126ba97SEli Cohen 
970e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
971e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
972938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
973938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
974938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
975938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
976288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
977288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
978288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
979e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
98033023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
98133023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
982986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
983938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9849f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
985938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
986938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
987938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
988938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
989938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
990938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
991938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
992e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
993e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
994911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
995911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
99662e3c379SMax Gurtovoy 	props->max_pi_fast_reg_page_list_len =
99762e3c379SMax Gurtovoy 		props->max_fast_reg_page_list_len / 2;
99836609056SYamin Friedman 	props->max_sgl_rd =
99936609056SYamin Friedman 		MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance);
1000776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
100181bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
1002938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1003938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
1004e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1005e126ba97SEli Cohen 					   props->max_mcast_grp;
1006e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
100786695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
10087c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
10097c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
1010e126ba97SEli Cohen 
1011e502b8b0SLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
101200815752SMoni Shoua 		if (dev->odp_caps.general_caps & IB_ODP_SUPPORT)
10138cdd312cSHaggai Eran 			props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
10148cdd312cSHaggai Eran 		props->odp_caps = dev->odp_caps;
1015a73a8955SMoni Shoua 		if (!uhw) {
1016a73a8955SMoni Shoua 			/* ODP for kernel QPs is not implemented for receive
1017a73a8955SMoni Shoua 			 * WQEs and SRQ WQEs
1018a73a8955SMoni Shoua 			 */
1019a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.rc_odp_caps &=
1020a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1021a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1022a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.uc_odp_caps &=
1023a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1024a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1025a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.ud_odp_caps &=
1026a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1027a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1028a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.xrc_odp_caps &=
1029a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1030a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1031a73a8955SMoni Shoua 		}
1032e502b8b0SLeon Romanovsky 	}
10338cdd312cSHaggai Eran 
1034051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
1035051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1036051f2630SLeon Romanovsky 
1037e53a9d26SParav Pandit 	if (mlx5_core_is_vf(mdev))
1038eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1039eff901d3SEli Cohen 
104031f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
104185c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
104231f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
104331f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
104431f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
104531f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
104631f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
104731f69a82SYishai Hadas 		props->max_wq_type_rq =
104831f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
104931f69a82SYishai Hadas 	}
105031f69a82SYishai Hadas 
1051eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
105278b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
1053eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
105478b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
1055eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
105678b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1057eb761894SArtemy Kovalyov 	}
1058eb761894SArtemy Kovalyov 
105989705e92SDanit Goldberg 	if (MLX5_CAP_GEN(mdev, tag_matching) &&
106089705e92SDanit Goldberg 	    MLX5_CAP_GEN(mdev, rndv_offload_rc)) {
106189705e92SDanit Goldberg 		props->tm_caps.flags = IB_TM_CAP_RNDV_RC;
106289705e92SDanit Goldberg 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
106389705e92SDanit Goldberg 	}
106489705e92SDanit Goldberg 
106587ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
106687ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
106787ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
106887ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
106987ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
107087ab3f52SYonatan Cohen 	}
107187ab3f52SYonatan Cohen 
1072a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), cqe_comp_caps) <= uhw_outlen) {
1073572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
1074572f46bfSYonatan Cohen 
1075572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
10767e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
1077572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
1078572f46bfSYonatan Cohen 					     cqe_compression_max_num);
1079572f46bfSYonatan Cohen 
10807e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
10817e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
10827e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
10836f1006a4SYonatan Cohen 
10846f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
10856f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
10866f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1087572f46bfSYonatan Cohen 		}
10887e43a2a5SBodong Wang 	}
10897e43a2a5SBodong Wang 
1090a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), packet_pacing_caps) <= uhw_outlen &&
109185c7c014SDaniel Jurgens 	    raw_support) {
1092d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1093d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
1094d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
1095d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1096d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
1097d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1098d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
1099d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
110061147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
110161147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
110261147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
110361147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
1104d949167dSBodong Wang 		}
1105d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1106d949167dSBodong Wang 	}
1107d949167dSBodong Wang 
1108a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes) <=
1109a762d460SLeon Romanovsky 	    uhw_outlen) {
1110795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
11119f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1112795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1113050da902SBodong Wang 
1114050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1115050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1116050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1117050da902SBodong Wang 
11189f885201SLeon Romanovsky 		resp.response_length +=
11199f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
11209f885201SLeon Romanovsky 	}
11219f885201SLeon Romanovsky 
1122a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), flags) <= uhw_outlen) {
1123de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
11247a0c8f42SGuy Levi 
1125de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1126de57f2adSGuy Levi 			resp.flags |=
1127de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
11287a0c8f42SGuy Levi 
11297a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
11307a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
11317e11b911SDanit Goldberg 		if (MLX5_CAP_GEN(mdev, qp_packet_based))
11327e11b911SDanit Goldberg 			resp.flags |=
11337e11b911SDanit Goldberg 				MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
11347249c8eaSGuy Levi 
11357249c8eaSGuy Levi 		resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1136de57f2adSGuy Levi 	}
11379f885201SLeon Romanovsky 
1138a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), sw_parsing_caps) <= uhw_outlen) {
113996dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
114096dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
114196dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
114296dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
114396dc3fc5SNoa Osherovich 
114496dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
114596dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
114696dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
114796dc3fc5SNoa Osherovich 
114896dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
114996dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
115096dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
115196dc3fc5SNoa Osherovich 
115296dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
115396dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
115496dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
115596dc3fc5SNoa Osherovich 		}
115696dc3fc5SNoa Osherovich 	}
115796dc3fc5SNoa Osherovich 
1158a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), striding_rq_caps) <= uhw_outlen &&
115985c7c014SDaniel Jurgens 	    raw_support) {
1160b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1161b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1162b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1163b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1164b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1165b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1166c16339b6SMark Zhang 			if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range))
1167c16339b6SMark Zhang 				resp.striding_rq_caps
1168c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1169c16339b6SMark Zhang 					MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1170c16339b6SMark Zhang 			else
1171c16339b6SMark Zhang 				resp.striding_rq_caps
1172c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1173b4f34597SNoa Osherovich 					MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1174b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1175b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1176b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1177b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1178b4f34597SNoa Osherovich 		}
1179b4f34597SNoa Osherovich 	}
1180b4f34597SNoa Osherovich 
1181a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), tunnel_offloads_caps) <= uhw_outlen) {
1182f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1183f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1184f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1185f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1186f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1187f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1188f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1189f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1190f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1191f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
119241e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre))
1193e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1194e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
119541e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_udp))
1196e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1197e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1198f95ef6cbSMaor Gottlieb 	}
1199f95ef6cbSMaor Gottlieb 
120048357091SLeon Romanovsky 	if (uhw_outlen) {
1201402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1202402ca536SBodong Wang 
1203402ca536SBodong Wang 		if (err)
1204402ca536SBodong Wang 			return err;
1205402ca536SBodong Wang 	}
1206402ca536SBodong Wang 
12071b5daf11SMajd Dibbiny 	return 0;
12081b5daf11SMajd Dibbiny }
1209e126ba97SEli Cohen 
12101b5daf11SMajd Dibbiny enum mlx5_ib_width {
12111b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
12121b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
12131b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
12141b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
12151b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
12161b5daf11SMajd Dibbiny };
12171b5daf11SMajd Dibbiny 
1218db7a691aSMichael Guralnik static void translate_active_width(struct ib_device *ibdev, u8 active_width,
12191b5daf11SMajd Dibbiny 				  u8 *ib_width)
12201b5daf11SMajd Dibbiny {
12211b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12221b5daf11SMajd Dibbiny 
1223db7a691aSMichael Guralnik 	if (active_width & MLX5_IB_WIDTH_1X)
12241b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1225d764970bSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_2X)
1226d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1227db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_4X)
12281b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1229db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_8X)
12301b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1231db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_12X)
12321b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1233db7a691aSMichael Guralnik 	else {
1234db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
12351b5daf11SMajd Dibbiny 			    (int)active_width);
1236db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
12371b5daf11SMajd Dibbiny 	}
12381b5daf11SMajd Dibbiny 
1239db7a691aSMichael Guralnik 	return;
12401b5daf11SMajd Dibbiny }
12411b5daf11SMajd Dibbiny 
12421b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
12431b5daf11SMajd Dibbiny {
12441b5daf11SMajd Dibbiny 	switch (mtu) {
12451b5daf11SMajd Dibbiny 	case 256: return 1;
12461b5daf11SMajd Dibbiny 	case 512: return 2;
12471b5daf11SMajd Dibbiny 	case 1024: return 3;
12481b5daf11SMajd Dibbiny 	case 2048: return 4;
12491b5daf11SMajd Dibbiny 	case 4096: return 5;
12501b5daf11SMajd Dibbiny 	default:
12511b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
12521b5daf11SMajd Dibbiny 		return -1;
12531b5daf11SMajd Dibbiny 	}
12541b5daf11SMajd Dibbiny }
12551b5daf11SMajd Dibbiny 
12561b5daf11SMajd Dibbiny enum ib_max_vl_num {
12571b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
12581b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
12591b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
12601b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
12611b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
12621b5daf11SMajd Dibbiny };
12631b5daf11SMajd Dibbiny 
12641b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
12651b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
12661b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
12671b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
12681b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
12691b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
12701b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
12711b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
12721b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
12731b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
12741b5daf11SMajd Dibbiny };
12751b5daf11SMajd Dibbiny 
12761b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
12771b5daf11SMajd Dibbiny 				u8 *max_vl_num)
12781b5daf11SMajd Dibbiny {
12791b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
12801b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
12811b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
12821b5daf11SMajd Dibbiny 		break;
12831b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
12841b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
12851b5daf11SMajd Dibbiny 		break;
12861b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
12871b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
12881b5daf11SMajd Dibbiny 		break;
12891b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
12901b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
12911b5daf11SMajd Dibbiny 		break;
12921b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
12931b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
12941b5daf11SMajd Dibbiny 		break;
12951b5daf11SMajd Dibbiny 
12961b5daf11SMajd Dibbiny 	default:
12971b5daf11SMajd Dibbiny 		return -EINVAL;
12981b5daf11SMajd Dibbiny 	}
12991b5daf11SMajd Dibbiny 
13001b5daf11SMajd Dibbiny 	return 0;
13011b5daf11SMajd Dibbiny }
13021b5daf11SMajd Dibbiny 
13031b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
13041b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
13051b5daf11SMajd Dibbiny {
13061b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
13071b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
13081b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1309046339eaSSaeed Mahameed 	u16 max_mtu;
1310046339eaSSaeed Mahameed 	u16 oper_mtu;
13111b5daf11SMajd Dibbiny 	int err;
13121b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
13131b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
13141b5daf11SMajd Dibbiny 
13151b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
13161b5daf11SMajd Dibbiny 	if (!rep) {
13171b5daf11SMajd Dibbiny 		err = -ENOMEM;
13181b5daf11SMajd Dibbiny 		goto out;
13191b5daf11SMajd Dibbiny 	}
13201b5daf11SMajd Dibbiny 
1321c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
13221b5daf11SMajd Dibbiny 
13231b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
13241b5daf11SMajd Dibbiny 	if (err)
13251b5daf11SMajd Dibbiny 		goto out;
13261b5daf11SMajd Dibbiny 
13271b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
13281b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
13291b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
13301b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
13311b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
13321b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
13331b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
13341b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
13351b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
13361b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
13371b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
13381b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
13391b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
13401b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
13411b5daf11SMajd Dibbiny 
13424106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
13434106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
13444106a758SMichael Guralnik 
13451b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
13461b5daf11SMajd Dibbiny 	if (err)
13471b5daf11SMajd Dibbiny 		goto out;
13481b5daf11SMajd Dibbiny 
1349db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1350db7a691aSMichael Guralnik 
1351d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
13521b5daf11SMajd Dibbiny 	if (err)
13531b5daf11SMajd Dibbiny 		goto out;
13541b5daf11SMajd Dibbiny 
1355facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
13561b5daf11SMajd Dibbiny 
13571b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
13581b5daf11SMajd Dibbiny 
1359facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
13601b5daf11SMajd Dibbiny 
13611b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
13621b5daf11SMajd Dibbiny 
13631b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
13641b5daf11SMajd Dibbiny 	if (err)
13651b5daf11SMajd Dibbiny 		goto out;
13661b5daf11SMajd Dibbiny 
13671b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
13681b5daf11SMajd Dibbiny 				   &props->max_vl_num);
13691b5daf11SMajd Dibbiny out:
13701b5daf11SMajd Dibbiny 	kfree(rep);
1371e126ba97SEli Cohen 	return err;
1372e126ba97SEli Cohen }
1373e126ba97SEli Cohen 
1374e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1375e126ba97SEli Cohen 		       struct ib_port_attr *props)
1376e126ba97SEli Cohen {
1377095b0927SIlan Tayari 	unsigned int count;
1378095b0927SIlan Tayari 	int ret;
1379095b0927SIlan Tayari 
13801b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13811b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1382095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1383095b0927SIlan Tayari 		break;
1384e126ba97SEli Cohen 
13851b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1386095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1387095b0927SIlan Tayari 		break;
13881b5daf11SMajd Dibbiny 
13893f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1390095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1391095b0927SIlan Tayari 		break;
13923f89a643SAchiad Shochat 
13931b5daf11SMajd Dibbiny 	default:
1394095b0927SIlan Tayari 		ret = -EINVAL;
1395e126ba97SEli Cohen 	}
1396095b0927SIlan Tayari 
1397095b0927SIlan Tayari 	if (!ret && props) {
1398b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1399b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1400b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1401b3cbd6f0SDaniel Jurgens 
1402b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1403b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1404b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1405b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1406b3cbd6f0SDaniel Jurgens 			 */
1407b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1408b3cbd6f0SDaniel Jurgens 			port = 1;
1409b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1410b3cbd6f0SDaniel Jurgens 		}
1411b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1412b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1413b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1414095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1415095b0927SIlan Tayari 	}
1416095b0927SIlan Tayari 	return ret;
1417e126ba97SEli Cohen }
1418e126ba97SEli Cohen 
14198e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
14208e6efa3aSMark Bloch 				  struct ib_port_attr *props)
14218e6efa3aSMark Bloch {
14228e6efa3aSMark Bloch 	int ret;
14238e6efa3aSMark Bloch 
142426628e2dSMark Bloch 	/* Only link layer == ethernet is valid for representors
142526628e2dSMark Bloch 	 * and we always use port 1
142626628e2dSMark Bloch 	 */
14278e6efa3aSMark Bloch 	ret = mlx5_query_port_roce(ibdev, port, props);
14288e6efa3aSMark Bloch 	if (ret || !props)
14298e6efa3aSMark Bloch 		return ret;
14308e6efa3aSMark Bloch 
14318e6efa3aSMark Bloch 	/* We don't support GIDS */
14328e6efa3aSMark Bloch 	props->gid_tbl_len = 0;
14338e6efa3aSMark Bloch 
14348e6efa3aSMark Bloch 	return ret;
14358e6efa3aSMark Bloch }
14368e6efa3aSMark Bloch 
1437e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1438e126ba97SEli Cohen 			     union ib_gid *gid)
1439e126ba97SEli Cohen {
14401b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
14411b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1442e126ba97SEli Cohen 
14431b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14441b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14451b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1446e126ba97SEli Cohen 
14471b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14481b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1449e126ba97SEli Cohen 
14501b5daf11SMajd Dibbiny 	default:
14511b5daf11SMajd Dibbiny 		return -EINVAL;
14521b5daf11SMajd Dibbiny 	}
1453e126ba97SEli Cohen 
1454e126ba97SEli Cohen }
1455e126ba97SEli Cohen 
1456b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1457b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1458b3cbd6f0SDaniel Jurgens {
1459b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1460b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1461b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1462b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1463b3cbd6f0SDaniel Jurgens 	int err;
1464b3cbd6f0SDaniel Jurgens 
1465b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1466b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1467b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1468b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1469b3cbd6f0SDaniel Jurgens 		 */
1470b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1471b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1472b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1473b3cbd6f0SDaniel Jurgens 	}
1474b3cbd6f0SDaniel Jurgens 
1475b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1476b3cbd6f0SDaniel Jurgens 					index, pkey);
1477b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1478b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1479b3cbd6f0SDaniel Jurgens 
1480b3cbd6f0SDaniel Jurgens 	return err;
1481b3cbd6f0SDaniel Jurgens }
1482b3cbd6f0SDaniel Jurgens 
1483e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1484e126ba97SEli Cohen 			      u16 *pkey)
1485e126ba97SEli Cohen {
14861b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14871b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14881b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1489e126ba97SEli Cohen 
14901b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14911b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1492b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
14931b5daf11SMajd Dibbiny 	default:
14941b5daf11SMajd Dibbiny 		return -EINVAL;
1495e126ba97SEli Cohen 	}
14961b5daf11SMajd Dibbiny }
1497e126ba97SEli Cohen 
1498e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1499e126ba97SEli Cohen 				 struct ib_device_modify *props)
1500e126ba97SEli Cohen {
1501e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1502e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1503e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1504e126ba97SEli Cohen 	int err;
1505e126ba97SEli Cohen 
1506e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1507e126ba97SEli Cohen 		return -EOPNOTSUPP;
1508e126ba97SEli Cohen 
1509e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1510e126ba97SEli Cohen 		return 0;
1511e126ba97SEli Cohen 
1512e126ba97SEli Cohen 	/*
1513e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1514e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1515e126ba97SEli Cohen 	 */
1516bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
15179603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1518e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1519e126ba97SEli Cohen 	if (err)
1520e126ba97SEli Cohen 		return err;
1521e126ba97SEli Cohen 
1522bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1523e126ba97SEli Cohen 
1524e126ba97SEli Cohen 	return err;
1525e126ba97SEli Cohen }
1526e126ba97SEli Cohen 
1527cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1528cdbe33d0SEli Cohen 				u32 value)
1529cdbe33d0SEli Cohen {
1530cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1531b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1532b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1533cdbe33d0SEli Cohen 	int err;
1534cdbe33d0SEli Cohen 
1535b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1536b3cbd6f0SDaniel Jurgens 	if (!mdev)
1537b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1538b3cbd6f0SDaniel Jurgens 
1539b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1540cdbe33d0SEli Cohen 	if (err)
1541b3cbd6f0SDaniel Jurgens 		goto out;
1542cdbe33d0SEli Cohen 
1543cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1544cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1545cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1546b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1547b3cbd6f0SDaniel Jurgens 		goto out;
1548cdbe33d0SEli Cohen 	}
1549cdbe33d0SEli Cohen 
1550cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1551cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1552b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1553b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1554b3cbd6f0SDaniel Jurgens 
1555b3cbd6f0SDaniel Jurgens out:
1556b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1557cdbe33d0SEli Cohen 
1558cdbe33d0SEli Cohen 	return err;
1559cdbe33d0SEli Cohen }
1560cdbe33d0SEli Cohen 
1561e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1562e126ba97SEli Cohen 			       struct ib_port_modify *props)
1563e126ba97SEli Cohen {
1564e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1565e126ba97SEli Cohen 	struct ib_port_attr attr;
1566e126ba97SEli Cohen 	u32 tmp;
1567e126ba97SEli Cohen 	int err;
1568cdbe33d0SEli Cohen 	u32 change_mask;
1569cdbe33d0SEli Cohen 	u32 value;
1570cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1571cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1572cdbe33d0SEli Cohen 
1573ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1574ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1575ec255879SMajd Dibbiny 	 */
1576ec255879SMajd Dibbiny 	if (!is_ib)
1577ec255879SMajd Dibbiny 		return 0;
1578ec255879SMajd Dibbiny 
1579cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1580cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1581cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1582cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1583cdbe33d0SEli Cohen 	}
1584e126ba97SEli Cohen 
1585e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1586e126ba97SEli Cohen 
1587c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1588e126ba97SEli Cohen 	if (err)
1589e126ba97SEli Cohen 		goto out;
1590e126ba97SEli Cohen 
1591e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1592e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1593e126ba97SEli Cohen 
15949603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1595e126ba97SEli Cohen 
1596e126ba97SEli Cohen out:
1597e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1598e126ba97SEli Cohen 	return err;
1599e126ba97SEli Cohen }
1600e126ba97SEli Cohen 
160130aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
160230aa60b3SEli Cohen {
160330aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
160430aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
160530aa60b3SEli Cohen }
160630aa60b3SEli Cohen 
160731a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
160831a78a5aSYishai Hadas {
160931a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
161031a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
161131a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
161231a78a5aSYishai Hadas 
161331a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
161431a78a5aSYishai Hadas }
161531a78a5aSYishai Hadas 
1616b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1617b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
161831a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1619b037c29aSEli Cohen {
1620b037c29aSEli Cohen 	int uars_per_sys_page;
1621b037c29aSEli Cohen 	int bfregs_per_sys_page;
1622b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1623b037c29aSEli Cohen 
1624b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1625b037c29aSEli Cohen 		return -EINVAL;
1626b037c29aSEli Cohen 
1627b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1628b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1629b037c29aSEli Cohen 
1630b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1631b037c29aSEli Cohen 		return -ENOMEM;
1632b037c29aSEli Cohen 
1633b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1634b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
163531a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1636b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1637b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1638b037c29aSEli Cohen 		return -EINVAL;
1639b037c29aSEli Cohen 
164031a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
164131a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
164231a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
164331a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
164431a78a5aSYishai Hadas 
164531a78a5aSYishai 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",
1646b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1647b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
164831a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
164931a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1650b037c29aSEli Cohen 
1651b037c29aSEli Cohen 	return 0;
1652b037c29aSEli Cohen }
1653b037c29aSEli Cohen 
1654b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1655b037c29aSEli Cohen {
1656b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1657b037c29aSEli Cohen 	int err;
1658b037c29aSEli Cohen 	int i;
1659b037c29aSEli Cohen 
1660b037c29aSEli Cohen 	bfregi = &context->bfregi;
166131a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1662b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1663b037c29aSEli Cohen 		if (err)
1664b037c29aSEli Cohen 			goto error;
1665b037c29aSEli Cohen 
1666b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1667b037c29aSEli Cohen 	}
16684ed131d0SYishai Hadas 
16694ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
16704ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
16714ed131d0SYishai Hadas 
1672b037c29aSEli Cohen 	return 0;
1673b037c29aSEli Cohen 
1674b037c29aSEli Cohen error:
1675b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1676b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1677b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1678b037c29aSEli Cohen 
1679b037c29aSEli Cohen 	return err;
1680b037c29aSEli Cohen }
1681b037c29aSEli Cohen 
168215177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
168315177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1684b037c29aSEli Cohen {
1685b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1686b037c29aSEli Cohen 	int i;
1687b037c29aSEli Cohen 
1688b037c29aSEli Cohen 	bfregi = &context->bfregi;
168915177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
16904ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
169115177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
169215177999SLeon Romanovsky 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1693b037c29aSEli Cohen }
1694b037c29aSEli Cohen 
16950042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1696a560f1d9SMark Bloch {
1697a560f1d9SMark Bloch 	int err = 0;
1698a560f1d9SMark Bloch 
1699a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
17000042f9e4SMark Bloch 	if (td)
1701a560f1d9SMark Bloch 		dev->lb.user_td++;
17020042f9e4SMark Bloch 	if (qp)
17030042f9e4SMark Bloch 		dev->lb.qps++;
1704a560f1d9SMark Bloch 
17050042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
17060042f9e4SMark Bloch 	    dev->lb.qps == 1) {
17070042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1708a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
17090042f9e4SMark Bloch 			dev->lb.enabled = true;
17100042f9e4SMark Bloch 		}
17110042f9e4SMark Bloch 	}
1712a560f1d9SMark Bloch 
1713a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1714a560f1d9SMark Bloch 
1715a560f1d9SMark Bloch 	return err;
1716a560f1d9SMark Bloch }
1717a560f1d9SMark Bloch 
17180042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1719a560f1d9SMark Bloch {
1720a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
17210042f9e4SMark Bloch 	if (td)
1722a560f1d9SMark Bloch 		dev->lb.user_td--;
17230042f9e4SMark Bloch 	if (qp)
17240042f9e4SMark Bloch 		dev->lb.qps--;
1725a560f1d9SMark Bloch 
17260042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
17270042f9e4SMark Bloch 	    dev->lb.qps == 0) {
17280042f9e4SMark Bloch 		if (dev->lb.enabled) {
1729a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
17300042f9e4SMark Bloch 			dev->lb.enabled = false;
17310042f9e4SMark Bloch 		}
17320042f9e4SMark Bloch 	}
1733a560f1d9SMark Bloch 
1734a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1735a560f1d9SMark Bloch }
1736a560f1d9SMark Bloch 
1737d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1738d2d19121SYishai Hadas 					  u16 uid)
1739c85023e1SHuy Nguyen {
1740c85023e1SHuy Nguyen 	int err;
1741c85023e1SHuy Nguyen 
1742cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1743cfdeb893SLeon Romanovsky 		return 0;
1744cfdeb893SLeon Romanovsky 
1745d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1746c85023e1SHuy Nguyen 	if (err)
1747c85023e1SHuy Nguyen 		return err;
1748c85023e1SHuy Nguyen 
1749c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17508978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17518978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1752c85023e1SHuy Nguyen 		return err;
1753c85023e1SHuy Nguyen 
17540042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1755c85023e1SHuy Nguyen }
1756c85023e1SHuy Nguyen 
1757d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1758d2d19121SYishai Hadas 					     u16 uid)
1759c85023e1SHuy Nguyen {
1760cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1761cfdeb893SLeon Romanovsky 		return;
1762cfdeb893SLeon Romanovsky 
1763d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1764c85023e1SHuy Nguyen 
1765c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17668978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17678978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1768c85023e1SHuy Nguyen 		return;
1769c85023e1SHuy Nguyen 
17700042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1771c85023e1SHuy Nguyen }
1772c85023e1SHuy Nguyen 
1773a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1774e126ba97SEli Cohen 				  struct ib_udata *udata)
1775e126ba97SEli Cohen {
1776a2a074efSLeon Romanovsky 	struct ib_device *ibdev = uctx->device;
1777e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1778b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1779b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
17805c99eaecSFeras Daoud 	struct mlx5_core_dev *mdev = dev->mdev;
1781a2a074efSLeon Romanovsky 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
17822f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
178378c0f98cSEli Cohen 	int ver;
1784e126ba97SEli Cohen 	int err;
1785a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1786a168a41cSMajd Dibbiny 				     max_cqe_version);
178725bb36e7SYonatan Cohen 	u32 dump_fill_mkey;
1788b037c29aSEli Cohen 	bool lib_uar_4k;
1789e126ba97SEli Cohen 
1790e126ba97SEli Cohen 	if (!dev->ib_active)
1791a2a074efSLeon Romanovsky 		return -EAGAIN;
1792e126ba97SEli Cohen 
1793e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
179478c0f98cSEli Cohen 		ver = 0;
1795e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
179678c0f98cSEli Cohen 		ver = 2;
179778c0f98cSEli Cohen 	else
1798a2a074efSLeon Romanovsky 		return -EINVAL;
179978c0f98cSEli Cohen 
1800e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1801e126ba97SEli Cohen 	if (err)
1802a2a074efSLeon Romanovsky 		return err;
1803e126ba97SEli Cohen 
1804a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1805a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
180678c0f98cSEli Cohen 
1807f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1808a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
1809b368d7cbSMatan Barak 
18102f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
18112f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
18122f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1813a2a074efSLeon Romanovsky 		return -EINVAL;
1814e126ba97SEli Cohen 
1815938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
181611f552e2SMichael Guralnik 	if (dev->wc_support)
1817938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1818b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1819938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1820938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1821938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1822938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1823938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1824f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1825f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1826f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
182730aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
182830aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
182930aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
183030aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1831b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1832b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1833e126ba97SEli Cohen 
1834c03faa56SMatan Barak 	if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1835c03faa56SMatan Barak 		if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1836c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1837c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1838c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1839c03faa56SMatan Barak 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1840c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1841c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1842c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1843c03faa56SMatan Barak 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1844c03faa56SMatan Barak 	}
1845c03faa56SMatan Barak 
184630aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
18472f5ff264SEli Cohen 	bfregi = &context->bfregi;
1848b037c29aSEli Cohen 
1849b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
185031a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1851b037c29aSEli Cohen 	if (err)
1852b037c29aSEli Cohen 		goto out_ctx;
1853b037c29aSEli Cohen 
18542f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1855b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
185631a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1857b037c29aSEli Cohen 				GFP_KERNEL);
1858b037c29aSEli Cohen 	if (!bfregi->count) {
1859e126ba97SEli Cohen 		err = -ENOMEM;
1860e126ba97SEli Cohen 		goto out_ctx;
1861e126ba97SEli Cohen 	}
1862e126ba97SEli Cohen 
1863b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1864b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1865e126ba97SEli Cohen 				    GFP_KERNEL);
1866b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1867e126ba97SEli Cohen 		err = -ENOMEM;
1868e126ba97SEli Cohen 		goto out_count;
1869e126ba97SEli Cohen 	}
1870e126ba97SEli Cohen 
1871b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1872b037c29aSEli Cohen 	if (err)
1873b037c29aSEli Cohen 		goto out_sys_pages;
1874b037c29aSEli Cohen 
1875a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1876fb98153bSYishai Hadas 		err = mlx5_ib_devx_create(dev, true);
187776dc5a84SYishai Hadas 		if (err < 0)
1878d2d19121SYishai Hadas 			goto out_uars;
187976dc5a84SYishai Hadas 		context->devx_uid = err;
1880a8b92ca1SYishai Hadas 	}
1881a8b92ca1SYishai Hadas 
1882d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1883d2d19121SYishai Hadas 					     context->devx_uid);
1884a8b92ca1SYishai Hadas 	if (err)
1885d2d19121SYishai Hadas 		goto out_devx;
1886a8b92ca1SYishai Hadas 
188725bb36e7SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
188825bb36e7SYonatan Cohen 		err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
188925bb36e7SYonatan Cohen 		if (err)
18908193abb6SJason Gunthorpe 			goto out_mdev;
189125bb36e7SYonatan Cohen 	}
189225bb36e7SYonatan Cohen 
1893e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1894e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1895e126ba97SEli Cohen 
18962f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1897508562d6SDaniel Jurgens 	resp.num_ports = dev->num_ports;
1898b368d7cbSMatan Barak 
1899a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), cqe_version) <= udata->outlen)
1900f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1901b368d7cbSMatan Barak 
1902a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), cmds_supp_uhw) <= udata->outlen) {
19036ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
19046ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1905402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1906402ca536SBodong Wang 	}
1907402ca536SBodong Wang 
1908a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), eth_min_inline) <= udata->outlen) {
190978984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
191078984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
191178984898SOr Gerlitz 			resp.eth_min_inline++;
191278984898SOr Gerlitz 		}
191378984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
191478984898SOr Gerlitz 	}
191578984898SOr Gerlitz 
1916a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), clock_info_versions) <= udata->outlen) {
19175c99eaecSFeras Daoud 		if (mdev->clock_info)
19185c99eaecSFeras Daoud 			resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
19195c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.clock_info_versions);
19205c99eaecSFeras Daoud 	}
19215c99eaecSFeras Daoud 
1922bc5c6eedSNoa Osherovich 	/*
1923bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1924bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1925bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1926bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1927bc5c6eedSNoa Osherovich 	 */
1928a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), hca_core_clock_offset) <= udata->outlen) {
1929de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1930b368d7cbSMatan Barak 			resp.comp_mask |=
1931b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1932b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1933de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1934de8d6e02SEli Cohen 		}
19355c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.hca_core_clock_offset);
1936b368d7cbSMatan Barak 	}
1937b368d7cbSMatan Barak 
1938a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), log_uar_size) <= udata->outlen)
193930aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
194030aa60b3SEli Cohen 
1941a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), num_uars_per_page) <= udata->outlen)
194230aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
194330aa60b3SEli Cohen 
1944a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), num_dyn_bfregs) <= udata->outlen) {
194531a78a5aSYishai Hadas 		resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
194631a78a5aSYishai Hadas 		resp.response_length += sizeof(resp.num_dyn_bfregs);
194731a78a5aSYishai Hadas 	}
194831a78a5aSYishai Hadas 
1949a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), dump_fill_mkey) <= udata->outlen) {
195025bb36e7SYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
195125bb36e7SYonatan Cohen 			resp.dump_fill_mkey = dump_fill_mkey;
195225bb36e7SYonatan Cohen 			resp.comp_mask |=
195325bb36e7SYonatan Cohen 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
195425bb36e7SYonatan Cohen 		}
195525bb36e7SYonatan Cohen 		resp.response_length += sizeof(resp.dump_fill_mkey);
195625bb36e7SYonatan Cohen 	}
195725bb36e7SYonatan Cohen 
1958b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1959e126ba97SEli Cohen 	if (err)
1960a8b92ca1SYishai Hadas 		goto out_mdev;
1961e126ba97SEli Cohen 
19622f5ff264SEli Cohen 	bfregi->ver = ver;
19632f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1964f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
196530aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
196630aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1967f72300c5SHaggai Abramovsky 
19687c34ec19SAviv Heller 	if (dev->lag_active) {
196995579e78SMark Bloch 		u8 port = mlx5_core_native_port_num(dev->mdev) - 1;
1970c6a21c38SMajd Dibbiny 
1971c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1972c6a21c38SMajd Dibbiny 			   atomic_add_return(
197395579e78SMark Bloch 				   1, &dev->port[port].roce.tx_port_affinity));
1974c6a21c38SMajd Dibbiny 	}
1975c6a21c38SMajd Dibbiny 
1976a2a074efSLeon Romanovsky 	return 0;
1977e126ba97SEli Cohen 
1978a8b92ca1SYishai Hadas out_mdev:
1979d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1980d2d19121SYishai Hadas out_devx:
1981a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
198276dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1983146d2f1aSmajd@mellanox.com 
1984e126ba97SEli Cohen out_uars:
1985b037c29aSEli Cohen 	deallocate_uars(dev, context);
1986b037c29aSEli Cohen 
1987b037c29aSEli Cohen out_sys_pages:
1988b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1989b037c29aSEli Cohen 
1990e126ba97SEli Cohen out_count:
19912f5ff264SEli Cohen 	kfree(bfregi->count);
1992e126ba97SEli Cohen 
1993e126ba97SEli Cohen out_ctx:
1994a2a074efSLeon Romanovsky 	return err;
1995e126ba97SEli Cohen }
1996e126ba97SEli Cohen 
1997a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1998e126ba97SEli Cohen {
1999e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2000e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
2001b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
2002e126ba97SEli Cohen 
2003b037c29aSEli Cohen 	bfregi = &context->bfregi;
2004d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2005d2d19121SYishai Hadas 
2006b037c29aSEli Cohen 	if (context->devx_uid)
200776dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
2008e126ba97SEli Cohen 
2009e126ba97SEli Cohen 	deallocate_uars(dev, context);
20102f5ff264SEli Cohen 	kfree(bfregi->sys_pages);
20112f5ff264SEli Cohen 	kfree(bfregi->count);
2012e126ba97SEli Cohen }
2013e126ba97SEli Cohen 
2014e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2015e126ba97SEli Cohen 				 int uar_idx)
2016e126ba97SEli Cohen {
2017b037c29aSEli Cohen 	int fw_uars_per_page;
2018b037c29aSEli Cohen 
2019b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2020b037c29aSEli Cohen 
2021aa8106f1SHuy Nguyen 	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2022e126ba97SEli Cohen }
2023e126ba97SEli Cohen 
2024e126ba97SEli Cohen static int get_command(unsigned long offset)
2025e126ba97SEli Cohen {
2026e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2027e126ba97SEli Cohen }
2028e126ba97SEli Cohen 
2029e126ba97SEli Cohen static int get_arg(unsigned long offset)
2030e126ba97SEli Cohen {
2031e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2032e126ba97SEli Cohen }
2033e126ba97SEli Cohen 
2034e126ba97SEli Cohen static int get_index(unsigned long offset)
2035e126ba97SEli Cohen {
2036e126ba97SEli Cohen 	return get_arg(offset);
2037e126ba97SEli Cohen }
2038e126ba97SEli Cohen 
20394ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
20404ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20414ed131d0SYishai Hadas {
20424ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20434ed131d0SYishai Hadas }
20444ed131d0SYishai Hadas 
20457c2344c3SMaor Gottlieb 
20467c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20477c2344c3SMaor Gottlieb {
20487c2344c3SMaor Gottlieb }
20497c2344c3SMaor Gottlieb 
205037aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2051e126ba97SEli Cohen {
205237aa5c36SGuy Levi 	switch (cmd) {
205337aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
205437aa5c36SGuy Levi 		return "WC";
2055e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
205637aa5c36SGuy Levi 		return "best effort WC";
205737aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
205837aa5c36SGuy Levi 		return "NC";
205924da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
206024da0016SAriel Levkovich 		return "Device Memory";
206137aa5c36SGuy Levi 	default:
206237aa5c36SGuy Levi 		return NULL;
206337aa5c36SGuy Levi 	}
206437aa5c36SGuy Levi }
206537aa5c36SGuy Levi 
20665c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20675c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20685c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20695c99eaecSFeras Daoud {
20704eb6ab13SJason Gunthorpe 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
20714eb6ab13SJason Gunthorpe 	    !(vma->vm_flags & VM_SHARED))
20725c99eaecSFeras Daoud 		return -EINVAL;
20735c99eaecSFeras Daoud 
20745c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20755c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20765c99eaecSFeras Daoud 
20774eb6ab13SJason Gunthorpe 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
20785c99eaecSFeras Daoud 		return -EPERM;
2079c660133cSJason Gunthorpe 	vma->vm_flags &= ~VM_MAYWRITE;
20805c99eaecSFeras Daoud 
2081ddcdc368SJason Gunthorpe 	if (!dev->mdev->clock_info)
20825c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20835c99eaecSFeras Daoud 
20844eb6ab13SJason Gunthorpe 	return vm_insert_page(vma, vma->vm_start,
20854eb6ab13SJason Gunthorpe 			      virt_to_page(dev->mdev->clock_info));
20865c99eaecSFeras Daoud }
20875c99eaecSFeras Daoud 
2088dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
2089dc2316ebSYishai Hadas {
2090dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
2091dc2316ebSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device);
20927be76befSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
2093dc2316ebSYishai Hadas 	struct mlx5_ib_dm *mdm;
2094dc2316ebSYishai Hadas 
2095dc2316ebSYishai Hadas 	switch (mentry->mmap_flag) {
2096dc2316ebSYishai Hadas 	case MLX5_IB_MMAP_TYPE_MEMIC:
2097dc2316ebSYishai Hadas 		mdm = container_of(mentry, struct mlx5_ib_dm, mentry);
2098dc2316ebSYishai Hadas 		mlx5_cmd_dealloc_memic(&dev->dm, mdm->dev_addr,
2099dc2316ebSYishai Hadas 				       mdm->size);
2100dc2316ebSYishai Hadas 		kfree(mdm);
2101dc2316ebSYishai Hadas 		break;
21027be76befSYishai Hadas 	case MLX5_IB_MMAP_TYPE_VAR:
21037be76befSYishai Hadas 		mutex_lock(&var_table->bitmap_lock);
21047be76befSYishai Hadas 		clear_bit(mentry->page_idx, var_table->bitmap);
21057be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
21067be76befSYishai Hadas 		kfree(mentry);
21077be76befSYishai Hadas 		break;
2108dc2316ebSYishai Hadas 	default:
2109dc2316ebSYishai Hadas 		WARN_ON(true);
2110dc2316ebSYishai Hadas 	}
2111dc2316ebSYishai Hadas }
2112dc2316ebSYishai Hadas 
211337aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
21147c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
21157c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
211637aa5c36SGuy Levi {
21172f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
211837aa5c36SGuy Levi 	int err;
211937aa5c36SGuy Levi 	unsigned long idx;
2120aa09ea6eSKamal Heib 	phys_addr_t pfn;
212137aa5c36SGuy Levi 	pgprot_t prot;
21224ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
21234ed131d0SYishai Hadas 	u32 uar_index;
21244ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
21254ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
21264ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2127b037c29aSEli Cohen 
2128b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2129b037c29aSEli Cohen 		return -EINVAL;
2130b037c29aSEli Cohen 
21314ed131d0SYishai Hadas 	if (dyn_uar)
21324ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
21334ed131d0SYishai Hadas 	else
2134b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
21354ed131d0SYishai Hadas 
21364ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21374ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21384ed131d0SYishai Hadas 			     idx, max_valid_idx);
2139b037c29aSEli Cohen 		return -EINVAL;
2140b037c29aSEli Cohen 	}
214137aa5c36SGuy Levi 
214237aa5c36SGuy Levi 	switch (cmd) {
214337aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21444ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
214537aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
214637aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
214737aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
214837aa5c36SGuy Levi 		break;
214937aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
215037aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
215137aa5c36SGuy Levi 		break;
215237aa5c36SGuy Levi 	default:
215337aa5c36SGuy Levi 		return -EINVAL;
215437aa5c36SGuy Levi 	}
215537aa5c36SGuy Levi 
21564ed131d0SYishai Hadas 	if (dyn_uar) {
21574ed131d0SYishai Hadas 		int uars_per_page;
21584ed131d0SYishai Hadas 
21594ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21604ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21614ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21624ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21634ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21644ed131d0SYishai Hadas 			return -EINVAL;
21654ed131d0SYishai Hadas 		}
21664ed131d0SYishai Hadas 
21674ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21684ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21694ed131d0SYishai Hadas 		 * page holds its count.
21704ed131d0SYishai Hadas 		 */
21714ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
21724ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
21734ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
21744ed131d0SYishai Hadas 			return -EINVAL;
21754ed131d0SYishai Hadas 		}
21764ed131d0SYishai Hadas 
21774ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
21784ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
21794ed131d0SYishai Hadas 
21804ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
21814ed131d0SYishai Hadas 		if (err) {
21824ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
21834ed131d0SYishai Hadas 			goto free_bfreg;
21844ed131d0SYishai Hadas 		}
21854ed131d0SYishai Hadas 	} else {
21864ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
21874ed131d0SYishai Hadas 	}
21884ed131d0SYishai Hadas 
21894ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
219037aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2191e126ba97SEli Cohen 
2192e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2193c043ff2cSMichal Kalderon 				prot, NULL);
219437aa5c36SGuy Levi 	if (err) {
21958f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2196e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
21978f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
21984ed131d0SYishai Hadas 		goto err;
219937aa5c36SGuy Levi 	}
2200e126ba97SEli Cohen 
22014ed131d0SYishai Hadas 	if (dyn_uar)
22024ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
22034ed131d0SYishai Hadas 	return 0;
22044ed131d0SYishai Hadas 
22054ed131d0SYishai Hadas err:
22064ed131d0SYishai Hadas 	if (!dyn_uar)
22074ed131d0SYishai Hadas 		return err;
22084ed131d0SYishai Hadas 
22094ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
22104ed131d0SYishai Hadas 
22114ed131d0SYishai Hadas free_bfreg:
22124ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
22134ed131d0SYishai Hadas 
22144ed131d0SYishai Hadas 	return err;
221537aa5c36SGuy Levi }
221637aa5c36SGuy Levi 
2217dc2316ebSYishai Hadas static int add_dm_mmap_entry(struct ib_ucontext *context,
2218dc2316ebSYishai Hadas 			     struct mlx5_ib_dm *mdm,
2219dc2316ebSYishai Hadas 			     u64 address)
222024da0016SAriel Levkovich {
2221dc2316ebSYishai Hadas 	mdm->mentry.mmap_flag = MLX5_IB_MMAP_TYPE_MEMIC;
2222dc2316ebSYishai Hadas 	mdm->mentry.address = address;
2223dc2316ebSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
2224dc2316ebSYishai Hadas 			context, &mdm->mentry.rdma_entry,
2225dc2316ebSYishai Hadas 			mdm->size,
2226dc2316ebSYishai Hadas 			MLX5_IB_MMAP_DEVICE_MEM << 16,
2227dc2316ebSYishai Hadas 			(MLX5_IB_MMAP_DEVICE_MEM << 16) + (1UL << 16) - 1);
2228dc2316ebSYishai Hadas }
222924da0016SAriel Levkovich 
2230dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma)
2231dc2316ebSYishai Hadas {
2232dc2316ebSYishai Hadas 	unsigned long idx;
2233dc2316ebSYishai Hadas 	u8 command;
2234dc2316ebSYishai Hadas 
2235dc2316ebSYishai Hadas 	command = get_command(vma->vm_pgoff);
2236dc2316ebSYishai Hadas 	idx = get_extended_index(vma->vm_pgoff);
2237dc2316ebSYishai Hadas 
2238dc2316ebSYishai Hadas 	return (command << 16 | idx);
2239dc2316ebSYishai Hadas }
2240dc2316ebSYishai Hadas 
2241dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev,
2242dc2316ebSYishai Hadas 			       struct vm_area_struct *vma,
2243dc2316ebSYishai Hadas 			       struct ib_ucontext *ucontext)
2244dc2316ebSYishai Hadas {
2245dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry;
2246dc2316ebSYishai Hadas 	struct rdma_user_mmap_entry *entry;
2247dc2316ebSYishai Hadas 	unsigned long pgoff;
2248dc2316ebSYishai Hadas 	pgprot_t prot;
2249dc2316ebSYishai Hadas 	phys_addr_t pfn;
2250dc2316ebSYishai Hadas 	int ret;
2251dc2316ebSYishai Hadas 
2252dc2316ebSYishai Hadas 	pgoff = mlx5_vma_to_pgoff(vma);
2253dc2316ebSYishai Hadas 	entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff);
2254dc2316ebSYishai Hadas 	if (!entry)
225524da0016SAriel Levkovich 		return -EINVAL;
225624da0016SAriel Levkovich 
2257dc2316ebSYishai Hadas 	mentry = to_mmmap(entry);
2258dc2316ebSYishai Hadas 	pfn = (mentry->address >> PAGE_SHIFT);
22593f59b6c3SYishai Hadas 	if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR)
22603f59b6c3SYishai Hadas 		prot = pgprot_noncached(vma->vm_page_prot);
22613f59b6c3SYishai Hadas 	else
2262dc2316ebSYishai Hadas 		prot = pgprot_writecombine(vma->vm_page_prot);
2263dc2316ebSYishai Hadas 	ret = rdma_user_mmap_io(ucontext, vma, pfn,
2264dc2316ebSYishai Hadas 				entry->npages * PAGE_SIZE,
2265dc2316ebSYishai Hadas 				prot,
2266dc2316ebSYishai Hadas 				entry);
2267dc2316ebSYishai Hadas 	rdma_user_mmap_entry_put(&mentry->rdma_entry);
2268dc2316ebSYishai Hadas 	return ret;
226924da0016SAriel Levkovich }
227024da0016SAriel Levkovich 
22717be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry)
22727be76befSYishai Hadas {
22739b6d3bbcSLeon Romanovsky 	u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF;
22749b6d3bbcSLeon Romanovsky 	u64 index = entry->rdma_entry.start_pgoff & 0xFFFF;
22757be76befSYishai Hadas 
22767be76befSYishai Hadas 	return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) |
22777be76befSYishai Hadas 		(index & 0xFF)) << PAGE_SHIFT;
22787be76befSYishai Hadas }
22797be76befSYishai Hadas 
228037aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
228137aa5c36SGuy Levi {
228237aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
228337aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
228437aa5c36SGuy Levi 	unsigned long command;
228537aa5c36SGuy Levi 	phys_addr_t pfn;
228637aa5c36SGuy Levi 
228737aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
228837aa5c36SGuy Levi 	switch (command) {
228937aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
2290*1f3db161SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
2291*1f3db161SYishai Hadas 		if (!dev->wc_support)
2292*1f3db161SYishai Hadas 			return -EPERM;
2293*1f3db161SYishai Hadas 		fallthrough;
229437aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
229537aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
22967c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2297e126ba97SEli Cohen 
2298e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2299e126ba97SEli Cohen 		return -ENOSYS;
2300e126ba97SEli Cohen 
2301d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2302d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2303d69e3bcfSMatan Barak 			return -EINVAL;
2304d69e3bcfSMatan Barak 
23056cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2306d69e3bcfSMatan Barak 			return -EPERM;
2307c660133cSJason Gunthorpe 		vma->vm_flags &= ~VM_MAYWRITE;
2308d69e3bcfSMatan Barak 
2309d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2310d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2311d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2312d69e3bcfSMatan Barak 
2313d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2314d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2315d69e3bcfSMatan Barak 			PAGE_SHIFT;
2316d5e560d3SJason Gunthorpe 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2317d5e560d3SJason Gunthorpe 					 PAGE_SIZE,
2318c043ff2cSMichal Kalderon 					 pgprot_noncached(vma->vm_page_prot),
2319c043ff2cSMichal Kalderon 					 NULL);
23205c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
23215c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2322d69e3bcfSMatan Barak 
2323e126ba97SEli Cohen 	default:
2324dc2316ebSYishai Hadas 		return mlx5_ib_mmap_offset(dev, vma, ibcontext);
2325e126ba97SEli Cohen 	}
2326e126ba97SEli Cohen 
2327e126ba97SEli Cohen 	return 0;
2328e126ba97SEli Cohen }
2329e126ba97SEli Cohen 
233025c13324SAriel Levkovich static inline int check_dm_type_support(struct mlx5_ib_dev *dev,
233125c13324SAriel Levkovich 					u32 type)
233225c13324SAriel Levkovich {
233325c13324SAriel Levkovich 	switch (type) {
233425c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
233525c13324SAriel Levkovich 		if (!MLX5_CAP_DEV_MEM(dev->mdev, memic))
233625c13324SAriel Levkovich 			return -EOPNOTSUPP;
233725c13324SAriel Levkovich 		break;
233825c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2339c9b9dcb4SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
234025c13324SAriel Levkovich 		if (!capable(CAP_SYS_RAWIO) ||
234125c13324SAriel Levkovich 		    !capable(CAP_NET_RAW))
234225c13324SAriel Levkovich 			return -EPERM;
234325c13324SAriel Levkovich 
234425c13324SAriel Levkovich 		if (!(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner) ||
234525c13324SAriel Levkovich 		      MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner)))
234625c13324SAriel Levkovich 			return -EOPNOTSUPP;
234725c13324SAriel Levkovich 		break;
234825c13324SAriel Levkovich 	}
234925c13324SAriel Levkovich 
235025c13324SAriel Levkovich 	return 0;
235125c13324SAriel Levkovich }
235225c13324SAriel Levkovich 
23533b113a1eSAriel Levkovich static int handle_alloc_dm_memic(struct ib_ucontext *ctx,
23543b113a1eSAriel Levkovich 				 struct mlx5_ib_dm *dm,
235524da0016SAriel Levkovich 				 struct ib_dm_alloc_attr *attr,
235624da0016SAriel Levkovich 				 struct uverbs_attr_bundle *attrs)
235724da0016SAriel Levkovich {
23583b113a1eSAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
235924da0016SAriel Levkovich 	u64 start_offset;
2360dc2316ebSYishai Hadas 	u16 page_idx;
236124da0016SAriel Levkovich 	int err;
2362dc2316ebSYishai Hadas 	u64 address;
236324da0016SAriel Levkovich 
23643b113a1eSAriel Levkovich 	dm->size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
236524da0016SAriel Levkovich 
23663b113a1eSAriel Levkovich 	err = mlx5_cmd_alloc_memic(dm_db, &dm->dev_addr,
23673b113a1eSAriel Levkovich 				   dm->size, attr->alignment);
236824da0016SAriel Levkovich 	if (err)
23693b113a1eSAriel Levkovich 		return err;
237024da0016SAriel Levkovich 
2371dc2316ebSYishai Hadas 	address = dm->dev_addr & PAGE_MASK;
2372dc2316ebSYishai Hadas 	err = add_dm_mmap_entry(ctx, dm, address);
237324da0016SAriel Levkovich 	if (err)
237424da0016SAriel Levkovich 		goto err_dealloc;
237524da0016SAriel Levkovich 
2376dc2316ebSYishai Hadas 	page_idx = dm->mentry.rdma_entry.start_pgoff & 0xFFFF;
2377dc2316ebSYishai Hadas 	err = uverbs_copy_to(attrs,
2378dc2316ebSYishai Hadas 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2379dc2316ebSYishai Hadas 			     &page_idx,
2380dc2316ebSYishai Hadas 			     sizeof(page_idx));
2381dc2316ebSYishai Hadas 	if (err)
2382dc2316ebSYishai Hadas 		goto err_copy;
2383dc2316ebSYishai Hadas 
23843b113a1eSAriel Levkovich 	start_offset = dm->dev_addr & ~PAGE_MASK;
23853b113a1eSAriel Levkovich 	err = uverbs_copy_to(attrs,
23863b113a1eSAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
23873b113a1eSAriel Levkovich 			     &start_offset, sizeof(start_offset));
23883b113a1eSAriel Levkovich 	if (err)
2389dc2316ebSYishai Hadas 		goto err_copy;
23903b113a1eSAriel Levkovich 
23913b113a1eSAriel Levkovich 	return 0;
23923b113a1eSAriel Levkovich 
2393dc2316ebSYishai Hadas err_copy:
2394dc2316ebSYishai Hadas 	rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
23953b113a1eSAriel Levkovich err_dealloc:
23963b113a1eSAriel Levkovich 	mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
23973b113a1eSAriel Levkovich 
23983b113a1eSAriel Levkovich 	return err;
23993b113a1eSAriel Levkovich }
24003b113a1eSAriel Levkovich 
240125c13324SAriel Levkovich static int handle_alloc_dm_sw_icm(struct ib_ucontext *ctx,
240225c13324SAriel Levkovich 				  struct mlx5_ib_dm *dm,
240325c13324SAriel Levkovich 				  struct ib_dm_alloc_attr *attr,
240425c13324SAriel Levkovich 				  struct uverbs_attr_bundle *attrs,
240525c13324SAriel Levkovich 				  int type)
240625c13324SAriel Levkovich {
2407c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ctx->device)->mdev;
240825c13324SAriel Levkovich 	u64 act_size;
240925c13324SAriel Levkovich 	int err;
241025c13324SAriel Levkovich 
241125c13324SAriel Levkovich 	/* Allocation size must a multiple of the basic block size
241225c13324SAriel Levkovich 	 * and a power of 2.
241325c13324SAriel Levkovich 	 */
2414c9b9dcb4SAriel Levkovich 	act_size = round_up(attr->length, MLX5_SW_ICM_BLOCK_SIZE(dev));
241525c13324SAriel Levkovich 	act_size = roundup_pow_of_two(act_size);
241625c13324SAriel Levkovich 
241725c13324SAriel Levkovich 	dm->size = act_size;
2418c9b9dcb4SAriel Levkovich 	err = mlx5_dm_sw_icm_alloc(dev, type, act_size,
241925c13324SAriel Levkovich 				   to_mucontext(ctx)->devx_uid, &dm->dev_addr,
242025c13324SAriel Levkovich 				   &dm->icm_dm.obj_id);
242125c13324SAriel Levkovich 	if (err)
242225c13324SAriel Levkovich 		return err;
242325c13324SAriel Levkovich 
242425c13324SAriel Levkovich 	err = uverbs_copy_to(attrs,
242525c13324SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
242625c13324SAriel Levkovich 			     &dm->dev_addr, sizeof(dm->dev_addr));
242725c13324SAriel Levkovich 	if (err)
2428c9b9dcb4SAriel Levkovich 		mlx5_dm_sw_icm_dealloc(dev, type, dm->size,
2429c9b9dcb4SAriel Levkovich 				       to_mucontext(ctx)->devx_uid, dm->dev_addr,
2430c9b9dcb4SAriel Levkovich 				       dm->icm_dm.obj_id);
243125c13324SAriel Levkovich 
243225c13324SAriel Levkovich 	return err;
243325c13324SAriel Levkovich }
243425c13324SAriel Levkovich 
24353b113a1eSAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
24363b113a1eSAriel Levkovich 			       struct ib_ucontext *context,
24373b113a1eSAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
24383b113a1eSAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
24393b113a1eSAriel Levkovich {
24403b113a1eSAriel Levkovich 	struct mlx5_ib_dm *dm;
24413b113a1eSAriel Levkovich 	enum mlx5_ib_uapi_dm_type type;
24423b113a1eSAriel Levkovich 	int err;
24433b113a1eSAriel Levkovich 
24443b113a1eSAriel Levkovich 	err = uverbs_get_const_default(&type, attrs,
24453b113a1eSAriel Levkovich 				       MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
24463b113a1eSAriel Levkovich 				       MLX5_IB_UAPI_DM_TYPE_MEMIC);
24473b113a1eSAriel Levkovich 	if (err)
24483b113a1eSAriel Levkovich 		return ERR_PTR(err);
24493b113a1eSAriel Levkovich 
24503b113a1eSAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_dm req: dm_type=%d user_length=0x%llx log_alignment=%d\n",
24513b113a1eSAriel Levkovich 		    type, attr->length, attr->alignment);
24523b113a1eSAriel Levkovich 
245325c13324SAriel Levkovich 	err = check_dm_type_support(to_mdev(ibdev), type);
245425c13324SAriel Levkovich 	if (err)
245525c13324SAriel Levkovich 		return ERR_PTR(err);
245625c13324SAriel Levkovich 
24573b113a1eSAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
24583b113a1eSAriel Levkovich 	if (!dm)
24593b113a1eSAriel Levkovich 		return ERR_PTR(-ENOMEM);
24603b113a1eSAriel Levkovich 
24613b113a1eSAriel Levkovich 	dm->type = type;
24623b113a1eSAriel Levkovich 
24633b113a1eSAriel Levkovich 	switch (type) {
24643b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
24653b113a1eSAriel Levkovich 		err = handle_alloc_dm_memic(context, dm,
24663b113a1eSAriel Levkovich 					    attr,
24673b113a1eSAriel Levkovich 					    attrs);
24683b113a1eSAriel Levkovich 		break;
246925c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2470c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2471c9b9dcb4SAriel Levkovich 					     attr, attrs,
2472c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_STEERING);
2473c9b9dcb4SAriel Levkovich 		break;
247425c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2475c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2476c9b9dcb4SAriel Levkovich 					     attr, attrs,
2477c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_HEADER_MODIFY);
247825c13324SAriel Levkovich 		break;
24793b113a1eSAriel Levkovich 	default:
24803b113a1eSAriel Levkovich 		err = -EOPNOTSUPP;
24813b113a1eSAriel Levkovich 	}
24823b113a1eSAriel Levkovich 
24833b113a1eSAriel Levkovich 	if (err)
24843b113a1eSAriel Levkovich 		goto err_free;
248524da0016SAriel Levkovich 
248624da0016SAriel Levkovich 	return &dm->ibdm;
248724da0016SAriel Levkovich 
248824da0016SAriel Levkovich err_free:
248924da0016SAriel Levkovich 	kfree(dm);
249024da0016SAriel Levkovich 	return ERR_PTR(err);
249124da0016SAriel Levkovich }
249224da0016SAriel Levkovich 
2493c4367a26SShamir Rabinovitch int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
249424da0016SAriel Levkovich {
249525c13324SAriel Levkovich 	struct mlx5_ib_ucontext *ctx = rdma_udata_to_drv_context(
249625c13324SAriel Levkovich 		&attrs->driver_udata, struct mlx5_ib_ucontext, ibucontext);
2497c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ibdm->device)->mdev;
249824da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
249924da0016SAriel Levkovich 	int ret;
250024da0016SAriel Levkovich 
25013b113a1eSAriel Levkovich 	switch (dm->type) {
25023b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
2503dc2316ebSYishai Hadas 		rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
2504dc2316ebSYishai Hadas 		return 0;
250525c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2506c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_STEERING,
2507c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
2508c9b9dcb4SAriel Levkovich 					     dm->icm_dm.obj_id);
2509c9b9dcb4SAriel Levkovich 		if (ret)
2510c9b9dcb4SAriel Levkovich 			return ret;
2511c9b9dcb4SAriel Levkovich 		break;
251225c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2513c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_HEADER_MODIFY,
2514c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
251525c13324SAriel Levkovich 					     dm->icm_dm.obj_id);
251625c13324SAriel Levkovich 		if (ret)
251725c13324SAriel Levkovich 			return ret;
25183b113a1eSAriel Levkovich 		break;
25193b113a1eSAriel Levkovich 	default:
25203b113a1eSAriel Levkovich 		return -EOPNOTSUPP;
25213b113a1eSAriel Levkovich 	}
252224da0016SAriel Levkovich 
252324da0016SAriel Levkovich 	kfree(dm);
252424da0016SAriel Levkovich 
252524da0016SAriel Levkovich 	return 0;
252624da0016SAriel Levkovich }
252724da0016SAriel Levkovich 
2528ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2529e126ba97SEli Cohen {
253021a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
253121a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2532e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2533e126ba97SEli Cohen 	int err;
2534a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2535a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)]   = {};
2536a1069c1cSYishai Hadas 	u16 uid = 0;
2537ff23dfa1SShamir Rabinovitch 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2538ff23dfa1SShamir Rabinovitch 		udata, struct mlx5_ib_ucontext, ibucontext);
2539e126ba97SEli Cohen 
2540ff23dfa1SShamir Rabinovitch 	uid = context ? context->devx_uid : 0;
2541a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2542a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
2543a1069c1cSYishai Hadas 	err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2544a1069c1cSYishai Hadas 			    out, sizeof(out));
254521a428a0SLeon Romanovsky 	if (err)
254621a428a0SLeon Romanovsky 		return err;
2547e126ba97SEli Cohen 
2548a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2549a1069c1cSYishai Hadas 	pd->uid = uid;
2550ff23dfa1SShamir Rabinovitch 	if (udata) {
2551e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2552e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2553a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
255421a428a0SLeon Romanovsky 			return -EFAULT;
2555e126ba97SEli Cohen 		}
2556e126ba97SEli Cohen 	}
2557e126ba97SEli Cohen 
255821a428a0SLeon Romanovsky 	return 0;
2559e126ba97SEli Cohen }
2560e126ba97SEli Cohen 
2561c4367a26SShamir Rabinovitch static void mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2562e126ba97SEli Cohen {
2563e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2564e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2565e126ba97SEli Cohen 
2566a1069c1cSYishai Hadas 	mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2567e126ba97SEli Cohen }
2568e126ba97SEli Cohen 
2569466fa6d2SMaor Gottlieb enum {
2570466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2571466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
257271c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_INNER_BIT,
257371c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_MISC2_BIT
2574466fa6d2SMaor Gottlieb };
2575038d2ef8SMaor Gottlieb 
2576466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2577466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2578466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2579466fa6d2SMaor Gottlieb 
2580466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2581466fa6d2SMaor Gottlieb {
2582466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2583466fa6d2SMaor Gottlieb 
2584466fa6d2SMaor Gottlieb 	match_criteria_enable =
2585466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2586466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2587466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2588466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2589466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2590466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2591466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2592466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
259371c6e863SAriel Levkovich 	match_criteria_enable |=
259471c6e863SAriel Levkovich 		(!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
259571c6e863SAriel Levkovich 		MATCH_CRITERIA_ENABLE_MISC2_BIT;
2596466fa6d2SMaor Gottlieb 
2597466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2598038d2ef8SMaor Gottlieb }
2599038d2ef8SMaor Gottlieb 
26006113cc44SMaor Gottlieb static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2601ca0d4753SMaor Gottlieb {
26026113cc44SMaor Gottlieb 	u8 entry_mask;
26036113cc44SMaor Gottlieb 	u8 entry_val;
26046113cc44SMaor Gottlieb 	int err = 0;
26056113cc44SMaor Gottlieb 
26066113cc44SMaor Gottlieb 	if (!mask)
26076113cc44SMaor Gottlieb 		goto out;
26086113cc44SMaor Gottlieb 
26096113cc44SMaor Gottlieb 	entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
26106113cc44SMaor Gottlieb 			      ip_protocol);
26116113cc44SMaor Gottlieb 	entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
26126113cc44SMaor Gottlieb 			     ip_protocol);
26136113cc44SMaor Gottlieb 	if (!entry_mask) {
2614ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2615ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
26166113cc44SMaor Gottlieb 		goto out;
26176113cc44SMaor Gottlieb 	}
26186113cc44SMaor Gottlieb 	/* Don't override existing ip protocol */
26196113cc44SMaor Gottlieb 	if (mask != entry_mask || val != entry_val)
26206113cc44SMaor Gottlieb 		err = -EINVAL;
26216113cc44SMaor Gottlieb out:
26226113cc44SMaor Gottlieb 	return err;
2623ca0d4753SMaor Gottlieb }
2624ca0d4753SMaor Gottlieb 
262537da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
26262d1e697eSMoses Reuben 			   bool inner)
26272d1e697eSMoses Reuben {
26282d1e697eSMoses Reuben 	if (inner) {
26292d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26302d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
26312d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26322d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
26332d1e697eSMoses Reuben 	} else {
26342d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26352d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
26362d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26372d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
26382d1e697eSMoses Reuben 	}
26392d1e697eSMoses Reuben }
26402d1e697eSMoses Reuben 
2641ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2642ca0d4753SMaor Gottlieb {
2643ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2644ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2645ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2646ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2647ca0d4753SMaor Gottlieb }
2648ca0d4753SMaor Gottlieb 
264971c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
265071c6e863SAriel Levkovich {
265171c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
265271c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
265371c6e863SAriel Levkovich 		return -EOPNOTSUPP;
265471c6e863SAriel Levkovich 
265571c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
265671c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
265771c6e863SAriel Levkovich 		return -EOPNOTSUPP;
265871c6e863SAriel Levkovich 
265971c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
266071c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
266171c6e863SAriel Levkovich 		return -EOPNOTSUPP;
266271c6e863SAriel Levkovich 
266371c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
266471c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
266571c6e863SAriel Levkovich 		return -EOPNOTSUPP;
266671c6e863SAriel Levkovich 
266771c6e863SAriel Levkovich 	return 0;
266871c6e863SAriel Levkovich }
266971c6e863SAriel Levkovich 
2670c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2671c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2672ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2673466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2674c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2675ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
26762ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2677a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
26783b3233fbSRaed Salem #define LAST_COUNTERS_FIELD counters
2679c47ac6aeSMaor Gottlieb 
2680c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2681c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2682c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2683c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2684c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2685c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2686c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2687c47ac6aeSMaor Gottlieb 
26882ea26203SMark Bloch int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
26892ea26203SMark Bloch 			   bool is_egress,
2690802c2125SAviad Yehezkel 			   struct mlx5_flow_act *action)
2691802c2125SAviad Yehezkel {
2692802c2125SAviad Yehezkel 
2693802c2125SAviad Yehezkel 	switch (maction->ib_action.type) {
2694802c2125SAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
2695501f14e3SMark Bloch 		if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2696501f14e3SMark Bloch 				      MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2697501f14e3SMark Bloch 			return -EINVAL;
2698802c2125SAviad Yehezkel 		/* Currently only AES_GCM keymat is supported by the driver */
2699802c2125SAviad Yehezkel 		action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
27002ea26203SMark Bloch 		action->action |= is_egress ?
2701802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2702802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2703802c2125SAviad Yehezkel 		return 0;
2704b1085be3SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
2705b1085be3SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2706b1085be3SMark Bloch 		    MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
2707501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2708501f14e3SMark Bloch 				return -EINVAL;
2709b1085be3SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
27102b688ea5SMaor Gottlieb 			action->modify_hdr =
27112b688ea5SMaor Gottlieb 				maction->flow_action_raw.modify_hdr;
2712b1085be3SMark Bloch 			return 0;
2713b1085be3SMark Bloch 		}
271410a30896SMark Bloch 		if (maction->flow_action_raw.sub_type ==
271510a30896SMark Bloch 		    MLX5_IB_FLOW_ACTION_DECAP) {
2716501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2717501f14e3SMark Bloch 				return -EINVAL;
271810a30896SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
271910a30896SMark Bloch 			return 0;
272010a30896SMark Bloch 		}
2721e806f932SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2722e806f932SMark Bloch 		    MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
2723501f14e3SMark Bloch 			if (action->action &
2724501f14e3SMark Bloch 			    MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2725501f14e3SMark Bloch 				return -EINVAL;
2726e806f932SMark Bloch 			action->action |=
2727e806f932SMark Bloch 				MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
27282b688ea5SMaor Gottlieb 			action->pkt_reformat =
27292b688ea5SMaor Gottlieb 				maction->flow_action_raw.pkt_reformat;
2730e806f932SMark Bloch 			return 0;
2731e806f932SMark Bloch 		}
2732b1085be3SMark Bloch 		/* fall through */
2733802c2125SAviad Yehezkel 	default:
2734802c2125SAviad Yehezkel 		return -EOPNOTSUPP;
2735802c2125SAviad Yehezkel 	}
2736802c2125SAviad Yehezkel }
2737802c2125SAviad Yehezkel 
2738bb0ee7dcSJianbo Liu static int parse_flow_attr(struct mlx5_core_dev *mdev,
2739bb0ee7dcSJianbo Liu 			   struct mlx5_flow_spec *spec,
2740bb0ee7dcSJianbo Liu 			   const union ib_flow_spec *ib_spec,
2741802c2125SAviad Yehezkel 			   const struct ib_flow_attr *flow_attr,
274271c6e863SAriel Levkovich 			   struct mlx5_flow_act *action, u32 prev_type)
2743038d2ef8SMaor Gottlieb {
2744bb0ee7dcSJianbo Liu 	struct mlx5_flow_context *flow_context = &spec->flow_context;
2745bb0ee7dcSJianbo Liu 	u32 *match_c = spec->match_criteria;
2746bb0ee7dcSJianbo Liu 	u32 *match_v = spec->match_value;
2747466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2748466fa6d2SMaor Gottlieb 					   misc_parameters);
2749466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2750466fa6d2SMaor Gottlieb 					   misc_parameters);
275171c6e863SAriel Levkovich 	void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
275271c6e863SAriel Levkovich 					    misc_parameters_2);
275371c6e863SAriel Levkovich 	void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
275471c6e863SAriel Levkovich 					    misc_parameters_2);
27552d1e697eSMoses Reuben 	void *headers_c;
27562d1e697eSMoses Reuben 	void *headers_v;
275719cc7524SAriel Levkovich 	int match_ipv;
2758802c2125SAviad Yehezkel 	int ret;
2759466fa6d2SMaor Gottlieb 
27602d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
27612d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
27622d1e697eSMoses Reuben 					 inner_headers);
27632d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
27642d1e697eSMoses Reuben 					 inner_headers);
276519cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
276619cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
27672d1e697eSMoses Reuben 	} else {
27682d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
27692d1e697eSMoses Reuben 					 outer_headers);
27702d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
27712d1e697eSMoses Reuben 					 outer_headers);
277219cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
277319cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
27742d1e697eSMoses Reuben 	}
27752d1e697eSMoses Reuben 
27762d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2777038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2778c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
27791ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2780038d2ef8SMaor Gottlieb 
27812d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2782038d2ef8SMaor Gottlieb 					     dmac_47_16),
2783038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
27842d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2785038d2ef8SMaor Gottlieb 					     dmac_47_16),
2786038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2787038d2ef8SMaor Gottlieb 
27882d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2789ee3da804SMaor Gottlieb 					     smac_47_16),
2790ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
27912d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2792ee3da804SMaor Gottlieb 					     smac_47_16),
2793ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2794ee3da804SMaor Gottlieb 
2795038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
27962d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
279710543365SMohamad Haj Yahia 				 cvlan_tag, 1);
27982d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
279910543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2800038d2ef8SMaor Gottlieb 
28012d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2802038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
28032d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2804038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2805038d2ef8SMaor Gottlieb 
28062d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2807038d2ef8SMaor Gottlieb 				 first_cfi,
2808038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
28092d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2810038d2ef8SMaor Gottlieb 				 first_cfi,
2811038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2812038d2ef8SMaor Gottlieb 
28132d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2814038d2ef8SMaor Gottlieb 				 first_prio,
2815038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
28162d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2817038d2ef8SMaor Gottlieb 				 first_prio,
2818038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2819038d2ef8SMaor Gottlieb 		}
28202d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2821038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
28222d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2823038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2824038d2ef8SMaor Gottlieb 		break;
2825038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2826c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
28271ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2828038d2ef8SMaor Gottlieb 
282919cc7524SAriel Levkovich 		if (match_ipv) {
283019cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
283119cc7524SAriel Levkovich 				 ip_version, 0xf);
283219cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
28333346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV4_VERSION);
283419cc7524SAriel Levkovich 		} else {
28352d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2836038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
28372d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2838038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
283919cc7524SAriel Levkovich 		}
2840038d2ef8SMaor Gottlieb 
28412d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2842038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2843038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2844038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
28452d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2846038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2847038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2848038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
28492d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2850038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2851038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2852038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
28532d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2854038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2855038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2856038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2857ca0d4753SMaor Gottlieb 
28582d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2859ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2860ca0d4753SMaor Gottlieb 
28616113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
28626113cc44SMaor Gottlieb 			      ib_spec->ipv4.mask.proto,
28636113cc44SMaor Gottlieb 			      ib_spec->ipv4.val.proto))
28646113cc44SMaor Gottlieb 			return -EINVAL;
2865038d2ef8SMaor Gottlieb 		break;
2866026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2867c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
28681ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2869026bae0cSMaor Gottlieb 
287019cc7524SAriel Levkovich 		if (match_ipv) {
287119cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
287219cc7524SAriel Levkovich 				 ip_version, 0xf);
287319cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
28743346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV6_VERSION);
287519cc7524SAriel Levkovich 		} else {
28762d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2877026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
28782d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2879026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
288019cc7524SAriel Levkovich 		}
2881026bae0cSMaor Gottlieb 
28822d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2883026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2884026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2885026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
28862d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2887026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2888026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2889026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
28902d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2891026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2892026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2893026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
28942d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2895026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2896026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2897026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2898466fa6d2SMaor Gottlieb 
28992d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2900466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2901466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2902466fa6d2SMaor Gottlieb 
29036113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
2904466fa6d2SMaor Gottlieb 			      ib_spec->ipv6.mask.next_hdr,
29056113cc44SMaor Gottlieb 			      ib_spec->ipv6.val.next_hdr))
29066113cc44SMaor Gottlieb 			return -EINVAL;
2907466fa6d2SMaor Gottlieb 
29082d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
29092d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
29102d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
29112d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
2912802c2125SAviad Yehezkel 		break;
2913802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ESP:
2914802c2125SAviad Yehezkel 		if (ib_spec->esp.mask.seq)
2915802c2125SAviad Yehezkel 			return -EOPNOTSUPP;
29162d1e697eSMoses Reuben 
2917802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2918802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.mask.spi));
2919802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2920802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.val.spi));
2921026bae0cSMaor Gottlieb 		break;
2922038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2923c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2924c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
29251ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2926038d2ef8SMaor Gottlieb 
29276113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
29286113cc44SMaor Gottlieb 			return -EINVAL;
2929038d2ef8SMaor Gottlieb 
29302d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2931038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
29322d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2933038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2934038d2ef8SMaor Gottlieb 
29352d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2936038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
29372d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2938038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2939038d2ef8SMaor Gottlieb 		break;
2940038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2941c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2942c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
29431ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2944038d2ef8SMaor Gottlieb 
29456113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
29466113cc44SMaor Gottlieb 			return -EINVAL;
2947038d2ef8SMaor Gottlieb 
29482d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2949038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
29502d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2951038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2952038d2ef8SMaor Gottlieb 
29532d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2954038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
29552d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2956038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2957038d2ef8SMaor Gottlieb 		break;
2958da2f22aeSAriel Levkovich 	case IB_FLOW_SPEC_GRE:
2959da2f22aeSAriel Levkovich 		if (ib_spec->gre.mask.c_ks_res0_ver)
2960da2f22aeSAriel Levkovich 			return -EOPNOTSUPP;
2961da2f22aeSAriel Levkovich 
29626113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
29636113cc44SMaor Gottlieb 			return -EINVAL;
29646113cc44SMaor Gottlieb 
2965da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2966da2f22aeSAriel Levkovich 			 0xff);
2967da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2968da2f22aeSAriel Levkovich 			 IPPROTO_GRE);
2969da2f22aeSAriel Levkovich 
2970da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
2971a93b632cSMaor Gottlieb 			 ntohs(ib_spec->gre.mask.protocol));
2972da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2973da2f22aeSAriel Levkovich 			 ntohs(ib_spec->gre.val.protocol));
2974da2f22aeSAriel Levkovich 
2975da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
29765886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2977da2f22aeSAriel Levkovich 		       &ib_spec->gre.mask.key,
2978da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.mask.key));
2979da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
29805886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2981da2f22aeSAriel Levkovich 		       &ib_spec->gre.val.key,
2982da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.val.key));
2983da2f22aeSAriel Levkovich 		break;
298471c6e863SAriel Levkovich 	case IB_FLOW_SPEC_MPLS:
298571c6e863SAriel Levkovich 		switch (prev_type) {
298671c6e863SAriel Levkovich 		case IB_FLOW_SPEC_UDP:
298771c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
298871c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_udp),
298971c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
299071c6e863SAriel Levkovich 				return -EOPNOTSUPP;
299171c6e863SAriel Levkovich 
299271c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
299371c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
299471c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
299571c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
299671c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
299771c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
299871c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
299971c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
300071c6e863SAriel Levkovich 			break;
300171c6e863SAriel Levkovich 		case IB_FLOW_SPEC_GRE:
300271c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
300371c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_gre),
300471c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
300571c6e863SAriel Levkovich 				return -EOPNOTSUPP;
300671c6e863SAriel Levkovich 
300771c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
300871c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
300971c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
301071c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
301171c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
301271c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
301371c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
301471c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
301571c6e863SAriel Levkovich 			break;
301671c6e863SAriel Levkovich 		default:
301771c6e863SAriel Levkovich 			if (ib_spec->type & IB_FLOW_SPEC_INNER) {
301871c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
301971c6e863SAriel Levkovich 							   ft_field_support.inner_first_mpls),
302071c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
302171c6e863SAriel Levkovich 					return -EOPNOTSUPP;
302271c6e863SAriel Levkovich 
302371c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
302471c6e863SAriel Levkovich 						    inner_first_mpls),
302571c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
302671c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
302771c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
302871c6e863SAriel Levkovich 						    inner_first_mpls),
302971c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
303071c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
303171c6e863SAriel Levkovich 			} else {
303271c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
303371c6e863SAriel Levkovich 							   ft_field_support.outer_first_mpls),
303471c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
303571c6e863SAriel Levkovich 					return -EOPNOTSUPP;
303671c6e863SAriel Levkovich 
303771c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
303871c6e863SAriel Levkovich 						    outer_first_mpls),
303971c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
304071c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
304171c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
304271c6e863SAriel Levkovich 						    outer_first_mpls),
304371c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
304471c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
304571c6e863SAriel Levkovich 			}
304671c6e863SAriel Levkovich 		}
304771c6e863SAriel Levkovich 		break;
3048ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
3049ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
3050ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
30511ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
3052ffb30d8fSMoses Reuben 
3053ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
3054ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
3055ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
3056ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
3057ffb30d8fSMoses Reuben 		break;
30582ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
30592ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
30602ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
30612ac693f9SMoses Reuben 			return -EOPNOTSUPP;
30622ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
30632ac693f9SMoses Reuben 			return -EINVAL;
30642ac693f9SMoses Reuben 
3065bb0ee7dcSJianbo Liu 		flow_context->flow_tag = ib_spec->flow_tag.tag_id;
3066bb0ee7dcSJianbo Liu 		flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
30672ac693f9SMoses Reuben 		break;
3068a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
3069a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
3070a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
3071a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
3072075572d4SBoris Pismenny 		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
3073a22ed86cSSlava Shwartsman 		break;
3074802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ACTION_HANDLE:
30752ea26203SMark Bloch 		ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
30762ea26203SMark Bloch 			flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
3077802c2125SAviad Yehezkel 		if (ret)
3078802c2125SAviad Yehezkel 			return ret;
3079802c2125SAviad Yehezkel 		break;
30803b3233fbSRaed Salem 	case IB_FLOW_SPEC_ACTION_COUNT:
30813b3233fbSRaed Salem 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
30823b3233fbSRaed Salem 					 LAST_COUNTERS_FIELD))
30833b3233fbSRaed Salem 			return -EOPNOTSUPP;
30843b3233fbSRaed Salem 
30853b3233fbSRaed Salem 		/* for now support only one counters spec per flow */
30863b3233fbSRaed Salem 		if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
30873b3233fbSRaed Salem 			return -EINVAL;
30883b3233fbSRaed Salem 
30893b3233fbSRaed Salem 		action->counters = ib_spec->flow_count.counters;
30903b3233fbSRaed Salem 		action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
30913b3233fbSRaed Salem 		break;
3092038d2ef8SMaor Gottlieb 	default:
3093038d2ef8SMaor Gottlieb 		return -EINVAL;
3094038d2ef8SMaor Gottlieb 	}
3095038d2ef8SMaor Gottlieb 
3096038d2ef8SMaor Gottlieb 	return 0;
3097038d2ef8SMaor Gottlieb }
3098038d2ef8SMaor Gottlieb 
3099038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
3100038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
3101038d2ef8SMaor Gottlieb  * could steal other multicast packets.
3102038d2ef8SMaor Gottlieb  */
3103a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
3104038d2ef8SMaor Gottlieb {
310581e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
3106038d2ef8SMaor Gottlieb 
3107038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
3108038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
3109038d2ef8SMaor Gottlieb 		return false;
3110038d2ef8SMaor Gottlieb 
311181e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
311281e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
311381e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
3114038d2ef8SMaor Gottlieb 
311581e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
311681e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
311781e30880SYishai Hadas 			return true;
311881e30880SYishai Hadas 
311981e30880SYishai Hadas 		return false;
312081e30880SYishai Hadas 	}
312181e30880SYishai Hadas 
312281e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
312381e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
312481e30880SYishai Hadas 
312581e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
3126038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
3127038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
3128038d2ef8SMaor Gottlieb 	}
3129038d2ef8SMaor Gottlieb 
313081e30880SYishai Hadas 	return false;
313181e30880SYishai Hadas }
313281e30880SYishai Hadas 
3133802c2125SAviad Yehezkel enum valid_spec {
3134802c2125SAviad Yehezkel 	VALID_SPEC_INVALID,
3135802c2125SAviad Yehezkel 	VALID_SPEC_VALID,
3136802c2125SAviad Yehezkel 	VALID_SPEC_NA,
3137802c2125SAviad Yehezkel };
3138802c2125SAviad Yehezkel 
3139802c2125SAviad Yehezkel static enum valid_spec
3140802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
3141802c2125SAviad Yehezkel 		     const struct mlx5_flow_spec *spec,
3142802c2125SAviad Yehezkel 		     const struct mlx5_flow_act *flow_act,
3143802c2125SAviad Yehezkel 		     bool egress)
3144802c2125SAviad Yehezkel {
3145802c2125SAviad Yehezkel 	const u32 *match_c = spec->match_criteria;
3146802c2125SAviad Yehezkel 	bool is_crypto =
3147802c2125SAviad Yehezkel 		(flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
3148802c2125SAviad Yehezkel 				     MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
3149802c2125SAviad Yehezkel 	bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
3150802c2125SAviad Yehezkel 	bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
3151802c2125SAviad Yehezkel 
3152802c2125SAviad Yehezkel 	/*
3153802c2125SAviad Yehezkel 	 * Currently only crypto is supported in egress, when regular egress
3154802c2125SAviad Yehezkel 	 * rules would be supported, always return VALID_SPEC_NA.
3155802c2125SAviad Yehezkel 	 */
3156802c2125SAviad Yehezkel 	if (!is_crypto)
315778dd0c43SMark Bloch 		return VALID_SPEC_NA;
3158802c2125SAviad Yehezkel 
3159802c2125SAviad Yehezkel 	return is_crypto && is_ipsec &&
3160bb0ee7dcSJianbo Liu 		(!egress || (!is_drop &&
3161bb0ee7dcSJianbo Liu 			     !(spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG))) ?
3162802c2125SAviad Yehezkel 		VALID_SPEC_VALID : VALID_SPEC_INVALID;
3163802c2125SAviad Yehezkel }
3164802c2125SAviad Yehezkel 
3165802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev,
3166802c2125SAviad Yehezkel 			  const struct mlx5_flow_spec *spec,
3167802c2125SAviad Yehezkel 			  const struct mlx5_flow_act *flow_act,
3168802c2125SAviad Yehezkel 			  bool egress)
3169802c2125SAviad Yehezkel {
3170802c2125SAviad Yehezkel 	/* We curretly only support ipsec egress flow */
3171802c2125SAviad Yehezkel 	return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
3172802c2125SAviad Yehezkel }
3173802c2125SAviad Yehezkel 
317419cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
317519cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
31760f750966SAriel Levkovich 			       bool check_inner)
3177038d2ef8SMaor Gottlieb {
3178038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
317919cc7524SAriel Levkovich 	int match_ipv = check_inner ?
318019cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
318119cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
318219cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
318319cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
31840f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
31850f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
31860f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
31870f750966SAriel Levkovich 	bool has_ethertype = false;
3188038d2ef8SMaor Gottlieb 	unsigned int spec_index;
31890f750966SAriel Levkovich 	bool mask_valid = true;
31900f750966SAriel Levkovich 	u16 eth_type = 0;
31910f750966SAriel Levkovich 	bool type_valid;
3192038d2ef8SMaor Gottlieb 
3193038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
3194038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
31950f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
3196038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
31970f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
31980f750966SAriel Levkovich 				      htons(0xffff));
31990f750966SAriel Levkovich 			has_ethertype = true;
32000f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
32010f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
32020f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
32030f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
3204038d2ef8SMaor Gottlieb 		}
3205038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
3206038d2ef8SMaor Gottlieb 	}
32070f750966SAriel Levkovich 
32080f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
32090f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
32100f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
32110f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
32120f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
32130f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
321419cc7524SAriel Levkovich 
321519cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
321619cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
321719cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
32180f750966SAriel Levkovich 	}
32190f750966SAriel Levkovich 
32200f750966SAriel Levkovich 	return type_valid;
32210f750966SAriel Levkovich }
32220f750966SAriel Levkovich 
322319cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
322419cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
32250f750966SAriel Levkovich {
322619cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
322719cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
3228038d2ef8SMaor Gottlieb }
3229038d2ef8SMaor Gottlieb 
3230038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
3231038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
3232038d2ef8SMaor Gottlieb {
3233038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
3234038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
3235038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
3236038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
3237038d2ef8SMaor Gottlieb 	}
3238038d2ef8SMaor Gottlieb }
3239038d2ef8SMaor Gottlieb 
32403b3233fbSRaed Salem static void counters_clear_description(struct ib_counters *counters)
32413b3233fbSRaed Salem {
32423b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
32433b3233fbSRaed Salem 
32443b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
32453b3233fbSRaed Salem 	kfree(mcounters->counters_data);
32463b3233fbSRaed Salem 	mcounters->counters_data = NULL;
32473b3233fbSRaed Salem 	mcounters->cntrs_max_index = 0;
32483b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
32493b3233fbSRaed Salem }
32503b3233fbSRaed Salem 
3251038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3252038d2ef8SMaor Gottlieb {
3253038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3254038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
3255038d2ef8SMaor Gottlieb 							  ibflow);
3256038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
3257d4be3f44SYishai Hadas 	struct mlx5_ib_dev *dev = handler->dev;
3258038d2ef8SMaor Gottlieb 
32599a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3260038d2ef8SMaor Gottlieb 
3261038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
326274491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
3263cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
3264038d2ef8SMaor Gottlieb 		list_del(&iter->list);
3265038d2ef8SMaor Gottlieb 		kfree(iter);
3266038d2ef8SMaor Gottlieb 	}
3267038d2ef8SMaor Gottlieb 
326874491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
32695497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
32703b3233fbSRaed Salem 	if (handler->ibcounters &&
32713b3233fbSRaed Salem 	    atomic_read(&handler->ibcounters->usecnt) == 1)
32723b3233fbSRaed Salem 		counters_clear_description(handler->ibcounters);
3273038d2ef8SMaor Gottlieb 
32743b3233fbSRaed Salem 	mutex_unlock(&dev->flow_db->lock);
3275d4be3f44SYishai Hadas 	if (handler->flow_matcher)
3276d4be3f44SYishai Hadas 		atomic_dec(&handler->flow_matcher->usecnt);
3277038d2ef8SMaor Gottlieb 	kfree(handler);
3278038d2ef8SMaor Gottlieb 
3279038d2ef8SMaor Gottlieb 	return 0;
3280038d2ef8SMaor Gottlieb }
3281038d2ef8SMaor Gottlieb 
328235d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
328335d19011SMaor Gottlieb {
328435d19011SMaor Gottlieb 	priority *= 2;
328535d19011SMaor Gottlieb 	if (!dont_trap)
328635d19011SMaor Gottlieb 		priority++;
328735d19011SMaor Gottlieb 	return priority;
328835d19011SMaor Gottlieb }
328935d19011SMaor Gottlieb 
3290cc0e5d42SMaor Gottlieb enum flow_table_type {
3291cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
3292cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
3293cc0e5d42SMaor Gottlieb };
3294cc0e5d42SMaor Gottlieb 
329500b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
329600b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
3297d4be3f44SYishai Hadas 
3298d4be3f44SYishai Hadas static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3299d4be3f44SYishai Hadas 					   struct mlx5_ib_flow_prio *prio,
3300d4be3f44SYishai Hadas 					   int priority,
33014adda112SMark Bloch 					   int num_entries, int num_groups,
33024adda112SMark Bloch 					   u32 flags)
3303d4be3f44SYishai Hadas {
330461dc7b01SPaul Blakey 	struct mlx5_flow_table_attr ft_attr = {};
3305d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft;
3306d4be3f44SYishai Hadas 
330761dc7b01SPaul Blakey 	ft_attr.prio = priority;
330861dc7b01SPaul Blakey 	ft_attr.max_fte = num_entries;
330961dc7b01SPaul Blakey 	ft_attr.flags = flags;
331061dc7b01SPaul Blakey 	ft_attr.autogroup.max_num_groups = num_groups;
331161dc7b01SPaul Blakey 	ft = mlx5_create_auto_grouped_flow_table(ns, &ft_attr);
3312d4be3f44SYishai Hadas 	if (IS_ERR(ft))
3313d4be3f44SYishai Hadas 		return ERR_CAST(ft);
3314d4be3f44SYishai Hadas 
3315d4be3f44SYishai Hadas 	prio->flow_table = ft;
3316d4be3f44SYishai Hadas 	prio->refcount = 0;
3317d4be3f44SYishai Hadas 	return prio;
3318d4be3f44SYishai Hadas }
3319d4be3f44SYishai Hadas 
3320038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
3321cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
3322cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
3323038d2ef8SMaor Gottlieb {
332435d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
3325038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
3326038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
3327038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
3328dac388efSMaor Gottlieb 	int max_table_size;
3329038d2ef8SMaor Gottlieb 	int num_entries;
3330038d2ef8SMaor Gottlieb 	int num_groups;
3331cecae747SMaor Gottlieb 	bool esw_encap;
33324adda112SMark Bloch 	u32 flags = 0;
3333038d2ef8SMaor Gottlieb 	int priority;
3334038d2ef8SMaor Gottlieb 
3335dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3336dac388efSMaor Gottlieb 						       log_max_ft_size));
3337cecae747SMaor Gottlieb 	esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
3338cecae747SMaor Gottlieb 		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
3339038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
334078dd0c43SMark Bloch 		enum mlx5_flow_namespace_type fn_type;
334178dd0c43SMark Bloch 
334278dd0c43SMark Bloch 		if (flow_is_multicast_only(flow_attr) &&
334335d19011SMaor Gottlieb 		    !dont_trap)
3344038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
3345038d2ef8SMaor Gottlieb 		else
334635d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
334735d19011SMaor Gottlieb 							dont_trap);
334878dd0c43SMark Bloch 		if (ft_type == MLX5_IB_FT_RX) {
334978dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
335078dd0c43SMark Bloch 			prio = &dev->flow_db->prios[priority];
3351cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
33524adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
33534adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3354cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
33555c2db53fSMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
33565c2db53fSMark Bloch 					reformat_l3_tunnel_to_l2))
33575c2db53fSMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
335878dd0c43SMark Bloch 		} else {
335978dd0c43SMark Bloch 			max_table_size =
336078dd0c43SMark Bloch 				BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
336178dd0c43SMark Bloch 							      log_max_ft_size));
336278dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
336378dd0c43SMark Bloch 			prio = &dev->flow_db->egress_prios[priority];
3364cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
33654adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
33664adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
336778dd0c43SMark Bloch 		}
336878dd0c43SMark Bloch 		ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
3369038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
3370038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
3371038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3372038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3373038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
3374038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
3375038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
3376038d2ef8SMaor Gottlieb 					 &num_entries,
3377038d2ef8SMaor Gottlieb 					 &num_groups);
33789a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
3379cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3380cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3381cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
3382cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
3383cc0e5d42SMaor Gottlieb 
3384cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3385cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3386cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3387cc0e5d42SMaor Gottlieb 
33889a4ca38dSMark Bloch 		prio = &dev->flow_db->sniffer[ft_type];
3389cc0e5d42SMaor Gottlieb 		priority = 0;
3390cc0e5d42SMaor Gottlieb 		num_entries = 1;
3391cc0e5d42SMaor Gottlieb 		num_groups = 1;
3392038d2ef8SMaor Gottlieb 	}
3393038d2ef8SMaor Gottlieb 
3394038d2ef8SMaor Gottlieb 	if (!ns)
3395038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
3396038d2ef8SMaor Gottlieb 
33973b70508aSMark Bloch 	max_table_size = min_t(int, num_entries, max_table_size);
3398dac388efSMaor Gottlieb 
3399038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
3400d4be3f44SYishai Hadas 	if (!ft)
34013b70508aSMark Bloch 		return _get_prio(ns, prio, priority, max_table_size, num_groups,
34024adda112SMark Bloch 				 flags);
3403038d2ef8SMaor Gottlieb 
3404d4be3f44SYishai Hadas 	return prio;
3405038d2ef8SMaor Gottlieb }
3406038d2ef8SMaor Gottlieb 
3407a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
3408a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
3409a550ddfcSYishai Hadas 			    u32 underlay_qpn)
3410a550ddfcSYishai Hadas {
3411a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3412a550ddfcSYishai Hadas 					   spec->match_criteria,
3413a550ddfcSYishai Hadas 					   misc_parameters);
3414a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3415a550ddfcSYishai Hadas 					   misc_parameters);
3416a550ddfcSYishai Hadas 
3417a550ddfcSYishai Hadas 	if (underlay_qpn &&
3418a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3419a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
3420a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3421a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
3422a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3423a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
3424a550ddfcSYishai Hadas 	}
3425a550ddfcSYishai Hadas }
3426a550ddfcSYishai Hadas 
34275e95af5fSRaed Salem static int read_flow_counters(struct ib_device *ibdev,
34285e95af5fSRaed Salem 			      struct mlx5_read_counters_attr *read_attr)
34295e95af5fSRaed Salem {
34305e95af5fSRaed Salem 	struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
34315e95af5fSRaed Salem 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
34325e95af5fSRaed Salem 
34335e95af5fSRaed Salem 	return mlx5_fc_query(dev->mdev, fc,
34345e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_PACKETS],
34355e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_BYTES]);
34365e95af5fSRaed Salem }
34375e95af5fSRaed Salem 
34385e95af5fSRaed Salem /* flow counters currently expose two counters packets and bytes */
34395e95af5fSRaed Salem #define FLOW_COUNTERS_NUM 2
34403b3233fbSRaed Salem static int counters_set_description(struct ib_counters *counters,
34413b3233fbSRaed Salem 				    enum mlx5_ib_counters_type counters_type,
34423b3233fbSRaed Salem 				    struct mlx5_ib_flow_counters_desc *desc_data,
34433b3233fbSRaed Salem 				    u32 ncounters)
34443b3233fbSRaed Salem {
34453b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
34463b3233fbSRaed Salem 	u32 cntrs_max_index = 0;
34473b3233fbSRaed Salem 	int i;
34483b3233fbSRaed Salem 
34493b3233fbSRaed Salem 	if (counters_type != MLX5_IB_COUNTERS_FLOW)
34503b3233fbSRaed Salem 		return -EINVAL;
34513b3233fbSRaed Salem 
34523b3233fbSRaed Salem 	/* init the fields for the object */
34533b3233fbSRaed Salem 	mcounters->type = counters_type;
34545e95af5fSRaed Salem 	mcounters->read_counters = read_flow_counters;
34555e95af5fSRaed Salem 	mcounters->counters_num = FLOW_COUNTERS_NUM;
34563b3233fbSRaed Salem 	mcounters->ncounters = ncounters;
34573b3233fbSRaed Salem 	/* each counter entry have both description and index pair */
34583b3233fbSRaed Salem 	for (i = 0; i < ncounters; i++) {
34593b3233fbSRaed Salem 		if (desc_data[i].description > IB_COUNTER_BYTES)
34603b3233fbSRaed Salem 			return -EINVAL;
34613b3233fbSRaed Salem 
34623b3233fbSRaed Salem 		if (cntrs_max_index <= desc_data[i].index)
34633b3233fbSRaed Salem 			cntrs_max_index = desc_data[i].index + 1;
34643b3233fbSRaed Salem 	}
34653b3233fbSRaed Salem 
34663b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
34673b3233fbSRaed Salem 	mcounters->counters_data = desc_data;
34683b3233fbSRaed Salem 	mcounters->cntrs_max_index = cntrs_max_index;
34693b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
34703b3233fbSRaed Salem 
34713b3233fbSRaed Salem 	return 0;
34723b3233fbSRaed Salem }
34733b3233fbSRaed Salem 
34743b3233fbSRaed Salem #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
34753b3233fbSRaed Salem static int flow_counters_set_data(struct ib_counters *ibcounters,
34763b3233fbSRaed Salem 				  struct mlx5_ib_create_flow *ucmd)
34773b3233fbSRaed Salem {
34783b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
34793b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
34803b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc_data = NULL;
34813b3233fbSRaed Salem 	bool hw_hndl = false;
34823b3233fbSRaed Salem 	int ret = 0;
34833b3233fbSRaed Salem 
34843b3233fbSRaed Salem 	if (ucmd && ucmd->ncounters_data != 0) {
34853b3233fbSRaed Salem 		cntrs_data = ucmd->data;
34863b3233fbSRaed Salem 		if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
34873b3233fbSRaed Salem 			return -EINVAL;
34883b3233fbSRaed Salem 
34893b3233fbSRaed Salem 		desc_data = kcalloc(cntrs_data->ncounters,
34903b3233fbSRaed Salem 				    sizeof(*desc_data),
34913b3233fbSRaed Salem 				    GFP_KERNEL);
34923b3233fbSRaed Salem 		if (!desc_data)
34933b3233fbSRaed Salem 			return  -ENOMEM;
34943b3233fbSRaed Salem 
34953b3233fbSRaed Salem 		if (copy_from_user(desc_data,
34963b3233fbSRaed Salem 				   u64_to_user_ptr(cntrs_data->counters_data),
34973b3233fbSRaed Salem 				   sizeof(*desc_data) * cntrs_data->ncounters)) {
34983b3233fbSRaed Salem 			ret = -EFAULT;
34993b3233fbSRaed Salem 			goto free;
35003b3233fbSRaed Salem 		}
35013b3233fbSRaed Salem 	}
35023b3233fbSRaed Salem 
35033b3233fbSRaed Salem 	if (!mcounters->hw_cntrs_hndl) {
35043b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = mlx5_fc_create(
35053b3233fbSRaed Salem 			to_mdev(ibcounters->device)->mdev, false);
3506e31abf76Sweiyongjun (A) 		if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3507e31abf76Sweiyongjun (A) 			ret = PTR_ERR(mcounters->hw_cntrs_hndl);
35083b3233fbSRaed Salem 			goto free;
35093b3233fbSRaed Salem 		}
35103b3233fbSRaed Salem 		hw_hndl = true;
35113b3233fbSRaed Salem 	}
35123b3233fbSRaed Salem 
35133b3233fbSRaed Salem 	if (desc_data) {
35143b3233fbSRaed Salem 		/* counters already bound to at least one flow */
35153b3233fbSRaed Salem 		if (mcounters->cntrs_max_index) {
35163b3233fbSRaed Salem 			ret = -EINVAL;
35173b3233fbSRaed Salem 			goto free_hndl;
35183b3233fbSRaed Salem 		}
35193b3233fbSRaed Salem 
35203b3233fbSRaed Salem 		ret = counters_set_description(ibcounters,
35213b3233fbSRaed Salem 					       MLX5_IB_COUNTERS_FLOW,
35223b3233fbSRaed Salem 					       desc_data,
35233b3233fbSRaed Salem 					       cntrs_data->ncounters);
35243b3233fbSRaed Salem 		if (ret)
35253b3233fbSRaed Salem 			goto free_hndl;
35263b3233fbSRaed Salem 
35273b3233fbSRaed Salem 	} else if (!mcounters->cntrs_max_index) {
35283b3233fbSRaed Salem 		/* counters not bound yet, must have udata passed */
35293b3233fbSRaed Salem 		ret = -EINVAL;
35303b3233fbSRaed Salem 		goto free_hndl;
35313b3233fbSRaed Salem 	}
35323b3233fbSRaed Salem 
35333b3233fbSRaed Salem 	return 0;
35343b3233fbSRaed Salem 
35353b3233fbSRaed Salem free_hndl:
35363b3233fbSRaed Salem 	if (hw_hndl) {
35373b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
35383b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
35393b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = NULL;
35403b3233fbSRaed Salem 	}
35413b3233fbSRaed Salem free:
35423b3233fbSRaed Salem 	kfree(desc_data);
35433b3233fbSRaed Salem 	return ret;
35443b3233fbSRaed Salem }
35453b3233fbSRaed Salem 
3546669ff1e3SJianbo Liu static void mlx5_ib_set_rule_source_port(struct mlx5_ib_dev *dev,
3547669ff1e3SJianbo Liu 					 struct mlx5_flow_spec *spec,
3548669ff1e3SJianbo Liu 					 struct mlx5_eswitch_rep *rep)
3549669ff1e3SJianbo Liu {
3550669ff1e3SJianbo Liu 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
3551669ff1e3SJianbo Liu 	void *misc;
3552669ff1e3SJianbo Liu 
3553669ff1e3SJianbo Liu 	if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
3554669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3555669ff1e3SJianbo Liu 				    misc_parameters_2);
3556669ff1e3SJianbo Liu 
3557669ff1e3SJianbo Liu 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
3558669ff1e3SJianbo Liu 			 mlx5_eswitch_get_vport_metadata_for_match(esw,
3559669ff1e3SJianbo Liu 								   rep->vport));
3560669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3561669ff1e3SJianbo Liu 				    misc_parameters_2);
3562669ff1e3SJianbo Liu 
3563669ff1e3SJianbo Liu 		MLX5_SET_TO_ONES(fte_match_set_misc2, misc, metadata_reg_c_0);
3564669ff1e3SJianbo Liu 	} else {
3565669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3566669ff1e3SJianbo Liu 				    misc_parameters);
3567669ff1e3SJianbo Liu 
3568669ff1e3SJianbo Liu 		MLX5_SET(fte_match_set_misc, misc, source_port, rep->vport);
3569669ff1e3SJianbo Liu 
3570669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3571669ff1e3SJianbo Liu 				    misc_parameters);
3572669ff1e3SJianbo Liu 
3573669ff1e3SJianbo Liu 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3574669ff1e3SJianbo Liu 	}
3575669ff1e3SJianbo Liu }
3576669ff1e3SJianbo Liu 
3577a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3578038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
3579dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
3580a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
35813b3233fbSRaed Salem 						      u32 underlay_qpn,
35823b3233fbSRaed Salem 						      struct mlx5_ib_create_flow *ucmd)
3583038d2ef8SMaor Gottlieb {
3584038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
3585038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
3586bb0ee7dcSJianbo Liu 	struct mlx5_flow_act flow_act = {};
3587c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
35883b3233fbSRaed Salem 	struct mlx5_flow_destination dest_arr[2] = {};
35893b3233fbSRaed Salem 	struct mlx5_flow_destination *rule_dst = dest_arr;
3590dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3591038d2ef8SMaor Gottlieb 	unsigned int spec_index;
359271c6e863SAriel Levkovich 	u32 prev_type = 0;
3593038d2ef8SMaor Gottlieb 	int err = 0;
35943b3233fbSRaed Salem 	int dest_num = 0;
3595802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3596038d2ef8SMaor Gottlieb 
359719cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
3598038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3599038d2ef8SMaor Gottlieb 
36006a4d00beSMark Bloch 	if (dev->is_rep && is_egress)
360178dd0c43SMark Bloch 		return ERR_PTR(-EINVAL);
360278dd0c43SMark Bloch 
36031b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3604038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3605c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
3606038d2ef8SMaor Gottlieb 		err = -ENOMEM;
3607038d2ef8SMaor Gottlieb 		goto free;
3608038d2ef8SMaor Gottlieb 	}
3609038d2ef8SMaor Gottlieb 
3610038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
3611038d2ef8SMaor Gottlieb 
3612038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
3613bb0ee7dcSJianbo Liu 		err = parse_flow_attr(dev->mdev, spec,
361471c6e863SAriel Levkovich 				      ib_flow, flow_attr, &flow_act,
361571c6e863SAriel Levkovich 				      prev_type);
3616038d2ef8SMaor Gottlieb 		if (err < 0)
3617038d2ef8SMaor Gottlieb 			goto free;
3618038d2ef8SMaor Gottlieb 
361971c6e863SAriel Levkovich 		prev_type = ((union ib_flow_spec *)ib_flow)->type;
3620038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3621038d2ef8SMaor Gottlieb 	}
3622038d2ef8SMaor Gottlieb 
3623ed9085feSMaor Gottlieb 	if (dst && !(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP)) {
3624ed9085feSMaor Gottlieb 		memcpy(&dest_arr[0], dst, sizeof(*dst));
3625ed9085feSMaor Gottlieb 		dest_num++;
3626ed9085feSMaor Gottlieb 	}
3627ed9085feSMaor Gottlieb 
3628a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
3629a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
3630a550ddfcSYishai Hadas 
36316a4d00beSMark Bloch 	if (dev->is_rep) {
3632669ff1e3SJianbo Liu 		struct mlx5_eswitch_rep *rep;
3633018a94eeSMark Bloch 
3634669ff1e3SJianbo Liu 		rep = dev->port[flow_attr->port - 1].rep;
3635669ff1e3SJianbo Liu 		if (!rep) {
36366a4d00beSMark Bloch 			err = -EINVAL;
36376a4d00beSMark Bloch 			goto free;
36386a4d00beSMark Bloch 		}
3639669ff1e3SJianbo Liu 
3640669ff1e3SJianbo Liu 		mlx5_ib_set_rule_source_port(dev, spec, rep);
3641018a94eeSMark Bloch 	}
3642018a94eeSMark Bloch 
3643466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3644802c2125SAviad Yehezkel 
3645802c2125SAviad Yehezkel 	if (is_egress &&
3646802c2125SAviad Yehezkel 	    !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3647802c2125SAviad Yehezkel 		err = -EINVAL;
3648802c2125SAviad Yehezkel 		goto free;
3649802c2125SAviad Yehezkel 	}
3650802c2125SAviad Yehezkel 
36513b3233fbSRaed Salem 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3652171c7625SMark Bloch 		struct mlx5_ib_mcounters *mcounters;
3653171c7625SMark Bloch 
36543b3233fbSRaed Salem 		err = flow_counters_set_data(flow_act.counters, ucmd);
36553b3233fbSRaed Salem 		if (err)
36563b3233fbSRaed Salem 			goto free;
36573b3233fbSRaed Salem 
3658171c7625SMark Bloch 		mcounters = to_mcounters(flow_act.counters);
36593b3233fbSRaed Salem 		handler->ibcounters = flow_act.counters;
36603b3233fbSRaed Salem 		dest_arr[dest_num].type =
36613b3233fbSRaed Salem 			MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3662171c7625SMark Bloch 		dest_arr[dest_num].counter_id =
3663171c7625SMark Bloch 			mlx5_fc_id(mcounters->hw_cntrs_hndl);
36643b3233fbSRaed Salem 		dest_num++;
36653b3233fbSRaed Salem 	}
36663b3233fbSRaed Salem 
3667075572d4SBoris Pismenny 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
3668ed9085feSMaor Gottlieb 		if (!dest_num)
3669a22ed86cSSlava Shwartsman 			rule_dst = NULL;
3670a22ed86cSSlava Shwartsman 	} else {
3671802c2125SAviad Yehezkel 		if (is_egress)
3672802c2125SAviad Yehezkel 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3673802c2125SAviad Yehezkel 		else
3674802c2125SAviad Yehezkel 			flow_act.action |=
36753b3233fbSRaed Salem 				dest_num ?  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
367635d19011SMaor Gottlieb 					MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3677a22ed86cSSlava Shwartsman 	}
36782ac693f9SMoses Reuben 
3679bb0ee7dcSJianbo Liu 	if ((spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG)  &&
36802ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
36812ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
36822ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3683bb0ee7dcSJianbo Liu 			     spec->flow_context.flow_tag, flow_attr->type);
36842ac693f9SMoses Reuben 		err = -EINVAL;
36852ac693f9SMoses Reuben 		goto free;
36862ac693f9SMoses Reuben 	}
368774491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
368866958ed9SHadar Hen Zion 					    &flow_act,
3689a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
3690038d2ef8SMaor Gottlieb 
3691038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
3692038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
3693038d2ef8SMaor Gottlieb 		goto free;
3694038d2ef8SMaor Gottlieb 	}
3695038d2ef8SMaor Gottlieb 
3696d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
36975497adc6SMaor Gottlieb 	handler->prio = ft_prio;
3698d4be3f44SYishai Hadas 	handler->dev = dev;
3699038d2ef8SMaor Gottlieb 
3700038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
3701038d2ef8SMaor Gottlieb free:
37023b3233fbSRaed Salem 	if (err && handler) {
37033b3233fbSRaed Salem 		if (handler->ibcounters &&
37043b3233fbSRaed Salem 		    atomic_read(&handler->ibcounters->usecnt) == 1)
37053b3233fbSRaed Salem 			counters_clear_description(handler->ibcounters);
3706038d2ef8SMaor Gottlieb 		kfree(handler);
37073b3233fbSRaed Salem 	}
3708c5bb1730SMaor Gottlieb 	kvfree(spec);
3709038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
3710038d2ef8SMaor Gottlieb }
3711038d2ef8SMaor Gottlieb 
3712a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3713a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
3714a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
3715a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
3716a550ddfcSYishai Hadas {
37173b3233fbSRaed Salem 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
3718a550ddfcSYishai Hadas }
3719a550ddfcSYishai Hadas 
372035d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
372135d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
372235d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
372335d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
372435d19011SMaor Gottlieb {
372535d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
372635d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
372735d19011SMaor Gottlieb 
372835d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
372935d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
373035d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
373135d19011SMaor Gottlieb 					       flow_attr, dst);
373235d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
373374491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3734d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
373535d19011SMaor Gottlieb 			kfree(handler);
373635d19011SMaor Gottlieb 			handler = handler_dst;
373735d19011SMaor Gottlieb 		} else {
373835d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
373935d19011SMaor Gottlieb 		}
374035d19011SMaor Gottlieb 	}
374135d19011SMaor Gottlieb 
374235d19011SMaor Gottlieb 	return handler;
374335d19011SMaor Gottlieb }
3744038d2ef8SMaor Gottlieb enum {
3745038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
3746038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
3747038d2ef8SMaor Gottlieb };
3748038d2ef8SMaor Gottlieb 
3749038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3750038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
3751038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
3752038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
3753038d2ef8SMaor Gottlieb {
3754038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
3755038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3756038d2ef8SMaor Gottlieb 
3757038d2ef8SMaor Gottlieb 	static struct {
3758038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
3759038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
3760038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
3761038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
3762038d2ef8SMaor Gottlieb 			.flow_attr = {
3763038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3764038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3765038d2ef8SMaor Gottlieb 			},
3766038d2ef8SMaor Gottlieb 			.eth_flow = {
3767038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3768038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3769038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3770038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
3771038d2ef8SMaor Gottlieb 			}
3772038d2ef8SMaor Gottlieb 		},
3773038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
3774038d2ef8SMaor Gottlieb 			.flow_attr = {
3775038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3776038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3777038d2ef8SMaor Gottlieb 			},
3778038d2ef8SMaor Gottlieb 			.eth_flow = {
3779038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3780038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3781038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3782038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
3783038d2ef8SMaor Gottlieb 			}
3784038d2ef8SMaor Gottlieb 		}
3785038d2ef8SMaor Gottlieb 	};
3786038d2ef8SMaor Gottlieb 
3787038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
3788038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
3789038d2ef8SMaor Gottlieb 				   dst);
3790038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
3791038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3792038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
3793038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3794038d2ef8SMaor Gottlieb 						 dst);
3795038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
379674491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3797d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
3798038d2ef8SMaor Gottlieb 			kfree(handler);
3799038d2ef8SMaor Gottlieb 			handler = handler_ucast;
3800038d2ef8SMaor Gottlieb 		} else {
3801038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
3802038d2ef8SMaor Gottlieb 		}
3803038d2ef8SMaor Gottlieb 	}
3804038d2ef8SMaor Gottlieb 
3805038d2ef8SMaor Gottlieb 	return handler;
3806038d2ef8SMaor Gottlieb }
3807038d2ef8SMaor Gottlieb 
3808cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3809cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
3810cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
3811cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
3812cc0e5d42SMaor Gottlieb {
3813cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
3814cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
3815cc0e5d42SMaor Gottlieb 	int err;
3816cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
3817cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
3818cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
3819cc0e5d42SMaor Gottlieb 	};
3820cc0e5d42SMaor Gottlieb 
3821cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3822cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
3823cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
3824cc0e5d42SMaor Gottlieb 		goto err;
3825cc0e5d42SMaor Gottlieb 	}
3826cc0e5d42SMaor Gottlieb 
3827cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3828cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
3829cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
3830cc0e5d42SMaor Gottlieb 		goto err_tx;
3831cc0e5d42SMaor Gottlieb 	}
3832cc0e5d42SMaor Gottlieb 
3833cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
3834cc0e5d42SMaor Gottlieb 
3835cc0e5d42SMaor Gottlieb 	return handler_rx;
3836cc0e5d42SMaor Gottlieb 
3837cc0e5d42SMaor Gottlieb err_tx:
383874491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
3839cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
3840cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
3841cc0e5d42SMaor Gottlieb err:
3842cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
3843cc0e5d42SMaor Gottlieb }
3844cc0e5d42SMaor Gottlieb 
3845038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3846038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
384759082a32SMatan Barak 					   int domain,
384859082a32SMatan Barak 					   struct ib_udata *udata)
3849038d2ef8SMaor Gottlieb {
3850038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3851d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
3852038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3853038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
3854cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3855038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
3856802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
38573b3233fbSRaed Salem 	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
38583b3233fbSRaed Salem 	size_t min_ucmd_sz, required_ucmd_sz;
3859038d2ef8SMaor Gottlieb 	int err;
3860a550ddfcSYishai Hadas 	int underlay_qpn;
3861038d2ef8SMaor Gottlieb 
38623b3233fbSRaed Salem 	if (udata && udata->inlen) {
38633b3233fbSRaed Salem 		min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
38643b3233fbSRaed Salem 				sizeof(ucmd_hdr.reserved);
38653b3233fbSRaed Salem 		if (udata->inlen < min_ucmd_sz)
386659082a32SMatan Barak 			return ERR_PTR(-EOPNOTSUPP);
386759082a32SMatan Barak 
38683b3233fbSRaed Salem 		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
38693b3233fbSRaed Salem 		if (err)
38703b3233fbSRaed Salem 			return ERR_PTR(err);
38713b3233fbSRaed Salem 
38723b3233fbSRaed Salem 		/* currently supports only one counters data */
38733b3233fbSRaed Salem 		if (ucmd_hdr.ncounters_data > 1)
38743b3233fbSRaed Salem 			return ERR_PTR(-EINVAL);
38753b3233fbSRaed Salem 
38763b3233fbSRaed Salem 		required_ucmd_sz = min_ucmd_sz +
38773b3233fbSRaed Salem 			sizeof(struct mlx5_ib_flow_counters_data) *
38783b3233fbSRaed Salem 			ucmd_hdr.ncounters_data;
38793b3233fbSRaed Salem 		if (udata->inlen > required_ucmd_sz &&
38803b3233fbSRaed Salem 		    !ib_is_udata_cleared(udata, required_ucmd_sz,
38813b3233fbSRaed Salem 					 udata->inlen - required_ucmd_sz))
38823b3233fbSRaed Salem 			return ERR_PTR(-EOPNOTSUPP);
38833b3233fbSRaed Salem 
38843b3233fbSRaed Salem 		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
38853b3233fbSRaed Salem 		if (!ucmd)
38863b3233fbSRaed Salem 			return ERR_PTR(-ENOMEM);
38873b3233fbSRaed Salem 
38883b3233fbSRaed Salem 		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
3889299eafeeSGustavo A. R. Silva 		if (err)
3890299eafeeSGustavo A. R. Silva 			goto free_ucmd;
38913b3233fbSRaed Salem 	}
38923b3233fbSRaed Salem 
3893299eafeeSGustavo A. R. Silva 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3894299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3895299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3896299eafeeSGustavo A. R. Silva 	}
3897038d2ef8SMaor Gottlieb 
3898038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
3899508562d6SDaniel Jurgens 	    flow_attr->port > dev->num_ports ||
3900802c2125SAviad Yehezkel 	    (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3901299eafeeSGustavo A. R. Silva 				  IB_FLOW_ATTR_FLAGS_EGRESS))) {
3902299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3903299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3904299eafeeSGustavo A. R. Silva 	}
3905802c2125SAviad Yehezkel 
3906802c2125SAviad Yehezkel 	if (is_egress &&
3907802c2125SAviad Yehezkel 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3908299eafeeSGustavo A. R. Silva 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3909299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3910299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3911299eafeeSGustavo A. R. Silva 	}
3912038d2ef8SMaor Gottlieb 
3913038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3914299eafeeSGustavo A. R. Silva 	if (!dst) {
3915299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3916299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3917299eafeeSGustavo A. R. Silva 	}
3918038d2ef8SMaor Gottlieb 
39199a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3920038d2ef8SMaor Gottlieb 
3921802c2125SAviad Yehezkel 	ft_prio = get_flow_table(dev, flow_attr,
3922802c2125SAviad Yehezkel 				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3923038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
3924038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
3925038d2ef8SMaor Gottlieb 		goto unlock;
3926038d2ef8SMaor Gottlieb 	}
3927cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3928cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3929cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
3930cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
3931cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
3932cc0e5d42SMaor Gottlieb 			goto destroy_ft;
3933cc0e5d42SMaor Gottlieb 		}
3934cc0e5d42SMaor Gottlieb 	}
3935038d2ef8SMaor Gottlieb 
3936802c2125SAviad Yehezkel 	if (is_egress) {
3937802c2125SAviad Yehezkel 		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3938802c2125SAviad Yehezkel 	} else {
3939038d2ef8SMaor Gottlieb 		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3940d9f88e5aSYishai Hadas 		if (mqp->flags & MLX5_IB_QP_RSS)
3941d9f88e5aSYishai Hadas 			dst->tir_num = mqp->rss_qp.tirn;
3942d9f88e5aSYishai Hadas 		else
3943d9f88e5aSYishai Hadas 			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3944802c2125SAviad Yehezkel 	}
3945038d2ef8SMaor Gottlieb 
3946038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
394735d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
394835d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
394935d19011SMaor Gottlieb 							flow_attr, dst);
395035d19011SMaor Gottlieb 		} else {
3951a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3952a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
3953a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
39543b3233fbSRaed Salem 						    dst, underlay_qpn, ucmd);
395535d19011SMaor Gottlieb 		}
3956038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3957038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3958038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3959038d2ef8SMaor Gottlieb 						dst);
3960cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3961cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3962038d2ef8SMaor Gottlieb 	} else {
3963038d2ef8SMaor Gottlieb 		err = -EINVAL;
3964038d2ef8SMaor Gottlieb 		goto destroy_ft;
3965038d2ef8SMaor Gottlieb 	}
3966038d2ef8SMaor Gottlieb 
3967038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
3968038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
3969038d2ef8SMaor Gottlieb 		handler = NULL;
3970038d2ef8SMaor Gottlieb 		goto destroy_ft;
3971038d2ef8SMaor Gottlieb 	}
3972038d2ef8SMaor Gottlieb 
39739a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3974038d2ef8SMaor Gottlieb 	kfree(dst);
39753b3233fbSRaed Salem 	kfree(ucmd);
3976038d2ef8SMaor Gottlieb 
3977038d2ef8SMaor Gottlieb 	return &handler->ibflow;
3978038d2ef8SMaor Gottlieb 
3979038d2ef8SMaor Gottlieb destroy_ft:
3980038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
3981cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
3982cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
3983038d2ef8SMaor Gottlieb unlock:
39849a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3985038d2ef8SMaor Gottlieb 	kfree(dst);
3986299eafeeSGustavo A. R. Silva free_ucmd:
39873b3233fbSRaed Salem 	kfree(ucmd);
3988038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
3989038d2ef8SMaor Gottlieb }
3990038d2ef8SMaor Gottlieb 
3991b47fd4ffSMark Bloch static struct mlx5_ib_flow_prio *
3992b47fd4ffSMark Bloch _get_flow_table(struct mlx5_ib_dev *dev,
3993b47fd4ffSMark Bloch 		struct mlx5_ib_flow_matcher *fs_matcher,
3994b47fd4ffSMark Bloch 		bool mcast)
3995d4be3f44SYishai Hadas {
3996d4be3f44SYishai Hadas 	struct mlx5_flow_namespace *ns = NULL;
399713a43765SMark Bloch 	struct mlx5_ib_flow_prio *prio = NULL;
399813a43765SMark Bloch 	int max_table_size = 0;
3999cecae747SMaor Gottlieb 	bool esw_encap;
4000b47fd4ffSMark Bloch 	u32 flags = 0;
4001b47fd4ffSMark Bloch 	int priority;
4002d4be3f44SYishai Hadas 
400313a43765SMark Bloch 	if (mcast)
400413a43765SMark Bloch 		priority = MLX5_IB_FLOW_MCAST_PRIO;
400513a43765SMark Bloch 	else
400613a43765SMark Bloch 		priority = ib_prio_to_core_prio(fs_matcher->priority, false);
400713a43765SMark Bloch 
4008cecae747SMaor Gottlieb 	esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
4009cecae747SMaor Gottlieb 		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
4010b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
4011d4be3f44SYishai Hadas 		max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
4012d4be3f44SYishai Hadas 					log_max_ft_size));
4013cecae747SMaor Gottlieb 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap) && !esw_encap)
4014b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
4015b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
4016cecae747SMaor Gottlieb 					      reformat_l3_tunnel_to_l2) &&
4017cecae747SMaor Gottlieb 		    !esw_encap)
4018b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
401913a43765SMark Bloch 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS) {
402013a43765SMark Bloch 		max_table_size = BIT(
402113a43765SMark Bloch 			MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, log_max_ft_size));
4022cecae747SMaor Gottlieb 		if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat) && !esw_encap)
4023b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
402413a43765SMark Bloch 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB) {
402513a43765SMark Bloch 		max_table_size = BIT(
402613a43765SMark Bloch 			MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, log_max_ft_size));
402709d985beSMaor Gottlieb 		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, decap) && esw_encap)
402809d985beSMaor Gottlieb 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
402909d985beSMaor Gottlieb 		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, reformat_l3_tunnel_to_l2) &&
403009d985beSMaor Gottlieb 		    esw_encap)
403109d985beSMaor Gottlieb 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
403213a43765SMark Bloch 		priority = FDB_BYPASS_PATH;
4033d8abe884SMark Zhang 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX) {
4034d8abe884SMark Zhang 		max_table_size =
4035d8abe884SMark Zhang 			BIT(MLX5_CAP_FLOWTABLE_RDMA_RX(dev->mdev,
4036d8abe884SMark Zhang 						       log_max_ft_size));
4037d8abe884SMark Zhang 		priority = fs_matcher->priority;
4038b47fd4ffSMark Bloch 	}
4039b47fd4ffSMark Bloch 
40403b70508aSMark Bloch 	max_table_size = min_t(int, max_table_size, MLX5_FS_MAX_ENTRIES);
4041d4be3f44SYishai Hadas 
4042b47fd4ffSMark Bloch 	ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
4043d4be3f44SYishai Hadas 	if (!ns)
4044d4be3f44SYishai Hadas 		return ERR_PTR(-ENOTSUPP);
4045d4be3f44SYishai Hadas 
4046b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
4047d4be3f44SYishai Hadas 		prio = &dev->flow_db->prios[priority];
404813a43765SMark Bloch 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS)
4049b47fd4ffSMark Bloch 		prio = &dev->flow_db->egress_prios[priority];
405013a43765SMark Bloch 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB)
405113a43765SMark Bloch 		prio = &dev->flow_db->fdb;
4052d8abe884SMark Zhang 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX)
4053d8abe884SMark Zhang 		prio = &dev->flow_db->rdma_rx[priority];
405413a43765SMark Bloch 
405513a43765SMark Bloch 	if (!prio)
405613a43765SMark Bloch 		return ERR_PTR(-EINVAL);
4057d4be3f44SYishai Hadas 
4058d4be3f44SYishai Hadas 	if (prio->flow_table)
4059d4be3f44SYishai Hadas 		return prio;
4060d4be3f44SYishai Hadas 
40613b70508aSMark Bloch 	return _get_prio(ns, prio, priority, max_table_size,
4062b47fd4ffSMark Bloch 			 MLX5_FS_MAX_TYPES, flags);
4063d4be3f44SYishai Hadas }
4064d4be3f44SYishai Hadas 
4065d4be3f44SYishai Hadas static struct mlx5_ib_flow_handler *
4066d4be3f44SYishai Hadas _create_raw_flow_rule(struct mlx5_ib_dev *dev,
4067d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_prio *ft_prio,
4068d4be3f44SYishai Hadas 		      struct mlx5_flow_destination *dst,
4069d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_matcher  *fs_matcher,
4070bb0ee7dcSJianbo Liu 		      struct mlx5_flow_context *flow_context,
4071b823dd6dSMark Bloch 		      struct mlx5_flow_act *flow_act,
4072bfc5d839SMark Bloch 		      void *cmd_in, int inlen,
4073bfc5d839SMark Bloch 		      int dst_num)
4074d4be3f44SYishai Hadas {
4075d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
4076d4be3f44SYishai Hadas 	struct mlx5_flow_spec *spec;
4077d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft = ft_prio->flow_table;
4078d4be3f44SYishai Hadas 	int err = 0;
4079d4be3f44SYishai Hadas 
4080d4be3f44SYishai Hadas 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
4081d4be3f44SYishai Hadas 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
4082d4be3f44SYishai Hadas 	if (!handler || !spec) {
4083d4be3f44SYishai Hadas 		err = -ENOMEM;
4084d4be3f44SYishai Hadas 		goto free;
4085d4be3f44SYishai Hadas 	}
4086d4be3f44SYishai Hadas 
4087d4be3f44SYishai Hadas 	INIT_LIST_HEAD(&handler->list);
4088d4be3f44SYishai Hadas 
4089d4be3f44SYishai Hadas 	memcpy(spec->match_value, cmd_in, inlen);
4090d4be3f44SYishai Hadas 	memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
4091d4be3f44SYishai Hadas 	       fs_matcher->mask_len);
4092d4be3f44SYishai Hadas 	spec->match_criteria_enable = fs_matcher->match_criteria_enable;
4093bb0ee7dcSJianbo Liu 	spec->flow_context = *flow_context;
4094d4be3f44SYishai Hadas 
4095d4be3f44SYishai Hadas 	handler->rule = mlx5_add_flow_rules(ft, spec,
4096bfc5d839SMark Bloch 					    flow_act, dst, dst_num);
4097d4be3f44SYishai Hadas 
4098d4be3f44SYishai Hadas 	if (IS_ERR(handler->rule)) {
4099d4be3f44SYishai Hadas 		err = PTR_ERR(handler->rule);
4100d4be3f44SYishai Hadas 		goto free;
4101d4be3f44SYishai Hadas 	}
4102d4be3f44SYishai Hadas 
4103d4be3f44SYishai Hadas 	ft_prio->refcount++;
4104d4be3f44SYishai Hadas 	handler->prio = ft_prio;
4105d4be3f44SYishai Hadas 	handler->dev = dev;
4106d4be3f44SYishai Hadas 	ft_prio->flow_table = ft;
4107d4be3f44SYishai Hadas 
4108d4be3f44SYishai Hadas free:
4109d4be3f44SYishai Hadas 	if (err)
4110d4be3f44SYishai Hadas 		kfree(handler);
4111d4be3f44SYishai Hadas 	kvfree(spec);
4112d4be3f44SYishai Hadas 	return err ? ERR_PTR(err) : handler;
4113d4be3f44SYishai Hadas }
4114d4be3f44SYishai Hadas 
4115d4be3f44SYishai Hadas static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
4116d4be3f44SYishai Hadas 				void *match_v)
4117d4be3f44SYishai Hadas {
4118d4be3f44SYishai Hadas 	void *match_c;
4119d4be3f44SYishai Hadas 	void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
4120d4be3f44SYishai Hadas 	void *dmac, *dmac_mask;
4121d4be3f44SYishai Hadas 	void *ipv4, *ipv4_mask;
4122d4be3f44SYishai Hadas 
4123d4be3f44SYishai Hadas 	if (!(fs_matcher->match_criteria_enable &
4124d4be3f44SYishai Hadas 	      (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
4125d4be3f44SYishai Hadas 		return false;
4126d4be3f44SYishai Hadas 
4127d4be3f44SYishai Hadas 	match_c = fs_matcher->matcher_mask.match_params;
4128d4be3f44SYishai Hadas 	match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
4129d4be3f44SYishai Hadas 					   outer_headers);
4130d4be3f44SYishai Hadas 	match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
4131d4be3f44SYishai Hadas 					   outer_headers);
4132d4be3f44SYishai Hadas 
4133d4be3f44SYishai Hadas 	dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4134d4be3f44SYishai Hadas 			    dmac_47_16);
4135d4be3f44SYishai Hadas 	dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4136d4be3f44SYishai Hadas 				 dmac_47_16);
4137d4be3f44SYishai Hadas 
4138d4be3f44SYishai Hadas 	if (is_multicast_ether_addr(dmac) &&
4139d4be3f44SYishai Hadas 	    is_multicast_ether_addr(dmac_mask))
4140d4be3f44SYishai Hadas 		return true;
4141d4be3f44SYishai Hadas 
4142d4be3f44SYishai Hadas 	ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4143d4be3f44SYishai Hadas 			    dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4144d4be3f44SYishai Hadas 
4145d4be3f44SYishai Hadas 	ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4146d4be3f44SYishai Hadas 				 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4147d4be3f44SYishai Hadas 
4148d4be3f44SYishai Hadas 	if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
4149d4be3f44SYishai Hadas 	    ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
4150d4be3f44SYishai Hadas 		return true;
4151d4be3f44SYishai Hadas 
4152d4be3f44SYishai Hadas 	return false;
4153d4be3f44SYishai Hadas }
4154d4be3f44SYishai Hadas 
415532269441SYishai Hadas struct mlx5_ib_flow_handler *
415632269441SYishai Hadas mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
415732269441SYishai Hadas 			struct mlx5_ib_flow_matcher *fs_matcher,
4158bb0ee7dcSJianbo Liu 			struct mlx5_flow_context *flow_context,
4159b823dd6dSMark Bloch 			struct mlx5_flow_act *flow_act,
4160bfc5d839SMark Bloch 			u32 counter_id,
416132269441SYishai Hadas 			void *cmd_in, int inlen, int dest_id,
416232269441SYishai Hadas 			int dest_type)
416332269441SYishai Hadas {
4164d4be3f44SYishai Hadas 	struct mlx5_flow_destination *dst;
4165d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *ft_prio;
4166d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
4167bfc5d839SMark Bloch 	int dst_num = 0;
4168d4be3f44SYishai Hadas 	bool mcast;
4169d4be3f44SYishai Hadas 	int err;
4170d4be3f44SYishai Hadas 
4171d4be3f44SYishai Hadas 	if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
417232269441SYishai Hadas 		return ERR_PTR(-EOPNOTSUPP);
4173d4be3f44SYishai Hadas 
4174d4be3f44SYishai Hadas 	if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
4175d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
4176d4be3f44SYishai Hadas 
41778e8aa145SGustavo A. R. Silva 	dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
4178d4be3f44SYishai Hadas 	if (!dst)
4179d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
4180d4be3f44SYishai Hadas 
4181d4be3f44SYishai Hadas 	mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
4182d4be3f44SYishai Hadas 	mutex_lock(&dev->flow_db->lock);
4183d4be3f44SYishai Hadas 
4184b47fd4ffSMark Bloch 	ft_prio = _get_flow_table(dev, fs_matcher, mcast);
4185d4be3f44SYishai Hadas 	if (IS_ERR(ft_prio)) {
4186d4be3f44SYishai Hadas 		err = PTR_ERR(ft_prio);
4187d4be3f44SYishai Hadas 		goto unlock;
4188d4be3f44SYishai Hadas 	}
4189d4be3f44SYishai Hadas 
41906346f0bfSYishai Hadas 	if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
4191bfc5d839SMark Bloch 		dst[dst_num].type = dest_type;
4192bfc5d839SMark Bloch 		dst[dst_num].tir_num = dest_id;
4193b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
4194a7ee18bdSMark Bloch 	} else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
4195bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
4196bfc5d839SMark Bloch 		dst[dst_num].ft_num = dest_id;
4197b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
4198a7ee18bdSMark Bloch 	} else {
4199bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
4200a7ee18bdSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
42016346f0bfSYishai Hadas 	}
42026346f0bfSYishai Hadas 
4203bfc5d839SMark Bloch 	dst_num++;
4204bfc5d839SMark Bloch 
4205bfc5d839SMark Bloch 	if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
4206bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
4207bfc5d839SMark Bloch 		dst[dst_num].counter_id = counter_id;
4208bfc5d839SMark Bloch 		dst_num++;
4209bfc5d839SMark Bloch 	}
4210bfc5d839SMark Bloch 
4211bb0ee7dcSJianbo Liu 	handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher,
4212bb0ee7dcSJianbo Liu 					flow_context, flow_act,
4213bfc5d839SMark Bloch 					cmd_in, inlen, dst_num);
4214d4be3f44SYishai Hadas 
4215d4be3f44SYishai Hadas 	if (IS_ERR(handler)) {
4216d4be3f44SYishai Hadas 		err = PTR_ERR(handler);
4217d4be3f44SYishai Hadas 		goto destroy_ft;
4218d4be3f44SYishai Hadas 	}
4219d4be3f44SYishai Hadas 
4220d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
4221d4be3f44SYishai Hadas 	atomic_inc(&fs_matcher->usecnt);
4222d4be3f44SYishai Hadas 	handler->flow_matcher = fs_matcher;
4223d4be3f44SYishai Hadas 
4224d4be3f44SYishai Hadas 	kfree(dst);
4225d4be3f44SYishai Hadas 
4226d4be3f44SYishai Hadas 	return handler;
4227d4be3f44SYishai Hadas 
4228d4be3f44SYishai Hadas destroy_ft:
4229d4be3f44SYishai Hadas 	put_flow_table(dev, ft_prio, false);
4230d4be3f44SYishai Hadas unlock:
4231d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
4232d4be3f44SYishai Hadas 	kfree(dst);
4233d4be3f44SYishai Hadas 
4234d4be3f44SYishai Hadas 	return ERR_PTR(err);
423532269441SYishai Hadas }
423632269441SYishai Hadas 
4237c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
4238c6475a0bSAviad Yehezkel {
4239c6475a0bSAviad Yehezkel 	u32 flags = 0;
4240c6475a0bSAviad Yehezkel 
4241c6475a0bSAviad Yehezkel 	if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
4242c6475a0bSAviad Yehezkel 		flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
4243c6475a0bSAviad Yehezkel 
4244c6475a0bSAviad Yehezkel 	return flags;
4245c6475a0bSAviad Yehezkel }
4246c6475a0bSAviad Yehezkel 
4247c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED	MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
4248c6475a0bSAviad Yehezkel static struct ib_flow_action *
4249c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device,
4250c6475a0bSAviad Yehezkel 			       const struct ib_flow_action_attrs_esp *attr,
4251c6475a0bSAviad Yehezkel 			       struct uverbs_attr_bundle *attrs)
4252c6475a0bSAviad Yehezkel {
4253c6475a0bSAviad Yehezkel 	struct mlx5_ib_dev *mdev = to_mdev(device);
4254c6475a0bSAviad Yehezkel 	struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
4255c6475a0bSAviad Yehezkel 	struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
4256c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *action;
4257c6475a0bSAviad Yehezkel 	u64 action_flags;
4258c6475a0bSAviad Yehezkel 	u64 flags;
4259c6475a0bSAviad Yehezkel 	int err = 0;
4260c6475a0bSAviad Yehezkel 
4261bccd0622SJason Gunthorpe 	err = uverbs_get_flags64(
4262bccd0622SJason Gunthorpe 		&action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4263bccd0622SJason Gunthorpe 		((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
4264bccd0622SJason Gunthorpe 	if (err)
4265bccd0622SJason Gunthorpe 		return ERR_PTR(err);
4266c6475a0bSAviad Yehezkel 
4267c6475a0bSAviad Yehezkel 	flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
4268c6475a0bSAviad Yehezkel 
4269c6475a0bSAviad Yehezkel 	/* We current only support a subset of the standard features. Only a
4270c6475a0bSAviad Yehezkel 	 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
4271c6475a0bSAviad Yehezkel 	 * (with overlap). Full offload mode isn't supported.
4272c6475a0bSAviad Yehezkel 	 */
4273c6475a0bSAviad Yehezkel 	if (!attr->keymat || attr->replay || attr->encap ||
4274c6475a0bSAviad Yehezkel 	    attr->spi || attr->seq || attr->tfc_pad ||
4275c6475a0bSAviad Yehezkel 	    attr->hard_limit_pkts ||
4276c6475a0bSAviad Yehezkel 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4277c6475a0bSAviad Yehezkel 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
4278c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4279c6475a0bSAviad Yehezkel 
4280c6475a0bSAviad Yehezkel 	if (attr->keymat->protocol !=
4281c6475a0bSAviad Yehezkel 	    IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
4282c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4283c6475a0bSAviad Yehezkel 
4284c6475a0bSAviad Yehezkel 	aes_gcm = &attr->keymat->keymat.aes_gcm;
4285c6475a0bSAviad Yehezkel 
4286c6475a0bSAviad Yehezkel 	if (aes_gcm->icv_len != 16 ||
4287c6475a0bSAviad Yehezkel 	    aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
4288c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4289c6475a0bSAviad Yehezkel 
4290c6475a0bSAviad Yehezkel 	action = kmalloc(sizeof(*action), GFP_KERNEL);
4291c6475a0bSAviad Yehezkel 	if (!action)
4292c6475a0bSAviad Yehezkel 		return ERR_PTR(-ENOMEM);
4293c6475a0bSAviad Yehezkel 
4294c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4295c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
4296c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.aes_key));
4297c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
4298c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
4299c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.salt));
4300c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
4301c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
4302c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
4303c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
4304c6475a0bSAviad Yehezkel 	accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
4305c6475a0bSAviad Yehezkel 
4306c6475a0bSAviad Yehezkel 	accel_attrs.esn = attr->esn;
4307c6475a0bSAviad Yehezkel 	if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4308c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4309c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4310c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4311c6475a0bSAviad Yehezkel 
4312c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4313c6475a0bSAviad Yehezkel 		accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4314c6475a0bSAviad Yehezkel 
4315c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ctx =
4316c6475a0bSAviad Yehezkel 		mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4317c6475a0bSAviad Yehezkel 	if (IS_ERR(action->esp_aes_gcm.ctx)) {
4318c6475a0bSAviad Yehezkel 		err = PTR_ERR(action->esp_aes_gcm.ctx);
4319c6475a0bSAviad Yehezkel 		goto err_parse;
4320c6475a0bSAviad Yehezkel 	}
4321c6475a0bSAviad Yehezkel 
4322c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4323c6475a0bSAviad Yehezkel 
4324c6475a0bSAviad Yehezkel 	return &action->ib_action;
4325c6475a0bSAviad Yehezkel 
4326c6475a0bSAviad Yehezkel err_parse:
4327c6475a0bSAviad Yehezkel 	kfree(action);
4328c6475a0bSAviad Yehezkel 	return ERR_PTR(err);
4329c6475a0bSAviad Yehezkel }
4330c6475a0bSAviad Yehezkel 
4331349705c1SMatan Barak static int
4332349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4333349705c1SMatan Barak 			       const struct ib_flow_action_attrs_esp *attr,
4334349705c1SMatan Barak 			       struct uverbs_attr_bundle *attrs)
4335349705c1SMatan Barak {
4336349705c1SMatan Barak 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4337349705c1SMatan Barak 	struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4338349705c1SMatan Barak 	int err = 0;
4339349705c1SMatan Barak 
4340349705c1SMatan Barak 	if (attr->keymat || attr->replay || attr->encap ||
4341349705c1SMatan Barak 	    attr->spi || attr->seq || attr->tfc_pad ||
4342349705c1SMatan Barak 	    attr->hard_limit_pkts ||
4343349705c1SMatan Barak 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4344349705c1SMatan Barak 			     IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4345349705c1SMatan Barak 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4346349705c1SMatan Barak 		return -EOPNOTSUPP;
4347349705c1SMatan Barak 
4348349705c1SMatan Barak 	/* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4349349705c1SMatan Barak 	 * be modified.
4350349705c1SMatan Barak 	 */
4351349705c1SMatan Barak 	if (!(maction->esp_aes_gcm.ib_flags &
4352349705c1SMatan Barak 	      IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4353349705c1SMatan Barak 	    attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4354349705c1SMatan Barak 			   IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4355349705c1SMatan Barak 		return -EINVAL;
4356349705c1SMatan Barak 
4357349705c1SMatan Barak 	memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4358349705c1SMatan Barak 	       sizeof(accel_attrs));
4359349705c1SMatan Barak 
4360349705c1SMatan Barak 	accel_attrs.esn = attr->esn;
4361349705c1SMatan Barak 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4362349705c1SMatan Barak 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4363349705c1SMatan Barak 	else
4364349705c1SMatan Barak 		accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4365349705c1SMatan Barak 
4366349705c1SMatan Barak 	err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4367349705c1SMatan Barak 					 &accel_attrs);
4368349705c1SMatan Barak 	if (err)
4369349705c1SMatan Barak 		return err;
4370349705c1SMatan Barak 
4371349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags &=
4372349705c1SMatan Barak 		~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4373349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags |=
4374349705c1SMatan Barak 		attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4375349705c1SMatan Barak 
4376349705c1SMatan Barak 	return 0;
4377349705c1SMatan Barak }
4378349705c1SMatan Barak 
4379c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4380c6475a0bSAviad Yehezkel {
4381c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4382c6475a0bSAviad Yehezkel 
4383c6475a0bSAviad Yehezkel 	switch (action->type) {
4384c6475a0bSAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
4385c6475a0bSAviad Yehezkel 		/*
4386c6475a0bSAviad Yehezkel 		 * We only support aes_gcm by now, so we implicitly know this is
4387c6475a0bSAviad Yehezkel 		 * the underline crypto.
4388c6475a0bSAviad Yehezkel 		 */
4389c6475a0bSAviad Yehezkel 		mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4390c6475a0bSAviad Yehezkel 		break;
4391b4749bf2SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
4392b4749bf2SMark Bloch 		mlx5_ib_destroy_flow_action_raw(maction);
4393b4749bf2SMark Bloch 		break;
4394c6475a0bSAviad Yehezkel 	default:
4395c6475a0bSAviad Yehezkel 		WARN_ON(true);
4396c6475a0bSAviad Yehezkel 		break;
4397c6475a0bSAviad Yehezkel 	}
4398c6475a0bSAviad Yehezkel 
4399c6475a0bSAviad Yehezkel 	kfree(maction);
4400c6475a0bSAviad Yehezkel 	return 0;
4401c6475a0bSAviad Yehezkel }
4402c6475a0bSAviad Yehezkel 
4403e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4404e126ba97SEli Cohen {
4405e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
440681e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
4407e126ba97SEli Cohen 	int err;
4408539ec982SYishai Hadas 	u16 uid;
4409539ec982SYishai Hadas 
4410539ec982SYishai Hadas 	uid = ibqp->pd ?
4411539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4412e126ba97SEli Cohen 
441381e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
441481e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
441581e30880SYishai Hadas 		return -EOPNOTSUPP;
441681e30880SYishai Hadas 	}
441781e30880SYishai Hadas 
4418539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4419e126ba97SEli Cohen 	if (err)
4420e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4421e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4422e126ba97SEli Cohen 
4423e126ba97SEli Cohen 	return err;
4424e126ba97SEli Cohen }
4425e126ba97SEli Cohen 
4426e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4427e126ba97SEli Cohen {
4428e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4429e126ba97SEli Cohen 	int err;
4430539ec982SYishai Hadas 	u16 uid;
4431e126ba97SEli Cohen 
4432539ec982SYishai Hadas 	uid = ibqp->pd ?
4433539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4434539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4435e126ba97SEli Cohen 	if (err)
4436e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4437e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4438e126ba97SEli Cohen 
4439e126ba97SEli Cohen 	return err;
4440e126ba97SEli Cohen }
4441e126ba97SEli Cohen 
4442e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
4443e126ba97SEli Cohen {
44441b5daf11SMajd Dibbiny 	int err;
4445e126ba97SEli Cohen 
44461b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
4447e126ba97SEli Cohen 	if (err)
4448e126ba97SEli Cohen 		return err;
44491b5daf11SMajd Dibbiny 
44501b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
44511b5daf11SMajd Dibbiny 
44521b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
4453e126ba97SEli Cohen }
4454e126ba97SEli Cohen 
4455508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
4456508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4457e126ba97SEli Cohen {
4458e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
445954747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4460e126ba97SEli Cohen 
44619603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
4462e126ba97SEli Cohen }
4463508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
4464e126ba97SEli Cohen 
4465508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
4466e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
4467e126ba97SEli Cohen {
4468e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
446954747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4470e126ba97SEli Cohen 
44716aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
4472e126ba97SEli Cohen }
4473508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
4474e126ba97SEli Cohen 
4475508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
4476508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4477e126ba97SEli Cohen {
4478e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
447954747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
448054747231SParav Pandit 
44819603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
4482e126ba97SEli Cohen }
4483508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
4484e126ba97SEli Cohen 
4485508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
4486508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
4487e126ba97SEli Cohen {
4488e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
448954747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
449054747231SParav Pandit 
44919603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
4492e126ba97SEli Cohen }
4493508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
4494e126ba97SEli Cohen 
4495508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
4496508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4497e126ba97SEli Cohen {
4498e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
449954747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
450054747231SParav Pandit 
4501e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
45029603b61dSJack Morgenstein 		       dev->mdev->board_id);
4503e126ba97SEli Cohen }
4504508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
4505e126ba97SEli Cohen 
4506508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
4507508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
4508508a523fSParav Pandit 	&dev_attr_hca_type.attr,
4509508a523fSParav Pandit 	&dev_attr_board_id.attr,
4510508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
4511508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
4512508a523fSParav Pandit 	NULL,
4513508a523fSParav Pandit };
4514e126ba97SEli Cohen 
4515508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
4516508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
4517e126ba97SEli Cohen };
4518e126ba97SEli Cohen 
45197722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
45207722f47eSHaggai Eran {
45217722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
45227722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
45237722f47eSHaggai Eran 			     pkey_change_work);
45247722f47eSHaggai Eran 
45257722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
45267722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
45277722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
45287722f47eSHaggai Eran }
45297722f47eSHaggai Eran 
453089ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
453189ea94a7SMaor Gottlieb {
453289ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
453389ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
453489ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
453589ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
453689ea94a7SMaor Gottlieb 	unsigned long flags_qp;
453789ea94a7SMaor Gottlieb 	unsigned long flags_cq;
453889ea94a7SMaor Gottlieb 	unsigned long flags;
453989ea94a7SMaor Gottlieb 
454089ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
454189ea94a7SMaor Gottlieb 
454289ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
454389ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
454489ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
454589ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
454689ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
454789ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
454889ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
454989ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
455089ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
455189ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
455289ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
455389ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
455489ea94a7SMaor Gottlieb 						      &cq_armed_list);
455589ea94a7SMaor Gottlieb 				}
455689ea94a7SMaor Gottlieb 			}
455789ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
455889ea94a7SMaor Gottlieb 		}
455989ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
456089ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
456189ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
456289ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
456389ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
456489ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
456589ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
456689ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
456789ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
456889ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
456989ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
457089ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
457189ea94a7SMaor Gottlieb 							      &cq_armed_list);
457289ea94a7SMaor Gottlieb 					}
457389ea94a7SMaor Gottlieb 				}
457489ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
457589ea94a7SMaor Gottlieb 						       flags_cq);
457689ea94a7SMaor Gottlieb 			}
457789ea94a7SMaor Gottlieb 		}
457889ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
457989ea94a7SMaor Gottlieb 	}
458089ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
458189ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
458289ea94a7SMaor Gottlieb 	 */
458389ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
45844e0e2ea1SYishai Hadas 		mcq->comp(mcq, NULL);
458589ea94a7SMaor Gottlieb 	}
458689ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
458789ea94a7SMaor Gottlieb }
458889ea94a7SMaor Gottlieb 
458903404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
459003404e8aSMaor Gottlieb {
459103404e8aSMaor Gottlieb 	int err;
459203404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
459303404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
459403404e8aSMaor Gottlieb 			     delay_drop_work);
459503404e8aSMaor Gottlieb 
4596fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
4597fe248c3aSMaor Gottlieb 
459803404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
459903404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
460003404e8aSMaor Gottlieb 				       delay_drop->timeout);
460103404e8aSMaor Gottlieb 	if (err) {
460203404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
460303404e8aSMaor Gottlieb 			     delay_drop->timeout);
460403404e8aSMaor Gottlieb 		delay_drop->activate = false;
460503404e8aSMaor Gottlieb 	}
460603404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
460703404e8aSMaor Gottlieb }
460803404e8aSMaor Gottlieb 
460909e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
461009e574faSSaeed Mahameed 				 struct ib_event *ibev)
461109e574faSSaeed Mahameed {
46126cfdc7e4SAya Levin 	u8 port = (eqe->data.port.port >> 4) & 0xf;
46136cfdc7e4SAya Levin 
461409e574faSSaeed Mahameed 	switch (eqe->sub_type) {
461509e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
46166cfdc7e4SAya Levin 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
46176cfdc7e4SAya Levin 					    IB_LINK_LAYER_ETHERNET)
461809e574faSSaeed Mahameed 			schedule_work(&ibdev->delay_drop.delay_drop_work);
461909e574faSSaeed Mahameed 		break;
462009e574faSSaeed Mahameed 	default: /* do nothing */
462109e574faSSaeed Mahameed 		return;
462209e574faSSaeed Mahameed 	}
462309e574faSSaeed Mahameed }
462409e574faSSaeed Mahameed 
4625134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4626134e9349SSaeed Mahameed 			      struct ib_event *ibev)
4627134e9349SSaeed Mahameed {
4628134e9349SSaeed Mahameed 	u8 port = (eqe->data.port.port >> 4) & 0xf;
4629134e9349SSaeed Mahameed 
4630134e9349SSaeed Mahameed 	ibev->element.port_num = port;
4631134e9349SSaeed Mahameed 
4632134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
4633134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4634134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4635134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4636134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
4637134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
4638134e9349SSaeed Mahameed 		 */
4639134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4640134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
4641134e9349SSaeed Mahameed 			return -EINVAL;
4642134e9349SSaeed Mahameed 
4643134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4644134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4645134e9349SSaeed Mahameed 		break;
4646134e9349SSaeed Mahameed 
4647134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
4648134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
4649134e9349SSaeed Mahameed 		break;
4650134e9349SSaeed Mahameed 
4651134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4652134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
4653134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4654134e9349SSaeed Mahameed 		break;
4655134e9349SSaeed Mahameed 
4656134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4657134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
4658134e9349SSaeed Mahameed 		break;
4659134e9349SSaeed Mahameed 
4660134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4661134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
4662134e9349SSaeed Mahameed 		break;
4663134e9349SSaeed Mahameed 	default:
4664134e9349SSaeed Mahameed 		return -EINVAL;
4665134e9349SSaeed Mahameed 	}
4666134e9349SSaeed Mahameed 
4667134e9349SSaeed Mahameed 	return 0;
4668134e9349SSaeed Mahameed }
4669134e9349SSaeed Mahameed 
4670d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
4671e126ba97SEli Cohen {
4672d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
4673d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
4674d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
4675e126ba97SEli Cohen 	struct ib_event ibev;
4676dbaaff2aSEli Cohen 	bool fatal = false;
4677e126ba97SEli Cohen 
4678df097a27SSaeed Mahameed 	if (work->is_slave) {
4679df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
4680d69a24e0SDaniel Jurgens 		if (!ibdev)
4681d69a24e0SDaniel Jurgens 			goto out;
4682d69a24e0SDaniel Jurgens 	} else {
4683df097a27SSaeed Mahameed 		ibdev = work->dev;
4684d69a24e0SDaniel Jurgens 	}
4685d69a24e0SDaniel Jurgens 
4686d69a24e0SDaniel Jurgens 	switch (work->event) {
4687e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
4688e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
468989ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
4690134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
4691dbaaff2aSEli Cohen 		fatal = true;
4692e126ba97SEli Cohen 		break;
4693134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
4694134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
4695d69a24e0SDaniel Jurgens 			goto out;
4696e126ba97SEli Cohen 		break;
469709e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
469809e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
469909e574faSSaeed Mahameed 		/* fall through */
4700bdc37924SSaeed Mahameed 	default:
470103404e8aSMaor Gottlieb 		goto out;
4702e126ba97SEli Cohen 	}
4703e126ba97SEli Cohen 
4704e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
4705e126ba97SEli Cohen 
4706134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4707134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
470803404e8aSMaor Gottlieb 		goto out;
4709a0c84c32SEli Cohen 	}
4710a0c84c32SEli Cohen 
4711e126ba97SEli Cohen 	if (ibdev->ib_active)
4712e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
4713dbaaff2aSEli Cohen 
4714dbaaff2aSEli Cohen 	if (fatal)
4715dbaaff2aSEli Cohen 		ibdev->ib_active = false;
471603404e8aSMaor Gottlieb out:
4717d69a24e0SDaniel Jurgens 	kfree(work);
4718d69a24e0SDaniel Jurgens }
4719d69a24e0SDaniel Jurgens 
4720df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
4721df097a27SSaeed Mahameed 			 unsigned long event, void *param)
4722d69a24e0SDaniel Jurgens {
4723d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
4724d69a24e0SDaniel Jurgens 
4725d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
472610bea9c8SLeon Romanovsky 	if (!work)
4727df097a27SSaeed Mahameed 		return NOTIFY_DONE;
472810bea9c8SLeon Romanovsky 
4729d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4730df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4731df097a27SSaeed Mahameed 	work->is_slave = false;
4732d69a24e0SDaniel Jurgens 	work->param = param;
4733d69a24e0SDaniel Jurgens 	work->event = event;
4734d69a24e0SDaniel Jurgens 
4735d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
4736df097a27SSaeed Mahameed 
4737df097a27SSaeed Mahameed 	return NOTIFY_OK;
4738df097a27SSaeed Mahameed }
4739df097a27SSaeed Mahameed 
4740df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4741df097a27SSaeed Mahameed 				    unsigned long event, void *param)
4742df097a27SSaeed Mahameed {
4743df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
4744df097a27SSaeed Mahameed 
4745df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
4746df097a27SSaeed Mahameed 	if (!work)
4747df097a27SSaeed Mahameed 		return NOTIFY_DONE;
4748df097a27SSaeed Mahameed 
4749df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4750df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4751df097a27SSaeed Mahameed 	work->is_slave = true;
4752df097a27SSaeed Mahameed 	work->param = param;
4753df097a27SSaeed Mahameed 	work->event = event;
4754df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
4755df097a27SSaeed Mahameed 
4756df097a27SSaeed Mahameed 	return NOTIFY_OK;
4757d69a24e0SDaniel Jurgens }
4758d69a24e0SDaniel Jurgens 
4759c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4760c43f1112SMaor Gottlieb {
4761c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
4762c43f1112SMaor Gottlieb 	int err;
4763c43f1112SMaor Gottlieb 	int port;
4764c43f1112SMaor Gottlieb 
4765a989ea01SMark Bloch 	for (port = 1; port <= ARRAY_SIZE(dev->mdev->port_caps); port++) {
4766c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
4767c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4768c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
4769c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4770c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
4771c43f1112SMaor Gottlieb 								   port, 0,
4772c43f1112SMaor Gottlieb 								   &vport_ctx);
4773c43f1112SMaor Gottlieb 				if (err) {
4774c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4775c43f1112SMaor Gottlieb 						    port, err);
4776c43f1112SMaor Gottlieb 					return err;
4777c43f1112SMaor Gottlieb 				}
4778c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
4779c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
4780c43f1112SMaor Gottlieb 			} else {
4781c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
4782c43f1112SMaor Gottlieb 			}
4783c43f1112SMaor Gottlieb 		}
4784c43f1112SMaor Gottlieb 	}
4785c43f1112SMaor Gottlieb 	return 0;
4786c43f1112SMaor Gottlieb }
4787c43f1112SMaor Gottlieb 
4788e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4789e126ba97SEli Cohen {
4790e126ba97SEli Cohen 	int port;
4791e126ba97SEli Cohen 
4792508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
4793e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
4794e126ba97SEli Cohen }
4795e126ba97SEli Cohen 
479626628e2dSMark Bloch static int __get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4797e126ba97SEli Cohen {
4798e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
4799e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
4800f614fc15SDan Carpenter 	int err = -ENOMEM;
4801e126ba97SEli Cohen 
480250ba3c18SLeon Romanovsky 	pprops = kzalloc(sizeof(*pprops), GFP_KERNEL);
4803e126ba97SEli Cohen 	if (!pprops)
4804e126ba97SEli Cohen 		goto out;
4805e126ba97SEli Cohen 
4806e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4807e126ba97SEli Cohen 	if (!dprops)
4808e126ba97SEli Cohen 		goto out;
4809e126ba97SEli Cohen 
481048357091SLeon Romanovsky 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, NULL);
4811e126ba97SEli Cohen 	if (err) {
4812e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
4813e126ba97SEli Cohen 		goto out;
4814e126ba97SEli Cohen 	}
4815e126ba97SEli Cohen 
4816e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4817e126ba97SEli Cohen 	if (err) {
4818938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
4819938fe83cSSaeed Mahameed 			     port, err);
482032f69e4bSDaniel Jurgens 		goto out;
4821e126ba97SEli Cohen 	}
482232f69e4bSDaniel Jurgens 
4823938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
4824938fe83cSSaeed Mahameed 					dprops->max_pkeys;
4825938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
4826938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
482732f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
482832f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
4829e126ba97SEli Cohen 
4830e126ba97SEli Cohen out:
4831e126ba97SEli Cohen 	kfree(pprops);
4832e126ba97SEli Cohen 	kfree(dprops);
4833e126ba97SEli Cohen 
4834e126ba97SEli Cohen 	return err;
4835e126ba97SEli Cohen }
4836e126ba97SEli Cohen 
483726628e2dSMark Bloch static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
483826628e2dSMark Bloch {
483926628e2dSMark Bloch 	/* For representors use port 1, is this is the only native
484026628e2dSMark Bloch 	 * port
484126628e2dSMark Bloch 	 */
484226628e2dSMark Bloch 	if (dev->is_rep)
484326628e2dSMark Bloch 		return __get_port_caps(dev, 1);
484426628e2dSMark Bloch 	return __get_port_caps(dev, port);
484526628e2dSMark Bloch }
484626628e2dSMark Bloch 
4847e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4848e126ba97SEli Cohen {
4849e126ba97SEli Cohen 	int err;
4850e126ba97SEli Cohen 
4851e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
4852e126ba97SEli Cohen 	if (err)
4853e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4854e126ba97SEli Cohen 
485532927e28SMark Bloch 	if (dev->umrc.qp)
4856c4367a26SShamir Rabinovitch 		mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
485732927e28SMark Bloch 	if (dev->umrc.cq)
4858add08d76SChristoph Hellwig 		ib_free_cq(dev->umrc.cq);
485932927e28SMark Bloch 	if (dev->umrc.pd)
4860e126ba97SEli Cohen 		ib_dealloc_pd(dev->umrc.pd);
4861e126ba97SEli Cohen }
4862e126ba97SEli Cohen 
4863e126ba97SEli Cohen enum {
4864e126ba97SEli Cohen 	MAX_UMR_WR = 128,
4865e126ba97SEli Cohen };
4866e126ba97SEli Cohen 
4867e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
4868e126ba97SEli Cohen {
4869e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
4870e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
4871e126ba97SEli Cohen 	struct ib_pd *pd;
4872e126ba97SEli Cohen 	struct ib_cq *cq;
4873e126ba97SEli Cohen 	struct ib_qp *qp;
4874e126ba97SEli Cohen 	int ret;
4875e126ba97SEli Cohen 
4876e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4877e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4878e126ba97SEli Cohen 	if (!attr || !init_attr) {
4879e126ba97SEli Cohen 		ret = -ENOMEM;
4880e126ba97SEli Cohen 		goto error_0;
4881e126ba97SEli Cohen 	}
4882e126ba97SEli Cohen 
4883ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
4884e126ba97SEli Cohen 	if (IS_ERR(pd)) {
4885e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4886e126ba97SEli Cohen 		ret = PTR_ERR(pd);
4887e126ba97SEli Cohen 		goto error_0;
4888e126ba97SEli Cohen 	}
4889e126ba97SEli Cohen 
4890add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
4891e126ba97SEli Cohen 	if (IS_ERR(cq)) {
4892e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4893e126ba97SEli Cohen 		ret = PTR_ERR(cq);
4894e126ba97SEli Cohen 		goto error_2;
4895e126ba97SEli Cohen 	}
4896e126ba97SEli Cohen 
4897e126ba97SEli Cohen 	init_attr->send_cq = cq;
4898e126ba97SEli Cohen 	init_attr->recv_cq = cq;
4899e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4900e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
4901e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
4902e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4903e126ba97SEli Cohen 	init_attr->port_num = 1;
4904e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4905e126ba97SEli Cohen 	if (IS_ERR(qp)) {
4906e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4907e126ba97SEli Cohen 		ret = PTR_ERR(qp);
4908e126ba97SEli Cohen 		goto error_3;
4909e126ba97SEli Cohen 	}
4910e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
4911e126ba97SEli Cohen 	qp->real_qp    = qp;
4912e126ba97SEli Cohen 	qp->uobject    = NULL;
4913e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
491431fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
491531fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
4916e126ba97SEli Cohen 
4917e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
4918e126ba97SEli Cohen 	attr->port_num = 1;
4919e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4920e126ba97SEli Cohen 				IB_QP_PORT, NULL);
4921e126ba97SEli Cohen 	if (ret) {
4922e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4923e126ba97SEli Cohen 		goto error_4;
4924e126ba97SEli Cohen 	}
4925e126ba97SEli Cohen 
4926e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4927e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
4928e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
4929e126ba97SEli Cohen 
4930e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4931e126ba97SEli Cohen 	if (ret) {
4932e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4933e126ba97SEli Cohen 		goto error_4;
4934e126ba97SEli Cohen 	}
4935e126ba97SEli Cohen 
4936e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4937e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
4938e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4939e126ba97SEli Cohen 	if (ret) {
4940e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4941e126ba97SEli Cohen 		goto error_4;
4942e126ba97SEli Cohen 	}
4943e126ba97SEli Cohen 
4944e126ba97SEli Cohen 	dev->umrc.qp = qp;
4945e126ba97SEli Cohen 	dev->umrc.cq = cq;
4946e126ba97SEli Cohen 	dev->umrc.pd = pd;
4947e126ba97SEli Cohen 
4948e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
4949e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
4950e126ba97SEli Cohen 	if (ret) {
4951e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4952e126ba97SEli Cohen 		goto error_4;
4953e126ba97SEli Cohen 	}
4954e126ba97SEli Cohen 
4955e126ba97SEli Cohen 	kfree(attr);
4956e126ba97SEli Cohen 	kfree(init_attr);
4957e126ba97SEli Cohen 
4958e126ba97SEli Cohen 	return 0;
4959e126ba97SEli Cohen 
4960e126ba97SEli Cohen error_4:
4961c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_qp(qp, NULL);
496232927e28SMark Bloch 	dev->umrc.qp = NULL;
4963e126ba97SEli Cohen 
4964e126ba97SEli Cohen error_3:
4965add08d76SChristoph Hellwig 	ib_free_cq(cq);
496632927e28SMark Bloch 	dev->umrc.cq = NULL;
4967e126ba97SEli Cohen 
4968e126ba97SEli Cohen error_2:
4969e126ba97SEli Cohen 	ib_dealloc_pd(pd);
497032927e28SMark Bloch 	dev->umrc.pd = NULL;
4971e126ba97SEli Cohen 
4972e126ba97SEli Cohen error_0:
4973e126ba97SEli Cohen 	kfree(attr);
4974e126ba97SEli Cohen 	kfree(init_attr);
4975e126ba97SEli Cohen 	return ret;
4976e126ba97SEli Cohen }
4977e126ba97SEli Cohen 
49786e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
49796e8484c5SMax Gurtovoy {
49806e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
49816e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
49826e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
49836e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
49846e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
49856e8484c5SMax Gurtovoy 	default:
49866e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
49876e8484c5SMax Gurtovoy 	}
49886e8484c5SMax Gurtovoy }
49896e8484c5SMax Gurtovoy 
4990e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
4991e126ba97SEli Cohen {
4992e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
4993e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
499421a428a0SLeon Romanovsky 	struct ib_device *ibdev;
4995bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
49967722f47eSHaggai Eran 	int port;
4997e126ba97SEli Cohen 	int ret = 0;
4998e126ba97SEli Cohen 
4999e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
500021a428a0SLeon Romanovsky 	ibdev = &dev->ib_dev;
5001e126ba97SEli Cohen 
5002d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
5003d16e91daSHaggai Eran 
500421a428a0SLeon Romanovsky 	devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
500521a428a0SLeon Romanovsky 	if (!devr->p0)
500621a428a0SLeon Romanovsky 		return -ENOMEM;
500721a428a0SLeon Romanovsky 
500821a428a0SLeon Romanovsky 	devr->p0->device  = ibdev;
5009e126ba97SEli Cohen 	devr->p0->uobject = NULL;
5010e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
5011e126ba97SEli Cohen 
5012ff23dfa1SShamir Rabinovitch 	ret = mlx5_ib_alloc_pd(devr->p0, NULL);
501321a428a0SLeon Romanovsky 	if (ret)
501421a428a0SLeon Romanovsky 		goto error0;
501521a428a0SLeon Romanovsky 
5016e39afe3dSLeon Romanovsky 	devr->c0 = rdma_zalloc_drv_obj(ibdev, ib_cq);
5017e39afe3dSLeon Romanovsky 	if (!devr->c0) {
5018e39afe3dSLeon Romanovsky 		ret = -ENOMEM;
5019e126ba97SEli Cohen 		goto error1;
5020e126ba97SEli Cohen 	}
5021e39afe3dSLeon Romanovsky 
5022e126ba97SEli Cohen 	devr->c0->device = &dev->ib_dev;
5023e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
5024e126ba97SEli Cohen 
5025e39afe3dSLeon Romanovsky 	ret = mlx5_ib_create_cq(devr->c0, &cq_attr, NULL);
5026e39afe3dSLeon Romanovsky 	if (ret)
5027e39afe3dSLeon Romanovsky 		goto err_create_cq;
5028e39afe3dSLeon Romanovsky 
5029ff23dfa1SShamir Rabinovitch 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
5030e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
5031e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
5032e126ba97SEli Cohen 		goto error2;
5033e126ba97SEli Cohen 	}
5034e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
5035e126ba97SEli Cohen 	devr->x0->inode = NULL;
5036e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
5037e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
5038e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
5039e126ba97SEli Cohen 
5040ff23dfa1SShamir Rabinovitch 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
5041e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
5042e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
5043e126ba97SEli Cohen 		goto error3;
5044e126ba97SEli Cohen 	}
5045e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
5046e126ba97SEli Cohen 	devr->x1->inode = NULL;
5047e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
5048e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
5049e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
5050e126ba97SEli Cohen 
5051e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
5052e126ba97SEli Cohen 	attr.attr.max_sge = 1;
5053e126ba97SEli Cohen 	attr.attr.max_wr = 1;
5054e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
50551a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
5056e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
5057e126ba97SEli Cohen 
505868e326deSLeon Romanovsky 	devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
505968e326deSLeon Romanovsky 	if (!devr->s0) {
506068e326deSLeon Romanovsky 		ret = -ENOMEM;
5061e126ba97SEli Cohen 		goto error4;
5062e126ba97SEli Cohen 	}
506368e326deSLeon Romanovsky 
5064e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
5065e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
5066e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
5067e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
50681a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
506968e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
507068e326deSLeon Romanovsky 	if (ret)
507168e326deSLeon Romanovsky 		goto err_create;
507268e326deSLeon Romanovsky 
5073e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
50741a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
5075e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
5076e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
5077e126ba97SEli Cohen 
50784aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
50794aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
50804aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
50814aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
508268e326deSLeon Romanovsky 	devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
508368e326deSLeon Romanovsky 	if (!devr->s1) {
508468e326deSLeon Romanovsky 		ret = -ENOMEM;
50854aa17b28SHaggai Abramonvsky 		goto error5;
50864aa17b28SHaggai Abramonvsky 	}
508768e326deSLeon Romanovsky 
50884aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
50894aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
50904aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
50911a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
509268e326deSLeon Romanovsky 
509368e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
509468e326deSLeon Romanovsky 	if (ret)
509568e326deSLeon Romanovsky 		goto error6;
509668e326deSLeon Romanovsky 
50974aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
50981a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
50994aa17b28SHaggai Abramonvsky 
51007722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
51017722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
51027722f47eSHaggai Eran 			  pkey_change_handler);
51037722f47eSHaggai Eran 		devr->ports[port].devr = devr;
51047722f47eSHaggai Eran 	}
51057722f47eSHaggai Eran 
5106e126ba97SEli Cohen 	return 0;
5107e126ba97SEli Cohen 
510868e326deSLeon Romanovsky error6:
510968e326deSLeon Romanovsky 	kfree(devr->s1);
51104aa17b28SHaggai Abramonvsky error5:
5111c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
511268e326deSLeon Romanovsky err_create:
511368e326deSLeon Romanovsky 	kfree(devr->s0);
5114e126ba97SEli Cohen error4:
5115c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x1, NULL);
5116e126ba97SEli Cohen error3:
5117c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x0, NULL);
5118e126ba97SEli Cohen error2:
5119c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
5120e39afe3dSLeon Romanovsky err_create_cq:
5121e39afe3dSLeon Romanovsky 	kfree(devr->c0);
5122e126ba97SEli Cohen error1:
5123c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
5124e126ba97SEli Cohen error0:
512521a428a0SLeon Romanovsky 	kfree(devr->p0);
5126e126ba97SEli Cohen 	return ret;
5127e126ba97SEli Cohen }
5128e126ba97SEli Cohen 
5129e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
5130e126ba97SEli Cohen {
51317722f47eSHaggai Eran 	int port;
51327722f47eSHaggai Eran 
5133c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s1, NULL);
513468e326deSLeon Romanovsky 	kfree(devr->s1);
5135c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
513668e326deSLeon Romanovsky 	kfree(devr->s0);
5137c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x0, NULL);
5138c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x1, NULL);
5139c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
5140e39afe3dSLeon Romanovsky 	kfree(devr->c0);
5141c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
514221a428a0SLeon Romanovsky 	kfree(devr->p0);
51437722f47eSHaggai Eran 
51447722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
51455d8f6a0eSMark Bloch 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
51467722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
5147e126ba97SEli Cohen }
5148e126ba97SEli Cohen 
5149b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
5150b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
5151e53505a8SAchiad Shochat {
5152e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5153e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
5154e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
5155e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
515685c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
5157e53505a8SAchiad Shochat 	u32 ret = 0;
5158e53505a8SAchiad Shochat 
5159b02289b3SArtemy Kovalyov 	if (rep->grh_required)
5160b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
5161b02289b3SArtemy Kovalyov 
5162e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
5163b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
5164e53505a8SAchiad Shochat 
516585c7c014SDaniel Jurgens 	if (raw_support)
5166b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
516772cd5717SOr Gerlitz 
5168e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
516972cd5717SOr Gerlitz 		return ret;
5170e53505a8SAchiad Shochat 
5171e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
517272cd5717SOr Gerlitz 		return ret;
5173e53505a8SAchiad Shochat 
5174e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
5175e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
5176e53505a8SAchiad Shochat 
5177e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
5178e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
5179e53505a8SAchiad Shochat 
5180e53505a8SAchiad Shochat 	return ret;
5181e53505a8SAchiad Shochat }
5182e53505a8SAchiad Shochat 
51837738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
51847738613eSIra Weiny 			       struct ib_port_immutable *immutable)
51857738613eSIra Weiny {
51867738613eSIra Weiny 	struct ib_port_attr attr;
5187ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5188ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
5189b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
51907738613eSIra Weiny 	int err;
51917738613eSIra Weiny 
5192c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
51937738613eSIra Weiny 	if (err)
51947738613eSIra Weiny 		return err;
51957738613eSIra Weiny 
5196b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
5197b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
5198b02289b3SArtemy Kovalyov 						   &rep);
5199b02289b3SArtemy Kovalyov 		if (err)
5200b02289b3SArtemy Kovalyov 			return err;
5201b02289b3SArtemy Kovalyov 	}
5202b02289b3SArtemy Kovalyov 
52037738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
52047738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
5205b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
5206337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
52077738613eSIra Weiny 
52087738613eSIra Weiny 	return 0;
52097738613eSIra Weiny }
52107738613eSIra Weiny 
52118e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
52128e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
52138e6efa3aSMark Bloch {
52148e6efa3aSMark Bloch 	struct ib_port_attr attr;
52158e6efa3aSMark Bloch 	int err;
52168e6efa3aSMark Bloch 
52178e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
52188e6efa3aSMark Bloch 
52198e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
52208e6efa3aSMark Bloch 	if (err)
52218e6efa3aSMark Bloch 		return err;
52228e6efa3aSMark Bloch 
52238e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
52248e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
52258e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
52268e6efa3aSMark Bloch 
52278e6efa3aSMark Bloch 	return 0;
52288e6efa3aSMark Bloch }
52298e6efa3aSMark Bloch 
52309abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
5231c7342823SIra Weiny {
5232c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
5233c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
52349abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
52359abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
52369abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
5237c7342823SIra Weiny }
5238c7342823SIra Weiny 
523945f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
52409ef9c640SAviv Heller {
52419ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
52429ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
52439ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
52449ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
52459ef9c640SAviv Heller 	int err;
52469ef9c640SAviv Heller 
52477c34ec19SAviv Heller 	if (!ns || !mlx5_lag_is_roce(mdev))
52489ef9c640SAviv Heller 		return 0;
52499ef9c640SAviv Heller 
52509ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
52519ef9c640SAviv Heller 	if (err)
52529ef9c640SAviv Heller 		return err;
52539ef9c640SAviv Heller 
52549ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
52559ef9c640SAviv Heller 	if (IS_ERR(ft)) {
52569ef9c640SAviv Heller 		err = PTR_ERR(ft);
52579ef9c640SAviv Heller 		goto err_destroy_vport_lag;
52589ef9c640SAviv Heller 	}
52599ef9c640SAviv Heller 
52609a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
52617c34ec19SAviv Heller 	dev->lag_active = true;
52629ef9c640SAviv Heller 	return 0;
52639ef9c640SAviv Heller 
52649ef9c640SAviv Heller err_destroy_vport_lag:
52659ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
52669ef9c640SAviv Heller 	return err;
52679ef9c640SAviv Heller }
52689ef9c640SAviv Heller 
526945f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
52709ef9c640SAviv Heller {
52719ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
52729ef9c640SAviv Heller 
52737c34ec19SAviv Heller 	if (dev->lag_active) {
52747c34ec19SAviv Heller 		dev->lag_active = false;
52757c34ec19SAviv Heller 
52769a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
52779a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
52789ef9c640SAviv Heller 
52799ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
52809ef9c640SAviv Heller 	}
52819ef9c640SAviv Heller }
52829ef9c640SAviv Heller 
52837fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5284fc24fc5eSAchiad Shochat {
5285e53505a8SAchiad Shochat 	int err;
5286e53505a8SAchiad Shochat 
528795579e78SMark Bloch 	dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
528895579e78SMark Bloch 	err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
52895ec8c83eSAviv Heller 	if (err) {
529095579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
5291e53505a8SAchiad Shochat 		return err;
52925ec8c83eSAviv Heller 	}
5293e53505a8SAchiad Shochat 
5294d012f5d6SOr Gerlitz 	return 0;
5295d012f5d6SOr Gerlitz }
5296d012f5d6SOr Gerlitz 
52977fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5298e126ba97SEli Cohen {
529995579e78SMark Bloch 	if (dev->port[port_num].roce.nb.notifier_call) {
530095579e78SMark Bloch 		unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
530195579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
5302e126ba97SEli Cohen 	}
5303e126ba97SEli Cohen }
5304e126ba97SEli Cohen 
5305e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
5306e126ba97SEli Cohen {
5307e126ba97SEli Cohen 	int err;
5308e126ba97SEli Cohen 
5309e53505a8SAchiad Shochat 	err = mlx5_nic_vport_enable_roce(dev->mdev);
5310e53505a8SAchiad Shochat 	if (err)
53118e6efa3aSMark Bloch 		return err;
5312e53505a8SAchiad Shochat 
531345f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
53149ef9c640SAviv Heller 	if (err)
53159ef9c640SAviv Heller 		goto err_disable_roce;
53169ef9c640SAviv Heller 
5317e53505a8SAchiad Shochat 	return 0;
5318e53505a8SAchiad Shochat 
53199ef9c640SAviv Heller err_disable_roce:
53209ef9c640SAviv Heller 	mlx5_nic_vport_disable_roce(dev->mdev);
53219ef9c640SAviv Heller 
5322e53505a8SAchiad Shochat 	return err;
5323fc24fc5eSAchiad Shochat }
5324fc24fc5eSAchiad Shochat 
532545f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
5326fc24fc5eSAchiad Shochat {
532745f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
5328e53505a8SAchiad Shochat 	mlx5_nic_vport_disable_roce(dev->mdev);
5329fc24fc5eSAchiad Shochat }
5330fc24fc5eSAchiad Shochat 
5331e1f24a79SParav Pandit struct mlx5_ib_counter {
53327c16f477SKamal Heib 	const char *name;
53337c16f477SKamal Heib 	size_t offset;
53347c16f477SKamal Heib };
53357c16f477SKamal Heib 
53367c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
53377c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
53387c16f477SKamal Heib 
5339e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
53407c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
53417c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
53427c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
53437c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
53447c16f477SKamal Heib };
53457c16f477SKamal Heib 
5346e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
53477c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
53487c16f477SKamal Heib };
53497c16f477SKamal Heib 
5350e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
53517c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
53527c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
53537c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
53547c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
53557c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
53567c16f477SKamal Heib };
53577c16f477SKamal Heib 
5358e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
5359e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
5360e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
5361e1f24a79SParav Pandit 
5362e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
5363e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
5364e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
5365e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
5366e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
5367e1f24a79SParav Pandit };
5368e1f24a79SParav Pandit 
536958dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
537058dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
537158dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
537258dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
537358dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
537458dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
537558dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
537658dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
537758dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
537858dcb60aSParav Pandit };
537958dcb60aSParav Pandit 
5380d7fab916SAvihai Horon static const struct mlx5_ib_counter roce_accl_cnts[] = {
5381d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_adp_retrans),
5382d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_adp_retrans_to),
5383d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_slow_restart),
5384d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_slow_restart_cnps),
5385d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_slow_restart_trans),
5386d7fab916SAvihai Horon };
5387d7fab916SAvihai Horon 
53889f876f3dSTalat Batheesh #define INIT_EXT_PPCNT_COUNTER(_name)		\
53899f876f3dSTalat Batheesh 	{ .name = #_name, .offset =	\
53909f876f3dSTalat Batheesh 	MLX5_BYTE_OFF(ppcnt_reg, \
53919f876f3dSTalat Batheesh 		      counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
53929f876f3dSTalat Batheesh 
53939f876f3dSTalat Batheesh static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
53949f876f3dSTalat Batheesh 	INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
53959f876f3dSTalat Batheesh };
53969f876f3dSTalat Batheesh 
53973e1f000fSParav Pandit static bool is_mdev_switchdev_mode(const struct mlx5_core_dev *mdev)
53983e1f000fSParav Pandit {
53993e1f000fSParav Pandit 	return MLX5_ESWITCH_MANAGER(mdev) &&
54003e1f000fSParav Pandit 	       mlx5_ib_eswitch_mode(mdev->priv.eswitch) ==
54013e1f000fSParav Pandit 		       MLX5_ESWITCH_OFFLOADS;
54023e1f000fSParav Pandit }
54033e1f000fSParav Pandit 
5404e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
54050837e86aSMark Bloch {
54063e1f000fSParav Pandit 	int num_cnt_ports;
5407aac4492eSDaniel Jurgens 	int i;
54080837e86aSMark Bloch 
54093e1f000fSParav Pandit 	num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports;
54103e1f000fSParav Pandit 
54113e1f000fSParav Pandit 	for (i = 0; i < num_cnt_ports; i++) {
5412921c0f5bSParav Pandit 		if (dev->port[i].cnts.set_id_valid)
54130837e86aSMark Bloch 			mlx5_core_dealloc_q_counter(dev->mdev,
5414e1f24a79SParav Pandit 						    dev->port[i].cnts.set_id);
5415e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
5416e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
54177c16f477SKamal Heib 	}
54187c16f477SKamal Heib }
54197c16f477SKamal Heib 
5420e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5421e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
54227c16f477SKamal Heib {
54237c16f477SKamal Heib 	u32 num_counters;
54247c16f477SKamal Heib 
54257c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
54267c16f477SKamal Heib 
54277c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
54287c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
54297c16f477SKamal Heib 
54307c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
54317c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
543258dcb60aSParav Pandit 
543358dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
543458dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
543558dcb60aSParav Pandit 
5436d7fab916SAvihai Horon 	if (MLX5_CAP_GEN(dev->mdev, roce_accl))
5437d7fab916SAvihai Horon 		num_counters += ARRAY_SIZE(roce_accl_cnts);
5438d7fab916SAvihai Horon 
5439e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
54407c16f477SKamal Heib 
5441e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5442e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5443e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
5444e1f24a79SParav Pandit 	}
54459f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
54469f876f3dSTalat Batheesh 		cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
54479f876f3dSTalat Batheesh 		num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
54489f876f3dSTalat Batheesh 	}
5449e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5450e1f24a79SParav Pandit 	if (!cnts->names)
54517c16f477SKamal Heib 		return -ENOMEM;
54527c16f477SKamal Heib 
5453e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
5454e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
5455e1f24a79SParav Pandit 	if (!cnts->offsets)
54567c16f477SKamal Heib 		goto err_names;
54577c16f477SKamal Heib 
54587c16f477SKamal Heib 	return 0;
54597c16f477SKamal Heib 
54607c16f477SKamal Heib err_names:
5461e1f24a79SParav Pandit 	kfree(cnts->names);
5462aac4492eSDaniel Jurgens 	cnts->names = NULL;
54637c16f477SKamal Heib 	return -ENOMEM;
54647c16f477SKamal Heib }
54657c16f477SKamal Heib 
5466e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
54677c16f477SKamal Heib 				  const char **names,
54687c16f477SKamal Heib 				  size_t *offsets)
54697c16f477SKamal Heib {
54707c16f477SKamal Heib 	int i;
54717c16f477SKamal Heib 	int j = 0;
54727c16f477SKamal Heib 
54737c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
54747c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
54757c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
54767c16f477SKamal Heib 	}
54777c16f477SKamal Heib 
54787c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
54797c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
54807c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
54817c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
54827c16f477SKamal Heib 		}
54837c16f477SKamal Heib 	}
54847c16f477SKamal Heib 
54857c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
54867c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
54877c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
54887c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
54897c16f477SKamal Heib 		}
54907c16f477SKamal Heib 	}
5491e1f24a79SParav Pandit 
549258dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
549358dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
549458dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
549558dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
549658dcb60aSParav Pandit 		}
549758dcb60aSParav Pandit 	}
549858dcb60aSParav Pandit 
5499d7fab916SAvihai Horon 	if (MLX5_CAP_GEN(dev->mdev, roce_accl)) {
5500d7fab916SAvihai Horon 		for (i = 0; i < ARRAY_SIZE(roce_accl_cnts); i++, j++) {
5501d7fab916SAvihai Horon 			names[j] = roce_accl_cnts[i].name;
5502d7fab916SAvihai Horon 			offsets[j] = roce_accl_cnts[i].offset;
5503d7fab916SAvihai Horon 		}
5504d7fab916SAvihai Horon 	}
5505d7fab916SAvihai Horon 
5506e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5507e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5508e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
5509e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
5510e1f24a79SParav Pandit 		}
5511e1f24a79SParav Pandit 	}
55129f876f3dSTalat Batheesh 
55139f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
55149f876f3dSTalat Batheesh 		for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
55159f876f3dSTalat Batheesh 			names[j] = ext_ppcnt_cnts[i].name;
55169f876f3dSTalat Batheesh 			offsets[j] = ext_ppcnt_cnts[i].offset;
55179f876f3dSTalat Batheesh 		}
55189f876f3dSTalat Batheesh 	}
55190837e86aSMark Bloch }
55200837e86aSMark Bloch 
5521e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
55220837e86aSMark Bloch {
55233e1f000fSParav Pandit 	int num_cnt_ports;
5524aac4492eSDaniel Jurgens 	int err = 0;
55250837e86aSMark Bloch 	int i;
5526aa74be6eSYishai Hadas 	bool is_shared;
5527aa74be6eSYishai Hadas 
5528aa74be6eSYishai Hadas 	is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
55293e1f000fSParav Pandit 	num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports;
55300837e86aSMark Bloch 
55313e1f000fSParav Pandit 	for (i = 0; i < num_cnt_ports; i++) {
5532aac4492eSDaniel Jurgens 		err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5533aac4492eSDaniel Jurgens 		if (err)
5534aac4492eSDaniel Jurgens 			goto err_alloc;
55357c16f477SKamal Heib 
5536aac4492eSDaniel Jurgens 		mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5537aac4492eSDaniel Jurgens 				      dev->port[i].cnts.offsets);
5538aac4492eSDaniel Jurgens 
5539aa74be6eSYishai Hadas 		err = mlx5_cmd_alloc_q_counter(dev->mdev,
5540aa74be6eSYishai Hadas 					       &dev->port[i].cnts.set_id,
5541aa74be6eSYishai Hadas 					       is_shared ?
5542aa74be6eSYishai Hadas 					       MLX5_SHARED_RESOURCE_UID : 0);
5543aac4492eSDaniel Jurgens 		if (err) {
55440837e86aSMark Bloch 			mlx5_ib_warn(dev,
55450837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
5546aac4492eSDaniel Jurgens 				     i + 1, err);
5547aac4492eSDaniel Jurgens 			goto err_alloc;
55480837e86aSMark Bloch 		}
5549aac4492eSDaniel Jurgens 		dev->port[i].cnts.set_id_valid = true;
55500837e86aSMark Bloch 	}
55510837e86aSMark Bloch 	return 0;
55520837e86aSMark Bloch 
5553aac4492eSDaniel Jurgens err_alloc:
5554aac4492eSDaniel Jurgens 	mlx5_ib_dealloc_counters(dev);
5555aac4492eSDaniel Jurgens 	return err;
55560837e86aSMark Bloch }
55570837e86aSMark Bloch 
55583e1f000fSParav Pandit static const struct mlx5_ib_counters *get_counters(struct mlx5_ib_dev *dev,
55593e1f000fSParav Pandit 						   u8 port_num)
55603e1f000fSParav Pandit {
55613e1f000fSParav Pandit 	return is_mdev_switchdev_mode(dev->mdev) ? &dev->port[0].cnts :
55623e1f000fSParav Pandit 						   &dev->port[port_num].cnts;
55633e1f000fSParav Pandit }
55643e1f000fSParav Pandit 
55653e1f000fSParav Pandit /**
55663e1f000fSParav Pandit  * mlx5_ib_get_counters_id - Returns counters id to use for device+port
55673e1f000fSParav Pandit  * @dev:	Pointer to mlx5 IB device
55683e1f000fSParav Pandit  * @port_num:	Zero based port number
55693e1f000fSParav Pandit  *
55703e1f000fSParav Pandit  * mlx5_ib_get_counters_id() Returns counters set id to use for given
55713e1f000fSParav Pandit  * device port combination in switchdev and non switchdev mode of the
55723e1f000fSParav Pandit  * parent device.
55733e1f000fSParav Pandit  */
55743e1f000fSParav Pandit u16 mlx5_ib_get_counters_id(struct mlx5_ib_dev *dev, u8 port_num)
55753e1f000fSParav Pandit {
55763e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts = get_counters(dev, port_num);
55773e1f000fSParav Pandit 
55783e1f000fSParav Pandit 	return cnts->set_id;
55793e1f000fSParav Pandit }
55803e1f000fSParav Pandit 
55810ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
55820ad17a8fSMark Bloch 						    u8 port_num)
55830ad17a8fSMark Bloch {
55847c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
55853e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts;
55863e1f000fSParav Pandit 	bool is_switchdev = is_mdev_switchdev_mode(dev->mdev);
55870ad17a8fSMark Bloch 
55883e1f000fSParav Pandit 	if ((is_switchdev && port_num) || (!is_switchdev && !port_num))
55890ad17a8fSMark Bloch 		return NULL;
55900ad17a8fSMark Bloch 
55913e1f000fSParav Pandit 	cnts = get_counters(dev, port_num - 1);
55923e1f000fSParav Pandit 
55935dcecbc9SParav Pandit 	return rdma_alloc_hw_stats_struct(cnts->names,
55945dcecbc9SParav Pandit 					  cnts->num_q_counters +
55955dcecbc9SParav Pandit 					  cnts->num_cong_counters +
55965dcecbc9SParav Pandit 					  cnts->num_ext_ppcnt_counters,
55970ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
55980ad17a8fSMark Bloch }
55990ad17a8fSMark Bloch 
5600aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
56015dcecbc9SParav Pandit 				    const struct mlx5_ib_counters *cnts,
5602318d535cSMark Zhang 				    struct rdma_hw_stats *stats,
5603318d535cSMark Zhang 				    u16 set_id)
5604e1f24a79SParav Pandit {
5605e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5606e1f24a79SParav Pandit 	void *out;
5607e1f24a79SParav Pandit 	__be32 val;
5608e1f24a79SParav Pandit 	int ret, i;
5609e1f24a79SParav Pandit 
56101b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
5611e1f24a79SParav Pandit 	if (!out)
5612e1f24a79SParav Pandit 		return -ENOMEM;
5613e1f24a79SParav Pandit 
5614318d535cSMark Zhang 	ret = mlx5_core_query_q_counter(mdev, set_id, 0, out, outlen);
5615e1f24a79SParav Pandit 	if (ret)
5616e1f24a79SParav Pandit 		goto free;
5617e1f24a79SParav Pandit 
56185dcecbc9SParav Pandit 	for (i = 0; i < cnts->num_q_counters; i++) {
56195dcecbc9SParav Pandit 		val = *(__be32 *)(out + cnts->offsets[i]);
5620e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
5621e1f24a79SParav Pandit 	}
5622e1f24a79SParav Pandit 
5623e1f24a79SParav Pandit free:
5624e1f24a79SParav Pandit 	kvfree(out);
5625e1f24a79SParav Pandit 	return ret;
5626e1f24a79SParav Pandit }
5627e1f24a79SParav Pandit 
56289f876f3dSTalat Batheesh static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
56295dcecbc9SParav Pandit 					    const struct mlx5_ib_counters *cnts,
56309f876f3dSTalat Batheesh 					    struct rdma_hw_stats *stats)
56319f876f3dSTalat Batheesh {
56325dcecbc9SParav Pandit 	int offset = cnts->num_q_counters + cnts->num_cong_counters;
56339f876f3dSTalat Batheesh 	int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
56349f876f3dSTalat Batheesh 	int ret, i;
56359f876f3dSTalat Batheesh 	void *out;
56369f876f3dSTalat Batheesh 
56379f876f3dSTalat Batheesh 	out = kvzalloc(sz, GFP_KERNEL);
56389f876f3dSTalat Batheesh 	if (!out)
56399f876f3dSTalat Batheesh 		return -ENOMEM;
56409f876f3dSTalat Batheesh 
56419f876f3dSTalat Batheesh 	ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
56429f876f3dSTalat Batheesh 	if (ret)
56439f876f3dSTalat Batheesh 		goto free;
56449f876f3dSTalat Batheesh 
56455dcecbc9SParav Pandit 	for (i = 0; i < cnts->num_ext_ppcnt_counters; i++)
56469f876f3dSTalat Batheesh 		stats->value[i + offset] =
56479f876f3dSTalat Batheesh 			be64_to_cpup((__be64 *)(out +
56485dcecbc9SParav Pandit 				    cnts->offsets[i + offset]));
56499f876f3dSTalat Batheesh free:
56509f876f3dSTalat Batheesh 	kvfree(out);
56519f876f3dSTalat Batheesh 	return ret;
56529f876f3dSTalat Batheesh }
56539f876f3dSTalat Batheesh 
56540ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
56550ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
56567c16f477SKamal Heib 				u8 port_num, int index)
56570ad17a8fSMark Bloch {
56580ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
56593e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts = get_counters(dev, port_num - 1);
5660aac4492eSDaniel Jurgens 	struct mlx5_core_dev *mdev;
5661e1f24a79SParav Pandit 	int ret, num_counters;
5662aac4492eSDaniel Jurgens 	u8 mdev_port_num;
56630ad17a8fSMark Bloch 
56647c16f477SKamal Heib 	if (!stats)
5665e1f24a79SParav Pandit 		return -EINVAL;
56660ad17a8fSMark Bloch 
56675dcecbc9SParav Pandit 	num_counters = cnts->num_q_counters +
56685dcecbc9SParav Pandit 		       cnts->num_cong_counters +
56695dcecbc9SParav Pandit 		       cnts->num_ext_ppcnt_counters;
5670aac4492eSDaniel Jurgens 
5671aac4492eSDaniel Jurgens 	/* q_counters are per IB device, query the master mdev */
56725dcecbc9SParav Pandit 	ret = mlx5_ib_query_q_counters(dev->mdev, cnts, stats, cnts->set_id);
56730ad17a8fSMark Bloch 	if (ret)
5674e1f24a79SParav Pandit 		return ret;
56750ad17a8fSMark Bloch 
56769f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
56775dcecbc9SParav Pandit 		ret =  mlx5_ib_query_ext_ppcnt_counters(dev, cnts, stats);
56789f876f3dSTalat Batheesh 		if (ret)
56799f876f3dSTalat Batheesh 			return ret;
56809f876f3dSTalat Batheesh 	}
56819f876f3dSTalat Batheesh 
5682e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5683aac4492eSDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5684aac4492eSDaniel Jurgens 						    &mdev_port_num);
5685aac4492eSDaniel Jurgens 		if (!mdev) {
5686aac4492eSDaniel Jurgens 			/* If port is not affiliated yet, its in down state
5687aac4492eSDaniel Jurgens 			 * which doesn't have any counters yet, so it would be
5688aac4492eSDaniel Jurgens 			 * zero. So no need to read from the HCA.
5689aac4492eSDaniel Jurgens 			 */
5690aac4492eSDaniel Jurgens 			goto done;
5691aac4492eSDaniel Jurgens 		}
569271a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
569371a0ff65SMajd Dibbiny 						   stats->value +
56945dcecbc9SParav Pandit 						   cnts->num_q_counters,
56955dcecbc9SParav Pandit 						   cnts->num_cong_counters,
56965dcecbc9SParav Pandit 						   cnts->offsets +
56975dcecbc9SParav Pandit 						   cnts->num_q_counters);
5698aac4492eSDaniel Jurgens 
5699aac4492eSDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
5700e1f24a79SParav Pandit 		if (ret)
5701e1f24a79SParav Pandit 			return ret;
57020ad17a8fSMark Bloch 	}
57037c16f477SKamal Heib 
5704aac4492eSDaniel Jurgens done:
5705e1f24a79SParav Pandit 	return num_counters;
57060ad17a8fSMark Bloch }
57070ad17a8fSMark Bloch 
570818d422ceSMark Zhang static struct rdma_hw_stats *
570918d422ceSMark Zhang mlx5_ib_counter_alloc_stats(struct rdma_counter *counter)
571018d422ceSMark Zhang {
571118d422ceSMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
57125dcecbc9SParav Pandit 	const struct mlx5_ib_counters *cnts =
57133e1f000fSParav Pandit 		get_counters(dev, counter->port - 1);
571418d422ceSMark Zhang 
571518d422ceSMark Zhang 	/* Q counters are in the beginning of all counters */
57165dcecbc9SParav Pandit 	return rdma_alloc_hw_stats_struct(cnts->names,
57175dcecbc9SParav Pandit 					  cnts->num_q_counters,
571818d422ceSMark Zhang 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
571918d422ceSMark Zhang }
572018d422ceSMark Zhang 
572118d422ceSMark Zhang static int mlx5_ib_counter_update_stats(struct rdma_counter *counter)
572218d422ceSMark Zhang {
572318d422ceSMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
57243e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts =
57253e1f000fSParav Pandit 		get_counters(dev, counter->port - 1);
572618d422ceSMark Zhang 
57275dcecbc9SParav Pandit 	return mlx5_ib_query_q_counters(dev->mdev, cnts,
572818d422ceSMark Zhang 					counter->stats, counter->id);
572918d422ceSMark Zhang }
573018d422ceSMark Zhang 
573145842fc6SMark Zhang static int mlx5_ib_counter_bind_qp(struct rdma_counter *counter,
573245842fc6SMark Zhang 				   struct ib_qp *qp)
573345842fc6SMark Zhang {
573445842fc6SMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
573545842fc6SMark Zhang 	u16 cnt_set_id = 0;
573645842fc6SMark Zhang 	int err;
573745842fc6SMark Zhang 
573845842fc6SMark Zhang 	if (!counter->id) {
573945842fc6SMark Zhang 		err = mlx5_cmd_alloc_q_counter(dev->mdev,
574045842fc6SMark Zhang 					       &cnt_set_id,
574145842fc6SMark Zhang 					       MLX5_SHARED_RESOURCE_UID);
574245842fc6SMark Zhang 		if (err)
574345842fc6SMark Zhang 			return err;
574445842fc6SMark Zhang 		counter->id = cnt_set_id;
574545842fc6SMark Zhang 	}
574645842fc6SMark Zhang 
574745842fc6SMark Zhang 	err = mlx5_ib_qp_set_counter(qp, counter);
574845842fc6SMark Zhang 	if (err)
574945842fc6SMark Zhang 		goto fail_set_counter;
575045842fc6SMark Zhang 
575145842fc6SMark Zhang 	return 0;
575245842fc6SMark Zhang 
575345842fc6SMark Zhang fail_set_counter:
575445842fc6SMark Zhang 	mlx5_core_dealloc_q_counter(dev->mdev, cnt_set_id);
575545842fc6SMark Zhang 	counter->id = 0;
575645842fc6SMark Zhang 
575745842fc6SMark Zhang 	return err;
575845842fc6SMark Zhang }
575945842fc6SMark Zhang 
576045842fc6SMark Zhang static int mlx5_ib_counter_unbind_qp(struct ib_qp *qp)
576145842fc6SMark Zhang {
576245842fc6SMark Zhang 	return mlx5_ib_qp_set_counter(qp, NULL);
576345842fc6SMark Zhang }
576445842fc6SMark Zhang 
576545842fc6SMark Zhang static int mlx5_ib_counter_dealloc(struct rdma_counter *counter)
576645842fc6SMark Zhang {
576745842fc6SMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
576845842fc6SMark Zhang 
576945842fc6SMark Zhang 	return mlx5_core_dealloc_q_counter(dev->mdev, counter->id);
577045842fc6SMark Zhang }
577145842fc6SMark Zhang 
5772f6a8a19bSDenis Drozdov static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5773693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
5774f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
5775693dfd5aSErez Shitrit {
5776693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
5777f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
5778693dfd5aSErez Shitrit 
5779f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
5780693dfd5aSErez Shitrit }
5781693dfd5aSErez Shitrit 
5782fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5783fe248c3aSMaor Gottlieb {
578409b0965eSGreg Kroah-Hartman 	if (!dev->delay_drop.dir_debugfs)
5785fe248c3aSMaor Gottlieb 		return;
578609b0965eSGreg Kroah-Hartman 	debugfs_remove_recursive(dev->delay_drop.dir_debugfs);
578709b0965eSGreg Kroah-Hartman 	dev->delay_drop.dir_debugfs = NULL;
5788fe248c3aSMaor Gottlieb }
5789fe248c3aSMaor Gottlieb 
579003404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
579103404e8aSMaor Gottlieb {
579203404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
579303404e8aSMaor Gottlieb 		return;
579403404e8aSMaor Gottlieb 
579503404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
5796fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5797fe248c3aSMaor Gottlieb }
5798fe248c3aSMaor Gottlieb 
5799fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5800fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
5801fe248c3aSMaor Gottlieb {
5802fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5803fe248c3aSMaor Gottlieb 	char lbuf[20];
5804fe248c3aSMaor Gottlieb 	int len;
5805fe248c3aSMaor Gottlieb 
5806fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5807fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
5808fe248c3aSMaor Gottlieb }
5809fe248c3aSMaor Gottlieb 
5810fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5811fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
5812fe248c3aSMaor Gottlieb {
5813fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5814fe248c3aSMaor Gottlieb 	u32 timeout;
5815fe248c3aSMaor Gottlieb 	u32 var;
5816fe248c3aSMaor Gottlieb 
5817fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
5818fe248c3aSMaor Gottlieb 		return -EFAULT;
5819fe248c3aSMaor Gottlieb 
5820fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5821fe248c3aSMaor Gottlieb 			1000);
5822fe248c3aSMaor Gottlieb 	if (timeout != var)
5823fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5824fe248c3aSMaor Gottlieb 			    timeout);
5825fe248c3aSMaor Gottlieb 
5826fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
5827fe248c3aSMaor Gottlieb 
5828fe248c3aSMaor Gottlieb 	return count;
5829fe248c3aSMaor Gottlieb }
5830fe248c3aSMaor Gottlieb 
5831fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
5832fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
5833fe248c3aSMaor Gottlieb 	.open	= simple_open,
5834fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
5835fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
5836fe248c3aSMaor Gottlieb };
5837fe248c3aSMaor Gottlieb 
583809b0965eSGreg Kroah-Hartman static void delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5839fe248c3aSMaor Gottlieb {
584009b0965eSGreg Kroah-Hartman 	struct dentry *root;
5841fe248c3aSMaor Gottlieb 
5842fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
584309b0965eSGreg Kroah-Hartman 		return;
5844fe248c3aSMaor Gottlieb 
584509b0965eSGreg Kroah-Hartman 	root = debugfs_create_dir("delay_drop", dev->mdev->priv.dbg_root);
584609b0965eSGreg Kroah-Hartman 	dev->delay_drop.dir_debugfs = root;
5847fe248c3aSMaor Gottlieb 
584809b0965eSGreg Kroah-Hartman 	debugfs_create_atomic_t("num_timeout_events", 0400, root,
5849fe248c3aSMaor Gottlieb 				&dev->delay_drop.events_cnt);
585009b0965eSGreg Kroah-Hartman 	debugfs_create_atomic_t("num_rqs", 0400, root,
5851fe248c3aSMaor Gottlieb 				&dev->delay_drop.rqs_cnt);
585209b0965eSGreg Kroah-Hartman 	debugfs_create_file("timeout", 0600, root, &dev->delay_drop,
5853fe248c3aSMaor Gottlieb 			    &fops_delay_drop_timeout);
585403404e8aSMaor Gottlieb }
585503404e8aSMaor Gottlieb 
585603404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
585703404e8aSMaor Gottlieb {
585803404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
585903404e8aSMaor Gottlieb 		return;
586003404e8aSMaor Gottlieb 
586103404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
586203404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
586303404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
586403404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
586503404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
5866fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
5867fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
5868fe248c3aSMaor Gottlieb 
586909b0965eSGreg Kroah-Hartman 	delay_drop_debugfs_init(dev);
587003404e8aSMaor Gottlieb }
587103404e8aSMaor Gottlieb 
587232f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
587332f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
587432f69e4bSDaniel Jurgens {
587532f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
587632f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
587732f69e4bSDaniel Jurgens 	int comps;
587832f69e4bSDaniel Jurgens 	int err;
587932f69e4bSDaniel Jurgens 	int i;
588032f69e4bSDaniel Jurgens 
58819dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
58829dc4cfffSLeon Romanovsky 
5883a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5884a9e546e7SParav Pandit 
588532f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
588632f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
588732f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
588832f69e4bSDaniel Jurgens 		return;
588932f69e4bSDaniel Jurgens 	}
5890df097a27SSaeed Mahameed 
589132f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
589232f69e4bSDaniel Jurgens 
589332f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
589423eaf3b5SLeon Romanovsky 	if (mpi->mdev_events.notifier_call)
589523eaf3b5SLeon Romanovsky 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
589623eaf3b5SLeon Romanovsky 	mpi->mdev_events.notifier_call = NULL;
589732f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
589832f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
589932f69e4bSDaniel Jurgens 
590032f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
590132f69e4bSDaniel Jurgens 	if (comps) {
590232f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
590332f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
590432f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
590532f69e4bSDaniel Jurgens 
590632f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
590732f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
590832f69e4bSDaniel Jurgens 
590932f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
591032f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
591132f69e4bSDaniel Jurgens 	}
591232f69e4bSDaniel Jurgens 
591332f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
591432f69e4bSDaniel Jurgens 
591532f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
591632f69e4bSDaniel Jurgens 
591732f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
591832f69e4bSDaniel Jurgens 
591932f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
592032f69e4bSDaniel Jurgens 
592132f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
592232f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
592332f69e4bSDaniel Jurgens 	 * it back to the list.
592432f69e4bSDaniel Jurgens 	 */
592532f69e4bSDaniel Jurgens 	if (err)
592632f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
592732f69e4bSDaniel Jurgens 			    port_num + 1);
592832f69e4bSDaniel Jurgens 
592995579e78SMark Bloch 	ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
593032f69e4bSDaniel Jurgens }
593132f69e4bSDaniel Jurgens 
593232f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
593332f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
593432f69e4bSDaniel Jurgens {
593532f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
593632f69e4bSDaniel Jurgens 	int err;
593732f69e4bSDaniel Jurgens 
59389dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
59399dc4cfffSLeon Romanovsky 
594032f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
594132f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
59422577188eSQing Huang 		mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
594332f69e4bSDaniel Jurgens 			    port_num + 1);
594432f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
594532f69e4bSDaniel Jurgens 		return false;
594632f69e4bSDaniel Jurgens 	}
594732f69e4bSDaniel Jurgens 
594832f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
594932f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
5950df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
595132f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
595232f69e4bSDaniel Jurgens 
595332f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
595432f69e4bSDaniel Jurgens 	if (err)
595532f69e4bSDaniel Jurgens 		goto unbind;
595632f69e4bSDaniel Jurgens 
595732f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
595832f69e4bSDaniel Jurgens 	if (err)
595932f69e4bSDaniel Jurgens 		goto unbind;
596032f69e4bSDaniel Jurgens 
596132f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
596232f69e4bSDaniel Jurgens 	if (err) {
596332f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
596432f69e4bSDaniel Jurgens 			    port_num + 1);
596532f69e4bSDaniel Jurgens 		goto unbind;
596632f69e4bSDaniel Jurgens 	}
596732f69e4bSDaniel Jurgens 
5968df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5969df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5970df097a27SSaeed Mahameed 
597173eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
5972a9e546e7SParav Pandit 
597332f69e4bSDaniel Jurgens 	return true;
597432f69e4bSDaniel Jurgens 
597532f69e4bSDaniel Jurgens unbind:
597632f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
597732f69e4bSDaniel Jurgens 	return false;
597832f69e4bSDaniel Jurgens }
597932f69e4bSDaniel Jurgens 
598032f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
598132f69e4bSDaniel Jurgens {
598232f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
598332f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
598432f69e4bSDaniel Jurgens 							  port_num + 1);
598532f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
598632f69e4bSDaniel Jurgens 	int err;
598732f69e4bSDaniel Jurgens 	int i;
598832f69e4bSDaniel Jurgens 
598932f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
599032f69e4bSDaniel Jurgens 		return 0;
599132f69e4bSDaniel Jurgens 
599232f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
599332f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
599432f69e4bSDaniel Jurgens 	if (err)
599532f69e4bSDaniel Jurgens 		return err;
599632f69e4bSDaniel Jurgens 
599732f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
599832f69e4bSDaniel Jurgens 	if (err)
599932f69e4bSDaniel Jurgens 		return err;
600032f69e4bSDaniel Jurgens 
600132f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
600232f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
600332f69e4bSDaniel Jurgens 		bool bound = false;
600432f69e4bSDaniel Jurgens 
600532f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
600632f69e4bSDaniel Jurgens 		if (i == port_num) {
600732f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
600832f69e4bSDaniel Jurgens 			if (!mpi) {
600932f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
601032f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
601132f69e4bSDaniel Jurgens 				return -ENOMEM;
601232f69e4bSDaniel Jurgens 			}
601332f69e4bSDaniel Jurgens 
601432f69e4bSDaniel Jurgens 			mpi->is_master = true;
601532f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
601632f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
601732f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
601832f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
601932f69e4bSDaniel Jurgens 			mpi = NULL;
602032f69e4bSDaniel Jurgens 			continue;
602132f69e4bSDaniel Jurgens 		}
602232f69e4bSDaniel Jurgens 
602332f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
602432f69e4bSDaniel Jurgens 				    list) {
602532f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
602632f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
602732f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
602832f69e4bSDaniel Jurgens 			}
602932f69e4bSDaniel Jurgens 
603032f69e4bSDaniel Jurgens 			if (bound) {
6031c42260f1SVu Pham 				dev_dbg(mpi->mdev->device,
6032c42260f1SVu Pham 					"removing port from unaffiliated list.\n");
603332f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
603432f69e4bSDaniel Jurgens 				list_del(&mpi->list);
603532f69e4bSDaniel Jurgens 				break;
603632f69e4bSDaniel Jurgens 			}
603732f69e4bSDaniel Jurgens 		}
603832f69e4bSDaniel Jurgens 		if (!bound) {
603932f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
604032f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
604132f69e4bSDaniel Jurgens 				    i + 1);
604232f69e4bSDaniel Jurgens 		}
604332f69e4bSDaniel Jurgens 	}
604432f69e4bSDaniel Jurgens 
604532f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
604632f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
604732f69e4bSDaniel Jurgens 	return err;
604832f69e4bSDaniel Jurgens }
604932f69e4bSDaniel Jurgens 
605032f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
605132f69e4bSDaniel Jurgens {
605232f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
605332f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
605432f69e4bSDaniel Jurgens 							  port_num + 1);
605532f69e4bSDaniel Jurgens 	int i;
605632f69e4bSDaniel Jurgens 
605732f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
605832f69e4bSDaniel Jurgens 		return;
605932f69e4bSDaniel Jurgens 
606032f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
606132f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
606232f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
606332f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
606432f69e4bSDaniel Jurgens 			if (i == port_num) {
606532f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
606632f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
606732f69e4bSDaniel Jurgens 			} else {
606832f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
606932f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
607032f69e4bSDaniel Jurgens 			}
607132f69e4bSDaniel Jurgens 		}
607232f69e4bSDaniel Jurgens 	}
607332f69e4bSDaniel Jurgens 
607432f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
607532f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
607632f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
607732f69e4bSDaniel Jurgens 
607832f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
607932f69e4bSDaniel Jurgens }
608032f69e4bSDaniel Jurgens 
60817be76befSYishai Hadas static int var_obj_cleanup(struct ib_uobject *uobject,
60827be76befSYishai Hadas 			   enum rdma_remove_reason why,
60837be76befSYishai Hadas 			   struct uverbs_attr_bundle *attrs)
60847be76befSYishai Hadas {
60857be76befSYishai Hadas 	struct mlx5_user_mmap_entry *obj = uobject->object;
60867be76befSYishai Hadas 
60877be76befSYishai Hadas 	rdma_user_mmap_entry_remove(&obj->rdma_entry);
60887be76befSYishai Hadas 	return 0;
60897be76befSYishai Hadas }
60907be76befSYishai Hadas 
60917be76befSYishai Hadas static struct mlx5_user_mmap_entry *
60927be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c)
60937be76befSYishai Hadas {
60947be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
60957be76befSYishai Hadas 	struct mlx5_var_table *var_table;
60967be76befSYishai Hadas 	u32 page_idx;
60977be76befSYishai Hadas 	int err;
60987be76befSYishai Hadas 
60997be76befSYishai Hadas 	var_table = &to_mdev(c->ibucontext.device)->var_table;
61007be76befSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
61017be76befSYishai Hadas 	if (!entry)
61027be76befSYishai Hadas 		return ERR_PTR(-ENOMEM);
61037be76befSYishai Hadas 
61047be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
61057be76befSYishai Hadas 	page_idx = find_first_zero_bit(var_table->bitmap,
61067be76befSYishai Hadas 				       var_table->num_var_hw_entries);
61077be76befSYishai Hadas 	if (page_idx >= var_table->num_var_hw_entries) {
61087be76befSYishai Hadas 		err = -ENOSPC;
61097be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
61107be76befSYishai Hadas 		goto end;
61117be76befSYishai Hadas 	}
61127be76befSYishai Hadas 
61137be76befSYishai Hadas 	set_bit(page_idx, var_table->bitmap);
61147be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
61157be76befSYishai Hadas 
61167be76befSYishai Hadas 	entry->address = var_table->hw_start_addr +
61177be76befSYishai Hadas 				(page_idx * var_table->stride_size);
61187be76befSYishai Hadas 	entry->page_idx = page_idx;
61197be76befSYishai Hadas 	entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR;
61207be76befSYishai Hadas 
61217be76befSYishai Hadas 	err = rdma_user_mmap_entry_insert_range(
61227be76befSYishai Hadas 		&c->ibucontext, &entry->rdma_entry, var_table->stride_size,
61237be76befSYishai Hadas 		MLX5_IB_MMAP_OFFSET_START << 16,
61247be76befSYishai Hadas 		(MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1);
61257be76befSYishai Hadas 	if (err)
61267be76befSYishai Hadas 		goto err_insert;
61277be76befSYishai Hadas 
61287be76befSYishai Hadas 	return entry;
61297be76befSYishai Hadas 
61307be76befSYishai Hadas err_insert:
61317be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
61327be76befSYishai Hadas 	clear_bit(page_idx, var_table->bitmap);
61337be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
61347be76befSYishai Hadas end:
61357be76befSYishai Hadas 	kfree(entry);
61367be76befSYishai Hadas 	return ERR_PTR(err);
61377be76befSYishai Hadas }
61387be76befSYishai Hadas 
61397be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)(
61407be76befSYishai Hadas 	struct uverbs_attr_bundle *attrs)
61417be76befSYishai Hadas {
61427be76befSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
61437be76befSYishai Hadas 		attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
61447be76befSYishai Hadas 	struct mlx5_ib_ucontext *c;
61457be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
61467be76befSYishai Hadas 	u64 mmap_offset;
61477be76befSYishai Hadas 	u32 length;
61487be76befSYishai Hadas 	int err;
61497be76befSYishai Hadas 
61507be76befSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
61517be76befSYishai Hadas 	if (IS_ERR(c))
61527be76befSYishai Hadas 		return PTR_ERR(c);
61537be76befSYishai Hadas 
61547be76befSYishai Hadas 	entry = alloc_var_entry(c);
61557be76befSYishai Hadas 	if (IS_ERR(entry))
61567be76befSYishai Hadas 		return PTR_ERR(entry);
61577be76befSYishai Hadas 
61587be76befSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
61597be76befSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
61607be76befSYishai Hadas 	uobj->object = entry;
61617be76befSYishai Hadas 
61627be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
61637be76befSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
61647be76befSYishai Hadas 	if (err)
61657be76befSYishai Hadas 		goto err;
61667be76befSYishai Hadas 
61677be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
61687be76befSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
61697be76befSYishai Hadas 	if (err)
61707be76befSYishai Hadas 		goto err;
61717be76befSYishai Hadas 
61727be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
61737be76befSYishai Hadas 			     &length, sizeof(length));
61747be76befSYishai Hadas 	if (err)
61757be76befSYishai Hadas 		goto err;
61767be76befSYishai Hadas 
61777be76befSYishai Hadas 	return 0;
61787be76befSYishai Hadas 
61797be76befSYishai Hadas err:
61807be76befSYishai Hadas 	rdma_user_mmap_entry_remove(&entry->rdma_entry);
61817be76befSYishai Hadas 	return err;
61827be76befSYishai Hadas }
61837be76befSYishai Hadas 
61847be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
61857be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_ALLOC,
61867be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE,
61877be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
61887be76befSYishai Hadas 			UVERBS_ACCESS_NEW,
61897be76befSYishai Hadas 			UA_MANDATORY),
61907be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
61917be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
61927be76befSYishai Hadas 			   UA_MANDATORY),
61937be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
61947be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
61957be76befSYishai Hadas 			   UA_MANDATORY),
61967be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
61977be76befSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
61987be76befSYishai Hadas 			    UA_MANDATORY));
61997be76befSYishai Hadas 
62007be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
62017be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_DESTROY,
62027be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE,
62037be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
62047be76befSYishai Hadas 			UVERBS_ACCESS_DESTROY,
62057be76befSYishai Hadas 			UA_MANDATORY));
62067be76befSYishai Hadas 
62077be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR,
62087be76befSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(var_obj_cleanup),
62097be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC),
62107be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY));
62117be76befSYishai Hadas 
62127be76befSYishai Hadas static bool var_is_supported(struct ib_device *device)
62137be76befSYishai Hadas {
62147be76befSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(device);
62157be76befSYishai Hadas 
62167be76befSYishai Hadas 	return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
62177be76befSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q);
62187be76befSYishai Hadas }
62197be76befSYishai Hadas 
62209a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
62219a119cd5SJason Gunthorpe 	mlx5_ib_dm,
62229a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
622324da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
62249a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
622524da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
622683bb4442SJason Gunthorpe 			    UA_MANDATORY),
62279a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
622824da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
62293b113a1eSAriel Levkovich 			    UA_OPTIONAL),
62303b113a1eSAriel Levkovich 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
62313b113a1eSAriel Levkovich 			     enum mlx5_ib_uapi_dm_type,
62323b113a1eSAriel Levkovich 			     UA_OPTIONAL));
623324da0016SAriel Levkovich 
62349a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
62359a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
62369a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
6237c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
6238bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
6239bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
6240c6475a0bSAviad Yehezkel 
62410cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
624236e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
62430cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
624430f2fe40SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_qos_defs),
62458c84660bSMatan Barak 
62460cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
62470cbf432dSJason Gunthorpe 				&mlx5_ib_flow_action),
62480cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
62497be76befSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR,
62507be76befSYishai Hadas 				UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)),
62510cbf432dSJason Gunthorpe 	{}
62520cbf432dSJason Gunthorpe };
62538c84660bSMatan Barak 
62541a1e03dcSRaed Salem static int mlx5_ib_read_counters(struct ib_counters *counters,
62551a1e03dcSRaed Salem 				 struct ib_counters_read_attr *read_attr,
62561a1e03dcSRaed Salem 				 struct uverbs_attr_bundle *attrs)
62571a1e03dcSRaed Salem {
62581a1e03dcSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
62591a1e03dcSRaed Salem 	struct mlx5_read_counters_attr mread_attr = {};
62601a1e03dcSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc;
62611a1e03dcSRaed Salem 	int ret, i;
62621a1e03dcSRaed Salem 
62631a1e03dcSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
62641a1e03dcSRaed Salem 	if (mcounters->cntrs_max_index > read_attr->ncounters) {
62651a1e03dcSRaed Salem 		ret = -EINVAL;
62661a1e03dcSRaed Salem 		goto err_bound;
62671a1e03dcSRaed Salem 	}
62681a1e03dcSRaed Salem 
62691a1e03dcSRaed Salem 	mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
62701a1e03dcSRaed Salem 				 GFP_KERNEL);
62711a1e03dcSRaed Salem 	if (!mread_attr.out) {
62721a1e03dcSRaed Salem 		ret = -ENOMEM;
62731a1e03dcSRaed Salem 		goto err_bound;
62741a1e03dcSRaed Salem 	}
62751a1e03dcSRaed Salem 
62761a1e03dcSRaed Salem 	mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
62771a1e03dcSRaed Salem 	mread_attr.flags = read_attr->flags;
62781a1e03dcSRaed Salem 	ret = mcounters->read_counters(counters->device, &mread_attr);
62791a1e03dcSRaed Salem 	if (ret)
62801a1e03dcSRaed Salem 		goto err_read;
62811a1e03dcSRaed Salem 
62821a1e03dcSRaed Salem 	/* do the pass over the counters data array to assign according to the
62831a1e03dcSRaed Salem 	 * descriptions and indexing pairs
62841a1e03dcSRaed Salem 	 */
62851a1e03dcSRaed Salem 	desc = mcounters->counters_data;
62861a1e03dcSRaed Salem 	for (i = 0; i < mcounters->ncounters; i++)
62871a1e03dcSRaed Salem 		read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
62881a1e03dcSRaed Salem 
62891a1e03dcSRaed Salem err_read:
62901a1e03dcSRaed Salem 	kfree(mread_attr.out);
62911a1e03dcSRaed Salem err_bound:
62921a1e03dcSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
62931a1e03dcSRaed Salem 	return ret;
62941a1e03dcSRaed Salem }
62951a1e03dcSRaed Salem 
6296b29e2a13SRaed Salem static int mlx5_ib_destroy_counters(struct ib_counters *counters)
6297b29e2a13SRaed Salem {
6298b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
6299b29e2a13SRaed Salem 
63003b3233fbSRaed Salem 	counters_clear_description(counters);
63013b3233fbSRaed Salem 	if (mcounters->hw_cntrs_hndl)
63023b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(counters->device)->mdev,
63033b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
63043b3233fbSRaed Salem 
6305b29e2a13SRaed Salem 	kfree(mcounters);
6306b29e2a13SRaed Salem 
6307b29e2a13SRaed Salem 	return 0;
6308b29e2a13SRaed Salem }
6309b29e2a13SRaed Salem 
6310b29e2a13SRaed Salem static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
6311b29e2a13SRaed Salem 						   struct uverbs_attr_bundle *attrs)
6312b29e2a13SRaed Salem {
6313b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters;
6314b29e2a13SRaed Salem 
6315b29e2a13SRaed Salem 	mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
6316b29e2a13SRaed Salem 	if (!mcounters)
6317b29e2a13SRaed Salem 		return ERR_PTR(-ENOMEM);
6318b29e2a13SRaed Salem 
63193b3233fbSRaed Salem 	mutex_init(&mcounters->mcntrs_mutex);
63203b3233fbSRaed Salem 
6321b29e2a13SRaed Salem 	return &mcounters->ibcntrs;
6322b29e2a13SRaed Salem }
6323b29e2a13SRaed Salem 
6324fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
6325e126ba97SEli Cohen {
632632f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
6327806b101bSJason Gunthorpe 	WARN_ON(!xa_empty(&dev->odp_mkeys));
6328806b101bSJason Gunthorpe 	cleanup_srcu_struct(&dev->odp_srcu);
63294056b12eSAriel Levkovich 
633050211ec9SJason Gunthorpe 	WARN_ON(!xa_empty(&dev->sig_mrs));
63314056b12eSAriel Levkovich 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
633216c1975fSMark Bloch }
633316c1975fSMark Bloch 
6334fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
633516c1975fSMark Bloch {
633616c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
6337e126ba97SEli Cohen 	int err;
633832f69e4bSDaniel Jurgens 	int i;
6339e126ba97SEli Cohen 
634032f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
634132f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
634295579e78SMark Bloch 		rwlock_init(&dev->port[i].roce.netdev_lock);
6343d3b5cc1cSMark Bloch 		dev->port[i].roce.dev = dev;
6344d3b5cc1cSMark Bloch 		dev->port[i].roce.native_port_num = i + 1;
6345d3b5cc1cSMark Bloch 		dev->port[i].roce.last_port_state = IB_PORT_DOWN;
634632f69e4bSDaniel Jurgens 	}
634732f69e4bSDaniel Jurgens 
634800815752SMoni Shoua 	mlx5_ib_internal_fill_odp_caps(dev);
634900815752SMoni Shoua 
635032f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
6351e126ba97SEli Cohen 	if (err)
6352da796ccbSMark Bloch 		return err;
6353e126ba97SEli Cohen 
6354a989ea01SMark Bloch 	err = set_has_smi_cap(dev);
6355a989ea01SMark Bloch 	if (err)
6356a989ea01SMark Bloch 		return err;
6357e126ba97SEli Cohen 
635832f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
635932f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
636032f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
636132f69e4bSDaniel Jurgens 			if (err)
636232f69e4bSDaniel Jurgens 				break;
636332f69e4bSDaniel Jurgens 		}
636432f69e4bSDaniel Jurgens 	} else {
636532f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
636632f69e4bSDaniel Jurgens 	}
636732f69e4bSDaniel Jurgens 	if (err)
636832f69e4bSDaniel Jurgens 		goto err_mp;
636932f69e4bSDaniel Jurgens 
63701b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
6371e126ba97SEli Cohen 		get_ext_port_caps(dev);
6372e126ba97SEli Cohen 
6373e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
6374c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
6375e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
6376f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
6377c42260f1SVu Pham 	dev->ib_dev.dev.parent		= mdev->device;
6378e126ba97SEli Cohen 
63793cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
63803cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
63813cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
6382806b101bSJason Gunthorpe 	xa_init(&dev->odp_mkeys);
638350211ec9SJason Gunthorpe 	xa_init(&dev->sig_mrs);
6384f743ff3bSSaeed Mahameed 	atomic_set(&dev->mkey_var, 0);
63853cc297dbSMark Bloch 
63863b113a1eSAriel Levkovich 	spin_lock_init(&dev->dm.lock);
63873b113a1eSAriel Levkovich 	dev->dm.dev = mdev;
638824da0016SAriel Levkovich 
6389806b101bSJason Gunthorpe 	err = init_srcu_struct(&dev->odp_srcu);
6390a6bc3875SMoni Shoua 	if (err)
6391c9b9dcb4SAriel Levkovich 		goto err_mp;
63923cc297dbSMark Bloch 
639316c1975fSMark Bloch 	return 0;
639425c13324SAriel Levkovich 
639532f69e4bSDaniel Jurgens err_mp:
639632f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
639716c1975fSMark Bloch 
639816c1975fSMark Bloch 	return -ENOMEM;
639916c1975fSMark Bloch }
640016c1975fSMark Bloch 
64019a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
64029a4ca38dSMark Bloch {
64039a4ca38dSMark Bloch 	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
64049a4ca38dSMark Bloch 
64059a4ca38dSMark Bloch 	if (!dev->flow_db)
64069a4ca38dSMark Bloch 		return -ENOMEM;
64079a4ca38dSMark Bloch 
64089a4ca38dSMark Bloch 	mutex_init(&dev->flow_db->lock);
64099a4ca38dSMark Bloch 
64109a4ca38dSMark Bloch 	return 0;
64119a4ca38dSMark Bloch }
64129a4ca38dSMark Bloch 
64139a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
64149a4ca38dSMark Bloch {
64159a4ca38dSMark Bloch 	kfree(dev->flow_db);
64169a4ca38dSMark Bloch }
64179a4ca38dSMark Bloch 
641896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
64197a154142SJason Gunthorpe 	.owner = THIS_MODULE,
6420b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_MLX5,
642172c6ec18SJason Gunthorpe 	.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION,
6422b9560a41SJason Gunthorpe 
642396458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
642496458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
64256c984472SMax Gurtovoy 	.alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
642696458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
642796458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
642896458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
642996458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
643096458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
643196458233SKamal Heib 	.create_counters = mlx5_ib_create_counters,
643296458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
643396458233SKamal Heib 	.create_flow = mlx5_ib_create_flow,
643496458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
643596458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
643696458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
643796458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
643896458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
643996458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
644096458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
644196458233SKamal Heib 	.destroy_counters = mlx5_ib_destroy_counters,
644296458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
644396458233SKamal Heib 	.destroy_flow = mlx5_ib_destroy_flow,
644496458233SKamal Heib 	.destroy_flow_action = mlx5_ib_destroy_flow_action,
644596458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
644696458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
644796458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
644896458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
644996458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
645096458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
645111f552e2SMichael Guralnik 	.enable_driver = mlx5_ib_enable_driver,
6452e1b95ae0SErez Alfasi 	.fill_res_entry = mlx5_ib_fill_res_entry,
64534061ff7aSErez Alfasi 	.fill_stat_entry = mlx5_ib_fill_stat_entry,
645496458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
645596458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
645696458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
645796458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
64586c984472SMax Gurtovoy 	.map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
645996458233SKamal Heib 	.mmap = mlx5_ib_mmap,
6460dc2316ebSYishai Hadas 	.mmap_free = mlx5_ib_mmap_free,
646196458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
646296458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
646396458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
646496458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
646596458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
646696458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
646796458233SKamal Heib 	.post_recv = mlx5_ib_post_recv,
646896458233SKamal Heib 	.post_send = mlx5_ib_post_send,
646996458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
647096458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
647196458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
647296458233SKamal Heib 	.query_device = mlx5_ib_query_device,
647396458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
647496458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
647596458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
647696458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
647796458233SKamal Heib 	.read_counters = mlx5_ib_read_counters,
647896458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
647996458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
648096458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
648196458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
6482d3456914SLeon Romanovsky 
6483d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
6484e39afe3dSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
648521a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
648668e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
6487a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
648896458233SKamal Heib };
648996458233SKamal Heib 
649096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
649196458233SKamal Heib 	.create_flow_action_esp = mlx5_ib_create_flow_action_esp,
649296458233SKamal Heib 	.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
649396458233SKamal Heib };
649496458233SKamal Heib 
649596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
649696458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
649796458233SKamal Heib };
649896458233SKamal Heib 
649996458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
650096458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
65019c0015efSDanit Goldberg 	.get_vf_guid = mlx5_ib_get_vf_guid,
650296458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
650396458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
650496458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
650596458233SKamal Heib };
650696458233SKamal Heib 
650796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
650896458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
650996458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
651096458233SKamal Heib };
651196458233SKamal Heib 
651296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
651396458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
651496458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
651596458233SKamal Heib };
651696458233SKamal Heib 
651796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
651896458233SKamal Heib 	.alloc_dm = mlx5_ib_alloc_dm,
651996458233SKamal Heib 	.dealloc_dm = mlx5_ib_dealloc_dm,
652096458233SKamal Heib 	.reg_dm_mr = mlx5_ib_reg_dm_mr,
652196458233SKamal Heib };
652296458233SKamal Heib 
6523f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev)
6524f164be8cSYishai Hadas {
6525f164be8cSYishai Hadas 	struct mlx5_core_dev *mdev = dev->mdev;
6526f164be8cSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
6527f164be8cSYishai Hadas 	u8 log_doorbell_bar_size;
6528f164be8cSYishai Hadas 	u8 log_doorbell_stride;
6529f164be8cSYishai Hadas 	u64 bar_size;
6530f164be8cSYishai Hadas 
6531f164be8cSYishai Hadas 	log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
6532f164be8cSYishai Hadas 					log_doorbell_bar_size);
6533f164be8cSYishai Hadas 	log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
6534f164be8cSYishai Hadas 					log_doorbell_stride);
6535f164be8cSYishai Hadas 	var_table->hw_start_addr = dev->mdev->bar_addr +
6536f164be8cSYishai Hadas 				MLX5_CAP64_DEV_VDPA_EMULATION(mdev,
6537f164be8cSYishai Hadas 					doorbell_bar_offset);
6538f164be8cSYishai Hadas 	bar_size = (1ULL << log_doorbell_bar_size) * 4096;
6539f164be8cSYishai Hadas 	var_table->stride_size = 1ULL << log_doorbell_stride;
654091b74bf5SAlexander Lobakin 	var_table->num_var_hw_entries = div_u64(bar_size,
654191b74bf5SAlexander Lobakin 						var_table->stride_size);
6542f164be8cSYishai Hadas 	mutex_init(&var_table->bitmap_lock);
6543f164be8cSYishai Hadas 	var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries,
6544f164be8cSYishai Hadas 					  GFP_KERNEL);
6545f164be8cSYishai Hadas 	return (var_table->bitmap) ? 0 : -ENOMEM;
6546f164be8cSYishai Hadas }
6547f164be8cSYishai Hadas 
6548f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev)
6549f164be8cSYishai Hadas {
6550f164be8cSYishai Hadas 	bitmap_free(dev->var_table.bitmap);
6551f164be8cSYishai Hadas }
6552f164be8cSYishai Hadas 
6553fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
655416c1975fSMark Bloch {
655516c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
655616c1975fSMark Bloch 	int err;
655716c1975fSMark Bloch 
6558e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
6559e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
6560e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
6561e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
6562e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
6563e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
656441c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
656541c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
6566e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
656756e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
6568e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
6569e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
6570e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
6571e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
6572e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
6573e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
6574e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
6575e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
6576e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
6577e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
6578e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
6579e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
6580e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
6581e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
6582e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
6583e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
6584e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
65851707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
6586d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
6587d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
65887d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
6589b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
659096458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ)	|
659196458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW)	|
659296458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
6593e126ba97SEli Cohen 
6594f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
6595f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
659696458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
659796458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
65988e959601SNiranjana Vishwanathapura 
659996458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
660096458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
66017c2344c3SMaor Gottlieb 
66026e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
66036e8484c5SMax Gurtovoy 
6604d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
6605d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
6606d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
6607d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
660896458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
6609d2370e0aSMatan Barak 	}
6610d2370e0aSMatan Barak 
6611938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
6612e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
6613e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
6614e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
661596458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
6616e126ba97SEli Cohen 	}
6617e126ba97SEli Cohen 
661825c13324SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic) ||
661925c13324SAriel Levkovich 	    MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
662025c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
662196458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
662224da0016SAriel Levkovich 
6623dfb631a1SJason Gunthorpe 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
662496458233SKamal Heib 	    MLX5_ACCEL_IPSEC_CAP_DEVICE)
662596458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
662696458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
662781e30880SYishai Hadas 
662836e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
662936e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
663081e30880SYishai Hadas 
6631e126ba97SEli Cohen 	err = init_node_data(dev);
6632e126ba97SEli Cohen 	if (err)
663316c1975fSMark Bloch 		return err;
6634e126ba97SEli Cohen 
6635c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
6636e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6637e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
6638a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
6639c8b89924SMark Bloch 
6640f164be8cSYishai Hadas 	if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
6641f164be8cSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) {
6642f164be8cSYishai Hadas 		err = mlx5_ib_init_var_table(dev);
6643f164be8cSYishai Hadas 		if (err)
6644f164be8cSYishai Hadas 			return err;
6645f164be8cSYishai Hadas 	}
6646f164be8cSYishai Hadas 
664796e2fd73SLeon Romanovsky 	dev->ib_dev.use_cq_dim = true;
664896e2fd73SLeon Romanovsky 
664916c1975fSMark Bloch 	return 0;
665016c1975fSMark Bloch }
665116c1975fSMark Bloch 
665296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
665396458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
665496458233SKamal Heib 	.query_port = mlx5_ib_query_port,
665596458233SKamal Heib };
665696458233SKamal Heib 
66578e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
665816c1975fSMark Bloch {
665996458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
66608e6efa3aSMark Bloch 	return 0;
66618e6efa3aSMark Bloch }
66628e6efa3aSMark Bloch 
666396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
666496458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
666596458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
666696458233SKamal Heib };
666796458233SKamal Heib 
6668b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev)
66698e6efa3aSMark Bloch {
667096458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
66718e6efa3aSMark Bloch 	return 0;
66728e6efa3aSMark Bloch }
66738e6efa3aSMark Bloch 
667496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
667596458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
667696458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
667796458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
667896458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
667996458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
668096458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
668196458233SKamal Heib };
668296458233SKamal Heib 
6683e3f1ed1fSLeon Romanovsky static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
66848e6efa3aSMark Bloch {
6685e3f1ed1fSLeon Romanovsky 	u8 port_num;
66867fd8aefbSDaniel Jurgens 
6687c11a226aSMark Bloch 	dev->ib_dev.uverbs_ex_cmd_mask |=
6688c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6689c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6690c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6691c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6692c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
669396458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
66948e6efa3aSMark Bloch 
6695e3f1ed1fSLeon Romanovsky 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6696e3f1ed1fSLeon Romanovsky 
669726628e2dSMark Bloch 	/* Register only for native ports */
66988e6efa3aSMark Bloch 	return mlx5_add_netdev_notifier(dev, port_num);
66998e6efa3aSMark Bloch }
67008e6efa3aSMark Bloch 
67018e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
67028e6efa3aSMark Bloch {
67038e6efa3aSMark Bloch 	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
67048e6efa3aSMark Bloch 
67058e6efa3aSMark Bloch 	mlx5_remove_netdev_notifier(dev, port_num);
67068e6efa3aSMark Bloch }
67078e6efa3aSMark Bloch 
6708b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_roce_init(struct mlx5_ib_dev *dev)
67098e6efa3aSMark Bloch {
67108e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
67118e6efa3aSMark Bloch 	enum rdma_link_layer ll;
67128e6efa3aSMark Bloch 	int port_type_cap;
67138e6efa3aSMark Bloch 	int err = 0;
67148e6efa3aSMark Bloch 
67158e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
67168e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
67178e6efa3aSMark Bloch 
67188e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET)
6719e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
67208e6efa3aSMark Bloch 
672116c1975fSMark Bloch 	return err;
6722fc24fc5eSAchiad Shochat }
6723fc24fc5eSAchiad Shochat 
6724b5a498baSMichael Guralnik static void mlx5_ib_stage_raw_eth_roce_cleanup(struct mlx5_ib_dev *dev)
67258e6efa3aSMark Bloch {
67268e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
67278e6efa3aSMark Bloch }
67288e6efa3aSMark Bloch 
67298e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
67308e6efa3aSMark Bloch {
67318e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
67328e6efa3aSMark Bloch 	enum rdma_link_layer ll;
67338e6efa3aSMark Bloch 	int port_type_cap;
67348e6efa3aSMark Bloch 	int err;
67358e6efa3aSMark Bloch 
67368e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
67378e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
67388e6efa3aSMark Bloch 
67398e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
6740e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
67418e6efa3aSMark Bloch 		if (err)
67428e6efa3aSMark Bloch 			return err;
67438e6efa3aSMark Bloch 
6744e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
67458e6efa3aSMark Bloch 		if (err)
67468e6efa3aSMark Bloch 			goto cleanup;
67478e6efa3aSMark Bloch 	}
67488e6efa3aSMark Bloch 
674916c1975fSMark Bloch 	return 0;
67508e6efa3aSMark Bloch cleanup:
67518e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
67528e6efa3aSMark Bloch 
67538e6efa3aSMark Bloch 	return err;
675445bded2cSKamal Heib }
67556aec21f6SHaggai Eran 
675616c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
675716c1975fSMark Bloch {
675816c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
675916c1975fSMark Bloch 	enum rdma_link_layer ll;
676016c1975fSMark Bloch 	int port_type_cap;
67614a2da0b8SParav Pandit 
676216c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
676316c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
676416c1975fSMark Bloch 
676516c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
676616c1975fSMark Bloch 		mlx5_disable_eth(dev);
67678e6efa3aSMark Bloch 		mlx5_ib_stage_common_roce_cleanup(dev);
676816c1975fSMark Bloch 	}
676916c1975fSMark Bloch }
677016c1975fSMark Bloch 
6771fb652d32SMark Bloch static int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
677216c1975fSMark Bloch {
677316c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
677416c1975fSMark Bloch }
677516c1975fSMark Bloch 
6776fb652d32SMark Bloch static void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
677716c1975fSMark Bloch {
677816c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
677916c1975fSMark Bloch }
678016c1975fSMark Bloch 
678116c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
678216c1975fSMark Bloch {
678316c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
678416c1975fSMark Bloch }
678516c1975fSMark Bloch 
6786f3ffed0cSKamal Heib static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
6787d5d284b8SSaeed Mahameed {
6788d5d284b8SSaeed Mahameed 	mlx5_ib_odp_cleanup_one(dev);
6789d5d284b8SSaeed Mahameed }
6790d5d284b8SSaeed Mahameed 
679196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
679296458233SKamal Heib 	.alloc_hw_stats = mlx5_ib_alloc_hw_stats,
679396458233SKamal Heib 	.get_hw_stats = mlx5_ib_get_hw_stats,
679445842fc6SMark Zhang 	.counter_bind_qp = mlx5_ib_counter_bind_qp,
679545842fc6SMark Zhang 	.counter_unbind_qp = mlx5_ib_counter_unbind_qp,
679645842fc6SMark Zhang 	.counter_dealloc = mlx5_ib_counter_dealloc,
679718d422ceSMark Zhang 	.counter_alloc_stats = mlx5_ib_counter_alloc_stats,
679818d422ceSMark Zhang 	.counter_update_stats = mlx5_ib_counter_update_stats,
679996458233SKamal Heib };
680096458233SKamal Heib 
6801fb652d32SMark Bloch static int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
680216c1975fSMark Bloch {
68035e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
680496458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
68055e1e7612SMark Bloch 
680616c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
68075e1e7612SMark Bloch 	}
680816c1975fSMark Bloch 
680916c1975fSMark Bloch 	return 0;
681016c1975fSMark Bloch }
681116c1975fSMark Bloch 
6812fb652d32SMark Bloch static void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
681316c1975fSMark Bloch {
681416c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
681516c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
681616c1975fSMark Bloch }
681716c1975fSMark Bloch 
681816c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
681916c1975fSMark Bloch {
682073eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
6821a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
682273eb8f03SGreg Kroah-Hartman 	return 0;
682316c1975fSMark Bloch }
682416c1975fSMark Bloch 
682516c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
682616c1975fSMark Bloch {
6827a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
6828a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
682916c1975fSMark Bloch }
683016c1975fSMark Bloch 
683116c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
683216c1975fSMark Bloch {
68335fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
6834444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
683516c1975fSMark Bloch }
683616c1975fSMark Bloch 
683716c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
683816c1975fSMark Bloch {
683916c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
684016c1975fSMark Bloch }
684116c1975fSMark Bloch 
6842fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
684316c1975fSMark Bloch {
684416c1975fSMark Bloch 	int err;
68455fe9dec0SEli Cohen 
68465fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
68475fe9dec0SEli Cohen 	if (err)
684816c1975fSMark Bloch 		return err;
68495fe9dec0SEli Cohen 
68505fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
68515fe9dec0SEli Cohen 	if (err)
685216c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
68535fe9dec0SEli Cohen 
685416c1975fSMark Bloch 	return err;
685516c1975fSMark Bloch }
68560837e86aSMark Bloch 
6857fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
685816c1975fSMark Bloch {
685916c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
686016c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
686116c1975fSMark Bloch }
6862e126ba97SEli Cohen 
6863fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
686416c1975fSMark Bloch {
6865e349f858SJason Gunthorpe 	const char *name;
6866e349f858SJason Gunthorpe 
6867508a523fSParav Pandit 	rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
68687c34ec19SAviv Heller 	if (!mlx5_lag_is_roce(dev->mdev))
6869e349f858SJason Gunthorpe 		name = "mlx5_%d";
6870e349f858SJason Gunthorpe 	else
6871e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
6872ea4baf7fSParav Pandit 	return ib_register_device(&dev->ib_dev, name);
687316c1975fSMark Bloch }
687416c1975fSMark Bloch 
6875fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
687642cea83fSMark Bloch {
687742cea83fSMark Bloch 	destroy_umrc_res(dev);
687842cea83fSMark Bloch }
687942cea83fSMark Bloch 
6880fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
688116c1975fSMark Bloch {
688216c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
688316c1975fSMark Bloch }
688416c1975fSMark Bloch 
6885fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
688616c1975fSMark Bloch {
688716c1975fSMark Bloch 	return create_umr_res(dev);
688816c1975fSMark Bloch }
688916c1975fSMark Bloch 
689016c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
689116c1975fSMark Bloch {
689203404e8aSMaor Gottlieb 	init_delay_drop(dev);
689303404e8aSMaor Gottlieb 
689416c1975fSMark Bloch 	return 0;
689516c1975fSMark Bloch }
689616c1975fSMark Bloch 
689716c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
689816c1975fSMark Bloch {
689916c1975fSMark Bloch 	cancel_delay_drop(dev);
690016c1975fSMark Bloch }
690116c1975fSMark Bloch 
6902df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
6903df097a27SSaeed Mahameed {
6904df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
6905df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
6906df097a27SSaeed Mahameed 	return 0;
6907df097a27SSaeed Mahameed }
6908df097a27SSaeed Mahameed 
6909df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
6910df097a27SSaeed Mahameed {
6911df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
6912df097a27SSaeed Mahameed }
6913df097a27SSaeed Mahameed 
691481773ce5SLeon Romanovsky static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
691581773ce5SLeon Romanovsky {
691681773ce5SLeon Romanovsky 	int uid;
691781773ce5SLeon Romanovsky 
6918fb98153bSYishai Hadas 	uid = mlx5_ib_devx_create(dev, false);
6919e337dd53SYishai Hadas 	if (uid > 0) {
692081773ce5SLeon Romanovsky 		dev->devx_whitelist_uid = uid;
6921e337dd53SYishai Hadas 		mlx5_ib_devx_init_event_table(dev);
6922e337dd53SYishai Hadas 	}
692381773ce5SLeon Romanovsky 
692481773ce5SLeon Romanovsky 	return 0;
692581773ce5SLeon Romanovsky }
692681773ce5SLeon Romanovsky static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
692781773ce5SLeon Romanovsky {
6928e337dd53SYishai Hadas 	if (dev->devx_whitelist_uid) {
6929e337dd53SYishai Hadas 		mlx5_ib_devx_cleanup_event_table(dev);
693081773ce5SLeon Romanovsky 		mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
693181773ce5SLeon Romanovsky 	}
6932e337dd53SYishai Hadas }
693381773ce5SLeon Romanovsky 
693411f552e2SMichael Guralnik int mlx5_ib_enable_driver(struct ib_device *dev)
693511f552e2SMichael Guralnik {
693611f552e2SMichael Guralnik 	struct mlx5_ib_dev *mdev = to_mdev(dev);
693711f552e2SMichael Guralnik 	int ret;
693811f552e2SMichael Guralnik 
693911f552e2SMichael Guralnik 	ret = mlx5_ib_test_wc(mdev);
694011f552e2SMichael Guralnik 	mlx5_ib_dbg(mdev, "Write-Combining %s",
694111f552e2SMichael Guralnik 		    mdev->wc_support ? "supported" : "not supported");
694211f552e2SMichael Guralnik 
694311f552e2SMichael Guralnik 	return ret;
694411f552e2SMichael Guralnik }
694511f552e2SMichael Guralnik 
6946b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
694716c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
694816c1975fSMark Bloch 		      int stage)
694916c1975fSMark Bloch {
69504cca96a8SParav Pandit 	dev->ib_active = false;
69514cca96a8SParav Pandit 
695216c1975fSMark Bloch 	/* Number of stages to cleanup */
695316c1975fSMark Bloch 	while (stage) {
695416c1975fSMark Bloch 		stage--;
695516c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
695616c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
695716c1975fSMark Bloch 	}
69584a6dc855SMark Bloch 
6959da796ccbSMark Bloch 	kfree(dev->port);
69604a6dc855SMark Bloch 	ib_dealloc_device(&dev->ib_dev);
696116c1975fSMark Bloch }
696216c1975fSMark Bloch 
6963b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
696416c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
696516c1975fSMark Bloch {
696616c1975fSMark Bloch 	int err;
696716c1975fSMark Bloch 	int i;
696816c1975fSMark Bloch 
696916c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
697016c1975fSMark Bloch 		if (profile->stage[i].init) {
697116c1975fSMark Bloch 			err = profile->stage[i].init(dev);
697216c1975fSMark Bloch 			if (err)
697316c1975fSMark Bloch 				goto err_out;
697416c1975fSMark Bloch 		}
697516c1975fSMark Bloch 	}
697616c1975fSMark Bloch 
697716c1975fSMark Bloch 	dev->profile = profile;
6978e126ba97SEli Cohen 	dev->ib_active = true;
6979e126ba97SEli Cohen 
69809603b61dSJack Morgenstein 	return dev;
6981e126ba97SEli Cohen 
698216c1975fSMark Bloch err_out:
698316c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
6984e126ba97SEli Cohen 
69859603b61dSJack Morgenstein 	return NULL;
6986e126ba97SEli Cohen }
6987e126ba97SEli Cohen 
698816c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
698916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
699016c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
699116c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
69929a4ca38dSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
69939a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_init,
69949a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
699516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
699616c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
6997f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
69988e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
69998e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
70008e6efa3aSMark Bloch 		     NULL),
700116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
700216c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
700316c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
7004f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
7005f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
7006f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
700716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
700816c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
700916c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
7010df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
7011df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
7012df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
701316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
701416c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
7015d5d284b8SSaeed Mahameed 		     mlx5_ib_stage_odp_cleanup),
701616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
701716c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
701816c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
701916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
702016c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
702116c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
702216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
702316c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
702416c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
702516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
702616c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
702716c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
702842cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
702942cea83fSMark Bloch 		     NULL,
703042cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
703181773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
703281773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_init,
703381773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_cleanup),
703416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
703516c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
703616c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
703742cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
703842cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
703942cea83fSMark Bloch 		     NULL),
704016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
704116c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
704216c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
704316c1975fSMark Bloch };
704416c1975fSMark Bloch 
7045b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = {
7046b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
7047b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
7048b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
7049b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
7050b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_init,
7051b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
7052b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
7053b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
7054f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
7055b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
7056b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_non_default_cb,
7057b5ca15adSMark Bloch 		     NULL),
7058b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
7059b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_roce_init,
7060b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_roce_cleanup),
7061f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
7062f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
7063f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
7064b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
7065b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_init,
7066b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
7067df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
7068df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
7069df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
7070b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
7071b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_init,
7072b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
707379db784eSParav Pandit 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
707479db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_init,
707579db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_cleanup),
7076b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
7077b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
7078b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
7079b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
7080b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
7081b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
708203fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
708303fe2debSDavid S. Miller 		     NULL,
708403fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
70857f575103SMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
70867f575103SMark Bloch 		     mlx5_ib_stage_devx_init,
70877f575103SMark Bloch 		     mlx5_ib_stage_devx_cleanup),
7088b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
7089b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
7090b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
709103fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
709203fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
709303fe2debSDavid S. Miller 		     NULL),
7094b5ca15adSMark Bloch };
7095b5ca15adSMark Bloch 
7096e3f1ed1fSLeon Romanovsky static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
709732f69e4bSDaniel Jurgens {
709832f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
709932f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
710032f69e4bSDaniel Jurgens 	bool bound = false;
710132f69e4bSDaniel Jurgens 	int err;
710232f69e4bSDaniel Jurgens 
710332f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
710432f69e4bSDaniel Jurgens 	if (!mpi)
710532f69e4bSDaniel Jurgens 		return NULL;
710632f69e4bSDaniel Jurgens 
710732f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
710832f69e4bSDaniel Jurgens 
710932f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
711032f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
711132f69e4bSDaniel Jurgens 	if (err) {
711232f69e4bSDaniel Jurgens 		kfree(mpi);
711332f69e4bSDaniel Jurgens 		return NULL;
711432f69e4bSDaniel Jurgens 	}
711532f69e4bSDaniel Jurgens 
711632f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
711732f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
711832f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
711932f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
712032f69e4bSDaniel Jurgens 
712132f69e4bSDaniel Jurgens 		if (bound) {
712232f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
712332f69e4bSDaniel Jurgens 			break;
712432f69e4bSDaniel Jurgens 		}
712532f69e4bSDaniel Jurgens 	}
712632f69e4bSDaniel Jurgens 
712732f69e4bSDaniel Jurgens 	if (!bound) {
712832f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
7129c42260f1SVu Pham 		dev_dbg(mdev->device,
7130c42260f1SVu Pham 			"no suitable IB device found to bind to, added to unaffiliated list.\n");
713132f69e4bSDaniel Jurgens 	}
713232f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
713332f69e4bSDaniel Jurgens 
713432f69e4bSDaniel Jurgens 	return mpi;
713532f69e4bSDaniel Jurgens }
713632f69e4bSDaniel Jurgens 
713716c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
713816c1975fSMark Bloch {
713994de879cSMichael Guralnik 	const struct mlx5_ib_profile *profile;
714032f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
7141b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
714232f69e4bSDaniel Jurgens 	int port_type_cap;
7143da796ccbSMark Bloch 	int num_ports;
714432f69e4bSDaniel Jurgens 
7145b5ca15adSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
7146b5ca15adSMark Bloch 
7147f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) &&
7148f6455de0SBodong Wang 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == MLX5_ESWITCH_OFFLOADS) {
71495fb58c9eSMark Bloch 		if (!mlx5_core_mp_enabled(mdev))
7150f0666f1fSBodong Wang 			mlx5_ib_register_vport_reps(mdev);
7151f0666f1fSBodong Wang 		return mdev;
7152f0666f1fSBodong Wang 	}
7153f0666f1fSBodong Wang 
715432f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
715532f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
715632f69e4bSDaniel Jurgens 
7157e3f1ed1fSLeon Romanovsky 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
7158e3f1ed1fSLeon Romanovsky 		return mlx5_ib_add_slave_port(mdev);
715932f69e4bSDaniel Jurgens 
7160da796ccbSMark Bloch 	num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
7161da796ccbSMark Bloch 			MLX5_CAP_GEN(mdev, num_vhca_ports));
7162459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
7163b5ca15adSMark Bloch 	if (!dev)
7164b5ca15adSMark Bloch 		return NULL;
7165da796ccbSMark Bloch 	dev->port = kcalloc(num_ports, sizeof(*dev->port),
7166da796ccbSMark Bloch 			     GFP_KERNEL);
7167da796ccbSMark Bloch 	if (!dev->port) {
7168a5c9c299SParav Pandit 		ib_dealloc_device(&dev->ib_dev);
7169da796ccbSMark Bloch 		return NULL;
7170da796ccbSMark Bloch 	}
7171b5ca15adSMark Bloch 
7172b5ca15adSMark Bloch 	dev->mdev = mdev;
7173da796ccbSMark Bloch 	dev->num_ports = num_ports;
7174b5ca15adSMark Bloch 
717594de879cSMichael Guralnik 	if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_is_roce_enabled(mdev))
717694de879cSMichael Guralnik 		profile = &raw_eth_profile;
717794de879cSMichael Guralnik 	else
717894de879cSMichael Guralnik 		profile = &pf_profile;
717994de879cSMichael Guralnik 
718094de879cSMichael Guralnik 	return __mlx5_ib_add(dev, profile);
718116c1975fSMark Bloch }
718216c1975fSMark Bloch 
71839603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
7184e126ba97SEli Cohen {
718532f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
718632f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
71876aec21f6SHaggai Eran 
7188f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
7189f0666f1fSBodong Wang 		mlx5_ib_unregister_vport_reps(mdev);
7190f0666f1fSBodong Wang 		return;
7191f0666f1fSBodong Wang 	}
7192f0666f1fSBodong Wang 
719332f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
719432f69e4bSDaniel Jurgens 		mpi = context;
719532f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
719632f69e4bSDaniel Jurgens 		if (mpi->ibdev)
719732f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
719832f69e4bSDaniel Jurgens 		list_del(&mpi->list);
719932f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
72005d44adebSDanit Goldberg 		kfree(mpi);
720132f69e4bSDaniel Jurgens 		return;
720232f69e4bSDaniel Jurgens 	}
720332f69e4bSDaniel Jurgens 
720432f69e4bSDaniel Jurgens 	dev = context;
720516c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
7206e126ba97SEli Cohen }
7207e126ba97SEli Cohen 
72089603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
72099603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
72109603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
721164613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
7212e126ba97SEli Cohen };
7213e126ba97SEli Cohen 
7214c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
7215c44ef998SIlya Lesokhin {
7216c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
7217c44ef998SIlya Lesokhin 	return xlt_emergency_page;
7218c44ef998SIlya Lesokhin }
7219c44ef998SIlya Lesokhin 
7220c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
7221c44ef998SIlya Lesokhin {
7222c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
7223c44ef998SIlya Lesokhin }
7224c44ef998SIlya Lesokhin 
7225e126ba97SEli Cohen static int __init mlx5_ib_init(void)
7226e126ba97SEli Cohen {
72276aec21f6SHaggai Eran 	int err;
72286aec21f6SHaggai Eran 
7229c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
7230c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
7231d69a24e0SDaniel Jurgens 		return -ENOMEM;
7232d69a24e0SDaniel Jurgens 
7233c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
7234c44ef998SIlya Lesokhin 
7235c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
7236c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
7237c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
7238c44ef998SIlya Lesokhin 		return -ENOMEM;
7239c44ef998SIlya Lesokhin 	}
7240c44ef998SIlya Lesokhin 
724181713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
72429603b61dSJack Morgenstein 
72436aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
72446aec21f6SHaggai Eran 
72456aec21f6SHaggai Eran 	return err;
7246e126ba97SEli Cohen }
7247e126ba97SEli Cohen 
7248e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
7249e126ba97SEli Cohen {
72509603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
7251d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
7252c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
7253c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
7254e126ba97SEli Cohen }
7255e126ba97SEli Cohen 
7256e126ba97SEli Cohen module_init(mlx5_ib_init);
7257e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
7258