xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 7a15414252ae4f1d450462d83f883b2d9d8036ee)
1e126ba97SEli Cohen /*
26cf0a15fSSaeed Mahameed  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3e126ba97SEli Cohen  *
4e126ba97SEli Cohen  * This software is available to you under a choice of one of two
5e126ba97SEli Cohen  * licenses.  You may choose to be licensed under the terms of the GNU
6e126ba97SEli Cohen  * General Public License (GPL) Version 2, available from the file
7e126ba97SEli Cohen  * COPYING in the main directory of this source tree, or the
8e126ba97SEli Cohen  * OpenIB.org BSD license below:
9e126ba97SEli Cohen  *
10e126ba97SEli Cohen  *     Redistribution and use in source and binary forms, with or
11e126ba97SEli Cohen  *     without modification, are permitted provided that the following
12e126ba97SEli Cohen  *     conditions are met:
13e126ba97SEli Cohen  *
14e126ba97SEli Cohen  *      - Redistributions of source code must retain the above
15e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
16e126ba97SEli Cohen  *        disclaimer.
17e126ba97SEli Cohen  *
18e126ba97SEli Cohen  *      - Redistributions in binary form must reproduce the above
19e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
20e126ba97SEli Cohen  *        disclaimer in the documentation and/or other materials
21e126ba97SEli Cohen  *        provided with the distribution.
22e126ba97SEli Cohen  *
23e126ba97SEli Cohen  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e126ba97SEli Cohen  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e126ba97SEli Cohen  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e126ba97SEli Cohen  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e126ba97SEli Cohen  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e126ba97SEli Cohen  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e126ba97SEli Cohen  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e126ba97SEli Cohen  * SOFTWARE.
31e126ba97SEli Cohen  */
32e126ba97SEli Cohen 
33fe248c3aSMaor Gottlieb #include <linux/debugfs.h>
34adec640eSChristoph Hellwig #include <linux/highmem.h>
35e126ba97SEli Cohen #include <linux/module.h>
36e126ba97SEli Cohen #include <linux/init.h>
37e126ba97SEli Cohen #include <linux/errno.h>
38e126ba97SEli Cohen #include <linux/pci.h>
39e126ba97SEli Cohen #include <linux/dma-mapping.h>
40e126ba97SEli Cohen #include <linux/slab.h>
4124da0016SAriel Levkovich #include <linux/bitmap.h>
4237aa5c36SGuy Levi #if defined(CONFIG_X86)
4337aa5c36SGuy Levi #include <asm/pat.h>
4437aa5c36SGuy Levi #endif
45e126ba97SEli Cohen #include <linux/sched.h>
466e84f315SIngo Molnar #include <linux/sched/mm.h>
470881e7bdSIngo Molnar #include <linux/sched/task.h>
487c2344c3SMaor Gottlieb #include <linux/delay.h>
49e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
503f89a643SAchiad Shochat #include <rdma/ib_addr.h>
512811ba51SAchiad Shochat #include <rdma/ib_cache.h>
52ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
531b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
5472c7fe90SPravin Shedge #include <linux/mlx5/fs.h>
557c2344c3SMaor Gottlieb #include <linux/list.h>
56e126ba97SEli Cohen #include <rdma/ib_smi.h>
57e126ba97SEli Cohen #include <rdma/ib_umem.h>
58038d2ef8SMaor Gottlieb #include <linux/in.h>
59038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
60e126ba97SEli Cohen #include "mlx5_ib.h"
61fc385b7aSMark Bloch #include "ib_rep.h"
62e1f24a79SParav Pandit #include "cmd.h"
63f3da6577SLeon Romanovsky #include "srq.h"
643346c487SBoris Pismenny #include <linux/mlx5/fs_helpers.h>
65c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h>
668c84660bSMatan Barak #include <rdma/uverbs_std_types.h>
67c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h>
68c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h>
698c84660bSMatan Barak 
708c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
718c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
72e126ba97SEli Cohen 
73e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
74b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0"
75e126ba97SEli Cohen 
76e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
77e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
78e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
79e126ba97SEli Cohen 
80e126ba97SEli Cohen static char mlx5_version[] =
81e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
82b359911dSTariq Toukan 	DRIVER_VERSION "\n";
83e126ba97SEli Cohen 
84d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
85d69a24e0SDaniel Jurgens 	struct work_struct	work;
86df097a27SSaeed Mahameed 	union {
87df097a27SSaeed Mahameed 		struct mlx5_ib_dev	      *dev;
88df097a27SSaeed Mahameed 		struct mlx5_ib_multiport_info *mpi;
89df097a27SSaeed Mahameed 	};
90df097a27SSaeed Mahameed 	bool			is_slave;
91134e9349SSaeed Mahameed 	unsigned int		event;
92df097a27SSaeed Mahameed 	void			*param;
93d69a24e0SDaniel Jurgens };
94d69a24e0SDaniel Jurgens 
95da7525d2SEran Ben Elisha enum {
96da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
97da7525d2SEran Ben Elisha };
981b5daf11SMajd Dibbiny 
99d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
10032f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
10132f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
10232f69e4bSDaniel Jurgens /*
10332f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
10432f69e4bSDaniel Jurgens  */
10532f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
10632f69e4bSDaniel Jurgens 
107c44ef998SIlya Lesokhin /* We can't use an array for xlt_emergency_page because dma_map_single
108c44ef998SIlya Lesokhin  * doesn't work on kernel modules memory
109c44ef998SIlya Lesokhin  */
110c44ef998SIlya Lesokhin static unsigned long xlt_emergency_page;
111c44ef998SIlya Lesokhin static struct mutex xlt_emergency_page_mutex;
112c44ef998SIlya Lesokhin 
11332f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
11432f69e4bSDaniel Jurgens {
11532f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
11632f69e4bSDaniel Jurgens 
11732f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
11832f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
11932f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
12032f69e4bSDaniel Jurgens 	return dev;
12132f69e4bSDaniel Jurgens }
12232f69e4bSDaniel Jurgens 
1231b5daf11SMajd Dibbiny static enum rdma_link_layer
124ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
1251b5daf11SMajd Dibbiny {
126ebd61f68SAchiad Shochat 	switch (port_type_cap) {
1271b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
1281b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
1291b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
1301b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
1311b5daf11SMajd Dibbiny 	default:
1321b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1331b5daf11SMajd Dibbiny 	}
1341b5daf11SMajd Dibbiny }
1351b5daf11SMajd Dibbiny 
136ebd61f68SAchiad Shochat static enum rdma_link_layer
137ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
138ebd61f68SAchiad Shochat {
139ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
140ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
141ebd61f68SAchiad Shochat 
142ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
143ebd61f68SAchiad Shochat }
144ebd61f68SAchiad Shochat 
145fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
146fd65f1b8SMoni Shoua 			  u8 port_num,
147fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
148fd65f1b8SMoni Shoua {
149fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
150fd65f1b8SMoni Shoua 	int ret;
151fd65f1b8SMoni Shoua 
152fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1533023a1e9SKamal Heib 	ret = ibdev->ops.query_port(ibdev, port_num, &attr);
154fd65f1b8SMoni Shoua 	if (!ret)
155fd65f1b8SMoni Shoua 		*state = attr.state;
156fd65f1b8SMoni Shoua 	return ret;
157fd65f1b8SMoni Shoua }
158fd65f1b8SMoni Shoua 
15935b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
16035b0aa67SMark Bloch 					   struct net_device *ndev,
16135b0aa67SMark Bloch 					   u8 *port_num)
16235b0aa67SMark Bloch {
16335b0aa67SMark Bloch 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
16435b0aa67SMark Bloch 	struct net_device *rep_ndev;
16535b0aa67SMark Bloch 	struct mlx5_ib_port *port;
16635b0aa67SMark Bloch 	int i;
16735b0aa67SMark Bloch 
16835b0aa67SMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
16935b0aa67SMark Bloch 		port  = &dev->port[i];
17035b0aa67SMark Bloch 		if (!port->rep)
17135b0aa67SMark Bloch 			continue;
17235b0aa67SMark Bloch 
17335b0aa67SMark Bloch 		read_lock(&port->roce.netdev_lock);
17435b0aa67SMark Bloch 		rep_ndev = mlx5_ib_get_rep_netdev(esw,
17535b0aa67SMark Bloch 						  port->rep->vport);
17635b0aa67SMark Bloch 		if (rep_ndev == ndev) {
17735b0aa67SMark Bloch 			read_unlock(&port->roce.netdev_lock);
17835b0aa67SMark Bloch 			*port_num = i + 1;
17935b0aa67SMark Bloch 			return &port->roce;
18035b0aa67SMark Bloch 		}
18135b0aa67SMark Bloch 		read_unlock(&port->roce.netdev_lock);
18235b0aa67SMark Bloch 	}
18335b0aa67SMark Bloch 
18435b0aa67SMark Bloch 	return NULL;
18535b0aa67SMark Bloch }
18635b0aa67SMark Bloch 
187fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
188fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
189fc24fc5eSAchiad Shochat {
1907fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
191fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1927fd8aefbSDaniel Jurgens 	u8 port_num = roce->native_port_num;
1937fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1947fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1957fd8aefbSDaniel Jurgens 
1967fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
19732f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
19832f69e4bSDaniel Jurgens 	if (!mdev)
19932f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
200fc24fc5eSAchiad Shochat 
2015ec8c83eSAviv Heller 	switch (event) {
2025ec8c83eSAviv Heller 	case NETDEV_REGISTER:
20335b0aa67SMark Bloch 		/* Should already be registered during the load */
20435b0aa67SMark Bloch 		if (ibdev->is_rep)
20535b0aa67SMark Bloch 			break;
2067fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
207dce45af5SLinus Torvalds 		if (ndev->dev.parent == mdev->device)
208842a9c83SOr Gerlitz 			roce->netdev = ndev;
2097fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
2105ec8c83eSAviv Heller 		break;
2115ec8c83eSAviv Heller 
212842a9c83SOr Gerlitz 	case NETDEV_UNREGISTER:
21335b0aa67SMark Bloch 		/* In case of reps, ib device goes away before the netdevs */
214842a9c83SOr Gerlitz 		write_lock(&roce->netdev_lock);
215842a9c83SOr Gerlitz 		if (roce->netdev == ndev)
216842a9c83SOr Gerlitz 			roce->netdev = NULL;
217842a9c83SOr Gerlitz 		write_unlock(&roce->netdev_lock);
218842a9c83SOr Gerlitz 		break;
219842a9c83SOr Gerlitz 
220fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
2215ec8c83eSAviv Heller 	case NETDEV_UP:
22288621dfeSAviv Heller 	case NETDEV_DOWN: {
2237fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
22488621dfeSAviv Heller 		struct net_device *upper = NULL;
22588621dfeSAviv Heller 
22688621dfeSAviv Heller 		if (lag_ndev) {
22788621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
22888621dfeSAviv Heller 			dev_put(lag_ndev);
22988621dfeSAviv Heller 		}
23088621dfeSAviv Heller 
23135b0aa67SMark Bloch 		if (ibdev->is_rep)
23235b0aa67SMark Bloch 			roce = mlx5_get_rep_roce(ibdev, ndev, &port_num);
23335b0aa67SMark Bloch 		if (!roce)
23435b0aa67SMark Bloch 			return NOTIFY_DONE;
2357fd8aefbSDaniel Jurgens 		if ((upper == ndev || (!upper && ndev == roce->netdev))
23688621dfeSAviv Heller 		    && ibdev->ib_active) {
237626bc02dSBart Van Assche 			struct ib_event ibev = { };
238fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2395ec8c83eSAviv Heller 
2407fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2417fd8aefbSDaniel Jurgens 					   &port_state))
2427fd8aefbSDaniel Jurgens 				goto done;
243fd65f1b8SMoni Shoua 
2447fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2457fd8aefbSDaniel Jurgens 				goto done;
246fd65f1b8SMoni Shoua 
2477fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2485ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
249fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
250fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
251fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
252fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
253fd65f1b8SMoni Shoua 			else
2547fd8aefbSDaniel Jurgens 				goto done;
255fd65f1b8SMoni Shoua 
2567fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2575ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2585ec8c83eSAviv Heller 		}
2595ec8c83eSAviv Heller 		break;
26088621dfeSAviv Heller 	}
2615ec8c83eSAviv Heller 
2625ec8c83eSAviv Heller 	default:
2635ec8c83eSAviv Heller 		break;
2645ec8c83eSAviv Heller 	}
2657fd8aefbSDaniel Jurgens done:
26632f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
267fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
268fc24fc5eSAchiad Shochat }
269fc24fc5eSAchiad Shochat 
270fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
271fc24fc5eSAchiad Shochat 					     u8 port_num)
272fc24fc5eSAchiad Shochat {
273fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
274fc24fc5eSAchiad Shochat 	struct net_device *ndev;
27532f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
276fc24fc5eSAchiad Shochat 
27732f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
27832f69e4bSDaniel Jurgens 	if (!mdev)
27932f69e4bSDaniel Jurgens 		return NULL;
28032f69e4bSDaniel Jurgens 
28132f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
28288621dfeSAviv Heller 	if (ndev)
28332f69e4bSDaniel Jurgens 		goto out;
28488621dfeSAviv Heller 
285fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
286fc24fc5eSAchiad Shochat 	 */
28795579e78SMark Bloch 	read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
28895579e78SMark Bloch 	ndev = ibdev->port[port_num - 1].roce.netdev;
289fc24fc5eSAchiad Shochat 	if (ndev)
290fc24fc5eSAchiad Shochat 		dev_hold(ndev);
29195579e78SMark Bloch 	read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
292fc24fc5eSAchiad Shochat 
29332f69e4bSDaniel Jurgens out:
29432f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
295fc24fc5eSAchiad Shochat 	return ndev;
296fc24fc5eSAchiad Shochat }
297fc24fc5eSAchiad Shochat 
29832f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
29932f69e4bSDaniel Jurgens 						   u8 ib_port_num,
30032f69e4bSDaniel Jurgens 						   u8 *native_port_num)
30132f69e4bSDaniel Jurgens {
30232f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
30332f69e4bSDaniel Jurgens 							  ib_port_num);
30432f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
30532f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
30632f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
30732f69e4bSDaniel Jurgens 
308210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
309210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
310210b1f78SMark Bloch 		if (native_port_num)
311210b1f78SMark Bloch 			*native_port_num = ib_port_num;
312210b1f78SMark Bloch 		return ibdev->mdev;
313210b1f78SMark Bloch 	}
314210b1f78SMark Bloch 
31532f69e4bSDaniel Jurgens 	if (native_port_num)
31632f69e4bSDaniel Jurgens 		*native_port_num = 1;
31732f69e4bSDaniel Jurgens 
31832f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
31932f69e4bSDaniel Jurgens 	if (!port)
32032f69e4bSDaniel Jurgens 		return NULL;
32132f69e4bSDaniel Jurgens 
32232f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
32332f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
32432f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
32532f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
32632f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
32732f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
32832f69e4bSDaniel Jurgens 		 */
32932f69e4bSDaniel Jurgens 		if (!mpi->is_master)
33032f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
33132f69e4bSDaniel Jurgens 	}
33232f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
33332f69e4bSDaniel Jurgens 
33432f69e4bSDaniel Jurgens 	return mdev;
33532f69e4bSDaniel Jurgens }
33632f69e4bSDaniel Jurgens 
33732f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
33832f69e4bSDaniel Jurgens {
33932f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
34032f69e4bSDaniel Jurgens 							  port_num);
34132f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
34232f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
34332f69e4bSDaniel Jurgens 
34432f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
34532f69e4bSDaniel Jurgens 		return;
34632f69e4bSDaniel Jurgens 
34732f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
34832f69e4bSDaniel Jurgens 
34932f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
35032f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
35132f69e4bSDaniel Jurgens 	if (mpi->is_master)
35232f69e4bSDaniel Jurgens 		goto out;
35332f69e4bSDaniel Jurgens 
35432f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
35532f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
35632f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
35732f69e4bSDaniel Jurgens out:
35832f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
35932f69e4bSDaniel Jurgens }
36032f69e4bSDaniel Jurgens 
36108e8676fSAya Levin static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, u8 *active_speed,
362f1b65df5SNoa Osherovich 					   u8 *active_width)
363f1b65df5SNoa Osherovich {
364f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
365f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
366f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
367f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
368f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
369f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
370f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
371f1b65df5SNoa Osherovich 		break;
372f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
373f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
374f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
375f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
376f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
377f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
378f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
379f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
380f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
381f1b65df5SNoa Osherovich 		break;
382f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
383f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
384f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
385f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
386f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
387f1b65df5SNoa Osherovich 		break;
388f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
389f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
390f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
391f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
392f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
393f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
394f1b65df5SNoa Osherovich 		break;
395f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
396f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
397f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
398f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
399f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
400f1b65df5SNoa Osherovich 		break;
401f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
402f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
403f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
404f1b65df5SNoa Osherovich 		break;
405f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
406f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
407f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
408f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
409f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
410f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
411f1b65df5SNoa Osherovich 		break;
412f1b65df5SNoa Osherovich 	default:
413f1b65df5SNoa Osherovich 		return -EINVAL;
414f1b65df5SNoa Osherovich 	}
415f1b65df5SNoa Osherovich 
416f1b65df5SNoa Osherovich 	return 0;
417f1b65df5SNoa Osherovich }
418f1b65df5SNoa Osherovich 
41908e8676fSAya Levin static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed,
42008e8676fSAya Levin 					u8 *active_width)
42108e8676fSAya Levin {
42208e8676fSAya Levin 	switch (eth_proto_oper) {
42308e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
42408e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
42508e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
42608e8676fSAya Levin 		*active_speed = IB_SPEED_SDR;
42708e8676fSAya Levin 		break;
42808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
42908e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
43008e8676fSAya Levin 		*active_speed = IB_SPEED_DDR;
43108e8676fSAya Levin 		break;
43208e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
43308e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
43408e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
43508e8676fSAya Levin 		break;
43608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
43708e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
43808e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
43908e8676fSAya Levin 		break;
44008e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
44108e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
44208e8676fSAya Levin 		*active_speed = IB_SPEED_EDR;
44308e8676fSAya Levin 		break;
44408e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
445cd272875SAya Levin 		*active_width = IB_WIDTH_2X;
446cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
447cd272875SAya Levin 		break;
44808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
44908e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
45008e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
45108e8676fSAya Levin 		break;
452cd272875SAya Levin 	case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
453cd272875SAya Levin 		*active_width = IB_WIDTH_4X;
454cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
455cd272875SAya Levin 		break;
45608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
45708e8676fSAya Levin 		*active_width = IB_WIDTH_2X;
45808e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
45908e8676fSAya Levin 		break;
46008e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
46108e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
46208e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
46308e8676fSAya Levin 		break;
46408e8676fSAya Levin 	default:
46508e8676fSAya Levin 		return -EINVAL;
46608e8676fSAya Levin 	}
46708e8676fSAya Levin 
46808e8676fSAya Levin 	return 0;
46908e8676fSAya Levin }
47008e8676fSAya Levin 
47108e8676fSAya Levin static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
47208e8676fSAya Levin 				    u8 *active_width, bool ext)
47308e8676fSAya Levin {
47408e8676fSAya Levin 	return ext ?
47508e8676fSAya Levin 		translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
47608e8676fSAya Levin 					     active_width) :
47708e8676fSAya Levin 		translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
47808e8676fSAya Levin 						active_width);
47908e8676fSAya Levin }
48008e8676fSAya Levin 
481095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
4823f89a643SAchiad Shochat 				struct ib_port_attr *props)
4833f89a643SAchiad Shochat {
4843f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
485bc4e12ffSAya Levin 	u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
486da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
48788621dfeSAviv Heller 	struct net_device *ndev, *upper;
4883f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
489b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
490c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
491f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
492b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
49308e8676fSAya Levin 	bool ext;
494095b0927SIlan Tayari 	int err;
4953f89a643SAchiad Shochat 
496b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
497b3cbd6f0SDaniel Jurgens 	if (!mdev) {
498b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
499b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
500b3cbd6f0SDaniel Jurgens 		 */
501b3cbd6f0SDaniel Jurgens 		put_mdev = false;
502b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
503b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
504b3cbd6f0SDaniel Jurgens 		port_num = 1;
505b3cbd6f0SDaniel Jurgens 	}
506b3cbd6f0SDaniel Jurgens 
507f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
508f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
50926628e2dSMark Bloch 	 * Use native port in case of reps
51050f22fd8SNoa Osherovich 	 */
51126628e2dSMark Bloch 	if (dev->is_rep)
51226628e2dSMark Bloch 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
51326628e2dSMark Bloch 					   1);
51426628e2dSMark Bloch 	else
515bc4e12ffSAya Levin 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
516b3cbd6f0SDaniel Jurgens 					   mdev_port_num);
517095b0927SIlan Tayari 	if (err)
518b3cbd6f0SDaniel Jurgens 		goto out;
51908e8676fSAya Levin 	ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet);
52008e8676fSAya Levin 	eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
521f1b65df5SNoa Osherovich 
5227672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
5237672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
5247672ed33SHonggang Li 
525f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
52608e8676fSAya Levin 				 &props->active_width, ext);
5273f89a643SAchiad Shochat 
5283f89a643SAchiad Shochat 	props->port_cap_flags |= IB_PORT_CM_SUP;
5292f944c0fSJason Gunthorpe 	props->ip_gids = true;
5303f89a643SAchiad Shochat 
5313f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
5323f89a643SAchiad Shochat 						roce_address_table_size);
5333f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
5343f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
5353f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
5363f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
5373f89a643SAchiad Shochat 	props->phys_state       = 3;
5383f89a643SAchiad Shochat 
539b3cbd6f0SDaniel Jurgens 	mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
540c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
5413f89a643SAchiad Shochat 
542b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
543b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
544b3cbd6f0SDaniel Jurgens 		goto out;
545b3cbd6f0SDaniel Jurgens 
5463f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
5473f89a643SAchiad Shochat 	if (!ndev)
548b3cbd6f0SDaniel Jurgens 		goto out;
5493f89a643SAchiad Shochat 
5507c34ec19SAviv Heller 	if (dev->lag_active) {
55188621dfeSAviv Heller 		rcu_read_lock();
55288621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
55388621dfeSAviv Heller 		if (upper) {
55488621dfeSAviv Heller 			dev_put(ndev);
55588621dfeSAviv Heller 			ndev = upper;
55688621dfeSAviv Heller 			dev_hold(ndev);
55788621dfeSAviv Heller 		}
55888621dfeSAviv Heller 		rcu_read_unlock();
55988621dfeSAviv Heller 	}
56088621dfeSAviv Heller 
5613f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
5623f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
5633f89a643SAchiad Shochat 		props->phys_state = 5;
5643f89a643SAchiad Shochat 	}
5653f89a643SAchiad Shochat 
5663f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
5673f89a643SAchiad Shochat 
5683f89a643SAchiad Shochat 	dev_put(ndev);
5693f89a643SAchiad Shochat 
5703f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
571b3cbd6f0SDaniel Jurgens out:
572b3cbd6f0SDaniel Jurgens 	if (put_mdev)
573b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
574b3cbd6f0SDaniel Jurgens 	return err;
5753f89a643SAchiad Shochat }
5763f89a643SAchiad Shochat 
577095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
578095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
579095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
5803cca2606SAchiad Shochat {
581095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
582a70c0739SParav Pandit 	u16 vlan_id = 0xffff;
583095b0927SIlan Tayari 	u8 roce_version = 0;
584095b0927SIlan Tayari 	u8 roce_l3_type = 0;
585095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
586a70c0739SParav Pandit 	int ret;
5873cca2606SAchiad Shochat 
588095b0927SIlan Tayari 	if (gid) {
589095b0927SIlan Tayari 		gid_type = attr->gid_type;
590a70c0739SParav Pandit 		ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
591a70c0739SParav Pandit 		if (ret)
592a70c0739SParav Pandit 			return ret;
5933cca2606SAchiad Shochat 	}
5943cca2606SAchiad Shochat 
595095b0927SIlan Tayari 	switch (gid_type) {
5963cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
597095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
5983cca2606SAchiad Shochat 		break;
5993cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
600095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
601095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
602095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
603095b0927SIlan Tayari 		else
604095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
6053cca2606SAchiad Shochat 		break;
6063cca2606SAchiad Shochat 
6073cca2606SAchiad Shochat 	default:
608095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
6093cca2606SAchiad Shochat 	}
6103cca2606SAchiad Shochat 
611095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
612cf34e1feSParav Pandit 				      roce_l3_type, gid->raw, mac,
613a70c0739SParav Pandit 				      vlan_id < VLAN_CFI_MASK, vlan_id,
614cf34e1feSParav Pandit 				      port_num);
6153cca2606SAchiad Shochat }
6163cca2606SAchiad Shochat 
617f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
6183cca2606SAchiad Shochat 			   __always_unused void **context)
6193cca2606SAchiad Shochat {
620414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
621f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
6223cca2606SAchiad Shochat }
6233cca2606SAchiad Shochat 
624414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
625414448d2SParav Pandit 			   __always_unused void **context)
6263cca2606SAchiad Shochat {
627414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
628414448d2SParav Pandit 			     attr->index, NULL, NULL);
6293cca2606SAchiad Shochat }
6303cca2606SAchiad Shochat 
63147ec3866SParav Pandit __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
63247ec3866SParav Pandit 			       const struct ib_gid_attr *attr)
6332811ba51SAchiad Shochat {
63447ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
6352811ba51SAchiad Shochat 		return 0;
6362811ba51SAchiad Shochat 
6372811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
6382811ba51SAchiad Shochat }
6392811ba51SAchiad Shochat 
6401b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
6411b5daf11SMajd Dibbiny {
6427fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
643d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
6447fae6655SNoa Osherovich 	return 0;
6451b5daf11SMajd Dibbiny }
6461b5daf11SMajd Dibbiny 
6471b5daf11SMajd Dibbiny enum {
6481b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
6491b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
6501b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
6511b5daf11SMajd Dibbiny };
6521b5daf11SMajd Dibbiny 
6531b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
6541b5daf11SMajd Dibbiny {
6551b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
6561b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
6571b5daf11SMajd Dibbiny 
658ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
6591b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
6601b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
6611b5daf11SMajd Dibbiny 
6621b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
6631b5daf11SMajd Dibbiny }
6641b5daf11SMajd Dibbiny 
665da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
666776a3906SMoni Shoua 			    u8 atomic_size_qp,
667da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
668da7525d2SEran Ben Elisha {
669da7525d2SEran Ben Elisha 	u8 tmp;
670da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
671da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
672bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
673da7525d2SEran Ben Elisha 
674da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
675da7525d2SEran Ben Elisha 	 * of host endianness respond
676da7525d2SEran Ben Elisha 	 */
677da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
678da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
679da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
680da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
681da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
682da7525d2SEran Ben Elisha 	} else {
683da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
684da7525d2SEran Ben Elisha 	}
685da7525d2SEran Ben Elisha }
686da7525d2SEran Ben Elisha 
687776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
688776a3906SMoni Shoua 			       struct ib_device_attr *props)
689776a3906SMoni Shoua {
690776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
691776a3906SMoni Shoua 
692776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
693776a3906SMoni Shoua }
694776a3906SMoni Shoua 
695776a3906SMoni Shoua static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
696776a3906SMoni Shoua 			       struct ib_device_attr *props)
697776a3906SMoni Shoua {
698776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
699776a3906SMoni Shoua 
700776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
701776a3906SMoni Shoua }
702776a3906SMoni Shoua 
703776a3906SMoni Shoua bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
704776a3906SMoni Shoua {
705776a3906SMoni Shoua 	struct ib_device_attr props = {};
706776a3906SMoni Shoua 
707776a3906SMoni Shoua 	get_atomic_caps_dc(dev, &props);
708776a3906SMoni Shoua 	return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
709776a3906SMoni Shoua }
7101b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
7111b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
7121b5daf11SMajd Dibbiny {
7131b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7141b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7151b5daf11SMajd Dibbiny 	u64 tmp;
7161b5daf11SMajd Dibbiny 	int err;
7171b5daf11SMajd Dibbiny 
7181b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7191b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7201b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
7211b5daf11SMajd Dibbiny 							    sys_image_guid);
7221b5daf11SMajd Dibbiny 
7231b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7241b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
7253f89a643SAchiad Shochat 		break;
7263f89a643SAchiad Shochat 
7273f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7283f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
7293f89a643SAchiad Shochat 		break;
7301b5daf11SMajd Dibbiny 
7311b5daf11SMajd Dibbiny 	default:
7321b5daf11SMajd Dibbiny 		return -EINVAL;
7331b5daf11SMajd Dibbiny 	}
7343f89a643SAchiad Shochat 
7353f89a643SAchiad Shochat 	if (!err)
7363f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
7373f89a643SAchiad Shochat 
7383f89a643SAchiad Shochat 	return err;
7393f89a643SAchiad Shochat 
7401b5daf11SMajd Dibbiny }
7411b5daf11SMajd Dibbiny 
7421b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
7431b5daf11SMajd Dibbiny 				u16 *max_pkeys)
7441b5daf11SMajd Dibbiny {
7451b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7461b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7471b5daf11SMajd Dibbiny 
7481b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7491b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7501b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
7511b5daf11SMajd Dibbiny 
7521b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7531b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7541b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
7551b5daf11SMajd Dibbiny 						pkey_table_size));
7561b5daf11SMajd Dibbiny 		return 0;
7571b5daf11SMajd Dibbiny 
7581b5daf11SMajd Dibbiny 	default:
7591b5daf11SMajd Dibbiny 		return -EINVAL;
7601b5daf11SMajd Dibbiny 	}
7611b5daf11SMajd Dibbiny }
7621b5daf11SMajd Dibbiny 
7631b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
7641b5daf11SMajd Dibbiny 				u32 *vendor_id)
7651b5daf11SMajd Dibbiny {
7661b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7671b5daf11SMajd Dibbiny 
7681b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7691b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7701b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
7711b5daf11SMajd Dibbiny 
7721b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7731b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7741b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
7751b5daf11SMajd Dibbiny 
7761b5daf11SMajd Dibbiny 	default:
7771b5daf11SMajd Dibbiny 		return -EINVAL;
7781b5daf11SMajd Dibbiny 	}
7791b5daf11SMajd Dibbiny }
7801b5daf11SMajd Dibbiny 
7811b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
7821b5daf11SMajd Dibbiny 				__be64 *node_guid)
7831b5daf11SMajd Dibbiny {
7841b5daf11SMajd Dibbiny 	u64 tmp;
7851b5daf11SMajd Dibbiny 	int err;
7861b5daf11SMajd Dibbiny 
7871b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7881b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7891b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7901b5daf11SMajd Dibbiny 
7911b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7921b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7933f89a643SAchiad Shochat 		break;
7943f89a643SAchiad Shochat 
7953f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7963f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7973f89a643SAchiad Shochat 		break;
7981b5daf11SMajd Dibbiny 
7991b5daf11SMajd Dibbiny 	default:
8001b5daf11SMajd Dibbiny 		return -EINVAL;
8011b5daf11SMajd Dibbiny 	}
8023f89a643SAchiad Shochat 
8033f89a643SAchiad Shochat 	if (!err)
8043f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
8053f89a643SAchiad Shochat 
8063f89a643SAchiad Shochat 	return err;
8071b5daf11SMajd Dibbiny }
8081b5daf11SMajd Dibbiny 
8091b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
810bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
8111b5daf11SMajd Dibbiny };
8121b5daf11SMajd Dibbiny 
8131b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
8141b5daf11SMajd Dibbiny {
8151b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
8161b5daf11SMajd Dibbiny 
8171b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
8181b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
8191b5daf11SMajd Dibbiny 
8201b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
8211b5daf11SMajd Dibbiny 
8221b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
8231b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
8241b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
8251b5daf11SMajd Dibbiny }
8261b5daf11SMajd Dibbiny 
827e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
8282528e33eSMatan Barak 				struct ib_device_attr *props,
8292528e33eSMatan Barak 				struct ib_udata *uhw)
830e126ba97SEli Cohen {
831e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
832938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
833e126ba97SEli Cohen 	int err = -ENOMEM;
834288c01b7SEli Cohen 	int max_sq_desc;
835e126ba97SEli Cohen 	int max_rq_sg;
836e126ba97SEli Cohen 	int max_sq_sg;
837e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
83885c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
839402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
840402ca536SBodong Wang 	size_t resp_len;
841402ca536SBodong Wang 	u64 max_tso;
842e126ba97SEli Cohen 
843402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
844402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
845402ca536SBodong Wang 		return -EINVAL;
846402ca536SBodong Wang 	else
847402ca536SBodong Wang 		resp.response_length = resp_len;
848402ca536SBodong Wang 
849402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
8502528e33eSMatan Barak 		return -EINVAL;
8512528e33eSMatan Barak 
852e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
8531b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
8541b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
8551b5daf11SMajd Dibbiny 	if (err)
8561b5daf11SMajd Dibbiny 		return err;
8571b5daf11SMajd Dibbiny 
8581b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
8591b5daf11SMajd Dibbiny 	if (err)
8601b5daf11SMajd Dibbiny 		return err;
8611b5daf11SMajd Dibbiny 
8621b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
8631b5daf11SMajd Dibbiny 	if (err)
8641b5daf11SMajd Dibbiny 		return err;
865e126ba97SEli Cohen 
8669603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
8679603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
8689603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
869e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
870e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
871e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
8721a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
873938fe83cSSaeed Mahameed 
874938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
875e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
876938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
877e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
878938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
879e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
880938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
881e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
882d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
883d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
884d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
885d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
886b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
887b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
888d2370e0aSMatan Barak 	}
889e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
890938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
8912dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
8922dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8932dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8942dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8952dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8962dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8972dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8982dea9094SSagi Grimberg 	}
899938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
900f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
901e126ba97SEli Cohen 
90285c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
903e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
904e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
90588115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
906e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
907e8161334SNoa Osherovich 		}
908e8161334SNoa Osherovich 
909e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
910e8161334SNoa Osherovich 			props->raw_packet_caps |=
911e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
91288115fe7SBodong Wang 
913402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
914402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
915402ca536SBodong Wang 			if (max_tso) {
916402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
917402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
918402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
919402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
920402ca536SBodong Wang 			}
921402ca536SBodong Wang 		}
92231f69a82SYishai Hadas 
92331f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
92431f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
92531f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
92631f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
92731f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
92831f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
92931f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
93031f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
93131f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
93231f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
93331f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
9344e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
9354e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
9362d93fc85SMatan Barak 			if (mlx5_accel_ipsec_device_caps(dev->mdev) &
9372d93fc85SMatan Barak 			    MLX5_ACCEL_IPSEC_CAP_DEVICE)
9382d93fc85SMatan Barak 				resp.rss_caps.rx_hash_fields_mask |=
9392d93fc85SMatan Barak 					MLX5_RX_HASH_IPSEC_SPI;
94031f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
94131f69a82SYishai Hadas 		}
94231f69a82SYishai Hadas 	} else {
94331f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
94431f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
94531f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
94631f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
947402ca536SBodong Wang 	}
948402ca536SBodong Wang 
949f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
950f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
951f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
952f0313965SErez Shitrit 	}
953f0313965SErez Shitrit 
95403404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
95585c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
95685c7c014SDaniel Jurgens 	    raw_support)
95703404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
95803404e8aSMaor Gottlieb 
9591d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
9601d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
9611d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
9621d54f890SYishai Hadas 
963cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
96485c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
96585c7c014SDaniel Jurgens 	    raw_support) {
966e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
967cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
968e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
969e8161334SNoa Osherovich 	}
970cff5a0f3SMajd Dibbiny 
97124da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
97224da0016SAriel Levkovich 		props->max_dm_size =
97324da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
97424da0016SAriel Levkovich 	}
97524da0016SAriel Levkovich 
976da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
977da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
978da6d6ba3SMaor Gottlieb 
979b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
980b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
981b1383aa6SNoa Osherovich 
9821b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
9831b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
984e126ba97SEli Cohen 
985e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
986e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
987938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
988938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
989938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
990938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
991288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
992288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
993288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
994e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
99533023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
99633023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
997986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
998938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9999f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
1000938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
1001938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
1002938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
1003938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
1004938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
1005938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
1006938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
1007e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
1008e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
1009911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
1010911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
1011776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
101281bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
1013938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1014938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
1015e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1016e126ba97SEli Cohen 					   props->max_mcast_grp;
1017e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
101886695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
10197c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
10207c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
1021e126ba97SEli Cohen 
1022e502b8b0SLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
1023938fe83cSSaeed Mahameed 		if (MLX5_CAP_GEN(mdev, pg))
10248cdd312cSHaggai Eran 			props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
10258cdd312cSHaggai Eran 		props->odp_caps = dev->odp_caps;
1026e502b8b0SLeon Romanovsky 	}
10278cdd312cSHaggai Eran 
1028051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
1029051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1030051f2630SLeon Romanovsky 
1031eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
1032eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1033eff901d3SEli Cohen 
103431f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
103585c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
103631f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
103731f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
103831f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
103931f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
104031f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
104131f69a82SYishai Hadas 		props->max_wq_type_rq =
104231f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
104331f69a82SYishai Hadas 	}
104431f69a82SYishai Hadas 
1045eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
104678b1beb0SLeon Romanovsky 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
104778b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
1048eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
104978b1beb0SLeon Romanovsky 		props->tm_caps.flags = IB_TM_CAP_RC;
105078b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
1051eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
105278b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1053eb761894SArtemy Kovalyov 	}
1054eb761894SArtemy Kovalyov 
105587ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
105687ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
105787ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
105887ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
105987ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
106087ab3f52SYonatan Cohen 	}
106187ab3f52SYonatan Cohen 
10627e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
1063572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
1064572f46bfSYonatan Cohen 
1065572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
10667e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
1067572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
1068572f46bfSYonatan Cohen 					     cqe_compression_max_num);
1069572f46bfSYonatan Cohen 
10707e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
10717e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
10727e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
10736f1006a4SYonatan Cohen 
10746f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
10756f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
10766f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1077572f46bfSYonatan Cohen 		}
10787e43a2a5SBodong Wang 	}
10797e43a2a5SBodong Wang 
108085c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
108185c7c014SDaniel Jurgens 	    raw_support) {
1082d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1083d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
1084d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
1085d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1086d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
1087d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1088d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
1089d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
109061147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
109161147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
109261147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
109361147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
1094d949167dSBodong Wang 		}
1095d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1096d949167dSBodong Wang 	}
1097d949167dSBodong Wang 
10989f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
10999f885201SLeon Romanovsky 			uhw->outlen)) {
1100795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
11019f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1102795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1103050da902SBodong Wang 
1104050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1105050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1106050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1107050da902SBodong Wang 
11089f885201SLeon Romanovsky 		resp.response_length +=
11099f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
11109f885201SLeon Romanovsky 	}
11119f885201SLeon Romanovsky 
1112de57f2adSGuy Levi 	if (field_avail(typeof(resp), flags, uhw->outlen)) {
1113de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
11147a0c8f42SGuy Levi 
1115de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1116de57f2adSGuy Levi 			resp.flags |=
1117de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
11187a0c8f42SGuy Levi 
11197a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
11207a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
11217e11b911SDanit Goldberg 		if (MLX5_CAP_GEN(mdev, qp_packet_based))
11227e11b911SDanit Goldberg 			resp.flags |=
11237e11b911SDanit Goldberg 				MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
11247249c8eaSGuy Levi 
11257249c8eaSGuy Levi 		resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1126de57f2adSGuy Levi 	}
11279f885201SLeon Romanovsky 
112896dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
112996dc3fc5SNoa Osherovich 			uhw->outlen)) {
113096dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
113196dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
113296dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
113396dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
113496dc3fc5SNoa Osherovich 
113596dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
113696dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
113796dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
113896dc3fc5SNoa Osherovich 
113996dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
114096dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
114196dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
114296dc3fc5SNoa Osherovich 
114396dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
114496dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
114596dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
114696dc3fc5SNoa Osherovich 		}
114796dc3fc5SNoa Osherovich 	}
114896dc3fc5SNoa Osherovich 
114985c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
115085c7c014SDaniel Jurgens 	    raw_support) {
1151b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1152b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1153b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1154b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1155b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1156b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1157b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1158b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1159b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1160b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1161b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1162b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1163b4f34597SNoa Osherovich 		}
1164b4f34597SNoa Osherovich 	}
1165b4f34597SNoa Osherovich 
1166f95ef6cbSMaor Gottlieb 	if (field_avail(typeof(resp), tunnel_offloads_caps,
1167f95ef6cbSMaor Gottlieb 			uhw->outlen)) {
1168f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1169f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1170f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1171f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1172f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1173f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1174f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1175f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1176f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1177f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
1178e818e255SAriel Levkovich 		if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1179e818e255SAriel Levkovich 		    MLX5_FLEX_PROTO_CW_MPLS_GRE)
1180e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1181e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1182e818e255SAriel Levkovich 		if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1183e818e255SAriel Levkovich 		    MLX5_FLEX_PROTO_CW_MPLS_UDP)
1184e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1185e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1186f95ef6cbSMaor Gottlieb 	}
1187f95ef6cbSMaor Gottlieb 
1188402ca536SBodong Wang 	if (uhw->outlen) {
1189402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1190402ca536SBodong Wang 
1191402ca536SBodong Wang 		if (err)
1192402ca536SBodong Wang 			return err;
1193402ca536SBodong Wang 	}
1194402ca536SBodong Wang 
11951b5daf11SMajd Dibbiny 	return 0;
11961b5daf11SMajd Dibbiny }
1197e126ba97SEli Cohen 
11981b5daf11SMajd Dibbiny enum mlx5_ib_width {
11991b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
12001b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
12011b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
12021b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
12031b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
12041b5daf11SMajd Dibbiny };
12051b5daf11SMajd Dibbiny 
1206db7a691aSMichael Guralnik static void translate_active_width(struct ib_device *ibdev, u8 active_width,
12071b5daf11SMajd Dibbiny 				  u8 *ib_width)
12081b5daf11SMajd Dibbiny {
12091b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12101b5daf11SMajd Dibbiny 
1211db7a691aSMichael Guralnik 	if (active_width & MLX5_IB_WIDTH_1X)
12121b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1213d764970bSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_2X)
1214d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1215db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_4X)
12161b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1217db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_8X)
12181b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1219db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_12X)
12201b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1221db7a691aSMichael Guralnik 	else {
1222db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
12231b5daf11SMajd Dibbiny 			    (int)active_width);
1224db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
12251b5daf11SMajd Dibbiny 	}
12261b5daf11SMajd Dibbiny 
1227db7a691aSMichael Guralnik 	return;
12281b5daf11SMajd Dibbiny }
12291b5daf11SMajd Dibbiny 
12301b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
12311b5daf11SMajd Dibbiny {
12321b5daf11SMajd Dibbiny 	switch (mtu) {
12331b5daf11SMajd Dibbiny 	case 256: return 1;
12341b5daf11SMajd Dibbiny 	case 512: return 2;
12351b5daf11SMajd Dibbiny 	case 1024: return 3;
12361b5daf11SMajd Dibbiny 	case 2048: return 4;
12371b5daf11SMajd Dibbiny 	case 4096: return 5;
12381b5daf11SMajd Dibbiny 	default:
12391b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
12401b5daf11SMajd Dibbiny 		return -1;
12411b5daf11SMajd Dibbiny 	}
12421b5daf11SMajd Dibbiny }
12431b5daf11SMajd Dibbiny 
12441b5daf11SMajd Dibbiny enum ib_max_vl_num {
12451b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
12461b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
12471b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
12481b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
12491b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
12501b5daf11SMajd Dibbiny };
12511b5daf11SMajd Dibbiny 
12521b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
12531b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
12541b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
12551b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
12561b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
12571b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
12581b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
12591b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
12601b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
12611b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
12621b5daf11SMajd Dibbiny };
12631b5daf11SMajd Dibbiny 
12641b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
12651b5daf11SMajd Dibbiny 				u8 *max_vl_num)
12661b5daf11SMajd Dibbiny {
12671b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
12681b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
12691b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
12701b5daf11SMajd Dibbiny 		break;
12711b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
12721b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
12731b5daf11SMajd Dibbiny 		break;
12741b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
12751b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
12761b5daf11SMajd Dibbiny 		break;
12771b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
12781b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
12791b5daf11SMajd Dibbiny 		break;
12801b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
12811b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
12821b5daf11SMajd Dibbiny 		break;
12831b5daf11SMajd Dibbiny 
12841b5daf11SMajd Dibbiny 	default:
12851b5daf11SMajd Dibbiny 		return -EINVAL;
12861b5daf11SMajd Dibbiny 	}
12871b5daf11SMajd Dibbiny 
12881b5daf11SMajd Dibbiny 	return 0;
12891b5daf11SMajd Dibbiny }
12901b5daf11SMajd Dibbiny 
12911b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
12921b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
12931b5daf11SMajd Dibbiny {
12941b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12951b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
12961b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1297046339eaSSaeed Mahameed 	u16 max_mtu;
1298046339eaSSaeed Mahameed 	u16 oper_mtu;
12991b5daf11SMajd Dibbiny 	int err;
13001b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
13011b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
13021b5daf11SMajd Dibbiny 
13031b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
13041b5daf11SMajd Dibbiny 	if (!rep) {
13051b5daf11SMajd Dibbiny 		err = -ENOMEM;
13061b5daf11SMajd Dibbiny 		goto out;
13071b5daf11SMajd Dibbiny 	}
13081b5daf11SMajd Dibbiny 
1309c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
13101b5daf11SMajd Dibbiny 
13111b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
13121b5daf11SMajd Dibbiny 	if (err)
13131b5daf11SMajd Dibbiny 		goto out;
13141b5daf11SMajd Dibbiny 
13151b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
13161b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
13171b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
13181b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
13191b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
13201b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
13211b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
13221b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
13231b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
13241b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
13251b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
13261b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
13271b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
13281b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
13291b5daf11SMajd Dibbiny 
13304106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
13314106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
13324106a758SMichael Guralnik 
13331b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
13341b5daf11SMajd Dibbiny 	if (err)
13351b5daf11SMajd Dibbiny 		goto out;
13361b5daf11SMajd Dibbiny 
1337db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1338db7a691aSMichael Guralnik 
1339d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
13401b5daf11SMajd Dibbiny 	if (err)
13411b5daf11SMajd Dibbiny 		goto out;
13421b5daf11SMajd Dibbiny 
1343facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
13441b5daf11SMajd Dibbiny 
13451b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
13461b5daf11SMajd Dibbiny 
1347facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
13481b5daf11SMajd Dibbiny 
13491b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
13501b5daf11SMajd Dibbiny 
13511b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
13521b5daf11SMajd Dibbiny 	if (err)
13531b5daf11SMajd Dibbiny 		goto out;
13541b5daf11SMajd Dibbiny 
13551b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
13561b5daf11SMajd Dibbiny 				   &props->max_vl_num);
13571b5daf11SMajd Dibbiny out:
13581b5daf11SMajd Dibbiny 	kfree(rep);
1359e126ba97SEli Cohen 	return err;
1360e126ba97SEli Cohen }
1361e126ba97SEli Cohen 
1362e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1363e126ba97SEli Cohen 		       struct ib_port_attr *props)
1364e126ba97SEli Cohen {
1365095b0927SIlan Tayari 	unsigned int count;
1366095b0927SIlan Tayari 	int ret;
1367095b0927SIlan Tayari 
13681b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13691b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1370095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1371095b0927SIlan Tayari 		break;
1372e126ba97SEli Cohen 
13731b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1374095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1375095b0927SIlan Tayari 		break;
13761b5daf11SMajd Dibbiny 
13773f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1378095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1379095b0927SIlan Tayari 		break;
13803f89a643SAchiad Shochat 
13811b5daf11SMajd Dibbiny 	default:
1382095b0927SIlan Tayari 		ret = -EINVAL;
1383e126ba97SEli Cohen 	}
1384095b0927SIlan Tayari 
1385095b0927SIlan Tayari 	if (!ret && props) {
1386b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1387b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1388b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1389b3cbd6f0SDaniel Jurgens 
1390b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1391b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1392b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1393b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1394b3cbd6f0SDaniel Jurgens 			 */
1395b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1396b3cbd6f0SDaniel Jurgens 			port = 1;
1397b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1398b3cbd6f0SDaniel Jurgens 		}
1399b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1400b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1401b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1402095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1403095b0927SIlan Tayari 	}
1404095b0927SIlan Tayari 	return ret;
1405e126ba97SEli Cohen }
1406e126ba97SEli Cohen 
14078e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
14088e6efa3aSMark Bloch 				  struct ib_port_attr *props)
14098e6efa3aSMark Bloch {
14108e6efa3aSMark Bloch 	int ret;
14118e6efa3aSMark Bloch 
141226628e2dSMark Bloch 	/* Only link layer == ethernet is valid for representors
141326628e2dSMark Bloch 	 * and we always use port 1
141426628e2dSMark Bloch 	 */
14158e6efa3aSMark Bloch 	ret = mlx5_query_port_roce(ibdev, port, props);
14168e6efa3aSMark Bloch 	if (ret || !props)
14178e6efa3aSMark Bloch 		return ret;
14188e6efa3aSMark Bloch 
14198e6efa3aSMark Bloch 	/* We don't support GIDS */
14208e6efa3aSMark Bloch 	props->gid_tbl_len = 0;
14218e6efa3aSMark Bloch 
14228e6efa3aSMark Bloch 	return ret;
14238e6efa3aSMark Bloch }
14248e6efa3aSMark Bloch 
1425e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1426e126ba97SEli Cohen 			     union ib_gid *gid)
1427e126ba97SEli Cohen {
14281b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
14291b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1430e126ba97SEli Cohen 
14311b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14321b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14331b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1434e126ba97SEli Cohen 
14351b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14361b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1437e126ba97SEli Cohen 
14381b5daf11SMajd Dibbiny 	default:
14391b5daf11SMajd Dibbiny 		return -EINVAL;
14401b5daf11SMajd Dibbiny 	}
1441e126ba97SEli Cohen 
1442e126ba97SEli Cohen }
1443e126ba97SEli Cohen 
1444b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1445b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1446b3cbd6f0SDaniel Jurgens {
1447b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1448b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1449b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1450b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1451b3cbd6f0SDaniel Jurgens 	int err;
1452b3cbd6f0SDaniel Jurgens 
1453b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1454b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1455b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1456b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1457b3cbd6f0SDaniel Jurgens 		 */
1458b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1459b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1460b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1461b3cbd6f0SDaniel Jurgens 	}
1462b3cbd6f0SDaniel Jurgens 
1463b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1464b3cbd6f0SDaniel Jurgens 					index, pkey);
1465b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1466b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1467b3cbd6f0SDaniel Jurgens 
1468b3cbd6f0SDaniel Jurgens 	return err;
1469b3cbd6f0SDaniel Jurgens }
1470b3cbd6f0SDaniel Jurgens 
1471e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1472e126ba97SEli Cohen 			      u16 *pkey)
1473e126ba97SEli Cohen {
14741b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14751b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14761b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1477e126ba97SEli Cohen 
14781b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14791b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1480b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
14811b5daf11SMajd Dibbiny 	default:
14821b5daf11SMajd Dibbiny 		return -EINVAL;
1483e126ba97SEli Cohen 	}
14841b5daf11SMajd Dibbiny }
1485e126ba97SEli Cohen 
1486e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1487e126ba97SEli Cohen 				 struct ib_device_modify *props)
1488e126ba97SEli Cohen {
1489e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1490e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1491e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1492e126ba97SEli Cohen 	int err;
1493e126ba97SEli Cohen 
1494e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1495e126ba97SEli Cohen 		return -EOPNOTSUPP;
1496e126ba97SEli Cohen 
1497e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1498e126ba97SEli Cohen 		return 0;
1499e126ba97SEli Cohen 
1500e126ba97SEli Cohen 	/*
1501e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1502e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1503e126ba97SEli Cohen 	 */
1504bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
15059603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1506e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1507e126ba97SEli Cohen 	if (err)
1508e126ba97SEli Cohen 		return err;
1509e126ba97SEli Cohen 
1510bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1511e126ba97SEli Cohen 
1512e126ba97SEli Cohen 	return err;
1513e126ba97SEli Cohen }
1514e126ba97SEli Cohen 
1515cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1516cdbe33d0SEli Cohen 				u32 value)
1517cdbe33d0SEli Cohen {
1518cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1519b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1520b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1521cdbe33d0SEli Cohen 	int err;
1522cdbe33d0SEli Cohen 
1523b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1524b3cbd6f0SDaniel Jurgens 	if (!mdev)
1525b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1526b3cbd6f0SDaniel Jurgens 
1527b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1528cdbe33d0SEli Cohen 	if (err)
1529b3cbd6f0SDaniel Jurgens 		goto out;
1530cdbe33d0SEli Cohen 
1531cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1532cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1533cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1534b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1535b3cbd6f0SDaniel Jurgens 		goto out;
1536cdbe33d0SEli Cohen 	}
1537cdbe33d0SEli Cohen 
1538cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1539cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1540b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1541b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1542b3cbd6f0SDaniel Jurgens 
1543b3cbd6f0SDaniel Jurgens out:
1544b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1545cdbe33d0SEli Cohen 
1546cdbe33d0SEli Cohen 	return err;
1547cdbe33d0SEli Cohen }
1548cdbe33d0SEli Cohen 
1549e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1550e126ba97SEli Cohen 			       struct ib_port_modify *props)
1551e126ba97SEli Cohen {
1552e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1553e126ba97SEli Cohen 	struct ib_port_attr attr;
1554e126ba97SEli Cohen 	u32 tmp;
1555e126ba97SEli Cohen 	int err;
1556cdbe33d0SEli Cohen 	u32 change_mask;
1557cdbe33d0SEli Cohen 	u32 value;
1558cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1559cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1560cdbe33d0SEli Cohen 
1561ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1562ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1563ec255879SMajd Dibbiny 	 */
1564ec255879SMajd Dibbiny 	if (!is_ib)
1565ec255879SMajd Dibbiny 		return 0;
1566ec255879SMajd Dibbiny 
1567cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1568cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1569cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1570cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1571cdbe33d0SEli Cohen 	}
1572e126ba97SEli Cohen 
1573e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1574e126ba97SEli Cohen 
1575c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1576e126ba97SEli Cohen 	if (err)
1577e126ba97SEli Cohen 		goto out;
1578e126ba97SEli Cohen 
1579e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1580e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1581e126ba97SEli Cohen 
15829603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1583e126ba97SEli Cohen 
1584e126ba97SEli Cohen out:
1585e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1586e126ba97SEli Cohen 	return err;
1587e126ba97SEli Cohen }
1588e126ba97SEli Cohen 
158930aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
159030aa60b3SEli Cohen {
159130aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
159230aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
159330aa60b3SEli Cohen }
159430aa60b3SEli Cohen 
159531a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
159631a78a5aSYishai Hadas {
159731a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
159831a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
159931a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
160031a78a5aSYishai Hadas 
160131a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
160231a78a5aSYishai Hadas }
160331a78a5aSYishai Hadas 
1604b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1605b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
160631a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1607b037c29aSEli Cohen {
1608b037c29aSEli Cohen 	int uars_per_sys_page;
1609b037c29aSEli Cohen 	int bfregs_per_sys_page;
1610b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1611b037c29aSEli Cohen 
1612b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1613b037c29aSEli Cohen 		return -EINVAL;
1614b037c29aSEli Cohen 
1615b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1616b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1617b037c29aSEli Cohen 
1618b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1619b037c29aSEli Cohen 		return -ENOMEM;
1620b037c29aSEli Cohen 
1621b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1622b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
162331a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1624b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1625b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1626b037c29aSEli Cohen 		return -EINVAL;
1627b037c29aSEli Cohen 
162831a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
162931a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
163031a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
163131a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
163231a78a5aSYishai Hadas 
163331a78a5aSYishai 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",
1634b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1635b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
163631a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
163731a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1638b037c29aSEli Cohen 
1639b037c29aSEli Cohen 	return 0;
1640b037c29aSEli Cohen }
1641b037c29aSEli Cohen 
1642b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1643b037c29aSEli Cohen {
1644b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1645b037c29aSEli Cohen 	int err;
1646b037c29aSEli Cohen 	int i;
1647b037c29aSEli Cohen 
1648b037c29aSEli Cohen 	bfregi = &context->bfregi;
164931a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1650b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1651b037c29aSEli Cohen 		if (err)
1652b037c29aSEli Cohen 			goto error;
1653b037c29aSEli Cohen 
1654b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1655b037c29aSEli Cohen 	}
16564ed131d0SYishai Hadas 
16574ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
16584ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
16594ed131d0SYishai Hadas 
1660b037c29aSEli Cohen 	return 0;
1661b037c29aSEli Cohen 
1662b037c29aSEli Cohen error:
1663b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1664b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1665b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1666b037c29aSEli Cohen 
1667b037c29aSEli Cohen 	return err;
1668b037c29aSEli Cohen }
1669b037c29aSEli Cohen 
167015177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
167115177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1672b037c29aSEli Cohen {
1673b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1674b037c29aSEli Cohen 	int i;
1675b037c29aSEli Cohen 
1676b037c29aSEli Cohen 	bfregi = &context->bfregi;
167715177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
16784ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
167915177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
168015177999SLeon Romanovsky 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1681b037c29aSEli Cohen }
1682b037c29aSEli Cohen 
16830042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1684a560f1d9SMark Bloch {
1685a560f1d9SMark Bloch 	int err = 0;
1686a560f1d9SMark Bloch 
1687a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16880042f9e4SMark Bloch 	if (td)
1689a560f1d9SMark Bloch 		dev->lb.user_td++;
16900042f9e4SMark Bloch 	if (qp)
16910042f9e4SMark Bloch 		dev->lb.qps++;
1692a560f1d9SMark Bloch 
16930042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
16940042f9e4SMark Bloch 	    dev->lb.qps == 1) {
16950042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1696a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
16970042f9e4SMark Bloch 			dev->lb.enabled = true;
16980042f9e4SMark Bloch 		}
16990042f9e4SMark Bloch 	}
1700a560f1d9SMark Bloch 
1701a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1702a560f1d9SMark Bloch 
1703a560f1d9SMark Bloch 	return err;
1704a560f1d9SMark Bloch }
1705a560f1d9SMark Bloch 
17060042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1707a560f1d9SMark Bloch {
1708a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
17090042f9e4SMark Bloch 	if (td)
1710a560f1d9SMark Bloch 		dev->lb.user_td--;
17110042f9e4SMark Bloch 	if (qp)
17120042f9e4SMark Bloch 		dev->lb.qps--;
1713a560f1d9SMark Bloch 
17140042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
17150042f9e4SMark Bloch 	    dev->lb.qps == 0) {
17160042f9e4SMark Bloch 		if (dev->lb.enabled) {
1717a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
17180042f9e4SMark Bloch 			dev->lb.enabled = false;
17190042f9e4SMark Bloch 		}
17200042f9e4SMark Bloch 	}
1721a560f1d9SMark Bloch 
1722a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1723a560f1d9SMark Bloch }
1724a560f1d9SMark Bloch 
1725d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1726d2d19121SYishai Hadas 					  u16 uid)
1727c85023e1SHuy Nguyen {
1728c85023e1SHuy Nguyen 	int err;
1729c85023e1SHuy Nguyen 
1730cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1731cfdeb893SLeon Romanovsky 		return 0;
1732cfdeb893SLeon Romanovsky 
1733d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1734c85023e1SHuy Nguyen 	if (err)
1735c85023e1SHuy Nguyen 		return err;
1736c85023e1SHuy Nguyen 
1737c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17388978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17398978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1740c85023e1SHuy Nguyen 		return err;
1741c85023e1SHuy Nguyen 
17420042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1743c85023e1SHuy Nguyen }
1744c85023e1SHuy Nguyen 
1745d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1746d2d19121SYishai Hadas 					     u16 uid)
1747c85023e1SHuy Nguyen {
1748cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1749cfdeb893SLeon Romanovsky 		return;
1750cfdeb893SLeon Romanovsky 
1751d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1752c85023e1SHuy Nguyen 
1753c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17548978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17558978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1756c85023e1SHuy Nguyen 		return;
1757c85023e1SHuy Nguyen 
17580042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1759c85023e1SHuy Nguyen }
1760c85023e1SHuy Nguyen 
1761a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1762e126ba97SEli Cohen 				  struct ib_udata *udata)
1763e126ba97SEli Cohen {
1764a2a074efSLeon Romanovsky 	struct ib_device *ibdev = uctx->device;
1765e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1766b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1767b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
17685c99eaecSFeras Daoud 	struct mlx5_core_dev *mdev = dev->mdev;
1769a2a074efSLeon Romanovsky 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
17702f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
177178c0f98cSEli Cohen 	int ver;
1772e126ba97SEli Cohen 	int err;
1773a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1774a168a41cSMajd Dibbiny 				     max_cqe_version);
177525bb36e7SYonatan Cohen 	u32 dump_fill_mkey;
1776b037c29aSEli Cohen 	bool lib_uar_4k;
1777e126ba97SEli Cohen 
1778e126ba97SEli Cohen 	if (!dev->ib_active)
1779a2a074efSLeon Romanovsky 		return -EAGAIN;
1780e126ba97SEli Cohen 
1781e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
178278c0f98cSEli Cohen 		ver = 0;
1783e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
178478c0f98cSEli Cohen 		ver = 2;
178578c0f98cSEli Cohen 	else
1786a2a074efSLeon Romanovsky 		return -EINVAL;
178778c0f98cSEli Cohen 
1788e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1789e126ba97SEli Cohen 	if (err)
1790a2a074efSLeon Romanovsky 		return err;
1791e126ba97SEli Cohen 
1792a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1793a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
179478c0f98cSEli Cohen 
1795f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1796a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
1797b368d7cbSMatan Barak 
17982f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
17992f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
18002f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1801a2a074efSLeon Romanovsky 		return -EINVAL;
1802e126ba97SEli Cohen 
1803938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
18042cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1805938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1806b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1807938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1808938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1809938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1810938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1811938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1812f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1813f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1814f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
181530aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
181630aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
181730aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
181830aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1819b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1820b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1821e126ba97SEli Cohen 
1822c03faa56SMatan Barak 	if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1823c03faa56SMatan Barak 		if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1824c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1825c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1826c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1827c03faa56SMatan Barak 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1828c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1829c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1830c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1831c03faa56SMatan Barak 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1832c03faa56SMatan Barak 	}
1833c03faa56SMatan Barak 
183430aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
18352f5ff264SEli Cohen 	bfregi = &context->bfregi;
1836b037c29aSEli Cohen 
1837b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
183831a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1839b037c29aSEli Cohen 	if (err)
1840b037c29aSEli Cohen 		goto out_ctx;
1841b037c29aSEli Cohen 
18422f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1843b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
184431a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1845b037c29aSEli Cohen 				GFP_KERNEL);
1846b037c29aSEli Cohen 	if (!bfregi->count) {
1847e126ba97SEli Cohen 		err = -ENOMEM;
1848e126ba97SEli Cohen 		goto out_ctx;
1849e126ba97SEli Cohen 	}
1850e126ba97SEli Cohen 
1851b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1852b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1853e126ba97SEli Cohen 				    GFP_KERNEL);
1854b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1855e126ba97SEli Cohen 		err = -ENOMEM;
1856e126ba97SEli Cohen 		goto out_count;
1857e126ba97SEli Cohen 	}
1858e126ba97SEli Cohen 
1859b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1860b037c29aSEli Cohen 	if (err)
1861b037c29aSEli Cohen 		goto out_sys_pages;
1862b037c29aSEli Cohen 
186313859d5dSLeon Romanovsky 	if (ibdev->attrs.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING)
186413859d5dSLeon Romanovsky 		context->ibucontext.invalidate_range =
186513859d5dSLeon Romanovsky 			&mlx5_ib_invalidate_range;
1866b4cfe447SHaggai Eran 
1867a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1868fb98153bSYishai Hadas 		err = mlx5_ib_devx_create(dev, true);
186976dc5a84SYishai Hadas 		if (err < 0)
1870d2d19121SYishai Hadas 			goto out_uars;
187176dc5a84SYishai Hadas 		context->devx_uid = err;
1872a8b92ca1SYishai Hadas 	}
1873a8b92ca1SYishai Hadas 
1874d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1875d2d19121SYishai Hadas 					     context->devx_uid);
1876a8b92ca1SYishai Hadas 	if (err)
1877d2d19121SYishai Hadas 		goto out_devx;
1878a8b92ca1SYishai Hadas 
187925bb36e7SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
188025bb36e7SYonatan Cohen 		err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
188125bb36e7SYonatan Cohen 		if (err)
18828193abb6SJason Gunthorpe 			goto out_mdev;
188325bb36e7SYonatan Cohen 	}
188425bb36e7SYonatan Cohen 
1885e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1886e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1887e126ba97SEli Cohen 
18882f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1889508562d6SDaniel Jurgens 	resp.num_ports = dev->num_ports;
1890b368d7cbSMatan Barak 
1891f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1892f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1893b368d7cbSMatan Barak 
1894402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
18956ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
18966ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1897402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1898402ca536SBodong Wang 	}
1899402ca536SBodong Wang 
190078984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
190178984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
190278984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
190378984898SOr Gerlitz 			resp.eth_min_inline++;
190478984898SOr Gerlitz 		}
190578984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
190678984898SOr Gerlitz 	}
190778984898SOr Gerlitz 
19085c99eaecSFeras Daoud 	if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
19095c99eaecSFeras Daoud 		if (mdev->clock_info)
19105c99eaecSFeras Daoud 			resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
19115c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.clock_info_versions);
19125c99eaecSFeras Daoud 	}
19135c99eaecSFeras Daoud 
1914bc5c6eedSNoa Osherovich 	/*
1915bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1916bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1917bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1918bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1919bc5c6eedSNoa Osherovich 	 */
1920de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1921de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1922b368d7cbSMatan Barak 			resp.comp_mask |=
1923b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1924b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1925de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1926de8d6e02SEli Cohen 		}
19275c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.hca_core_clock_offset);
1928b368d7cbSMatan Barak 	}
1929b368d7cbSMatan Barak 
193030aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
193130aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
193230aa60b3SEli Cohen 
193330aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
193430aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
193530aa60b3SEli Cohen 
193631a78a5aSYishai Hadas 	if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
193731a78a5aSYishai Hadas 		resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
193831a78a5aSYishai Hadas 		resp.response_length += sizeof(resp.num_dyn_bfregs);
193931a78a5aSYishai Hadas 	}
194031a78a5aSYishai Hadas 
194125bb36e7SYonatan Cohen 	if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
194225bb36e7SYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
194325bb36e7SYonatan Cohen 			resp.dump_fill_mkey = dump_fill_mkey;
194425bb36e7SYonatan Cohen 			resp.comp_mask |=
194525bb36e7SYonatan Cohen 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
194625bb36e7SYonatan Cohen 		}
194725bb36e7SYonatan Cohen 		resp.response_length += sizeof(resp.dump_fill_mkey);
194825bb36e7SYonatan Cohen 	}
194925bb36e7SYonatan Cohen 
1950b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1951e126ba97SEli Cohen 	if (err)
1952a8b92ca1SYishai Hadas 		goto out_mdev;
1953e126ba97SEli Cohen 
19542f5ff264SEli Cohen 	bfregi->ver = ver;
19552f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1956f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
195730aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
195830aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1959f72300c5SHaggai Abramovsky 
19607c34ec19SAviv Heller 	if (dev->lag_active) {
196195579e78SMark Bloch 		u8 port = mlx5_core_native_port_num(dev->mdev) - 1;
1962c6a21c38SMajd Dibbiny 
1963c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1964c6a21c38SMajd Dibbiny 			   atomic_add_return(
196595579e78SMark Bloch 				   1, &dev->port[port].roce.tx_port_affinity));
1966c6a21c38SMajd Dibbiny 	}
1967c6a21c38SMajd Dibbiny 
1968a2a074efSLeon Romanovsky 	return 0;
1969e126ba97SEli Cohen 
1970a8b92ca1SYishai Hadas out_mdev:
1971d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1972d2d19121SYishai Hadas out_devx:
1973a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
197476dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1975146d2f1aSmajd@mellanox.com 
1976e126ba97SEli Cohen out_uars:
1977b037c29aSEli Cohen 	deallocate_uars(dev, context);
1978b037c29aSEli Cohen 
1979b037c29aSEli Cohen out_sys_pages:
1980b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1981b037c29aSEli Cohen 
1982e126ba97SEli Cohen out_count:
19832f5ff264SEli Cohen 	kfree(bfregi->count);
1984e126ba97SEli Cohen 
1985e126ba97SEli Cohen out_ctx:
1986a2a074efSLeon Romanovsky 	return err;
1987e126ba97SEli Cohen }
1988e126ba97SEli Cohen 
1989a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1990e126ba97SEli Cohen {
1991e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1992e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1993b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1994e126ba97SEli Cohen 
1995f27a0d50SJason Gunthorpe 	/* All umem's must be destroyed before destroying the ucontext. */
1996f27a0d50SJason Gunthorpe 	mutex_lock(&ibcontext->per_mm_list_lock);
1997f27a0d50SJason Gunthorpe 	WARN_ON(!list_empty(&ibcontext->per_mm_list));
1998f27a0d50SJason Gunthorpe 	mutex_unlock(&ibcontext->per_mm_list_lock);
1999a8b92ca1SYishai Hadas 
2000b037c29aSEli Cohen 	bfregi = &context->bfregi;
2001d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2002d2d19121SYishai Hadas 
2003b037c29aSEli Cohen 	if (context->devx_uid)
200476dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
2005e126ba97SEli Cohen 
2006e126ba97SEli Cohen 	deallocate_uars(dev, context);
20072f5ff264SEli Cohen 	kfree(bfregi->sys_pages);
20082f5ff264SEli Cohen 	kfree(bfregi->count);
2009e126ba97SEli Cohen }
2010e126ba97SEli Cohen 
2011e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2012e126ba97SEli Cohen 				 int uar_idx)
2013e126ba97SEli Cohen {
2014b037c29aSEli Cohen 	int fw_uars_per_page;
2015b037c29aSEli Cohen 
2016b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2017b037c29aSEli Cohen 
2018aa8106f1SHuy Nguyen 	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2019e126ba97SEli Cohen }
2020e126ba97SEli Cohen 
2021e126ba97SEli Cohen static int get_command(unsigned long offset)
2022e126ba97SEli Cohen {
2023e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2024e126ba97SEli Cohen }
2025e126ba97SEli Cohen 
2026e126ba97SEli Cohen static int get_arg(unsigned long offset)
2027e126ba97SEli Cohen {
2028e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2029e126ba97SEli Cohen }
2030e126ba97SEli Cohen 
2031e126ba97SEli Cohen static int get_index(unsigned long offset)
2032e126ba97SEli Cohen {
2033e126ba97SEli Cohen 	return get_arg(offset);
2034e126ba97SEli Cohen }
2035e126ba97SEli Cohen 
20364ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
20374ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20384ed131d0SYishai Hadas {
20394ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20404ed131d0SYishai Hadas }
20414ed131d0SYishai Hadas 
20427c2344c3SMaor Gottlieb 
20437c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20447c2344c3SMaor Gottlieb {
20457c2344c3SMaor Gottlieb }
20467c2344c3SMaor Gottlieb 
204737aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2048e126ba97SEli Cohen {
204937aa5c36SGuy Levi 	switch (cmd) {
205037aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
205137aa5c36SGuy Levi 		return "WC";
2052e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
205337aa5c36SGuy Levi 		return "best effort WC";
205437aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
205537aa5c36SGuy Levi 		return "NC";
205624da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
205724da0016SAriel Levkovich 		return "Device Memory";
205837aa5c36SGuy Levi 	default:
205937aa5c36SGuy Levi 		return NULL;
206037aa5c36SGuy Levi 	}
206137aa5c36SGuy Levi }
206237aa5c36SGuy Levi 
20635c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20645c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20655c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20665c99eaecSFeras Daoud {
20674eb6ab13SJason Gunthorpe 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
20684eb6ab13SJason Gunthorpe 	    !(vma->vm_flags & VM_SHARED))
20695c99eaecSFeras Daoud 		return -EINVAL;
20705c99eaecSFeras Daoud 
20715c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20725c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20735c99eaecSFeras Daoud 
20744eb6ab13SJason Gunthorpe 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
20755c99eaecSFeras Daoud 		return -EPERM;
2076c660133cSJason Gunthorpe 	vma->vm_flags &= ~VM_MAYWRITE;
20775c99eaecSFeras Daoud 
2078ddcdc368SJason Gunthorpe 	if (!dev->mdev->clock_info)
20795c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20805c99eaecSFeras Daoud 
20814eb6ab13SJason Gunthorpe 	return vm_insert_page(vma, vma->vm_start,
20824eb6ab13SJason Gunthorpe 			      virt_to_page(dev->mdev->clock_info));
20835c99eaecSFeras Daoud }
20845c99eaecSFeras Daoud 
208537aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
20867c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
20877c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
208837aa5c36SGuy Levi {
20892f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
209037aa5c36SGuy Levi 	int err;
209137aa5c36SGuy Levi 	unsigned long idx;
2092aa09ea6eSKamal Heib 	phys_addr_t pfn;
209337aa5c36SGuy Levi 	pgprot_t prot;
20944ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
20954ed131d0SYishai Hadas 	u32 uar_index;
20964ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
20974ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
20984ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2099b037c29aSEli Cohen 
2100b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2101b037c29aSEli Cohen 		return -EINVAL;
2102b037c29aSEli Cohen 
21034ed131d0SYishai Hadas 	if (dyn_uar)
21044ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
21054ed131d0SYishai Hadas 	else
2106b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
21074ed131d0SYishai Hadas 
21084ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21094ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21104ed131d0SYishai Hadas 			     idx, max_valid_idx);
2111b037c29aSEli Cohen 		return -EINVAL;
2112b037c29aSEli Cohen 	}
211337aa5c36SGuy Levi 
211437aa5c36SGuy Levi 	switch (cmd) {
211537aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21164ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
211737aa5c36SGuy Levi /* Some architectures don't support WC memory */
211837aa5c36SGuy Levi #if defined(CONFIG_X86)
211937aa5c36SGuy Levi 		if (!pat_enabled())
212037aa5c36SGuy Levi 			return -EPERM;
212137aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
212237aa5c36SGuy Levi 			return -EPERM;
212337aa5c36SGuy Levi #endif
212437aa5c36SGuy Levi 	/* fall through */
212537aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
212637aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
212737aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
212837aa5c36SGuy Levi 		break;
212937aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
213037aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
213137aa5c36SGuy Levi 		break;
213237aa5c36SGuy Levi 	default:
213337aa5c36SGuy Levi 		return -EINVAL;
213437aa5c36SGuy Levi 	}
213537aa5c36SGuy Levi 
21364ed131d0SYishai Hadas 	if (dyn_uar) {
21374ed131d0SYishai Hadas 		int uars_per_page;
21384ed131d0SYishai Hadas 
21394ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21404ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21414ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21424ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21434ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21444ed131d0SYishai Hadas 			return -EINVAL;
21454ed131d0SYishai Hadas 		}
21464ed131d0SYishai Hadas 
21474ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21484ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21494ed131d0SYishai Hadas 		 * page holds its count.
21504ed131d0SYishai Hadas 		 */
21514ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
21524ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
21534ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
21544ed131d0SYishai Hadas 			return -EINVAL;
21554ed131d0SYishai Hadas 		}
21564ed131d0SYishai Hadas 
21574ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
21584ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
21594ed131d0SYishai Hadas 
21604ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
21614ed131d0SYishai Hadas 		if (err) {
21624ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
21634ed131d0SYishai Hadas 			goto free_bfreg;
21644ed131d0SYishai Hadas 		}
21654ed131d0SYishai Hadas 	} else {
21664ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
21674ed131d0SYishai Hadas 	}
21684ed131d0SYishai Hadas 
21694ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
217037aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2171e126ba97SEli Cohen 
2172e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2173e2cd1d1aSJason Gunthorpe 				prot);
217437aa5c36SGuy Levi 	if (err) {
21758f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2176e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
21778f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
21784ed131d0SYishai Hadas 		goto err;
217937aa5c36SGuy Levi 	}
2180e126ba97SEli Cohen 
21814ed131d0SYishai Hadas 	if (dyn_uar)
21824ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
21834ed131d0SYishai Hadas 	return 0;
21844ed131d0SYishai Hadas 
21854ed131d0SYishai Hadas err:
21864ed131d0SYishai Hadas 	if (!dyn_uar)
21874ed131d0SYishai Hadas 		return err;
21884ed131d0SYishai Hadas 
21894ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
21904ed131d0SYishai Hadas 
21914ed131d0SYishai Hadas free_bfreg:
21924ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
21934ed131d0SYishai Hadas 
21944ed131d0SYishai Hadas 	return err;
219537aa5c36SGuy Levi }
219637aa5c36SGuy Levi 
219724da0016SAriel Levkovich static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
219824da0016SAriel Levkovich {
219924da0016SAriel Levkovich 	struct mlx5_ib_ucontext *mctx = to_mucontext(context);
220024da0016SAriel Levkovich 	struct mlx5_ib_dev *dev = to_mdev(context->device);
220124da0016SAriel Levkovich 	u16 page_idx = get_extended_index(vma->vm_pgoff);
220224da0016SAriel Levkovich 	size_t map_size = vma->vm_end - vma->vm_start;
220324da0016SAriel Levkovich 	u32 npages = map_size >> PAGE_SHIFT;
220424da0016SAriel Levkovich 	phys_addr_t pfn;
220524da0016SAriel Levkovich 
220624da0016SAriel Levkovich 	if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
220724da0016SAriel Levkovich 	    page_idx + npages)
220824da0016SAriel Levkovich 		return -EINVAL;
220924da0016SAriel Levkovich 
2210aa8106f1SHuy Nguyen 	pfn = ((dev->mdev->bar_addr +
221124da0016SAriel Levkovich 	      MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
221224da0016SAriel Levkovich 	      PAGE_SHIFT) +
221324da0016SAriel Levkovich 	      page_idx;
2214e2cd1d1aSJason Gunthorpe 	return rdma_user_mmap_io(context, vma, pfn, map_size,
2215e2cd1d1aSJason Gunthorpe 				 pgprot_writecombine(vma->vm_page_prot));
221624da0016SAriel Levkovich }
221724da0016SAriel Levkovich 
221837aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
221937aa5c36SGuy Levi {
222037aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
222137aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
222237aa5c36SGuy Levi 	unsigned long command;
222337aa5c36SGuy Levi 	phys_addr_t pfn;
222437aa5c36SGuy Levi 
222537aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
222637aa5c36SGuy Levi 	switch (command) {
222737aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
222837aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
222937aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
22304ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
22317c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2232e126ba97SEli Cohen 
2233e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2234e126ba97SEli Cohen 		return -ENOSYS;
2235e126ba97SEli Cohen 
2236d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2237d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2238d69e3bcfSMatan Barak 			return -EINVAL;
2239d69e3bcfSMatan Barak 
22406cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2241d69e3bcfSMatan Barak 			return -EPERM;
2242c660133cSJason Gunthorpe 		vma->vm_flags &= ~VM_MAYWRITE;
2243d69e3bcfSMatan Barak 
2244d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2245d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2246d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2247d69e3bcfSMatan Barak 
2248d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2249d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2250d69e3bcfSMatan Barak 			PAGE_SHIFT;
2251d5e560d3SJason Gunthorpe 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2252d5e560d3SJason Gunthorpe 					 PAGE_SIZE,
2253d5e560d3SJason Gunthorpe 					 pgprot_noncached(vma->vm_page_prot));
22545c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
22555c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2256d69e3bcfSMatan Barak 
225724da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
225824da0016SAriel Levkovich 		return dm_mmap(ibcontext, vma);
225924da0016SAriel Levkovich 
2260e126ba97SEli Cohen 	default:
2261e126ba97SEli Cohen 		return -EINVAL;
2262e126ba97SEli Cohen 	}
2263e126ba97SEli Cohen 
2264e126ba97SEli Cohen 	return 0;
2265e126ba97SEli Cohen }
2266e126ba97SEli Cohen 
226725c13324SAriel Levkovich static inline int check_dm_type_support(struct mlx5_ib_dev *dev,
226825c13324SAriel Levkovich 					u32 type)
226925c13324SAriel Levkovich {
227025c13324SAriel Levkovich 	switch (type) {
227125c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
227225c13324SAriel Levkovich 		if (!MLX5_CAP_DEV_MEM(dev->mdev, memic))
227325c13324SAriel Levkovich 			return -EOPNOTSUPP;
227425c13324SAriel Levkovich 		break;
227525c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
227625c13324SAriel Levkovich 		if (!capable(CAP_SYS_RAWIO) ||
227725c13324SAriel Levkovich 		    !capable(CAP_NET_RAW))
227825c13324SAriel Levkovich 			return -EPERM;
227925c13324SAriel Levkovich 
228025c13324SAriel Levkovich 		if (!(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner) ||
228125c13324SAriel Levkovich 		      MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner)))
228225c13324SAriel Levkovich 			return -EOPNOTSUPP;
228325c13324SAriel Levkovich 		break;
228425c13324SAriel Levkovich 	}
228525c13324SAriel Levkovich 
228625c13324SAriel Levkovich 	return 0;
228725c13324SAriel Levkovich }
228825c13324SAriel Levkovich 
22893b113a1eSAriel Levkovich static int handle_alloc_dm_memic(struct ib_ucontext *ctx,
22903b113a1eSAriel Levkovich 				 struct mlx5_ib_dm *dm,
229124da0016SAriel Levkovich 				 struct ib_dm_alloc_attr *attr,
229224da0016SAriel Levkovich 				 struct uverbs_attr_bundle *attrs)
229324da0016SAriel Levkovich {
22943b113a1eSAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
229524da0016SAriel Levkovich 	u64 start_offset;
229624da0016SAriel Levkovich 	u32 page_idx;
229724da0016SAriel Levkovich 	int err;
229824da0016SAriel Levkovich 
22993b113a1eSAriel Levkovich 	dm->size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
230024da0016SAriel Levkovich 
23013b113a1eSAriel Levkovich 	err = mlx5_cmd_alloc_memic(dm_db, &dm->dev_addr,
23023b113a1eSAriel Levkovich 				   dm->size, attr->alignment);
230324da0016SAriel Levkovich 	if (err)
23043b113a1eSAriel Levkovich 		return err;
230524da0016SAriel Levkovich 
23063b113a1eSAriel Levkovich 	page_idx = (dm->dev_addr - pci_resource_start(dm_db->dev->pdev, 0) -
23073b113a1eSAriel Levkovich 		    MLX5_CAP64_DEV_MEM(dm_db->dev, memic_bar_start_addr)) >>
230824da0016SAriel Levkovich 		    PAGE_SHIFT;
230924da0016SAriel Levkovich 
231024da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
231124da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
231224da0016SAriel Levkovich 			     &page_idx, sizeof(page_idx));
231324da0016SAriel Levkovich 	if (err)
231424da0016SAriel Levkovich 		goto err_dealloc;
231524da0016SAriel Levkovich 
23163b113a1eSAriel Levkovich 	start_offset = dm->dev_addr & ~PAGE_MASK;
23173b113a1eSAriel Levkovich 	err = uverbs_copy_to(attrs,
23183b113a1eSAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
23193b113a1eSAriel Levkovich 			     &start_offset, sizeof(start_offset));
23203b113a1eSAriel Levkovich 	if (err)
23213b113a1eSAriel Levkovich 		goto err_dealloc;
232224da0016SAriel Levkovich 
23233b113a1eSAriel Levkovich 	bitmap_set(to_mucontext(ctx)->dm_pages, page_idx,
23243b113a1eSAriel Levkovich 		   DIV_ROUND_UP(dm->size, PAGE_SIZE));
23253b113a1eSAriel Levkovich 
23263b113a1eSAriel Levkovich 	return 0;
23273b113a1eSAriel Levkovich 
23283b113a1eSAriel Levkovich err_dealloc:
23293b113a1eSAriel Levkovich 	mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
23303b113a1eSAriel Levkovich 
23313b113a1eSAriel Levkovich 	return err;
23323b113a1eSAriel Levkovich }
23333b113a1eSAriel Levkovich 
233425c13324SAriel Levkovich static int handle_alloc_dm_sw_icm(struct ib_ucontext *ctx,
233525c13324SAriel Levkovich 				  struct mlx5_ib_dm *dm,
233625c13324SAriel Levkovich 				  struct ib_dm_alloc_attr *attr,
233725c13324SAriel Levkovich 				  struct uverbs_attr_bundle *attrs,
233825c13324SAriel Levkovich 				  int type)
233925c13324SAriel Levkovich {
234025c13324SAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
234125c13324SAriel Levkovich 	u64 act_size;
234225c13324SAriel Levkovich 	int err;
234325c13324SAriel Levkovich 
234425c13324SAriel Levkovich 	/* Allocation size must a multiple of the basic block size
234525c13324SAriel Levkovich 	 * and a power of 2.
234625c13324SAriel Levkovich 	 */
234725c13324SAriel Levkovich 	act_size = roundup(attr->length, MLX5_SW_ICM_BLOCK_SIZE(dm_db->dev));
234825c13324SAriel Levkovich 	act_size = roundup_pow_of_two(act_size);
234925c13324SAriel Levkovich 
235025c13324SAriel Levkovich 	dm->size = act_size;
235125c13324SAriel Levkovich 	err = mlx5_cmd_alloc_sw_icm(dm_db, type, act_size,
235225c13324SAriel Levkovich 				    to_mucontext(ctx)->devx_uid, &dm->dev_addr,
235325c13324SAriel Levkovich 				    &dm->icm_dm.obj_id);
235425c13324SAriel Levkovich 	if (err)
235525c13324SAriel Levkovich 		return err;
235625c13324SAriel Levkovich 
235725c13324SAriel Levkovich 	err = uverbs_copy_to(attrs,
235825c13324SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
235925c13324SAriel Levkovich 			     &dm->dev_addr, sizeof(dm->dev_addr));
236025c13324SAriel Levkovich 	if (err)
236125c13324SAriel Levkovich 		mlx5_cmd_dealloc_sw_icm(dm_db, type, dm->size,
236225c13324SAriel Levkovich 					to_mucontext(ctx)->devx_uid,
236325c13324SAriel Levkovich 					dm->dev_addr, dm->icm_dm.obj_id);
236425c13324SAriel Levkovich 
236525c13324SAriel Levkovich 	return err;
236625c13324SAriel Levkovich }
236725c13324SAriel Levkovich 
23683b113a1eSAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
23693b113a1eSAriel Levkovich 			       struct ib_ucontext *context,
23703b113a1eSAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
23713b113a1eSAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
23723b113a1eSAriel Levkovich {
23733b113a1eSAriel Levkovich 	struct mlx5_ib_dm *dm;
23743b113a1eSAriel Levkovich 	enum mlx5_ib_uapi_dm_type type;
23753b113a1eSAriel Levkovich 	int err;
23763b113a1eSAriel Levkovich 
23773b113a1eSAriel Levkovich 	err = uverbs_get_const_default(&type, attrs,
23783b113a1eSAriel Levkovich 				       MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
23793b113a1eSAriel Levkovich 				       MLX5_IB_UAPI_DM_TYPE_MEMIC);
23803b113a1eSAriel Levkovich 	if (err)
23813b113a1eSAriel Levkovich 		return ERR_PTR(err);
23823b113a1eSAriel Levkovich 
23833b113a1eSAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_dm req: dm_type=%d user_length=0x%llx log_alignment=%d\n",
23843b113a1eSAriel Levkovich 		    type, attr->length, attr->alignment);
23853b113a1eSAriel Levkovich 
238625c13324SAriel Levkovich 	err = check_dm_type_support(to_mdev(ibdev), type);
238725c13324SAriel Levkovich 	if (err)
238825c13324SAriel Levkovich 		return ERR_PTR(err);
238925c13324SAriel Levkovich 
23903b113a1eSAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
23913b113a1eSAriel Levkovich 	if (!dm)
23923b113a1eSAriel Levkovich 		return ERR_PTR(-ENOMEM);
23933b113a1eSAriel Levkovich 
23943b113a1eSAriel Levkovich 	dm->type = type;
23953b113a1eSAriel Levkovich 
23963b113a1eSAriel Levkovich 	switch (type) {
23973b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
23983b113a1eSAriel Levkovich 		err = handle_alloc_dm_memic(context, dm,
23993b113a1eSAriel Levkovich 					    attr,
24003b113a1eSAriel Levkovich 					    attrs);
24013b113a1eSAriel Levkovich 		break;
240225c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
240325c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
240425c13324SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm, attr, attrs, type);
240525c13324SAriel Levkovich 		break;
24063b113a1eSAriel Levkovich 	default:
24073b113a1eSAriel Levkovich 		err = -EOPNOTSUPP;
24083b113a1eSAriel Levkovich 	}
24093b113a1eSAriel Levkovich 
24103b113a1eSAriel Levkovich 	if (err)
24113b113a1eSAriel Levkovich 		goto err_free;
241224da0016SAriel Levkovich 
241324da0016SAriel Levkovich 	return &dm->ibdm;
241424da0016SAriel Levkovich 
241524da0016SAriel Levkovich err_free:
241624da0016SAriel Levkovich 	kfree(dm);
241724da0016SAriel Levkovich 	return ERR_PTR(err);
241824da0016SAriel Levkovich }
241924da0016SAriel Levkovich 
2420c4367a26SShamir Rabinovitch int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
242124da0016SAriel Levkovich {
242225c13324SAriel Levkovich 	struct mlx5_ib_ucontext *ctx = rdma_udata_to_drv_context(
242325c13324SAriel Levkovich 		&attrs->driver_udata, struct mlx5_ib_ucontext, ibucontext);
24243b113a1eSAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ibdm->device)->dm;
242524da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
242624da0016SAriel Levkovich 	u32 page_idx;
242724da0016SAriel Levkovich 	int ret;
242824da0016SAriel Levkovich 
24293b113a1eSAriel Levkovich 	switch (dm->type) {
24303b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
24313b113a1eSAriel Levkovich 		ret = mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
243224da0016SAriel Levkovich 		if (ret)
243324da0016SAriel Levkovich 			return ret;
243424da0016SAriel Levkovich 
24353b113a1eSAriel Levkovich 		page_idx = (dm->dev_addr -
24363b113a1eSAriel Levkovich 			    pci_resource_start(dm_db->dev->pdev, 0) -
24373b113a1eSAriel Levkovich 			    MLX5_CAP64_DEV_MEM(dm_db->dev,
24383b113a1eSAriel Levkovich 					       memic_bar_start_addr)) >>
243924da0016SAriel Levkovich 			   PAGE_SHIFT;
244025c13324SAriel Levkovich 		bitmap_clear(ctx->dm_pages, page_idx,
244125c13324SAriel Levkovich 			     DIV_ROUND_UP(dm->size, PAGE_SIZE));
244225c13324SAriel Levkovich 		break;
244325c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
244425c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
244525c13324SAriel Levkovich 		ret = mlx5_cmd_dealloc_sw_icm(dm_db, dm->type, dm->size,
244625c13324SAriel Levkovich 					      ctx->devx_uid, dm->dev_addr,
244725c13324SAriel Levkovich 					      dm->icm_dm.obj_id);
244825c13324SAriel Levkovich 		if (ret)
244925c13324SAriel Levkovich 			return ret;
24503b113a1eSAriel Levkovich 		break;
24513b113a1eSAriel Levkovich 	default:
24523b113a1eSAriel Levkovich 		return -EOPNOTSUPP;
24533b113a1eSAriel Levkovich 	}
245424da0016SAriel Levkovich 
245524da0016SAriel Levkovich 	kfree(dm);
245624da0016SAriel Levkovich 
245724da0016SAriel Levkovich 	return 0;
245824da0016SAriel Levkovich }
245924da0016SAriel Levkovich 
2460ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2461e126ba97SEli Cohen {
246221a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
246321a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2464e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2465e126ba97SEli Cohen 	int err;
2466a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2467a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)]   = {};
2468a1069c1cSYishai Hadas 	u16 uid = 0;
2469ff23dfa1SShamir Rabinovitch 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2470ff23dfa1SShamir Rabinovitch 		udata, struct mlx5_ib_ucontext, ibucontext);
2471e126ba97SEli Cohen 
2472ff23dfa1SShamir Rabinovitch 	uid = context ? context->devx_uid : 0;
2473a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2474a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
2475a1069c1cSYishai Hadas 	err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2476a1069c1cSYishai Hadas 			    out, sizeof(out));
247721a428a0SLeon Romanovsky 	if (err)
247821a428a0SLeon Romanovsky 		return err;
2479e126ba97SEli Cohen 
2480a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2481a1069c1cSYishai Hadas 	pd->uid = uid;
2482ff23dfa1SShamir Rabinovitch 	if (udata) {
2483e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2484e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2485a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
248621a428a0SLeon Romanovsky 			return -EFAULT;
2487e126ba97SEli Cohen 		}
2488e126ba97SEli Cohen 	}
2489e126ba97SEli Cohen 
249021a428a0SLeon Romanovsky 	return 0;
2491e126ba97SEli Cohen }
2492e126ba97SEli Cohen 
2493c4367a26SShamir Rabinovitch static void mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2494e126ba97SEli Cohen {
2495e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2496e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2497e126ba97SEli Cohen 
2498a1069c1cSYishai Hadas 	mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2499e126ba97SEli Cohen }
2500e126ba97SEli Cohen 
2501466fa6d2SMaor Gottlieb enum {
2502466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2503466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
250471c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_INNER_BIT,
250571c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_MISC2_BIT
2506466fa6d2SMaor Gottlieb };
2507038d2ef8SMaor Gottlieb 
2508466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2509466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2510466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2511466fa6d2SMaor Gottlieb 
2512466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2513466fa6d2SMaor Gottlieb {
2514466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2515466fa6d2SMaor Gottlieb 
2516466fa6d2SMaor Gottlieb 	match_criteria_enable =
2517466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2518466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2519466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2520466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2521466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2522466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2523466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2524466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
252571c6e863SAriel Levkovich 	match_criteria_enable |=
252671c6e863SAriel Levkovich 		(!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
252771c6e863SAriel Levkovich 		MATCH_CRITERIA_ENABLE_MISC2_BIT;
2528466fa6d2SMaor Gottlieb 
2529466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2530038d2ef8SMaor Gottlieb }
2531038d2ef8SMaor Gottlieb 
25326113cc44SMaor Gottlieb static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2533ca0d4753SMaor Gottlieb {
25346113cc44SMaor Gottlieb 	u8 entry_mask;
25356113cc44SMaor Gottlieb 	u8 entry_val;
25366113cc44SMaor Gottlieb 	int err = 0;
25376113cc44SMaor Gottlieb 
25386113cc44SMaor Gottlieb 	if (!mask)
25396113cc44SMaor Gottlieb 		goto out;
25406113cc44SMaor Gottlieb 
25416113cc44SMaor Gottlieb 	entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
25426113cc44SMaor Gottlieb 			      ip_protocol);
25436113cc44SMaor Gottlieb 	entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
25446113cc44SMaor Gottlieb 			     ip_protocol);
25456113cc44SMaor Gottlieb 	if (!entry_mask) {
2546ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2547ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
25486113cc44SMaor Gottlieb 		goto out;
25496113cc44SMaor Gottlieb 	}
25506113cc44SMaor Gottlieb 	/* Don't override existing ip protocol */
25516113cc44SMaor Gottlieb 	if (mask != entry_mask || val != entry_val)
25526113cc44SMaor Gottlieb 		err = -EINVAL;
25536113cc44SMaor Gottlieb out:
25546113cc44SMaor Gottlieb 	return err;
2555ca0d4753SMaor Gottlieb }
2556ca0d4753SMaor Gottlieb 
255737da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
25582d1e697eSMoses Reuben 			   bool inner)
25592d1e697eSMoses Reuben {
25602d1e697eSMoses Reuben 	if (inner) {
25612d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
25622d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
25632d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
25642d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
25652d1e697eSMoses Reuben 	} else {
25662d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
25672d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
25682d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
25692d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
25702d1e697eSMoses Reuben 	}
25712d1e697eSMoses Reuben }
25722d1e697eSMoses Reuben 
2573ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2574ca0d4753SMaor Gottlieb {
2575ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2576ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2577ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2578ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2579ca0d4753SMaor Gottlieb }
2580ca0d4753SMaor Gottlieb 
258171c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
258271c6e863SAriel Levkovich {
258371c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
258471c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
258571c6e863SAriel Levkovich 		return -EOPNOTSUPP;
258671c6e863SAriel Levkovich 
258771c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
258871c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
258971c6e863SAriel Levkovich 		return -EOPNOTSUPP;
259071c6e863SAriel Levkovich 
259171c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
259271c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
259371c6e863SAriel Levkovich 		return -EOPNOTSUPP;
259471c6e863SAriel Levkovich 
259571c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
259671c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
259771c6e863SAriel Levkovich 		return -EOPNOTSUPP;
259871c6e863SAriel Levkovich 
259971c6e863SAriel Levkovich 	return 0;
260071c6e863SAriel Levkovich }
260171c6e863SAriel Levkovich 
2602c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2603c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2604ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2605466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2606c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2607ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
26082ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2609a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
26103b3233fbSRaed Salem #define LAST_COUNTERS_FIELD counters
2611c47ac6aeSMaor Gottlieb 
2612c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2613c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2614c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2615c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2616c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2617c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2618c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2619c47ac6aeSMaor Gottlieb 
26202ea26203SMark Bloch int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
26212ea26203SMark Bloch 			   bool is_egress,
2622802c2125SAviad Yehezkel 			   struct mlx5_flow_act *action)
2623802c2125SAviad Yehezkel {
2624802c2125SAviad Yehezkel 
2625802c2125SAviad Yehezkel 	switch (maction->ib_action.type) {
2626802c2125SAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
2627501f14e3SMark Bloch 		if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2628501f14e3SMark Bloch 				      MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2629501f14e3SMark Bloch 			return -EINVAL;
2630802c2125SAviad Yehezkel 		/* Currently only AES_GCM keymat is supported by the driver */
2631802c2125SAviad Yehezkel 		action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
26322ea26203SMark Bloch 		action->action |= is_egress ?
2633802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2634802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2635802c2125SAviad Yehezkel 		return 0;
2636b1085be3SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
2637b1085be3SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2638b1085be3SMark Bloch 		    MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
2639501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2640501f14e3SMark Bloch 				return -EINVAL;
2641b1085be3SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2642b1085be3SMark Bloch 			action->modify_id = maction->flow_action_raw.action_id;
2643b1085be3SMark Bloch 			return 0;
2644b1085be3SMark Bloch 		}
264510a30896SMark Bloch 		if (maction->flow_action_raw.sub_type ==
264610a30896SMark Bloch 		    MLX5_IB_FLOW_ACTION_DECAP) {
2647501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2648501f14e3SMark Bloch 				return -EINVAL;
264910a30896SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
265010a30896SMark Bloch 			return 0;
265110a30896SMark Bloch 		}
2652e806f932SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2653e806f932SMark Bloch 		    MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
2654501f14e3SMark Bloch 			if (action->action &
2655501f14e3SMark Bloch 			    MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2656501f14e3SMark Bloch 				return -EINVAL;
2657e806f932SMark Bloch 			action->action |=
2658e806f932SMark Bloch 				MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2659e806f932SMark Bloch 			action->reformat_id =
2660e806f932SMark Bloch 				maction->flow_action_raw.action_id;
2661e806f932SMark Bloch 			return 0;
2662e806f932SMark Bloch 		}
2663b1085be3SMark Bloch 		/* fall through */
2664802c2125SAviad Yehezkel 	default:
2665802c2125SAviad Yehezkel 		return -EOPNOTSUPP;
2666802c2125SAviad Yehezkel 	}
2667802c2125SAviad Yehezkel }
2668802c2125SAviad Yehezkel 
266919cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
267019cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
2671802c2125SAviad Yehezkel 			   const struct ib_flow_attr *flow_attr,
267271c6e863SAriel Levkovich 			   struct mlx5_flow_act *action, u32 prev_type)
2673038d2ef8SMaor Gottlieb {
2674466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2675466fa6d2SMaor Gottlieb 					   misc_parameters);
2676466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2677466fa6d2SMaor Gottlieb 					   misc_parameters);
267871c6e863SAriel Levkovich 	void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
267971c6e863SAriel Levkovich 					    misc_parameters_2);
268071c6e863SAriel Levkovich 	void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
268171c6e863SAriel Levkovich 					    misc_parameters_2);
26822d1e697eSMoses Reuben 	void *headers_c;
26832d1e697eSMoses Reuben 	void *headers_v;
268419cc7524SAriel Levkovich 	int match_ipv;
2685802c2125SAviad Yehezkel 	int ret;
2686466fa6d2SMaor Gottlieb 
26872d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
26882d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
26892d1e697eSMoses Reuben 					 inner_headers);
26902d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
26912d1e697eSMoses Reuben 					 inner_headers);
269219cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
269319cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
26942d1e697eSMoses Reuben 	} else {
26952d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
26962d1e697eSMoses Reuben 					 outer_headers);
26972d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
26982d1e697eSMoses Reuben 					 outer_headers);
269919cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
270019cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
27012d1e697eSMoses Reuben 	}
27022d1e697eSMoses Reuben 
27032d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2704038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2705c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
27061ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2707038d2ef8SMaor Gottlieb 
27082d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2709038d2ef8SMaor Gottlieb 					     dmac_47_16),
2710038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
27112d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2712038d2ef8SMaor Gottlieb 					     dmac_47_16),
2713038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2714038d2ef8SMaor Gottlieb 
27152d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2716ee3da804SMaor Gottlieb 					     smac_47_16),
2717ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
27182d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2719ee3da804SMaor Gottlieb 					     smac_47_16),
2720ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2721ee3da804SMaor Gottlieb 
2722038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
27232d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
272410543365SMohamad Haj Yahia 				 cvlan_tag, 1);
27252d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
272610543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2727038d2ef8SMaor Gottlieb 
27282d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2729038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
27302d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2731038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2732038d2ef8SMaor Gottlieb 
27332d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2734038d2ef8SMaor Gottlieb 				 first_cfi,
2735038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
27362d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2737038d2ef8SMaor Gottlieb 				 first_cfi,
2738038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2739038d2ef8SMaor Gottlieb 
27402d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2741038d2ef8SMaor Gottlieb 				 first_prio,
2742038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
27432d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2744038d2ef8SMaor Gottlieb 				 first_prio,
2745038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2746038d2ef8SMaor Gottlieb 		}
27472d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2748038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
27492d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2750038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2751038d2ef8SMaor Gottlieb 		break;
2752038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2753c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
27541ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2755038d2ef8SMaor Gottlieb 
275619cc7524SAriel Levkovich 		if (match_ipv) {
275719cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
275819cc7524SAriel Levkovich 				 ip_version, 0xf);
275919cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
27603346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV4_VERSION);
276119cc7524SAriel Levkovich 		} else {
27622d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2763038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
27642d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2765038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
276619cc7524SAriel Levkovich 		}
2767038d2ef8SMaor Gottlieb 
27682d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2769038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2770038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2771038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
27722d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2773038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2774038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2775038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
27762d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2777038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2778038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2779038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
27802d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2781038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2782038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2783038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2784ca0d4753SMaor Gottlieb 
27852d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2786ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2787ca0d4753SMaor Gottlieb 
27886113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
27896113cc44SMaor Gottlieb 			      ib_spec->ipv4.mask.proto,
27906113cc44SMaor Gottlieb 			      ib_spec->ipv4.val.proto))
27916113cc44SMaor Gottlieb 			return -EINVAL;
2792038d2ef8SMaor Gottlieb 		break;
2793026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2794c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
27951ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2796026bae0cSMaor Gottlieb 
279719cc7524SAriel Levkovich 		if (match_ipv) {
279819cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
279919cc7524SAriel Levkovich 				 ip_version, 0xf);
280019cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
28013346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV6_VERSION);
280219cc7524SAriel Levkovich 		} else {
28032d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2804026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
28052d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2806026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
280719cc7524SAriel Levkovich 		}
2808026bae0cSMaor Gottlieb 
28092d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2810026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2811026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2812026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
28132d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2814026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2815026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2816026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
28172d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2818026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2819026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2820026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
28212d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2822026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2823026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2824026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2825466fa6d2SMaor Gottlieb 
28262d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2827466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2828466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2829466fa6d2SMaor Gottlieb 
28306113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
2831466fa6d2SMaor Gottlieb 			      ib_spec->ipv6.mask.next_hdr,
28326113cc44SMaor Gottlieb 			      ib_spec->ipv6.val.next_hdr))
28336113cc44SMaor Gottlieb 			return -EINVAL;
2834466fa6d2SMaor Gottlieb 
28352d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
28362d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
28372d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
28382d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
2839802c2125SAviad Yehezkel 		break;
2840802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ESP:
2841802c2125SAviad Yehezkel 		if (ib_spec->esp.mask.seq)
2842802c2125SAviad Yehezkel 			return -EOPNOTSUPP;
28432d1e697eSMoses Reuben 
2844802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2845802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.mask.spi));
2846802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2847802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.val.spi));
2848026bae0cSMaor Gottlieb 		break;
2849038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2850c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2851c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
28521ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2853038d2ef8SMaor Gottlieb 
28546113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
28556113cc44SMaor Gottlieb 			return -EINVAL;
2856038d2ef8SMaor Gottlieb 
28572d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2858038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
28592d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2860038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2861038d2ef8SMaor Gottlieb 
28622d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2863038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
28642d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2865038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2866038d2ef8SMaor Gottlieb 		break;
2867038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2868c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2869c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
28701ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2871038d2ef8SMaor Gottlieb 
28726113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
28736113cc44SMaor Gottlieb 			return -EINVAL;
2874038d2ef8SMaor Gottlieb 
28752d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2876038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
28772d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2878038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2879038d2ef8SMaor Gottlieb 
28802d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2881038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
28822d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2883038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2884038d2ef8SMaor Gottlieb 		break;
2885da2f22aeSAriel Levkovich 	case IB_FLOW_SPEC_GRE:
2886da2f22aeSAriel Levkovich 		if (ib_spec->gre.mask.c_ks_res0_ver)
2887da2f22aeSAriel Levkovich 			return -EOPNOTSUPP;
2888da2f22aeSAriel Levkovich 
28896113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
28906113cc44SMaor Gottlieb 			return -EINVAL;
28916113cc44SMaor Gottlieb 
2892da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2893da2f22aeSAriel Levkovich 			 0xff);
2894da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2895da2f22aeSAriel Levkovich 			 IPPROTO_GRE);
2896da2f22aeSAriel Levkovich 
2897da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
2898a93b632cSMaor Gottlieb 			 ntohs(ib_spec->gre.mask.protocol));
2899da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2900da2f22aeSAriel Levkovich 			 ntohs(ib_spec->gre.val.protocol));
2901da2f22aeSAriel Levkovich 
2902da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
29035886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2904da2f22aeSAriel Levkovich 		       &ib_spec->gre.mask.key,
2905da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.mask.key));
2906da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
29075886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2908da2f22aeSAriel Levkovich 		       &ib_spec->gre.val.key,
2909da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.val.key));
2910da2f22aeSAriel Levkovich 		break;
291171c6e863SAriel Levkovich 	case IB_FLOW_SPEC_MPLS:
291271c6e863SAriel Levkovich 		switch (prev_type) {
291371c6e863SAriel Levkovich 		case IB_FLOW_SPEC_UDP:
291471c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
291571c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_udp),
291671c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
291771c6e863SAriel Levkovich 				return -EOPNOTSUPP;
291871c6e863SAriel Levkovich 
291971c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
292071c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
292171c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
292271c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
292371c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
292471c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
292571c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
292671c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
292771c6e863SAriel Levkovich 			break;
292871c6e863SAriel Levkovich 		case IB_FLOW_SPEC_GRE:
292971c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
293071c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_gre),
293171c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
293271c6e863SAriel Levkovich 				return -EOPNOTSUPP;
293371c6e863SAriel Levkovich 
293471c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
293571c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
293671c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
293771c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
293871c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
293971c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
294071c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
294171c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
294271c6e863SAriel Levkovich 			break;
294371c6e863SAriel Levkovich 		default:
294471c6e863SAriel Levkovich 			if (ib_spec->type & IB_FLOW_SPEC_INNER) {
294571c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
294671c6e863SAriel Levkovich 							   ft_field_support.inner_first_mpls),
294771c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
294871c6e863SAriel Levkovich 					return -EOPNOTSUPP;
294971c6e863SAriel Levkovich 
295071c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
295171c6e863SAriel Levkovich 						    inner_first_mpls),
295271c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
295371c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
295471c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
295571c6e863SAriel Levkovich 						    inner_first_mpls),
295671c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
295771c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
295871c6e863SAriel Levkovich 			} else {
295971c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
296071c6e863SAriel Levkovich 							   ft_field_support.outer_first_mpls),
296171c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
296271c6e863SAriel Levkovich 					return -EOPNOTSUPP;
296371c6e863SAriel Levkovich 
296471c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
296571c6e863SAriel Levkovich 						    outer_first_mpls),
296671c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
296771c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
296871c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
296971c6e863SAriel Levkovich 						    outer_first_mpls),
297071c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
297171c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
297271c6e863SAriel Levkovich 			}
297371c6e863SAriel Levkovich 		}
297471c6e863SAriel Levkovich 		break;
2975ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2976ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2977ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
29781ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2979ffb30d8fSMoses Reuben 
2980ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2981ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2982ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2983ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2984ffb30d8fSMoses Reuben 		break;
29852ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
29862ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
29872ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
29882ac693f9SMoses Reuben 			return -EOPNOTSUPP;
29892ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
29902ac693f9SMoses Reuben 			return -EINVAL;
29912ac693f9SMoses Reuben 
2992075572d4SBoris Pismenny 		action->flow_tag = ib_spec->flow_tag.tag_id;
2993d5634feeSPaul Blakey 		action->flags |= FLOW_ACT_HAS_TAG;
29942ac693f9SMoses Reuben 		break;
2995a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2996a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2997a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2998a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2999075572d4SBoris Pismenny 		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
3000a22ed86cSSlava Shwartsman 		break;
3001802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ACTION_HANDLE:
30022ea26203SMark Bloch 		ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
30032ea26203SMark Bloch 			flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
3004802c2125SAviad Yehezkel 		if (ret)
3005802c2125SAviad Yehezkel 			return ret;
3006802c2125SAviad Yehezkel 		break;
30073b3233fbSRaed Salem 	case IB_FLOW_SPEC_ACTION_COUNT:
30083b3233fbSRaed Salem 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
30093b3233fbSRaed Salem 					 LAST_COUNTERS_FIELD))
30103b3233fbSRaed Salem 			return -EOPNOTSUPP;
30113b3233fbSRaed Salem 
30123b3233fbSRaed Salem 		/* for now support only one counters spec per flow */
30133b3233fbSRaed Salem 		if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
30143b3233fbSRaed Salem 			return -EINVAL;
30153b3233fbSRaed Salem 
30163b3233fbSRaed Salem 		action->counters = ib_spec->flow_count.counters;
30173b3233fbSRaed Salem 		action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
30183b3233fbSRaed Salem 		break;
3019038d2ef8SMaor Gottlieb 	default:
3020038d2ef8SMaor Gottlieb 		return -EINVAL;
3021038d2ef8SMaor Gottlieb 	}
3022038d2ef8SMaor Gottlieb 
3023038d2ef8SMaor Gottlieb 	return 0;
3024038d2ef8SMaor Gottlieb }
3025038d2ef8SMaor Gottlieb 
3026038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
3027038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
3028038d2ef8SMaor Gottlieb  * could steal other multicast packets.
3029038d2ef8SMaor Gottlieb  */
3030a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
3031038d2ef8SMaor Gottlieb {
303281e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
3033038d2ef8SMaor Gottlieb 
3034038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
3035038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
3036038d2ef8SMaor Gottlieb 		return false;
3037038d2ef8SMaor Gottlieb 
303881e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
303981e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
304081e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
3041038d2ef8SMaor Gottlieb 
304281e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
304381e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
304481e30880SYishai Hadas 			return true;
304581e30880SYishai Hadas 
304681e30880SYishai Hadas 		return false;
304781e30880SYishai Hadas 	}
304881e30880SYishai Hadas 
304981e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
305081e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
305181e30880SYishai Hadas 
305281e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
3053038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
3054038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
3055038d2ef8SMaor Gottlieb 	}
3056038d2ef8SMaor Gottlieb 
305781e30880SYishai Hadas 	return false;
305881e30880SYishai Hadas }
305981e30880SYishai Hadas 
3060802c2125SAviad Yehezkel enum valid_spec {
3061802c2125SAviad Yehezkel 	VALID_SPEC_INVALID,
3062802c2125SAviad Yehezkel 	VALID_SPEC_VALID,
3063802c2125SAviad Yehezkel 	VALID_SPEC_NA,
3064802c2125SAviad Yehezkel };
3065802c2125SAviad Yehezkel 
3066802c2125SAviad Yehezkel static enum valid_spec
3067802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
3068802c2125SAviad Yehezkel 		     const struct mlx5_flow_spec *spec,
3069802c2125SAviad Yehezkel 		     const struct mlx5_flow_act *flow_act,
3070802c2125SAviad Yehezkel 		     bool egress)
3071802c2125SAviad Yehezkel {
3072802c2125SAviad Yehezkel 	const u32 *match_c = spec->match_criteria;
3073802c2125SAviad Yehezkel 	bool is_crypto =
3074802c2125SAviad Yehezkel 		(flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
3075802c2125SAviad Yehezkel 				     MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
3076802c2125SAviad Yehezkel 	bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
3077802c2125SAviad Yehezkel 	bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
3078802c2125SAviad Yehezkel 
3079802c2125SAviad Yehezkel 	/*
3080802c2125SAviad Yehezkel 	 * Currently only crypto is supported in egress, when regular egress
3081802c2125SAviad Yehezkel 	 * rules would be supported, always return VALID_SPEC_NA.
3082802c2125SAviad Yehezkel 	 */
3083802c2125SAviad Yehezkel 	if (!is_crypto)
308478dd0c43SMark Bloch 		return VALID_SPEC_NA;
3085802c2125SAviad Yehezkel 
3086802c2125SAviad Yehezkel 	return is_crypto && is_ipsec &&
3087d5634feeSPaul Blakey 		(!egress || (!is_drop && !(flow_act->flags & FLOW_ACT_HAS_TAG))) ?
3088802c2125SAviad Yehezkel 		VALID_SPEC_VALID : VALID_SPEC_INVALID;
3089802c2125SAviad Yehezkel }
3090802c2125SAviad Yehezkel 
3091802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev,
3092802c2125SAviad Yehezkel 			  const struct mlx5_flow_spec *spec,
3093802c2125SAviad Yehezkel 			  const struct mlx5_flow_act *flow_act,
3094802c2125SAviad Yehezkel 			  bool egress)
3095802c2125SAviad Yehezkel {
3096802c2125SAviad Yehezkel 	/* We curretly only support ipsec egress flow */
3097802c2125SAviad Yehezkel 	return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
3098802c2125SAviad Yehezkel }
3099802c2125SAviad Yehezkel 
310019cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
310119cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
31020f750966SAriel Levkovich 			       bool check_inner)
3103038d2ef8SMaor Gottlieb {
3104038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
310519cc7524SAriel Levkovich 	int match_ipv = check_inner ?
310619cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
310719cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
310819cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
310919cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
31100f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
31110f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
31120f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
31130f750966SAriel Levkovich 	bool has_ethertype = false;
3114038d2ef8SMaor Gottlieb 	unsigned int spec_index;
31150f750966SAriel Levkovich 	bool mask_valid = true;
31160f750966SAriel Levkovich 	u16 eth_type = 0;
31170f750966SAriel Levkovich 	bool type_valid;
3118038d2ef8SMaor Gottlieb 
3119038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
3120038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
31210f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
3122038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
31230f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
31240f750966SAriel Levkovich 				      htons(0xffff));
31250f750966SAriel Levkovich 			has_ethertype = true;
31260f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
31270f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
31280f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
31290f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
3130038d2ef8SMaor Gottlieb 		}
3131038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
3132038d2ef8SMaor Gottlieb 	}
31330f750966SAriel Levkovich 
31340f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
31350f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
31360f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
31370f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
31380f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
31390f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
314019cc7524SAriel Levkovich 
314119cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
314219cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
314319cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
31440f750966SAriel Levkovich 	}
31450f750966SAriel Levkovich 
31460f750966SAriel Levkovich 	return type_valid;
31470f750966SAriel Levkovich }
31480f750966SAriel Levkovich 
314919cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
315019cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
31510f750966SAriel Levkovich {
315219cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
315319cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
3154038d2ef8SMaor Gottlieb }
3155038d2ef8SMaor Gottlieb 
3156038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
3157038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
3158038d2ef8SMaor Gottlieb {
3159038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
3160038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
3161038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
3162038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
3163038d2ef8SMaor Gottlieb 	}
3164038d2ef8SMaor Gottlieb }
3165038d2ef8SMaor Gottlieb 
31663b3233fbSRaed Salem static void counters_clear_description(struct ib_counters *counters)
31673b3233fbSRaed Salem {
31683b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
31693b3233fbSRaed Salem 
31703b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
31713b3233fbSRaed Salem 	kfree(mcounters->counters_data);
31723b3233fbSRaed Salem 	mcounters->counters_data = NULL;
31733b3233fbSRaed Salem 	mcounters->cntrs_max_index = 0;
31743b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
31753b3233fbSRaed Salem }
31763b3233fbSRaed Salem 
3177038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3178038d2ef8SMaor Gottlieb {
3179038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3180038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
3181038d2ef8SMaor Gottlieb 							  ibflow);
3182038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
3183d4be3f44SYishai Hadas 	struct mlx5_ib_dev *dev = handler->dev;
3184038d2ef8SMaor Gottlieb 
31859a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3186038d2ef8SMaor Gottlieb 
3187038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
318874491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
3189cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
3190038d2ef8SMaor Gottlieb 		list_del(&iter->list);
3191038d2ef8SMaor Gottlieb 		kfree(iter);
3192038d2ef8SMaor Gottlieb 	}
3193038d2ef8SMaor Gottlieb 
319474491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
31955497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
31963b3233fbSRaed Salem 	if (handler->ibcounters &&
31973b3233fbSRaed Salem 	    atomic_read(&handler->ibcounters->usecnt) == 1)
31983b3233fbSRaed Salem 		counters_clear_description(handler->ibcounters);
3199038d2ef8SMaor Gottlieb 
32003b3233fbSRaed Salem 	mutex_unlock(&dev->flow_db->lock);
3201d4be3f44SYishai Hadas 	if (handler->flow_matcher)
3202d4be3f44SYishai Hadas 		atomic_dec(&handler->flow_matcher->usecnt);
3203038d2ef8SMaor Gottlieb 	kfree(handler);
3204038d2ef8SMaor Gottlieb 
3205038d2ef8SMaor Gottlieb 	return 0;
3206038d2ef8SMaor Gottlieb }
3207038d2ef8SMaor Gottlieb 
320835d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
320935d19011SMaor Gottlieb {
321035d19011SMaor Gottlieb 	priority *= 2;
321135d19011SMaor Gottlieb 	if (!dont_trap)
321235d19011SMaor Gottlieb 		priority++;
321335d19011SMaor Gottlieb 	return priority;
321435d19011SMaor Gottlieb }
321535d19011SMaor Gottlieb 
3216cc0e5d42SMaor Gottlieb enum flow_table_type {
3217cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
3218cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
3219cc0e5d42SMaor Gottlieb };
3220cc0e5d42SMaor Gottlieb 
322100b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
322200b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
3223d4be3f44SYishai Hadas 
3224d4be3f44SYishai Hadas static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3225d4be3f44SYishai Hadas 					   struct mlx5_ib_flow_prio *prio,
3226d4be3f44SYishai Hadas 					   int priority,
32274adda112SMark Bloch 					   int num_entries, int num_groups,
32284adda112SMark Bloch 					   u32 flags)
3229d4be3f44SYishai Hadas {
3230d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft;
3231d4be3f44SYishai Hadas 
3232d4be3f44SYishai Hadas 	ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3233d4be3f44SYishai Hadas 						 num_entries,
3234d4be3f44SYishai Hadas 						 num_groups,
32354adda112SMark Bloch 						 0, flags);
3236d4be3f44SYishai Hadas 	if (IS_ERR(ft))
3237d4be3f44SYishai Hadas 		return ERR_CAST(ft);
3238d4be3f44SYishai Hadas 
3239d4be3f44SYishai Hadas 	prio->flow_table = ft;
3240d4be3f44SYishai Hadas 	prio->refcount = 0;
3241d4be3f44SYishai Hadas 	return prio;
3242d4be3f44SYishai Hadas }
3243d4be3f44SYishai Hadas 
3244038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
3245cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
3246cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
3247038d2ef8SMaor Gottlieb {
324835d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
3249038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
3250038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
3251038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
3252dac388efSMaor Gottlieb 	int max_table_size;
3253038d2ef8SMaor Gottlieb 	int num_entries;
3254038d2ef8SMaor Gottlieb 	int num_groups;
32554adda112SMark Bloch 	u32 flags = 0;
3256038d2ef8SMaor Gottlieb 	int priority;
3257038d2ef8SMaor Gottlieb 
3258dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3259dac388efSMaor Gottlieb 						       log_max_ft_size));
3260038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
326178dd0c43SMark Bloch 		enum mlx5_flow_namespace_type fn_type;
326278dd0c43SMark Bloch 
326378dd0c43SMark Bloch 		if (flow_is_multicast_only(flow_attr) &&
326435d19011SMaor Gottlieb 		    !dont_trap)
3265038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
3266038d2ef8SMaor Gottlieb 		else
326735d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
326835d19011SMaor Gottlieb 							dont_trap);
326978dd0c43SMark Bloch 		if (ft_type == MLX5_IB_FT_RX) {
327078dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
327178dd0c43SMark Bloch 			prio = &dev->flow_db->prios[priority];
32726a4d00beSMark Bloch 			if (!dev->is_rep &&
32734adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
32744adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
32756a4d00beSMark Bloch 			if (!dev->is_rep &&
32765c2db53fSMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
32775c2db53fSMark Bloch 					reformat_l3_tunnel_to_l2))
32785c2db53fSMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
327978dd0c43SMark Bloch 		} else {
328078dd0c43SMark Bloch 			max_table_size =
328178dd0c43SMark Bloch 				BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
328278dd0c43SMark Bloch 							      log_max_ft_size));
328378dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
328478dd0c43SMark Bloch 			prio = &dev->flow_db->egress_prios[priority];
32856a4d00beSMark Bloch 			if (!dev->is_rep &&
32864adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
32874adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
328878dd0c43SMark Bloch 		}
328978dd0c43SMark Bloch 		ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
3290038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
3291038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
3292038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3293038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3294038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
3295038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
3296038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
3297038d2ef8SMaor Gottlieb 					 &num_entries,
3298038d2ef8SMaor Gottlieb 					 &num_groups);
32999a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
3300cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3301cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3302cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
3303cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
3304cc0e5d42SMaor Gottlieb 
3305cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3306cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3307cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3308cc0e5d42SMaor Gottlieb 
33099a4ca38dSMark Bloch 		prio = &dev->flow_db->sniffer[ft_type];
3310cc0e5d42SMaor Gottlieb 		priority = 0;
3311cc0e5d42SMaor Gottlieb 		num_entries = 1;
3312cc0e5d42SMaor Gottlieb 		num_groups = 1;
3313038d2ef8SMaor Gottlieb 	}
3314038d2ef8SMaor Gottlieb 
3315038d2ef8SMaor Gottlieb 	if (!ns)
3316038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
3317038d2ef8SMaor Gottlieb 
33183b70508aSMark Bloch 	max_table_size = min_t(int, num_entries, max_table_size);
3319dac388efSMaor Gottlieb 
3320038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
3321d4be3f44SYishai Hadas 	if (!ft)
33223b70508aSMark Bloch 		return _get_prio(ns, prio, priority, max_table_size, num_groups,
33234adda112SMark Bloch 				 flags);
3324038d2ef8SMaor Gottlieb 
3325d4be3f44SYishai Hadas 	return prio;
3326038d2ef8SMaor Gottlieb }
3327038d2ef8SMaor Gottlieb 
3328a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
3329a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
3330a550ddfcSYishai Hadas 			    u32 underlay_qpn)
3331a550ddfcSYishai Hadas {
3332a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3333a550ddfcSYishai Hadas 					   spec->match_criteria,
3334a550ddfcSYishai Hadas 					   misc_parameters);
3335a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3336a550ddfcSYishai Hadas 					   misc_parameters);
3337a550ddfcSYishai Hadas 
3338a550ddfcSYishai Hadas 	if (underlay_qpn &&
3339a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3340a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
3341a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3342a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
3343a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3344a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
3345a550ddfcSYishai Hadas 	}
3346a550ddfcSYishai Hadas }
3347a550ddfcSYishai Hadas 
33485e95af5fSRaed Salem static int read_flow_counters(struct ib_device *ibdev,
33495e95af5fSRaed Salem 			      struct mlx5_read_counters_attr *read_attr)
33505e95af5fSRaed Salem {
33515e95af5fSRaed Salem 	struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
33525e95af5fSRaed Salem 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
33535e95af5fSRaed Salem 
33545e95af5fSRaed Salem 	return mlx5_fc_query(dev->mdev, fc,
33555e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_PACKETS],
33565e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_BYTES]);
33575e95af5fSRaed Salem }
33585e95af5fSRaed Salem 
33595e95af5fSRaed Salem /* flow counters currently expose two counters packets and bytes */
33605e95af5fSRaed Salem #define FLOW_COUNTERS_NUM 2
33613b3233fbSRaed Salem static int counters_set_description(struct ib_counters *counters,
33623b3233fbSRaed Salem 				    enum mlx5_ib_counters_type counters_type,
33633b3233fbSRaed Salem 				    struct mlx5_ib_flow_counters_desc *desc_data,
33643b3233fbSRaed Salem 				    u32 ncounters)
33653b3233fbSRaed Salem {
33663b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
33673b3233fbSRaed Salem 	u32 cntrs_max_index = 0;
33683b3233fbSRaed Salem 	int i;
33693b3233fbSRaed Salem 
33703b3233fbSRaed Salem 	if (counters_type != MLX5_IB_COUNTERS_FLOW)
33713b3233fbSRaed Salem 		return -EINVAL;
33723b3233fbSRaed Salem 
33733b3233fbSRaed Salem 	/* init the fields for the object */
33743b3233fbSRaed Salem 	mcounters->type = counters_type;
33755e95af5fSRaed Salem 	mcounters->read_counters = read_flow_counters;
33765e95af5fSRaed Salem 	mcounters->counters_num = FLOW_COUNTERS_NUM;
33773b3233fbSRaed Salem 	mcounters->ncounters = ncounters;
33783b3233fbSRaed Salem 	/* each counter entry have both description and index pair */
33793b3233fbSRaed Salem 	for (i = 0; i < ncounters; i++) {
33803b3233fbSRaed Salem 		if (desc_data[i].description > IB_COUNTER_BYTES)
33813b3233fbSRaed Salem 			return -EINVAL;
33823b3233fbSRaed Salem 
33833b3233fbSRaed Salem 		if (cntrs_max_index <= desc_data[i].index)
33843b3233fbSRaed Salem 			cntrs_max_index = desc_data[i].index + 1;
33853b3233fbSRaed Salem 	}
33863b3233fbSRaed Salem 
33873b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
33883b3233fbSRaed Salem 	mcounters->counters_data = desc_data;
33893b3233fbSRaed Salem 	mcounters->cntrs_max_index = cntrs_max_index;
33903b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
33913b3233fbSRaed Salem 
33923b3233fbSRaed Salem 	return 0;
33933b3233fbSRaed Salem }
33943b3233fbSRaed Salem 
33953b3233fbSRaed Salem #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
33963b3233fbSRaed Salem static int flow_counters_set_data(struct ib_counters *ibcounters,
33973b3233fbSRaed Salem 				  struct mlx5_ib_create_flow *ucmd)
33983b3233fbSRaed Salem {
33993b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
34003b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
34013b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc_data = NULL;
34023b3233fbSRaed Salem 	bool hw_hndl = false;
34033b3233fbSRaed Salem 	int ret = 0;
34043b3233fbSRaed Salem 
34053b3233fbSRaed Salem 	if (ucmd && ucmd->ncounters_data != 0) {
34063b3233fbSRaed Salem 		cntrs_data = ucmd->data;
34073b3233fbSRaed Salem 		if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
34083b3233fbSRaed Salem 			return -EINVAL;
34093b3233fbSRaed Salem 
34103b3233fbSRaed Salem 		desc_data = kcalloc(cntrs_data->ncounters,
34113b3233fbSRaed Salem 				    sizeof(*desc_data),
34123b3233fbSRaed Salem 				    GFP_KERNEL);
34133b3233fbSRaed Salem 		if (!desc_data)
34143b3233fbSRaed Salem 			return  -ENOMEM;
34153b3233fbSRaed Salem 
34163b3233fbSRaed Salem 		if (copy_from_user(desc_data,
34173b3233fbSRaed Salem 				   u64_to_user_ptr(cntrs_data->counters_data),
34183b3233fbSRaed Salem 				   sizeof(*desc_data) * cntrs_data->ncounters)) {
34193b3233fbSRaed Salem 			ret = -EFAULT;
34203b3233fbSRaed Salem 			goto free;
34213b3233fbSRaed Salem 		}
34223b3233fbSRaed Salem 	}
34233b3233fbSRaed Salem 
34243b3233fbSRaed Salem 	if (!mcounters->hw_cntrs_hndl) {
34253b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = mlx5_fc_create(
34263b3233fbSRaed Salem 			to_mdev(ibcounters->device)->mdev, false);
3427e31abf76Sweiyongjun (A) 		if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3428e31abf76Sweiyongjun (A) 			ret = PTR_ERR(mcounters->hw_cntrs_hndl);
34293b3233fbSRaed Salem 			goto free;
34303b3233fbSRaed Salem 		}
34313b3233fbSRaed Salem 		hw_hndl = true;
34323b3233fbSRaed Salem 	}
34333b3233fbSRaed Salem 
34343b3233fbSRaed Salem 	if (desc_data) {
34353b3233fbSRaed Salem 		/* counters already bound to at least one flow */
34363b3233fbSRaed Salem 		if (mcounters->cntrs_max_index) {
34373b3233fbSRaed Salem 			ret = -EINVAL;
34383b3233fbSRaed Salem 			goto free_hndl;
34393b3233fbSRaed Salem 		}
34403b3233fbSRaed Salem 
34413b3233fbSRaed Salem 		ret = counters_set_description(ibcounters,
34423b3233fbSRaed Salem 					       MLX5_IB_COUNTERS_FLOW,
34433b3233fbSRaed Salem 					       desc_data,
34443b3233fbSRaed Salem 					       cntrs_data->ncounters);
34453b3233fbSRaed Salem 		if (ret)
34463b3233fbSRaed Salem 			goto free_hndl;
34473b3233fbSRaed Salem 
34483b3233fbSRaed Salem 	} else if (!mcounters->cntrs_max_index) {
34493b3233fbSRaed Salem 		/* counters not bound yet, must have udata passed */
34503b3233fbSRaed Salem 		ret = -EINVAL;
34513b3233fbSRaed Salem 		goto free_hndl;
34523b3233fbSRaed Salem 	}
34533b3233fbSRaed Salem 
34543b3233fbSRaed Salem 	return 0;
34553b3233fbSRaed Salem 
34563b3233fbSRaed Salem free_hndl:
34573b3233fbSRaed Salem 	if (hw_hndl) {
34583b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
34593b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
34603b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = NULL;
34613b3233fbSRaed Salem 	}
34623b3233fbSRaed Salem free:
34633b3233fbSRaed Salem 	kfree(desc_data);
34643b3233fbSRaed Salem 	return ret;
34653b3233fbSRaed Salem }
34663b3233fbSRaed Salem 
3467a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3468038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
3469dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
3470a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
34713b3233fbSRaed Salem 						      u32 underlay_qpn,
34723b3233fbSRaed Salem 						      struct mlx5_ib_create_flow *ucmd)
3473038d2ef8SMaor Gottlieb {
3474038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
3475038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
3476075572d4SBoris Pismenny 	struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
3477c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
34783b3233fbSRaed Salem 	struct mlx5_flow_destination dest_arr[2] = {};
34793b3233fbSRaed Salem 	struct mlx5_flow_destination *rule_dst = dest_arr;
3480dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3481038d2ef8SMaor Gottlieb 	unsigned int spec_index;
348271c6e863SAriel Levkovich 	u32 prev_type = 0;
3483038d2ef8SMaor Gottlieb 	int err = 0;
34843b3233fbSRaed Salem 	int dest_num = 0;
3485802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3486038d2ef8SMaor Gottlieb 
348719cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
3488038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3489038d2ef8SMaor Gottlieb 
34906a4d00beSMark Bloch 	if (dev->is_rep && is_egress)
349178dd0c43SMark Bloch 		return ERR_PTR(-EINVAL);
349278dd0c43SMark Bloch 
34931b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3494038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3495c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
3496038d2ef8SMaor Gottlieb 		err = -ENOMEM;
3497038d2ef8SMaor Gottlieb 		goto free;
3498038d2ef8SMaor Gottlieb 	}
3499038d2ef8SMaor Gottlieb 
3500038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
35013b3233fbSRaed Salem 	if (dst) {
35023b3233fbSRaed Salem 		memcpy(&dest_arr[0], dst, sizeof(*dst));
35033b3233fbSRaed Salem 		dest_num++;
35043b3233fbSRaed Salem 	}
3505038d2ef8SMaor Gottlieb 
3506038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
350719cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
3508a22ed86cSSlava Shwartsman 				      spec->match_value,
350971c6e863SAriel Levkovich 				      ib_flow, flow_attr, &flow_act,
351071c6e863SAriel Levkovich 				      prev_type);
3511038d2ef8SMaor Gottlieb 		if (err < 0)
3512038d2ef8SMaor Gottlieb 			goto free;
3513038d2ef8SMaor Gottlieb 
351471c6e863SAriel Levkovich 		prev_type = ((union ib_flow_spec *)ib_flow)->type;
3515038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3516038d2ef8SMaor Gottlieb 	}
3517038d2ef8SMaor Gottlieb 
3518a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
3519a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
3520a550ddfcSYishai Hadas 
35216a4d00beSMark Bloch 	if (dev->is_rep) {
3522018a94eeSMark Bloch 		void *misc;
3523018a94eeSMark Bloch 
35246a4d00beSMark Bloch 		if (!dev->port[flow_attr->port - 1].rep) {
35256a4d00beSMark Bloch 			err = -EINVAL;
35266a4d00beSMark Bloch 			goto free;
35276a4d00beSMark Bloch 		}
3528018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3529018a94eeSMark Bloch 				    misc_parameters);
3530018a94eeSMark Bloch 		MLX5_SET(fte_match_set_misc, misc, source_port,
35316a4d00beSMark Bloch 			 dev->port[flow_attr->port - 1].rep->vport);
3532018a94eeSMark Bloch 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3533018a94eeSMark Bloch 				    misc_parameters);
3534018a94eeSMark Bloch 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3535018a94eeSMark Bloch 	}
3536018a94eeSMark Bloch 
3537466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3538802c2125SAviad Yehezkel 
3539802c2125SAviad Yehezkel 	if (is_egress &&
3540802c2125SAviad Yehezkel 	    !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3541802c2125SAviad Yehezkel 		err = -EINVAL;
3542802c2125SAviad Yehezkel 		goto free;
3543802c2125SAviad Yehezkel 	}
3544802c2125SAviad Yehezkel 
35453b3233fbSRaed Salem 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3546171c7625SMark Bloch 		struct mlx5_ib_mcounters *mcounters;
3547171c7625SMark Bloch 
35483b3233fbSRaed Salem 		err = flow_counters_set_data(flow_act.counters, ucmd);
35493b3233fbSRaed Salem 		if (err)
35503b3233fbSRaed Salem 			goto free;
35513b3233fbSRaed Salem 
3552171c7625SMark Bloch 		mcounters = to_mcounters(flow_act.counters);
35533b3233fbSRaed Salem 		handler->ibcounters = flow_act.counters;
35543b3233fbSRaed Salem 		dest_arr[dest_num].type =
35553b3233fbSRaed Salem 			MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3556171c7625SMark Bloch 		dest_arr[dest_num].counter_id =
3557171c7625SMark Bloch 			mlx5_fc_id(mcounters->hw_cntrs_hndl);
35583b3233fbSRaed Salem 		dest_num++;
35593b3233fbSRaed Salem 	}
35603b3233fbSRaed Salem 
3561075572d4SBoris Pismenny 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
35623b3233fbSRaed Salem 		if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3563a22ed86cSSlava Shwartsman 			rule_dst = NULL;
3564a22ed86cSSlava Shwartsman 			dest_num = 0;
35653b3233fbSRaed Salem 		}
3566a22ed86cSSlava Shwartsman 	} else {
3567802c2125SAviad Yehezkel 		if (is_egress)
3568802c2125SAviad Yehezkel 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3569802c2125SAviad Yehezkel 		else
3570802c2125SAviad Yehezkel 			flow_act.action |=
35713b3233fbSRaed Salem 				dest_num ?  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
357235d19011SMaor Gottlieb 					MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3573a22ed86cSSlava Shwartsman 	}
35742ac693f9SMoses Reuben 
3575d5634feeSPaul Blakey 	if ((flow_act.flags & FLOW_ACT_HAS_TAG)  &&
35762ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
35772ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
35782ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3579075572d4SBoris Pismenny 			     flow_act.flow_tag, flow_attr->type);
35802ac693f9SMoses Reuben 		err = -EINVAL;
35812ac693f9SMoses Reuben 		goto free;
35822ac693f9SMoses Reuben 	}
358374491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
358466958ed9SHadar Hen Zion 					    &flow_act,
3585a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
3586038d2ef8SMaor Gottlieb 
3587038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
3588038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
3589038d2ef8SMaor Gottlieb 		goto free;
3590038d2ef8SMaor Gottlieb 	}
3591038d2ef8SMaor Gottlieb 
3592d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
35935497adc6SMaor Gottlieb 	handler->prio = ft_prio;
3594d4be3f44SYishai Hadas 	handler->dev = dev;
3595038d2ef8SMaor Gottlieb 
3596038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
3597038d2ef8SMaor Gottlieb free:
35983b3233fbSRaed Salem 	if (err && handler) {
35993b3233fbSRaed Salem 		if (handler->ibcounters &&
36003b3233fbSRaed Salem 		    atomic_read(&handler->ibcounters->usecnt) == 1)
36013b3233fbSRaed Salem 			counters_clear_description(handler->ibcounters);
3602038d2ef8SMaor Gottlieb 		kfree(handler);
36033b3233fbSRaed Salem 	}
3604c5bb1730SMaor Gottlieb 	kvfree(spec);
3605038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
3606038d2ef8SMaor Gottlieb }
3607038d2ef8SMaor Gottlieb 
3608a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3609a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
3610a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
3611a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
3612a550ddfcSYishai Hadas {
36133b3233fbSRaed Salem 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
3614a550ddfcSYishai Hadas }
3615a550ddfcSYishai Hadas 
361635d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
361735d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
361835d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
361935d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
362035d19011SMaor Gottlieb {
362135d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
362235d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
362335d19011SMaor Gottlieb 
362435d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
362535d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
362635d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
362735d19011SMaor Gottlieb 					       flow_attr, dst);
362835d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
362974491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3630d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
363135d19011SMaor Gottlieb 			kfree(handler);
363235d19011SMaor Gottlieb 			handler = handler_dst;
363335d19011SMaor Gottlieb 		} else {
363435d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
363535d19011SMaor Gottlieb 		}
363635d19011SMaor Gottlieb 	}
363735d19011SMaor Gottlieb 
363835d19011SMaor Gottlieb 	return handler;
363935d19011SMaor Gottlieb }
3640038d2ef8SMaor Gottlieb enum {
3641038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
3642038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
3643038d2ef8SMaor Gottlieb };
3644038d2ef8SMaor Gottlieb 
3645038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3646038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
3647038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
3648038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
3649038d2ef8SMaor Gottlieb {
3650038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
3651038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3652038d2ef8SMaor Gottlieb 
3653038d2ef8SMaor Gottlieb 	static struct {
3654038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
3655038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
3656038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
3657038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
3658038d2ef8SMaor Gottlieb 			.flow_attr = {
3659038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3660038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3661038d2ef8SMaor Gottlieb 			},
3662038d2ef8SMaor Gottlieb 			.eth_flow = {
3663038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3664038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3665038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3666038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
3667038d2ef8SMaor Gottlieb 			}
3668038d2ef8SMaor Gottlieb 		},
3669038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
3670038d2ef8SMaor Gottlieb 			.flow_attr = {
3671038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3672038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3673038d2ef8SMaor Gottlieb 			},
3674038d2ef8SMaor Gottlieb 			.eth_flow = {
3675038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3676038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3677038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3678038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
3679038d2ef8SMaor Gottlieb 			}
3680038d2ef8SMaor Gottlieb 		}
3681038d2ef8SMaor Gottlieb 	};
3682038d2ef8SMaor Gottlieb 
3683038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
3684038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
3685038d2ef8SMaor Gottlieb 				   dst);
3686038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
3687038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3688038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
3689038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3690038d2ef8SMaor Gottlieb 						 dst);
3691038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
369274491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3693d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
3694038d2ef8SMaor Gottlieb 			kfree(handler);
3695038d2ef8SMaor Gottlieb 			handler = handler_ucast;
3696038d2ef8SMaor Gottlieb 		} else {
3697038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
3698038d2ef8SMaor Gottlieb 		}
3699038d2ef8SMaor Gottlieb 	}
3700038d2ef8SMaor Gottlieb 
3701038d2ef8SMaor Gottlieb 	return handler;
3702038d2ef8SMaor Gottlieb }
3703038d2ef8SMaor Gottlieb 
3704cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3705cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
3706cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
3707cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
3708cc0e5d42SMaor Gottlieb {
3709cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
3710cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
3711cc0e5d42SMaor Gottlieb 	int err;
3712cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
3713cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
3714cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
3715cc0e5d42SMaor Gottlieb 	};
3716cc0e5d42SMaor Gottlieb 
3717cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3718cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
3719cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
3720cc0e5d42SMaor Gottlieb 		goto err;
3721cc0e5d42SMaor Gottlieb 	}
3722cc0e5d42SMaor Gottlieb 
3723cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3724cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
3725cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
3726cc0e5d42SMaor Gottlieb 		goto err_tx;
3727cc0e5d42SMaor Gottlieb 	}
3728cc0e5d42SMaor Gottlieb 
3729cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
3730cc0e5d42SMaor Gottlieb 
3731cc0e5d42SMaor Gottlieb 	return handler_rx;
3732cc0e5d42SMaor Gottlieb 
3733cc0e5d42SMaor Gottlieb err_tx:
373474491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
3735cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
3736cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
3737cc0e5d42SMaor Gottlieb err:
3738cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
3739cc0e5d42SMaor Gottlieb }
3740cc0e5d42SMaor Gottlieb 
3741038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3742038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
374359082a32SMatan Barak 					   int domain,
374459082a32SMatan Barak 					   struct ib_udata *udata)
3745038d2ef8SMaor Gottlieb {
3746038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3747d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
3748038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3749038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
3750cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3751038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
3752802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
37533b3233fbSRaed Salem 	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
37543b3233fbSRaed Salem 	size_t min_ucmd_sz, required_ucmd_sz;
3755038d2ef8SMaor Gottlieb 	int err;
3756a550ddfcSYishai Hadas 	int underlay_qpn;
3757038d2ef8SMaor Gottlieb 
37583b3233fbSRaed Salem 	if (udata && udata->inlen) {
37593b3233fbSRaed Salem 		min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
37603b3233fbSRaed Salem 				sizeof(ucmd_hdr.reserved);
37613b3233fbSRaed Salem 		if (udata->inlen < min_ucmd_sz)
376259082a32SMatan Barak 			return ERR_PTR(-EOPNOTSUPP);
376359082a32SMatan Barak 
37643b3233fbSRaed Salem 		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
37653b3233fbSRaed Salem 		if (err)
37663b3233fbSRaed Salem 			return ERR_PTR(err);
37673b3233fbSRaed Salem 
37683b3233fbSRaed Salem 		/* currently supports only one counters data */
37693b3233fbSRaed Salem 		if (ucmd_hdr.ncounters_data > 1)
37703b3233fbSRaed Salem 			return ERR_PTR(-EINVAL);
37713b3233fbSRaed Salem 
37723b3233fbSRaed Salem 		required_ucmd_sz = min_ucmd_sz +
37733b3233fbSRaed Salem 			sizeof(struct mlx5_ib_flow_counters_data) *
37743b3233fbSRaed Salem 			ucmd_hdr.ncounters_data;
37753b3233fbSRaed Salem 		if (udata->inlen > required_ucmd_sz &&
37763b3233fbSRaed Salem 		    !ib_is_udata_cleared(udata, required_ucmd_sz,
37773b3233fbSRaed Salem 					 udata->inlen - required_ucmd_sz))
37783b3233fbSRaed Salem 			return ERR_PTR(-EOPNOTSUPP);
37793b3233fbSRaed Salem 
37803b3233fbSRaed Salem 		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
37813b3233fbSRaed Salem 		if (!ucmd)
37823b3233fbSRaed Salem 			return ERR_PTR(-ENOMEM);
37833b3233fbSRaed Salem 
37843b3233fbSRaed Salem 		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
3785299eafeeSGustavo A. R. Silva 		if (err)
3786299eafeeSGustavo A. R. Silva 			goto free_ucmd;
37873b3233fbSRaed Salem 	}
37883b3233fbSRaed Salem 
3789299eafeeSGustavo A. R. Silva 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3790299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3791299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3792299eafeeSGustavo A. R. Silva 	}
3793038d2ef8SMaor Gottlieb 
3794038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
3795508562d6SDaniel Jurgens 	    flow_attr->port > dev->num_ports ||
3796802c2125SAviad Yehezkel 	    (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3797299eafeeSGustavo A. R. Silva 				  IB_FLOW_ATTR_FLAGS_EGRESS))) {
3798299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3799299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3800299eafeeSGustavo A. R. Silva 	}
3801802c2125SAviad Yehezkel 
3802802c2125SAviad Yehezkel 	if (is_egress &&
3803802c2125SAviad Yehezkel 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3804299eafeeSGustavo A. R. Silva 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3805299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3806299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3807299eafeeSGustavo A. R. Silva 	}
3808038d2ef8SMaor Gottlieb 
3809038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3810299eafeeSGustavo A. R. Silva 	if (!dst) {
3811299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3812299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3813299eafeeSGustavo A. R. Silva 	}
3814038d2ef8SMaor Gottlieb 
38159a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3816038d2ef8SMaor Gottlieb 
3817802c2125SAviad Yehezkel 	ft_prio = get_flow_table(dev, flow_attr,
3818802c2125SAviad Yehezkel 				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3819038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
3820038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
3821038d2ef8SMaor Gottlieb 		goto unlock;
3822038d2ef8SMaor Gottlieb 	}
3823cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3824cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3825cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
3826cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
3827cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
3828cc0e5d42SMaor Gottlieb 			goto destroy_ft;
3829cc0e5d42SMaor Gottlieb 		}
3830cc0e5d42SMaor Gottlieb 	}
3831038d2ef8SMaor Gottlieb 
3832802c2125SAviad Yehezkel 	if (is_egress) {
3833802c2125SAviad Yehezkel 		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3834802c2125SAviad Yehezkel 	} else {
3835038d2ef8SMaor Gottlieb 		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3836d9f88e5aSYishai Hadas 		if (mqp->flags & MLX5_IB_QP_RSS)
3837d9f88e5aSYishai Hadas 			dst->tir_num = mqp->rss_qp.tirn;
3838d9f88e5aSYishai Hadas 		else
3839d9f88e5aSYishai Hadas 			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3840802c2125SAviad Yehezkel 	}
3841038d2ef8SMaor Gottlieb 
3842038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
384335d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
384435d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
384535d19011SMaor Gottlieb 							flow_attr, dst);
384635d19011SMaor Gottlieb 		} else {
3847a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3848a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
3849a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
38503b3233fbSRaed Salem 						    dst, underlay_qpn, ucmd);
385135d19011SMaor Gottlieb 		}
3852038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3853038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3854038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3855038d2ef8SMaor Gottlieb 						dst);
3856cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3857cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3858038d2ef8SMaor Gottlieb 	} else {
3859038d2ef8SMaor Gottlieb 		err = -EINVAL;
3860038d2ef8SMaor Gottlieb 		goto destroy_ft;
3861038d2ef8SMaor Gottlieb 	}
3862038d2ef8SMaor Gottlieb 
3863038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
3864038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
3865038d2ef8SMaor Gottlieb 		handler = NULL;
3866038d2ef8SMaor Gottlieb 		goto destroy_ft;
3867038d2ef8SMaor Gottlieb 	}
3868038d2ef8SMaor Gottlieb 
38699a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3870038d2ef8SMaor Gottlieb 	kfree(dst);
38713b3233fbSRaed Salem 	kfree(ucmd);
3872038d2ef8SMaor Gottlieb 
3873038d2ef8SMaor Gottlieb 	return &handler->ibflow;
3874038d2ef8SMaor Gottlieb 
3875038d2ef8SMaor Gottlieb destroy_ft:
3876038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
3877cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
3878cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
3879038d2ef8SMaor Gottlieb unlock:
38809a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3881038d2ef8SMaor Gottlieb 	kfree(dst);
3882299eafeeSGustavo A. R. Silva free_ucmd:
38833b3233fbSRaed Salem 	kfree(ucmd);
3884038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
3885038d2ef8SMaor Gottlieb }
3886038d2ef8SMaor Gottlieb 
3887b47fd4ffSMark Bloch static struct mlx5_ib_flow_prio *
3888b47fd4ffSMark Bloch _get_flow_table(struct mlx5_ib_dev *dev,
3889b47fd4ffSMark Bloch 		struct mlx5_ib_flow_matcher *fs_matcher,
3890b47fd4ffSMark Bloch 		bool mcast)
3891d4be3f44SYishai Hadas {
3892d4be3f44SYishai Hadas 	struct mlx5_flow_namespace *ns = NULL;
389313a43765SMark Bloch 	struct mlx5_ib_flow_prio *prio = NULL;
389413a43765SMark Bloch 	int max_table_size = 0;
3895b47fd4ffSMark Bloch 	u32 flags = 0;
3896b47fd4ffSMark Bloch 	int priority;
3897d4be3f44SYishai Hadas 
389813a43765SMark Bloch 	if (mcast)
389913a43765SMark Bloch 		priority = MLX5_IB_FLOW_MCAST_PRIO;
390013a43765SMark Bloch 	else
390113a43765SMark Bloch 		priority = ib_prio_to_core_prio(fs_matcher->priority, false);
390213a43765SMark Bloch 
3903b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
3904d4be3f44SYishai Hadas 		max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3905d4be3f44SYishai Hadas 					log_max_ft_size));
3906b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3907b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3908b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3909b47fd4ffSMark Bloch 					      reformat_l3_tunnel_to_l2))
3910b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
391113a43765SMark Bloch 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS) {
391213a43765SMark Bloch 		max_table_size = BIT(
391313a43765SMark Bloch 			MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, log_max_ft_size));
3914b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3915b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
391613a43765SMark Bloch 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB) {
391713a43765SMark Bloch 		max_table_size = BIT(
391813a43765SMark Bloch 			MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, log_max_ft_size));
391913a43765SMark Bloch 		priority = FDB_BYPASS_PATH;
3920b47fd4ffSMark Bloch 	}
3921b47fd4ffSMark Bloch 
39223b70508aSMark Bloch 	max_table_size = min_t(int, max_table_size, MLX5_FS_MAX_ENTRIES);
3923d4be3f44SYishai Hadas 
3924b47fd4ffSMark Bloch 	ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
3925d4be3f44SYishai Hadas 	if (!ns)
3926d4be3f44SYishai Hadas 		return ERR_PTR(-ENOTSUPP);
3927d4be3f44SYishai Hadas 
3928b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
3929d4be3f44SYishai Hadas 		prio = &dev->flow_db->prios[priority];
393013a43765SMark Bloch 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS)
3931b47fd4ffSMark Bloch 		prio = &dev->flow_db->egress_prios[priority];
393213a43765SMark Bloch 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB)
393313a43765SMark Bloch 		prio = &dev->flow_db->fdb;
393413a43765SMark Bloch 
393513a43765SMark Bloch 	if (!prio)
393613a43765SMark Bloch 		return ERR_PTR(-EINVAL);
3937d4be3f44SYishai Hadas 
3938d4be3f44SYishai Hadas 	if (prio->flow_table)
3939d4be3f44SYishai Hadas 		return prio;
3940d4be3f44SYishai Hadas 
39413b70508aSMark Bloch 	return _get_prio(ns, prio, priority, max_table_size,
3942b47fd4ffSMark Bloch 			 MLX5_FS_MAX_TYPES, flags);
3943d4be3f44SYishai Hadas }
3944d4be3f44SYishai Hadas 
3945d4be3f44SYishai Hadas static struct mlx5_ib_flow_handler *
3946d4be3f44SYishai Hadas _create_raw_flow_rule(struct mlx5_ib_dev *dev,
3947d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_prio *ft_prio,
3948d4be3f44SYishai Hadas 		      struct mlx5_flow_destination *dst,
3949d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_matcher  *fs_matcher,
3950b823dd6dSMark Bloch 		      struct mlx5_flow_act *flow_act,
3951bfc5d839SMark Bloch 		      void *cmd_in, int inlen,
3952bfc5d839SMark Bloch 		      int dst_num)
3953d4be3f44SYishai Hadas {
3954d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
3955d4be3f44SYishai Hadas 	struct mlx5_flow_spec *spec;
3956d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft = ft_prio->flow_table;
3957d4be3f44SYishai Hadas 	int err = 0;
3958d4be3f44SYishai Hadas 
3959d4be3f44SYishai Hadas 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3960d4be3f44SYishai Hadas 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3961d4be3f44SYishai Hadas 	if (!handler || !spec) {
3962d4be3f44SYishai Hadas 		err = -ENOMEM;
3963d4be3f44SYishai Hadas 		goto free;
3964d4be3f44SYishai Hadas 	}
3965d4be3f44SYishai Hadas 
3966d4be3f44SYishai Hadas 	INIT_LIST_HEAD(&handler->list);
3967d4be3f44SYishai Hadas 
3968d4be3f44SYishai Hadas 	memcpy(spec->match_value, cmd_in, inlen);
3969d4be3f44SYishai Hadas 	memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
3970d4be3f44SYishai Hadas 	       fs_matcher->mask_len);
3971d4be3f44SYishai Hadas 	spec->match_criteria_enable = fs_matcher->match_criteria_enable;
3972d4be3f44SYishai Hadas 
3973d4be3f44SYishai Hadas 	handler->rule = mlx5_add_flow_rules(ft, spec,
3974bfc5d839SMark Bloch 					    flow_act, dst, dst_num);
3975d4be3f44SYishai Hadas 
3976d4be3f44SYishai Hadas 	if (IS_ERR(handler->rule)) {
3977d4be3f44SYishai Hadas 		err = PTR_ERR(handler->rule);
3978d4be3f44SYishai Hadas 		goto free;
3979d4be3f44SYishai Hadas 	}
3980d4be3f44SYishai Hadas 
3981d4be3f44SYishai Hadas 	ft_prio->refcount++;
3982d4be3f44SYishai Hadas 	handler->prio = ft_prio;
3983d4be3f44SYishai Hadas 	handler->dev = dev;
3984d4be3f44SYishai Hadas 	ft_prio->flow_table = ft;
3985d4be3f44SYishai Hadas 
3986d4be3f44SYishai Hadas free:
3987d4be3f44SYishai Hadas 	if (err)
3988d4be3f44SYishai Hadas 		kfree(handler);
3989d4be3f44SYishai Hadas 	kvfree(spec);
3990d4be3f44SYishai Hadas 	return err ? ERR_PTR(err) : handler;
3991d4be3f44SYishai Hadas }
3992d4be3f44SYishai Hadas 
3993d4be3f44SYishai Hadas static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
3994d4be3f44SYishai Hadas 				void *match_v)
3995d4be3f44SYishai Hadas {
3996d4be3f44SYishai Hadas 	void *match_c;
3997d4be3f44SYishai Hadas 	void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
3998d4be3f44SYishai Hadas 	void *dmac, *dmac_mask;
3999d4be3f44SYishai Hadas 	void *ipv4, *ipv4_mask;
4000d4be3f44SYishai Hadas 
4001d4be3f44SYishai Hadas 	if (!(fs_matcher->match_criteria_enable &
4002d4be3f44SYishai Hadas 	      (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
4003d4be3f44SYishai Hadas 		return false;
4004d4be3f44SYishai Hadas 
4005d4be3f44SYishai Hadas 	match_c = fs_matcher->matcher_mask.match_params;
4006d4be3f44SYishai Hadas 	match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
4007d4be3f44SYishai Hadas 					   outer_headers);
4008d4be3f44SYishai Hadas 	match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
4009d4be3f44SYishai Hadas 					   outer_headers);
4010d4be3f44SYishai Hadas 
4011d4be3f44SYishai Hadas 	dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4012d4be3f44SYishai Hadas 			    dmac_47_16);
4013d4be3f44SYishai Hadas 	dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4014d4be3f44SYishai Hadas 				 dmac_47_16);
4015d4be3f44SYishai Hadas 
4016d4be3f44SYishai Hadas 	if (is_multicast_ether_addr(dmac) &&
4017d4be3f44SYishai Hadas 	    is_multicast_ether_addr(dmac_mask))
4018d4be3f44SYishai Hadas 		return true;
4019d4be3f44SYishai Hadas 
4020d4be3f44SYishai Hadas 	ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4021d4be3f44SYishai Hadas 			    dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4022d4be3f44SYishai Hadas 
4023d4be3f44SYishai Hadas 	ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4024d4be3f44SYishai Hadas 				 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4025d4be3f44SYishai Hadas 
4026d4be3f44SYishai Hadas 	if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
4027d4be3f44SYishai Hadas 	    ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
4028d4be3f44SYishai Hadas 		return true;
4029d4be3f44SYishai Hadas 
4030d4be3f44SYishai Hadas 	return false;
4031d4be3f44SYishai Hadas }
4032d4be3f44SYishai Hadas 
403332269441SYishai Hadas struct mlx5_ib_flow_handler *
403432269441SYishai Hadas mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
403532269441SYishai Hadas 			struct mlx5_ib_flow_matcher *fs_matcher,
4036b823dd6dSMark Bloch 			struct mlx5_flow_act *flow_act,
4037bfc5d839SMark Bloch 			u32 counter_id,
403832269441SYishai Hadas 			void *cmd_in, int inlen, int dest_id,
403932269441SYishai Hadas 			int dest_type)
404032269441SYishai Hadas {
4041d4be3f44SYishai Hadas 	struct mlx5_flow_destination *dst;
4042d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *ft_prio;
4043d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
4044bfc5d839SMark Bloch 	int dst_num = 0;
4045d4be3f44SYishai Hadas 	bool mcast;
4046d4be3f44SYishai Hadas 	int err;
4047d4be3f44SYishai Hadas 
4048d4be3f44SYishai Hadas 	if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
404932269441SYishai Hadas 		return ERR_PTR(-EOPNOTSUPP);
4050d4be3f44SYishai Hadas 
4051d4be3f44SYishai Hadas 	if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
4052d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
4053d4be3f44SYishai Hadas 
40548e8aa145SGustavo A. R. Silva 	dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
4055d4be3f44SYishai Hadas 	if (!dst)
4056d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
4057d4be3f44SYishai Hadas 
4058d4be3f44SYishai Hadas 	mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
4059d4be3f44SYishai Hadas 	mutex_lock(&dev->flow_db->lock);
4060d4be3f44SYishai Hadas 
4061b47fd4ffSMark Bloch 	ft_prio = _get_flow_table(dev, fs_matcher, mcast);
4062d4be3f44SYishai Hadas 	if (IS_ERR(ft_prio)) {
4063d4be3f44SYishai Hadas 		err = PTR_ERR(ft_prio);
4064d4be3f44SYishai Hadas 		goto unlock;
4065d4be3f44SYishai Hadas 	}
4066d4be3f44SYishai Hadas 
40676346f0bfSYishai Hadas 	if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
4068bfc5d839SMark Bloch 		dst[dst_num].type = dest_type;
4069bfc5d839SMark Bloch 		dst[dst_num].tir_num = dest_id;
4070b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
4071a7ee18bdSMark Bloch 	} else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
4072bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
4073bfc5d839SMark Bloch 		dst[dst_num].ft_num = dest_id;
4074b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
4075a7ee18bdSMark Bloch 	} else {
4076bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
4077a7ee18bdSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
40786346f0bfSYishai Hadas 	}
40796346f0bfSYishai Hadas 
4080bfc5d839SMark Bloch 	dst_num++;
4081bfc5d839SMark Bloch 
4082bfc5d839SMark Bloch 	if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
4083bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
4084bfc5d839SMark Bloch 		dst[dst_num].counter_id = counter_id;
4085bfc5d839SMark Bloch 		dst_num++;
4086bfc5d839SMark Bloch 	}
4087bfc5d839SMark Bloch 
4088b823dd6dSMark Bloch 	handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher, flow_act,
4089bfc5d839SMark Bloch 					cmd_in, inlen, dst_num);
4090d4be3f44SYishai Hadas 
4091d4be3f44SYishai Hadas 	if (IS_ERR(handler)) {
4092d4be3f44SYishai Hadas 		err = PTR_ERR(handler);
4093d4be3f44SYishai Hadas 		goto destroy_ft;
4094d4be3f44SYishai Hadas 	}
4095d4be3f44SYishai Hadas 
4096d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
4097d4be3f44SYishai Hadas 	atomic_inc(&fs_matcher->usecnt);
4098d4be3f44SYishai Hadas 	handler->flow_matcher = fs_matcher;
4099d4be3f44SYishai Hadas 
4100d4be3f44SYishai Hadas 	kfree(dst);
4101d4be3f44SYishai Hadas 
4102d4be3f44SYishai Hadas 	return handler;
4103d4be3f44SYishai Hadas 
4104d4be3f44SYishai Hadas destroy_ft:
4105d4be3f44SYishai Hadas 	put_flow_table(dev, ft_prio, false);
4106d4be3f44SYishai Hadas unlock:
4107d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
4108d4be3f44SYishai Hadas 	kfree(dst);
4109d4be3f44SYishai Hadas 
4110d4be3f44SYishai Hadas 	return ERR_PTR(err);
411132269441SYishai Hadas }
411232269441SYishai Hadas 
4113c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
4114c6475a0bSAviad Yehezkel {
4115c6475a0bSAviad Yehezkel 	u32 flags = 0;
4116c6475a0bSAviad Yehezkel 
4117c6475a0bSAviad Yehezkel 	if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
4118c6475a0bSAviad Yehezkel 		flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
4119c6475a0bSAviad Yehezkel 
4120c6475a0bSAviad Yehezkel 	return flags;
4121c6475a0bSAviad Yehezkel }
4122c6475a0bSAviad Yehezkel 
4123c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED	MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
4124c6475a0bSAviad Yehezkel static struct ib_flow_action *
4125c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device,
4126c6475a0bSAviad Yehezkel 			       const struct ib_flow_action_attrs_esp *attr,
4127c6475a0bSAviad Yehezkel 			       struct uverbs_attr_bundle *attrs)
4128c6475a0bSAviad Yehezkel {
4129c6475a0bSAviad Yehezkel 	struct mlx5_ib_dev *mdev = to_mdev(device);
4130c6475a0bSAviad Yehezkel 	struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
4131c6475a0bSAviad Yehezkel 	struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
4132c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *action;
4133c6475a0bSAviad Yehezkel 	u64 action_flags;
4134c6475a0bSAviad Yehezkel 	u64 flags;
4135c6475a0bSAviad Yehezkel 	int err = 0;
4136c6475a0bSAviad Yehezkel 
4137bccd0622SJason Gunthorpe 	err = uverbs_get_flags64(
4138bccd0622SJason Gunthorpe 		&action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4139bccd0622SJason Gunthorpe 		((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
4140bccd0622SJason Gunthorpe 	if (err)
4141bccd0622SJason Gunthorpe 		return ERR_PTR(err);
4142c6475a0bSAviad Yehezkel 
4143c6475a0bSAviad Yehezkel 	flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
4144c6475a0bSAviad Yehezkel 
4145c6475a0bSAviad Yehezkel 	/* We current only support a subset of the standard features. Only a
4146c6475a0bSAviad Yehezkel 	 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
4147c6475a0bSAviad Yehezkel 	 * (with overlap). Full offload mode isn't supported.
4148c6475a0bSAviad Yehezkel 	 */
4149c6475a0bSAviad Yehezkel 	if (!attr->keymat || attr->replay || attr->encap ||
4150c6475a0bSAviad Yehezkel 	    attr->spi || attr->seq || attr->tfc_pad ||
4151c6475a0bSAviad Yehezkel 	    attr->hard_limit_pkts ||
4152c6475a0bSAviad Yehezkel 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4153c6475a0bSAviad Yehezkel 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
4154c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4155c6475a0bSAviad Yehezkel 
4156c6475a0bSAviad Yehezkel 	if (attr->keymat->protocol !=
4157c6475a0bSAviad Yehezkel 	    IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
4158c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4159c6475a0bSAviad Yehezkel 
4160c6475a0bSAviad Yehezkel 	aes_gcm = &attr->keymat->keymat.aes_gcm;
4161c6475a0bSAviad Yehezkel 
4162c6475a0bSAviad Yehezkel 	if (aes_gcm->icv_len != 16 ||
4163c6475a0bSAviad Yehezkel 	    aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
4164c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4165c6475a0bSAviad Yehezkel 
4166c6475a0bSAviad Yehezkel 	action = kmalloc(sizeof(*action), GFP_KERNEL);
4167c6475a0bSAviad Yehezkel 	if (!action)
4168c6475a0bSAviad Yehezkel 		return ERR_PTR(-ENOMEM);
4169c6475a0bSAviad Yehezkel 
4170c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4171c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
4172c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.aes_key));
4173c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
4174c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
4175c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.salt));
4176c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
4177c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
4178c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
4179c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
4180c6475a0bSAviad Yehezkel 	accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
4181c6475a0bSAviad Yehezkel 
4182c6475a0bSAviad Yehezkel 	accel_attrs.esn = attr->esn;
4183c6475a0bSAviad Yehezkel 	if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4184c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4185c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4186c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4187c6475a0bSAviad Yehezkel 
4188c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4189c6475a0bSAviad Yehezkel 		accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4190c6475a0bSAviad Yehezkel 
4191c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ctx =
4192c6475a0bSAviad Yehezkel 		mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4193c6475a0bSAviad Yehezkel 	if (IS_ERR(action->esp_aes_gcm.ctx)) {
4194c6475a0bSAviad Yehezkel 		err = PTR_ERR(action->esp_aes_gcm.ctx);
4195c6475a0bSAviad Yehezkel 		goto err_parse;
4196c6475a0bSAviad Yehezkel 	}
4197c6475a0bSAviad Yehezkel 
4198c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4199c6475a0bSAviad Yehezkel 
4200c6475a0bSAviad Yehezkel 	return &action->ib_action;
4201c6475a0bSAviad Yehezkel 
4202c6475a0bSAviad Yehezkel err_parse:
4203c6475a0bSAviad Yehezkel 	kfree(action);
4204c6475a0bSAviad Yehezkel 	return ERR_PTR(err);
4205c6475a0bSAviad Yehezkel }
4206c6475a0bSAviad Yehezkel 
4207349705c1SMatan Barak static int
4208349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4209349705c1SMatan Barak 			       const struct ib_flow_action_attrs_esp *attr,
4210349705c1SMatan Barak 			       struct uverbs_attr_bundle *attrs)
4211349705c1SMatan Barak {
4212349705c1SMatan Barak 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4213349705c1SMatan Barak 	struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4214349705c1SMatan Barak 	int err = 0;
4215349705c1SMatan Barak 
4216349705c1SMatan Barak 	if (attr->keymat || attr->replay || attr->encap ||
4217349705c1SMatan Barak 	    attr->spi || attr->seq || attr->tfc_pad ||
4218349705c1SMatan Barak 	    attr->hard_limit_pkts ||
4219349705c1SMatan Barak 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4220349705c1SMatan Barak 			     IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4221349705c1SMatan Barak 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4222349705c1SMatan Barak 		return -EOPNOTSUPP;
4223349705c1SMatan Barak 
4224349705c1SMatan Barak 	/* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4225349705c1SMatan Barak 	 * be modified.
4226349705c1SMatan Barak 	 */
4227349705c1SMatan Barak 	if (!(maction->esp_aes_gcm.ib_flags &
4228349705c1SMatan Barak 	      IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4229349705c1SMatan Barak 	    attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4230349705c1SMatan Barak 			   IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4231349705c1SMatan Barak 		return -EINVAL;
4232349705c1SMatan Barak 
4233349705c1SMatan Barak 	memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4234349705c1SMatan Barak 	       sizeof(accel_attrs));
4235349705c1SMatan Barak 
4236349705c1SMatan Barak 	accel_attrs.esn = attr->esn;
4237349705c1SMatan Barak 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4238349705c1SMatan Barak 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4239349705c1SMatan Barak 	else
4240349705c1SMatan Barak 		accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4241349705c1SMatan Barak 
4242349705c1SMatan Barak 	err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4243349705c1SMatan Barak 					 &accel_attrs);
4244349705c1SMatan Barak 	if (err)
4245349705c1SMatan Barak 		return err;
4246349705c1SMatan Barak 
4247349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags &=
4248349705c1SMatan Barak 		~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4249349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags |=
4250349705c1SMatan Barak 		attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4251349705c1SMatan Barak 
4252349705c1SMatan Barak 	return 0;
4253349705c1SMatan Barak }
4254349705c1SMatan Barak 
4255c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4256c6475a0bSAviad Yehezkel {
4257c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4258c6475a0bSAviad Yehezkel 
4259c6475a0bSAviad Yehezkel 	switch (action->type) {
4260c6475a0bSAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
4261c6475a0bSAviad Yehezkel 		/*
4262c6475a0bSAviad Yehezkel 		 * We only support aes_gcm by now, so we implicitly know this is
4263c6475a0bSAviad Yehezkel 		 * the underline crypto.
4264c6475a0bSAviad Yehezkel 		 */
4265c6475a0bSAviad Yehezkel 		mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4266c6475a0bSAviad Yehezkel 		break;
4267b4749bf2SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
4268b4749bf2SMark Bloch 		mlx5_ib_destroy_flow_action_raw(maction);
4269b4749bf2SMark Bloch 		break;
4270c6475a0bSAviad Yehezkel 	default:
4271c6475a0bSAviad Yehezkel 		WARN_ON(true);
4272c6475a0bSAviad Yehezkel 		break;
4273c6475a0bSAviad Yehezkel 	}
4274c6475a0bSAviad Yehezkel 
4275c6475a0bSAviad Yehezkel 	kfree(maction);
4276c6475a0bSAviad Yehezkel 	return 0;
4277c6475a0bSAviad Yehezkel }
4278c6475a0bSAviad Yehezkel 
4279e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4280e126ba97SEli Cohen {
4281e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
428281e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
4283e126ba97SEli Cohen 	int err;
4284539ec982SYishai Hadas 	u16 uid;
4285539ec982SYishai Hadas 
4286539ec982SYishai Hadas 	uid = ibqp->pd ?
4287539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4288e126ba97SEli Cohen 
428981e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
429081e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
429181e30880SYishai Hadas 		return -EOPNOTSUPP;
429281e30880SYishai Hadas 	}
429381e30880SYishai Hadas 
4294539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4295e126ba97SEli Cohen 	if (err)
4296e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4297e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4298e126ba97SEli Cohen 
4299e126ba97SEli Cohen 	return err;
4300e126ba97SEli Cohen }
4301e126ba97SEli Cohen 
4302e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4303e126ba97SEli Cohen {
4304e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4305e126ba97SEli Cohen 	int err;
4306539ec982SYishai Hadas 	u16 uid;
4307e126ba97SEli Cohen 
4308539ec982SYishai Hadas 	uid = ibqp->pd ?
4309539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4310539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4311e126ba97SEli Cohen 	if (err)
4312e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4313e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4314e126ba97SEli Cohen 
4315e126ba97SEli Cohen 	return err;
4316e126ba97SEli Cohen }
4317e126ba97SEli Cohen 
4318e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
4319e126ba97SEli Cohen {
43201b5daf11SMajd Dibbiny 	int err;
4321e126ba97SEli Cohen 
43221b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
4323e126ba97SEli Cohen 	if (err)
4324e126ba97SEli Cohen 		return err;
43251b5daf11SMajd Dibbiny 
43261b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
43271b5daf11SMajd Dibbiny 
43281b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
4329e126ba97SEli Cohen }
4330e126ba97SEli Cohen 
4331508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
4332508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4333e126ba97SEli Cohen {
4334e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
433554747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4336e126ba97SEli Cohen 
43379603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
4338e126ba97SEli Cohen }
4339508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
4340e126ba97SEli Cohen 
4341508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
4342e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
4343e126ba97SEli Cohen {
4344e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
434554747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4346e126ba97SEli Cohen 
43476aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
4348e126ba97SEli Cohen }
4349508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
4350e126ba97SEli Cohen 
4351508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
4352508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4353e126ba97SEli Cohen {
4354e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
435554747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
435654747231SParav Pandit 
43579603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
4358e126ba97SEli Cohen }
4359508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
4360e126ba97SEli Cohen 
4361508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
4362508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
4363e126ba97SEli Cohen {
4364e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
436554747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
436654747231SParav Pandit 
43679603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
4368e126ba97SEli Cohen }
4369508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
4370e126ba97SEli Cohen 
4371508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
4372508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4373e126ba97SEli Cohen {
4374e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
437554747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
437654747231SParav Pandit 
4377e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
43789603b61dSJack Morgenstein 		       dev->mdev->board_id);
4379e126ba97SEli Cohen }
4380508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
4381e126ba97SEli Cohen 
4382508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
4383508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
4384508a523fSParav Pandit 	&dev_attr_hca_type.attr,
4385508a523fSParav Pandit 	&dev_attr_board_id.attr,
4386508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
4387508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
4388508a523fSParav Pandit 	NULL,
4389508a523fSParav Pandit };
4390e126ba97SEli Cohen 
4391508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
4392508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
4393e126ba97SEli Cohen };
4394e126ba97SEli Cohen 
43957722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
43967722f47eSHaggai Eran {
43977722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
43987722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
43997722f47eSHaggai Eran 			     pkey_change_work);
44007722f47eSHaggai Eran 
44017722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
44027722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
44037722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
44047722f47eSHaggai Eran }
44057722f47eSHaggai Eran 
440689ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
440789ea94a7SMaor Gottlieb {
440889ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
440989ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
441089ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
441189ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
441289ea94a7SMaor Gottlieb 	unsigned long flags_qp;
441389ea94a7SMaor Gottlieb 	unsigned long flags_cq;
441489ea94a7SMaor Gottlieb 	unsigned long flags;
441589ea94a7SMaor Gottlieb 
441689ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
441789ea94a7SMaor Gottlieb 
441889ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
441989ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
442089ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
442189ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
442289ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
442389ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
442489ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
442589ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
442689ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
442789ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
442889ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
442989ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
443089ea94a7SMaor Gottlieb 						      &cq_armed_list);
443189ea94a7SMaor Gottlieb 				}
443289ea94a7SMaor Gottlieb 			}
443389ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
443489ea94a7SMaor Gottlieb 		}
443589ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
443689ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
443789ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
443889ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
443989ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
444089ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
444189ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
444289ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
444389ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
444489ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
444589ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
444689ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
444789ea94a7SMaor Gottlieb 							      &cq_armed_list);
444889ea94a7SMaor Gottlieb 					}
444989ea94a7SMaor Gottlieb 				}
445089ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
445189ea94a7SMaor Gottlieb 						       flags_cq);
445289ea94a7SMaor Gottlieb 			}
445389ea94a7SMaor Gottlieb 		}
445489ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
445589ea94a7SMaor Gottlieb 	}
445689ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
445789ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
445889ea94a7SMaor Gottlieb 	 */
445989ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
446089ea94a7SMaor Gottlieb 		mcq->comp(mcq);
446189ea94a7SMaor Gottlieb 	}
446289ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
446389ea94a7SMaor Gottlieb }
446489ea94a7SMaor Gottlieb 
446503404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
446603404e8aSMaor Gottlieb {
446703404e8aSMaor Gottlieb 	int err;
446803404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
446903404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
447003404e8aSMaor Gottlieb 			     delay_drop_work);
447103404e8aSMaor Gottlieb 
4472fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
4473fe248c3aSMaor Gottlieb 
447403404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
447503404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
447603404e8aSMaor Gottlieb 				       delay_drop->timeout);
447703404e8aSMaor Gottlieb 	if (err) {
447803404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
447903404e8aSMaor Gottlieb 			     delay_drop->timeout);
448003404e8aSMaor Gottlieb 		delay_drop->activate = false;
448103404e8aSMaor Gottlieb 	}
448203404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
448303404e8aSMaor Gottlieb }
448403404e8aSMaor Gottlieb 
448509e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
448609e574faSSaeed Mahameed 				 struct ib_event *ibev)
448709e574faSSaeed Mahameed {
44886cfdc7e4SAya Levin 	u8 port = (eqe->data.port.port >> 4) & 0xf;
44896cfdc7e4SAya Levin 
449009e574faSSaeed Mahameed 	switch (eqe->sub_type) {
449109e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
44926cfdc7e4SAya Levin 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
44936cfdc7e4SAya Levin 					    IB_LINK_LAYER_ETHERNET)
449409e574faSSaeed Mahameed 			schedule_work(&ibdev->delay_drop.delay_drop_work);
449509e574faSSaeed Mahameed 		break;
449609e574faSSaeed Mahameed 	default: /* do nothing */
449709e574faSSaeed Mahameed 		return;
449809e574faSSaeed Mahameed 	}
449909e574faSSaeed Mahameed }
450009e574faSSaeed Mahameed 
4501134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4502134e9349SSaeed Mahameed 			      struct ib_event *ibev)
4503134e9349SSaeed Mahameed {
4504134e9349SSaeed Mahameed 	u8 port = (eqe->data.port.port >> 4) & 0xf;
4505134e9349SSaeed Mahameed 
4506134e9349SSaeed Mahameed 	ibev->element.port_num = port;
4507134e9349SSaeed Mahameed 
4508134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
4509134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4510134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4511134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4512134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
4513134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
4514134e9349SSaeed Mahameed 		 */
4515134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4516134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
4517134e9349SSaeed Mahameed 			return -EINVAL;
4518134e9349SSaeed Mahameed 
4519134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4520134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4521134e9349SSaeed Mahameed 		break;
4522134e9349SSaeed Mahameed 
4523134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
4524134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
4525134e9349SSaeed Mahameed 		break;
4526134e9349SSaeed Mahameed 
4527134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4528134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
4529134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4530134e9349SSaeed Mahameed 		break;
4531134e9349SSaeed Mahameed 
4532134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4533134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
4534134e9349SSaeed Mahameed 		break;
4535134e9349SSaeed Mahameed 
4536134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4537134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
4538134e9349SSaeed Mahameed 		break;
4539134e9349SSaeed Mahameed 	default:
4540134e9349SSaeed Mahameed 		return -EINVAL;
4541134e9349SSaeed Mahameed 	}
4542134e9349SSaeed Mahameed 
4543134e9349SSaeed Mahameed 	return 0;
4544134e9349SSaeed Mahameed }
4545134e9349SSaeed Mahameed 
4546d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
4547e126ba97SEli Cohen {
4548d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
4549d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
4550d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
4551e126ba97SEli Cohen 	struct ib_event ibev;
4552dbaaff2aSEli Cohen 	bool fatal = false;
4553e126ba97SEli Cohen 
4554df097a27SSaeed Mahameed 	if (work->is_slave) {
4555df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
4556d69a24e0SDaniel Jurgens 		if (!ibdev)
4557d69a24e0SDaniel Jurgens 			goto out;
4558d69a24e0SDaniel Jurgens 	} else {
4559df097a27SSaeed Mahameed 		ibdev = work->dev;
4560d69a24e0SDaniel Jurgens 	}
4561d69a24e0SDaniel Jurgens 
4562d69a24e0SDaniel Jurgens 	switch (work->event) {
4563e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
4564e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
456589ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
4566134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
4567dbaaff2aSEli Cohen 		fatal = true;
4568e126ba97SEli Cohen 		break;
4569134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
4570134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
4571d69a24e0SDaniel Jurgens 			goto out;
4572e126ba97SEli Cohen 		break;
457309e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
457409e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
457509e574faSSaeed Mahameed 		/* fall through */
4576bdc37924SSaeed Mahameed 	default:
457703404e8aSMaor Gottlieb 		goto out;
4578e126ba97SEli Cohen 	}
4579e126ba97SEli Cohen 
4580e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
4581e126ba97SEli Cohen 
4582134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4583134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
458403404e8aSMaor Gottlieb 		goto out;
4585a0c84c32SEli Cohen 	}
4586a0c84c32SEli Cohen 
4587e126ba97SEli Cohen 	if (ibdev->ib_active)
4588e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
4589dbaaff2aSEli Cohen 
4590dbaaff2aSEli Cohen 	if (fatal)
4591dbaaff2aSEli Cohen 		ibdev->ib_active = false;
459203404e8aSMaor Gottlieb out:
4593d69a24e0SDaniel Jurgens 	kfree(work);
4594d69a24e0SDaniel Jurgens }
4595d69a24e0SDaniel Jurgens 
4596df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
4597df097a27SSaeed Mahameed 			 unsigned long event, void *param)
4598d69a24e0SDaniel Jurgens {
4599d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
4600d69a24e0SDaniel Jurgens 
4601d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
460210bea9c8SLeon Romanovsky 	if (!work)
4603df097a27SSaeed Mahameed 		return NOTIFY_DONE;
460410bea9c8SLeon Romanovsky 
4605d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4606df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4607df097a27SSaeed Mahameed 	work->is_slave = false;
4608d69a24e0SDaniel Jurgens 	work->param = param;
4609d69a24e0SDaniel Jurgens 	work->event = event;
4610d69a24e0SDaniel Jurgens 
4611d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
4612df097a27SSaeed Mahameed 
4613df097a27SSaeed Mahameed 	return NOTIFY_OK;
4614df097a27SSaeed Mahameed }
4615df097a27SSaeed Mahameed 
4616df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4617df097a27SSaeed Mahameed 				    unsigned long event, void *param)
4618df097a27SSaeed Mahameed {
4619df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
4620df097a27SSaeed Mahameed 
4621df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
4622df097a27SSaeed Mahameed 	if (!work)
4623df097a27SSaeed Mahameed 		return NOTIFY_DONE;
4624df097a27SSaeed Mahameed 
4625df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4626df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4627df097a27SSaeed Mahameed 	work->is_slave = true;
4628df097a27SSaeed Mahameed 	work->param = param;
4629df097a27SSaeed Mahameed 	work->event = event;
4630df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
4631df097a27SSaeed Mahameed 
4632df097a27SSaeed Mahameed 	return NOTIFY_OK;
4633d69a24e0SDaniel Jurgens }
4634d69a24e0SDaniel Jurgens 
4635c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4636c43f1112SMaor Gottlieb {
4637c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
4638c43f1112SMaor Gottlieb 	int err;
4639c43f1112SMaor Gottlieb 	int port;
4640c43f1112SMaor Gottlieb 
4641a989ea01SMark Bloch 	for (port = 1; port <= ARRAY_SIZE(dev->mdev->port_caps); port++) {
4642c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
4643c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4644c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
4645c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4646c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
4647c43f1112SMaor Gottlieb 								   port, 0,
4648c43f1112SMaor Gottlieb 								   &vport_ctx);
4649c43f1112SMaor Gottlieb 				if (err) {
4650c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4651c43f1112SMaor Gottlieb 						    port, err);
4652c43f1112SMaor Gottlieb 					return err;
4653c43f1112SMaor Gottlieb 				}
4654c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
4655c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
4656c43f1112SMaor Gottlieb 			} else {
4657c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
4658c43f1112SMaor Gottlieb 			}
4659c43f1112SMaor Gottlieb 		}
4660c43f1112SMaor Gottlieb 	}
4661c43f1112SMaor Gottlieb 	return 0;
4662c43f1112SMaor Gottlieb }
4663c43f1112SMaor Gottlieb 
4664e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4665e126ba97SEli Cohen {
4666e126ba97SEli Cohen 	int port;
4667e126ba97SEli Cohen 
4668508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
4669e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
4670e126ba97SEli Cohen }
4671e126ba97SEli Cohen 
467226628e2dSMark Bloch static int __get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4673e126ba97SEli Cohen {
4674e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
4675e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
4676f614fc15SDan Carpenter 	int err = -ENOMEM;
46772528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
4678e126ba97SEli Cohen 
4679e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
4680e126ba97SEli Cohen 	if (!pprops)
4681e126ba97SEli Cohen 		goto out;
4682e126ba97SEli Cohen 
4683e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4684e126ba97SEli Cohen 	if (!dprops)
4685e126ba97SEli Cohen 		goto out;
4686e126ba97SEli Cohen 
46872528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
4688e126ba97SEli Cohen 	if (err) {
4689e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
4690e126ba97SEli Cohen 		goto out;
4691e126ba97SEli Cohen 	}
4692e126ba97SEli Cohen 
4693c4550c63SOr Gerlitz 	memset(pprops, 0, sizeof(*pprops));
4694e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4695e126ba97SEli Cohen 	if (err) {
4696938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
4697938fe83cSSaeed Mahameed 			     port, err);
469832f69e4bSDaniel Jurgens 		goto out;
4699e126ba97SEli Cohen 	}
470032f69e4bSDaniel Jurgens 
4701938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
4702938fe83cSSaeed Mahameed 					dprops->max_pkeys;
4703938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
4704938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
470532f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
470632f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
4707e126ba97SEli Cohen 
4708e126ba97SEli Cohen out:
4709e126ba97SEli Cohen 	kfree(pprops);
4710e126ba97SEli Cohen 	kfree(dprops);
4711e126ba97SEli Cohen 
4712e126ba97SEli Cohen 	return err;
4713e126ba97SEli Cohen }
4714e126ba97SEli Cohen 
471526628e2dSMark Bloch static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
471626628e2dSMark Bloch {
471726628e2dSMark Bloch 	/* For representors use port 1, is this is the only native
471826628e2dSMark Bloch 	 * port
471926628e2dSMark Bloch 	 */
472026628e2dSMark Bloch 	if (dev->is_rep)
472126628e2dSMark Bloch 		return __get_port_caps(dev, 1);
472226628e2dSMark Bloch 	return __get_port_caps(dev, port);
472326628e2dSMark Bloch }
472426628e2dSMark Bloch 
4725e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4726e126ba97SEli Cohen {
4727e126ba97SEli Cohen 	int err;
4728e126ba97SEli Cohen 
4729e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
4730e126ba97SEli Cohen 	if (err)
4731e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4732e126ba97SEli Cohen 
473332927e28SMark Bloch 	if (dev->umrc.qp)
4734c4367a26SShamir Rabinovitch 		mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
473532927e28SMark Bloch 	if (dev->umrc.cq)
4736add08d76SChristoph Hellwig 		ib_free_cq(dev->umrc.cq);
473732927e28SMark Bloch 	if (dev->umrc.pd)
4738e126ba97SEli Cohen 		ib_dealloc_pd(dev->umrc.pd);
4739e126ba97SEli Cohen }
4740e126ba97SEli Cohen 
4741e126ba97SEli Cohen enum {
4742e126ba97SEli Cohen 	MAX_UMR_WR = 128,
4743e126ba97SEli Cohen };
4744e126ba97SEli Cohen 
4745e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
4746e126ba97SEli Cohen {
4747e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
4748e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
4749e126ba97SEli Cohen 	struct ib_pd *pd;
4750e126ba97SEli Cohen 	struct ib_cq *cq;
4751e126ba97SEli Cohen 	struct ib_qp *qp;
4752e126ba97SEli Cohen 	int ret;
4753e126ba97SEli Cohen 
4754e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4755e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4756e126ba97SEli Cohen 	if (!attr || !init_attr) {
4757e126ba97SEli Cohen 		ret = -ENOMEM;
4758e126ba97SEli Cohen 		goto error_0;
4759e126ba97SEli Cohen 	}
4760e126ba97SEli Cohen 
4761ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
4762e126ba97SEli Cohen 	if (IS_ERR(pd)) {
4763e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4764e126ba97SEli Cohen 		ret = PTR_ERR(pd);
4765e126ba97SEli Cohen 		goto error_0;
4766e126ba97SEli Cohen 	}
4767e126ba97SEli Cohen 
4768add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
4769e126ba97SEli Cohen 	if (IS_ERR(cq)) {
4770e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4771e126ba97SEli Cohen 		ret = PTR_ERR(cq);
4772e126ba97SEli Cohen 		goto error_2;
4773e126ba97SEli Cohen 	}
4774e126ba97SEli Cohen 
4775e126ba97SEli Cohen 	init_attr->send_cq = cq;
4776e126ba97SEli Cohen 	init_attr->recv_cq = cq;
4777e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4778e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
4779e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
4780e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4781e126ba97SEli Cohen 	init_attr->port_num = 1;
4782e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4783e126ba97SEli Cohen 	if (IS_ERR(qp)) {
4784e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4785e126ba97SEli Cohen 		ret = PTR_ERR(qp);
4786e126ba97SEli Cohen 		goto error_3;
4787e126ba97SEli Cohen 	}
4788e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
4789e126ba97SEli Cohen 	qp->real_qp    = qp;
4790e126ba97SEli Cohen 	qp->uobject    = NULL;
4791e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
479231fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
479331fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
4794e126ba97SEli Cohen 
4795e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
4796e126ba97SEli Cohen 	attr->port_num = 1;
4797e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4798e126ba97SEli Cohen 				IB_QP_PORT, NULL);
4799e126ba97SEli Cohen 	if (ret) {
4800e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4801e126ba97SEli Cohen 		goto error_4;
4802e126ba97SEli Cohen 	}
4803e126ba97SEli Cohen 
4804e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4805e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
4806e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
4807e126ba97SEli Cohen 
4808e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4809e126ba97SEli Cohen 	if (ret) {
4810e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4811e126ba97SEli Cohen 		goto error_4;
4812e126ba97SEli Cohen 	}
4813e126ba97SEli Cohen 
4814e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4815e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
4816e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4817e126ba97SEli Cohen 	if (ret) {
4818e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4819e126ba97SEli Cohen 		goto error_4;
4820e126ba97SEli Cohen 	}
4821e126ba97SEli Cohen 
4822e126ba97SEli Cohen 	dev->umrc.qp = qp;
4823e126ba97SEli Cohen 	dev->umrc.cq = cq;
4824e126ba97SEli Cohen 	dev->umrc.pd = pd;
4825e126ba97SEli Cohen 
4826e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
4827e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
4828e126ba97SEli Cohen 	if (ret) {
4829e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4830e126ba97SEli Cohen 		goto error_4;
4831e126ba97SEli Cohen 	}
4832e126ba97SEli Cohen 
4833e126ba97SEli Cohen 	kfree(attr);
4834e126ba97SEli Cohen 	kfree(init_attr);
4835e126ba97SEli Cohen 
4836e126ba97SEli Cohen 	return 0;
4837e126ba97SEli Cohen 
4838e126ba97SEli Cohen error_4:
4839c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_qp(qp, NULL);
484032927e28SMark Bloch 	dev->umrc.qp = NULL;
4841e126ba97SEli Cohen 
4842e126ba97SEli Cohen error_3:
4843add08d76SChristoph Hellwig 	ib_free_cq(cq);
484432927e28SMark Bloch 	dev->umrc.cq = NULL;
4845e126ba97SEli Cohen 
4846e126ba97SEli Cohen error_2:
4847e126ba97SEli Cohen 	ib_dealloc_pd(pd);
484832927e28SMark Bloch 	dev->umrc.pd = NULL;
4849e126ba97SEli Cohen 
4850e126ba97SEli Cohen error_0:
4851e126ba97SEli Cohen 	kfree(attr);
4852e126ba97SEli Cohen 	kfree(init_attr);
4853e126ba97SEli Cohen 	return ret;
4854e126ba97SEli Cohen }
4855e126ba97SEli Cohen 
48566e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
48576e8484c5SMax Gurtovoy {
48586e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
48596e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
48606e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
48616e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
48626e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
48636e8484c5SMax Gurtovoy 	default:
48646e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
48656e8484c5SMax Gurtovoy 	}
48666e8484c5SMax Gurtovoy }
48676e8484c5SMax Gurtovoy 
4868e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
4869e126ba97SEli Cohen {
4870e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
4871e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
487221a428a0SLeon Romanovsky 	struct ib_device *ibdev;
4873bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
48747722f47eSHaggai Eran 	int port;
4875e126ba97SEli Cohen 	int ret = 0;
4876e126ba97SEli Cohen 
4877e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
487821a428a0SLeon Romanovsky 	ibdev = &dev->ib_dev;
4879e126ba97SEli Cohen 
4880d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
4881d16e91daSHaggai Eran 
488221a428a0SLeon Romanovsky 	devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
488321a428a0SLeon Romanovsky 	if (!devr->p0)
488421a428a0SLeon Romanovsky 		return -ENOMEM;
488521a428a0SLeon Romanovsky 
488621a428a0SLeon Romanovsky 	devr->p0->device  = ibdev;
4887e126ba97SEli Cohen 	devr->p0->uobject = NULL;
4888e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
4889e126ba97SEli Cohen 
4890ff23dfa1SShamir Rabinovitch 	ret = mlx5_ib_alloc_pd(devr->p0, NULL);
489121a428a0SLeon Romanovsky 	if (ret)
489221a428a0SLeon Romanovsky 		goto error0;
489321a428a0SLeon Romanovsky 
4894ff23dfa1SShamir Rabinovitch 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL);
4895e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
4896e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
4897e126ba97SEli Cohen 		goto error1;
4898e126ba97SEli Cohen 	}
4899e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
4900e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
4901e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
4902e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
4903e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
4904e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
4905e126ba97SEli Cohen 
4906ff23dfa1SShamir Rabinovitch 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
4907e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
4908e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
4909e126ba97SEli Cohen 		goto error2;
4910e126ba97SEli Cohen 	}
4911e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
4912e126ba97SEli Cohen 	devr->x0->inode = NULL;
4913e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
4914e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
4915e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4916e126ba97SEli Cohen 
4917ff23dfa1SShamir Rabinovitch 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
4918e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
4919e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
4920e126ba97SEli Cohen 		goto error3;
4921e126ba97SEli Cohen 	}
4922e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
4923e126ba97SEli Cohen 	devr->x1->inode = NULL;
4924e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
4925e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
4926e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4927e126ba97SEli Cohen 
4928e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
4929e126ba97SEli Cohen 	attr.attr.max_sge = 1;
4930e126ba97SEli Cohen 	attr.attr.max_wr = 1;
4931e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
49321a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
4933e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
4934e126ba97SEli Cohen 
493568e326deSLeon Romanovsky 	devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
493668e326deSLeon Romanovsky 	if (!devr->s0) {
493768e326deSLeon Romanovsky 		ret = -ENOMEM;
4938e126ba97SEli Cohen 		goto error4;
4939e126ba97SEli Cohen 	}
494068e326deSLeon Romanovsky 
4941e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
4942e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
4943e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
4944e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
49451a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
494668e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
494768e326deSLeon Romanovsky 	if (ret)
494868e326deSLeon Romanovsky 		goto err_create;
494968e326deSLeon Romanovsky 
4950e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
49511a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
4952e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
4953e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
4954e126ba97SEli Cohen 
49554aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
49564aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
49574aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
49584aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
495968e326deSLeon Romanovsky 	devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
496068e326deSLeon Romanovsky 	if (!devr->s1) {
496168e326deSLeon Romanovsky 		ret = -ENOMEM;
49624aa17b28SHaggai Abramonvsky 		goto error5;
49634aa17b28SHaggai Abramonvsky 	}
496468e326deSLeon Romanovsky 
49654aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
49664aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
49674aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
49681a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
496968e326deSLeon Romanovsky 
497068e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
497168e326deSLeon Romanovsky 	if (ret)
497268e326deSLeon Romanovsky 		goto error6;
497368e326deSLeon Romanovsky 
49744aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
49751a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
49764aa17b28SHaggai Abramonvsky 
49777722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
49787722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
49797722f47eSHaggai Eran 			  pkey_change_handler);
49807722f47eSHaggai Eran 		devr->ports[port].devr = devr;
49817722f47eSHaggai Eran 	}
49827722f47eSHaggai Eran 
4983e126ba97SEli Cohen 	return 0;
4984e126ba97SEli Cohen 
498568e326deSLeon Romanovsky error6:
498668e326deSLeon Romanovsky 	kfree(devr->s1);
49874aa17b28SHaggai Abramonvsky error5:
4988c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
498968e326deSLeon Romanovsky err_create:
499068e326deSLeon Romanovsky 	kfree(devr->s0);
4991e126ba97SEli Cohen error4:
4992c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x1, NULL);
4993e126ba97SEli Cohen error3:
4994c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x0, NULL);
4995e126ba97SEli Cohen error2:
4996c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
4997e126ba97SEli Cohen error1:
4998c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
4999e126ba97SEli Cohen error0:
500021a428a0SLeon Romanovsky 	kfree(devr->p0);
5001e126ba97SEli Cohen 	return ret;
5002e126ba97SEli Cohen }
5003e126ba97SEli Cohen 
5004e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
5005e126ba97SEli Cohen {
50067722f47eSHaggai Eran 	int port;
50077722f47eSHaggai Eran 
5008c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s1, NULL);
500968e326deSLeon Romanovsky 	kfree(devr->s1);
5010c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
501168e326deSLeon Romanovsky 	kfree(devr->s0);
5012c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x0, NULL);
5013c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x1, NULL);
5014c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
5015c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
501621a428a0SLeon Romanovsky 	kfree(devr->p0);
50177722f47eSHaggai Eran 
50187722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
50195d8f6a0eSMark Bloch 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
50207722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
5021e126ba97SEli Cohen }
5022e126ba97SEli Cohen 
5023b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
5024b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
5025e53505a8SAchiad Shochat {
5026e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5027e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
5028e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
5029e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
503085c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
5031e53505a8SAchiad Shochat 	u32 ret = 0;
5032e53505a8SAchiad Shochat 
5033b02289b3SArtemy Kovalyov 	if (rep->grh_required)
5034b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
5035b02289b3SArtemy Kovalyov 
5036e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
5037b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
5038e53505a8SAchiad Shochat 
503985c7c014SDaniel Jurgens 	if (raw_support)
5040b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
504172cd5717SOr Gerlitz 
5042e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
504372cd5717SOr Gerlitz 		return ret;
5044e53505a8SAchiad Shochat 
5045e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
504672cd5717SOr Gerlitz 		return ret;
5047e53505a8SAchiad Shochat 
5048e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
5049e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
5050e53505a8SAchiad Shochat 
5051e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
5052e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
5053e53505a8SAchiad Shochat 
5054e53505a8SAchiad Shochat 	return ret;
5055e53505a8SAchiad Shochat }
5056e53505a8SAchiad Shochat 
50577738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
50587738613eSIra Weiny 			       struct ib_port_immutable *immutable)
50597738613eSIra Weiny {
50607738613eSIra Weiny 	struct ib_port_attr attr;
5061ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5062ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
5063b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
50647738613eSIra Weiny 	int err;
50657738613eSIra Weiny 
5066c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
50677738613eSIra Weiny 	if (err)
50687738613eSIra Weiny 		return err;
50697738613eSIra Weiny 
5070b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
5071b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
5072b02289b3SArtemy Kovalyov 						   &rep);
5073b02289b3SArtemy Kovalyov 		if (err)
5074b02289b3SArtemy Kovalyov 			return err;
5075b02289b3SArtemy Kovalyov 	}
5076b02289b3SArtemy Kovalyov 
50777738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
50787738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
5079b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
5080ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
5081337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
50827738613eSIra Weiny 
50837738613eSIra Weiny 	return 0;
50847738613eSIra Weiny }
50857738613eSIra Weiny 
50868e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
50878e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
50888e6efa3aSMark Bloch {
50898e6efa3aSMark Bloch 	struct ib_port_attr attr;
50908e6efa3aSMark Bloch 	int err;
50918e6efa3aSMark Bloch 
50928e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
50938e6efa3aSMark Bloch 
50948e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
50958e6efa3aSMark Bloch 	if (err)
50968e6efa3aSMark Bloch 		return err;
50978e6efa3aSMark Bloch 
50988e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
50998e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
51008e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
51018e6efa3aSMark Bloch 
51028e6efa3aSMark Bloch 	return 0;
51038e6efa3aSMark Bloch }
51048e6efa3aSMark Bloch 
51059abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
5106c7342823SIra Weiny {
5107c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
5108c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
51099abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
51109abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
51119abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
5112c7342823SIra Weiny }
5113c7342823SIra Weiny 
511445f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
51159ef9c640SAviv Heller {
51169ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
51179ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
51189ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
51199ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
51209ef9c640SAviv Heller 	int err;
51219ef9c640SAviv Heller 
51227c34ec19SAviv Heller 	if (!ns || !mlx5_lag_is_roce(mdev))
51239ef9c640SAviv Heller 		return 0;
51249ef9c640SAviv Heller 
51259ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
51269ef9c640SAviv Heller 	if (err)
51279ef9c640SAviv Heller 		return err;
51289ef9c640SAviv Heller 
51299ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
51309ef9c640SAviv Heller 	if (IS_ERR(ft)) {
51319ef9c640SAviv Heller 		err = PTR_ERR(ft);
51329ef9c640SAviv Heller 		goto err_destroy_vport_lag;
51339ef9c640SAviv Heller 	}
51349ef9c640SAviv Heller 
51359a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
51367c34ec19SAviv Heller 	dev->lag_active = true;
51379ef9c640SAviv Heller 	return 0;
51389ef9c640SAviv Heller 
51399ef9c640SAviv Heller err_destroy_vport_lag:
51409ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
51419ef9c640SAviv Heller 	return err;
51429ef9c640SAviv Heller }
51439ef9c640SAviv Heller 
514445f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
51459ef9c640SAviv Heller {
51469ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
51479ef9c640SAviv Heller 
51487c34ec19SAviv Heller 	if (dev->lag_active) {
51497c34ec19SAviv Heller 		dev->lag_active = false;
51507c34ec19SAviv Heller 
51519a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
51529a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
51539ef9c640SAviv Heller 
51549ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
51559ef9c640SAviv Heller 	}
51569ef9c640SAviv Heller }
51579ef9c640SAviv Heller 
51587fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5159fc24fc5eSAchiad Shochat {
5160e53505a8SAchiad Shochat 	int err;
5161e53505a8SAchiad Shochat 
516295579e78SMark Bloch 	dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
516395579e78SMark Bloch 	err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
51645ec8c83eSAviv Heller 	if (err) {
516595579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
5166e53505a8SAchiad Shochat 		return err;
51675ec8c83eSAviv Heller 	}
5168e53505a8SAchiad Shochat 
5169d012f5d6SOr Gerlitz 	return 0;
5170d012f5d6SOr Gerlitz }
5171d012f5d6SOr Gerlitz 
51727fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5173e126ba97SEli Cohen {
517495579e78SMark Bloch 	if (dev->port[port_num].roce.nb.notifier_call) {
517595579e78SMark Bloch 		unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
517695579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
5177e126ba97SEli Cohen 	}
5178e126ba97SEli Cohen }
5179e126ba97SEli Cohen 
5180e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
5181e126ba97SEli Cohen {
5182e126ba97SEli Cohen 	int err;
5183e126ba97SEli Cohen 
5184ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
5185e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
5186e53505a8SAchiad Shochat 		if (err)
51878e6efa3aSMark Bloch 			return err;
5188ca5b91d6SOr Gerlitz 	}
5189e53505a8SAchiad Shochat 
519045f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
51919ef9c640SAviv Heller 	if (err)
51929ef9c640SAviv Heller 		goto err_disable_roce;
51939ef9c640SAviv Heller 
5194e53505a8SAchiad Shochat 	return 0;
5195e53505a8SAchiad Shochat 
51969ef9c640SAviv Heller err_disable_roce:
5197ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
51989ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
51999ef9c640SAviv Heller 
5200e53505a8SAchiad Shochat 	return err;
5201fc24fc5eSAchiad Shochat }
5202fc24fc5eSAchiad Shochat 
520345f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
5204fc24fc5eSAchiad Shochat {
520545f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
5206ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
5207e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
5208fc24fc5eSAchiad Shochat }
5209fc24fc5eSAchiad Shochat 
5210e1f24a79SParav Pandit struct mlx5_ib_counter {
52117c16f477SKamal Heib 	const char *name;
52127c16f477SKamal Heib 	size_t offset;
52137c16f477SKamal Heib };
52147c16f477SKamal Heib 
52157c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
52167c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
52177c16f477SKamal Heib 
5218e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
52197c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
52207c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
52217c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
52227c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
52237c16f477SKamal Heib };
52247c16f477SKamal Heib 
5225e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
52267c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
52277c16f477SKamal Heib };
52287c16f477SKamal Heib 
5229e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
52307c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
52317c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
52327c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
52337c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
52347c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
52357c16f477SKamal Heib };
52367c16f477SKamal Heib 
5237e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
5238e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
5239e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
5240e1f24a79SParav Pandit 
5241e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
5242e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
5243e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
5244e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
5245e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
5246e1f24a79SParav Pandit };
5247e1f24a79SParav Pandit 
524858dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
524958dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
525058dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
525158dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
525258dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
525358dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
525458dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
525558dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
525658dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
525758dcb60aSParav Pandit };
525858dcb60aSParav Pandit 
52599f876f3dSTalat Batheesh #define INIT_EXT_PPCNT_COUNTER(_name)		\
52609f876f3dSTalat Batheesh 	{ .name = #_name, .offset =	\
52619f876f3dSTalat Batheesh 	MLX5_BYTE_OFF(ppcnt_reg, \
52629f876f3dSTalat Batheesh 		      counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
52639f876f3dSTalat Batheesh 
52649f876f3dSTalat Batheesh static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
52659f876f3dSTalat Batheesh 	INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
52669f876f3dSTalat Batheesh };
52679f876f3dSTalat Batheesh 
5268e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
52690837e86aSMark Bloch {
5270aac4492eSDaniel Jurgens 	int i;
52710837e86aSMark Bloch 
52727c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
5273921c0f5bSParav Pandit 		if (dev->port[i].cnts.set_id_valid)
52740837e86aSMark Bloch 			mlx5_core_dealloc_q_counter(dev->mdev,
5275e1f24a79SParav Pandit 						    dev->port[i].cnts.set_id);
5276e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
5277e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
52787c16f477SKamal Heib 	}
52797c16f477SKamal Heib }
52807c16f477SKamal Heib 
5281e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5282e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
52837c16f477SKamal Heib {
52847c16f477SKamal Heib 	u32 num_counters;
52857c16f477SKamal Heib 
52867c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
52877c16f477SKamal Heib 
52887c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
52897c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
52907c16f477SKamal Heib 
52917c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
52927c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
529358dcb60aSParav Pandit 
529458dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
529558dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
529658dcb60aSParav Pandit 
5297e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
52987c16f477SKamal Heib 
5299e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5300e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5301e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
5302e1f24a79SParav Pandit 	}
53039f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
53049f876f3dSTalat Batheesh 		cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
53059f876f3dSTalat Batheesh 		num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
53069f876f3dSTalat Batheesh 	}
5307e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5308e1f24a79SParav Pandit 	if (!cnts->names)
53097c16f477SKamal Heib 		return -ENOMEM;
53107c16f477SKamal Heib 
5311e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
5312e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
5313e1f24a79SParav Pandit 	if (!cnts->offsets)
53147c16f477SKamal Heib 		goto err_names;
53157c16f477SKamal Heib 
53167c16f477SKamal Heib 	return 0;
53177c16f477SKamal Heib 
53187c16f477SKamal Heib err_names:
5319e1f24a79SParav Pandit 	kfree(cnts->names);
5320aac4492eSDaniel Jurgens 	cnts->names = NULL;
53217c16f477SKamal Heib 	return -ENOMEM;
53227c16f477SKamal Heib }
53237c16f477SKamal Heib 
5324e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
53257c16f477SKamal Heib 				  const char **names,
53267c16f477SKamal Heib 				  size_t *offsets)
53277c16f477SKamal Heib {
53287c16f477SKamal Heib 	int i;
53297c16f477SKamal Heib 	int j = 0;
53307c16f477SKamal Heib 
53317c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
53327c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
53337c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
53347c16f477SKamal Heib 	}
53357c16f477SKamal Heib 
53367c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
53377c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
53387c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
53397c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
53407c16f477SKamal Heib 		}
53417c16f477SKamal Heib 	}
53427c16f477SKamal Heib 
53437c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
53447c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
53457c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
53467c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
53477c16f477SKamal Heib 		}
53487c16f477SKamal Heib 	}
5349e1f24a79SParav Pandit 
535058dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
535158dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
535258dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
535358dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
535458dcb60aSParav Pandit 		}
535558dcb60aSParav Pandit 	}
535658dcb60aSParav Pandit 
5357e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5358e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5359e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
5360e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
5361e1f24a79SParav Pandit 		}
5362e1f24a79SParav Pandit 	}
53639f876f3dSTalat Batheesh 
53649f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
53659f876f3dSTalat Batheesh 		for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
53669f876f3dSTalat Batheesh 			names[j] = ext_ppcnt_cnts[i].name;
53679f876f3dSTalat Batheesh 			offsets[j] = ext_ppcnt_cnts[i].offset;
53689f876f3dSTalat Batheesh 		}
53699f876f3dSTalat Batheesh 	}
53700837e86aSMark Bloch }
53710837e86aSMark Bloch 
5372e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
53730837e86aSMark Bloch {
5374aac4492eSDaniel Jurgens 	int err = 0;
53750837e86aSMark Bloch 	int i;
5376aa74be6eSYishai Hadas 	bool is_shared;
5377aa74be6eSYishai Hadas 
5378aa74be6eSYishai Hadas 	is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
53790837e86aSMark Bloch 
53800837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
5381aac4492eSDaniel Jurgens 		err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5382aac4492eSDaniel Jurgens 		if (err)
5383aac4492eSDaniel Jurgens 			goto err_alloc;
53847c16f477SKamal Heib 
5385aac4492eSDaniel Jurgens 		mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5386aac4492eSDaniel Jurgens 				      dev->port[i].cnts.offsets);
5387aac4492eSDaniel Jurgens 
5388aa74be6eSYishai Hadas 		err = mlx5_cmd_alloc_q_counter(dev->mdev,
5389aa74be6eSYishai Hadas 					       &dev->port[i].cnts.set_id,
5390aa74be6eSYishai Hadas 					       is_shared ?
5391aa74be6eSYishai Hadas 					       MLX5_SHARED_RESOURCE_UID : 0);
5392aac4492eSDaniel Jurgens 		if (err) {
53930837e86aSMark Bloch 			mlx5_ib_warn(dev,
53940837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
5395aac4492eSDaniel Jurgens 				     i + 1, err);
5396aac4492eSDaniel Jurgens 			goto err_alloc;
53970837e86aSMark Bloch 		}
5398aac4492eSDaniel Jurgens 		dev->port[i].cnts.set_id_valid = true;
53990837e86aSMark Bloch 	}
54000837e86aSMark Bloch 
54010837e86aSMark Bloch 	return 0;
54020837e86aSMark Bloch 
5403aac4492eSDaniel Jurgens err_alloc:
5404aac4492eSDaniel Jurgens 	mlx5_ib_dealloc_counters(dev);
5405aac4492eSDaniel Jurgens 	return err;
54060837e86aSMark Bloch }
54070837e86aSMark Bloch 
54080ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
54090ad17a8fSMark Bloch 						    u8 port_num)
54100ad17a8fSMark Bloch {
54117c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
54127c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
54130ad17a8fSMark Bloch 
54140ad17a8fSMark Bloch 	/* We support only per port stats */
54150ad17a8fSMark Bloch 	if (port_num == 0)
54160ad17a8fSMark Bloch 		return NULL;
54170ad17a8fSMark Bloch 
5418e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
5419e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
54209f876f3dSTalat Batheesh 					  port->cnts.num_cong_counters +
54219f876f3dSTalat Batheesh 					  port->cnts.num_ext_ppcnt_counters,
54220ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
54230ad17a8fSMark Bloch }
54240ad17a8fSMark Bloch 
5425aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
5426e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
5427e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
5428e1f24a79SParav Pandit {
5429e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5430e1f24a79SParav Pandit 	void *out;
5431e1f24a79SParav Pandit 	__be32 val;
5432e1f24a79SParav Pandit 	int ret, i;
5433e1f24a79SParav Pandit 
54341b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
5435e1f24a79SParav Pandit 	if (!out)
5436e1f24a79SParav Pandit 		return -ENOMEM;
5437e1f24a79SParav Pandit 
5438aac4492eSDaniel Jurgens 	ret = mlx5_core_query_q_counter(mdev,
5439e1f24a79SParav Pandit 					port->cnts.set_id, 0,
5440e1f24a79SParav Pandit 					out, outlen);
5441e1f24a79SParav Pandit 	if (ret)
5442e1f24a79SParav Pandit 		goto free;
5443e1f24a79SParav Pandit 
5444e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
5445e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
5446e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
5447e1f24a79SParav Pandit 	}
5448e1f24a79SParav Pandit 
5449e1f24a79SParav Pandit free:
5450e1f24a79SParav Pandit 	kvfree(out);
5451e1f24a79SParav Pandit 	return ret;
5452e1f24a79SParav Pandit }
5453e1f24a79SParav Pandit 
54549f876f3dSTalat Batheesh static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
54559f876f3dSTalat Batheesh 					  struct mlx5_ib_port *port,
54569f876f3dSTalat Batheesh 					  struct rdma_hw_stats *stats)
54579f876f3dSTalat Batheesh {
54589f876f3dSTalat Batheesh 	int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
54599f876f3dSTalat Batheesh 	int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
54609f876f3dSTalat Batheesh 	int ret, i;
54619f876f3dSTalat Batheesh 	void *out;
54629f876f3dSTalat Batheesh 
54639f876f3dSTalat Batheesh 	out = kvzalloc(sz, GFP_KERNEL);
54649f876f3dSTalat Batheesh 	if (!out)
54659f876f3dSTalat Batheesh 		return -ENOMEM;
54669f876f3dSTalat Batheesh 
54679f876f3dSTalat Batheesh 	ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
54689f876f3dSTalat Batheesh 	if (ret)
54699f876f3dSTalat Batheesh 		goto free;
54709f876f3dSTalat Batheesh 
54719f876f3dSTalat Batheesh 	for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
54729f876f3dSTalat Batheesh 		stats->value[i + offset] =
54739f876f3dSTalat Batheesh 			be64_to_cpup((__be64 *)(out +
54749f876f3dSTalat Batheesh 				    port->cnts.offsets[i + offset]));
54759f876f3dSTalat Batheesh 	}
54769f876f3dSTalat Batheesh 
54779f876f3dSTalat Batheesh free:
54789f876f3dSTalat Batheesh 	kvfree(out);
54799f876f3dSTalat Batheesh 	return ret;
54809f876f3dSTalat Batheesh }
54819f876f3dSTalat Batheesh 
54820ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
54830ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
54847c16f477SKamal Heib 				u8 port_num, int index)
54850ad17a8fSMark Bloch {
54860ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
54877c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
5488aac4492eSDaniel Jurgens 	struct mlx5_core_dev *mdev;
5489e1f24a79SParav Pandit 	int ret, num_counters;
5490aac4492eSDaniel Jurgens 	u8 mdev_port_num;
54910ad17a8fSMark Bloch 
54927c16f477SKamal Heib 	if (!stats)
5493e1f24a79SParav Pandit 		return -EINVAL;
54940ad17a8fSMark Bloch 
54959f876f3dSTalat Batheesh 	num_counters = port->cnts.num_q_counters +
54969f876f3dSTalat Batheesh 		       port->cnts.num_cong_counters +
54979f876f3dSTalat Batheesh 		       port->cnts.num_ext_ppcnt_counters;
5498aac4492eSDaniel Jurgens 
5499aac4492eSDaniel Jurgens 	/* q_counters are per IB device, query the master mdev */
5500aac4492eSDaniel Jurgens 	ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
55010ad17a8fSMark Bloch 	if (ret)
5502e1f24a79SParav Pandit 		return ret;
55030ad17a8fSMark Bloch 
55049f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
55059f876f3dSTalat Batheesh 		ret =  mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
55069f876f3dSTalat Batheesh 		if (ret)
55079f876f3dSTalat Batheesh 			return ret;
55089f876f3dSTalat Batheesh 	}
55099f876f3dSTalat Batheesh 
5510e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5511aac4492eSDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5512aac4492eSDaniel Jurgens 						    &mdev_port_num);
5513aac4492eSDaniel Jurgens 		if (!mdev) {
5514aac4492eSDaniel Jurgens 			/* If port is not affiliated yet, its in down state
5515aac4492eSDaniel Jurgens 			 * which doesn't have any counters yet, so it would be
5516aac4492eSDaniel Jurgens 			 * zero. So no need to read from the HCA.
5517aac4492eSDaniel Jurgens 			 */
5518aac4492eSDaniel Jurgens 			goto done;
5519aac4492eSDaniel Jurgens 		}
552071a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
552171a0ff65SMajd Dibbiny 						   stats->value +
552271a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters,
552371a0ff65SMajd Dibbiny 						   port->cnts.num_cong_counters,
552471a0ff65SMajd Dibbiny 						   port->cnts.offsets +
552571a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters);
5526aac4492eSDaniel Jurgens 
5527aac4492eSDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
5528e1f24a79SParav Pandit 		if (ret)
5529e1f24a79SParav Pandit 			return ret;
55300ad17a8fSMark Bloch 	}
55317c16f477SKamal Heib 
5532aac4492eSDaniel Jurgens done:
5533e1f24a79SParav Pandit 	return num_counters;
55340ad17a8fSMark Bloch }
55350ad17a8fSMark Bloch 
5536f6a8a19bSDenis Drozdov static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5537693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
5538f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
5539693dfd5aSErez Shitrit {
5540693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
5541f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
5542693dfd5aSErez Shitrit 
5543f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
5544693dfd5aSErez Shitrit }
5545693dfd5aSErez Shitrit 
5546fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5547fe248c3aSMaor Gottlieb {
5548fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
5549fe248c3aSMaor Gottlieb 		return;
5550fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5551fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
5552fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
5553fe248c3aSMaor Gottlieb }
5554fe248c3aSMaor Gottlieb 
555503404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
555603404e8aSMaor Gottlieb {
555703404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
555803404e8aSMaor Gottlieb 		return;
555903404e8aSMaor Gottlieb 
556003404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
5561fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5562fe248c3aSMaor Gottlieb }
5563fe248c3aSMaor Gottlieb 
5564fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5565fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
5566fe248c3aSMaor Gottlieb {
5567fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5568fe248c3aSMaor Gottlieb 	char lbuf[20];
5569fe248c3aSMaor Gottlieb 	int len;
5570fe248c3aSMaor Gottlieb 
5571fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5572fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
5573fe248c3aSMaor Gottlieb }
5574fe248c3aSMaor Gottlieb 
5575fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5576fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
5577fe248c3aSMaor Gottlieb {
5578fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5579fe248c3aSMaor Gottlieb 	u32 timeout;
5580fe248c3aSMaor Gottlieb 	u32 var;
5581fe248c3aSMaor Gottlieb 
5582fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
5583fe248c3aSMaor Gottlieb 		return -EFAULT;
5584fe248c3aSMaor Gottlieb 
5585fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5586fe248c3aSMaor Gottlieb 			1000);
5587fe248c3aSMaor Gottlieb 	if (timeout != var)
5588fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5589fe248c3aSMaor Gottlieb 			    timeout);
5590fe248c3aSMaor Gottlieb 
5591fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
5592fe248c3aSMaor Gottlieb 
5593fe248c3aSMaor Gottlieb 	return count;
5594fe248c3aSMaor Gottlieb }
5595fe248c3aSMaor Gottlieb 
5596fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
5597fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
5598fe248c3aSMaor Gottlieb 	.open	= simple_open,
5599fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
5600fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
5601fe248c3aSMaor Gottlieb };
5602fe248c3aSMaor Gottlieb 
5603fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5604fe248c3aSMaor Gottlieb {
5605fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
5606fe248c3aSMaor Gottlieb 
5607fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
5608fe248c3aSMaor Gottlieb 		return 0;
5609fe248c3aSMaor Gottlieb 
5610fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5611fe248c3aSMaor Gottlieb 	if (!dbg)
5612fe248c3aSMaor Gottlieb 		return -ENOMEM;
5613fe248c3aSMaor Gottlieb 
5614cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
5615cbafad87SSudip Mukherjee 
5616fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
5617fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
5618fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
5619fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
5620cbafad87SSudip Mukherjee 		goto out_debugfs;
5621fe248c3aSMaor Gottlieb 
5622fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
5623fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
5624fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5625fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
5626fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
5627fe248c3aSMaor Gottlieb 		goto out_debugfs;
5628fe248c3aSMaor Gottlieb 
5629fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
5630fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
5631fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5632fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
5633fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
5634fe248c3aSMaor Gottlieb 		goto out_debugfs;
5635fe248c3aSMaor Gottlieb 
5636fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
5637fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
5638fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
5639fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
5640fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
5641fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
5642fe248c3aSMaor Gottlieb 		goto out_debugfs;
5643fe248c3aSMaor Gottlieb 
5644fe248c3aSMaor Gottlieb 	return 0;
5645fe248c3aSMaor Gottlieb 
5646fe248c3aSMaor Gottlieb out_debugfs:
5647fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5648fe248c3aSMaor Gottlieb 	return -ENOMEM;
564903404e8aSMaor Gottlieb }
565003404e8aSMaor Gottlieb 
565103404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
565203404e8aSMaor Gottlieb {
565303404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
565403404e8aSMaor Gottlieb 		return;
565503404e8aSMaor Gottlieb 
565603404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
565703404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
565803404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
565903404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
566003404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
5661fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
5662fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
5663fe248c3aSMaor Gottlieb 
5664fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
5665fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
566603404e8aSMaor Gottlieb }
566703404e8aSMaor Gottlieb 
566832f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
566932f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
567032f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
567132f69e4bSDaniel Jurgens {
567232f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
567332f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
567432f69e4bSDaniel Jurgens 	int comps;
567532f69e4bSDaniel Jurgens 	int err;
567632f69e4bSDaniel Jurgens 	int i;
567732f69e4bSDaniel Jurgens 
5678a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5679a9e546e7SParav Pandit 
568032f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
568132f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
568232f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
568332f69e4bSDaniel Jurgens 		return;
568432f69e4bSDaniel Jurgens 	}
5685df097a27SSaeed Mahameed 
5686df097a27SSaeed Mahameed 	if (mpi->mdev_events.notifier_call)
5687df097a27SSaeed Mahameed 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
5688df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
5689df097a27SSaeed Mahameed 
569032f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
569132f69e4bSDaniel Jurgens 
569232f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
569332f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
569432f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
569532f69e4bSDaniel Jurgens 
569632f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
569732f69e4bSDaniel Jurgens 	if (comps) {
569832f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
569932f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
570032f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
570132f69e4bSDaniel Jurgens 
570232f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
570332f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
570432f69e4bSDaniel Jurgens 
570532f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
570632f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
570732f69e4bSDaniel Jurgens 	}
570832f69e4bSDaniel Jurgens 
570932f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
571032f69e4bSDaniel Jurgens 
571132f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
571232f69e4bSDaniel Jurgens 
571332f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
571432f69e4bSDaniel Jurgens 
571532f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
571632f69e4bSDaniel Jurgens 
571732f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
571832f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
571932f69e4bSDaniel Jurgens 	 * it back to the list.
572032f69e4bSDaniel Jurgens 	 */
572132f69e4bSDaniel Jurgens 	if (err)
572232f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
572332f69e4bSDaniel Jurgens 			    port_num + 1);
572432f69e4bSDaniel Jurgens 
572595579e78SMark Bloch 	ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
572632f69e4bSDaniel Jurgens }
572732f69e4bSDaniel Jurgens 
572832f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
572932f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
573032f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
573132f69e4bSDaniel Jurgens {
573232f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
573332f69e4bSDaniel Jurgens 	int err;
573432f69e4bSDaniel Jurgens 
573532f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
573632f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
57372577188eSQing Huang 		mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
573832f69e4bSDaniel Jurgens 			    port_num + 1);
573932f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
574032f69e4bSDaniel Jurgens 		return false;
574132f69e4bSDaniel Jurgens 	}
574232f69e4bSDaniel Jurgens 
574332f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
574432f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
5745df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
574632f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
574732f69e4bSDaniel Jurgens 
574832f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
574932f69e4bSDaniel Jurgens 	if (err)
575032f69e4bSDaniel Jurgens 		goto unbind;
575132f69e4bSDaniel Jurgens 
575232f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
575332f69e4bSDaniel Jurgens 	if (err)
575432f69e4bSDaniel Jurgens 		goto unbind;
575532f69e4bSDaniel Jurgens 
575632f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
575732f69e4bSDaniel Jurgens 	if (err) {
575832f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
575932f69e4bSDaniel Jurgens 			    port_num + 1);
576032f69e4bSDaniel Jurgens 		goto unbind;
576132f69e4bSDaniel Jurgens 	}
576232f69e4bSDaniel Jurgens 
5763df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5764df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5765df097a27SSaeed Mahameed 
576673eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
5767a9e546e7SParav Pandit 
576832f69e4bSDaniel Jurgens 	return true;
576932f69e4bSDaniel Jurgens 
577032f69e4bSDaniel Jurgens unbind:
577132f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
577232f69e4bSDaniel Jurgens 	return false;
577332f69e4bSDaniel Jurgens }
577432f69e4bSDaniel Jurgens 
577532f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
577632f69e4bSDaniel Jurgens {
577732f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
577832f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
577932f69e4bSDaniel Jurgens 							  port_num + 1);
578032f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
578132f69e4bSDaniel Jurgens 	int err;
578232f69e4bSDaniel Jurgens 	int i;
578332f69e4bSDaniel Jurgens 
578432f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
578532f69e4bSDaniel Jurgens 		return 0;
578632f69e4bSDaniel Jurgens 
578732f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
578832f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
578932f69e4bSDaniel Jurgens 	if (err)
579032f69e4bSDaniel Jurgens 		return err;
579132f69e4bSDaniel Jurgens 
579232f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
579332f69e4bSDaniel Jurgens 	if (err)
579432f69e4bSDaniel Jurgens 		return err;
579532f69e4bSDaniel Jurgens 
579632f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
579732f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
579832f69e4bSDaniel Jurgens 		bool bound = false;
579932f69e4bSDaniel Jurgens 
580032f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
580132f69e4bSDaniel Jurgens 		if (i == port_num) {
580232f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
580332f69e4bSDaniel Jurgens 			if (!mpi) {
580432f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
580532f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
580632f69e4bSDaniel Jurgens 				return -ENOMEM;
580732f69e4bSDaniel Jurgens 			}
580832f69e4bSDaniel Jurgens 
580932f69e4bSDaniel Jurgens 			mpi->is_master = true;
581032f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
581132f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
581232f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
581332f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
581432f69e4bSDaniel Jurgens 			mpi = NULL;
581532f69e4bSDaniel Jurgens 			continue;
581632f69e4bSDaniel Jurgens 		}
581732f69e4bSDaniel Jurgens 
581832f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
581932f69e4bSDaniel Jurgens 				    list) {
582032f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
582132f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
582232f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
582332f69e4bSDaniel Jurgens 			}
582432f69e4bSDaniel Jurgens 
582532f69e4bSDaniel Jurgens 			if (bound) {
5826c42260f1SVu Pham 				dev_dbg(mpi->mdev->device,
5827c42260f1SVu Pham 					"removing port from unaffiliated list.\n");
582832f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
582932f69e4bSDaniel Jurgens 				list_del(&mpi->list);
583032f69e4bSDaniel Jurgens 				break;
583132f69e4bSDaniel Jurgens 			}
583232f69e4bSDaniel Jurgens 		}
583332f69e4bSDaniel Jurgens 		if (!bound) {
583432f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
583532f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
583632f69e4bSDaniel Jurgens 				    i + 1);
583732f69e4bSDaniel Jurgens 		}
583832f69e4bSDaniel Jurgens 	}
583932f69e4bSDaniel Jurgens 
584032f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
584132f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
584232f69e4bSDaniel Jurgens 	return err;
584332f69e4bSDaniel Jurgens }
584432f69e4bSDaniel Jurgens 
584532f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
584632f69e4bSDaniel Jurgens {
584732f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
584832f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
584932f69e4bSDaniel Jurgens 							  port_num + 1);
585032f69e4bSDaniel Jurgens 	int i;
585132f69e4bSDaniel Jurgens 
585232f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
585332f69e4bSDaniel Jurgens 		return;
585432f69e4bSDaniel Jurgens 
585532f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
585632f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
585732f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
585832f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
585932f69e4bSDaniel Jurgens 			if (i == port_num) {
586032f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
586132f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
586232f69e4bSDaniel Jurgens 			} else {
586332f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
586432f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
586532f69e4bSDaniel Jurgens 			}
586632f69e4bSDaniel Jurgens 		}
586732f69e4bSDaniel Jurgens 	}
586832f69e4bSDaniel Jurgens 
586932f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
587032f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
587132f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
587232f69e4bSDaniel Jurgens 
587332f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
587432f69e4bSDaniel Jurgens }
587532f69e4bSDaniel Jurgens 
58769a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
58779a119cd5SJason Gunthorpe 	mlx5_ib_dm,
58789a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
587924da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
58809a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
588124da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
588283bb4442SJason Gunthorpe 			    UA_MANDATORY),
58839a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
588424da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
58853b113a1eSAriel Levkovich 			    UA_OPTIONAL),
58863b113a1eSAriel Levkovich 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
58873b113a1eSAriel Levkovich 			     enum mlx5_ib_uapi_dm_type,
58883b113a1eSAriel Levkovich 			     UA_OPTIONAL));
588924da0016SAriel Levkovich 
58909a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
58919a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
58929a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
5893c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
5894bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
5895bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
5896c6475a0bSAviad Yehezkel 
58970cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
58980cbf432dSJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
589936e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
59000cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
59010cbf432dSJason Gunthorpe #endif
59028c84660bSMatan Barak 
59030cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
59040cbf432dSJason Gunthorpe 				&mlx5_ib_flow_action),
59050cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
59060cbf432dSJason Gunthorpe 	{}
59070cbf432dSJason Gunthorpe };
59088c84660bSMatan Barak 
59091a1e03dcSRaed Salem static int mlx5_ib_read_counters(struct ib_counters *counters,
59101a1e03dcSRaed Salem 				 struct ib_counters_read_attr *read_attr,
59111a1e03dcSRaed Salem 				 struct uverbs_attr_bundle *attrs)
59121a1e03dcSRaed Salem {
59131a1e03dcSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
59141a1e03dcSRaed Salem 	struct mlx5_read_counters_attr mread_attr = {};
59151a1e03dcSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc;
59161a1e03dcSRaed Salem 	int ret, i;
59171a1e03dcSRaed Salem 
59181a1e03dcSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
59191a1e03dcSRaed Salem 	if (mcounters->cntrs_max_index > read_attr->ncounters) {
59201a1e03dcSRaed Salem 		ret = -EINVAL;
59211a1e03dcSRaed Salem 		goto err_bound;
59221a1e03dcSRaed Salem 	}
59231a1e03dcSRaed Salem 
59241a1e03dcSRaed Salem 	mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
59251a1e03dcSRaed Salem 				 GFP_KERNEL);
59261a1e03dcSRaed Salem 	if (!mread_attr.out) {
59271a1e03dcSRaed Salem 		ret = -ENOMEM;
59281a1e03dcSRaed Salem 		goto err_bound;
59291a1e03dcSRaed Salem 	}
59301a1e03dcSRaed Salem 
59311a1e03dcSRaed Salem 	mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
59321a1e03dcSRaed Salem 	mread_attr.flags = read_attr->flags;
59331a1e03dcSRaed Salem 	ret = mcounters->read_counters(counters->device, &mread_attr);
59341a1e03dcSRaed Salem 	if (ret)
59351a1e03dcSRaed Salem 		goto err_read;
59361a1e03dcSRaed Salem 
59371a1e03dcSRaed Salem 	/* do the pass over the counters data array to assign according to the
59381a1e03dcSRaed Salem 	 * descriptions and indexing pairs
59391a1e03dcSRaed Salem 	 */
59401a1e03dcSRaed Salem 	desc = mcounters->counters_data;
59411a1e03dcSRaed Salem 	for (i = 0; i < mcounters->ncounters; i++)
59421a1e03dcSRaed Salem 		read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
59431a1e03dcSRaed Salem 
59441a1e03dcSRaed Salem err_read:
59451a1e03dcSRaed Salem 	kfree(mread_attr.out);
59461a1e03dcSRaed Salem err_bound:
59471a1e03dcSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
59481a1e03dcSRaed Salem 	return ret;
59491a1e03dcSRaed Salem }
59501a1e03dcSRaed Salem 
5951b29e2a13SRaed Salem static int mlx5_ib_destroy_counters(struct ib_counters *counters)
5952b29e2a13SRaed Salem {
5953b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5954b29e2a13SRaed Salem 
59553b3233fbSRaed Salem 	counters_clear_description(counters);
59563b3233fbSRaed Salem 	if (mcounters->hw_cntrs_hndl)
59573b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(counters->device)->mdev,
59583b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
59593b3233fbSRaed Salem 
5960b29e2a13SRaed Salem 	kfree(mcounters);
5961b29e2a13SRaed Salem 
5962b29e2a13SRaed Salem 	return 0;
5963b29e2a13SRaed Salem }
5964b29e2a13SRaed Salem 
5965b29e2a13SRaed Salem static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
5966b29e2a13SRaed Salem 						   struct uverbs_attr_bundle *attrs)
5967b29e2a13SRaed Salem {
5968b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters;
5969b29e2a13SRaed Salem 
5970b29e2a13SRaed Salem 	mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
5971b29e2a13SRaed Salem 	if (!mcounters)
5972b29e2a13SRaed Salem 		return ERR_PTR(-ENOMEM);
5973b29e2a13SRaed Salem 
59743b3233fbSRaed Salem 	mutex_init(&mcounters->mcntrs_mutex);
59753b3233fbSRaed Salem 
5976b29e2a13SRaed Salem 	return &mcounters->ibcntrs;
5977b29e2a13SRaed Salem }
5978b29e2a13SRaed Salem 
5979fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
5980e126ba97SEli Cohen {
598125c13324SAriel Levkovich 	struct mlx5_core_dev *mdev = dev->mdev;
598225c13324SAriel Levkovich 
598332f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
598413859d5dSLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
5985534fd7aaSYishai Hadas 		srcu_barrier(&dev->mr_srcu);
59863cc297dbSMark Bloch 		cleanup_srcu_struct(&dev->mr_srcu);
598713859d5dSLeon Romanovsky 	}
59884056b12eSAriel Levkovich 
59894056b12eSAriel Levkovich 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
599025c13324SAriel Levkovich 
599125c13324SAriel Levkovich 	WARN_ON(dev->dm.steering_sw_icm_alloc_blocks &&
599225c13324SAriel Levkovich 		!bitmap_empty(
599325c13324SAriel Levkovich 			dev->dm.steering_sw_icm_alloc_blocks,
599425c13324SAriel Levkovich 			BIT(MLX5_CAP_DEV_MEM(mdev, log_steering_sw_icm_size) -
599525c13324SAriel Levkovich 			    MLX5_LOG_SW_ICM_BLOCK_SIZE(mdev))));
599625c13324SAriel Levkovich 
599725c13324SAriel Levkovich 	kfree(dev->dm.steering_sw_icm_alloc_blocks);
599825c13324SAriel Levkovich 
599925c13324SAriel Levkovich 	WARN_ON(dev->dm.header_modify_sw_icm_alloc_blocks &&
600025c13324SAriel Levkovich 		!bitmap_empty(dev->dm.header_modify_sw_icm_alloc_blocks,
600125c13324SAriel Levkovich 			      BIT(MLX5_CAP_DEV_MEM(
600225c13324SAriel Levkovich 					  mdev, log_header_modify_sw_icm_size) -
600325c13324SAriel Levkovich 				  MLX5_LOG_SW_ICM_BLOCK_SIZE(mdev))));
600425c13324SAriel Levkovich 
600525c13324SAriel Levkovich 	kfree(dev->dm.header_modify_sw_icm_alloc_blocks);
600616c1975fSMark Bloch }
600716c1975fSMark Bloch 
6008fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
600916c1975fSMark Bloch {
601016c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
601125c13324SAriel Levkovich 	u64 header_modify_icm_blocks = 0;
601225c13324SAriel Levkovich 	u64 steering_icm_blocks = 0;
6013e126ba97SEli Cohen 	int err;
601432f69e4bSDaniel Jurgens 	int i;
6015e126ba97SEli Cohen 
601632f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
601732f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
601895579e78SMark Bloch 		rwlock_init(&dev->port[i].roce.netdev_lock);
6019d3b5cc1cSMark Bloch 		dev->port[i].roce.dev = dev;
6020d3b5cc1cSMark Bloch 		dev->port[i].roce.native_port_num = i + 1;
6021d3b5cc1cSMark Bloch 		dev->port[i].roce.last_port_state = IB_PORT_DOWN;
602232f69e4bSDaniel Jurgens 	}
602332f69e4bSDaniel Jurgens 
602432f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
6025e126ba97SEli Cohen 	if (err)
6026da796ccbSMark Bloch 		return err;
6027e126ba97SEli Cohen 
6028a989ea01SMark Bloch 	err = set_has_smi_cap(dev);
6029a989ea01SMark Bloch 	if (err)
6030a989ea01SMark Bloch 		return err;
6031e126ba97SEli Cohen 
603232f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
603332f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
603432f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
603532f69e4bSDaniel Jurgens 			if (err)
603632f69e4bSDaniel Jurgens 				break;
603732f69e4bSDaniel Jurgens 		}
603832f69e4bSDaniel Jurgens 	} else {
603932f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
604032f69e4bSDaniel Jurgens 	}
604132f69e4bSDaniel Jurgens 	if (err)
604232f69e4bSDaniel Jurgens 		goto err_mp;
604332f69e4bSDaniel Jurgens 
60441b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
6045e126ba97SEli Cohen 		get_ext_port_caps(dev);
6046e126ba97SEli Cohen 
6047e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
6048c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
6049e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
6050f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
6051c42260f1SVu Pham 	dev->ib_dev.dev.parent		= mdev->device;
6052e126ba97SEli Cohen 
60533cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
60543cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
60553cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
60563cc297dbSMark Bloch 
605725c13324SAriel Levkovich 	if (MLX5_CAP_GEN_64(mdev, general_obj_types) &
605825c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM) {
605925c13324SAriel Levkovich 		if (MLX5_CAP64_DEV_MEM(mdev, steering_sw_icm_start_address)) {
606025c13324SAriel Levkovich 			steering_icm_blocks =
606125c13324SAriel Levkovich 				BIT(MLX5_CAP_DEV_MEM(mdev,
606225c13324SAriel Levkovich 						     log_steering_sw_icm_size) -
606325c13324SAriel Levkovich 				    MLX5_LOG_SW_ICM_BLOCK_SIZE(mdev));
606425c13324SAriel Levkovich 
606525c13324SAriel Levkovich 			dev->dm.steering_sw_icm_alloc_blocks =
606625c13324SAriel Levkovich 				kcalloc(BITS_TO_LONGS(steering_icm_blocks),
606725c13324SAriel Levkovich 					sizeof(unsigned long), GFP_KERNEL);
606825c13324SAriel Levkovich 			if (!dev->dm.steering_sw_icm_alloc_blocks)
606925c13324SAriel Levkovich 				goto err_mp;
607025c13324SAriel Levkovich 		}
607125c13324SAriel Levkovich 
607225c13324SAriel Levkovich 		if (MLX5_CAP64_DEV_MEM(mdev,
607325c13324SAriel Levkovich 				       header_modify_sw_icm_start_address)) {
607425c13324SAriel Levkovich 			header_modify_icm_blocks = BIT(
607525c13324SAriel Levkovich 				MLX5_CAP_DEV_MEM(
607625c13324SAriel Levkovich 					mdev, log_header_modify_sw_icm_size) -
607725c13324SAriel Levkovich 				MLX5_LOG_SW_ICM_BLOCK_SIZE(mdev));
607825c13324SAriel Levkovich 
607925c13324SAriel Levkovich 			dev->dm.header_modify_sw_icm_alloc_blocks =
608025c13324SAriel Levkovich 				kcalloc(BITS_TO_LONGS(header_modify_icm_blocks),
608125c13324SAriel Levkovich 					sizeof(unsigned long), GFP_KERNEL);
608225c13324SAriel Levkovich 			if (!dev->dm.header_modify_sw_icm_alloc_blocks)
608325c13324SAriel Levkovich 				goto err_dm;
608425c13324SAriel Levkovich 		}
608525c13324SAriel Levkovich 	}
608625c13324SAriel Levkovich 
60873b113a1eSAriel Levkovich 	spin_lock_init(&dev->dm.lock);
60883b113a1eSAriel Levkovich 	dev->dm.dev = mdev;
608924da0016SAriel Levkovich 
609013859d5dSLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
60913cc297dbSMark Bloch 		err = init_srcu_struct(&dev->mr_srcu);
6092a6bc3875SMoni Shoua 		if (err)
609325c13324SAriel Levkovich 			goto err_dm;
6094623d1543SJason Gunthorpe 	}
60953cc297dbSMark Bloch 
609616c1975fSMark Bloch 	return 0;
609725c13324SAriel Levkovich 
609825c13324SAriel Levkovich err_dm:
609925c13324SAriel Levkovich 	kfree(dev->dm.steering_sw_icm_alloc_blocks);
610025c13324SAriel Levkovich 	kfree(dev->dm.header_modify_sw_icm_alloc_blocks);
610125c13324SAriel Levkovich 
610232f69e4bSDaniel Jurgens err_mp:
610332f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
610416c1975fSMark Bloch 
610516c1975fSMark Bloch 	return -ENOMEM;
610616c1975fSMark Bloch }
610716c1975fSMark Bloch 
61089a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
61099a4ca38dSMark Bloch {
61109a4ca38dSMark Bloch 	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
61119a4ca38dSMark Bloch 
61129a4ca38dSMark Bloch 	if (!dev->flow_db)
61139a4ca38dSMark Bloch 		return -ENOMEM;
61149a4ca38dSMark Bloch 
61159a4ca38dSMark Bloch 	mutex_init(&dev->flow_db->lock);
61169a4ca38dSMark Bloch 
61179a4ca38dSMark Bloch 	return 0;
61189a4ca38dSMark Bloch }
61199a4ca38dSMark Bloch 
61209a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
61219a4ca38dSMark Bloch {
61229a4ca38dSMark Bloch 	kfree(dev->flow_db);
61239a4ca38dSMark Bloch }
61249a4ca38dSMark Bloch 
612596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
6126*7a154142SJason Gunthorpe 	.owner = THIS_MODULE,
6127b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_MLX5,
612872c6ec18SJason Gunthorpe 	.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION,
6129b9560a41SJason Gunthorpe 
613096458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
613196458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
613296458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
613396458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
613496458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
613596458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
613696458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
613796458233SKamal Heib 	.create_counters = mlx5_ib_create_counters,
613896458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
613996458233SKamal Heib 	.create_flow = mlx5_ib_create_flow,
614096458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
614196458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
614296458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
614396458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
614496458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
614596458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
614696458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
614796458233SKamal Heib 	.destroy_counters = mlx5_ib_destroy_counters,
614896458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
614996458233SKamal Heib 	.destroy_flow = mlx5_ib_destroy_flow,
615096458233SKamal Heib 	.destroy_flow_action = mlx5_ib_destroy_flow_action,
615196458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
615296458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
615396458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
615496458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
615596458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
615696458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
615796458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
615896458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
615996458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
616096458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
616196458233SKamal Heib 	.mmap = mlx5_ib_mmap,
616296458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
616396458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
616496458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
616596458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
616696458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
616796458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
616896458233SKamal Heib 	.post_recv = mlx5_ib_post_recv,
616996458233SKamal Heib 	.post_send = mlx5_ib_post_send,
617096458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
617196458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
617296458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
617396458233SKamal Heib 	.query_device = mlx5_ib_query_device,
617496458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
617596458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
617696458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
617796458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
617896458233SKamal Heib 	.read_counters = mlx5_ib_read_counters,
617996458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
618096458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
618196458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
618296458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
6183d3456914SLeon Romanovsky 
6184d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
618521a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
618668e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
6187a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
618896458233SKamal Heib };
618996458233SKamal Heib 
619096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
619196458233SKamal Heib 	.create_flow_action_esp = mlx5_ib_create_flow_action_esp,
619296458233SKamal Heib 	.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
619396458233SKamal Heib };
619496458233SKamal Heib 
619596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
619696458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
619796458233SKamal Heib };
619896458233SKamal Heib 
619996458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
620096458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
620196458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
620296458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
620396458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
620496458233SKamal Heib };
620596458233SKamal Heib 
620696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
620796458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
620896458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
620996458233SKamal Heib };
621096458233SKamal Heib 
621196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
621296458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
621396458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
621496458233SKamal Heib };
621596458233SKamal Heib 
621696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
621796458233SKamal Heib 	.alloc_dm = mlx5_ib_alloc_dm,
621896458233SKamal Heib 	.dealloc_dm = mlx5_ib_dealloc_dm,
621996458233SKamal Heib 	.reg_dm_mr = mlx5_ib_reg_dm_mr,
622096458233SKamal Heib };
622196458233SKamal Heib 
6222fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
622316c1975fSMark Bloch {
622416c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
622516c1975fSMark Bloch 	int err;
622616c1975fSMark Bloch 
6227e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
6228e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
6229e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
6230e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
6231e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
6232e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
623341c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
623441c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
6235e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
623656e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
6237e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
6238e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
6239e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
6240e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
6241e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
6242e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
6243e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
6244e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
6245e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
6246e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
6247e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
6248e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
6249e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
6250e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
6251e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
6252e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
6253e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
62541707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
6255d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
6256d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
62577d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
6258b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
625996458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ)	|
626096458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW)	|
626196458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
6262e126ba97SEli Cohen 
6263f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
6264f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
626596458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
626696458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
62678e959601SNiranjana Vishwanathapura 
626896458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
626996458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
62707c2344c3SMaor Gottlieb 
62716e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
62726e8484c5SMax Gurtovoy 
6273d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
6274d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
6275d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
6276d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
627796458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
6278d2370e0aSMatan Barak 	}
6279d2370e0aSMatan Barak 
6280938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
6281e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
6282e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
6283e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
628496458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
6285e126ba97SEli Cohen 	}
6286e126ba97SEli Cohen 
628725c13324SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic) ||
628825c13324SAriel Levkovich 	    MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
628925c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
629096458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
629124da0016SAriel Levkovich 
6292dfb631a1SJason Gunthorpe 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
629396458233SKamal Heib 	    MLX5_ACCEL_IPSEC_CAP_DEVICE)
629496458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
629596458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
629681e30880SYishai Hadas 
629736e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
629836e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
629981e30880SYishai Hadas 
6300e126ba97SEli Cohen 	err = init_node_data(dev);
6301e126ba97SEli Cohen 	if (err)
630216c1975fSMark Bloch 		return err;
6303e126ba97SEli Cohen 
6304c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
6305e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6306e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
6307a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
6308c8b89924SMark Bloch 
630916c1975fSMark Bloch 	return 0;
631016c1975fSMark Bloch }
631116c1975fSMark Bloch 
631296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
631396458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
631496458233SKamal Heib 	.query_port = mlx5_ib_query_port,
631596458233SKamal Heib };
631696458233SKamal Heib 
63178e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
631816c1975fSMark Bloch {
631996458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
63208e6efa3aSMark Bloch 	return 0;
63218e6efa3aSMark Bloch }
63228e6efa3aSMark Bloch 
632396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
632496458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
632596458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
632696458233SKamal Heib };
632796458233SKamal Heib 
6328fb652d32SMark Bloch static int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
63298e6efa3aSMark Bloch {
633096458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
63318e6efa3aSMark Bloch 	return 0;
63328e6efa3aSMark Bloch }
63338e6efa3aSMark Bloch 
633496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
633596458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
633696458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
633796458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
633896458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
633996458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
634096458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
634196458233SKamal Heib };
634296458233SKamal Heib 
6343e3f1ed1fSLeon Romanovsky static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
63448e6efa3aSMark Bloch {
6345e3f1ed1fSLeon Romanovsky 	u8 port_num;
63467fd8aefbSDaniel Jurgens 
6347c11a226aSMark Bloch 	dev->ib_dev.uverbs_ex_cmd_mask |=
6348c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6349c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6350c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6351c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6352c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
635396458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
63548e6efa3aSMark Bloch 
6355e3f1ed1fSLeon Romanovsky 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6356e3f1ed1fSLeon Romanovsky 
635726628e2dSMark Bloch 	/* Register only for native ports */
63588e6efa3aSMark Bloch 	return mlx5_add_netdev_notifier(dev, port_num);
63598e6efa3aSMark Bloch }
63608e6efa3aSMark Bloch 
63618e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
63628e6efa3aSMark Bloch {
63638e6efa3aSMark Bloch 	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
63648e6efa3aSMark Bloch 
63658e6efa3aSMark Bloch 	mlx5_remove_netdev_notifier(dev, port_num);
63668e6efa3aSMark Bloch }
63678e6efa3aSMark Bloch 
6368fb652d32SMark Bloch static int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
63698e6efa3aSMark Bloch {
63708e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
63718e6efa3aSMark Bloch 	enum rdma_link_layer ll;
63728e6efa3aSMark Bloch 	int port_type_cap;
63738e6efa3aSMark Bloch 	int err = 0;
63748e6efa3aSMark Bloch 
63758e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
63768e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
63778e6efa3aSMark Bloch 
63788e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET)
6379e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
63808e6efa3aSMark Bloch 
638116c1975fSMark Bloch 	return err;
6382fc24fc5eSAchiad Shochat }
6383fc24fc5eSAchiad Shochat 
6384fb652d32SMark Bloch static void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
63858e6efa3aSMark Bloch {
63868e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
63878e6efa3aSMark Bloch }
63888e6efa3aSMark Bloch 
63898e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
63908e6efa3aSMark Bloch {
63918e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
63928e6efa3aSMark Bloch 	enum rdma_link_layer ll;
63938e6efa3aSMark Bloch 	int port_type_cap;
63948e6efa3aSMark Bloch 	int err;
63958e6efa3aSMark Bloch 
63968e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
63978e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
63988e6efa3aSMark Bloch 
63998e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
6400e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
64018e6efa3aSMark Bloch 		if (err)
64028e6efa3aSMark Bloch 			return err;
64038e6efa3aSMark Bloch 
6404e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
64058e6efa3aSMark Bloch 		if (err)
64068e6efa3aSMark Bloch 			goto cleanup;
64078e6efa3aSMark Bloch 	}
64088e6efa3aSMark Bloch 
640916c1975fSMark Bloch 	return 0;
64108e6efa3aSMark Bloch cleanup:
64118e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
64128e6efa3aSMark Bloch 
64138e6efa3aSMark Bloch 	return err;
641445bded2cSKamal Heib }
64156aec21f6SHaggai Eran 
641616c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
641716c1975fSMark Bloch {
641816c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
641916c1975fSMark Bloch 	enum rdma_link_layer ll;
642016c1975fSMark Bloch 	int port_type_cap;
64214a2da0b8SParav Pandit 
642216c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
642316c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
642416c1975fSMark Bloch 
642516c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
642616c1975fSMark Bloch 		mlx5_disable_eth(dev);
64278e6efa3aSMark Bloch 		mlx5_ib_stage_common_roce_cleanup(dev);
642816c1975fSMark Bloch 	}
642916c1975fSMark Bloch }
643016c1975fSMark Bloch 
6431fb652d32SMark Bloch static int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
643216c1975fSMark Bloch {
643316c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
643416c1975fSMark Bloch }
643516c1975fSMark Bloch 
6436fb652d32SMark Bloch static void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
643716c1975fSMark Bloch {
643816c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
643916c1975fSMark Bloch }
644016c1975fSMark Bloch 
644116c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
644216c1975fSMark Bloch {
644307321b3cSMark Bloch 	mlx5_ib_internal_fill_odp_caps(dev);
644407321b3cSMark Bloch 
644516c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
644616c1975fSMark Bloch }
644716c1975fSMark Bloch 
6448f3ffed0cSKamal Heib static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
6449d5d284b8SSaeed Mahameed {
6450d5d284b8SSaeed Mahameed 	mlx5_ib_odp_cleanup_one(dev);
6451d5d284b8SSaeed Mahameed }
6452d5d284b8SSaeed Mahameed 
645396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
645496458233SKamal Heib 	.alloc_hw_stats = mlx5_ib_alloc_hw_stats,
645596458233SKamal Heib 	.get_hw_stats = mlx5_ib_get_hw_stats,
645696458233SKamal Heib };
645796458233SKamal Heib 
6458fb652d32SMark Bloch static int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
645916c1975fSMark Bloch {
64605e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
646196458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
64625e1e7612SMark Bloch 
646316c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
64645e1e7612SMark Bloch 	}
646516c1975fSMark Bloch 
646616c1975fSMark Bloch 	return 0;
646716c1975fSMark Bloch }
646816c1975fSMark Bloch 
6469fb652d32SMark Bloch static void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
647016c1975fSMark Bloch {
647116c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
647216c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
647316c1975fSMark Bloch }
647416c1975fSMark Bloch 
647516c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
647616c1975fSMark Bloch {
647773eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
6478a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
647973eb8f03SGreg Kroah-Hartman 	return 0;
648016c1975fSMark Bloch }
648116c1975fSMark Bloch 
648216c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
648316c1975fSMark Bloch {
6484a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
6485a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
648616c1975fSMark Bloch }
648716c1975fSMark Bloch 
648816c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
648916c1975fSMark Bloch {
64905fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
6491444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
649216c1975fSMark Bloch }
649316c1975fSMark Bloch 
649416c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
649516c1975fSMark Bloch {
649616c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
649716c1975fSMark Bloch }
649816c1975fSMark Bloch 
6499fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
650016c1975fSMark Bloch {
650116c1975fSMark Bloch 	int err;
65025fe9dec0SEli Cohen 
65035fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
65045fe9dec0SEli Cohen 	if (err)
650516c1975fSMark Bloch 		return err;
65065fe9dec0SEli Cohen 
65075fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
65085fe9dec0SEli Cohen 	if (err)
650916c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
65105fe9dec0SEli Cohen 
651116c1975fSMark Bloch 	return err;
651216c1975fSMark Bloch }
65130837e86aSMark Bloch 
6514fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
651516c1975fSMark Bloch {
651616c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
651716c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
651816c1975fSMark Bloch }
6519e126ba97SEli Cohen 
6520fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
652116c1975fSMark Bloch {
6522e349f858SJason Gunthorpe 	const char *name;
6523e349f858SJason Gunthorpe 
6524508a523fSParav Pandit 	rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
65257c34ec19SAviv Heller 	if (!mlx5_lag_is_roce(dev->mdev))
6526e349f858SJason Gunthorpe 		name = "mlx5_%d";
6527e349f858SJason Gunthorpe 	else
6528e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
6529ea4baf7fSParav Pandit 	return ib_register_device(&dev->ib_dev, name);
653016c1975fSMark Bloch }
653116c1975fSMark Bloch 
6532fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
653342cea83fSMark Bloch {
653442cea83fSMark Bloch 	destroy_umrc_res(dev);
653542cea83fSMark Bloch }
653642cea83fSMark Bloch 
6537fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
653816c1975fSMark Bloch {
653916c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
654016c1975fSMark Bloch }
654116c1975fSMark Bloch 
6542fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
654316c1975fSMark Bloch {
654416c1975fSMark Bloch 	return create_umr_res(dev);
654516c1975fSMark Bloch }
654616c1975fSMark Bloch 
654716c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
654816c1975fSMark Bloch {
654903404e8aSMaor Gottlieb 	init_delay_drop(dev);
655003404e8aSMaor Gottlieb 
655116c1975fSMark Bloch 	return 0;
655216c1975fSMark Bloch }
655316c1975fSMark Bloch 
655416c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
655516c1975fSMark Bloch {
655616c1975fSMark Bloch 	cancel_delay_drop(dev);
655716c1975fSMark Bloch }
655816c1975fSMark Bloch 
6559df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
6560df097a27SSaeed Mahameed {
6561df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
6562df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
6563df097a27SSaeed Mahameed 	return 0;
6564df097a27SSaeed Mahameed }
6565df097a27SSaeed Mahameed 
6566df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
6567df097a27SSaeed Mahameed {
6568df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
6569df097a27SSaeed Mahameed }
6570df097a27SSaeed Mahameed 
657181773ce5SLeon Romanovsky static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
657281773ce5SLeon Romanovsky {
657381773ce5SLeon Romanovsky 	int uid;
657481773ce5SLeon Romanovsky 
6575fb98153bSYishai Hadas 	uid = mlx5_ib_devx_create(dev, false);
657681773ce5SLeon Romanovsky 	if (uid > 0)
657781773ce5SLeon Romanovsky 		dev->devx_whitelist_uid = uid;
657881773ce5SLeon Romanovsky 
657981773ce5SLeon Romanovsky 	return 0;
658081773ce5SLeon Romanovsky }
658181773ce5SLeon Romanovsky static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
658281773ce5SLeon Romanovsky {
658381773ce5SLeon Romanovsky 	if (dev->devx_whitelist_uid)
658481773ce5SLeon Romanovsky 		mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
658581773ce5SLeon Romanovsky }
658681773ce5SLeon Romanovsky 
6587b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
658816c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
658916c1975fSMark Bloch 		      int stage)
659016c1975fSMark Bloch {
659116c1975fSMark Bloch 	/* Number of stages to cleanup */
659216c1975fSMark Bloch 	while (stage) {
659316c1975fSMark Bloch 		stage--;
659416c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
659516c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
659616c1975fSMark Bloch 	}
65974a6dc855SMark Bloch 
6598da796ccbSMark Bloch 	kfree(dev->port);
65994a6dc855SMark Bloch 	ib_dealloc_device(&dev->ib_dev);
660016c1975fSMark Bloch }
660116c1975fSMark Bloch 
6602b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
660316c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
660416c1975fSMark Bloch {
660516c1975fSMark Bloch 	int err;
660616c1975fSMark Bloch 	int i;
660716c1975fSMark Bloch 
660816c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
660916c1975fSMark Bloch 		if (profile->stage[i].init) {
661016c1975fSMark Bloch 			err = profile->stage[i].init(dev);
661116c1975fSMark Bloch 			if (err)
661216c1975fSMark Bloch 				goto err_out;
661316c1975fSMark Bloch 		}
661416c1975fSMark Bloch 	}
661516c1975fSMark Bloch 
661616c1975fSMark Bloch 	dev->profile = profile;
6617e126ba97SEli Cohen 	dev->ib_active = true;
6618e126ba97SEli Cohen 
66199603b61dSJack Morgenstein 	return dev;
6620e126ba97SEli Cohen 
662116c1975fSMark Bloch err_out:
662216c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
6623e126ba97SEli Cohen 
66249603b61dSJack Morgenstein 	return NULL;
6625e126ba97SEli Cohen }
6626e126ba97SEli Cohen 
662716c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
662816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
662916c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
663016c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
66319a4ca38dSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
66329a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_init,
66339a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
663416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
663516c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
663616c1975fSMark Bloch 		     NULL),
66378e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
66388e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
66398e6efa3aSMark Bloch 		     NULL),
664016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
664116c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
664216c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
6643f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6644f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
6645f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
664616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
664716c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
664816c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
6649df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6650df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
6651df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
665216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
665316c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
6654d5d284b8SSaeed Mahameed 		     mlx5_ib_stage_odp_cleanup),
665516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
665616c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
665716c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
665816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
665916c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
666016c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
666116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
666216c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
666316c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
666416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
666516c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
666616c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
666742cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
666842cea83fSMark Bloch 		     NULL,
666942cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
667081773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
667181773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_init,
667281773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_cleanup),
667316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
667416c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
667516c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
667642cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
667742cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
667842cea83fSMark Bloch 		     NULL),
667916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
668016c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
668116c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
668216c1975fSMark Bloch };
668316c1975fSMark Bloch 
6684f0666f1fSBodong Wang const struct mlx5_ib_profile uplink_rep_profile = {
6685b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
6686b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
6687b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
6688b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6689b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_init,
6690b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
6691b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6692b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
6693b5ca15adSMark Bloch 		     NULL),
6694b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6695b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_non_default_cb,
6696b5ca15adSMark Bloch 		     NULL),
6697b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6698b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_init,
6699b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_cleanup),
6700f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6701f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
6702f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
6703b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6704b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_init,
6705b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
6706df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6707df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
6708df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
6709b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6710b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_init,
6711b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
6712b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
6713b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
6714b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
6715b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6716b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
6717b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
671803fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
671903fe2debSDavid S. Miller 		     NULL,
672003fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
67217f575103SMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
67227f575103SMark Bloch 		     mlx5_ib_stage_devx_init,
67237f575103SMark Bloch 		     mlx5_ib_stage_devx_cleanup),
6724b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6725b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
6726b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
672703fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
672803fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
672903fe2debSDavid S. Miller 		     NULL),
6730b5ca15adSMark Bloch };
6731b5ca15adSMark Bloch 
6732e3f1ed1fSLeon Romanovsky static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
673332f69e4bSDaniel Jurgens {
673432f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
673532f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
673632f69e4bSDaniel Jurgens 	bool bound = false;
673732f69e4bSDaniel Jurgens 	int err;
673832f69e4bSDaniel Jurgens 
673932f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
674032f69e4bSDaniel Jurgens 	if (!mpi)
674132f69e4bSDaniel Jurgens 		return NULL;
674232f69e4bSDaniel Jurgens 
674332f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
674432f69e4bSDaniel Jurgens 
674532f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
674632f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
674732f69e4bSDaniel Jurgens 	if (err) {
674832f69e4bSDaniel Jurgens 		kfree(mpi);
674932f69e4bSDaniel Jurgens 		return NULL;
675032f69e4bSDaniel Jurgens 	}
675132f69e4bSDaniel Jurgens 
675232f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
675332f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
675432f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
675532f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
675632f69e4bSDaniel Jurgens 
675732f69e4bSDaniel Jurgens 		if (bound) {
675832f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
675932f69e4bSDaniel Jurgens 			break;
676032f69e4bSDaniel Jurgens 		}
676132f69e4bSDaniel Jurgens 	}
676232f69e4bSDaniel Jurgens 
676332f69e4bSDaniel Jurgens 	if (!bound) {
676432f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
6765c42260f1SVu Pham 		dev_dbg(mdev->device,
6766c42260f1SVu Pham 			"no suitable IB device found to bind to, added to unaffiliated list.\n");
676732f69e4bSDaniel Jurgens 	}
676832f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
676932f69e4bSDaniel Jurgens 
677032f69e4bSDaniel Jurgens 	return mpi;
677132f69e4bSDaniel Jurgens }
677232f69e4bSDaniel Jurgens 
677316c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
677416c1975fSMark Bloch {
677532f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
6776b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
677732f69e4bSDaniel Jurgens 	int port_type_cap;
6778da796ccbSMark Bloch 	int num_ports;
677932f69e4bSDaniel Jurgens 
6780b5ca15adSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
6781b5ca15adSMark Bloch 
6782f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) &&
6783f0666f1fSBodong Wang 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
67845fb58c9eSMark Bloch 		if (!mlx5_core_mp_enabled(mdev))
6785f0666f1fSBodong Wang 			mlx5_ib_register_vport_reps(mdev);
6786f0666f1fSBodong Wang 		return mdev;
6787f0666f1fSBodong Wang 	}
6788f0666f1fSBodong Wang 
678932f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
679032f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
679132f69e4bSDaniel Jurgens 
6792e3f1ed1fSLeon Romanovsky 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6793e3f1ed1fSLeon Romanovsky 		return mlx5_ib_add_slave_port(mdev);
679432f69e4bSDaniel Jurgens 
6795da796ccbSMark Bloch 	num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6796da796ccbSMark Bloch 			MLX5_CAP_GEN(mdev, num_vhca_ports));
6797459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
6798b5ca15adSMark Bloch 	if (!dev)
6799b5ca15adSMark Bloch 		return NULL;
6800da796ccbSMark Bloch 	dev->port = kcalloc(num_ports, sizeof(*dev->port),
6801da796ccbSMark Bloch 			     GFP_KERNEL);
6802da796ccbSMark Bloch 	if (!dev->port) {
6803da796ccbSMark Bloch 		ib_dealloc_device((struct ib_device *)dev);
6804da796ccbSMark Bloch 		return NULL;
6805da796ccbSMark Bloch 	}
6806b5ca15adSMark Bloch 
6807b5ca15adSMark Bloch 	dev->mdev = mdev;
6808da796ccbSMark Bloch 	dev->num_ports = num_ports;
6809b5ca15adSMark Bloch 
6810b5ca15adSMark Bloch 	return __mlx5_ib_add(dev, &pf_profile);
681116c1975fSMark Bloch }
681216c1975fSMark Bloch 
68139603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
6814e126ba97SEli Cohen {
681532f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
681632f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
68176aec21f6SHaggai Eran 
6818f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
6819f0666f1fSBodong Wang 		mlx5_ib_unregister_vport_reps(mdev);
6820f0666f1fSBodong Wang 		return;
6821f0666f1fSBodong Wang 	}
6822f0666f1fSBodong Wang 
682332f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
682432f69e4bSDaniel Jurgens 		mpi = context;
682532f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
682632f69e4bSDaniel Jurgens 		if (mpi->ibdev)
682732f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
682832f69e4bSDaniel Jurgens 		list_del(&mpi->list);
682932f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
683032f69e4bSDaniel Jurgens 		return;
683132f69e4bSDaniel Jurgens 	}
683232f69e4bSDaniel Jurgens 
683332f69e4bSDaniel Jurgens 	dev = context;
683416c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
6835e126ba97SEli Cohen }
6836e126ba97SEli Cohen 
68379603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
68389603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
68399603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
684064613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
6841e126ba97SEli Cohen };
6842e126ba97SEli Cohen 
6843c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
6844c44ef998SIlya Lesokhin {
6845c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
6846c44ef998SIlya Lesokhin 	return xlt_emergency_page;
6847c44ef998SIlya Lesokhin }
6848c44ef998SIlya Lesokhin 
6849c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
6850c44ef998SIlya Lesokhin {
6851c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
6852c44ef998SIlya Lesokhin }
6853c44ef998SIlya Lesokhin 
6854e126ba97SEli Cohen static int __init mlx5_ib_init(void)
6855e126ba97SEli Cohen {
68566aec21f6SHaggai Eran 	int err;
68576aec21f6SHaggai Eran 
6858c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
6859c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
6860d69a24e0SDaniel Jurgens 		return -ENOMEM;
6861d69a24e0SDaniel Jurgens 
6862c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
6863c44ef998SIlya Lesokhin 
6864c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6865c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
6866c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
6867c44ef998SIlya Lesokhin 		return -ENOMEM;
6868c44ef998SIlya Lesokhin 	}
6869c44ef998SIlya Lesokhin 
687081713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
68719603b61dSJack Morgenstein 
68726aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
68736aec21f6SHaggai Eran 
68746aec21f6SHaggai Eran 	return err;
6875e126ba97SEli Cohen }
6876e126ba97SEli Cohen 
6877e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
6878e126ba97SEli Cohen {
68799603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
6880d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
6881c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
6882c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
6883e126ba97SEli Cohen }
6884e126ba97SEli Cohen 
6885e126ba97SEli Cohen module_init(mlx5_ib_init);
6886e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
6887