xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 3b023e1b680a56e84c22d43486875a5aa4c78afe)
1e126ba97SEli Cohen /*
26cf0a15fSSaeed Mahameed  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3e126ba97SEli Cohen  *
4e126ba97SEli Cohen  * This software is available to you under a choice of one of two
5e126ba97SEli Cohen  * licenses.  You may choose to be licensed under the terms of the GNU
6e126ba97SEli Cohen  * General Public License (GPL) Version 2, available from the file
7e126ba97SEli Cohen  * COPYING in the main directory of this source tree, or the
8e126ba97SEli Cohen  * OpenIB.org BSD license below:
9e126ba97SEli Cohen  *
10e126ba97SEli Cohen  *     Redistribution and use in source and binary forms, with or
11e126ba97SEli Cohen  *     without modification, are permitted provided that the following
12e126ba97SEli Cohen  *     conditions are met:
13e126ba97SEli Cohen  *
14e126ba97SEli Cohen  *      - Redistributions of source code must retain the above
15e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
16e126ba97SEli Cohen  *        disclaimer.
17e126ba97SEli Cohen  *
18e126ba97SEli Cohen  *      - Redistributions in binary form must reproduce the above
19e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
20e126ba97SEli Cohen  *        disclaimer in the documentation and/or other materials
21e126ba97SEli Cohen  *        provided with the distribution.
22e126ba97SEli Cohen  *
23e126ba97SEli Cohen  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e126ba97SEli Cohen  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e126ba97SEli Cohen  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e126ba97SEli Cohen  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e126ba97SEli Cohen  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e126ba97SEli Cohen  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e126ba97SEli Cohen  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e126ba97SEli Cohen  * SOFTWARE.
31e126ba97SEli Cohen  */
32e126ba97SEli Cohen 
33fe248c3aSMaor Gottlieb #include <linux/debugfs.h>
34adec640eSChristoph Hellwig #include <linux/highmem.h>
35e126ba97SEli Cohen #include <linux/module.h>
36e126ba97SEli Cohen #include <linux/init.h>
37e126ba97SEli Cohen #include <linux/errno.h>
38e126ba97SEli Cohen #include <linux/pci.h>
39e126ba97SEli Cohen #include <linux/dma-mapping.h>
40e126ba97SEli Cohen #include <linux/slab.h>
4124da0016SAriel Levkovich #include <linux/bitmap.h>
42e126ba97SEli Cohen #include <linux/sched.h>
436e84f315SIngo Molnar #include <linux/sched/mm.h>
440881e7bdSIngo Molnar #include <linux/sched/task.h>
457c2344c3SMaor Gottlieb #include <linux/delay.h>
46e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
473f89a643SAchiad Shochat #include <rdma/ib_addr.h>
482811ba51SAchiad Shochat #include <rdma/ib_cache.h>
49ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
501b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
5172c7fe90SPravin Shedge #include <linux/mlx5/fs.h>
52cecae747SMaor Gottlieb #include <linux/mlx5/eswitch.h>
537c2344c3SMaor Gottlieb #include <linux/list.h>
54e126ba97SEli Cohen #include <rdma/ib_smi.h>
55e126ba97SEli Cohen #include <rdma/ib_umem.h>
56cfc1a89eSMaor Gottlieb #include <rdma/lag.h>
57038d2ef8SMaor Gottlieb #include <linux/in.h>
58038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
59e126ba97SEli Cohen #include "mlx5_ib.h"
60fc385b7aSMark Bloch #include "ib_rep.h"
61e1f24a79SParav Pandit #include "cmd.h"
62f3da6577SLeon Romanovsky #include "srq.h"
63333fbaa0SLeon Romanovsky #include "qp.h"
64029e88fdSLeon Romanovsky #include "wr.h"
653346c487SBoris Pismenny #include <linux/mlx5/fs_helpers.h>
66c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h>
678c84660bSMatan Barak #include <rdma/uverbs_std_types.h>
68c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h>
69c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h>
704061ff7aSErez Alfasi #include <rdma/ib_umem_odp.h>
718c84660bSMatan Barak 
728c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
738c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
74e126ba97SEli Cohen 
75e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
76daeee976SShay Drory MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) IB driver");
77e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
78e126ba97SEli Cohen 
79d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
80d69a24e0SDaniel Jurgens 	struct work_struct	work;
81df097a27SSaeed Mahameed 	union {
82df097a27SSaeed Mahameed 		struct mlx5_ib_dev	      *dev;
83df097a27SSaeed Mahameed 		struct mlx5_ib_multiport_info *mpi;
84df097a27SSaeed Mahameed 	};
85df097a27SSaeed Mahameed 	bool			is_slave;
86134e9349SSaeed Mahameed 	unsigned int		event;
87df097a27SSaeed Mahameed 	void			*param;
88d69a24e0SDaniel Jurgens };
89d69a24e0SDaniel Jurgens 
90da7525d2SEran Ben Elisha enum {
91da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
92da7525d2SEran Ben Elisha };
931b5daf11SMajd Dibbiny 
94d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
9532f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
9632f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
9732f69e4bSDaniel Jurgens /*
9832f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
9932f69e4bSDaniel Jurgens  */
10032f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
10132f69e4bSDaniel Jurgens 
102c44ef998SIlya Lesokhin /* We can't use an array for xlt_emergency_page because dma_map_single
103c44ef998SIlya Lesokhin  * doesn't work on kernel modules memory
104c44ef998SIlya Lesokhin  */
105c44ef998SIlya Lesokhin static unsigned long xlt_emergency_page;
106c44ef998SIlya Lesokhin static struct mutex xlt_emergency_page_mutex;
107c44ef998SIlya Lesokhin 
10832f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
10932f69e4bSDaniel Jurgens {
11032f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
11132f69e4bSDaniel Jurgens 
11232f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
11332f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
11432f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
11532f69e4bSDaniel Jurgens 	return dev;
11632f69e4bSDaniel Jurgens }
11732f69e4bSDaniel Jurgens 
1181b5daf11SMajd Dibbiny static enum rdma_link_layer
119ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
1201b5daf11SMajd Dibbiny {
121ebd61f68SAchiad Shochat 	switch (port_type_cap) {
1221b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
1231b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
1241b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
1251b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
1261b5daf11SMajd Dibbiny 	default:
1271b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1281b5daf11SMajd Dibbiny 	}
1291b5daf11SMajd Dibbiny }
1301b5daf11SMajd Dibbiny 
131ebd61f68SAchiad Shochat static enum rdma_link_layer
132ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
133ebd61f68SAchiad Shochat {
134ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
135ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
136ebd61f68SAchiad Shochat 
137ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
138ebd61f68SAchiad Shochat }
139ebd61f68SAchiad Shochat 
140fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
141fd65f1b8SMoni Shoua 			  u8 port_num,
142fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
143fd65f1b8SMoni Shoua {
144fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
145fd65f1b8SMoni Shoua 	int ret;
146fd65f1b8SMoni Shoua 
147fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1483023a1e9SKamal Heib 	ret = ibdev->ops.query_port(ibdev, port_num, &attr);
149fd65f1b8SMoni Shoua 	if (!ret)
150fd65f1b8SMoni Shoua 		*state = attr.state;
151fd65f1b8SMoni Shoua 	return ret;
152fd65f1b8SMoni Shoua }
153fd65f1b8SMoni Shoua 
15435b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
15535b0aa67SMark Bloch 					   struct net_device *ndev,
15635b0aa67SMark Bloch 					   u8 *port_num)
15735b0aa67SMark Bloch {
15835b0aa67SMark Bloch 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
15935b0aa67SMark Bloch 	struct net_device *rep_ndev;
16035b0aa67SMark Bloch 	struct mlx5_ib_port *port;
16135b0aa67SMark Bloch 	int i;
16235b0aa67SMark Bloch 
16335b0aa67SMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
16435b0aa67SMark Bloch 		port  = &dev->port[i];
16535b0aa67SMark Bloch 		if (!port->rep)
16635b0aa67SMark Bloch 			continue;
16735b0aa67SMark Bloch 
16835b0aa67SMark Bloch 		read_lock(&port->roce.netdev_lock);
16935b0aa67SMark Bloch 		rep_ndev = mlx5_ib_get_rep_netdev(esw,
17035b0aa67SMark Bloch 						  port->rep->vport);
17135b0aa67SMark Bloch 		if (rep_ndev == ndev) {
17235b0aa67SMark Bloch 			read_unlock(&port->roce.netdev_lock);
17335b0aa67SMark Bloch 			*port_num = i + 1;
17435b0aa67SMark Bloch 			return &port->roce;
17535b0aa67SMark Bloch 		}
17635b0aa67SMark Bloch 		read_unlock(&port->roce.netdev_lock);
17735b0aa67SMark Bloch 	}
17835b0aa67SMark Bloch 
17935b0aa67SMark Bloch 	return NULL;
18035b0aa67SMark Bloch }
18135b0aa67SMark Bloch 
182fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
183fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
184fc24fc5eSAchiad Shochat {
1857fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
186fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1877fd8aefbSDaniel Jurgens 	u8 port_num = roce->native_port_num;
1887fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1897fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1907fd8aefbSDaniel Jurgens 
1917fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
19232f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
19332f69e4bSDaniel Jurgens 	if (!mdev)
19432f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
195fc24fc5eSAchiad Shochat 
1965ec8c83eSAviv Heller 	switch (event) {
1975ec8c83eSAviv Heller 	case NETDEV_REGISTER:
19835b0aa67SMark Bloch 		/* Should already be registered during the load */
19935b0aa67SMark Bloch 		if (ibdev->is_rep)
20035b0aa67SMark Bloch 			break;
2017fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
202dce45af5SLinus Torvalds 		if (ndev->dev.parent == mdev->device)
203842a9c83SOr Gerlitz 			roce->netdev = ndev;
2047fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
2055ec8c83eSAviv Heller 		break;
2065ec8c83eSAviv Heller 
207842a9c83SOr Gerlitz 	case NETDEV_UNREGISTER:
20835b0aa67SMark Bloch 		/* In case of reps, ib device goes away before the netdevs */
209842a9c83SOr Gerlitz 		write_lock(&roce->netdev_lock);
210842a9c83SOr Gerlitz 		if (roce->netdev == ndev)
211842a9c83SOr Gerlitz 			roce->netdev = NULL;
212842a9c83SOr Gerlitz 		write_unlock(&roce->netdev_lock);
213842a9c83SOr Gerlitz 		break;
214842a9c83SOr Gerlitz 
215fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
2165ec8c83eSAviv Heller 	case NETDEV_UP:
21788621dfeSAviv Heller 	case NETDEV_DOWN: {
2187fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
21988621dfeSAviv Heller 		struct net_device *upper = NULL;
22088621dfeSAviv Heller 
22188621dfeSAviv Heller 		if (lag_ndev) {
22288621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
22388621dfeSAviv Heller 			dev_put(lag_ndev);
22488621dfeSAviv Heller 		}
22588621dfeSAviv Heller 
22635b0aa67SMark Bloch 		if (ibdev->is_rep)
22735b0aa67SMark Bloch 			roce = mlx5_get_rep_roce(ibdev, ndev, &port_num);
22835b0aa67SMark Bloch 		if (!roce)
22935b0aa67SMark Bloch 			return NOTIFY_DONE;
2307fd8aefbSDaniel Jurgens 		if ((upper == ndev || (!upper && ndev == roce->netdev))
23188621dfeSAviv Heller 		    && ibdev->ib_active) {
232626bc02dSBart Van Assche 			struct ib_event ibev = { };
233fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2345ec8c83eSAviv Heller 
2357fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2367fd8aefbSDaniel Jurgens 					   &port_state))
2377fd8aefbSDaniel Jurgens 				goto done;
238fd65f1b8SMoni Shoua 
2397fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2407fd8aefbSDaniel Jurgens 				goto done;
241fd65f1b8SMoni Shoua 
2427fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2435ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
244fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
245fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
246fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
247fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
248fd65f1b8SMoni Shoua 			else
2497fd8aefbSDaniel Jurgens 				goto done;
250fd65f1b8SMoni Shoua 
2517fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2525ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2535ec8c83eSAviv Heller 		}
2545ec8c83eSAviv Heller 		break;
25588621dfeSAviv Heller 	}
2565ec8c83eSAviv Heller 
2575ec8c83eSAviv Heller 	default:
2585ec8c83eSAviv Heller 		break;
2595ec8c83eSAviv Heller 	}
2607fd8aefbSDaniel Jurgens done:
26132f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
262fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
263fc24fc5eSAchiad Shochat }
264fc24fc5eSAchiad Shochat 
265fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
266fc24fc5eSAchiad Shochat 					     u8 port_num)
267fc24fc5eSAchiad Shochat {
268fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
269fc24fc5eSAchiad Shochat 	struct net_device *ndev;
27032f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
271fc24fc5eSAchiad Shochat 
27232f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
27332f69e4bSDaniel Jurgens 	if (!mdev)
27432f69e4bSDaniel Jurgens 		return NULL;
27532f69e4bSDaniel Jurgens 
27632f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
27788621dfeSAviv Heller 	if (ndev)
27832f69e4bSDaniel Jurgens 		goto out;
27988621dfeSAviv Heller 
280fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
281fc24fc5eSAchiad Shochat 	 */
28295579e78SMark Bloch 	read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
28395579e78SMark Bloch 	ndev = ibdev->port[port_num - 1].roce.netdev;
284fc24fc5eSAchiad Shochat 	if (ndev)
285fc24fc5eSAchiad Shochat 		dev_hold(ndev);
28695579e78SMark Bloch 	read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
287fc24fc5eSAchiad Shochat 
28832f69e4bSDaniel Jurgens out:
28932f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
290fc24fc5eSAchiad Shochat 	return ndev;
291fc24fc5eSAchiad Shochat }
292fc24fc5eSAchiad Shochat 
29332f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
29432f69e4bSDaniel Jurgens 						   u8 ib_port_num,
29532f69e4bSDaniel Jurgens 						   u8 *native_port_num)
29632f69e4bSDaniel Jurgens {
29732f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
29832f69e4bSDaniel Jurgens 							  ib_port_num);
29932f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
30032f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
30132f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
30232f69e4bSDaniel Jurgens 
303210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
304210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
305210b1f78SMark Bloch 		if (native_port_num)
306210b1f78SMark Bloch 			*native_port_num = ib_port_num;
307210b1f78SMark Bloch 		return ibdev->mdev;
308210b1f78SMark Bloch 	}
309210b1f78SMark Bloch 
31032f69e4bSDaniel Jurgens 	if (native_port_num)
31132f69e4bSDaniel Jurgens 		*native_port_num = 1;
31232f69e4bSDaniel Jurgens 
31332f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
31432f69e4bSDaniel Jurgens 	if (!port)
31532f69e4bSDaniel Jurgens 		return NULL;
31632f69e4bSDaniel Jurgens 
31732f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
31832f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
31932f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
32032f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
32132f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
32232f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
32332f69e4bSDaniel Jurgens 		 */
32432f69e4bSDaniel Jurgens 		if (!mpi->is_master)
32532f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
32632f69e4bSDaniel Jurgens 	}
32732f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
32832f69e4bSDaniel Jurgens 
32932f69e4bSDaniel Jurgens 	return mdev;
33032f69e4bSDaniel Jurgens }
33132f69e4bSDaniel Jurgens 
33232f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
33332f69e4bSDaniel Jurgens {
33432f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
33532f69e4bSDaniel Jurgens 							  port_num);
33632f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
33732f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
33832f69e4bSDaniel Jurgens 
33932f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
34032f69e4bSDaniel Jurgens 		return;
34132f69e4bSDaniel Jurgens 
34232f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
34332f69e4bSDaniel Jurgens 
34432f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
34532f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
34632f69e4bSDaniel Jurgens 	if (mpi->is_master)
34732f69e4bSDaniel Jurgens 		goto out;
34832f69e4bSDaniel Jurgens 
34932f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
35032f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
35132f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
35232f69e4bSDaniel Jurgens out:
35332f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
35432f69e4bSDaniel Jurgens }
35532f69e4bSDaniel Jurgens 
35608e8676fSAya Levin static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, u8 *active_speed,
357f1b65df5SNoa Osherovich 					   u8 *active_width)
358f1b65df5SNoa Osherovich {
359f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
360f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
361f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
362f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
363f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
364f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
365f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
366f1b65df5SNoa Osherovich 		break;
367f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
368f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
369f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
370f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
371f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
372f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
373f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
374f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
375f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
376f1b65df5SNoa Osherovich 		break;
377f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
378f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
379f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
380f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
381f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
382f1b65df5SNoa Osherovich 		break;
383f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
384f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
385f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
386f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
387f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
388f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
389f1b65df5SNoa Osherovich 		break;
390f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
391f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
392f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
393f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
394f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
395f1b65df5SNoa Osherovich 		break;
396f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
397f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
398f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
399f1b65df5SNoa Osherovich 		break;
400f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
401f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
402f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
403f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
404f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
405f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
406f1b65df5SNoa Osherovich 		break;
407f1b65df5SNoa Osherovich 	default:
408f1b65df5SNoa Osherovich 		return -EINVAL;
409f1b65df5SNoa Osherovich 	}
410f1b65df5SNoa Osherovich 
411f1b65df5SNoa Osherovich 	return 0;
412f1b65df5SNoa Osherovich }
413f1b65df5SNoa Osherovich 
41408e8676fSAya Levin static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed,
41508e8676fSAya Levin 					u8 *active_width)
41608e8676fSAya Levin {
41708e8676fSAya Levin 	switch (eth_proto_oper) {
41808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
41908e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
42008e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
42108e8676fSAya Levin 		*active_speed = IB_SPEED_SDR;
42208e8676fSAya Levin 		break;
42308e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
42408e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
42508e8676fSAya Levin 		*active_speed = IB_SPEED_DDR;
42608e8676fSAya Levin 		break;
42708e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
42808e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
42908e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
43008e8676fSAya Levin 		break;
43108e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
43208e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
43308e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
43408e8676fSAya Levin 		break;
43508e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
43608e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
43708e8676fSAya Levin 		*active_speed = IB_SPEED_EDR;
43808e8676fSAya Levin 		break;
43908e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
440cd272875SAya Levin 		*active_width = IB_WIDTH_2X;
441cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
442cd272875SAya Levin 		break;
44308e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
44408e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
44508e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
44608e8676fSAya Levin 		break;
447cd272875SAya Levin 	case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
448cd272875SAya Levin 		*active_width = IB_WIDTH_4X;
449cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
450cd272875SAya Levin 		break;
45108e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
45208e8676fSAya Levin 		*active_width = IB_WIDTH_2X;
45308e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
45408e8676fSAya Levin 		break;
45508e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
45608e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
45708e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
45808e8676fSAya Levin 		break;
45908e8676fSAya Levin 	default:
46008e8676fSAya Levin 		return -EINVAL;
46108e8676fSAya Levin 	}
46208e8676fSAya Levin 
46308e8676fSAya Levin 	return 0;
46408e8676fSAya Levin }
46508e8676fSAya Levin 
46608e8676fSAya Levin static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
46708e8676fSAya Levin 				    u8 *active_width, bool ext)
46808e8676fSAya Levin {
46908e8676fSAya Levin 	return ext ?
47008e8676fSAya Levin 		translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
47108e8676fSAya Levin 					     active_width) :
47208e8676fSAya Levin 		translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
47308e8676fSAya Levin 						active_width);
47408e8676fSAya Levin }
47508e8676fSAya Levin 
476095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
4773f89a643SAchiad Shochat 				struct ib_port_attr *props)
4783f89a643SAchiad Shochat {
4793f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
480bc4e12ffSAya Levin 	u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
481da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
48288621dfeSAviv Heller 	struct net_device *ndev, *upper;
4833f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
484b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
485c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
486f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
487b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
48808e8676fSAya Levin 	bool ext;
489095b0927SIlan Tayari 	int err;
4903f89a643SAchiad Shochat 
491b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
492b3cbd6f0SDaniel Jurgens 	if (!mdev) {
493b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
494b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
495b3cbd6f0SDaniel Jurgens 		 */
496b3cbd6f0SDaniel Jurgens 		put_mdev = false;
497b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
498b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
499b3cbd6f0SDaniel Jurgens 		port_num = 1;
500b3cbd6f0SDaniel Jurgens 	}
501b3cbd6f0SDaniel Jurgens 
502f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
503f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
50426628e2dSMark Bloch 	 * Use native port in case of reps
50550f22fd8SNoa Osherovich 	 */
50626628e2dSMark Bloch 	if (dev->is_rep)
50726628e2dSMark Bloch 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
50826628e2dSMark Bloch 					   1);
50926628e2dSMark Bloch 	else
510bc4e12ffSAya Levin 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
511b3cbd6f0SDaniel Jurgens 					   mdev_port_num);
512095b0927SIlan Tayari 	if (err)
513b3cbd6f0SDaniel Jurgens 		goto out;
51408e8676fSAya Levin 	ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet);
51508e8676fSAya Levin 	eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
516f1b65df5SNoa Osherovich 
5177672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
5187672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
5197672ed33SHonggang Li 
520f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
52108e8676fSAya Levin 				 &props->active_width, ext);
5223f89a643SAchiad Shochat 
5233f89a643SAchiad Shochat 	props->port_cap_flags |= IB_PORT_CM_SUP;
5242f944c0fSJason Gunthorpe 	props->ip_gids = true;
5253f89a643SAchiad Shochat 
5263f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
5273f89a643SAchiad Shochat 						roce_address_table_size);
5283f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
5293f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
5303f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
5313f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
53272a7720fSKamal Heib 	props->phys_state       = IB_PORT_PHYS_STATE_DISABLED;
5333f89a643SAchiad Shochat 
534b3cbd6f0SDaniel Jurgens 	mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
535c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
5363f89a643SAchiad Shochat 
537b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
538b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
539b3cbd6f0SDaniel Jurgens 		goto out;
540b3cbd6f0SDaniel Jurgens 
5413f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
5423f89a643SAchiad Shochat 	if (!ndev)
543b3cbd6f0SDaniel Jurgens 		goto out;
5443f89a643SAchiad Shochat 
5457c34ec19SAviv Heller 	if (dev->lag_active) {
54688621dfeSAviv Heller 		rcu_read_lock();
54788621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
54888621dfeSAviv Heller 		if (upper) {
54988621dfeSAviv Heller 			dev_put(ndev);
55088621dfeSAviv Heller 			ndev = upper;
55188621dfeSAviv Heller 			dev_hold(ndev);
55288621dfeSAviv Heller 		}
55388621dfeSAviv Heller 		rcu_read_unlock();
55488621dfeSAviv Heller 	}
55588621dfeSAviv Heller 
5563f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
5573f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
55872a7720fSKamal Heib 		props->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
5593f89a643SAchiad Shochat 	}
5603f89a643SAchiad Shochat 
5613f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
5623f89a643SAchiad Shochat 
5633f89a643SAchiad Shochat 	dev_put(ndev);
5643f89a643SAchiad Shochat 
5653f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
566b3cbd6f0SDaniel Jurgens out:
567b3cbd6f0SDaniel Jurgens 	if (put_mdev)
568b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
569b3cbd6f0SDaniel Jurgens 	return err;
5703f89a643SAchiad Shochat }
5713f89a643SAchiad Shochat 
572095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
573095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
574095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
5753cca2606SAchiad Shochat {
576095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
577a70c0739SParav Pandit 	u16 vlan_id = 0xffff;
578095b0927SIlan Tayari 	u8 roce_version = 0;
579095b0927SIlan Tayari 	u8 roce_l3_type = 0;
580095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
581a70c0739SParav Pandit 	int ret;
5823cca2606SAchiad Shochat 
583095b0927SIlan Tayari 	if (gid) {
584095b0927SIlan Tayari 		gid_type = attr->gid_type;
585a70c0739SParav Pandit 		ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
586a70c0739SParav Pandit 		if (ret)
587a70c0739SParav Pandit 			return ret;
5883cca2606SAchiad Shochat 	}
5893cca2606SAchiad Shochat 
590095b0927SIlan Tayari 	switch (gid_type) {
5913cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
592095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
5933cca2606SAchiad Shochat 		break;
5943cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
595095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
596095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
597095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
598095b0927SIlan Tayari 		else
599095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
6003cca2606SAchiad Shochat 		break;
6013cca2606SAchiad Shochat 
6023cca2606SAchiad Shochat 	default:
603095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
6043cca2606SAchiad Shochat 	}
6053cca2606SAchiad Shochat 
606095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
607cf34e1feSParav Pandit 				      roce_l3_type, gid->raw, mac,
608a70c0739SParav Pandit 				      vlan_id < VLAN_CFI_MASK, vlan_id,
609cf34e1feSParav Pandit 				      port_num);
6103cca2606SAchiad Shochat }
6113cca2606SAchiad Shochat 
612f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
6133cca2606SAchiad Shochat 			   __always_unused void **context)
6143cca2606SAchiad Shochat {
615414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
616f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
6173cca2606SAchiad Shochat }
6183cca2606SAchiad Shochat 
619414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
620414448d2SParav Pandit 			   __always_unused void **context)
6213cca2606SAchiad Shochat {
622414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
623414448d2SParav Pandit 			     attr->index, NULL, NULL);
6243cca2606SAchiad Shochat }
6253cca2606SAchiad Shochat 
6265ac55dfcSMark Zhang __be16 mlx5_get_roce_udp_sport_min(const struct mlx5_ib_dev *dev,
62747ec3866SParav Pandit 				   const struct ib_gid_attr *attr)
6282811ba51SAchiad Shochat {
62947ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
6302811ba51SAchiad Shochat 		return 0;
6312811ba51SAchiad Shochat 
6322811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
6332811ba51SAchiad Shochat }
6342811ba51SAchiad Shochat 
6351b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
6361b5daf11SMajd Dibbiny {
6377fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
638d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
6397fae6655SNoa Osherovich 	return 0;
6401b5daf11SMajd Dibbiny }
6411b5daf11SMajd Dibbiny 
6421b5daf11SMajd Dibbiny enum {
6431b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
6441b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
6451b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
6461b5daf11SMajd Dibbiny };
6471b5daf11SMajd Dibbiny 
6481b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
6491b5daf11SMajd Dibbiny {
6501b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
6511b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
6521b5daf11SMajd Dibbiny 
653ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
6541b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
6551b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
6561b5daf11SMajd Dibbiny 
6571b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
6581b5daf11SMajd Dibbiny }
6591b5daf11SMajd Dibbiny 
660da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
661776a3906SMoni Shoua 			    u8 atomic_size_qp,
662da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
663da7525d2SEran Ben Elisha {
664da7525d2SEran Ben Elisha 	u8 tmp;
665da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
666da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
667bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
668da7525d2SEran Ben Elisha 
669da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
670da7525d2SEran Ben Elisha 	 * of host endianness respond
671da7525d2SEran Ben Elisha 	 */
672da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
673da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
674da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
675da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
676da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
677da7525d2SEran Ben Elisha 	} else {
678da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
679da7525d2SEran Ben Elisha 	}
680da7525d2SEran Ben Elisha }
681da7525d2SEran Ben Elisha 
682776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
683776a3906SMoni Shoua 			       struct ib_device_attr *props)
684776a3906SMoni Shoua {
685776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
686776a3906SMoni Shoua 
687776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
688776a3906SMoni Shoua }
689776a3906SMoni Shoua 
6901b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
6911b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
6921b5daf11SMajd Dibbiny {
6931b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6941b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6951b5daf11SMajd Dibbiny 	u64 tmp;
6961b5daf11SMajd Dibbiny 	int err;
6971b5daf11SMajd Dibbiny 
6981b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6991b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7001b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
7011b5daf11SMajd Dibbiny 							    sys_image_guid);
7021b5daf11SMajd Dibbiny 
7031b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7041b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
7053f89a643SAchiad Shochat 		break;
7063f89a643SAchiad Shochat 
7073f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7083f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
7093f89a643SAchiad Shochat 		break;
7101b5daf11SMajd Dibbiny 
7111b5daf11SMajd Dibbiny 	default:
7121b5daf11SMajd Dibbiny 		return -EINVAL;
7131b5daf11SMajd Dibbiny 	}
7143f89a643SAchiad Shochat 
7153f89a643SAchiad Shochat 	if (!err)
7163f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
7173f89a643SAchiad Shochat 
7183f89a643SAchiad Shochat 	return err;
7193f89a643SAchiad Shochat 
7201b5daf11SMajd Dibbiny }
7211b5daf11SMajd Dibbiny 
7221b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
7231b5daf11SMajd Dibbiny 				u16 *max_pkeys)
7241b5daf11SMajd Dibbiny {
7251b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7261b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7271b5daf11SMajd Dibbiny 
7281b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7291b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7301b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
7311b5daf11SMajd Dibbiny 
7321b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7331b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7341b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
7351b5daf11SMajd Dibbiny 						pkey_table_size));
7361b5daf11SMajd Dibbiny 		return 0;
7371b5daf11SMajd Dibbiny 
7381b5daf11SMajd Dibbiny 	default:
7391b5daf11SMajd Dibbiny 		return -EINVAL;
7401b5daf11SMajd Dibbiny 	}
7411b5daf11SMajd Dibbiny }
7421b5daf11SMajd Dibbiny 
7431b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
7441b5daf11SMajd Dibbiny 				u32 *vendor_id)
7451b5daf11SMajd Dibbiny {
7461b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
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_vendor_id(ibdev, vendor_id);
7511b5daf11SMajd Dibbiny 
7521b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7531b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7541b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
7551b5daf11SMajd Dibbiny 
7561b5daf11SMajd Dibbiny 	default:
7571b5daf11SMajd Dibbiny 		return -EINVAL;
7581b5daf11SMajd Dibbiny 	}
7591b5daf11SMajd Dibbiny }
7601b5daf11SMajd Dibbiny 
7611b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
7621b5daf11SMajd Dibbiny 				__be64 *node_guid)
7631b5daf11SMajd Dibbiny {
7641b5daf11SMajd Dibbiny 	u64 tmp;
7651b5daf11SMajd Dibbiny 	int err;
7661b5daf11SMajd Dibbiny 
7671b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7681b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7691b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7701b5daf11SMajd Dibbiny 
7711b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7721b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7733f89a643SAchiad Shochat 		break;
7743f89a643SAchiad Shochat 
7753f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7763f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7773f89a643SAchiad Shochat 		break;
7781b5daf11SMajd Dibbiny 
7791b5daf11SMajd Dibbiny 	default:
7801b5daf11SMajd Dibbiny 		return -EINVAL;
7811b5daf11SMajd Dibbiny 	}
7823f89a643SAchiad Shochat 
7833f89a643SAchiad Shochat 	if (!err)
7843f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7853f89a643SAchiad Shochat 
7863f89a643SAchiad Shochat 	return err;
7871b5daf11SMajd Dibbiny }
7881b5daf11SMajd Dibbiny 
7891b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
790bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
7911b5daf11SMajd Dibbiny };
7921b5daf11SMajd Dibbiny 
7931b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
7941b5daf11SMajd Dibbiny {
7951b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
7961b5daf11SMajd Dibbiny 
7971b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
7981b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
7991b5daf11SMajd Dibbiny 
8001b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
8011b5daf11SMajd Dibbiny 
8021b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
8031b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
8041b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
8051b5daf11SMajd Dibbiny }
8061b5daf11SMajd Dibbiny 
807e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
8082528e33eSMatan Barak 				struct ib_device_attr *props,
8092528e33eSMatan Barak 				struct ib_udata *uhw)
810e126ba97SEli Cohen {
81148357091SLeon Romanovsky 	size_t uhw_outlen = (uhw) ? uhw->outlen : 0;
812e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
813938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
814e126ba97SEli Cohen 	int err = -ENOMEM;
815288c01b7SEli Cohen 	int max_sq_desc;
816e126ba97SEli Cohen 	int max_rq_sg;
817e126ba97SEli Cohen 	int max_sq_sg;
818e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
81985c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
820402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
821402ca536SBodong Wang 	size_t resp_len;
822402ca536SBodong Wang 	u64 max_tso;
823e126ba97SEli Cohen 
824402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
82548357091SLeon Romanovsky 	if (uhw_outlen && uhw_outlen < resp_len)
826402ca536SBodong Wang 		return -EINVAL;
8276f26b2acSErez Alfasi 
828402ca536SBodong Wang 	resp.response_length = resp_len;
829402ca536SBodong Wang 
83048357091SLeon Romanovsky 	if (uhw && uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
8312528e33eSMatan Barak 		return -EINVAL;
8322528e33eSMatan Barak 
833e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
8341b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
8351b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
8361b5daf11SMajd Dibbiny 	if (err)
8371b5daf11SMajd Dibbiny 		return err;
8381b5daf11SMajd Dibbiny 
8391b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
8401b5daf11SMajd Dibbiny 	if (err)
8411b5daf11SMajd Dibbiny 		return err;
8421b5daf11SMajd Dibbiny 
8431b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
8441b5daf11SMajd Dibbiny 	if (err)
8451b5daf11SMajd Dibbiny 		return err;
846e126ba97SEli Cohen 
8479603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
8489603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
8499603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
850e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
851e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
852e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
8531a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
854938fe83cSSaeed Mahameed 
855938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
856e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
857938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
858e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
859938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
860e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
861938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
862e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
863d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
864d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
865d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
866d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
867b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
868b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
869d2370e0aSMatan Barak 	}
870e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
871938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
872c0a6cbb9SIsrael Rukshin 		props->device_cap_flags |= IB_DEVICE_INTEGRITY_HANDOVER;
8732dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8742dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8752dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8762dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8772dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8782dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8792dea9094SSagi Grimberg 	}
880938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
881f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
882e126ba97SEli Cohen 
88385c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
884e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
885e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
88688115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
887e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
888e8161334SNoa Osherovich 		}
889e8161334SNoa Osherovich 
890e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
891e8161334SNoa Osherovich 			props->raw_packet_caps |=
892e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
89388115fe7SBodong Wang 
894a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) {
895402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
896402ca536SBodong Wang 			if (max_tso) {
897402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
898402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
899402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
900402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
901402ca536SBodong Wang 			}
902402ca536SBodong Wang 		}
90331f69a82SYishai Hadas 
904a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) {
90531f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
90631f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
90731f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
90831f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
90931f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
91031f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
91131f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
91231f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
91331f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
91431f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
9154e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
9164e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
9172d93fc85SMatan Barak 			if (mlx5_accel_ipsec_device_caps(dev->mdev) &
9182d93fc85SMatan Barak 			    MLX5_ACCEL_IPSEC_CAP_DEVICE)
9192d93fc85SMatan Barak 				resp.rss_caps.rx_hash_fields_mask |=
9202d93fc85SMatan Barak 					MLX5_RX_HASH_IPSEC_SPI;
92131f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
92231f69a82SYishai Hadas 		}
92331f69a82SYishai Hadas 	} else {
924a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen)
92531f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
926a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen)
92731f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
928402ca536SBodong Wang 	}
929402ca536SBodong Wang 
930f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
931f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
932f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
933f0313965SErez Shitrit 	}
934f0313965SErez Shitrit 
93503404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
93685c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
93785c7c014SDaniel Jurgens 	    raw_support)
93803404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
93903404e8aSMaor Gottlieb 
9401d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
9411d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
9421d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
9431d54f890SYishai Hadas 
944cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
94585c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
94685c7c014SDaniel Jurgens 	    raw_support) {
947e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
948cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
949e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
950e8161334SNoa Osherovich 	}
951cff5a0f3SMajd Dibbiny 
95224da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
95324da0016SAriel Levkovich 		props->max_dm_size =
95424da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
95524da0016SAriel Levkovich 	}
95624da0016SAriel Levkovich 
957da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
958da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
959da6d6ba3SMaor Gottlieb 
960b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
961b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
962b1383aa6SNoa Osherovich 
9631b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
9641b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
965e126ba97SEli Cohen 
966e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
967e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
968938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
969938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
970938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
971938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
972288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
973288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
974288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
975e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
97633023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
97733023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
978986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
979938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9809f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
981938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
982938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
983938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
984938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
985938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
986938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
987938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
988e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
989e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
990911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
991911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
99262e3c379SMax Gurtovoy 	props->max_pi_fast_reg_page_list_len =
99362e3c379SMax Gurtovoy 		props->max_fast_reg_page_list_len / 2;
99436609056SYamin Friedman 	props->max_sgl_rd =
99536609056SYamin Friedman 		MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance);
996776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
99781bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
998938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
999938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
1000e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1001e126ba97SEli Cohen 					   props->max_mcast_grp;
100286695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
10037c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
10047c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
1005e126ba97SEli Cohen 
1006e502b8b0SLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
100700815752SMoni Shoua 		if (dev->odp_caps.general_caps & IB_ODP_SUPPORT)
10088cdd312cSHaggai Eran 			props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
10098cdd312cSHaggai Eran 		props->odp_caps = dev->odp_caps;
1010a73a8955SMoni Shoua 		if (!uhw) {
1011a73a8955SMoni Shoua 			/* ODP for kernel QPs is not implemented for receive
1012a73a8955SMoni Shoua 			 * WQEs and SRQ WQEs
1013a73a8955SMoni Shoua 			 */
1014a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.rc_odp_caps &=
1015a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1016a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1017a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.uc_odp_caps &=
1018a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1019a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1020a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.ud_odp_caps &=
1021a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1022a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1023a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.xrc_odp_caps &=
1024a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1025a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1026a73a8955SMoni Shoua 		}
1027e502b8b0SLeon Romanovsky 	}
10288cdd312cSHaggai Eran 
1029051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
1030051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1031051f2630SLeon Romanovsky 
1032e53a9d26SParav Pandit 	if (mlx5_core_is_vf(mdev))
1033eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1034eff901d3SEli Cohen 
103531f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
103685c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
103731f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
103831f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
103931f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
104031f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
104131f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
104231f69a82SYishai Hadas 		props->max_wq_type_rq =
104331f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
104431f69a82SYishai Hadas 	}
104531f69a82SYishai Hadas 
1046eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
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.max_ops =
1050eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
105178b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1052eb761894SArtemy Kovalyov 	}
1053eb761894SArtemy Kovalyov 
105489705e92SDanit Goldberg 	if (MLX5_CAP_GEN(mdev, tag_matching) &&
105589705e92SDanit Goldberg 	    MLX5_CAP_GEN(mdev, rndv_offload_rc)) {
105689705e92SDanit Goldberg 		props->tm_caps.flags = IB_TM_CAP_RNDV_RC;
105789705e92SDanit Goldberg 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
105889705e92SDanit Goldberg 	}
105989705e92SDanit Goldberg 
106087ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
106187ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
106287ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
106387ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
106487ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
106587ab3f52SYonatan Cohen 	}
106687ab3f52SYonatan Cohen 
1067a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), cqe_comp_caps) <= uhw_outlen) {
1068572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
1069572f46bfSYonatan Cohen 
1070572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
10717e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
1072572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
1073572f46bfSYonatan Cohen 					     cqe_compression_max_num);
1074572f46bfSYonatan Cohen 
10757e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
10767e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
10777e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
10786f1006a4SYonatan Cohen 
10796f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
10806f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
10816f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1082572f46bfSYonatan Cohen 		}
10837e43a2a5SBodong Wang 	}
10847e43a2a5SBodong Wang 
1085a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), packet_pacing_caps) <= uhw_outlen &&
108685c7c014SDaniel Jurgens 	    raw_support) {
1087d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1088d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
1089d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
1090d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1091d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
1092d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1093d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
1094d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
109561147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
109661147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
109761147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
109861147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
1099d949167dSBodong Wang 		}
1100d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1101d949167dSBodong Wang 	}
1102d949167dSBodong Wang 
1103a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes) <=
1104a762d460SLeon Romanovsky 	    uhw_outlen) {
1105795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
11069f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1107795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1108050da902SBodong Wang 
1109050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1110050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1111050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1112050da902SBodong Wang 
11139f885201SLeon Romanovsky 		resp.response_length +=
11149f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
11159f885201SLeon Romanovsky 	}
11169f885201SLeon Romanovsky 
1117a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), flags) <= uhw_outlen) {
1118de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
11197a0c8f42SGuy Levi 
1120de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1121de57f2adSGuy Levi 			resp.flags |=
1122de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
11237a0c8f42SGuy Levi 
11247a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
11257a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
11267e11b911SDanit Goldberg 		if (MLX5_CAP_GEN(mdev, qp_packet_based))
11277e11b911SDanit Goldberg 			resp.flags |=
11287e11b911SDanit Goldberg 				MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
11297249c8eaSGuy Levi 
11307249c8eaSGuy Levi 		resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1131de57f2adSGuy Levi 	}
11329f885201SLeon Romanovsky 
1133a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), sw_parsing_caps) <= uhw_outlen) {
113496dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
113596dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
113696dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
113796dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
113896dc3fc5SNoa Osherovich 
113996dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
114096dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
114196dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
114296dc3fc5SNoa Osherovich 
114396dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
114496dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
114596dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
114696dc3fc5SNoa Osherovich 
114796dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
114896dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
114996dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
115096dc3fc5SNoa Osherovich 		}
115196dc3fc5SNoa Osherovich 	}
115296dc3fc5SNoa Osherovich 
1153a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), striding_rq_caps) <= uhw_outlen &&
115485c7c014SDaniel Jurgens 	    raw_support) {
1155b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1156b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1157b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1158b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1159b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1160b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1161c16339b6SMark Zhang 			if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range))
1162c16339b6SMark Zhang 				resp.striding_rq_caps
1163c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1164c16339b6SMark Zhang 					MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1165c16339b6SMark Zhang 			else
1166c16339b6SMark Zhang 				resp.striding_rq_caps
1167c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1168b4f34597SNoa Osherovich 					MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1169b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1170b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1171b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1172b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1173b4f34597SNoa Osherovich 		}
1174b4f34597SNoa Osherovich 	}
1175b4f34597SNoa Osherovich 
1176a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), tunnel_offloads_caps) <= uhw_outlen) {
1177f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1178f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1179f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1180f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1181f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1182f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1183f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1184f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1185f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1186f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
118741e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre))
1188e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1189e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
119041e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_udp))
1191e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1192e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1193f95ef6cbSMaor Gottlieb 	}
1194f95ef6cbSMaor Gottlieb 
119548357091SLeon Romanovsky 	if (uhw_outlen) {
1196402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1197402ca536SBodong Wang 
1198402ca536SBodong Wang 		if (err)
1199402ca536SBodong Wang 			return err;
1200402ca536SBodong Wang 	}
1201402ca536SBodong Wang 
12021b5daf11SMajd Dibbiny 	return 0;
12031b5daf11SMajd Dibbiny }
1204e126ba97SEli Cohen 
12051b5daf11SMajd Dibbiny enum mlx5_ib_width {
12061b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
12071b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
12081b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
12091b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
12101b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
12111b5daf11SMajd Dibbiny };
12121b5daf11SMajd Dibbiny 
1213db7a691aSMichael Guralnik static void translate_active_width(struct ib_device *ibdev, u8 active_width,
12141b5daf11SMajd Dibbiny 				  u8 *ib_width)
12151b5daf11SMajd Dibbiny {
12161b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12171b5daf11SMajd Dibbiny 
1218db7a691aSMichael Guralnik 	if (active_width & MLX5_IB_WIDTH_1X)
12191b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1220d764970bSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_2X)
1221d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1222db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_4X)
12231b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1224db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_8X)
12251b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1226db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_12X)
12271b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1228db7a691aSMichael Guralnik 	else {
1229db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
12301b5daf11SMajd Dibbiny 			    (int)active_width);
1231db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
12321b5daf11SMajd Dibbiny 	}
12331b5daf11SMajd Dibbiny 
1234db7a691aSMichael Guralnik 	return;
12351b5daf11SMajd Dibbiny }
12361b5daf11SMajd Dibbiny 
12371b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
12381b5daf11SMajd Dibbiny {
12391b5daf11SMajd Dibbiny 	switch (mtu) {
12401b5daf11SMajd Dibbiny 	case 256: return 1;
12411b5daf11SMajd Dibbiny 	case 512: return 2;
12421b5daf11SMajd Dibbiny 	case 1024: return 3;
12431b5daf11SMajd Dibbiny 	case 2048: return 4;
12441b5daf11SMajd Dibbiny 	case 4096: return 5;
12451b5daf11SMajd Dibbiny 	default:
12461b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
12471b5daf11SMajd Dibbiny 		return -1;
12481b5daf11SMajd Dibbiny 	}
12491b5daf11SMajd Dibbiny }
12501b5daf11SMajd Dibbiny 
12511b5daf11SMajd Dibbiny enum ib_max_vl_num {
12521b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
12531b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
12541b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
12551b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
12561b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
12571b5daf11SMajd Dibbiny };
12581b5daf11SMajd Dibbiny 
12591b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
12601b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
12611b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
12621b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
12631b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
12641b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
12651b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
12661b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
12671b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
12681b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
12691b5daf11SMajd Dibbiny };
12701b5daf11SMajd Dibbiny 
12711b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
12721b5daf11SMajd Dibbiny 				u8 *max_vl_num)
12731b5daf11SMajd Dibbiny {
12741b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
12751b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
12761b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
12771b5daf11SMajd Dibbiny 		break;
12781b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
12791b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
12801b5daf11SMajd Dibbiny 		break;
12811b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
12821b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
12831b5daf11SMajd Dibbiny 		break;
12841b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
12851b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
12861b5daf11SMajd Dibbiny 		break;
12871b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
12881b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
12891b5daf11SMajd Dibbiny 		break;
12901b5daf11SMajd Dibbiny 
12911b5daf11SMajd Dibbiny 	default:
12921b5daf11SMajd Dibbiny 		return -EINVAL;
12931b5daf11SMajd Dibbiny 	}
12941b5daf11SMajd Dibbiny 
12951b5daf11SMajd Dibbiny 	return 0;
12961b5daf11SMajd Dibbiny }
12971b5daf11SMajd Dibbiny 
12981b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
12991b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
13001b5daf11SMajd Dibbiny {
13011b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
13021b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
13031b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1304046339eaSSaeed Mahameed 	u16 max_mtu;
1305046339eaSSaeed Mahameed 	u16 oper_mtu;
13061b5daf11SMajd Dibbiny 	int err;
13071b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
13081b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
13091b5daf11SMajd Dibbiny 
13101b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
13111b5daf11SMajd Dibbiny 	if (!rep) {
13121b5daf11SMajd Dibbiny 		err = -ENOMEM;
13131b5daf11SMajd Dibbiny 		goto out;
13141b5daf11SMajd Dibbiny 	}
13151b5daf11SMajd Dibbiny 
1316c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
13171b5daf11SMajd Dibbiny 
13181b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
13191b5daf11SMajd Dibbiny 	if (err)
13201b5daf11SMajd Dibbiny 		goto out;
13211b5daf11SMajd Dibbiny 
13221b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
13231b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
13241b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
13251b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
13261b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
13271b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
13281b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
13291b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
13301b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
13311b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
13321b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
13331b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
13341b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
13351b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
13361b5daf11SMajd Dibbiny 
13374106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
13384106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
13394106a758SMichael Guralnik 
13401b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
13411b5daf11SMajd Dibbiny 	if (err)
13421b5daf11SMajd Dibbiny 		goto out;
13431b5daf11SMajd Dibbiny 
1344db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1345db7a691aSMichael Guralnik 
1346d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
13471b5daf11SMajd Dibbiny 	if (err)
13481b5daf11SMajd Dibbiny 		goto out;
13491b5daf11SMajd Dibbiny 
1350facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
13511b5daf11SMajd Dibbiny 
13521b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
13531b5daf11SMajd Dibbiny 
1354facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
13551b5daf11SMajd Dibbiny 
13561b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
13571b5daf11SMajd Dibbiny 
13581b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
13591b5daf11SMajd Dibbiny 	if (err)
13601b5daf11SMajd Dibbiny 		goto out;
13611b5daf11SMajd Dibbiny 
13621b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
13631b5daf11SMajd Dibbiny 				   &props->max_vl_num);
13641b5daf11SMajd Dibbiny out:
13651b5daf11SMajd Dibbiny 	kfree(rep);
1366e126ba97SEli Cohen 	return err;
1367e126ba97SEli Cohen }
1368e126ba97SEli Cohen 
1369e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1370e126ba97SEli Cohen 		       struct ib_port_attr *props)
1371e126ba97SEli Cohen {
1372095b0927SIlan Tayari 	unsigned int count;
1373095b0927SIlan Tayari 	int ret;
1374095b0927SIlan Tayari 
13751b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13761b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1377095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1378095b0927SIlan Tayari 		break;
1379e126ba97SEli Cohen 
13801b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1381095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1382095b0927SIlan Tayari 		break;
13831b5daf11SMajd Dibbiny 
13843f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1385095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1386095b0927SIlan Tayari 		break;
13873f89a643SAchiad Shochat 
13881b5daf11SMajd Dibbiny 	default:
1389095b0927SIlan Tayari 		ret = -EINVAL;
1390e126ba97SEli Cohen 	}
1391095b0927SIlan Tayari 
1392095b0927SIlan Tayari 	if (!ret && props) {
1393b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1394b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1395b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1396b3cbd6f0SDaniel Jurgens 
1397b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1398b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1399b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1400b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1401b3cbd6f0SDaniel Jurgens 			 */
1402b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1403b3cbd6f0SDaniel Jurgens 			port = 1;
1404b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1405b3cbd6f0SDaniel Jurgens 		}
1406b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1407b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1408b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1409095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1410095b0927SIlan Tayari 	}
1411095b0927SIlan Tayari 	return ret;
1412e126ba97SEli Cohen }
1413e126ba97SEli Cohen 
14148e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
14158e6efa3aSMark Bloch 				  struct ib_port_attr *props)
14168e6efa3aSMark Bloch {
14178e6efa3aSMark Bloch 	int ret;
14188e6efa3aSMark Bloch 
141926628e2dSMark Bloch 	/* Only link layer == ethernet is valid for representors
142026628e2dSMark Bloch 	 * and we always use port 1
142126628e2dSMark Bloch 	 */
14228e6efa3aSMark Bloch 	ret = mlx5_query_port_roce(ibdev, port, props);
14238e6efa3aSMark Bloch 	if (ret || !props)
14248e6efa3aSMark Bloch 		return ret;
14258e6efa3aSMark Bloch 
14268e6efa3aSMark Bloch 	/* We don't support GIDS */
14278e6efa3aSMark Bloch 	props->gid_tbl_len = 0;
14288e6efa3aSMark Bloch 
14298e6efa3aSMark Bloch 	return ret;
14308e6efa3aSMark Bloch }
14318e6efa3aSMark Bloch 
1432e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1433e126ba97SEli Cohen 			     union ib_gid *gid)
1434e126ba97SEli Cohen {
14351b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
14361b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1437e126ba97SEli Cohen 
14381b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14391b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14401b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1441e126ba97SEli Cohen 
14421b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14431b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1444e126ba97SEli Cohen 
14451b5daf11SMajd Dibbiny 	default:
14461b5daf11SMajd Dibbiny 		return -EINVAL;
14471b5daf11SMajd Dibbiny 	}
1448e126ba97SEli Cohen 
1449e126ba97SEli Cohen }
1450e126ba97SEli Cohen 
1451b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1452b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1453b3cbd6f0SDaniel Jurgens {
1454b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1455b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1456b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1457b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1458b3cbd6f0SDaniel Jurgens 	int err;
1459b3cbd6f0SDaniel Jurgens 
1460b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1461b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1462b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1463b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1464b3cbd6f0SDaniel Jurgens 		 */
1465b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1466b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1467b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1468b3cbd6f0SDaniel Jurgens 	}
1469b3cbd6f0SDaniel Jurgens 
1470b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1471b3cbd6f0SDaniel Jurgens 					index, pkey);
1472b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1473b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1474b3cbd6f0SDaniel Jurgens 
1475b3cbd6f0SDaniel Jurgens 	return err;
1476b3cbd6f0SDaniel Jurgens }
1477b3cbd6f0SDaniel Jurgens 
1478e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1479e126ba97SEli Cohen 			      u16 *pkey)
1480e126ba97SEli Cohen {
14811b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14821b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14831b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1484e126ba97SEli Cohen 
14851b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14861b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1487b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
14881b5daf11SMajd Dibbiny 	default:
14891b5daf11SMajd Dibbiny 		return -EINVAL;
1490e126ba97SEli Cohen 	}
14911b5daf11SMajd Dibbiny }
1492e126ba97SEli Cohen 
1493e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1494e126ba97SEli Cohen 				 struct ib_device_modify *props)
1495e126ba97SEli Cohen {
1496e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1497e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1498e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1499e126ba97SEli Cohen 	int err;
1500e126ba97SEli Cohen 
1501e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1502e126ba97SEli Cohen 		return -EOPNOTSUPP;
1503e126ba97SEli Cohen 
1504e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1505e126ba97SEli Cohen 		return 0;
1506e126ba97SEli Cohen 
1507e126ba97SEli Cohen 	/*
1508e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1509e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1510e126ba97SEli Cohen 	 */
1511bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
15129603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1513e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1514e126ba97SEli Cohen 	if (err)
1515e126ba97SEli Cohen 		return err;
1516e126ba97SEli Cohen 
1517bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1518e126ba97SEli Cohen 
1519e126ba97SEli Cohen 	return err;
1520e126ba97SEli Cohen }
1521e126ba97SEli Cohen 
1522cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1523cdbe33d0SEli Cohen 				u32 value)
1524cdbe33d0SEli Cohen {
1525cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1526b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1527b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1528cdbe33d0SEli Cohen 	int err;
1529cdbe33d0SEli Cohen 
1530b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1531b3cbd6f0SDaniel Jurgens 	if (!mdev)
1532b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1533b3cbd6f0SDaniel Jurgens 
1534b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1535cdbe33d0SEli Cohen 	if (err)
1536b3cbd6f0SDaniel Jurgens 		goto out;
1537cdbe33d0SEli Cohen 
1538cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1539cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1540cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1541b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1542b3cbd6f0SDaniel Jurgens 		goto out;
1543cdbe33d0SEli Cohen 	}
1544cdbe33d0SEli Cohen 
1545cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1546cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1547b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1548b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1549b3cbd6f0SDaniel Jurgens 
1550b3cbd6f0SDaniel Jurgens out:
1551b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1552cdbe33d0SEli Cohen 
1553cdbe33d0SEli Cohen 	return err;
1554cdbe33d0SEli Cohen }
1555cdbe33d0SEli Cohen 
1556e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1557e126ba97SEli Cohen 			       struct ib_port_modify *props)
1558e126ba97SEli Cohen {
1559e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1560e126ba97SEli Cohen 	struct ib_port_attr attr;
1561e126ba97SEli Cohen 	u32 tmp;
1562e126ba97SEli Cohen 	int err;
1563cdbe33d0SEli Cohen 	u32 change_mask;
1564cdbe33d0SEli Cohen 	u32 value;
1565cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1566cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1567cdbe33d0SEli Cohen 
1568ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1569ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1570ec255879SMajd Dibbiny 	 */
1571ec255879SMajd Dibbiny 	if (!is_ib)
1572ec255879SMajd Dibbiny 		return 0;
1573ec255879SMajd Dibbiny 
1574cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1575cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1576cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1577cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1578cdbe33d0SEli Cohen 	}
1579e126ba97SEli Cohen 
1580e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1581e126ba97SEli Cohen 
1582c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1583e126ba97SEli Cohen 	if (err)
1584e126ba97SEli Cohen 		goto out;
1585e126ba97SEli Cohen 
1586e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1587e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1588e126ba97SEli Cohen 
15899603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1590e126ba97SEli Cohen 
1591e126ba97SEli Cohen out:
1592e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1593e126ba97SEli Cohen 	return err;
1594e126ba97SEli Cohen }
1595e126ba97SEli Cohen 
159630aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
159730aa60b3SEli Cohen {
159830aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
159930aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
160030aa60b3SEli Cohen }
160130aa60b3SEli Cohen 
160231a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
160331a78a5aSYishai Hadas {
160431a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
160531a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
160631a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
160731a78a5aSYishai Hadas 
160831a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
160931a78a5aSYishai Hadas }
161031a78a5aSYishai Hadas 
1611b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1612b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
161331a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1614b037c29aSEli Cohen {
1615b037c29aSEli Cohen 	int uars_per_sys_page;
1616b037c29aSEli Cohen 	int bfregs_per_sys_page;
1617b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1618b037c29aSEli Cohen 
1619b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1620b037c29aSEli Cohen 		return -EINVAL;
1621b037c29aSEli Cohen 
1622b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1623b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1624b037c29aSEli Cohen 
1625b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1626b037c29aSEli Cohen 		return -ENOMEM;
1627b037c29aSEli Cohen 
1628b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1629b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
163031a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1631b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1632b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1633b037c29aSEli Cohen 		return -EINVAL;
1634b037c29aSEli Cohen 
163531a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
163631a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
163731a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
163831a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
163931a78a5aSYishai Hadas 
164031a78a5aSYishai 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",
1641b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1642b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
164331a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
164431a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1645b037c29aSEli Cohen 
1646b037c29aSEli Cohen 	return 0;
1647b037c29aSEli Cohen }
1648b037c29aSEli Cohen 
1649b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1650b037c29aSEli Cohen {
1651b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1652b037c29aSEli Cohen 	int err;
1653b037c29aSEli Cohen 	int i;
1654b037c29aSEli Cohen 
1655b037c29aSEli Cohen 	bfregi = &context->bfregi;
165631a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1657b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1658b037c29aSEli Cohen 		if (err)
1659b037c29aSEli Cohen 			goto error;
1660b037c29aSEli Cohen 
1661b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1662b037c29aSEli Cohen 	}
16634ed131d0SYishai Hadas 
16644ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
16654ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
16664ed131d0SYishai Hadas 
1667b037c29aSEli Cohen 	return 0;
1668b037c29aSEli Cohen 
1669b037c29aSEli Cohen error:
1670b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1671b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1672b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1673b037c29aSEli Cohen 
1674b037c29aSEli Cohen 	return err;
1675b037c29aSEli Cohen }
1676b037c29aSEli Cohen 
167715177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
167815177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1679b037c29aSEli Cohen {
1680b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1681b037c29aSEli Cohen 	int i;
1682b037c29aSEli Cohen 
1683b037c29aSEli Cohen 	bfregi = &context->bfregi;
168415177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
16854ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
168615177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
168715177999SLeon Romanovsky 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1688b037c29aSEli Cohen }
1689b037c29aSEli Cohen 
16900042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1691a560f1d9SMark Bloch {
1692a560f1d9SMark Bloch 	int err = 0;
1693a560f1d9SMark Bloch 
1694a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16950042f9e4SMark Bloch 	if (td)
1696a560f1d9SMark Bloch 		dev->lb.user_td++;
16970042f9e4SMark Bloch 	if (qp)
16980042f9e4SMark Bloch 		dev->lb.qps++;
1699a560f1d9SMark Bloch 
17000042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
17010042f9e4SMark Bloch 	    dev->lb.qps == 1) {
17020042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1703a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
17040042f9e4SMark Bloch 			dev->lb.enabled = true;
17050042f9e4SMark Bloch 		}
17060042f9e4SMark Bloch 	}
1707a560f1d9SMark Bloch 
1708a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1709a560f1d9SMark Bloch 
1710a560f1d9SMark Bloch 	return err;
1711a560f1d9SMark Bloch }
1712a560f1d9SMark Bloch 
17130042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1714a560f1d9SMark Bloch {
1715a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
17160042f9e4SMark Bloch 	if (td)
1717a560f1d9SMark Bloch 		dev->lb.user_td--;
17180042f9e4SMark Bloch 	if (qp)
17190042f9e4SMark Bloch 		dev->lb.qps--;
1720a560f1d9SMark Bloch 
17210042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
17220042f9e4SMark Bloch 	    dev->lb.qps == 0) {
17230042f9e4SMark Bloch 		if (dev->lb.enabled) {
1724a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
17250042f9e4SMark Bloch 			dev->lb.enabled = false;
17260042f9e4SMark Bloch 		}
17270042f9e4SMark Bloch 	}
1728a560f1d9SMark Bloch 
1729a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1730a560f1d9SMark Bloch }
1731a560f1d9SMark Bloch 
1732d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1733d2d19121SYishai Hadas 					  u16 uid)
1734c85023e1SHuy Nguyen {
1735c85023e1SHuy Nguyen 	int err;
1736c85023e1SHuy Nguyen 
1737cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1738cfdeb893SLeon Romanovsky 		return 0;
1739cfdeb893SLeon Romanovsky 
1740d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1741c85023e1SHuy Nguyen 	if (err)
1742c85023e1SHuy Nguyen 		return err;
1743c85023e1SHuy Nguyen 
1744c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17458978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17468978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1747c85023e1SHuy Nguyen 		return err;
1748c85023e1SHuy Nguyen 
17490042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1750c85023e1SHuy Nguyen }
1751c85023e1SHuy Nguyen 
1752d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1753d2d19121SYishai Hadas 					     u16 uid)
1754c85023e1SHuy Nguyen {
1755cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1756cfdeb893SLeon Romanovsky 		return;
1757cfdeb893SLeon Romanovsky 
1758d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1759c85023e1SHuy Nguyen 
1760c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17618978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17628978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1763c85023e1SHuy Nguyen 		return;
1764c85023e1SHuy Nguyen 
17650042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1766c85023e1SHuy Nguyen }
1767c85023e1SHuy Nguyen 
176845ec21c9SYishai Hadas static int set_ucontext_resp(struct ib_ucontext *uctx,
176945ec21c9SYishai Hadas 			     struct mlx5_ib_alloc_ucontext_resp *resp)
177045ec21c9SYishai Hadas {
177145ec21c9SYishai Hadas 	struct ib_device *ibdev = uctx->device;
177245ec21c9SYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
177345ec21c9SYishai Hadas 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
177445ec21c9SYishai Hadas 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
177545ec21c9SYishai Hadas 	int err;
177645ec21c9SYishai Hadas 
177745ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
177845ec21c9SYishai Hadas 		err = mlx5_cmd_dump_fill_mkey(dev->mdev,
177945ec21c9SYishai Hadas 					      &resp->dump_fill_mkey);
178045ec21c9SYishai Hadas 		if (err)
178145ec21c9SYishai Hadas 			return err;
178245ec21c9SYishai Hadas 		resp->comp_mask |=
178345ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
178445ec21c9SYishai Hadas 	}
178545ec21c9SYishai Hadas 
178645ec21c9SYishai Hadas 	resp->qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
178745ec21c9SYishai Hadas 	if (dev->wc_support)
178845ec21c9SYishai Hadas 		resp->bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev,
178945ec21c9SYishai Hadas 						      log_bf_reg_size);
179045ec21c9SYishai Hadas 	resp->cache_line_size = cache_line_size();
179145ec21c9SYishai Hadas 	resp->max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
179245ec21c9SYishai Hadas 	resp->max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
179345ec21c9SYishai Hadas 	resp->max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
179445ec21c9SYishai Hadas 	resp->max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
179545ec21c9SYishai Hadas 	resp->max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
179645ec21c9SYishai Hadas 	resp->cqe_version = context->cqe_version;
179745ec21c9SYishai Hadas 	resp->log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
179845ec21c9SYishai Hadas 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
179945ec21c9SYishai Hadas 	resp->num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
180045ec21c9SYishai Hadas 					MLX5_CAP_GEN(dev->mdev,
180145ec21c9SYishai Hadas 						     num_of_uars_per_page) : 1;
180245ec21c9SYishai Hadas 
180345ec21c9SYishai Hadas 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
180445ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_DEVICE) {
180545ec21c9SYishai Hadas 		if (mlx5_get_flow_namespace(dev->mdev,
180645ec21c9SYishai Hadas 				MLX5_FLOW_NAMESPACE_EGRESS))
180745ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
180845ec21c9SYishai Hadas 		if (mlx5_accel_ipsec_device_caps(dev->mdev) &
180945ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
181045ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
181145ec21c9SYishai Hadas 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
181245ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
181345ec21c9SYishai Hadas 		if (mlx5_accel_ipsec_device_caps(dev->mdev) &
181445ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
181545ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
181645ec21c9SYishai Hadas 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
181745ec21c9SYishai Hadas 	}
181845ec21c9SYishai Hadas 
181945ec21c9SYishai Hadas 	resp->tot_bfregs = bfregi->lib_uar_dyn ? 0 :
182045ec21c9SYishai Hadas 			bfregi->total_num_bfregs - bfregi->num_dyn_bfregs;
182145ec21c9SYishai Hadas 	resp->num_ports = dev->num_ports;
182245ec21c9SYishai Hadas 	resp->cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
182345ec21c9SYishai Hadas 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
182445ec21c9SYishai Hadas 
182545ec21c9SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
182645ec21c9SYishai Hadas 		mlx5_query_min_inline(dev->mdev, &resp->eth_min_inline);
182745ec21c9SYishai Hadas 		resp->eth_min_inline++;
182845ec21c9SYishai Hadas 	}
182945ec21c9SYishai Hadas 
183045ec21c9SYishai Hadas 	if (dev->mdev->clock_info)
183145ec21c9SYishai Hadas 		resp->clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
183245ec21c9SYishai Hadas 
183345ec21c9SYishai Hadas 	/*
183445ec21c9SYishai Hadas 	 * We don't want to expose information from the PCI bar that is located
183545ec21c9SYishai Hadas 	 * after 4096 bytes, so if the arch only supports larger pages, let's
183645ec21c9SYishai Hadas 	 * pretend we don't support reading the HCA's core clock. This is also
183745ec21c9SYishai Hadas 	 * forced by mmap function.
183845ec21c9SYishai Hadas 	 */
183945ec21c9SYishai Hadas 	if (PAGE_SIZE <= 4096) {
184045ec21c9SYishai Hadas 		resp->comp_mask |=
184145ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
184245ec21c9SYishai Hadas 		resp->hca_core_clock_offset =
184345ec21c9SYishai Hadas 			offsetof(struct mlx5_init_seg,
184445ec21c9SYishai Hadas 				 internal_timer_h) % PAGE_SIZE;
184545ec21c9SYishai Hadas 	}
184645ec21c9SYishai Hadas 
184745ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, ece_support))
184845ec21c9SYishai Hadas 		resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_ECE;
184945ec21c9SYishai Hadas 
185045ec21c9SYishai Hadas 	resp->num_dyn_bfregs = bfregi->num_dyn_bfregs;
185145ec21c9SYishai Hadas 	return 0;
185245ec21c9SYishai Hadas }
185345ec21c9SYishai Hadas 
1854a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1855e126ba97SEli Cohen 				  struct ib_udata *udata)
1856e126ba97SEli Cohen {
1857a2a074efSLeon Romanovsky 	struct ib_device *ibdev = uctx->device;
1858e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1859b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1860b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1861a2a074efSLeon Romanovsky 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
18622f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
186378c0f98cSEli Cohen 	int ver;
1864e126ba97SEli Cohen 	int err;
1865a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1866a168a41cSMajd Dibbiny 				     max_cqe_version);
1867b037c29aSEli Cohen 	bool lib_uar_4k;
18680a2fd01cSYishai Hadas 	bool lib_uar_dyn;
1869e126ba97SEli Cohen 
1870e126ba97SEli Cohen 	if (!dev->ib_active)
1871a2a074efSLeon Romanovsky 		return -EAGAIN;
1872e126ba97SEli Cohen 
1873e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
187478c0f98cSEli Cohen 		ver = 0;
1875e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
187678c0f98cSEli Cohen 		ver = 2;
187778c0f98cSEli Cohen 	else
1878a2a074efSLeon Romanovsky 		return -EINVAL;
187978c0f98cSEli Cohen 
1880e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1881e126ba97SEli Cohen 	if (err)
1882a2a074efSLeon Romanovsky 		return err;
1883e126ba97SEli Cohen 
1884a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1885a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
188678c0f98cSEli Cohen 
1887f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1888a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
1889b368d7cbSMatan Barak 
18902f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
18912f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
18922f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1893a2a074efSLeon Romanovsky 		return -EINVAL;
1894e126ba97SEli Cohen 
189530aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
18960a2fd01cSYishai Hadas 	lib_uar_dyn = req.lib_caps & MLX5_LIB_CAP_DYN_UAR;
18972f5ff264SEli Cohen 	bfregi = &context->bfregi;
1898b037c29aSEli Cohen 
18990a2fd01cSYishai Hadas 	if (lib_uar_dyn) {
19000a2fd01cSYishai Hadas 		bfregi->lib_uar_dyn = lib_uar_dyn;
19010a2fd01cSYishai Hadas 		goto uar_done;
19020a2fd01cSYishai Hadas 	}
19030a2fd01cSYishai Hadas 
1904b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
190531a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1906b037c29aSEli Cohen 	if (err)
1907b037c29aSEli Cohen 		goto out_ctx;
1908b037c29aSEli Cohen 
19092f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1910b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
191131a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1912b037c29aSEli Cohen 				GFP_KERNEL);
1913b037c29aSEli Cohen 	if (!bfregi->count) {
1914e126ba97SEli Cohen 		err = -ENOMEM;
1915e126ba97SEli Cohen 		goto out_ctx;
1916e126ba97SEli Cohen 	}
1917e126ba97SEli Cohen 
1918b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1919b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1920e126ba97SEli Cohen 				    GFP_KERNEL);
1921b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1922e126ba97SEli Cohen 		err = -ENOMEM;
1923e126ba97SEli Cohen 		goto out_count;
1924e126ba97SEli Cohen 	}
1925e126ba97SEli Cohen 
1926b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1927b037c29aSEli Cohen 	if (err)
1928b037c29aSEli Cohen 		goto out_sys_pages;
1929b037c29aSEli Cohen 
19300a2fd01cSYishai Hadas uar_done:
1931a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1932fb98153bSYishai Hadas 		err = mlx5_ib_devx_create(dev, true);
193376dc5a84SYishai Hadas 		if (err < 0)
1934d2d19121SYishai Hadas 			goto out_uars;
193576dc5a84SYishai Hadas 		context->devx_uid = err;
1936a8b92ca1SYishai Hadas 	}
1937a8b92ca1SYishai Hadas 
1938d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1939d2d19121SYishai Hadas 					     context->devx_uid);
1940a8b92ca1SYishai Hadas 	if (err)
1941d2d19121SYishai Hadas 		goto out_devx;
1942a8b92ca1SYishai Hadas 
1943e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1944e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1945e126ba97SEli Cohen 
194645ec21c9SYishai Hadas 	context->cqe_version = min_t(__u8,
194745ec21c9SYishai Hadas 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
194845ec21c9SYishai Hadas 				 req.max_cqe_version);
1949b368d7cbSMatan Barak 
195045ec21c9SYishai Hadas 	err = set_ucontext_resp(uctx, &resp);
195145ec21c9SYishai Hadas 	if (err)
195245ec21c9SYishai Hadas 		goto out_mdev;
1953b368d7cbSMatan Barak 
195445ec21c9SYishai Hadas 	resp.response_length = min(udata->outlen, sizeof(resp));
1955b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1956e126ba97SEli Cohen 	if (err)
1957a8b92ca1SYishai Hadas 		goto out_mdev;
1958e126ba97SEli Cohen 
19592f5ff264SEli Cohen 	bfregi->ver = ver;
19602f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
196130aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
196230aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1963f72300c5SHaggai Abramovsky 
1964802dcc7fSMark Zhang 	if (mlx5_ib_lag_should_assign_affinity(dev)) {
196595579e78SMark Bloch 		u8 port = mlx5_core_native_port_num(dev->mdev) - 1;
1966c6a21c38SMajd Dibbiny 
1967c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1968c6a21c38SMajd Dibbiny 			   atomic_add_return(
196995579e78SMark Bloch 				   1, &dev->port[port].roce.tx_port_affinity));
1970c6a21c38SMajd Dibbiny 	}
1971c6a21c38SMajd Dibbiny 
1972a2a074efSLeon Romanovsky 	return 0;
1973e126ba97SEli Cohen 
1974a8b92ca1SYishai Hadas out_mdev:
1975d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1976d2d19121SYishai Hadas out_devx:
1977a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
197876dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1979146d2f1aSmajd@mellanox.com 
1980e126ba97SEli Cohen out_uars:
1981b037c29aSEli Cohen 	deallocate_uars(dev, context);
1982b037c29aSEli Cohen 
1983b037c29aSEli Cohen out_sys_pages:
1984b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1985b037c29aSEli Cohen 
1986e126ba97SEli Cohen out_count:
19872f5ff264SEli Cohen 	kfree(bfregi->count);
1988e126ba97SEli Cohen 
1989e126ba97SEli Cohen out_ctx:
1990a2a074efSLeon Romanovsky 	return err;
1991e126ba97SEli Cohen }
1992e126ba97SEli Cohen 
19930fb556b2SYishai Hadas static int mlx5_ib_query_ucontext(struct ib_ucontext *ibcontext,
19940fb556b2SYishai Hadas 				  struct uverbs_attr_bundle *attrs)
19950fb556b2SYishai Hadas {
19960fb556b2SYishai Hadas 	struct mlx5_ib_alloc_ucontext_resp uctx_resp = {};
19970fb556b2SYishai Hadas 	int ret;
19980fb556b2SYishai Hadas 
19990fb556b2SYishai Hadas 	ret = set_ucontext_resp(ibcontext, &uctx_resp);
20000fb556b2SYishai Hadas 	if (ret)
20010fb556b2SYishai Hadas 		return ret;
20020fb556b2SYishai Hadas 
20030fb556b2SYishai Hadas 	uctx_resp.response_length =
20040fb556b2SYishai Hadas 		min_t(size_t,
20050fb556b2SYishai Hadas 		      uverbs_attr_get_len(attrs,
20060fb556b2SYishai Hadas 				MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX),
20070fb556b2SYishai Hadas 		      sizeof(uctx_resp));
20080fb556b2SYishai Hadas 
20090fb556b2SYishai Hadas 	ret = uverbs_copy_to_struct_or_zero(attrs,
20100fb556b2SYishai Hadas 					MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
20110fb556b2SYishai Hadas 					&uctx_resp,
20120fb556b2SYishai Hadas 					sizeof(uctx_resp));
20130fb556b2SYishai Hadas 	return ret;
20140fb556b2SYishai Hadas }
20150fb556b2SYishai Hadas 
2016a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
2017e126ba97SEli Cohen {
2018e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2019e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
2020b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
2021e126ba97SEli Cohen 
2022b037c29aSEli Cohen 	bfregi = &context->bfregi;
2023d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2024d2d19121SYishai Hadas 
2025b037c29aSEli Cohen 	if (context->devx_uid)
202676dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
2027e126ba97SEli Cohen 
2028e126ba97SEli Cohen 	deallocate_uars(dev, context);
20292f5ff264SEli Cohen 	kfree(bfregi->sys_pages);
20302f5ff264SEli Cohen 	kfree(bfregi->count);
2031e126ba97SEli Cohen }
2032e126ba97SEli Cohen 
2033e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2034e126ba97SEli Cohen 				 int uar_idx)
2035e126ba97SEli Cohen {
2036b037c29aSEli Cohen 	int fw_uars_per_page;
2037b037c29aSEli Cohen 
2038b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2039b037c29aSEli Cohen 
2040aa8106f1SHuy Nguyen 	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2041e126ba97SEli Cohen }
2042e126ba97SEli Cohen 
2043342ee59dSYishai Hadas static u64 uar_index2paddress(struct mlx5_ib_dev *dev,
2044342ee59dSYishai Hadas 				 int uar_idx)
2045342ee59dSYishai Hadas {
2046342ee59dSYishai Hadas 	unsigned int fw_uars_per_page;
2047342ee59dSYishai Hadas 
2048342ee59dSYishai Hadas 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
2049342ee59dSYishai Hadas 				MLX5_UARS_IN_PAGE : 1;
2050342ee59dSYishai Hadas 
2051342ee59dSYishai Hadas 	return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE);
2052342ee59dSYishai Hadas }
2053342ee59dSYishai Hadas 
2054e126ba97SEli Cohen static int get_command(unsigned long offset)
2055e126ba97SEli Cohen {
2056e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2057e126ba97SEli Cohen }
2058e126ba97SEli Cohen 
2059e126ba97SEli Cohen static int get_arg(unsigned long offset)
2060e126ba97SEli Cohen {
2061e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2062e126ba97SEli Cohen }
2063e126ba97SEli Cohen 
2064e126ba97SEli Cohen static int get_index(unsigned long offset)
2065e126ba97SEli Cohen {
2066e126ba97SEli Cohen 	return get_arg(offset);
2067e126ba97SEli Cohen }
2068e126ba97SEli Cohen 
20694ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
20704ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20714ed131d0SYishai Hadas {
20724ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20734ed131d0SYishai Hadas }
20744ed131d0SYishai Hadas 
20757c2344c3SMaor Gottlieb 
20767c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20777c2344c3SMaor Gottlieb {
20787c2344c3SMaor Gottlieb }
20797c2344c3SMaor Gottlieb 
208037aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2081e126ba97SEli Cohen {
208237aa5c36SGuy Levi 	switch (cmd) {
208337aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
208437aa5c36SGuy Levi 		return "WC";
2085e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
208637aa5c36SGuy Levi 		return "best effort WC";
208737aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
208837aa5c36SGuy Levi 		return "NC";
208924da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
209024da0016SAriel Levkovich 		return "Device Memory";
209137aa5c36SGuy Levi 	default:
209237aa5c36SGuy Levi 		return NULL;
209337aa5c36SGuy Levi 	}
209437aa5c36SGuy Levi }
209537aa5c36SGuy Levi 
20965c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20975c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20985c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20995c99eaecSFeras Daoud {
21004eb6ab13SJason Gunthorpe 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
21014eb6ab13SJason Gunthorpe 	    !(vma->vm_flags & VM_SHARED))
21025c99eaecSFeras Daoud 		return -EINVAL;
21035c99eaecSFeras Daoud 
21045c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
21055c99eaecSFeras Daoud 		return -EOPNOTSUPP;
21065c99eaecSFeras Daoud 
21074eb6ab13SJason Gunthorpe 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
21085c99eaecSFeras Daoud 		return -EPERM;
2109c660133cSJason Gunthorpe 	vma->vm_flags &= ~VM_MAYWRITE;
21105c99eaecSFeras Daoud 
2111ddcdc368SJason Gunthorpe 	if (!dev->mdev->clock_info)
21125c99eaecSFeras Daoud 		return -EOPNOTSUPP;
21135c99eaecSFeras Daoud 
21144eb6ab13SJason Gunthorpe 	return vm_insert_page(vma, vma->vm_start,
21154eb6ab13SJason Gunthorpe 			      virt_to_page(dev->mdev->clock_info));
21165c99eaecSFeras Daoud }
21175c99eaecSFeras Daoud 
2118dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
2119dc2316ebSYishai Hadas {
2120dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
2121dc2316ebSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device);
21227be76befSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
2123dc2316ebSYishai Hadas 	struct mlx5_ib_dm *mdm;
2124dc2316ebSYishai Hadas 
2125dc2316ebSYishai Hadas 	switch (mentry->mmap_flag) {
2126dc2316ebSYishai Hadas 	case MLX5_IB_MMAP_TYPE_MEMIC:
2127dc2316ebSYishai Hadas 		mdm = container_of(mentry, struct mlx5_ib_dm, mentry);
2128dc2316ebSYishai Hadas 		mlx5_cmd_dealloc_memic(&dev->dm, mdm->dev_addr,
2129dc2316ebSYishai Hadas 				       mdm->size);
2130dc2316ebSYishai Hadas 		kfree(mdm);
2131dc2316ebSYishai Hadas 		break;
21327be76befSYishai Hadas 	case MLX5_IB_MMAP_TYPE_VAR:
21337be76befSYishai Hadas 		mutex_lock(&var_table->bitmap_lock);
21347be76befSYishai Hadas 		clear_bit(mentry->page_idx, var_table->bitmap);
21357be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
21367be76befSYishai Hadas 		kfree(mentry);
21377be76befSYishai Hadas 		break;
2138342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_WC:
2139342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_NC:
2140342ee59dSYishai Hadas 		mlx5_cmd_free_uar(dev->mdev, mentry->page_idx);
2141342ee59dSYishai Hadas 		kfree(mentry);
2142342ee59dSYishai Hadas 		break;
2143dc2316ebSYishai Hadas 	default:
2144dc2316ebSYishai Hadas 		WARN_ON(true);
2145dc2316ebSYishai Hadas 	}
2146dc2316ebSYishai Hadas }
2147dc2316ebSYishai Hadas 
214837aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
21497c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
21507c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
215137aa5c36SGuy Levi {
21522f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
215337aa5c36SGuy Levi 	int err;
215437aa5c36SGuy Levi 	unsigned long idx;
2155aa09ea6eSKamal Heib 	phys_addr_t pfn;
215637aa5c36SGuy Levi 	pgprot_t prot;
21574ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
21584ed131d0SYishai Hadas 	u32 uar_index;
21594ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
21604ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
21614ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2162b037c29aSEli Cohen 
21630a2fd01cSYishai Hadas 	if (bfregi->lib_uar_dyn)
21640a2fd01cSYishai Hadas 		return -EINVAL;
21650a2fd01cSYishai Hadas 
2166b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2167b037c29aSEli Cohen 		return -EINVAL;
2168b037c29aSEli Cohen 
21694ed131d0SYishai Hadas 	if (dyn_uar)
21704ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
21714ed131d0SYishai Hadas 	else
2172b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
21734ed131d0SYishai Hadas 
21744ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21754ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21764ed131d0SYishai Hadas 			     idx, max_valid_idx);
2177b037c29aSEli Cohen 		return -EINVAL;
2178b037c29aSEli Cohen 	}
217937aa5c36SGuy Levi 
218037aa5c36SGuy Levi 	switch (cmd) {
218137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21824ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
218337aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
218437aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
218537aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
218637aa5c36SGuy Levi 		break;
218737aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
218837aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
218937aa5c36SGuy Levi 		break;
219037aa5c36SGuy Levi 	default:
219137aa5c36SGuy Levi 		return -EINVAL;
219237aa5c36SGuy Levi 	}
219337aa5c36SGuy Levi 
21944ed131d0SYishai Hadas 	if (dyn_uar) {
21954ed131d0SYishai Hadas 		int uars_per_page;
21964ed131d0SYishai Hadas 
21974ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21984ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21994ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
22004ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
22014ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
22024ed131d0SYishai Hadas 			return -EINVAL;
22034ed131d0SYishai Hadas 		}
22044ed131d0SYishai Hadas 
22054ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
22064ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
22074ed131d0SYishai Hadas 		 * page holds its count.
22084ed131d0SYishai Hadas 		 */
22094ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
22104ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
22114ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
22124ed131d0SYishai Hadas 			return -EINVAL;
22134ed131d0SYishai Hadas 		}
22144ed131d0SYishai Hadas 
22154ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
22164ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
22174ed131d0SYishai Hadas 
22184ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
22194ed131d0SYishai Hadas 		if (err) {
22204ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
22214ed131d0SYishai Hadas 			goto free_bfreg;
22224ed131d0SYishai Hadas 		}
22234ed131d0SYishai Hadas 	} else {
22244ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
22254ed131d0SYishai Hadas 	}
22264ed131d0SYishai Hadas 
22274ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
222837aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2229e126ba97SEli Cohen 
2230e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2231c043ff2cSMichal Kalderon 				prot, NULL);
223237aa5c36SGuy Levi 	if (err) {
22338f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2234e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
22358f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
22364ed131d0SYishai Hadas 		goto err;
223737aa5c36SGuy Levi 	}
2238e126ba97SEli Cohen 
22394ed131d0SYishai Hadas 	if (dyn_uar)
22404ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
22414ed131d0SYishai Hadas 	return 0;
22424ed131d0SYishai Hadas 
22434ed131d0SYishai Hadas err:
22444ed131d0SYishai Hadas 	if (!dyn_uar)
22454ed131d0SYishai Hadas 		return err;
22464ed131d0SYishai Hadas 
22474ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
22484ed131d0SYishai Hadas 
22494ed131d0SYishai Hadas free_bfreg:
22504ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
22514ed131d0SYishai Hadas 
22524ed131d0SYishai Hadas 	return err;
225337aa5c36SGuy Levi }
225437aa5c36SGuy Levi 
2255dc2316ebSYishai Hadas static int add_dm_mmap_entry(struct ib_ucontext *context,
2256dc2316ebSYishai Hadas 			     struct mlx5_ib_dm *mdm,
2257dc2316ebSYishai Hadas 			     u64 address)
225824da0016SAriel Levkovich {
2259dc2316ebSYishai Hadas 	mdm->mentry.mmap_flag = MLX5_IB_MMAP_TYPE_MEMIC;
2260dc2316ebSYishai Hadas 	mdm->mentry.address = address;
2261dc2316ebSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
2262dc2316ebSYishai Hadas 			context, &mdm->mentry.rdma_entry,
2263dc2316ebSYishai Hadas 			mdm->size,
2264dc2316ebSYishai Hadas 			MLX5_IB_MMAP_DEVICE_MEM << 16,
2265dc2316ebSYishai Hadas 			(MLX5_IB_MMAP_DEVICE_MEM << 16) + (1UL << 16) - 1);
2266dc2316ebSYishai Hadas }
226724da0016SAriel Levkovich 
2268dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma)
2269dc2316ebSYishai Hadas {
2270dc2316ebSYishai Hadas 	unsigned long idx;
2271dc2316ebSYishai Hadas 	u8 command;
2272dc2316ebSYishai Hadas 
2273dc2316ebSYishai Hadas 	command = get_command(vma->vm_pgoff);
2274dc2316ebSYishai Hadas 	idx = get_extended_index(vma->vm_pgoff);
2275dc2316ebSYishai Hadas 
2276dc2316ebSYishai Hadas 	return (command << 16 | idx);
2277dc2316ebSYishai Hadas }
2278dc2316ebSYishai Hadas 
2279dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev,
2280dc2316ebSYishai Hadas 			       struct vm_area_struct *vma,
2281dc2316ebSYishai Hadas 			       struct ib_ucontext *ucontext)
2282dc2316ebSYishai Hadas {
2283dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry;
2284dc2316ebSYishai Hadas 	struct rdma_user_mmap_entry *entry;
2285dc2316ebSYishai Hadas 	unsigned long pgoff;
2286dc2316ebSYishai Hadas 	pgprot_t prot;
2287dc2316ebSYishai Hadas 	phys_addr_t pfn;
2288dc2316ebSYishai Hadas 	int ret;
2289dc2316ebSYishai Hadas 
2290dc2316ebSYishai Hadas 	pgoff = mlx5_vma_to_pgoff(vma);
2291dc2316ebSYishai Hadas 	entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff);
2292dc2316ebSYishai Hadas 	if (!entry)
229324da0016SAriel Levkovich 		return -EINVAL;
229424da0016SAriel Levkovich 
2295dc2316ebSYishai Hadas 	mentry = to_mmmap(entry);
2296dc2316ebSYishai Hadas 	pfn = (mentry->address >> PAGE_SHIFT);
2297342ee59dSYishai Hadas 	if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR ||
2298342ee59dSYishai Hadas 	    mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC)
22993f59b6c3SYishai Hadas 		prot = pgprot_noncached(vma->vm_page_prot);
23003f59b6c3SYishai Hadas 	else
2301dc2316ebSYishai Hadas 		prot = pgprot_writecombine(vma->vm_page_prot);
2302dc2316ebSYishai Hadas 	ret = rdma_user_mmap_io(ucontext, vma, pfn,
2303dc2316ebSYishai Hadas 				entry->npages * PAGE_SIZE,
2304dc2316ebSYishai Hadas 				prot,
2305dc2316ebSYishai Hadas 				entry);
2306dc2316ebSYishai Hadas 	rdma_user_mmap_entry_put(&mentry->rdma_entry);
2307dc2316ebSYishai Hadas 	return ret;
230824da0016SAriel Levkovich }
230924da0016SAriel Levkovich 
23107be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry)
23117be76befSYishai Hadas {
23129b6d3bbcSLeon Romanovsky 	u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF;
23139b6d3bbcSLeon Romanovsky 	u64 index = entry->rdma_entry.start_pgoff & 0xFFFF;
23147be76befSYishai Hadas 
23157be76befSYishai Hadas 	return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) |
23167be76befSYishai Hadas 		(index & 0xFF)) << PAGE_SHIFT;
23177be76befSYishai Hadas }
23187be76befSYishai Hadas 
231937aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
232037aa5c36SGuy Levi {
232137aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
232237aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
232337aa5c36SGuy Levi 	unsigned long command;
232437aa5c36SGuy Levi 	phys_addr_t pfn;
232537aa5c36SGuy Levi 
232637aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
232737aa5c36SGuy Levi 	switch (command) {
232837aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
23291f3db161SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
23301f3db161SYishai Hadas 		if (!dev->wc_support)
23311f3db161SYishai Hadas 			return -EPERM;
23321f3db161SYishai Hadas 		fallthrough;
233337aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
233437aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
23357c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2336e126ba97SEli Cohen 
2337e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2338e126ba97SEli Cohen 		return -ENOSYS;
2339e126ba97SEli Cohen 
2340d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2341d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2342d69e3bcfSMatan Barak 			return -EINVAL;
2343d69e3bcfSMatan Barak 
23446cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2345d69e3bcfSMatan Barak 			return -EPERM;
2346c660133cSJason Gunthorpe 		vma->vm_flags &= ~VM_MAYWRITE;
2347d69e3bcfSMatan Barak 
2348d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2349d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2350d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2351d69e3bcfSMatan Barak 
2352d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2353d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2354d69e3bcfSMatan Barak 			PAGE_SHIFT;
2355d5e560d3SJason Gunthorpe 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2356d5e560d3SJason Gunthorpe 					 PAGE_SIZE,
2357c043ff2cSMichal Kalderon 					 pgprot_noncached(vma->vm_page_prot),
2358c043ff2cSMichal Kalderon 					 NULL);
23595c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
23605c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2361d69e3bcfSMatan Barak 
2362e126ba97SEli Cohen 	default:
2363dc2316ebSYishai Hadas 		return mlx5_ib_mmap_offset(dev, vma, ibcontext);
2364e126ba97SEli Cohen 	}
2365e126ba97SEli Cohen 
2366e126ba97SEli Cohen 	return 0;
2367e126ba97SEli Cohen }
2368e126ba97SEli Cohen 
236925c13324SAriel Levkovich static inline int check_dm_type_support(struct mlx5_ib_dev *dev,
237025c13324SAriel Levkovich 					u32 type)
237125c13324SAriel Levkovich {
237225c13324SAriel Levkovich 	switch (type) {
237325c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
237425c13324SAriel Levkovich 		if (!MLX5_CAP_DEV_MEM(dev->mdev, memic))
237525c13324SAriel Levkovich 			return -EOPNOTSUPP;
237625c13324SAriel Levkovich 		break;
237725c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2378c9b9dcb4SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
237925c13324SAriel Levkovich 		if (!capable(CAP_SYS_RAWIO) ||
238025c13324SAriel Levkovich 		    !capable(CAP_NET_RAW))
238125c13324SAriel Levkovich 			return -EPERM;
238225c13324SAriel Levkovich 
238325c13324SAriel Levkovich 		if (!(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner) ||
238425c13324SAriel Levkovich 		      MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner)))
238525c13324SAriel Levkovich 			return -EOPNOTSUPP;
238625c13324SAriel Levkovich 		break;
238725c13324SAriel Levkovich 	}
238825c13324SAriel Levkovich 
238925c13324SAriel Levkovich 	return 0;
239025c13324SAriel Levkovich }
239125c13324SAriel Levkovich 
23923b113a1eSAriel Levkovich static int handle_alloc_dm_memic(struct ib_ucontext *ctx,
23933b113a1eSAriel Levkovich 				 struct mlx5_ib_dm *dm,
239424da0016SAriel Levkovich 				 struct ib_dm_alloc_attr *attr,
239524da0016SAriel Levkovich 				 struct uverbs_attr_bundle *attrs)
239624da0016SAriel Levkovich {
23973b113a1eSAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
239824da0016SAriel Levkovich 	u64 start_offset;
2399dc2316ebSYishai Hadas 	u16 page_idx;
240024da0016SAriel Levkovich 	int err;
2401dc2316ebSYishai Hadas 	u64 address;
240224da0016SAriel Levkovich 
24033b113a1eSAriel Levkovich 	dm->size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
240424da0016SAriel Levkovich 
24053b113a1eSAriel Levkovich 	err = mlx5_cmd_alloc_memic(dm_db, &dm->dev_addr,
24063b113a1eSAriel Levkovich 				   dm->size, attr->alignment);
240724da0016SAriel Levkovich 	if (err)
24083b113a1eSAriel Levkovich 		return err;
240924da0016SAriel Levkovich 
2410dc2316ebSYishai Hadas 	address = dm->dev_addr & PAGE_MASK;
2411dc2316ebSYishai Hadas 	err = add_dm_mmap_entry(ctx, dm, address);
241224da0016SAriel Levkovich 	if (err)
241324da0016SAriel Levkovich 		goto err_dealloc;
241424da0016SAriel Levkovich 
2415dc2316ebSYishai Hadas 	page_idx = dm->mentry.rdma_entry.start_pgoff & 0xFFFF;
2416dc2316ebSYishai Hadas 	err = uverbs_copy_to(attrs,
2417dc2316ebSYishai Hadas 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2418dc2316ebSYishai Hadas 			     &page_idx,
2419dc2316ebSYishai Hadas 			     sizeof(page_idx));
2420dc2316ebSYishai Hadas 	if (err)
2421dc2316ebSYishai Hadas 		goto err_copy;
2422dc2316ebSYishai Hadas 
24233b113a1eSAriel Levkovich 	start_offset = dm->dev_addr & ~PAGE_MASK;
24243b113a1eSAriel Levkovich 	err = uverbs_copy_to(attrs,
24253b113a1eSAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
24263b113a1eSAriel Levkovich 			     &start_offset, sizeof(start_offset));
24273b113a1eSAriel Levkovich 	if (err)
2428dc2316ebSYishai Hadas 		goto err_copy;
24293b113a1eSAriel Levkovich 
24303b113a1eSAriel Levkovich 	return 0;
24313b113a1eSAriel Levkovich 
2432dc2316ebSYishai Hadas err_copy:
2433dc2316ebSYishai Hadas 	rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
24343b113a1eSAriel Levkovich err_dealloc:
24353b113a1eSAriel Levkovich 	mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
24363b113a1eSAriel Levkovich 
24373b113a1eSAriel Levkovich 	return err;
24383b113a1eSAriel Levkovich }
24393b113a1eSAriel Levkovich 
244025c13324SAriel Levkovich static int handle_alloc_dm_sw_icm(struct ib_ucontext *ctx,
244125c13324SAriel Levkovich 				  struct mlx5_ib_dm *dm,
244225c13324SAriel Levkovich 				  struct ib_dm_alloc_attr *attr,
244325c13324SAriel Levkovich 				  struct uverbs_attr_bundle *attrs,
244425c13324SAriel Levkovich 				  int type)
244525c13324SAriel Levkovich {
2446c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ctx->device)->mdev;
244725c13324SAriel Levkovich 	u64 act_size;
244825c13324SAriel Levkovich 	int err;
244925c13324SAriel Levkovich 
245025c13324SAriel Levkovich 	/* Allocation size must a multiple of the basic block size
245125c13324SAriel Levkovich 	 * and a power of 2.
245225c13324SAriel Levkovich 	 */
2453c9b9dcb4SAriel Levkovich 	act_size = round_up(attr->length, MLX5_SW_ICM_BLOCK_SIZE(dev));
245425c13324SAriel Levkovich 	act_size = roundup_pow_of_two(act_size);
245525c13324SAriel Levkovich 
245625c13324SAriel Levkovich 	dm->size = act_size;
2457dff8e2d1SErez Shitrit 	err = mlx5_dm_sw_icm_alloc(dev, type, act_size, attr->alignment,
245825c13324SAriel Levkovich 				   to_mucontext(ctx)->devx_uid, &dm->dev_addr,
245925c13324SAriel Levkovich 				   &dm->icm_dm.obj_id);
246025c13324SAriel Levkovich 	if (err)
246125c13324SAriel Levkovich 		return err;
246225c13324SAriel Levkovich 
246325c13324SAriel Levkovich 	err = uverbs_copy_to(attrs,
246425c13324SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
246525c13324SAriel Levkovich 			     &dm->dev_addr, sizeof(dm->dev_addr));
246625c13324SAriel Levkovich 	if (err)
2467c9b9dcb4SAriel Levkovich 		mlx5_dm_sw_icm_dealloc(dev, type, dm->size,
2468c9b9dcb4SAriel Levkovich 				       to_mucontext(ctx)->devx_uid, dm->dev_addr,
2469c9b9dcb4SAriel Levkovich 				       dm->icm_dm.obj_id);
247025c13324SAriel Levkovich 
247125c13324SAriel Levkovich 	return err;
247225c13324SAriel Levkovich }
247325c13324SAriel Levkovich 
24743b113a1eSAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
24753b113a1eSAriel Levkovich 			       struct ib_ucontext *context,
24763b113a1eSAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
24773b113a1eSAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
24783b113a1eSAriel Levkovich {
24793b113a1eSAriel Levkovich 	struct mlx5_ib_dm *dm;
24803b113a1eSAriel Levkovich 	enum mlx5_ib_uapi_dm_type type;
24813b113a1eSAriel Levkovich 	int err;
24823b113a1eSAriel Levkovich 
24833b113a1eSAriel Levkovich 	err = uverbs_get_const_default(&type, attrs,
24843b113a1eSAriel Levkovich 				       MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
24853b113a1eSAriel Levkovich 				       MLX5_IB_UAPI_DM_TYPE_MEMIC);
24863b113a1eSAriel Levkovich 	if (err)
24873b113a1eSAriel Levkovich 		return ERR_PTR(err);
24883b113a1eSAriel Levkovich 
24893b113a1eSAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_dm req: dm_type=%d user_length=0x%llx log_alignment=%d\n",
24903b113a1eSAriel Levkovich 		    type, attr->length, attr->alignment);
24913b113a1eSAriel Levkovich 
249225c13324SAriel Levkovich 	err = check_dm_type_support(to_mdev(ibdev), type);
249325c13324SAriel Levkovich 	if (err)
249425c13324SAriel Levkovich 		return ERR_PTR(err);
249525c13324SAriel Levkovich 
24963b113a1eSAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
24973b113a1eSAriel Levkovich 	if (!dm)
24983b113a1eSAriel Levkovich 		return ERR_PTR(-ENOMEM);
24993b113a1eSAriel Levkovich 
25003b113a1eSAriel Levkovich 	dm->type = type;
25013b113a1eSAriel Levkovich 
25023b113a1eSAriel Levkovich 	switch (type) {
25033b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
25043b113a1eSAriel Levkovich 		err = handle_alloc_dm_memic(context, dm,
25053b113a1eSAriel Levkovich 					    attr,
25063b113a1eSAriel Levkovich 					    attrs);
25073b113a1eSAriel Levkovich 		break;
250825c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2509c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2510c9b9dcb4SAriel Levkovich 					     attr, attrs,
2511c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_STEERING);
2512c9b9dcb4SAriel Levkovich 		break;
251325c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2514c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2515c9b9dcb4SAriel Levkovich 					     attr, attrs,
2516c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_HEADER_MODIFY);
251725c13324SAriel Levkovich 		break;
25183b113a1eSAriel Levkovich 	default:
25193b113a1eSAriel Levkovich 		err = -EOPNOTSUPP;
25203b113a1eSAriel Levkovich 	}
25213b113a1eSAriel Levkovich 
25223b113a1eSAriel Levkovich 	if (err)
25233b113a1eSAriel Levkovich 		goto err_free;
252424da0016SAriel Levkovich 
252524da0016SAriel Levkovich 	return &dm->ibdm;
252624da0016SAriel Levkovich 
252724da0016SAriel Levkovich err_free:
252824da0016SAriel Levkovich 	kfree(dm);
252924da0016SAriel Levkovich 	return ERR_PTR(err);
253024da0016SAriel Levkovich }
253124da0016SAriel Levkovich 
2532c4367a26SShamir Rabinovitch int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
253324da0016SAriel Levkovich {
253425c13324SAriel Levkovich 	struct mlx5_ib_ucontext *ctx = rdma_udata_to_drv_context(
253525c13324SAriel Levkovich 		&attrs->driver_udata, struct mlx5_ib_ucontext, ibucontext);
2536c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ibdm->device)->mdev;
253724da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
253824da0016SAriel Levkovich 	int ret;
253924da0016SAriel Levkovich 
25403b113a1eSAriel Levkovich 	switch (dm->type) {
25413b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
2542dc2316ebSYishai Hadas 		rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
2543dc2316ebSYishai Hadas 		return 0;
254425c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2545c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_STEERING,
2546c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
2547c9b9dcb4SAriel Levkovich 					     dm->icm_dm.obj_id);
2548c9b9dcb4SAriel Levkovich 		if (ret)
2549c9b9dcb4SAriel Levkovich 			return ret;
2550c9b9dcb4SAriel Levkovich 		break;
255125c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2552c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_HEADER_MODIFY,
2553c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
255425c13324SAriel Levkovich 					     dm->icm_dm.obj_id);
255525c13324SAriel Levkovich 		if (ret)
255625c13324SAriel Levkovich 			return ret;
25573b113a1eSAriel Levkovich 		break;
25583b113a1eSAriel Levkovich 	default:
25593b113a1eSAriel Levkovich 		return -EOPNOTSUPP;
25603b113a1eSAriel Levkovich 	}
256124da0016SAriel Levkovich 
256224da0016SAriel Levkovich 	kfree(dm);
256324da0016SAriel Levkovich 
256424da0016SAriel Levkovich 	return 0;
256524da0016SAriel Levkovich }
256624da0016SAriel Levkovich 
2567ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2568e126ba97SEli Cohen {
256921a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
257021a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2571e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2572e126ba97SEli Cohen 	int err;
2573a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2574a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2575a1069c1cSYishai Hadas 	u16 uid = 0;
2576ff23dfa1SShamir Rabinovitch 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2577ff23dfa1SShamir Rabinovitch 		udata, struct mlx5_ib_ucontext, ibucontext);
2578e126ba97SEli Cohen 
2579ff23dfa1SShamir Rabinovitch 	uid = context ? context->devx_uid : 0;
2580a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2581a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
258231578defSLeon Romanovsky 	err = mlx5_cmd_exec_inout(to_mdev(ibdev)->mdev, alloc_pd, in, out);
258321a428a0SLeon Romanovsky 	if (err)
258421a428a0SLeon Romanovsky 		return err;
2585e126ba97SEli Cohen 
2586a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2587a1069c1cSYishai Hadas 	pd->uid = uid;
2588ff23dfa1SShamir Rabinovitch 	if (udata) {
2589e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2590e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2591a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
259221a428a0SLeon Romanovsky 			return -EFAULT;
2593e126ba97SEli Cohen 		}
2594e126ba97SEli Cohen 	}
2595e126ba97SEli Cohen 
259621a428a0SLeon Romanovsky 	return 0;
2597e126ba97SEli Cohen }
2598e126ba97SEli Cohen 
2599c4367a26SShamir Rabinovitch static void mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2600e126ba97SEli Cohen {
2601e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2602e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2603e126ba97SEli Cohen 
2604a1069c1cSYishai Hadas 	mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2605e126ba97SEli Cohen }
2606e126ba97SEli Cohen 
2607466fa6d2SMaor Gottlieb enum {
2608466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2609466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
261071c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_INNER_BIT,
261171c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_MISC2_BIT
2612466fa6d2SMaor Gottlieb };
2613038d2ef8SMaor Gottlieb 
2614466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2615466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2616466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2617466fa6d2SMaor Gottlieb 
2618466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2619466fa6d2SMaor Gottlieb {
2620466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2621466fa6d2SMaor Gottlieb 
2622466fa6d2SMaor Gottlieb 	match_criteria_enable =
2623466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2624466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2625466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2626466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2627466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2628466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2629466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2630466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
263171c6e863SAriel Levkovich 	match_criteria_enable |=
263271c6e863SAriel Levkovich 		(!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
263371c6e863SAriel Levkovich 		MATCH_CRITERIA_ENABLE_MISC2_BIT;
2634466fa6d2SMaor Gottlieb 
2635466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2636038d2ef8SMaor Gottlieb }
2637038d2ef8SMaor Gottlieb 
26386113cc44SMaor Gottlieb static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2639ca0d4753SMaor Gottlieb {
26406113cc44SMaor Gottlieb 	u8 entry_mask;
26416113cc44SMaor Gottlieb 	u8 entry_val;
26426113cc44SMaor Gottlieb 	int err = 0;
26436113cc44SMaor Gottlieb 
26446113cc44SMaor Gottlieb 	if (!mask)
26456113cc44SMaor Gottlieb 		goto out;
26466113cc44SMaor Gottlieb 
26476113cc44SMaor Gottlieb 	entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
26486113cc44SMaor Gottlieb 			      ip_protocol);
26496113cc44SMaor Gottlieb 	entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
26506113cc44SMaor Gottlieb 			     ip_protocol);
26516113cc44SMaor Gottlieb 	if (!entry_mask) {
2652ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2653ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
26546113cc44SMaor Gottlieb 		goto out;
26556113cc44SMaor Gottlieb 	}
26566113cc44SMaor Gottlieb 	/* Don't override existing ip protocol */
26576113cc44SMaor Gottlieb 	if (mask != entry_mask || val != entry_val)
26586113cc44SMaor Gottlieb 		err = -EINVAL;
26596113cc44SMaor Gottlieb out:
26606113cc44SMaor Gottlieb 	return err;
2661ca0d4753SMaor Gottlieb }
2662ca0d4753SMaor Gottlieb 
266337da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
26642d1e697eSMoses Reuben 			   bool inner)
26652d1e697eSMoses Reuben {
26662d1e697eSMoses Reuben 	if (inner) {
26672d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26682d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
26692d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26702d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
26712d1e697eSMoses Reuben 	} else {
26722d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26732d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
26742d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26752d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
26762d1e697eSMoses Reuben 	}
26772d1e697eSMoses Reuben }
26782d1e697eSMoses Reuben 
2679ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2680ca0d4753SMaor Gottlieb {
2681ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2682ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2683ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2684ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2685ca0d4753SMaor Gottlieb }
2686ca0d4753SMaor Gottlieb 
268771c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
268871c6e863SAriel Levkovich {
268971c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
269071c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
269171c6e863SAriel Levkovich 		return -EOPNOTSUPP;
269271c6e863SAriel Levkovich 
269371c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
269471c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
269571c6e863SAriel Levkovich 		return -EOPNOTSUPP;
269671c6e863SAriel Levkovich 
269771c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
269871c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
269971c6e863SAriel Levkovich 		return -EOPNOTSUPP;
270071c6e863SAriel Levkovich 
270171c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
270271c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
270371c6e863SAriel Levkovich 		return -EOPNOTSUPP;
270471c6e863SAriel Levkovich 
270571c6e863SAriel Levkovich 	return 0;
270671c6e863SAriel Levkovich }
270771c6e863SAriel Levkovich 
2708c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2709c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2710ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2711466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2712c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2713ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
27142ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2715a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
27163b3233fbSRaed Salem #define LAST_COUNTERS_FIELD counters
2717c47ac6aeSMaor Gottlieb 
2718c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2719c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2720c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2721c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2722c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2723c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2724c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2725c47ac6aeSMaor Gottlieb 
27262ea26203SMark Bloch int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
27272ea26203SMark Bloch 			   bool is_egress,
2728802c2125SAviad Yehezkel 			   struct mlx5_flow_act *action)
2729802c2125SAviad Yehezkel {
2730802c2125SAviad Yehezkel 
2731802c2125SAviad Yehezkel 	switch (maction->ib_action.type) {
2732802c2125SAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
2733501f14e3SMark Bloch 		if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2734501f14e3SMark Bloch 				      MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2735501f14e3SMark Bloch 			return -EINVAL;
2736802c2125SAviad Yehezkel 		/* Currently only AES_GCM keymat is supported by the driver */
2737802c2125SAviad Yehezkel 		action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
27382ea26203SMark Bloch 		action->action |= is_egress ?
2739802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2740802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2741802c2125SAviad Yehezkel 		return 0;
2742b1085be3SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
2743b1085be3SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2744b1085be3SMark Bloch 		    MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
2745501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2746501f14e3SMark Bloch 				return -EINVAL;
2747b1085be3SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
27482b688ea5SMaor Gottlieb 			action->modify_hdr =
27492b688ea5SMaor Gottlieb 				maction->flow_action_raw.modify_hdr;
2750b1085be3SMark Bloch 			return 0;
2751b1085be3SMark Bloch 		}
275210a30896SMark Bloch 		if (maction->flow_action_raw.sub_type ==
275310a30896SMark Bloch 		    MLX5_IB_FLOW_ACTION_DECAP) {
2754501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2755501f14e3SMark Bloch 				return -EINVAL;
275610a30896SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
275710a30896SMark Bloch 			return 0;
275810a30896SMark Bloch 		}
2759e806f932SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2760e806f932SMark Bloch 		    MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
2761501f14e3SMark Bloch 			if (action->action &
2762501f14e3SMark Bloch 			    MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2763501f14e3SMark Bloch 				return -EINVAL;
2764e806f932SMark Bloch 			action->action |=
2765e806f932SMark Bloch 				MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
27662b688ea5SMaor Gottlieb 			action->pkt_reformat =
27672b688ea5SMaor Gottlieb 				maction->flow_action_raw.pkt_reformat;
2768e806f932SMark Bloch 			return 0;
2769e806f932SMark Bloch 		}
2770b1085be3SMark Bloch 		/* fall through */
2771802c2125SAviad Yehezkel 	default:
2772802c2125SAviad Yehezkel 		return -EOPNOTSUPP;
2773802c2125SAviad Yehezkel 	}
2774802c2125SAviad Yehezkel }
2775802c2125SAviad Yehezkel 
2776bb0ee7dcSJianbo Liu static int parse_flow_attr(struct mlx5_core_dev *mdev,
2777bb0ee7dcSJianbo Liu 			   struct mlx5_flow_spec *spec,
2778bb0ee7dcSJianbo Liu 			   const union ib_flow_spec *ib_spec,
2779802c2125SAviad Yehezkel 			   const struct ib_flow_attr *flow_attr,
278071c6e863SAriel Levkovich 			   struct mlx5_flow_act *action, u32 prev_type)
2781038d2ef8SMaor Gottlieb {
2782bb0ee7dcSJianbo Liu 	struct mlx5_flow_context *flow_context = &spec->flow_context;
2783bb0ee7dcSJianbo Liu 	u32 *match_c = spec->match_criteria;
2784bb0ee7dcSJianbo Liu 	u32 *match_v = spec->match_value;
2785466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2786466fa6d2SMaor Gottlieb 					   misc_parameters);
2787466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2788466fa6d2SMaor Gottlieb 					   misc_parameters);
278971c6e863SAriel Levkovich 	void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
279071c6e863SAriel Levkovich 					    misc_parameters_2);
279171c6e863SAriel Levkovich 	void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
279271c6e863SAriel Levkovich 					    misc_parameters_2);
27932d1e697eSMoses Reuben 	void *headers_c;
27942d1e697eSMoses Reuben 	void *headers_v;
279519cc7524SAriel Levkovich 	int match_ipv;
2796802c2125SAviad Yehezkel 	int ret;
2797466fa6d2SMaor Gottlieb 
27982d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
27992d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
28002d1e697eSMoses Reuben 					 inner_headers);
28012d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
28022d1e697eSMoses Reuben 					 inner_headers);
280319cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
280419cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
28052d1e697eSMoses Reuben 	} else {
28062d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
28072d1e697eSMoses Reuben 					 outer_headers);
28082d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
28092d1e697eSMoses Reuben 					 outer_headers);
281019cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
281119cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
28122d1e697eSMoses Reuben 	}
28132d1e697eSMoses Reuben 
28142d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2815038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2816c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
28171ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2818038d2ef8SMaor Gottlieb 
28192d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2820038d2ef8SMaor Gottlieb 					     dmac_47_16),
2821038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
28222d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2823038d2ef8SMaor Gottlieb 					     dmac_47_16),
2824038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2825038d2ef8SMaor Gottlieb 
28262d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2827ee3da804SMaor Gottlieb 					     smac_47_16),
2828ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
28292d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2830ee3da804SMaor Gottlieb 					     smac_47_16),
2831ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2832ee3da804SMaor Gottlieb 
2833038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
28342d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
283510543365SMohamad Haj Yahia 				 cvlan_tag, 1);
28362d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
283710543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2838038d2ef8SMaor Gottlieb 
28392d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2840038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
28412d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2842038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2843038d2ef8SMaor Gottlieb 
28442d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2845038d2ef8SMaor Gottlieb 				 first_cfi,
2846038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
28472d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2848038d2ef8SMaor Gottlieb 				 first_cfi,
2849038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2850038d2ef8SMaor Gottlieb 
28512d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2852038d2ef8SMaor Gottlieb 				 first_prio,
2853038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
28542d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2855038d2ef8SMaor Gottlieb 				 first_prio,
2856038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2857038d2ef8SMaor Gottlieb 		}
28582d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2859038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
28602d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2861038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2862038d2ef8SMaor Gottlieb 		break;
2863038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2864c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
28651ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2866038d2ef8SMaor Gottlieb 
286719cc7524SAriel Levkovich 		if (match_ipv) {
286819cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
286919cc7524SAriel Levkovich 				 ip_version, 0xf);
287019cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
28713346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV4_VERSION);
287219cc7524SAriel Levkovich 		} else {
28732d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2874038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
28752d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2876038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
287719cc7524SAriel Levkovich 		}
2878038d2ef8SMaor Gottlieb 
28792d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2880038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2881038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2882038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
28832d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2884038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2885038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2886038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
28872d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2888038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2889038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2890038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
28912d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2892038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2893038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2894038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2895ca0d4753SMaor Gottlieb 
28962d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2897ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2898ca0d4753SMaor Gottlieb 
28996113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
29006113cc44SMaor Gottlieb 			      ib_spec->ipv4.mask.proto,
29016113cc44SMaor Gottlieb 			      ib_spec->ipv4.val.proto))
29026113cc44SMaor Gottlieb 			return -EINVAL;
2903038d2ef8SMaor Gottlieb 		break;
2904026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2905c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
29061ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2907026bae0cSMaor Gottlieb 
290819cc7524SAriel Levkovich 		if (match_ipv) {
290919cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
291019cc7524SAriel Levkovich 				 ip_version, 0xf);
291119cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
29123346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV6_VERSION);
291319cc7524SAriel Levkovich 		} else {
29142d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2915026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
29162d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2917026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
291819cc7524SAriel Levkovich 		}
2919026bae0cSMaor Gottlieb 
29202d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2921026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2922026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2923026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
29242d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2925026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2926026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2927026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
29282d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2929026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2930026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2931026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
29322d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2933026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2934026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2935026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2936466fa6d2SMaor Gottlieb 
29372d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2938466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2939466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2940466fa6d2SMaor Gottlieb 
29416113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
2942466fa6d2SMaor Gottlieb 			      ib_spec->ipv6.mask.next_hdr,
29436113cc44SMaor Gottlieb 			      ib_spec->ipv6.val.next_hdr))
29446113cc44SMaor Gottlieb 			return -EINVAL;
2945466fa6d2SMaor Gottlieb 
29462d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
29472d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
29482d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
29492d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
2950802c2125SAviad Yehezkel 		break;
2951802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ESP:
2952802c2125SAviad Yehezkel 		if (ib_spec->esp.mask.seq)
2953802c2125SAviad Yehezkel 			return -EOPNOTSUPP;
29542d1e697eSMoses Reuben 
2955802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2956802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.mask.spi));
2957802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2958802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.val.spi));
2959026bae0cSMaor Gottlieb 		break;
2960038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2961c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2962c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
29631ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2964038d2ef8SMaor Gottlieb 
29656113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
29666113cc44SMaor Gottlieb 			return -EINVAL;
2967038d2ef8SMaor Gottlieb 
29682d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2969038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
29702d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2971038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2972038d2ef8SMaor Gottlieb 
29732d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2974038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
29752d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2976038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2977038d2ef8SMaor Gottlieb 		break;
2978038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2979c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2980c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
29811ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2982038d2ef8SMaor Gottlieb 
29836113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
29846113cc44SMaor Gottlieb 			return -EINVAL;
2985038d2ef8SMaor Gottlieb 
29862d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2987038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
29882d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2989038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2990038d2ef8SMaor Gottlieb 
29912d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2992038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
29932d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2994038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2995038d2ef8SMaor Gottlieb 		break;
2996da2f22aeSAriel Levkovich 	case IB_FLOW_SPEC_GRE:
2997da2f22aeSAriel Levkovich 		if (ib_spec->gre.mask.c_ks_res0_ver)
2998da2f22aeSAriel Levkovich 			return -EOPNOTSUPP;
2999da2f22aeSAriel Levkovich 
30006113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
30016113cc44SMaor Gottlieb 			return -EINVAL;
30026113cc44SMaor Gottlieb 
3003da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
3004da2f22aeSAriel Levkovich 			 0xff);
3005da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
3006da2f22aeSAriel Levkovich 			 IPPROTO_GRE);
3007da2f22aeSAriel Levkovich 
3008da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
3009a93b632cSMaor Gottlieb 			 ntohs(ib_spec->gre.mask.protocol));
3010da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
3011da2f22aeSAriel Levkovich 			 ntohs(ib_spec->gre.val.protocol));
3012da2f22aeSAriel Levkovich 
3013da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
30145886a96aSOz Shlomo 				    gre_key.nvgre.hi),
3015da2f22aeSAriel Levkovich 		       &ib_spec->gre.mask.key,
3016da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.mask.key));
3017da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
30185886a96aSOz Shlomo 				    gre_key.nvgre.hi),
3019da2f22aeSAriel Levkovich 		       &ib_spec->gre.val.key,
3020da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.val.key));
3021da2f22aeSAriel Levkovich 		break;
302271c6e863SAriel Levkovich 	case IB_FLOW_SPEC_MPLS:
302371c6e863SAriel Levkovich 		switch (prev_type) {
302471c6e863SAriel Levkovich 		case IB_FLOW_SPEC_UDP:
302571c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
302671c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_udp),
302771c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
302871c6e863SAriel Levkovich 				return -EOPNOTSUPP;
302971c6e863SAriel Levkovich 
303071c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
303171c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
303271c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
303371c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
303471c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
303571c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
303671c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
303771c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
303871c6e863SAriel Levkovich 			break;
303971c6e863SAriel Levkovich 		case IB_FLOW_SPEC_GRE:
304071c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
304171c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_gre),
304271c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
304371c6e863SAriel Levkovich 				return -EOPNOTSUPP;
304471c6e863SAriel Levkovich 
304571c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
304671c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
304771c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
304871c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
304971c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
305071c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
305171c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
305271c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
305371c6e863SAriel Levkovich 			break;
305471c6e863SAriel Levkovich 		default:
305571c6e863SAriel Levkovich 			if (ib_spec->type & IB_FLOW_SPEC_INNER) {
305671c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
305771c6e863SAriel Levkovich 							   ft_field_support.inner_first_mpls),
305871c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
305971c6e863SAriel Levkovich 					return -EOPNOTSUPP;
306071c6e863SAriel Levkovich 
306171c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
306271c6e863SAriel Levkovich 						    inner_first_mpls),
306371c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
306471c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
306571c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
306671c6e863SAriel Levkovich 						    inner_first_mpls),
306771c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
306871c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
306971c6e863SAriel Levkovich 			} else {
307071c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
307171c6e863SAriel Levkovich 							   ft_field_support.outer_first_mpls),
307271c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
307371c6e863SAriel Levkovich 					return -EOPNOTSUPP;
307471c6e863SAriel Levkovich 
307571c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
307671c6e863SAriel Levkovich 						    outer_first_mpls),
307771c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
307871c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
307971c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
308071c6e863SAriel Levkovich 						    outer_first_mpls),
308171c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
308271c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
308371c6e863SAriel Levkovich 			}
308471c6e863SAriel Levkovich 		}
308571c6e863SAriel Levkovich 		break;
3086ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
3087ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
3088ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
30891ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
3090ffb30d8fSMoses Reuben 
3091ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
3092ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
3093ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
3094ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
3095ffb30d8fSMoses Reuben 		break;
30962ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
30972ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
30982ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
30992ac693f9SMoses Reuben 			return -EOPNOTSUPP;
31002ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
31012ac693f9SMoses Reuben 			return -EINVAL;
31022ac693f9SMoses Reuben 
3103bb0ee7dcSJianbo Liu 		flow_context->flow_tag = ib_spec->flow_tag.tag_id;
3104bb0ee7dcSJianbo Liu 		flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
31052ac693f9SMoses Reuben 		break;
3106a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
3107a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
3108a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
3109a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
3110075572d4SBoris Pismenny 		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
3111a22ed86cSSlava Shwartsman 		break;
3112802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ACTION_HANDLE:
31132ea26203SMark Bloch 		ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
31142ea26203SMark Bloch 			flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
3115802c2125SAviad Yehezkel 		if (ret)
3116802c2125SAviad Yehezkel 			return ret;
3117802c2125SAviad Yehezkel 		break;
31183b3233fbSRaed Salem 	case IB_FLOW_SPEC_ACTION_COUNT:
31193b3233fbSRaed Salem 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
31203b3233fbSRaed Salem 					 LAST_COUNTERS_FIELD))
31213b3233fbSRaed Salem 			return -EOPNOTSUPP;
31223b3233fbSRaed Salem 
31233b3233fbSRaed Salem 		/* for now support only one counters spec per flow */
31243b3233fbSRaed Salem 		if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
31253b3233fbSRaed Salem 			return -EINVAL;
31263b3233fbSRaed Salem 
31273b3233fbSRaed Salem 		action->counters = ib_spec->flow_count.counters;
31283b3233fbSRaed Salem 		action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
31293b3233fbSRaed Salem 		break;
3130038d2ef8SMaor Gottlieb 	default:
3131038d2ef8SMaor Gottlieb 		return -EINVAL;
3132038d2ef8SMaor Gottlieb 	}
3133038d2ef8SMaor Gottlieb 
3134038d2ef8SMaor Gottlieb 	return 0;
3135038d2ef8SMaor Gottlieb }
3136038d2ef8SMaor Gottlieb 
3137038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
3138038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
3139038d2ef8SMaor Gottlieb  * could steal other multicast packets.
3140038d2ef8SMaor Gottlieb  */
3141a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
3142038d2ef8SMaor Gottlieb {
314381e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
3144038d2ef8SMaor Gottlieb 
3145038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
3146038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
3147038d2ef8SMaor Gottlieb 		return false;
3148038d2ef8SMaor Gottlieb 
314981e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
315081e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
315181e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
3152038d2ef8SMaor Gottlieb 
315381e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
315481e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
315581e30880SYishai Hadas 			return true;
315681e30880SYishai Hadas 
315781e30880SYishai Hadas 		return false;
315881e30880SYishai Hadas 	}
315981e30880SYishai Hadas 
316081e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
316181e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
316281e30880SYishai Hadas 
316381e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
3164038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
3165038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
3166038d2ef8SMaor Gottlieb 	}
3167038d2ef8SMaor Gottlieb 
316881e30880SYishai Hadas 	return false;
316981e30880SYishai Hadas }
317081e30880SYishai Hadas 
3171802c2125SAviad Yehezkel enum valid_spec {
3172802c2125SAviad Yehezkel 	VALID_SPEC_INVALID,
3173802c2125SAviad Yehezkel 	VALID_SPEC_VALID,
3174802c2125SAviad Yehezkel 	VALID_SPEC_NA,
3175802c2125SAviad Yehezkel };
3176802c2125SAviad Yehezkel 
3177802c2125SAviad Yehezkel static enum valid_spec
3178802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
3179802c2125SAviad Yehezkel 		     const struct mlx5_flow_spec *spec,
3180802c2125SAviad Yehezkel 		     const struct mlx5_flow_act *flow_act,
3181802c2125SAviad Yehezkel 		     bool egress)
3182802c2125SAviad Yehezkel {
3183802c2125SAviad Yehezkel 	const u32 *match_c = spec->match_criteria;
3184802c2125SAviad Yehezkel 	bool is_crypto =
3185802c2125SAviad Yehezkel 		(flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
3186802c2125SAviad Yehezkel 				     MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
3187802c2125SAviad Yehezkel 	bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
3188802c2125SAviad Yehezkel 	bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
3189802c2125SAviad Yehezkel 
3190802c2125SAviad Yehezkel 	/*
3191802c2125SAviad Yehezkel 	 * Currently only crypto is supported in egress, when regular egress
3192802c2125SAviad Yehezkel 	 * rules would be supported, always return VALID_SPEC_NA.
3193802c2125SAviad Yehezkel 	 */
3194802c2125SAviad Yehezkel 	if (!is_crypto)
319578dd0c43SMark Bloch 		return VALID_SPEC_NA;
3196802c2125SAviad Yehezkel 
3197802c2125SAviad Yehezkel 	return is_crypto && is_ipsec &&
3198bb0ee7dcSJianbo Liu 		(!egress || (!is_drop &&
3199bb0ee7dcSJianbo Liu 			     !(spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG))) ?
3200802c2125SAviad Yehezkel 		VALID_SPEC_VALID : VALID_SPEC_INVALID;
3201802c2125SAviad Yehezkel }
3202802c2125SAviad Yehezkel 
3203802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev,
3204802c2125SAviad Yehezkel 			  const struct mlx5_flow_spec *spec,
3205802c2125SAviad Yehezkel 			  const struct mlx5_flow_act *flow_act,
3206802c2125SAviad Yehezkel 			  bool egress)
3207802c2125SAviad Yehezkel {
3208802c2125SAviad Yehezkel 	/* We curretly only support ipsec egress flow */
3209802c2125SAviad Yehezkel 	return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
3210802c2125SAviad Yehezkel }
3211802c2125SAviad Yehezkel 
321219cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
321319cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
32140f750966SAriel Levkovich 			       bool check_inner)
3215038d2ef8SMaor Gottlieb {
3216038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
321719cc7524SAriel Levkovich 	int match_ipv = check_inner ?
321819cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
321919cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
322019cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
322119cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
32220f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
32230f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
32240f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
32250f750966SAriel Levkovich 	bool has_ethertype = false;
3226038d2ef8SMaor Gottlieb 	unsigned int spec_index;
32270f750966SAriel Levkovich 	bool mask_valid = true;
32280f750966SAriel Levkovich 	u16 eth_type = 0;
32290f750966SAriel Levkovich 	bool type_valid;
3230038d2ef8SMaor Gottlieb 
3231038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
3232038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
32330f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
3234038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
32350f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
32360f750966SAriel Levkovich 				      htons(0xffff));
32370f750966SAriel Levkovich 			has_ethertype = true;
32380f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
32390f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
32400f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
32410f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
3242038d2ef8SMaor Gottlieb 		}
3243038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
3244038d2ef8SMaor Gottlieb 	}
32450f750966SAriel Levkovich 
32460f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
32470f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
32480f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
32490f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
32500f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
32510f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
325219cc7524SAriel Levkovich 
325319cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
325419cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
325519cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
32560f750966SAriel Levkovich 	}
32570f750966SAriel Levkovich 
32580f750966SAriel Levkovich 	return type_valid;
32590f750966SAriel Levkovich }
32600f750966SAriel Levkovich 
326119cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
326219cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
32630f750966SAriel Levkovich {
326419cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
326519cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
3266038d2ef8SMaor Gottlieb }
3267038d2ef8SMaor Gottlieb 
3268038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
3269038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
3270038d2ef8SMaor Gottlieb {
3271038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
3272038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
3273038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
3274038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
3275038d2ef8SMaor Gottlieb 	}
3276038d2ef8SMaor Gottlieb }
3277038d2ef8SMaor Gottlieb 
32783b3233fbSRaed Salem static void counters_clear_description(struct ib_counters *counters)
32793b3233fbSRaed Salem {
32803b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
32813b3233fbSRaed Salem 
32823b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
32833b3233fbSRaed Salem 	kfree(mcounters->counters_data);
32843b3233fbSRaed Salem 	mcounters->counters_data = NULL;
32853b3233fbSRaed Salem 	mcounters->cntrs_max_index = 0;
32863b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
32873b3233fbSRaed Salem }
32883b3233fbSRaed Salem 
3289038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3290038d2ef8SMaor Gottlieb {
3291038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3292038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
3293038d2ef8SMaor Gottlieb 							  ibflow);
3294038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
3295d4be3f44SYishai Hadas 	struct mlx5_ib_dev *dev = handler->dev;
3296038d2ef8SMaor Gottlieb 
32979a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3298038d2ef8SMaor Gottlieb 
3299038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
330074491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
3301cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
3302038d2ef8SMaor Gottlieb 		list_del(&iter->list);
3303038d2ef8SMaor Gottlieb 		kfree(iter);
3304038d2ef8SMaor Gottlieb 	}
3305038d2ef8SMaor Gottlieb 
330674491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
33075497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
33083b3233fbSRaed Salem 	if (handler->ibcounters &&
33093b3233fbSRaed Salem 	    atomic_read(&handler->ibcounters->usecnt) == 1)
33103b3233fbSRaed Salem 		counters_clear_description(handler->ibcounters);
3311038d2ef8SMaor Gottlieb 
33123b3233fbSRaed Salem 	mutex_unlock(&dev->flow_db->lock);
3313d4be3f44SYishai Hadas 	if (handler->flow_matcher)
3314d4be3f44SYishai Hadas 		atomic_dec(&handler->flow_matcher->usecnt);
3315038d2ef8SMaor Gottlieb 	kfree(handler);
3316038d2ef8SMaor Gottlieb 
3317038d2ef8SMaor Gottlieb 	return 0;
3318038d2ef8SMaor Gottlieb }
3319038d2ef8SMaor Gottlieb 
332035d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
332135d19011SMaor Gottlieb {
332235d19011SMaor Gottlieb 	priority *= 2;
332335d19011SMaor Gottlieb 	if (!dont_trap)
332435d19011SMaor Gottlieb 		priority++;
332535d19011SMaor Gottlieb 	return priority;
332635d19011SMaor Gottlieb }
332735d19011SMaor Gottlieb 
3328cc0e5d42SMaor Gottlieb enum flow_table_type {
3329cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
3330cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
3331cc0e5d42SMaor Gottlieb };
3332cc0e5d42SMaor Gottlieb 
333300b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
333400b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
3335d4be3f44SYishai Hadas 
3336d4be3f44SYishai Hadas static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3337d4be3f44SYishai Hadas 					   struct mlx5_ib_flow_prio *prio,
3338d4be3f44SYishai Hadas 					   int priority,
33394adda112SMark Bloch 					   int num_entries, int num_groups,
33404adda112SMark Bloch 					   u32 flags)
3341d4be3f44SYishai Hadas {
334261dc7b01SPaul Blakey 	struct mlx5_flow_table_attr ft_attr = {};
3343d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft;
3344d4be3f44SYishai Hadas 
334561dc7b01SPaul Blakey 	ft_attr.prio = priority;
334661dc7b01SPaul Blakey 	ft_attr.max_fte = num_entries;
334761dc7b01SPaul Blakey 	ft_attr.flags = flags;
334861dc7b01SPaul Blakey 	ft_attr.autogroup.max_num_groups = num_groups;
334961dc7b01SPaul Blakey 	ft = mlx5_create_auto_grouped_flow_table(ns, &ft_attr);
3350d4be3f44SYishai Hadas 	if (IS_ERR(ft))
3351d4be3f44SYishai Hadas 		return ERR_CAST(ft);
3352d4be3f44SYishai Hadas 
3353d4be3f44SYishai Hadas 	prio->flow_table = ft;
3354d4be3f44SYishai Hadas 	prio->refcount = 0;
3355d4be3f44SYishai Hadas 	return prio;
3356d4be3f44SYishai Hadas }
3357d4be3f44SYishai Hadas 
3358038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
3359cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
3360cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
3361038d2ef8SMaor Gottlieb {
336235d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
3363038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
3364038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
3365038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
3366dac388efSMaor Gottlieb 	int max_table_size;
3367038d2ef8SMaor Gottlieb 	int num_entries;
3368038d2ef8SMaor Gottlieb 	int num_groups;
3369cecae747SMaor Gottlieb 	bool esw_encap;
33704adda112SMark Bloch 	u32 flags = 0;
3371038d2ef8SMaor Gottlieb 	int priority;
3372038d2ef8SMaor Gottlieb 
3373dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3374dac388efSMaor Gottlieb 						       log_max_ft_size));
3375cecae747SMaor Gottlieb 	esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
3376cecae747SMaor Gottlieb 		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
3377038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
337878dd0c43SMark Bloch 		enum mlx5_flow_namespace_type fn_type;
337978dd0c43SMark Bloch 
338078dd0c43SMark Bloch 		if (flow_is_multicast_only(flow_attr) &&
338135d19011SMaor Gottlieb 		    !dont_trap)
3382038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
3383038d2ef8SMaor Gottlieb 		else
338435d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
338535d19011SMaor Gottlieb 							dont_trap);
338678dd0c43SMark Bloch 		if (ft_type == MLX5_IB_FT_RX) {
338778dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
338878dd0c43SMark Bloch 			prio = &dev->flow_db->prios[priority];
3389cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
33904adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
33914adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3392cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
33935c2db53fSMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
33945c2db53fSMark Bloch 					reformat_l3_tunnel_to_l2))
33955c2db53fSMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
339678dd0c43SMark Bloch 		} else {
339778dd0c43SMark Bloch 			max_table_size =
339878dd0c43SMark Bloch 				BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
339978dd0c43SMark Bloch 							      log_max_ft_size));
340078dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
340178dd0c43SMark Bloch 			prio = &dev->flow_db->egress_prios[priority];
3402cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
34034adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
34044adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
340578dd0c43SMark Bloch 		}
340678dd0c43SMark Bloch 		ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
3407038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
3408038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
3409038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3410038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3411038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
3412038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
3413038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
3414038d2ef8SMaor Gottlieb 					 &num_entries,
3415038d2ef8SMaor Gottlieb 					 &num_groups);
34169a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
3417cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3418cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3419cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
3420cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
3421cc0e5d42SMaor Gottlieb 
3422cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3423cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3424cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3425cc0e5d42SMaor Gottlieb 
34269a4ca38dSMark Bloch 		prio = &dev->flow_db->sniffer[ft_type];
3427cc0e5d42SMaor Gottlieb 		priority = 0;
3428cc0e5d42SMaor Gottlieb 		num_entries = 1;
3429cc0e5d42SMaor Gottlieb 		num_groups = 1;
3430038d2ef8SMaor Gottlieb 	}
3431038d2ef8SMaor Gottlieb 
3432038d2ef8SMaor Gottlieb 	if (!ns)
3433038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
3434038d2ef8SMaor Gottlieb 
34353b70508aSMark Bloch 	max_table_size = min_t(int, num_entries, max_table_size);
3436dac388efSMaor Gottlieb 
3437038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
3438d4be3f44SYishai Hadas 	if (!ft)
34393b70508aSMark Bloch 		return _get_prio(ns, prio, priority, max_table_size, num_groups,
34404adda112SMark Bloch 				 flags);
3441038d2ef8SMaor Gottlieb 
3442d4be3f44SYishai Hadas 	return prio;
3443038d2ef8SMaor Gottlieb }
3444038d2ef8SMaor Gottlieb 
3445a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
3446a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
3447a550ddfcSYishai Hadas 			    u32 underlay_qpn)
3448a550ddfcSYishai Hadas {
3449a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3450a550ddfcSYishai Hadas 					   spec->match_criteria,
3451a550ddfcSYishai Hadas 					   misc_parameters);
3452a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3453a550ddfcSYishai Hadas 					   misc_parameters);
3454a550ddfcSYishai Hadas 
3455a550ddfcSYishai Hadas 	if (underlay_qpn &&
3456a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3457a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
3458a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3459a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
3460a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3461a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
3462a550ddfcSYishai Hadas 	}
3463a550ddfcSYishai Hadas }
3464a550ddfcSYishai Hadas 
34655e95af5fSRaed Salem static int read_flow_counters(struct ib_device *ibdev,
34665e95af5fSRaed Salem 			      struct mlx5_read_counters_attr *read_attr)
34675e95af5fSRaed Salem {
34685e95af5fSRaed Salem 	struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
34695e95af5fSRaed Salem 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
34705e95af5fSRaed Salem 
34715e95af5fSRaed Salem 	return mlx5_fc_query(dev->mdev, fc,
34725e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_PACKETS],
34735e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_BYTES]);
34745e95af5fSRaed Salem }
34755e95af5fSRaed Salem 
34765e95af5fSRaed Salem /* flow counters currently expose two counters packets and bytes */
34775e95af5fSRaed Salem #define FLOW_COUNTERS_NUM 2
34783b3233fbSRaed Salem static int counters_set_description(struct ib_counters *counters,
34793b3233fbSRaed Salem 				    enum mlx5_ib_counters_type counters_type,
34803b3233fbSRaed Salem 				    struct mlx5_ib_flow_counters_desc *desc_data,
34813b3233fbSRaed Salem 				    u32 ncounters)
34823b3233fbSRaed Salem {
34833b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
34843b3233fbSRaed Salem 	u32 cntrs_max_index = 0;
34853b3233fbSRaed Salem 	int i;
34863b3233fbSRaed Salem 
34873b3233fbSRaed Salem 	if (counters_type != MLX5_IB_COUNTERS_FLOW)
34883b3233fbSRaed Salem 		return -EINVAL;
34893b3233fbSRaed Salem 
34903b3233fbSRaed Salem 	/* init the fields for the object */
34913b3233fbSRaed Salem 	mcounters->type = counters_type;
34925e95af5fSRaed Salem 	mcounters->read_counters = read_flow_counters;
34935e95af5fSRaed Salem 	mcounters->counters_num = FLOW_COUNTERS_NUM;
34943b3233fbSRaed Salem 	mcounters->ncounters = ncounters;
34953b3233fbSRaed Salem 	/* each counter entry have both description and index pair */
34963b3233fbSRaed Salem 	for (i = 0; i < ncounters; i++) {
34973b3233fbSRaed Salem 		if (desc_data[i].description > IB_COUNTER_BYTES)
34983b3233fbSRaed Salem 			return -EINVAL;
34993b3233fbSRaed Salem 
35003b3233fbSRaed Salem 		if (cntrs_max_index <= desc_data[i].index)
35013b3233fbSRaed Salem 			cntrs_max_index = desc_data[i].index + 1;
35023b3233fbSRaed Salem 	}
35033b3233fbSRaed Salem 
35043b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
35053b3233fbSRaed Salem 	mcounters->counters_data = desc_data;
35063b3233fbSRaed Salem 	mcounters->cntrs_max_index = cntrs_max_index;
35073b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
35083b3233fbSRaed Salem 
35093b3233fbSRaed Salem 	return 0;
35103b3233fbSRaed Salem }
35113b3233fbSRaed Salem 
35123b3233fbSRaed Salem #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
35133b3233fbSRaed Salem static int flow_counters_set_data(struct ib_counters *ibcounters,
35143b3233fbSRaed Salem 				  struct mlx5_ib_create_flow *ucmd)
35153b3233fbSRaed Salem {
35163b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
35173b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
35183b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc_data = NULL;
35193b3233fbSRaed Salem 	bool hw_hndl = false;
35203b3233fbSRaed Salem 	int ret = 0;
35213b3233fbSRaed Salem 
35223b3233fbSRaed Salem 	if (ucmd && ucmd->ncounters_data != 0) {
35233b3233fbSRaed Salem 		cntrs_data = ucmd->data;
35243b3233fbSRaed Salem 		if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
35253b3233fbSRaed Salem 			return -EINVAL;
35263b3233fbSRaed Salem 
35273b3233fbSRaed Salem 		desc_data = kcalloc(cntrs_data->ncounters,
35283b3233fbSRaed Salem 				    sizeof(*desc_data),
35293b3233fbSRaed Salem 				    GFP_KERNEL);
35303b3233fbSRaed Salem 		if (!desc_data)
35313b3233fbSRaed Salem 			return  -ENOMEM;
35323b3233fbSRaed Salem 
35333b3233fbSRaed Salem 		if (copy_from_user(desc_data,
35343b3233fbSRaed Salem 				   u64_to_user_ptr(cntrs_data->counters_data),
35353b3233fbSRaed Salem 				   sizeof(*desc_data) * cntrs_data->ncounters)) {
35363b3233fbSRaed Salem 			ret = -EFAULT;
35373b3233fbSRaed Salem 			goto free;
35383b3233fbSRaed Salem 		}
35393b3233fbSRaed Salem 	}
35403b3233fbSRaed Salem 
35413b3233fbSRaed Salem 	if (!mcounters->hw_cntrs_hndl) {
35423b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = mlx5_fc_create(
35433b3233fbSRaed Salem 			to_mdev(ibcounters->device)->mdev, false);
3544e31abf76Sweiyongjun (A) 		if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3545e31abf76Sweiyongjun (A) 			ret = PTR_ERR(mcounters->hw_cntrs_hndl);
35463b3233fbSRaed Salem 			goto free;
35473b3233fbSRaed Salem 		}
35483b3233fbSRaed Salem 		hw_hndl = true;
35493b3233fbSRaed Salem 	}
35503b3233fbSRaed Salem 
35513b3233fbSRaed Salem 	if (desc_data) {
35523b3233fbSRaed Salem 		/* counters already bound to at least one flow */
35533b3233fbSRaed Salem 		if (mcounters->cntrs_max_index) {
35543b3233fbSRaed Salem 			ret = -EINVAL;
35553b3233fbSRaed Salem 			goto free_hndl;
35563b3233fbSRaed Salem 		}
35573b3233fbSRaed Salem 
35583b3233fbSRaed Salem 		ret = counters_set_description(ibcounters,
35593b3233fbSRaed Salem 					       MLX5_IB_COUNTERS_FLOW,
35603b3233fbSRaed Salem 					       desc_data,
35613b3233fbSRaed Salem 					       cntrs_data->ncounters);
35623b3233fbSRaed Salem 		if (ret)
35633b3233fbSRaed Salem 			goto free_hndl;
35643b3233fbSRaed Salem 
35653b3233fbSRaed Salem 	} else if (!mcounters->cntrs_max_index) {
35663b3233fbSRaed Salem 		/* counters not bound yet, must have udata passed */
35673b3233fbSRaed Salem 		ret = -EINVAL;
35683b3233fbSRaed Salem 		goto free_hndl;
35693b3233fbSRaed Salem 	}
35703b3233fbSRaed Salem 
35713b3233fbSRaed Salem 	return 0;
35723b3233fbSRaed Salem 
35733b3233fbSRaed Salem free_hndl:
35743b3233fbSRaed Salem 	if (hw_hndl) {
35753b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
35763b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
35773b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = NULL;
35783b3233fbSRaed Salem 	}
35793b3233fbSRaed Salem free:
35803b3233fbSRaed Salem 	kfree(desc_data);
35813b3233fbSRaed Salem 	return ret;
35823b3233fbSRaed Salem }
35833b3233fbSRaed Salem 
3584669ff1e3SJianbo Liu static void mlx5_ib_set_rule_source_port(struct mlx5_ib_dev *dev,
3585669ff1e3SJianbo Liu 					 struct mlx5_flow_spec *spec,
3586669ff1e3SJianbo Liu 					 struct mlx5_eswitch_rep *rep)
3587669ff1e3SJianbo Liu {
3588669ff1e3SJianbo Liu 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
3589669ff1e3SJianbo Liu 	void *misc;
3590669ff1e3SJianbo Liu 
3591669ff1e3SJianbo Liu 	if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
3592669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3593669ff1e3SJianbo Liu 				    misc_parameters_2);
3594669ff1e3SJianbo Liu 
3595669ff1e3SJianbo Liu 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
3596669ff1e3SJianbo Liu 			 mlx5_eswitch_get_vport_metadata_for_match(esw,
3597669ff1e3SJianbo Liu 								   rep->vport));
3598669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3599669ff1e3SJianbo Liu 				    misc_parameters_2);
3600669ff1e3SJianbo Liu 
36010f0d3827SPaul Blakey 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
36020f0d3827SPaul Blakey 			 mlx5_eswitch_get_vport_metadata_mask());
3603669ff1e3SJianbo Liu 	} else {
3604669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3605669ff1e3SJianbo Liu 				    misc_parameters);
3606669ff1e3SJianbo Liu 
3607669ff1e3SJianbo Liu 		MLX5_SET(fte_match_set_misc, misc, source_port, rep->vport);
3608669ff1e3SJianbo Liu 
3609669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3610669ff1e3SJianbo Liu 				    misc_parameters);
3611669ff1e3SJianbo Liu 
3612669ff1e3SJianbo Liu 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3613669ff1e3SJianbo Liu 	}
3614669ff1e3SJianbo Liu }
3615669ff1e3SJianbo Liu 
3616a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3617038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
3618dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
3619a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
36203b3233fbSRaed Salem 						      u32 underlay_qpn,
36213b3233fbSRaed Salem 						      struct mlx5_ib_create_flow *ucmd)
3622038d2ef8SMaor Gottlieb {
3623038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
3624038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
3625bb0ee7dcSJianbo Liu 	struct mlx5_flow_act flow_act = {};
3626c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
36273b3233fbSRaed Salem 	struct mlx5_flow_destination dest_arr[2] = {};
36283b3233fbSRaed Salem 	struct mlx5_flow_destination *rule_dst = dest_arr;
3629dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3630038d2ef8SMaor Gottlieb 	unsigned int spec_index;
363171c6e863SAriel Levkovich 	u32 prev_type = 0;
3632038d2ef8SMaor Gottlieb 	int err = 0;
36333b3233fbSRaed Salem 	int dest_num = 0;
3634802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3635038d2ef8SMaor Gottlieb 
363619cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
3637038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3638038d2ef8SMaor Gottlieb 
36396a4d00beSMark Bloch 	if (dev->is_rep && is_egress)
364078dd0c43SMark Bloch 		return ERR_PTR(-EINVAL);
364178dd0c43SMark Bloch 
36421b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3643038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3644c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
3645038d2ef8SMaor Gottlieb 		err = -ENOMEM;
3646038d2ef8SMaor Gottlieb 		goto free;
3647038d2ef8SMaor Gottlieb 	}
3648038d2ef8SMaor Gottlieb 
3649038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
3650038d2ef8SMaor Gottlieb 
3651038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
3652bb0ee7dcSJianbo Liu 		err = parse_flow_attr(dev->mdev, spec,
365371c6e863SAriel Levkovich 				      ib_flow, flow_attr, &flow_act,
365471c6e863SAriel Levkovich 				      prev_type);
3655038d2ef8SMaor Gottlieb 		if (err < 0)
3656038d2ef8SMaor Gottlieb 			goto free;
3657038d2ef8SMaor Gottlieb 
365871c6e863SAriel Levkovich 		prev_type = ((union ib_flow_spec *)ib_flow)->type;
3659038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3660038d2ef8SMaor Gottlieb 	}
3661038d2ef8SMaor Gottlieb 
3662ed9085feSMaor Gottlieb 	if (dst && !(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP)) {
3663ed9085feSMaor Gottlieb 		memcpy(&dest_arr[0], dst, sizeof(*dst));
3664ed9085feSMaor Gottlieb 		dest_num++;
3665ed9085feSMaor Gottlieb 	}
3666ed9085feSMaor Gottlieb 
3667a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
3668a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
3669a550ddfcSYishai Hadas 
36706a4d00beSMark Bloch 	if (dev->is_rep) {
3671669ff1e3SJianbo Liu 		struct mlx5_eswitch_rep *rep;
3672018a94eeSMark Bloch 
3673669ff1e3SJianbo Liu 		rep = dev->port[flow_attr->port - 1].rep;
3674669ff1e3SJianbo Liu 		if (!rep) {
36756a4d00beSMark Bloch 			err = -EINVAL;
36766a4d00beSMark Bloch 			goto free;
36776a4d00beSMark Bloch 		}
3678669ff1e3SJianbo Liu 
3679669ff1e3SJianbo Liu 		mlx5_ib_set_rule_source_port(dev, spec, rep);
3680018a94eeSMark Bloch 	}
3681018a94eeSMark Bloch 
3682466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3683802c2125SAviad Yehezkel 
3684802c2125SAviad Yehezkel 	if (is_egress &&
3685802c2125SAviad Yehezkel 	    !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3686802c2125SAviad Yehezkel 		err = -EINVAL;
3687802c2125SAviad Yehezkel 		goto free;
3688802c2125SAviad Yehezkel 	}
3689802c2125SAviad Yehezkel 
36903b3233fbSRaed Salem 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3691171c7625SMark Bloch 		struct mlx5_ib_mcounters *mcounters;
3692171c7625SMark Bloch 
36933b3233fbSRaed Salem 		err = flow_counters_set_data(flow_act.counters, ucmd);
36943b3233fbSRaed Salem 		if (err)
36953b3233fbSRaed Salem 			goto free;
36963b3233fbSRaed Salem 
3697171c7625SMark Bloch 		mcounters = to_mcounters(flow_act.counters);
36983b3233fbSRaed Salem 		handler->ibcounters = flow_act.counters;
36993b3233fbSRaed Salem 		dest_arr[dest_num].type =
37003b3233fbSRaed Salem 			MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3701171c7625SMark Bloch 		dest_arr[dest_num].counter_id =
3702171c7625SMark Bloch 			mlx5_fc_id(mcounters->hw_cntrs_hndl);
37033b3233fbSRaed Salem 		dest_num++;
37043b3233fbSRaed Salem 	}
37053b3233fbSRaed Salem 
3706075572d4SBoris Pismenny 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
3707ed9085feSMaor Gottlieb 		if (!dest_num)
3708a22ed86cSSlava Shwartsman 			rule_dst = NULL;
3709a22ed86cSSlava Shwartsman 	} else {
371014c129e3SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)
371114c129e3SMaor Gottlieb 			flow_act.action |=
371214c129e3SMaor Gottlieb 				MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3713802c2125SAviad Yehezkel 		if (is_egress)
3714802c2125SAviad Yehezkel 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
371514c129e3SMaor Gottlieb 		else if (dest_num)
371614c129e3SMaor Gottlieb 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3717a22ed86cSSlava Shwartsman 	}
37182ac693f9SMoses Reuben 
3719bb0ee7dcSJianbo Liu 	if ((spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG)  &&
37202ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
37212ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
37222ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3723bb0ee7dcSJianbo Liu 			     spec->flow_context.flow_tag, flow_attr->type);
37242ac693f9SMoses Reuben 		err = -EINVAL;
37252ac693f9SMoses Reuben 		goto free;
37262ac693f9SMoses Reuben 	}
372774491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
372866958ed9SHadar Hen Zion 					    &flow_act,
3729a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
3730038d2ef8SMaor Gottlieb 
3731038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
3732038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
3733038d2ef8SMaor Gottlieb 		goto free;
3734038d2ef8SMaor Gottlieb 	}
3735038d2ef8SMaor Gottlieb 
3736d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
37375497adc6SMaor Gottlieb 	handler->prio = ft_prio;
3738d4be3f44SYishai Hadas 	handler->dev = dev;
3739038d2ef8SMaor Gottlieb 
3740038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
3741038d2ef8SMaor Gottlieb free:
37423b3233fbSRaed Salem 	if (err && handler) {
37433b3233fbSRaed Salem 		if (handler->ibcounters &&
37443b3233fbSRaed Salem 		    atomic_read(&handler->ibcounters->usecnt) == 1)
37453b3233fbSRaed Salem 			counters_clear_description(handler->ibcounters);
3746038d2ef8SMaor Gottlieb 		kfree(handler);
37473b3233fbSRaed Salem 	}
3748c5bb1730SMaor Gottlieb 	kvfree(spec);
3749038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
3750038d2ef8SMaor Gottlieb }
3751038d2ef8SMaor Gottlieb 
3752a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3753a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
3754a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
3755a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
3756a550ddfcSYishai Hadas {
37573b3233fbSRaed Salem 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
3758a550ddfcSYishai Hadas }
3759a550ddfcSYishai Hadas 
3760038d2ef8SMaor Gottlieb enum {
3761038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
3762038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
3763038d2ef8SMaor Gottlieb };
3764038d2ef8SMaor Gottlieb 
3765038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3766038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
3767038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
3768038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
3769038d2ef8SMaor Gottlieb {
3770038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
3771038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3772038d2ef8SMaor Gottlieb 
3773038d2ef8SMaor Gottlieb 	static struct {
3774038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
3775038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
3776038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
3777038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
3778038d2ef8SMaor Gottlieb 			.flow_attr = {
3779038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3780038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3781038d2ef8SMaor Gottlieb 			},
3782038d2ef8SMaor Gottlieb 			.eth_flow = {
3783038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3784038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3785038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3786038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
3787038d2ef8SMaor Gottlieb 			}
3788038d2ef8SMaor Gottlieb 		},
3789038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
3790038d2ef8SMaor Gottlieb 			.flow_attr = {
3791038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3792038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3793038d2ef8SMaor Gottlieb 			},
3794038d2ef8SMaor Gottlieb 			.eth_flow = {
3795038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3796038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3797038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3798038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
3799038d2ef8SMaor Gottlieb 			}
3800038d2ef8SMaor Gottlieb 		}
3801038d2ef8SMaor Gottlieb 	};
3802038d2ef8SMaor Gottlieb 
3803038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
3804038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
3805038d2ef8SMaor Gottlieb 				   dst);
3806038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
3807038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3808038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
3809038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3810038d2ef8SMaor Gottlieb 						 dst);
3811038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
381274491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3813d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
3814038d2ef8SMaor Gottlieb 			kfree(handler);
3815038d2ef8SMaor Gottlieb 			handler = handler_ucast;
3816038d2ef8SMaor Gottlieb 		} else {
3817038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
3818038d2ef8SMaor Gottlieb 		}
3819038d2ef8SMaor Gottlieb 	}
3820038d2ef8SMaor Gottlieb 
3821038d2ef8SMaor Gottlieb 	return handler;
3822038d2ef8SMaor Gottlieb }
3823038d2ef8SMaor Gottlieb 
3824cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3825cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
3826cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
3827cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
3828cc0e5d42SMaor Gottlieb {
3829cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
3830cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
3831cc0e5d42SMaor Gottlieb 	int err;
3832cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
3833cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
3834cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
3835cc0e5d42SMaor Gottlieb 	};
3836cc0e5d42SMaor Gottlieb 
3837cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3838cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
3839cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
3840cc0e5d42SMaor Gottlieb 		goto err;
3841cc0e5d42SMaor Gottlieb 	}
3842cc0e5d42SMaor Gottlieb 
3843cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3844cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
3845cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
3846cc0e5d42SMaor Gottlieb 		goto err_tx;
3847cc0e5d42SMaor Gottlieb 	}
3848cc0e5d42SMaor Gottlieb 
3849cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
3850cc0e5d42SMaor Gottlieb 
3851cc0e5d42SMaor Gottlieb 	return handler_rx;
3852cc0e5d42SMaor Gottlieb 
3853cc0e5d42SMaor Gottlieb err_tx:
385474491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
3855cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
3856cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
3857cc0e5d42SMaor Gottlieb err:
3858cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
3859cc0e5d42SMaor Gottlieb }
3860cc0e5d42SMaor Gottlieb 
3861038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3862038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
386359082a32SMatan Barak 					   int domain,
386459082a32SMatan Barak 					   struct ib_udata *udata)
3865038d2ef8SMaor Gottlieb {
3866038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3867d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
3868038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3869038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
3870cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3871038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
3872802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
38733b3233fbSRaed Salem 	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
38743b3233fbSRaed Salem 	size_t min_ucmd_sz, required_ucmd_sz;
3875038d2ef8SMaor Gottlieb 	int err;
3876a550ddfcSYishai Hadas 	int underlay_qpn;
3877038d2ef8SMaor Gottlieb 
38783b3233fbSRaed Salem 	if (udata && udata->inlen) {
38793b3233fbSRaed Salem 		min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
38803b3233fbSRaed Salem 				sizeof(ucmd_hdr.reserved);
38813b3233fbSRaed Salem 		if (udata->inlen < min_ucmd_sz)
388259082a32SMatan Barak 			return ERR_PTR(-EOPNOTSUPP);
388359082a32SMatan Barak 
38843b3233fbSRaed Salem 		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
38853b3233fbSRaed Salem 		if (err)
38863b3233fbSRaed Salem 			return ERR_PTR(err);
38873b3233fbSRaed Salem 
38883b3233fbSRaed Salem 		/* currently supports only one counters data */
38893b3233fbSRaed Salem 		if (ucmd_hdr.ncounters_data > 1)
38903b3233fbSRaed Salem 			return ERR_PTR(-EINVAL);
38913b3233fbSRaed Salem 
38923b3233fbSRaed Salem 		required_ucmd_sz = min_ucmd_sz +
38933b3233fbSRaed Salem 			sizeof(struct mlx5_ib_flow_counters_data) *
38943b3233fbSRaed Salem 			ucmd_hdr.ncounters_data;
38953b3233fbSRaed Salem 		if (udata->inlen > required_ucmd_sz &&
38963b3233fbSRaed Salem 		    !ib_is_udata_cleared(udata, required_ucmd_sz,
38973b3233fbSRaed Salem 					 udata->inlen - required_ucmd_sz))
38983b3233fbSRaed Salem 			return ERR_PTR(-EOPNOTSUPP);
38993b3233fbSRaed Salem 
39003b3233fbSRaed Salem 		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
39013b3233fbSRaed Salem 		if (!ucmd)
39023b3233fbSRaed Salem 			return ERR_PTR(-ENOMEM);
39033b3233fbSRaed Salem 
39043b3233fbSRaed Salem 		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
3905299eafeeSGustavo A. R. Silva 		if (err)
3906299eafeeSGustavo A. R. Silva 			goto free_ucmd;
39073b3233fbSRaed Salem 	}
39083b3233fbSRaed Salem 
3909299eafeeSGustavo A. R. Silva 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3910299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3911299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3912299eafeeSGustavo A. R. Silva 	}
3913038d2ef8SMaor Gottlieb 
3914038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
3915508562d6SDaniel Jurgens 	    flow_attr->port > dev->num_ports ||
3916802c2125SAviad Yehezkel 	    (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3917299eafeeSGustavo A. R. Silva 				  IB_FLOW_ATTR_FLAGS_EGRESS))) {
3918299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3919299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3920299eafeeSGustavo A. R. Silva 	}
3921802c2125SAviad Yehezkel 
3922802c2125SAviad Yehezkel 	if (is_egress &&
3923802c2125SAviad Yehezkel 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3924299eafeeSGustavo A. R. Silva 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3925299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3926299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3927299eafeeSGustavo A. R. Silva 	}
3928038d2ef8SMaor Gottlieb 
3929038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3930299eafeeSGustavo A. R. Silva 	if (!dst) {
3931299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3932299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3933299eafeeSGustavo A. R. Silva 	}
3934038d2ef8SMaor Gottlieb 
39359a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3936038d2ef8SMaor Gottlieb 
3937802c2125SAviad Yehezkel 	ft_prio = get_flow_table(dev, flow_attr,
3938802c2125SAviad Yehezkel 				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3939038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
3940038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
3941038d2ef8SMaor Gottlieb 		goto unlock;
3942038d2ef8SMaor Gottlieb 	}
3943cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3944cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3945cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
3946cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
3947cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
3948cc0e5d42SMaor Gottlieb 			goto destroy_ft;
3949cc0e5d42SMaor Gottlieb 		}
3950cc0e5d42SMaor Gottlieb 	}
3951038d2ef8SMaor Gottlieb 
3952802c2125SAviad Yehezkel 	if (is_egress) {
3953802c2125SAviad Yehezkel 		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3954802c2125SAviad Yehezkel 	} else {
3955038d2ef8SMaor Gottlieb 		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
39562be08c30SLeon Romanovsky 		if (mqp->is_rss)
3957d9f88e5aSYishai Hadas 			dst->tir_num = mqp->rss_qp.tirn;
3958d9f88e5aSYishai Hadas 		else
3959d9f88e5aSYishai Hadas 			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3960802c2125SAviad Yehezkel 	}
3961038d2ef8SMaor Gottlieb 
3962038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
39632be08c30SLeon Romanovsky 		underlay_qpn = (mqp->flags & IB_QP_CREATE_SOURCE_QPN) ?
396414c129e3SMaor Gottlieb 				       mqp->underlay_qpn :
396514c129e3SMaor Gottlieb 				       0;
396614c129e3SMaor Gottlieb 		handler = _create_flow_rule(dev, ft_prio, flow_attr, dst,
396714c129e3SMaor Gottlieb 					    underlay_qpn, ucmd);
3968038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3969038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3970038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3971038d2ef8SMaor Gottlieb 						dst);
3972cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3973cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3974038d2ef8SMaor Gottlieb 	} else {
3975038d2ef8SMaor Gottlieb 		err = -EINVAL;
3976038d2ef8SMaor Gottlieb 		goto destroy_ft;
3977038d2ef8SMaor Gottlieb 	}
3978038d2ef8SMaor Gottlieb 
3979038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
3980038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
3981038d2ef8SMaor Gottlieb 		handler = NULL;
3982038d2ef8SMaor Gottlieb 		goto destroy_ft;
3983038d2ef8SMaor Gottlieb 	}
3984038d2ef8SMaor Gottlieb 
39859a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3986038d2ef8SMaor Gottlieb 	kfree(dst);
39873b3233fbSRaed Salem 	kfree(ucmd);
3988038d2ef8SMaor Gottlieb 
3989038d2ef8SMaor Gottlieb 	return &handler->ibflow;
3990038d2ef8SMaor Gottlieb 
3991038d2ef8SMaor Gottlieb destroy_ft:
3992038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
3993cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
3994cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
3995038d2ef8SMaor Gottlieb unlock:
39969a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3997038d2ef8SMaor Gottlieb 	kfree(dst);
3998299eafeeSGustavo A. R. Silva free_ucmd:
39993b3233fbSRaed Salem 	kfree(ucmd);
4000038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
4001038d2ef8SMaor Gottlieb }
4002038d2ef8SMaor Gottlieb 
4003b47fd4ffSMark Bloch static struct mlx5_ib_flow_prio *
4004b47fd4ffSMark Bloch _get_flow_table(struct mlx5_ib_dev *dev,
4005b47fd4ffSMark Bloch 		struct mlx5_ib_flow_matcher *fs_matcher,
4006b47fd4ffSMark Bloch 		bool mcast)
4007d4be3f44SYishai Hadas {
4008d4be3f44SYishai Hadas 	struct mlx5_flow_namespace *ns = NULL;
400913a43765SMark Bloch 	struct mlx5_ib_flow_prio *prio = NULL;
401013a43765SMark Bloch 	int max_table_size = 0;
4011cecae747SMaor Gottlieb 	bool esw_encap;
4012b47fd4ffSMark Bloch 	u32 flags = 0;
4013b47fd4ffSMark Bloch 	int priority;
4014d4be3f44SYishai Hadas 
401513a43765SMark Bloch 	if (mcast)
401613a43765SMark Bloch 		priority = MLX5_IB_FLOW_MCAST_PRIO;
401713a43765SMark Bloch 	else
401813a43765SMark Bloch 		priority = ib_prio_to_core_prio(fs_matcher->priority, false);
401913a43765SMark Bloch 
4020cecae747SMaor Gottlieb 	esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
4021cecae747SMaor Gottlieb 		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
4022b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
4023d4be3f44SYishai Hadas 		max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
4024d4be3f44SYishai Hadas 					log_max_ft_size));
4025cecae747SMaor Gottlieb 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap) && !esw_encap)
4026b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
4027b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
4028cecae747SMaor Gottlieb 					      reformat_l3_tunnel_to_l2) &&
4029cecae747SMaor Gottlieb 		    !esw_encap)
4030b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
403113a43765SMark Bloch 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS) {
403213a43765SMark Bloch 		max_table_size = BIT(
403313a43765SMark Bloch 			MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, log_max_ft_size));
4034cecae747SMaor Gottlieb 		if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat) && !esw_encap)
4035b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
403613a43765SMark Bloch 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB) {
403713a43765SMark Bloch 		max_table_size = BIT(
403813a43765SMark Bloch 			MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, log_max_ft_size));
403909d985beSMaor Gottlieb 		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, decap) && esw_encap)
404009d985beSMaor Gottlieb 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
404109d985beSMaor Gottlieb 		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, reformat_l3_tunnel_to_l2) &&
404209d985beSMaor Gottlieb 		    esw_encap)
404309d985beSMaor Gottlieb 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
404413a43765SMark Bloch 		priority = FDB_BYPASS_PATH;
4045d8abe884SMark Zhang 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX) {
4046d8abe884SMark Zhang 		max_table_size =
4047d8abe884SMark Zhang 			BIT(MLX5_CAP_FLOWTABLE_RDMA_RX(dev->mdev,
4048d8abe884SMark Zhang 						       log_max_ft_size));
4049d8abe884SMark Zhang 		priority = fs_matcher->priority;
4050af9c3841SMichael Guralnik 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_TX) {
4051af9c3841SMichael Guralnik 		max_table_size =
4052af9c3841SMichael Guralnik 			BIT(MLX5_CAP_FLOWTABLE_RDMA_TX(dev->mdev,
4053af9c3841SMichael Guralnik 						       log_max_ft_size));
4054af9c3841SMichael Guralnik 		priority = fs_matcher->priority;
4055b47fd4ffSMark Bloch 	}
4056b47fd4ffSMark Bloch 
40573b70508aSMark Bloch 	max_table_size = min_t(int, max_table_size, MLX5_FS_MAX_ENTRIES);
4058d4be3f44SYishai Hadas 
4059b47fd4ffSMark Bloch 	ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
4060d4be3f44SYishai Hadas 	if (!ns)
4061d4be3f44SYishai Hadas 		return ERR_PTR(-ENOTSUPP);
4062d4be3f44SYishai Hadas 
4063b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
4064d4be3f44SYishai Hadas 		prio = &dev->flow_db->prios[priority];
406513a43765SMark Bloch 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS)
4066b47fd4ffSMark Bloch 		prio = &dev->flow_db->egress_prios[priority];
406713a43765SMark Bloch 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB)
406813a43765SMark Bloch 		prio = &dev->flow_db->fdb;
4069d8abe884SMark Zhang 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX)
4070d8abe884SMark Zhang 		prio = &dev->flow_db->rdma_rx[priority];
4071af9c3841SMichael Guralnik 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_TX)
4072af9c3841SMichael Guralnik 		prio = &dev->flow_db->rdma_tx[priority];
407313a43765SMark Bloch 
407413a43765SMark Bloch 	if (!prio)
407513a43765SMark Bloch 		return ERR_PTR(-EINVAL);
4076d4be3f44SYishai Hadas 
4077d4be3f44SYishai Hadas 	if (prio->flow_table)
4078d4be3f44SYishai Hadas 		return prio;
4079d4be3f44SYishai Hadas 
40803b70508aSMark Bloch 	return _get_prio(ns, prio, priority, max_table_size,
4081b47fd4ffSMark Bloch 			 MLX5_FS_MAX_TYPES, flags);
4082d4be3f44SYishai Hadas }
4083d4be3f44SYishai Hadas 
4084d4be3f44SYishai Hadas static struct mlx5_ib_flow_handler *
4085d4be3f44SYishai Hadas _create_raw_flow_rule(struct mlx5_ib_dev *dev,
4086d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_prio *ft_prio,
4087d4be3f44SYishai Hadas 		      struct mlx5_flow_destination *dst,
4088d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_matcher  *fs_matcher,
4089bb0ee7dcSJianbo Liu 		      struct mlx5_flow_context *flow_context,
4090b823dd6dSMark Bloch 		      struct mlx5_flow_act *flow_act,
4091bfc5d839SMark Bloch 		      void *cmd_in, int inlen,
4092bfc5d839SMark Bloch 		      int dst_num)
4093d4be3f44SYishai Hadas {
4094d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
4095d4be3f44SYishai Hadas 	struct mlx5_flow_spec *spec;
4096d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft = ft_prio->flow_table;
4097d4be3f44SYishai Hadas 	int err = 0;
4098d4be3f44SYishai Hadas 
4099d4be3f44SYishai Hadas 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
4100d4be3f44SYishai Hadas 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
4101d4be3f44SYishai Hadas 	if (!handler || !spec) {
4102d4be3f44SYishai Hadas 		err = -ENOMEM;
4103d4be3f44SYishai Hadas 		goto free;
4104d4be3f44SYishai Hadas 	}
4105d4be3f44SYishai Hadas 
4106d4be3f44SYishai Hadas 	INIT_LIST_HEAD(&handler->list);
4107d4be3f44SYishai Hadas 
4108d4be3f44SYishai Hadas 	memcpy(spec->match_value, cmd_in, inlen);
4109d4be3f44SYishai Hadas 	memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
4110d4be3f44SYishai Hadas 	       fs_matcher->mask_len);
4111d4be3f44SYishai Hadas 	spec->match_criteria_enable = fs_matcher->match_criteria_enable;
4112bb0ee7dcSJianbo Liu 	spec->flow_context = *flow_context;
4113d4be3f44SYishai Hadas 
4114d4be3f44SYishai Hadas 	handler->rule = mlx5_add_flow_rules(ft, spec,
4115bfc5d839SMark Bloch 					    flow_act, dst, dst_num);
4116d4be3f44SYishai Hadas 
4117d4be3f44SYishai Hadas 	if (IS_ERR(handler->rule)) {
4118d4be3f44SYishai Hadas 		err = PTR_ERR(handler->rule);
4119d4be3f44SYishai Hadas 		goto free;
4120d4be3f44SYishai Hadas 	}
4121d4be3f44SYishai Hadas 
4122d4be3f44SYishai Hadas 	ft_prio->refcount++;
4123d4be3f44SYishai Hadas 	handler->prio = ft_prio;
4124d4be3f44SYishai Hadas 	handler->dev = dev;
4125d4be3f44SYishai Hadas 	ft_prio->flow_table = ft;
4126d4be3f44SYishai Hadas 
4127d4be3f44SYishai Hadas free:
4128d4be3f44SYishai Hadas 	if (err)
4129d4be3f44SYishai Hadas 		kfree(handler);
4130d4be3f44SYishai Hadas 	kvfree(spec);
4131d4be3f44SYishai Hadas 	return err ? ERR_PTR(err) : handler;
4132d4be3f44SYishai Hadas }
4133d4be3f44SYishai Hadas 
4134d4be3f44SYishai Hadas static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
4135d4be3f44SYishai Hadas 				void *match_v)
4136d4be3f44SYishai Hadas {
4137d4be3f44SYishai Hadas 	void *match_c;
4138d4be3f44SYishai Hadas 	void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
4139d4be3f44SYishai Hadas 	void *dmac, *dmac_mask;
4140d4be3f44SYishai Hadas 	void *ipv4, *ipv4_mask;
4141d4be3f44SYishai Hadas 
4142d4be3f44SYishai Hadas 	if (!(fs_matcher->match_criteria_enable &
4143d4be3f44SYishai Hadas 	      (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
4144d4be3f44SYishai Hadas 		return false;
4145d4be3f44SYishai Hadas 
4146d4be3f44SYishai Hadas 	match_c = fs_matcher->matcher_mask.match_params;
4147d4be3f44SYishai Hadas 	match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
4148d4be3f44SYishai Hadas 					   outer_headers);
4149d4be3f44SYishai Hadas 	match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
4150d4be3f44SYishai Hadas 					   outer_headers);
4151d4be3f44SYishai Hadas 
4152d4be3f44SYishai Hadas 	dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4153d4be3f44SYishai Hadas 			    dmac_47_16);
4154d4be3f44SYishai Hadas 	dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4155d4be3f44SYishai Hadas 				 dmac_47_16);
4156d4be3f44SYishai Hadas 
4157d4be3f44SYishai Hadas 	if (is_multicast_ether_addr(dmac) &&
4158d4be3f44SYishai Hadas 	    is_multicast_ether_addr(dmac_mask))
4159d4be3f44SYishai Hadas 		return true;
4160d4be3f44SYishai Hadas 
4161d4be3f44SYishai Hadas 	ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4162d4be3f44SYishai Hadas 			    dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4163d4be3f44SYishai Hadas 
4164d4be3f44SYishai Hadas 	ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4165d4be3f44SYishai Hadas 				 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4166d4be3f44SYishai Hadas 
4167d4be3f44SYishai Hadas 	if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
4168d4be3f44SYishai Hadas 	    ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
4169d4be3f44SYishai Hadas 		return true;
4170d4be3f44SYishai Hadas 
4171d4be3f44SYishai Hadas 	return false;
4172d4be3f44SYishai Hadas }
4173d4be3f44SYishai Hadas 
417432269441SYishai Hadas struct mlx5_ib_flow_handler *
417532269441SYishai Hadas mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
417632269441SYishai Hadas 			struct mlx5_ib_flow_matcher *fs_matcher,
4177bb0ee7dcSJianbo Liu 			struct mlx5_flow_context *flow_context,
4178b823dd6dSMark Bloch 			struct mlx5_flow_act *flow_act,
4179bfc5d839SMark Bloch 			u32 counter_id,
418032269441SYishai Hadas 			void *cmd_in, int inlen, int dest_id,
418132269441SYishai Hadas 			int dest_type)
418232269441SYishai Hadas {
4183d4be3f44SYishai Hadas 	struct mlx5_flow_destination *dst;
4184d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *ft_prio;
4185d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
4186bfc5d839SMark Bloch 	int dst_num = 0;
4187d4be3f44SYishai Hadas 	bool mcast;
4188d4be3f44SYishai Hadas 	int err;
4189d4be3f44SYishai Hadas 
4190d4be3f44SYishai Hadas 	if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
419132269441SYishai Hadas 		return ERR_PTR(-EOPNOTSUPP);
4192d4be3f44SYishai Hadas 
4193d4be3f44SYishai Hadas 	if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
4194d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
4195d4be3f44SYishai Hadas 
41968e8aa145SGustavo A. R. Silva 	dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
4197d4be3f44SYishai Hadas 	if (!dst)
4198d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
4199d4be3f44SYishai Hadas 
4200d4be3f44SYishai Hadas 	mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
4201d4be3f44SYishai Hadas 	mutex_lock(&dev->flow_db->lock);
4202d4be3f44SYishai Hadas 
4203b47fd4ffSMark Bloch 	ft_prio = _get_flow_table(dev, fs_matcher, mcast);
4204d4be3f44SYishai Hadas 	if (IS_ERR(ft_prio)) {
4205d4be3f44SYishai Hadas 		err = PTR_ERR(ft_prio);
4206d4be3f44SYishai Hadas 		goto unlock;
4207d4be3f44SYishai Hadas 	}
4208d4be3f44SYishai Hadas 
42096346f0bfSYishai Hadas 	if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
4210bfc5d839SMark Bloch 		dst[dst_num].type = dest_type;
42118c112a5fSMaor Gottlieb 		dst[dst_num++].tir_num = dest_id;
4212b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
4213a7ee18bdSMark Bloch 	} else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
4214bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
42158c112a5fSMaor Gottlieb 		dst[dst_num++].ft_num = dest_id;
4216b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
42178c112a5fSMaor Gottlieb 	} else  if (dest_type == MLX5_FLOW_DESTINATION_TYPE_PORT) {
42188c112a5fSMaor Gottlieb 		dst[dst_num++].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
4219a7ee18bdSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
42206346f0bfSYishai Hadas 	}
42216346f0bfSYishai Hadas 
4222bfc5d839SMark Bloch 
4223bfc5d839SMark Bloch 	if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
4224bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
4225bfc5d839SMark Bloch 		dst[dst_num].counter_id = counter_id;
4226bfc5d839SMark Bloch 		dst_num++;
4227bfc5d839SMark Bloch 	}
4228bfc5d839SMark Bloch 
4229bb0ee7dcSJianbo Liu 	handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher,
4230bb0ee7dcSJianbo Liu 					flow_context, flow_act,
4231bfc5d839SMark Bloch 					cmd_in, inlen, dst_num);
4232d4be3f44SYishai Hadas 
4233d4be3f44SYishai Hadas 	if (IS_ERR(handler)) {
4234d4be3f44SYishai Hadas 		err = PTR_ERR(handler);
4235d4be3f44SYishai Hadas 		goto destroy_ft;
4236d4be3f44SYishai Hadas 	}
4237d4be3f44SYishai Hadas 
4238d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
4239d4be3f44SYishai Hadas 	atomic_inc(&fs_matcher->usecnt);
4240d4be3f44SYishai Hadas 	handler->flow_matcher = fs_matcher;
4241d4be3f44SYishai Hadas 
4242d4be3f44SYishai Hadas 	kfree(dst);
4243d4be3f44SYishai Hadas 
4244d4be3f44SYishai Hadas 	return handler;
4245d4be3f44SYishai Hadas 
4246d4be3f44SYishai Hadas destroy_ft:
4247d4be3f44SYishai Hadas 	put_flow_table(dev, ft_prio, false);
4248d4be3f44SYishai Hadas unlock:
4249d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
4250d4be3f44SYishai Hadas 	kfree(dst);
4251d4be3f44SYishai Hadas 
4252d4be3f44SYishai Hadas 	return ERR_PTR(err);
425332269441SYishai Hadas }
425432269441SYishai Hadas 
4255c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
4256c6475a0bSAviad Yehezkel {
4257c6475a0bSAviad Yehezkel 	u32 flags = 0;
4258c6475a0bSAviad Yehezkel 
4259c6475a0bSAviad Yehezkel 	if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
4260c6475a0bSAviad Yehezkel 		flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
4261c6475a0bSAviad Yehezkel 
4262c6475a0bSAviad Yehezkel 	return flags;
4263c6475a0bSAviad Yehezkel }
4264c6475a0bSAviad Yehezkel 
4265c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED	MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
4266c6475a0bSAviad Yehezkel static struct ib_flow_action *
4267c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device,
4268c6475a0bSAviad Yehezkel 			       const struct ib_flow_action_attrs_esp *attr,
4269c6475a0bSAviad Yehezkel 			       struct uverbs_attr_bundle *attrs)
4270c6475a0bSAviad Yehezkel {
4271c6475a0bSAviad Yehezkel 	struct mlx5_ib_dev *mdev = to_mdev(device);
4272c6475a0bSAviad Yehezkel 	struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
4273c6475a0bSAviad Yehezkel 	struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
4274c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *action;
4275c6475a0bSAviad Yehezkel 	u64 action_flags;
4276c6475a0bSAviad Yehezkel 	u64 flags;
4277c6475a0bSAviad Yehezkel 	int err = 0;
4278c6475a0bSAviad Yehezkel 
4279bccd0622SJason Gunthorpe 	err = uverbs_get_flags64(
4280bccd0622SJason Gunthorpe 		&action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4281bccd0622SJason Gunthorpe 		((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
4282bccd0622SJason Gunthorpe 	if (err)
4283bccd0622SJason Gunthorpe 		return ERR_PTR(err);
4284c6475a0bSAviad Yehezkel 
4285c6475a0bSAviad Yehezkel 	flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
4286c6475a0bSAviad Yehezkel 
4287c6475a0bSAviad Yehezkel 	/* We current only support a subset of the standard features. Only a
4288c6475a0bSAviad Yehezkel 	 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
4289c6475a0bSAviad Yehezkel 	 * (with overlap). Full offload mode isn't supported.
4290c6475a0bSAviad Yehezkel 	 */
4291c6475a0bSAviad Yehezkel 	if (!attr->keymat || attr->replay || attr->encap ||
4292c6475a0bSAviad Yehezkel 	    attr->spi || attr->seq || attr->tfc_pad ||
4293c6475a0bSAviad Yehezkel 	    attr->hard_limit_pkts ||
4294c6475a0bSAviad Yehezkel 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4295c6475a0bSAviad Yehezkel 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
4296c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4297c6475a0bSAviad Yehezkel 
4298c6475a0bSAviad Yehezkel 	if (attr->keymat->protocol !=
4299c6475a0bSAviad Yehezkel 	    IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
4300c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4301c6475a0bSAviad Yehezkel 
4302c6475a0bSAviad Yehezkel 	aes_gcm = &attr->keymat->keymat.aes_gcm;
4303c6475a0bSAviad Yehezkel 
4304c6475a0bSAviad Yehezkel 	if (aes_gcm->icv_len != 16 ||
4305c6475a0bSAviad Yehezkel 	    aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
4306c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4307c6475a0bSAviad Yehezkel 
4308c6475a0bSAviad Yehezkel 	action = kmalloc(sizeof(*action), GFP_KERNEL);
4309c6475a0bSAviad Yehezkel 	if (!action)
4310c6475a0bSAviad Yehezkel 		return ERR_PTR(-ENOMEM);
4311c6475a0bSAviad Yehezkel 
4312c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4313c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
4314c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.aes_key));
4315c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
4316c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
4317c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.salt));
4318c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
4319c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
4320c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
4321c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
4322c6475a0bSAviad Yehezkel 	accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
4323c6475a0bSAviad Yehezkel 
4324c6475a0bSAviad Yehezkel 	accel_attrs.esn = attr->esn;
4325c6475a0bSAviad Yehezkel 	if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4326c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4327c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4328c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4329c6475a0bSAviad Yehezkel 
4330c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4331c6475a0bSAviad Yehezkel 		accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4332c6475a0bSAviad Yehezkel 
4333c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ctx =
4334c6475a0bSAviad Yehezkel 		mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4335c6475a0bSAviad Yehezkel 	if (IS_ERR(action->esp_aes_gcm.ctx)) {
4336c6475a0bSAviad Yehezkel 		err = PTR_ERR(action->esp_aes_gcm.ctx);
4337c6475a0bSAviad Yehezkel 		goto err_parse;
4338c6475a0bSAviad Yehezkel 	}
4339c6475a0bSAviad Yehezkel 
4340c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4341c6475a0bSAviad Yehezkel 
4342c6475a0bSAviad Yehezkel 	return &action->ib_action;
4343c6475a0bSAviad Yehezkel 
4344c6475a0bSAviad Yehezkel err_parse:
4345c6475a0bSAviad Yehezkel 	kfree(action);
4346c6475a0bSAviad Yehezkel 	return ERR_PTR(err);
4347c6475a0bSAviad Yehezkel }
4348c6475a0bSAviad Yehezkel 
4349349705c1SMatan Barak static int
4350349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4351349705c1SMatan Barak 			       const struct ib_flow_action_attrs_esp *attr,
4352349705c1SMatan Barak 			       struct uverbs_attr_bundle *attrs)
4353349705c1SMatan Barak {
4354349705c1SMatan Barak 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4355349705c1SMatan Barak 	struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4356349705c1SMatan Barak 	int err = 0;
4357349705c1SMatan Barak 
4358349705c1SMatan Barak 	if (attr->keymat || attr->replay || attr->encap ||
4359349705c1SMatan Barak 	    attr->spi || attr->seq || attr->tfc_pad ||
4360349705c1SMatan Barak 	    attr->hard_limit_pkts ||
4361349705c1SMatan Barak 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4362349705c1SMatan Barak 			     IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4363349705c1SMatan Barak 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4364349705c1SMatan Barak 		return -EOPNOTSUPP;
4365349705c1SMatan Barak 
4366349705c1SMatan Barak 	/* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4367349705c1SMatan Barak 	 * be modified.
4368349705c1SMatan Barak 	 */
4369349705c1SMatan Barak 	if (!(maction->esp_aes_gcm.ib_flags &
4370349705c1SMatan Barak 	      IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4371349705c1SMatan Barak 	    attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4372349705c1SMatan Barak 			   IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4373349705c1SMatan Barak 		return -EINVAL;
4374349705c1SMatan Barak 
4375349705c1SMatan Barak 	memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4376349705c1SMatan Barak 	       sizeof(accel_attrs));
4377349705c1SMatan Barak 
4378349705c1SMatan Barak 	accel_attrs.esn = attr->esn;
4379349705c1SMatan Barak 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4380349705c1SMatan Barak 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4381349705c1SMatan Barak 	else
4382349705c1SMatan Barak 		accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4383349705c1SMatan Barak 
4384349705c1SMatan Barak 	err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4385349705c1SMatan Barak 					 &accel_attrs);
4386349705c1SMatan Barak 	if (err)
4387349705c1SMatan Barak 		return err;
4388349705c1SMatan Barak 
4389349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags &=
4390349705c1SMatan Barak 		~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4391349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags |=
4392349705c1SMatan Barak 		attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4393349705c1SMatan Barak 
4394349705c1SMatan Barak 	return 0;
4395349705c1SMatan Barak }
4396349705c1SMatan Barak 
4397c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4398c6475a0bSAviad Yehezkel {
4399c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4400c6475a0bSAviad Yehezkel 
4401c6475a0bSAviad Yehezkel 	switch (action->type) {
4402c6475a0bSAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
4403c6475a0bSAviad Yehezkel 		/*
4404c6475a0bSAviad Yehezkel 		 * We only support aes_gcm by now, so we implicitly know this is
4405c6475a0bSAviad Yehezkel 		 * the underline crypto.
4406c6475a0bSAviad Yehezkel 		 */
4407c6475a0bSAviad Yehezkel 		mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4408c6475a0bSAviad Yehezkel 		break;
4409b4749bf2SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
4410b4749bf2SMark Bloch 		mlx5_ib_destroy_flow_action_raw(maction);
4411b4749bf2SMark Bloch 		break;
4412c6475a0bSAviad Yehezkel 	default:
4413c6475a0bSAviad Yehezkel 		WARN_ON(true);
4414c6475a0bSAviad Yehezkel 		break;
4415c6475a0bSAviad Yehezkel 	}
4416c6475a0bSAviad Yehezkel 
4417c6475a0bSAviad Yehezkel 	kfree(maction);
4418c6475a0bSAviad Yehezkel 	return 0;
4419c6475a0bSAviad Yehezkel }
4420c6475a0bSAviad Yehezkel 
4421e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4422e126ba97SEli Cohen {
4423e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
442481e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
4425e126ba97SEli Cohen 	int err;
4426539ec982SYishai Hadas 	u16 uid;
4427539ec982SYishai Hadas 
4428539ec982SYishai Hadas 	uid = ibqp->pd ?
4429539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4430e126ba97SEli Cohen 
44312be08c30SLeon Romanovsky 	if (mqp->flags & IB_QP_CREATE_SOURCE_QPN) {
443281e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
443381e30880SYishai Hadas 		return -EOPNOTSUPP;
443481e30880SYishai Hadas 	}
443581e30880SYishai Hadas 
4436539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4437e126ba97SEli Cohen 	if (err)
4438e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4439e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4440e126ba97SEli Cohen 
4441e126ba97SEli Cohen 	return err;
4442e126ba97SEli Cohen }
4443e126ba97SEli Cohen 
4444e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4445e126ba97SEli Cohen {
4446e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4447e126ba97SEli Cohen 	int err;
4448539ec982SYishai Hadas 	u16 uid;
4449e126ba97SEli Cohen 
4450539ec982SYishai Hadas 	uid = ibqp->pd ?
4451539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4452539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4453e126ba97SEli Cohen 	if (err)
4454e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4455e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4456e126ba97SEli Cohen 
4457e126ba97SEli Cohen 	return err;
4458e126ba97SEli Cohen }
4459e126ba97SEli Cohen 
4460e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
4461e126ba97SEli Cohen {
44621b5daf11SMajd Dibbiny 	int err;
4463e126ba97SEli Cohen 
44641b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
4465e126ba97SEli Cohen 	if (err)
4466e126ba97SEli Cohen 		return err;
44671b5daf11SMajd Dibbiny 
44681b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
44691b5daf11SMajd Dibbiny 
44701b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
4471e126ba97SEli Cohen }
4472e126ba97SEli Cohen 
4473508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
4474508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4475e126ba97SEli Cohen {
4476e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
447754747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4478e126ba97SEli Cohen 
44799603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
4480e126ba97SEli Cohen }
4481508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
4482e126ba97SEli Cohen 
4483508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
4484e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
4485e126ba97SEli Cohen {
4486e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
448754747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4488e126ba97SEli Cohen 
44896aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
4490e126ba97SEli Cohen }
4491508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
4492e126ba97SEli Cohen 
4493508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
4494508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4495e126ba97SEli Cohen {
4496e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
449754747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
449854747231SParav Pandit 
44999603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
4500e126ba97SEli Cohen }
4501508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
4502e126ba97SEli Cohen 
4503508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
4504508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
4505e126ba97SEli Cohen {
4506e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
450754747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
450854747231SParav Pandit 
45099603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
4510e126ba97SEli Cohen }
4511508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
4512e126ba97SEli Cohen 
4513508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
4514508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4515e126ba97SEli Cohen {
4516e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
451754747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
451854747231SParav Pandit 
4519e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
45209603b61dSJack Morgenstein 		       dev->mdev->board_id);
4521e126ba97SEli Cohen }
4522508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
4523e126ba97SEli Cohen 
4524508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
4525508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
4526508a523fSParav Pandit 	&dev_attr_hca_type.attr,
4527508a523fSParav Pandit 	&dev_attr_board_id.attr,
4528508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
4529508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
4530508a523fSParav Pandit 	NULL,
4531508a523fSParav Pandit };
4532e126ba97SEli Cohen 
4533508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
4534508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
4535e126ba97SEli Cohen };
4536e126ba97SEli Cohen 
45377722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
45387722f47eSHaggai Eran {
45397722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
45407722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
45417722f47eSHaggai Eran 			     pkey_change_work);
45427722f47eSHaggai Eran 
45437722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
45447722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
45457722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
45467722f47eSHaggai Eran }
45477722f47eSHaggai Eran 
454889ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
454989ea94a7SMaor Gottlieb {
455089ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
455189ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
455289ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
455389ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
455489ea94a7SMaor Gottlieb 	unsigned long flags_qp;
455589ea94a7SMaor Gottlieb 	unsigned long flags_cq;
455689ea94a7SMaor Gottlieb 	unsigned long flags;
455789ea94a7SMaor Gottlieb 
455889ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
455989ea94a7SMaor Gottlieb 
456089ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
456189ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
456289ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
456389ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
456489ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
456589ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
456689ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
456789ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
456889ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
456989ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
457089ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
457189ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
457289ea94a7SMaor Gottlieb 						      &cq_armed_list);
457389ea94a7SMaor Gottlieb 				}
457489ea94a7SMaor Gottlieb 			}
457589ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
457689ea94a7SMaor Gottlieb 		}
457789ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
457889ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
457989ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
458089ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
458189ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
458289ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
458389ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
458489ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
458589ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
458689ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
458789ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
458889ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
458989ea94a7SMaor Gottlieb 							      &cq_armed_list);
459089ea94a7SMaor Gottlieb 					}
459189ea94a7SMaor Gottlieb 				}
459289ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
459389ea94a7SMaor Gottlieb 						       flags_cq);
459489ea94a7SMaor Gottlieb 			}
459589ea94a7SMaor Gottlieb 		}
459689ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
459789ea94a7SMaor Gottlieb 	}
459889ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
459989ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
460089ea94a7SMaor Gottlieb 	 */
460189ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
46024e0e2ea1SYishai Hadas 		mcq->comp(mcq, NULL);
460389ea94a7SMaor Gottlieb 	}
460489ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
460589ea94a7SMaor Gottlieb }
460689ea94a7SMaor Gottlieb 
460703404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
460803404e8aSMaor Gottlieb {
460903404e8aSMaor Gottlieb 	int err;
461003404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
461103404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
461203404e8aSMaor Gottlieb 			     delay_drop_work);
461303404e8aSMaor Gottlieb 
4614fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
4615fe248c3aSMaor Gottlieb 
461603404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
4617333fbaa0SLeon Romanovsky 	err = mlx5_core_set_delay_drop(delay_drop->dev, delay_drop->timeout);
461803404e8aSMaor Gottlieb 	if (err) {
461903404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
462003404e8aSMaor Gottlieb 			     delay_drop->timeout);
462103404e8aSMaor Gottlieb 		delay_drop->activate = false;
462203404e8aSMaor Gottlieb 	}
462303404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
462403404e8aSMaor Gottlieb }
462503404e8aSMaor Gottlieb 
462609e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
462709e574faSSaeed Mahameed 				 struct ib_event *ibev)
462809e574faSSaeed Mahameed {
46296cfdc7e4SAya Levin 	u8 port = (eqe->data.port.port >> 4) & 0xf;
46306cfdc7e4SAya Levin 
463109e574faSSaeed Mahameed 	switch (eqe->sub_type) {
463209e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
46336cfdc7e4SAya Levin 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
46346cfdc7e4SAya Levin 					    IB_LINK_LAYER_ETHERNET)
463509e574faSSaeed Mahameed 			schedule_work(&ibdev->delay_drop.delay_drop_work);
463609e574faSSaeed Mahameed 		break;
463709e574faSSaeed Mahameed 	default: /* do nothing */
463809e574faSSaeed Mahameed 		return;
463909e574faSSaeed Mahameed 	}
464009e574faSSaeed Mahameed }
464109e574faSSaeed Mahameed 
4642134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4643134e9349SSaeed Mahameed 			      struct ib_event *ibev)
4644134e9349SSaeed Mahameed {
4645134e9349SSaeed Mahameed 	u8 port = (eqe->data.port.port >> 4) & 0xf;
4646134e9349SSaeed Mahameed 
4647134e9349SSaeed Mahameed 	ibev->element.port_num = port;
4648134e9349SSaeed Mahameed 
4649134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
4650134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4651134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4652134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4653134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
4654134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
4655134e9349SSaeed Mahameed 		 */
4656134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4657134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
4658134e9349SSaeed Mahameed 			return -EINVAL;
4659134e9349SSaeed Mahameed 
4660134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4661134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4662134e9349SSaeed Mahameed 		break;
4663134e9349SSaeed Mahameed 
4664134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
4665134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
4666134e9349SSaeed Mahameed 		break;
4667134e9349SSaeed Mahameed 
4668134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4669134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
4670134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4671134e9349SSaeed Mahameed 		break;
4672134e9349SSaeed Mahameed 
4673134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4674134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
4675134e9349SSaeed Mahameed 		break;
4676134e9349SSaeed Mahameed 
4677134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4678134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
4679134e9349SSaeed Mahameed 		break;
4680134e9349SSaeed Mahameed 	default:
4681134e9349SSaeed Mahameed 		return -EINVAL;
4682134e9349SSaeed Mahameed 	}
4683134e9349SSaeed Mahameed 
4684134e9349SSaeed Mahameed 	return 0;
4685134e9349SSaeed Mahameed }
4686134e9349SSaeed Mahameed 
4687d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
4688e126ba97SEli Cohen {
4689d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
4690d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
4691d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
4692e126ba97SEli Cohen 	struct ib_event ibev;
4693dbaaff2aSEli Cohen 	bool fatal = false;
4694e126ba97SEli Cohen 
4695df097a27SSaeed Mahameed 	if (work->is_slave) {
4696df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
4697d69a24e0SDaniel Jurgens 		if (!ibdev)
4698d69a24e0SDaniel Jurgens 			goto out;
4699d69a24e0SDaniel Jurgens 	} else {
4700df097a27SSaeed Mahameed 		ibdev = work->dev;
4701d69a24e0SDaniel Jurgens 	}
4702d69a24e0SDaniel Jurgens 
4703d69a24e0SDaniel Jurgens 	switch (work->event) {
4704e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
4705e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
470689ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
4707134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
4708dbaaff2aSEli Cohen 		fatal = true;
4709e126ba97SEli Cohen 		break;
4710134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
4711134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
4712d69a24e0SDaniel Jurgens 			goto out;
4713e126ba97SEli Cohen 		break;
471409e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
471509e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
471609e574faSSaeed Mahameed 		/* fall through */
4717bdc37924SSaeed Mahameed 	default:
471803404e8aSMaor Gottlieb 		goto out;
4719e126ba97SEli Cohen 	}
4720e126ba97SEli Cohen 
4721e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
4722e126ba97SEli Cohen 
4723134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4724134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
472503404e8aSMaor Gottlieb 		goto out;
4726a0c84c32SEli Cohen 	}
4727a0c84c32SEli Cohen 
4728e126ba97SEli Cohen 	if (ibdev->ib_active)
4729e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
4730dbaaff2aSEli Cohen 
4731dbaaff2aSEli Cohen 	if (fatal)
4732dbaaff2aSEli Cohen 		ibdev->ib_active = false;
473303404e8aSMaor Gottlieb out:
4734d69a24e0SDaniel Jurgens 	kfree(work);
4735d69a24e0SDaniel Jurgens }
4736d69a24e0SDaniel Jurgens 
4737df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
4738df097a27SSaeed Mahameed 			 unsigned long event, void *param)
4739d69a24e0SDaniel Jurgens {
4740d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
4741d69a24e0SDaniel Jurgens 
4742d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
474310bea9c8SLeon Romanovsky 	if (!work)
4744df097a27SSaeed Mahameed 		return NOTIFY_DONE;
474510bea9c8SLeon Romanovsky 
4746d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4747df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4748df097a27SSaeed Mahameed 	work->is_slave = false;
4749d69a24e0SDaniel Jurgens 	work->param = param;
4750d69a24e0SDaniel Jurgens 	work->event = event;
4751d69a24e0SDaniel Jurgens 
4752d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
4753df097a27SSaeed Mahameed 
4754df097a27SSaeed Mahameed 	return NOTIFY_OK;
4755df097a27SSaeed Mahameed }
4756df097a27SSaeed Mahameed 
4757df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4758df097a27SSaeed Mahameed 				    unsigned long event, void *param)
4759df097a27SSaeed Mahameed {
4760df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
4761df097a27SSaeed Mahameed 
4762df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
4763df097a27SSaeed Mahameed 	if (!work)
4764df097a27SSaeed Mahameed 		return NOTIFY_DONE;
4765df097a27SSaeed Mahameed 
4766df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4767df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4768df097a27SSaeed Mahameed 	work->is_slave = true;
4769df097a27SSaeed Mahameed 	work->param = param;
4770df097a27SSaeed Mahameed 	work->event = event;
4771df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
4772df097a27SSaeed Mahameed 
4773df097a27SSaeed Mahameed 	return NOTIFY_OK;
4774d69a24e0SDaniel Jurgens }
4775d69a24e0SDaniel Jurgens 
4776c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4777c43f1112SMaor Gottlieb {
4778c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
4779c43f1112SMaor Gottlieb 	int err;
4780c43f1112SMaor Gottlieb 	int port;
4781c43f1112SMaor Gottlieb 
4782a989ea01SMark Bloch 	for (port = 1; port <= ARRAY_SIZE(dev->mdev->port_caps); port++) {
4783c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
4784c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4785c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
4786c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4787c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
4788c43f1112SMaor Gottlieb 								   port, 0,
4789c43f1112SMaor Gottlieb 								   &vport_ctx);
4790c43f1112SMaor Gottlieb 				if (err) {
4791c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4792c43f1112SMaor Gottlieb 						    port, err);
4793c43f1112SMaor Gottlieb 					return err;
4794c43f1112SMaor Gottlieb 				}
4795c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
4796c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
4797c43f1112SMaor Gottlieb 			} else {
4798c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
4799c43f1112SMaor Gottlieb 			}
4800c43f1112SMaor Gottlieb 		}
4801c43f1112SMaor Gottlieb 	}
4802c43f1112SMaor Gottlieb 	return 0;
4803c43f1112SMaor Gottlieb }
4804c43f1112SMaor Gottlieb 
4805e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4806e126ba97SEli Cohen {
4807e126ba97SEli Cohen 	int port;
4808e126ba97SEli Cohen 
4809508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
4810e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
4811e126ba97SEli Cohen }
4812e126ba97SEli Cohen 
481326628e2dSMark Bloch static int __get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4814e126ba97SEli Cohen {
4815e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
4816e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
4817f614fc15SDan Carpenter 	int err = -ENOMEM;
4818e126ba97SEli Cohen 
481950ba3c18SLeon Romanovsky 	pprops = kzalloc(sizeof(*pprops), GFP_KERNEL);
4820e126ba97SEli Cohen 	if (!pprops)
4821e126ba97SEli Cohen 		goto out;
4822e126ba97SEli Cohen 
4823e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4824e126ba97SEli Cohen 	if (!dprops)
4825e126ba97SEli Cohen 		goto out;
4826e126ba97SEli Cohen 
482748357091SLeon Romanovsky 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, NULL);
4828e126ba97SEli Cohen 	if (err) {
4829e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
4830e126ba97SEli Cohen 		goto out;
4831e126ba97SEli Cohen 	}
4832e126ba97SEli Cohen 
4833e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4834e126ba97SEli Cohen 	if (err) {
4835938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
4836938fe83cSSaeed Mahameed 			     port, err);
483732f69e4bSDaniel Jurgens 		goto out;
4838e126ba97SEli Cohen 	}
483932f69e4bSDaniel Jurgens 
4840938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
4841938fe83cSSaeed Mahameed 					dprops->max_pkeys;
4842938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
4843938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
484432f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
484532f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
4846e126ba97SEli Cohen 
4847e126ba97SEli Cohen out:
4848e126ba97SEli Cohen 	kfree(pprops);
4849e126ba97SEli Cohen 	kfree(dprops);
4850e126ba97SEli Cohen 
4851e126ba97SEli Cohen 	return err;
4852e126ba97SEli Cohen }
4853e126ba97SEli Cohen 
485426628e2dSMark Bloch static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
485526628e2dSMark Bloch {
485626628e2dSMark Bloch 	/* For representors use port 1, is this is the only native
485726628e2dSMark Bloch 	 * port
485826628e2dSMark Bloch 	 */
485926628e2dSMark Bloch 	if (dev->is_rep)
486026628e2dSMark Bloch 		return __get_port_caps(dev, 1);
486126628e2dSMark Bloch 	return __get_port_caps(dev, port);
486226628e2dSMark Bloch }
486326628e2dSMark Bloch 
4864e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4865e126ba97SEli Cohen {
4866e126ba97SEli Cohen 	int err;
4867e126ba97SEli Cohen 
4868e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
4869e126ba97SEli Cohen 	if (err)
4870e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4871e126ba97SEli Cohen 
487232927e28SMark Bloch 	if (dev->umrc.qp)
4873c4367a26SShamir Rabinovitch 		mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
487432927e28SMark Bloch 	if (dev->umrc.cq)
4875add08d76SChristoph Hellwig 		ib_free_cq(dev->umrc.cq);
487632927e28SMark Bloch 	if (dev->umrc.pd)
4877e126ba97SEli Cohen 		ib_dealloc_pd(dev->umrc.pd);
4878e126ba97SEli Cohen }
4879e126ba97SEli Cohen 
4880e126ba97SEli Cohen enum {
4881e126ba97SEli Cohen 	MAX_UMR_WR = 128,
4882e126ba97SEli Cohen };
4883e126ba97SEli Cohen 
4884e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
4885e126ba97SEli Cohen {
4886e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
4887e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
4888e126ba97SEli Cohen 	struct ib_pd *pd;
4889e126ba97SEli Cohen 	struct ib_cq *cq;
4890e126ba97SEli Cohen 	struct ib_qp *qp;
4891e126ba97SEli Cohen 	int ret;
4892e126ba97SEli Cohen 
4893e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4894e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4895e126ba97SEli Cohen 	if (!attr || !init_attr) {
4896e126ba97SEli Cohen 		ret = -ENOMEM;
4897e126ba97SEli Cohen 		goto error_0;
4898e126ba97SEli Cohen 	}
4899e126ba97SEli Cohen 
4900ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
4901e126ba97SEli Cohen 	if (IS_ERR(pd)) {
4902e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4903e126ba97SEli Cohen 		ret = PTR_ERR(pd);
4904e126ba97SEli Cohen 		goto error_0;
4905e126ba97SEli Cohen 	}
4906e126ba97SEli Cohen 
4907add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
4908e126ba97SEli Cohen 	if (IS_ERR(cq)) {
4909e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4910e126ba97SEli Cohen 		ret = PTR_ERR(cq);
4911e126ba97SEli Cohen 		goto error_2;
4912e126ba97SEli Cohen 	}
4913e126ba97SEli Cohen 
4914e126ba97SEli Cohen 	init_attr->send_cq = cq;
4915e126ba97SEli Cohen 	init_attr->recv_cq = cq;
4916e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4917e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
4918e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
4919e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4920e126ba97SEli Cohen 	init_attr->port_num = 1;
4921e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4922e126ba97SEli Cohen 	if (IS_ERR(qp)) {
4923e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4924e126ba97SEli Cohen 		ret = PTR_ERR(qp);
4925e126ba97SEli Cohen 		goto error_3;
4926e126ba97SEli Cohen 	}
4927e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
4928e126ba97SEli Cohen 	qp->real_qp    = qp;
4929e126ba97SEli Cohen 	qp->uobject    = NULL;
4930e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
493131fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
493231fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
4933e126ba97SEli Cohen 
4934e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
4935e126ba97SEli Cohen 	attr->port_num = 1;
4936e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4937e126ba97SEli Cohen 				IB_QP_PORT, NULL);
4938e126ba97SEli Cohen 	if (ret) {
4939e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4940e126ba97SEli Cohen 		goto error_4;
4941e126ba97SEli Cohen 	}
4942e126ba97SEli Cohen 
4943e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4944e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
4945e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
4946e126ba97SEli Cohen 
4947e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4948e126ba97SEli Cohen 	if (ret) {
4949e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4950e126ba97SEli Cohen 		goto error_4;
4951e126ba97SEli Cohen 	}
4952e126ba97SEli Cohen 
4953e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4954e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
4955e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4956e126ba97SEli Cohen 	if (ret) {
4957e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4958e126ba97SEli Cohen 		goto error_4;
4959e126ba97SEli Cohen 	}
4960e126ba97SEli Cohen 
4961e126ba97SEli Cohen 	dev->umrc.qp = qp;
4962e126ba97SEli Cohen 	dev->umrc.cq = cq;
4963e126ba97SEli Cohen 	dev->umrc.pd = pd;
4964e126ba97SEli Cohen 
4965e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
4966e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
4967e126ba97SEli Cohen 	if (ret) {
4968e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4969e126ba97SEli Cohen 		goto error_4;
4970e126ba97SEli Cohen 	}
4971e126ba97SEli Cohen 
4972e126ba97SEli Cohen 	kfree(attr);
4973e126ba97SEli Cohen 	kfree(init_attr);
4974e126ba97SEli Cohen 
4975e126ba97SEli Cohen 	return 0;
4976e126ba97SEli Cohen 
4977e126ba97SEli Cohen error_4:
4978c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_qp(qp, NULL);
497932927e28SMark Bloch 	dev->umrc.qp = NULL;
4980e126ba97SEli Cohen 
4981e126ba97SEli Cohen error_3:
4982add08d76SChristoph Hellwig 	ib_free_cq(cq);
498332927e28SMark Bloch 	dev->umrc.cq = NULL;
4984e126ba97SEli Cohen 
4985e126ba97SEli Cohen error_2:
4986e126ba97SEli Cohen 	ib_dealloc_pd(pd);
498732927e28SMark Bloch 	dev->umrc.pd = NULL;
4988e126ba97SEli Cohen 
4989e126ba97SEli Cohen error_0:
4990e126ba97SEli Cohen 	kfree(attr);
4991e126ba97SEli Cohen 	kfree(init_attr);
4992e126ba97SEli Cohen 	return ret;
4993e126ba97SEli Cohen }
4994e126ba97SEli Cohen 
49956e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
49966e8484c5SMax Gurtovoy {
49976e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
49986e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
49996e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
50006e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
50016e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
50026e8484c5SMax Gurtovoy 	default:
50036e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
50046e8484c5SMax Gurtovoy 	}
50056e8484c5SMax Gurtovoy }
50066e8484c5SMax Gurtovoy 
5007e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
5008e126ba97SEli Cohen {
5009e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
5010e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
501121a428a0SLeon Romanovsky 	struct ib_device *ibdev;
5012bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
50137722f47eSHaggai Eran 	int port;
5014e126ba97SEli Cohen 	int ret = 0;
5015e126ba97SEli Cohen 
5016e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
501721a428a0SLeon Romanovsky 	ibdev = &dev->ib_dev;
5018e126ba97SEli Cohen 
5019f4375443SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, xrc))
5020f4375443SLeon Romanovsky 		return -EOPNOTSUPP;
5021f4375443SLeon Romanovsky 
5022d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
5023d16e91daSHaggai Eran 
502421a428a0SLeon Romanovsky 	devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
502521a428a0SLeon Romanovsky 	if (!devr->p0)
502621a428a0SLeon Romanovsky 		return -ENOMEM;
502721a428a0SLeon Romanovsky 
502821a428a0SLeon Romanovsky 	devr->p0->device  = ibdev;
5029e126ba97SEli Cohen 	devr->p0->uobject = NULL;
5030e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
5031e126ba97SEli Cohen 
5032ff23dfa1SShamir Rabinovitch 	ret = mlx5_ib_alloc_pd(devr->p0, NULL);
503321a428a0SLeon Romanovsky 	if (ret)
503421a428a0SLeon Romanovsky 		goto error0;
503521a428a0SLeon Romanovsky 
5036e39afe3dSLeon Romanovsky 	devr->c0 = rdma_zalloc_drv_obj(ibdev, ib_cq);
5037e39afe3dSLeon Romanovsky 	if (!devr->c0) {
5038e39afe3dSLeon Romanovsky 		ret = -ENOMEM;
5039e126ba97SEli Cohen 		goto error1;
5040e126ba97SEli Cohen 	}
5041e39afe3dSLeon Romanovsky 
5042e126ba97SEli Cohen 	devr->c0->device = &dev->ib_dev;
5043e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
5044e126ba97SEli Cohen 
5045e39afe3dSLeon Romanovsky 	ret = mlx5_ib_create_cq(devr->c0, &cq_attr, NULL);
5046e39afe3dSLeon Romanovsky 	if (ret)
5047e39afe3dSLeon Romanovsky 		goto err_create_cq;
5048e39afe3dSLeon Romanovsky 
5049f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn0, 0);
5050f4375443SLeon Romanovsky 	if (ret)
5051e126ba97SEli Cohen 		goto error2;
5052e126ba97SEli Cohen 
5053f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn1, 0);
5054f4375443SLeon Romanovsky 	if (ret)
5055e126ba97SEli Cohen 		goto error3;
5056e126ba97SEli Cohen 
5057e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
5058e126ba97SEli Cohen 	attr.attr.max_sge = 1;
5059e126ba97SEli Cohen 	attr.attr.max_wr = 1;
5060e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
50611a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
5062e126ba97SEli Cohen 
506368e326deSLeon Romanovsky 	devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
506468e326deSLeon Romanovsky 	if (!devr->s0) {
506568e326deSLeon Romanovsky 		ret = -ENOMEM;
5066e126ba97SEli Cohen 		goto error4;
5067e126ba97SEli Cohen 	}
506868e326deSLeon Romanovsky 
5069e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
5070e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
5071e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
50721a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
507368e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
507468e326deSLeon Romanovsky 	if (ret)
507568e326deSLeon Romanovsky 		goto err_create;
507668e326deSLeon Romanovsky 
50771a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
5078e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
5079e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
5080e126ba97SEli Cohen 
50814aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
50824aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
50834aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
50844aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
508568e326deSLeon Romanovsky 	devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
508668e326deSLeon Romanovsky 	if (!devr->s1) {
508768e326deSLeon Romanovsky 		ret = -ENOMEM;
50884aa17b28SHaggai Abramonvsky 		goto error5;
50894aa17b28SHaggai Abramonvsky 	}
509068e326deSLeon Romanovsky 
50914aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
50924aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
50934aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
50941a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
509568e326deSLeon Romanovsky 
509668e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
509768e326deSLeon Romanovsky 	if (ret)
509868e326deSLeon Romanovsky 		goto error6;
509968e326deSLeon Romanovsky 
51004aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
51011a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
51024aa17b28SHaggai Abramonvsky 
51037722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
51047722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
51057722f47eSHaggai Eran 			  pkey_change_handler);
51067722f47eSHaggai Eran 		devr->ports[port].devr = devr;
51077722f47eSHaggai Eran 	}
51087722f47eSHaggai Eran 
5109e126ba97SEli Cohen 	return 0;
5110e126ba97SEli Cohen 
511168e326deSLeon Romanovsky error6:
511268e326deSLeon Romanovsky 	kfree(devr->s1);
51134aa17b28SHaggai Abramonvsky error5:
5114c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
511568e326deSLeon Romanovsky err_create:
511668e326deSLeon Romanovsky 	kfree(devr->s0);
5117e126ba97SEli Cohen error4:
5118f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
5119e126ba97SEli Cohen error3:
5120f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
5121e126ba97SEli Cohen error2:
5122c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
5123e39afe3dSLeon Romanovsky err_create_cq:
5124e39afe3dSLeon Romanovsky 	kfree(devr->c0);
5125e126ba97SEli Cohen error1:
5126c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
5127e126ba97SEli Cohen error0:
512821a428a0SLeon Romanovsky 	kfree(devr->p0);
5129e126ba97SEli Cohen 	return ret;
5130e126ba97SEli Cohen }
5131e126ba97SEli Cohen 
5132e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
5133e126ba97SEli Cohen {
5134f4375443SLeon Romanovsky 	struct mlx5_ib_dev *dev;
51357722f47eSHaggai Eran 	int port;
51367722f47eSHaggai Eran 
5137f4375443SLeon Romanovsky 	dev = container_of(devr, struct mlx5_ib_dev, devr);
5138f4375443SLeon Romanovsky 
5139c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s1, NULL);
514068e326deSLeon Romanovsky 	kfree(devr->s1);
5141c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
514268e326deSLeon Romanovsky 	kfree(devr->s0);
5143f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
5144f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
5145c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
5146e39afe3dSLeon Romanovsky 	kfree(devr->c0);
5147c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
514821a428a0SLeon Romanovsky 	kfree(devr->p0);
51497722f47eSHaggai Eran 
51507722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
51515d8f6a0eSMark Bloch 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
51527722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
5153e126ba97SEli Cohen }
5154e126ba97SEli Cohen 
5155b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
5156b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
5157e53505a8SAchiad Shochat {
5158e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5159e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
5160e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
5161e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
516285c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
5163e53505a8SAchiad Shochat 	u32 ret = 0;
5164e53505a8SAchiad Shochat 
5165b02289b3SArtemy Kovalyov 	if (rep->grh_required)
5166b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
5167b02289b3SArtemy Kovalyov 
5168e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
5169b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
5170e53505a8SAchiad Shochat 
517185c7c014SDaniel Jurgens 	if (raw_support)
5172b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
517372cd5717SOr Gerlitz 
5174e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
517572cd5717SOr Gerlitz 		return ret;
5176e53505a8SAchiad Shochat 
5177e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
517872cd5717SOr Gerlitz 		return ret;
5179e53505a8SAchiad Shochat 
5180e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
5181e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
5182e53505a8SAchiad Shochat 
5183e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
5184e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
5185e53505a8SAchiad Shochat 
5186e53505a8SAchiad Shochat 	return ret;
5187e53505a8SAchiad Shochat }
5188e53505a8SAchiad Shochat 
51897738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
51907738613eSIra Weiny 			       struct ib_port_immutable *immutable)
51917738613eSIra Weiny {
51927738613eSIra Weiny 	struct ib_port_attr attr;
5193ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5194ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
5195b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
51967738613eSIra Weiny 	int err;
51977738613eSIra Weiny 
5198c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
51997738613eSIra Weiny 	if (err)
52007738613eSIra Weiny 		return err;
52017738613eSIra Weiny 
5202b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
5203b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
5204b02289b3SArtemy Kovalyov 						   &rep);
5205b02289b3SArtemy Kovalyov 		if (err)
5206b02289b3SArtemy Kovalyov 			return err;
5207b02289b3SArtemy Kovalyov 	}
5208b02289b3SArtemy Kovalyov 
52097738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
52107738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
5211b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
5212337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
52137738613eSIra Weiny 
52147738613eSIra Weiny 	return 0;
52157738613eSIra Weiny }
52167738613eSIra Weiny 
52178e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
52188e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
52198e6efa3aSMark Bloch {
52208e6efa3aSMark Bloch 	struct ib_port_attr attr;
52218e6efa3aSMark Bloch 	int err;
52228e6efa3aSMark Bloch 
52238e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
52248e6efa3aSMark Bloch 
52258e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
52268e6efa3aSMark Bloch 	if (err)
52278e6efa3aSMark Bloch 		return err;
52288e6efa3aSMark Bloch 
52298e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
52308e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
52318e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
52328e6efa3aSMark Bloch 
52338e6efa3aSMark Bloch 	return 0;
52348e6efa3aSMark Bloch }
52358e6efa3aSMark Bloch 
52369abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
5237c7342823SIra Weiny {
5238c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
5239c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
52409abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
52419abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
52429abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
5243c7342823SIra Weiny }
5244c7342823SIra Weiny 
524545f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
52469ef9c640SAviv Heller {
52479ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
52489ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
52499ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
52509ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
52519ef9c640SAviv Heller 	int err;
52529ef9c640SAviv Heller 
52537c34ec19SAviv Heller 	if (!ns || !mlx5_lag_is_roce(mdev))
52549ef9c640SAviv Heller 		return 0;
52559ef9c640SAviv Heller 
52569ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
52579ef9c640SAviv Heller 	if (err)
52589ef9c640SAviv Heller 		return err;
52599ef9c640SAviv Heller 
52609ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
52619ef9c640SAviv Heller 	if (IS_ERR(ft)) {
52629ef9c640SAviv Heller 		err = PTR_ERR(ft);
52639ef9c640SAviv Heller 		goto err_destroy_vport_lag;
52649ef9c640SAviv Heller 	}
52659ef9c640SAviv Heller 
52669a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
52677c34ec19SAviv Heller 	dev->lag_active = true;
52689ef9c640SAviv Heller 	return 0;
52699ef9c640SAviv Heller 
52709ef9c640SAviv Heller err_destroy_vport_lag:
52719ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
52729ef9c640SAviv Heller 	return err;
52739ef9c640SAviv Heller }
52749ef9c640SAviv Heller 
527545f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
52769ef9c640SAviv Heller {
52779ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
52789ef9c640SAviv Heller 
52797c34ec19SAviv Heller 	if (dev->lag_active) {
52807c34ec19SAviv Heller 		dev->lag_active = false;
52817c34ec19SAviv Heller 
52829a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
52839a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
52849ef9c640SAviv Heller 
52859ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
52869ef9c640SAviv Heller 	}
52879ef9c640SAviv Heller }
52889ef9c640SAviv Heller 
52897fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5290fc24fc5eSAchiad Shochat {
5291e53505a8SAchiad Shochat 	int err;
5292e53505a8SAchiad Shochat 
529395579e78SMark Bloch 	dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
529495579e78SMark Bloch 	err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
52955ec8c83eSAviv Heller 	if (err) {
529695579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
5297e53505a8SAchiad Shochat 		return err;
52985ec8c83eSAviv Heller 	}
5299e53505a8SAchiad Shochat 
5300d012f5d6SOr Gerlitz 	return 0;
5301d012f5d6SOr Gerlitz }
5302d012f5d6SOr Gerlitz 
53037fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5304e126ba97SEli Cohen {
530595579e78SMark Bloch 	if (dev->port[port_num].roce.nb.notifier_call) {
530695579e78SMark Bloch 		unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
530795579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
5308e126ba97SEli Cohen 	}
5309e126ba97SEli Cohen }
5310e126ba97SEli Cohen 
5311e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
5312e126ba97SEli Cohen {
5313e126ba97SEli Cohen 	int err;
5314e126ba97SEli Cohen 
5315e53505a8SAchiad Shochat 	err = mlx5_nic_vport_enable_roce(dev->mdev);
5316e53505a8SAchiad Shochat 	if (err)
53178e6efa3aSMark Bloch 		return err;
5318e53505a8SAchiad Shochat 
531945f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
53209ef9c640SAviv Heller 	if (err)
53219ef9c640SAviv Heller 		goto err_disable_roce;
53229ef9c640SAviv Heller 
5323e53505a8SAchiad Shochat 	return 0;
5324e53505a8SAchiad Shochat 
53259ef9c640SAviv Heller err_disable_roce:
53269ef9c640SAviv Heller 	mlx5_nic_vport_disable_roce(dev->mdev);
53279ef9c640SAviv Heller 
5328e53505a8SAchiad Shochat 	return err;
5329fc24fc5eSAchiad Shochat }
5330fc24fc5eSAchiad Shochat 
533145f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
5332fc24fc5eSAchiad Shochat {
533345f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
5334e53505a8SAchiad Shochat 	mlx5_nic_vport_disable_roce(dev->mdev);
5335fc24fc5eSAchiad Shochat }
5336fc24fc5eSAchiad Shochat 
5337e1f24a79SParav Pandit struct mlx5_ib_counter {
53387c16f477SKamal Heib 	const char *name;
53397c16f477SKamal Heib 	size_t offset;
53407c16f477SKamal Heib };
53417c16f477SKamal Heib 
53427c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
53437c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
53447c16f477SKamal Heib 
5345e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
53467c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
53477c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
53487c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
53497c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
53507c16f477SKamal Heib };
53517c16f477SKamal Heib 
5352e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
53537c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
53547c16f477SKamal Heib };
53557c16f477SKamal Heib 
5356e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
53577c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
53587c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
53597c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
53607c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
53617c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
53627c16f477SKamal Heib };
53637c16f477SKamal Heib 
5364e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
5365e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
5366e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
5367e1f24a79SParav Pandit 
5368e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
5369e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
5370e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
5371e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
5372e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
5373e1f24a79SParav Pandit };
5374e1f24a79SParav Pandit 
537558dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
537658dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
537758dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
537858dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
537958dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
538058dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
538158dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
538258dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
538358dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
538458dcb60aSParav Pandit };
538558dcb60aSParav Pandit 
5386d7fab916SAvihai Horon static const struct mlx5_ib_counter roce_accl_cnts[] = {
5387d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_adp_retrans),
5388d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_adp_retrans_to),
5389d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_slow_restart),
5390d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_slow_restart_cnps),
5391d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_slow_restart_trans),
5392d7fab916SAvihai Horon };
5393d7fab916SAvihai Horon 
53949f876f3dSTalat Batheesh #define INIT_EXT_PPCNT_COUNTER(_name)		\
53959f876f3dSTalat Batheesh 	{ .name = #_name, .offset =	\
53969f876f3dSTalat Batheesh 	MLX5_BYTE_OFF(ppcnt_reg, \
53979f876f3dSTalat Batheesh 		      counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
53989f876f3dSTalat Batheesh 
53999f876f3dSTalat Batheesh static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
54009f876f3dSTalat Batheesh 	INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
54019f876f3dSTalat Batheesh };
54029f876f3dSTalat Batheesh 
54033e1f000fSParav Pandit static bool is_mdev_switchdev_mode(const struct mlx5_core_dev *mdev)
54043e1f000fSParav Pandit {
54053e1f000fSParav Pandit 	return MLX5_ESWITCH_MANAGER(mdev) &&
54063e1f000fSParav Pandit 	       mlx5_ib_eswitch_mode(mdev->priv.eswitch) ==
54073e1f000fSParav Pandit 		       MLX5_ESWITCH_OFFLOADS;
54083e1f000fSParav Pandit }
54093e1f000fSParav Pandit 
5410e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
54110837e86aSMark Bloch {
541266247fbbSLeon Romanovsky 	u32 in[MLX5_ST_SZ_DW(dealloc_q_counter_in)] = {};
54133e1f000fSParav Pandit 	int num_cnt_ports;
5414aac4492eSDaniel Jurgens 	int i;
54150837e86aSMark Bloch 
54163e1f000fSParav Pandit 	num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports;
54173e1f000fSParav Pandit 
541866247fbbSLeon Romanovsky 	MLX5_SET(dealloc_q_counter_in, in, opcode,
541966247fbbSLeon Romanovsky 		 MLX5_CMD_OP_DEALLOC_Q_COUNTER);
542066247fbbSLeon Romanovsky 
54213e1f000fSParav Pandit 	for (i = 0; i < num_cnt_ports; i++) {
5422bfd745f8SLeon Romanovsky 		if (dev->port[i].cnts.set_id) {
542366247fbbSLeon Romanovsky 			MLX5_SET(dealloc_q_counter_in, in, counter_set_id,
5424e1f24a79SParav Pandit 				 dev->port[i].cnts.set_id);
542566247fbbSLeon Romanovsky 			mlx5_cmd_exec_in(dev->mdev, dealloc_q_counter, in);
542666247fbbSLeon Romanovsky 		}
5427e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
5428e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
54297c16f477SKamal Heib 	}
54307c16f477SKamal Heib }
54317c16f477SKamal Heib 
5432e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5433e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
54347c16f477SKamal Heib {
54357c16f477SKamal Heib 	u32 num_counters;
54367c16f477SKamal Heib 
54377c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
54387c16f477SKamal Heib 
54397c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
54407c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
54417c16f477SKamal Heib 
54427c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
54437c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
544458dcb60aSParav Pandit 
544558dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
544658dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
544758dcb60aSParav Pandit 
5448d7fab916SAvihai Horon 	if (MLX5_CAP_GEN(dev->mdev, roce_accl))
5449d7fab916SAvihai Horon 		num_counters += ARRAY_SIZE(roce_accl_cnts);
5450d7fab916SAvihai Horon 
5451e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
54527c16f477SKamal Heib 
5453e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5454e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5455e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
5456e1f24a79SParav Pandit 	}
54579f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
54589f876f3dSTalat Batheesh 		cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
54599f876f3dSTalat Batheesh 		num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
54609f876f3dSTalat Batheesh 	}
5461e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5462e1f24a79SParav Pandit 	if (!cnts->names)
54637c16f477SKamal Heib 		return -ENOMEM;
54647c16f477SKamal Heib 
5465e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
5466e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
5467e1f24a79SParav Pandit 	if (!cnts->offsets)
54687c16f477SKamal Heib 		goto err_names;
54697c16f477SKamal Heib 
54707c16f477SKamal Heib 	return 0;
54717c16f477SKamal Heib 
54727c16f477SKamal Heib err_names:
5473e1f24a79SParav Pandit 	kfree(cnts->names);
5474aac4492eSDaniel Jurgens 	cnts->names = NULL;
54757c16f477SKamal Heib 	return -ENOMEM;
54767c16f477SKamal Heib }
54777c16f477SKamal Heib 
5478e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
54797c16f477SKamal Heib 				  const char **names,
54807c16f477SKamal Heib 				  size_t *offsets)
54817c16f477SKamal Heib {
54827c16f477SKamal Heib 	int i;
54837c16f477SKamal Heib 	int j = 0;
54847c16f477SKamal Heib 
54857c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
54867c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
54877c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
54887c16f477SKamal Heib 	}
54897c16f477SKamal Heib 
54907c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
54917c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
54927c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
54937c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
54947c16f477SKamal Heib 		}
54957c16f477SKamal Heib 	}
54967c16f477SKamal Heib 
54977c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
54987c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
54997c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
55007c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
55017c16f477SKamal Heib 		}
55027c16f477SKamal Heib 	}
5503e1f24a79SParav Pandit 
550458dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
550558dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
550658dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
550758dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
550858dcb60aSParav Pandit 		}
550958dcb60aSParav Pandit 	}
551058dcb60aSParav Pandit 
5511d7fab916SAvihai Horon 	if (MLX5_CAP_GEN(dev->mdev, roce_accl)) {
5512d7fab916SAvihai Horon 		for (i = 0; i < ARRAY_SIZE(roce_accl_cnts); i++, j++) {
5513d7fab916SAvihai Horon 			names[j] = roce_accl_cnts[i].name;
5514d7fab916SAvihai Horon 			offsets[j] = roce_accl_cnts[i].offset;
5515d7fab916SAvihai Horon 		}
5516d7fab916SAvihai Horon 	}
5517d7fab916SAvihai Horon 
5518e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5519e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5520e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
5521e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
5522e1f24a79SParav Pandit 		}
5523e1f24a79SParav Pandit 	}
55249f876f3dSTalat Batheesh 
55259f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
55269f876f3dSTalat Batheesh 		for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
55279f876f3dSTalat Batheesh 			names[j] = ext_ppcnt_cnts[i].name;
55289f876f3dSTalat Batheesh 			offsets[j] = ext_ppcnt_cnts[i].offset;
55299f876f3dSTalat Batheesh 		}
55309f876f3dSTalat Batheesh 	}
55310837e86aSMark Bloch }
55320837e86aSMark Bloch 
5533e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
55340837e86aSMark Bloch {
5535bfd745f8SLeon Romanovsky 	u32 out[MLX5_ST_SZ_DW(alloc_q_counter_out)] = {};
5536bfd745f8SLeon Romanovsky 	u32 in[MLX5_ST_SZ_DW(alloc_q_counter_in)] = {};
55373e1f000fSParav Pandit 	int num_cnt_ports;
5538aac4492eSDaniel Jurgens 	int err = 0;
55390837e86aSMark Bloch 	int i;
5540aa74be6eSYishai Hadas 	bool is_shared;
5541aa74be6eSYishai Hadas 
5542bfd745f8SLeon Romanovsky 	MLX5_SET(alloc_q_counter_in, in, opcode, MLX5_CMD_OP_ALLOC_Q_COUNTER);
5543aa74be6eSYishai Hadas 	is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
55443e1f000fSParav Pandit 	num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports;
55450837e86aSMark Bloch 
55463e1f000fSParav Pandit 	for (i = 0; i < num_cnt_ports; i++) {
5547aac4492eSDaniel Jurgens 		err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5548aac4492eSDaniel Jurgens 		if (err)
5549aac4492eSDaniel Jurgens 			goto err_alloc;
55507c16f477SKamal Heib 
5551aac4492eSDaniel Jurgens 		mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5552aac4492eSDaniel Jurgens 				      dev->port[i].cnts.offsets);
5553aac4492eSDaniel Jurgens 
5554bfd745f8SLeon Romanovsky 		MLX5_SET(alloc_q_counter_in, in, uid,
5555bfd745f8SLeon Romanovsky 			 is_shared ? MLX5_SHARED_RESOURCE_UID : 0);
5556bfd745f8SLeon Romanovsky 
5557bfd745f8SLeon Romanovsky 		err = mlx5_cmd_exec_inout(dev->mdev, alloc_q_counter, in, out);
5558aac4492eSDaniel Jurgens 		if (err) {
55590837e86aSMark Bloch 			mlx5_ib_warn(dev,
55600837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
5561aac4492eSDaniel Jurgens 				     i + 1, err);
5562aac4492eSDaniel Jurgens 			goto err_alloc;
55630837e86aSMark Bloch 		}
5564bfd745f8SLeon Romanovsky 
5565bfd745f8SLeon Romanovsky 		dev->port[i].cnts.set_id =
5566bfd745f8SLeon Romanovsky 			MLX5_GET(alloc_q_counter_out, out, counter_set_id);
55670837e86aSMark Bloch 	}
55680837e86aSMark Bloch 	return 0;
55690837e86aSMark Bloch 
5570aac4492eSDaniel Jurgens err_alloc:
5571aac4492eSDaniel Jurgens 	mlx5_ib_dealloc_counters(dev);
5572aac4492eSDaniel Jurgens 	return err;
55730837e86aSMark Bloch }
55740837e86aSMark Bloch 
55753e1f000fSParav Pandit static const struct mlx5_ib_counters *get_counters(struct mlx5_ib_dev *dev,
55763e1f000fSParav Pandit 						   u8 port_num)
55773e1f000fSParav Pandit {
55783e1f000fSParav Pandit 	return is_mdev_switchdev_mode(dev->mdev) ? &dev->port[0].cnts :
55793e1f000fSParav Pandit 						   &dev->port[port_num].cnts;
55803e1f000fSParav Pandit }
55813e1f000fSParav Pandit 
55823e1f000fSParav Pandit /**
55833e1f000fSParav Pandit  * mlx5_ib_get_counters_id - Returns counters id to use for device+port
55843e1f000fSParav Pandit  * @dev:	Pointer to mlx5 IB device
55853e1f000fSParav Pandit  * @port_num:	Zero based port number
55863e1f000fSParav Pandit  *
55873e1f000fSParav Pandit  * mlx5_ib_get_counters_id() Returns counters set id to use for given
55883e1f000fSParav Pandit  * device port combination in switchdev and non switchdev mode of the
55893e1f000fSParav Pandit  * parent device.
55903e1f000fSParav Pandit  */
55913e1f000fSParav Pandit u16 mlx5_ib_get_counters_id(struct mlx5_ib_dev *dev, u8 port_num)
55923e1f000fSParav Pandit {
55933e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts = get_counters(dev, port_num);
55943e1f000fSParav Pandit 
55953e1f000fSParav Pandit 	return cnts->set_id;
55963e1f000fSParav Pandit }
55973e1f000fSParav Pandit 
55980ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
55990ad17a8fSMark Bloch 						    u8 port_num)
56000ad17a8fSMark Bloch {
56017c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
56023e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts;
56033e1f000fSParav Pandit 	bool is_switchdev = is_mdev_switchdev_mode(dev->mdev);
56040ad17a8fSMark Bloch 
56053e1f000fSParav Pandit 	if ((is_switchdev && port_num) || (!is_switchdev && !port_num))
56060ad17a8fSMark Bloch 		return NULL;
56070ad17a8fSMark Bloch 
56083e1f000fSParav Pandit 	cnts = get_counters(dev, port_num - 1);
56093e1f000fSParav Pandit 
56105dcecbc9SParav Pandit 	return rdma_alloc_hw_stats_struct(cnts->names,
56115dcecbc9SParav Pandit 					  cnts->num_q_counters +
56125dcecbc9SParav Pandit 					  cnts->num_cong_counters +
56135dcecbc9SParav Pandit 					  cnts->num_ext_ppcnt_counters,
56140ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
56150ad17a8fSMark Bloch }
56160ad17a8fSMark Bloch 
5617aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
56185dcecbc9SParav Pandit 				    const struct mlx5_ib_counters *cnts,
5619318d535cSMark Zhang 				    struct rdma_hw_stats *stats,
5620318d535cSMark Zhang 				    u16 set_id)
5621e1f24a79SParav Pandit {
562266247fbbSLeon Romanovsky 	u32 out[MLX5_ST_SZ_DW(query_q_counter_out)] = {};
562366247fbbSLeon Romanovsky 	u32 in[MLX5_ST_SZ_DW(query_q_counter_in)] = {};
5624e1f24a79SParav Pandit 	__be32 val;
5625e1f24a79SParav Pandit 	int ret, i;
5626e1f24a79SParav Pandit 
562766247fbbSLeon Romanovsky 	MLX5_SET(query_q_counter_in, in, opcode, MLX5_CMD_OP_QUERY_Q_COUNTER);
562866247fbbSLeon Romanovsky 	MLX5_SET(query_q_counter_in, in, counter_set_id, set_id);
562966247fbbSLeon Romanovsky 	ret = mlx5_cmd_exec_inout(mdev, query_q_counter, in, out);
5630e1f24a79SParav Pandit 	if (ret)
563166247fbbSLeon Romanovsky 		return ret;
5632e1f24a79SParav Pandit 
56335dcecbc9SParav Pandit 	for (i = 0; i < cnts->num_q_counters; i++) {
563466247fbbSLeon Romanovsky 		val = *(__be32 *)((void *)out + cnts->offsets[i]);
5635e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
5636e1f24a79SParav Pandit 	}
5637e1f24a79SParav Pandit 
563866247fbbSLeon Romanovsky 	return 0;
5639e1f24a79SParav Pandit }
5640e1f24a79SParav Pandit 
56419f876f3dSTalat Batheesh static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
56425dcecbc9SParav Pandit 					    const struct mlx5_ib_counters *cnts,
56439f876f3dSTalat Batheesh 					    struct rdma_hw_stats *stats)
56449f876f3dSTalat Batheesh {
56455dcecbc9SParav Pandit 	int offset = cnts->num_q_counters + cnts->num_cong_counters;
56469f876f3dSTalat Batheesh 	int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
56479f876f3dSTalat Batheesh 	int ret, i;
56489f876f3dSTalat Batheesh 	void *out;
56499f876f3dSTalat Batheesh 
56509f876f3dSTalat Batheesh 	out = kvzalloc(sz, GFP_KERNEL);
56519f876f3dSTalat Batheesh 	if (!out)
56529f876f3dSTalat Batheesh 		return -ENOMEM;
56539f876f3dSTalat Batheesh 
56549f876f3dSTalat Batheesh 	ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
56559f876f3dSTalat Batheesh 	if (ret)
56569f876f3dSTalat Batheesh 		goto free;
56579f876f3dSTalat Batheesh 
56585dcecbc9SParav Pandit 	for (i = 0; i < cnts->num_ext_ppcnt_counters; i++)
56599f876f3dSTalat Batheesh 		stats->value[i + offset] =
56609f876f3dSTalat Batheesh 			be64_to_cpup((__be64 *)(out +
56615dcecbc9SParav Pandit 				    cnts->offsets[i + offset]));
56629f876f3dSTalat Batheesh free:
56639f876f3dSTalat Batheesh 	kvfree(out);
56649f876f3dSTalat Batheesh 	return ret;
56659f876f3dSTalat Batheesh }
56669f876f3dSTalat Batheesh 
56670ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
56680ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
56697c16f477SKamal Heib 				u8 port_num, int index)
56700ad17a8fSMark Bloch {
56710ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
56723e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts = get_counters(dev, port_num - 1);
5673aac4492eSDaniel Jurgens 	struct mlx5_core_dev *mdev;
5674e1f24a79SParav Pandit 	int ret, num_counters;
5675aac4492eSDaniel Jurgens 	u8 mdev_port_num;
56760ad17a8fSMark Bloch 
56777c16f477SKamal Heib 	if (!stats)
5678e1f24a79SParav Pandit 		return -EINVAL;
56790ad17a8fSMark Bloch 
56805dcecbc9SParav Pandit 	num_counters = cnts->num_q_counters +
56815dcecbc9SParav Pandit 		       cnts->num_cong_counters +
56825dcecbc9SParav Pandit 		       cnts->num_ext_ppcnt_counters;
5683aac4492eSDaniel Jurgens 
5684aac4492eSDaniel Jurgens 	/* q_counters are per IB device, query the master mdev */
56855dcecbc9SParav Pandit 	ret = mlx5_ib_query_q_counters(dev->mdev, cnts, stats, cnts->set_id);
56860ad17a8fSMark Bloch 	if (ret)
5687e1f24a79SParav Pandit 		return ret;
56880ad17a8fSMark Bloch 
56899f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
56905dcecbc9SParav Pandit 		ret =  mlx5_ib_query_ext_ppcnt_counters(dev, cnts, stats);
56919f876f3dSTalat Batheesh 		if (ret)
56929f876f3dSTalat Batheesh 			return ret;
56939f876f3dSTalat Batheesh 	}
56949f876f3dSTalat Batheesh 
5695e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5696aac4492eSDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5697aac4492eSDaniel Jurgens 						    &mdev_port_num);
5698aac4492eSDaniel Jurgens 		if (!mdev) {
5699aac4492eSDaniel Jurgens 			/* If port is not affiliated yet, its in down state
5700aac4492eSDaniel Jurgens 			 * which doesn't have any counters yet, so it would be
5701aac4492eSDaniel Jurgens 			 * zero. So no need to read from the HCA.
5702aac4492eSDaniel Jurgens 			 */
5703aac4492eSDaniel Jurgens 			goto done;
5704aac4492eSDaniel Jurgens 		}
570571a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
570671a0ff65SMajd Dibbiny 						   stats->value +
57075dcecbc9SParav Pandit 						   cnts->num_q_counters,
57085dcecbc9SParav Pandit 						   cnts->num_cong_counters,
57095dcecbc9SParav Pandit 						   cnts->offsets +
57105dcecbc9SParav Pandit 						   cnts->num_q_counters);
5711aac4492eSDaniel Jurgens 
5712aac4492eSDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
5713e1f24a79SParav Pandit 		if (ret)
5714e1f24a79SParav Pandit 			return ret;
57150ad17a8fSMark Bloch 	}
57167c16f477SKamal Heib 
5717aac4492eSDaniel Jurgens done:
5718e1f24a79SParav Pandit 	return num_counters;
57190ad17a8fSMark Bloch }
57200ad17a8fSMark Bloch 
572118d422ceSMark Zhang static struct rdma_hw_stats *
572218d422ceSMark Zhang mlx5_ib_counter_alloc_stats(struct rdma_counter *counter)
572318d422ceSMark Zhang {
572418d422ceSMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
57255dcecbc9SParav Pandit 	const struct mlx5_ib_counters *cnts =
57263e1f000fSParav Pandit 		get_counters(dev, counter->port - 1);
572718d422ceSMark Zhang 
57285dcecbc9SParav Pandit 	return rdma_alloc_hw_stats_struct(cnts->names,
5729ec16b6bbSMark Zhang 					  cnts->num_q_counters +
5730ec16b6bbSMark Zhang 					  cnts->num_cong_counters +
5731ec16b6bbSMark Zhang 					  cnts->num_ext_ppcnt_counters,
573218d422ceSMark Zhang 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
573318d422ceSMark Zhang }
573418d422ceSMark Zhang 
573518d422ceSMark Zhang static int mlx5_ib_counter_update_stats(struct rdma_counter *counter)
573618d422ceSMark Zhang {
573718d422ceSMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
57383e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts =
57393e1f000fSParav Pandit 		get_counters(dev, counter->port - 1);
574018d422ceSMark Zhang 
57415dcecbc9SParav Pandit 	return mlx5_ib_query_q_counters(dev->mdev, cnts,
574218d422ceSMark Zhang 					counter->stats, counter->id);
574318d422ceSMark Zhang }
574418d422ceSMark Zhang 
574566247fbbSLeon Romanovsky static int mlx5_ib_counter_dealloc(struct rdma_counter *counter)
574666247fbbSLeon Romanovsky {
574766247fbbSLeon Romanovsky 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
574866247fbbSLeon Romanovsky 	u32 in[MLX5_ST_SZ_DW(dealloc_q_counter_in)] = {};
574966247fbbSLeon Romanovsky 
575066247fbbSLeon Romanovsky 	if (!counter->id)
575166247fbbSLeon Romanovsky 		return 0;
575266247fbbSLeon Romanovsky 
575366247fbbSLeon Romanovsky 	MLX5_SET(dealloc_q_counter_in, in, opcode,
575466247fbbSLeon Romanovsky 		 MLX5_CMD_OP_DEALLOC_Q_COUNTER);
575566247fbbSLeon Romanovsky 	MLX5_SET(dealloc_q_counter_in, in, counter_set_id, counter->id);
575666247fbbSLeon Romanovsky 	return mlx5_cmd_exec_in(dev->mdev, dealloc_q_counter, in);
575766247fbbSLeon Romanovsky }
575866247fbbSLeon Romanovsky 
575945842fc6SMark Zhang static int mlx5_ib_counter_bind_qp(struct rdma_counter *counter,
576045842fc6SMark Zhang 				   struct ib_qp *qp)
576145842fc6SMark Zhang {
576245842fc6SMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
576345842fc6SMark Zhang 	int err;
576445842fc6SMark Zhang 
576545842fc6SMark Zhang 	if (!counter->id) {
5766bfd745f8SLeon Romanovsky 		u32 out[MLX5_ST_SZ_DW(alloc_q_counter_out)] = {};
5767bfd745f8SLeon Romanovsky 		u32 in[MLX5_ST_SZ_DW(alloc_q_counter_in)] = {};
5768bfd745f8SLeon Romanovsky 
5769bfd745f8SLeon Romanovsky 		MLX5_SET(alloc_q_counter_in, in, opcode,
5770bfd745f8SLeon Romanovsky 			 MLX5_CMD_OP_ALLOC_Q_COUNTER);
5771bfd745f8SLeon Romanovsky 		MLX5_SET(alloc_q_counter_in, in, uid, MLX5_SHARED_RESOURCE_UID);
5772bfd745f8SLeon Romanovsky 		err = mlx5_cmd_exec_inout(dev->mdev, alloc_q_counter, in, out);
577345842fc6SMark Zhang 		if (err)
577445842fc6SMark Zhang 			return err;
5775bfd745f8SLeon Romanovsky 		counter->id =
5776bfd745f8SLeon Romanovsky 			MLX5_GET(alloc_q_counter_out, out, counter_set_id);
577745842fc6SMark Zhang 	}
577845842fc6SMark Zhang 
577945842fc6SMark Zhang 	err = mlx5_ib_qp_set_counter(qp, counter);
578045842fc6SMark Zhang 	if (err)
578145842fc6SMark Zhang 		goto fail_set_counter;
578245842fc6SMark Zhang 
578345842fc6SMark Zhang 	return 0;
578445842fc6SMark Zhang 
578545842fc6SMark Zhang fail_set_counter:
578666247fbbSLeon Romanovsky 	mlx5_ib_counter_dealloc(counter);
578745842fc6SMark Zhang 	counter->id = 0;
578845842fc6SMark Zhang 
578945842fc6SMark Zhang 	return err;
579045842fc6SMark Zhang }
579145842fc6SMark Zhang 
579245842fc6SMark Zhang static int mlx5_ib_counter_unbind_qp(struct ib_qp *qp)
579345842fc6SMark Zhang {
579445842fc6SMark Zhang 	return mlx5_ib_qp_set_counter(qp, NULL);
579545842fc6SMark Zhang }
579645842fc6SMark Zhang 
5797f6a8a19bSDenis Drozdov static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5798693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
5799f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
5800693dfd5aSErez Shitrit {
5801693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
5802f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
5803693dfd5aSErez Shitrit 
5804f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
5805693dfd5aSErez Shitrit }
5806693dfd5aSErez Shitrit 
5807fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5808fe248c3aSMaor Gottlieb {
580909b0965eSGreg Kroah-Hartman 	if (!dev->delay_drop.dir_debugfs)
5810fe248c3aSMaor Gottlieb 		return;
581109b0965eSGreg Kroah-Hartman 	debugfs_remove_recursive(dev->delay_drop.dir_debugfs);
581209b0965eSGreg Kroah-Hartman 	dev->delay_drop.dir_debugfs = NULL;
5813fe248c3aSMaor Gottlieb }
5814fe248c3aSMaor Gottlieb 
581503404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
581603404e8aSMaor Gottlieb {
581703404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
581803404e8aSMaor Gottlieb 		return;
581903404e8aSMaor Gottlieb 
582003404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
5821fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5822fe248c3aSMaor Gottlieb }
5823fe248c3aSMaor Gottlieb 
5824fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5825fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
5826fe248c3aSMaor Gottlieb {
5827fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5828fe248c3aSMaor Gottlieb 	char lbuf[20];
5829fe248c3aSMaor Gottlieb 	int len;
5830fe248c3aSMaor Gottlieb 
5831fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5832fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
5833fe248c3aSMaor Gottlieb }
5834fe248c3aSMaor Gottlieb 
5835fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5836fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
5837fe248c3aSMaor Gottlieb {
5838fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5839fe248c3aSMaor Gottlieb 	u32 timeout;
5840fe248c3aSMaor Gottlieb 	u32 var;
5841fe248c3aSMaor Gottlieb 
5842fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
5843fe248c3aSMaor Gottlieb 		return -EFAULT;
5844fe248c3aSMaor Gottlieb 
5845fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5846fe248c3aSMaor Gottlieb 			1000);
5847fe248c3aSMaor Gottlieb 	if (timeout != var)
5848fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5849fe248c3aSMaor Gottlieb 			    timeout);
5850fe248c3aSMaor Gottlieb 
5851fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
5852fe248c3aSMaor Gottlieb 
5853fe248c3aSMaor Gottlieb 	return count;
5854fe248c3aSMaor Gottlieb }
5855fe248c3aSMaor Gottlieb 
5856fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
5857fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
5858fe248c3aSMaor Gottlieb 	.open	= simple_open,
5859fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
5860fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
5861fe248c3aSMaor Gottlieb };
5862fe248c3aSMaor Gottlieb 
586309b0965eSGreg Kroah-Hartman static void delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5864fe248c3aSMaor Gottlieb {
586509b0965eSGreg Kroah-Hartman 	struct dentry *root;
5866fe248c3aSMaor Gottlieb 
5867fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
586809b0965eSGreg Kroah-Hartman 		return;
5869fe248c3aSMaor Gottlieb 
587009b0965eSGreg Kroah-Hartman 	root = debugfs_create_dir("delay_drop", dev->mdev->priv.dbg_root);
587109b0965eSGreg Kroah-Hartman 	dev->delay_drop.dir_debugfs = root;
5872fe248c3aSMaor Gottlieb 
587309b0965eSGreg Kroah-Hartman 	debugfs_create_atomic_t("num_timeout_events", 0400, root,
5874fe248c3aSMaor Gottlieb 				&dev->delay_drop.events_cnt);
587509b0965eSGreg Kroah-Hartman 	debugfs_create_atomic_t("num_rqs", 0400, root,
5876fe248c3aSMaor Gottlieb 				&dev->delay_drop.rqs_cnt);
587709b0965eSGreg Kroah-Hartman 	debugfs_create_file("timeout", 0600, root, &dev->delay_drop,
5878fe248c3aSMaor Gottlieb 			    &fops_delay_drop_timeout);
587903404e8aSMaor Gottlieb }
588003404e8aSMaor Gottlieb 
588103404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
588203404e8aSMaor Gottlieb {
588303404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
588403404e8aSMaor Gottlieb 		return;
588503404e8aSMaor Gottlieb 
588603404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
588703404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
588803404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
588903404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
589003404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
5891fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
5892fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
5893fe248c3aSMaor Gottlieb 
589409b0965eSGreg Kroah-Hartman 	delay_drop_debugfs_init(dev);
589503404e8aSMaor Gottlieb }
589603404e8aSMaor Gottlieb 
589732f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
589832f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
589932f69e4bSDaniel Jurgens {
590032f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
590132f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
590232f69e4bSDaniel Jurgens 	int comps;
590332f69e4bSDaniel Jurgens 	int err;
590432f69e4bSDaniel Jurgens 	int i;
590532f69e4bSDaniel Jurgens 
59069dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
59079dc4cfffSLeon Romanovsky 
5908a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5909a9e546e7SParav Pandit 
591032f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
591132f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
591232f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
591332f69e4bSDaniel Jurgens 		return;
591432f69e4bSDaniel Jurgens 	}
5915df097a27SSaeed Mahameed 
591632f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
591732f69e4bSDaniel Jurgens 
591832f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
591923eaf3b5SLeon Romanovsky 	if (mpi->mdev_events.notifier_call)
592023eaf3b5SLeon Romanovsky 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
592123eaf3b5SLeon Romanovsky 	mpi->mdev_events.notifier_call = NULL;
592232f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
592332f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
592432f69e4bSDaniel Jurgens 
592532f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
592632f69e4bSDaniel Jurgens 	if (comps) {
592732f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
592832f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
592932f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
593032f69e4bSDaniel Jurgens 
593132f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
593232f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
593332f69e4bSDaniel Jurgens 
593432f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
593532f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
593632f69e4bSDaniel Jurgens 	}
593732f69e4bSDaniel Jurgens 
593832f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
593932f69e4bSDaniel Jurgens 
594032f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
594132f69e4bSDaniel Jurgens 
594232f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
594332f69e4bSDaniel Jurgens 
594432f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
594532f69e4bSDaniel Jurgens 
594632f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
594732f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
594832f69e4bSDaniel Jurgens 	 * it back to the list.
594932f69e4bSDaniel Jurgens 	 */
595032f69e4bSDaniel Jurgens 	if (err)
595132f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
595232f69e4bSDaniel Jurgens 			    port_num + 1);
595332f69e4bSDaniel Jurgens 
595495579e78SMark Bloch 	ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
595532f69e4bSDaniel Jurgens }
595632f69e4bSDaniel Jurgens 
595732f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
595832f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
595932f69e4bSDaniel Jurgens {
596032f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
596132f69e4bSDaniel Jurgens 	int err;
596232f69e4bSDaniel Jurgens 
59639dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
59649dc4cfffSLeon Romanovsky 
596532f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
596632f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
59672577188eSQing Huang 		mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
596832f69e4bSDaniel Jurgens 			    port_num + 1);
596932f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
597032f69e4bSDaniel Jurgens 		return false;
597132f69e4bSDaniel Jurgens 	}
597232f69e4bSDaniel Jurgens 
597332f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
597432f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
5975df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
597632f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
597732f69e4bSDaniel Jurgens 
597832f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
597932f69e4bSDaniel Jurgens 	if (err)
598032f69e4bSDaniel Jurgens 		goto unbind;
598132f69e4bSDaniel Jurgens 
598232f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
598332f69e4bSDaniel Jurgens 	if (err)
598432f69e4bSDaniel Jurgens 		goto unbind;
598532f69e4bSDaniel Jurgens 
598632f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
598732f69e4bSDaniel Jurgens 	if (err) {
598832f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
598932f69e4bSDaniel Jurgens 			    port_num + 1);
599032f69e4bSDaniel Jurgens 		goto unbind;
599132f69e4bSDaniel Jurgens 	}
599232f69e4bSDaniel Jurgens 
5993df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5994df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5995df097a27SSaeed Mahameed 
599673eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
5997a9e546e7SParav Pandit 
599832f69e4bSDaniel Jurgens 	return true;
599932f69e4bSDaniel Jurgens 
600032f69e4bSDaniel Jurgens unbind:
600132f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
600232f69e4bSDaniel Jurgens 	return false;
600332f69e4bSDaniel Jurgens }
600432f69e4bSDaniel Jurgens 
600532f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
600632f69e4bSDaniel Jurgens {
600732f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
600832f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
600932f69e4bSDaniel Jurgens 							  port_num + 1);
601032f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
601132f69e4bSDaniel Jurgens 	int err;
601232f69e4bSDaniel Jurgens 	int i;
601332f69e4bSDaniel Jurgens 
601432f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
601532f69e4bSDaniel Jurgens 		return 0;
601632f69e4bSDaniel Jurgens 
601732f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
601832f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
601932f69e4bSDaniel Jurgens 	if (err)
602032f69e4bSDaniel Jurgens 		return err;
602132f69e4bSDaniel Jurgens 
602232f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
602332f69e4bSDaniel Jurgens 	if (err)
602432f69e4bSDaniel Jurgens 		return err;
602532f69e4bSDaniel Jurgens 
602632f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
602732f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
602832f69e4bSDaniel Jurgens 		bool bound = false;
602932f69e4bSDaniel Jurgens 
603032f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
603132f69e4bSDaniel Jurgens 		if (i == port_num) {
603232f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
603332f69e4bSDaniel Jurgens 			if (!mpi) {
603432f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
603532f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
603632f69e4bSDaniel Jurgens 				return -ENOMEM;
603732f69e4bSDaniel Jurgens 			}
603832f69e4bSDaniel Jurgens 
603932f69e4bSDaniel Jurgens 			mpi->is_master = true;
604032f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
604132f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
604232f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
604332f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
604432f69e4bSDaniel Jurgens 			mpi = NULL;
604532f69e4bSDaniel Jurgens 			continue;
604632f69e4bSDaniel Jurgens 		}
604732f69e4bSDaniel Jurgens 
604832f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
604932f69e4bSDaniel Jurgens 				    list) {
605032f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
605132f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
605232f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
605332f69e4bSDaniel Jurgens 			}
605432f69e4bSDaniel Jurgens 
605532f69e4bSDaniel Jurgens 			if (bound) {
6056c42260f1SVu Pham 				dev_dbg(mpi->mdev->device,
6057c42260f1SVu Pham 					"removing port from unaffiliated list.\n");
605832f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
605932f69e4bSDaniel Jurgens 				list_del(&mpi->list);
606032f69e4bSDaniel Jurgens 				break;
606132f69e4bSDaniel Jurgens 			}
606232f69e4bSDaniel Jurgens 		}
606332f69e4bSDaniel Jurgens 		if (!bound) {
606432f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
606532f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
606632f69e4bSDaniel Jurgens 				    i + 1);
606732f69e4bSDaniel Jurgens 		}
606832f69e4bSDaniel Jurgens 	}
606932f69e4bSDaniel Jurgens 
607032f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
607132f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
607232f69e4bSDaniel Jurgens 	return err;
607332f69e4bSDaniel Jurgens }
607432f69e4bSDaniel Jurgens 
607532f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
607632f69e4bSDaniel Jurgens {
607732f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
607832f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
607932f69e4bSDaniel Jurgens 							  port_num + 1);
608032f69e4bSDaniel Jurgens 	int i;
608132f69e4bSDaniel Jurgens 
608232f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
608332f69e4bSDaniel Jurgens 		return;
608432f69e4bSDaniel Jurgens 
608532f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
608632f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
608732f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
608832f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
608932f69e4bSDaniel Jurgens 			if (i == port_num) {
609032f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
609132f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
609232f69e4bSDaniel Jurgens 			} else {
609332f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
609432f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
609532f69e4bSDaniel Jurgens 			}
609632f69e4bSDaniel Jurgens 		}
609732f69e4bSDaniel Jurgens 	}
609832f69e4bSDaniel Jurgens 
609932f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
610032f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
610132f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
610232f69e4bSDaniel Jurgens 
610332f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
610432f69e4bSDaniel Jurgens }
610532f69e4bSDaniel Jurgens 
6106342ee59dSYishai Hadas static int mmap_obj_cleanup(struct ib_uobject *uobject,
61077be76befSYishai Hadas 			    enum rdma_remove_reason why,
61087be76befSYishai Hadas 			    struct uverbs_attr_bundle *attrs)
61097be76befSYishai Hadas {
61107be76befSYishai Hadas 	struct mlx5_user_mmap_entry *obj = uobject->object;
61117be76befSYishai Hadas 
61127be76befSYishai Hadas 	rdma_user_mmap_entry_remove(&obj->rdma_entry);
61137be76befSYishai Hadas 	return 0;
61147be76befSYishai Hadas }
61157be76befSYishai Hadas 
6116342ee59dSYishai Hadas static int mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext *c,
6117342ee59dSYishai Hadas 					    struct mlx5_user_mmap_entry *entry,
6118342ee59dSYishai Hadas 					    size_t length)
6119342ee59dSYishai Hadas {
6120342ee59dSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
6121342ee59dSYishai Hadas 		&c->ibucontext, &entry->rdma_entry, length,
6122342ee59dSYishai Hadas 		(MLX5_IB_MMAP_OFFSET_START << 16),
6123342ee59dSYishai Hadas 		((MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1));
6124342ee59dSYishai Hadas }
6125342ee59dSYishai Hadas 
61267be76befSYishai Hadas static struct mlx5_user_mmap_entry *
61277be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c)
61287be76befSYishai Hadas {
61297be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
61307be76befSYishai Hadas 	struct mlx5_var_table *var_table;
61317be76befSYishai Hadas 	u32 page_idx;
61327be76befSYishai Hadas 	int err;
61337be76befSYishai Hadas 
61347be76befSYishai Hadas 	var_table = &to_mdev(c->ibucontext.device)->var_table;
61357be76befSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
61367be76befSYishai Hadas 	if (!entry)
61377be76befSYishai Hadas 		return ERR_PTR(-ENOMEM);
61387be76befSYishai Hadas 
61397be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
61407be76befSYishai Hadas 	page_idx = find_first_zero_bit(var_table->bitmap,
61417be76befSYishai Hadas 				       var_table->num_var_hw_entries);
61427be76befSYishai Hadas 	if (page_idx >= var_table->num_var_hw_entries) {
61437be76befSYishai Hadas 		err = -ENOSPC;
61447be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
61457be76befSYishai Hadas 		goto end;
61467be76befSYishai Hadas 	}
61477be76befSYishai Hadas 
61487be76befSYishai Hadas 	set_bit(page_idx, var_table->bitmap);
61497be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
61507be76befSYishai Hadas 
61517be76befSYishai Hadas 	entry->address = var_table->hw_start_addr +
61527be76befSYishai Hadas 				(page_idx * var_table->stride_size);
61537be76befSYishai Hadas 	entry->page_idx = page_idx;
61547be76befSYishai Hadas 	entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR;
61557be76befSYishai Hadas 
6156342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry,
6157342ee59dSYishai Hadas 					       var_table->stride_size);
61587be76befSYishai Hadas 	if (err)
61597be76befSYishai Hadas 		goto err_insert;
61607be76befSYishai Hadas 
61617be76befSYishai Hadas 	return entry;
61627be76befSYishai Hadas 
61637be76befSYishai Hadas err_insert:
61647be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
61657be76befSYishai Hadas 	clear_bit(page_idx, var_table->bitmap);
61667be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
61677be76befSYishai Hadas end:
61687be76befSYishai Hadas 	kfree(entry);
61697be76befSYishai Hadas 	return ERR_PTR(err);
61707be76befSYishai Hadas }
61717be76befSYishai Hadas 
61727be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)(
61737be76befSYishai Hadas 	struct uverbs_attr_bundle *attrs)
61747be76befSYishai Hadas {
61757be76befSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
61767be76befSYishai Hadas 		attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
61777be76befSYishai Hadas 	struct mlx5_ib_ucontext *c;
61787be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
61797be76befSYishai Hadas 	u64 mmap_offset;
61807be76befSYishai Hadas 	u32 length;
61817be76befSYishai Hadas 	int err;
61827be76befSYishai Hadas 
61837be76befSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
61847be76befSYishai Hadas 	if (IS_ERR(c))
61857be76befSYishai Hadas 		return PTR_ERR(c);
61867be76befSYishai Hadas 
61877be76befSYishai Hadas 	entry = alloc_var_entry(c);
61887be76befSYishai Hadas 	if (IS_ERR(entry))
61897be76befSYishai Hadas 		return PTR_ERR(entry);
61907be76befSYishai Hadas 
61917be76befSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
61927be76befSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
61937be76befSYishai Hadas 	uobj->object = entry;
61940ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
61957be76befSYishai Hadas 
61967be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
61977be76befSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
61987be76befSYishai Hadas 	if (err)
61990ac8903cSJason Gunthorpe 		return err;
62007be76befSYishai Hadas 
62017be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
62027be76befSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
62037be76befSYishai Hadas 	if (err)
62040ac8903cSJason Gunthorpe 		return err;
62057be76befSYishai Hadas 
62067be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
62077be76befSYishai Hadas 			     &length, sizeof(length));
62087be76befSYishai Hadas 	return err;
62097be76befSYishai Hadas }
62107be76befSYishai Hadas 
62117be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
62127be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_ALLOC,
62137be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE,
62147be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
62157be76befSYishai Hadas 			UVERBS_ACCESS_NEW,
62167be76befSYishai Hadas 			UA_MANDATORY),
62177be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
62187be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
62197be76befSYishai Hadas 			   UA_MANDATORY),
62207be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
62217be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
62227be76befSYishai Hadas 			   UA_MANDATORY),
62237be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
62247be76befSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
62257be76befSYishai Hadas 			    UA_MANDATORY));
62267be76befSYishai Hadas 
62277be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
62287be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_DESTROY,
62297be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE,
62307be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
62317be76befSYishai Hadas 			UVERBS_ACCESS_DESTROY,
62327be76befSYishai Hadas 			UA_MANDATORY));
62337be76befSYishai Hadas 
62347be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR,
6235342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
62367be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC),
62377be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY));
62387be76befSYishai Hadas 
62397be76befSYishai Hadas static bool var_is_supported(struct ib_device *device)
62407be76befSYishai Hadas {
62417be76befSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(device);
62427be76befSYishai Hadas 
62437be76befSYishai Hadas 	return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
62447be76befSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q);
62457be76befSYishai Hadas }
62467be76befSYishai Hadas 
6247342ee59dSYishai Hadas static struct mlx5_user_mmap_entry *
6248342ee59dSYishai Hadas alloc_uar_entry(struct mlx5_ib_ucontext *c,
6249342ee59dSYishai Hadas 		enum mlx5_ib_uapi_uar_alloc_type alloc_type)
6250342ee59dSYishai Hadas {
6251342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
6252342ee59dSYishai Hadas 	struct mlx5_ib_dev *dev;
6253342ee59dSYishai Hadas 	u32 uar_index;
6254342ee59dSYishai Hadas 	int err;
6255342ee59dSYishai Hadas 
6256342ee59dSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
6257342ee59dSYishai Hadas 	if (!entry)
6258342ee59dSYishai Hadas 		return ERR_PTR(-ENOMEM);
6259342ee59dSYishai Hadas 
6260342ee59dSYishai Hadas 	dev = to_mdev(c->ibucontext.device);
6261342ee59dSYishai Hadas 	err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
6262342ee59dSYishai Hadas 	if (err)
6263342ee59dSYishai Hadas 		goto end;
6264342ee59dSYishai Hadas 
6265342ee59dSYishai Hadas 	entry->page_idx = uar_index;
6266342ee59dSYishai Hadas 	entry->address = uar_index2paddress(dev, uar_index);
6267342ee59dSYishai Hadas 	if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
6268342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC;
6269342ee59dSYishai Hadas 	else
6270342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_NC;
6271342ee59dSYishai Hadas 
6272342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry, PAGE_SIZE);
6273342ee59dSYishai Hadas 	if (err)
6274342ee59dSYishai Hadas 		goto err_insert;
6275342ee59dSYishai Hadas 
6276342ee59dSYishai Hadas 	return entry;
6277342ee59dSYishai Hadas 
6278342ee59dSYishai Hadas err_insert:
6279342ee59dSYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, uar_index);
6280342ee59dSYishai Hadas end:
6281342ee59dSYishai Hadas 	kfree(entry);
6282342ee59dSYishai Hadas 	return ERR_PTR(err);
6283342ee59dSYishai Hadas }
6284342ee59dSYishai Hadas 
6285342ee59dSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)(
6286342ee59dSYishai Hadas 	struct uverbs_attr_bundle *attrs)
6287342ee59dSYishai Hadas {
6288342ee59dSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
6289342ee59dSYishai Hadas 		attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
6290342ee59dSYishai Hadas 	enum mlx5_ib_uapi_uar_alloc_type alloc_type;
6291342ee59dSYishai Hadas 	struct mlx5_ib_ucontext *c;
6292342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
6293342ee59dSYishai Hadas 	u64 mmap_offset;
6294342ee59dSYishai Hadas 	u32 length;
6295342ee59dSYishai Hadas 	int err;
6296342ee59dSYishai Hadas 
6297342ee59dSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
6298342ee59dSYishai Hadas 	if (IS_ERR(c))
6299342ee59dSYishai Hadas 		return PTR_ERR(c);
6300342ee59dSYishai Hadas 
6301342ee59dSYishai Hadas 	err = uverbs_get_const(&alloc_type, attrs,
6302342ee59dSYishai Hadas 			       MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE);
6303342ee59dSYishai Hadas 	if (err)
6304342ee59dSYishai Hadas 		return err;
6305342ee59dSYishai Hadas 
6306342ee59dSYishai Hadas 	if (alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF &&
6307342ee59dSYishai Hadas 	    alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_NC)
6308342ee59dSYishai Hadas 		return -EOPNOTSUPP;
6309342ee59dSYishai Hadas 
6310342ee59dSYishai Hadas 	if (!to_mdev(c->ibucontext.device)->wc_support &&
6311342ee59dSYishai Hadas 	    alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
6312342ee59dSYishai Hadas 		return -EOPNOTSUPP;
6313342ee59dSYishai Hadas 
6314342ee59dSYishai Hadas 	entry = alloc_uar_entry(c, alloc_type);
6315342ee59dSYishai Hadas 	if (IS_ERR(entry))
6316342ee59dSYishai Hadas 		return PTR_ERR(entry);
6317342ee59dSYishai Hadas 
6318342ee59dSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
6319342ee59dSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
6320342ee59dSYishai Hadas 	uobj->object = entry;
63210ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
6322342ee59dSYishai Hadas 
6323342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
6324342ee59dSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
6325342ee59dSYishai Hadas 	if (err)
63260ac8903cSJason Gunthorpe 		return err;
6327342ee59dSYishai Hadas 
6328342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
6329342ee59dSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
6330342ee59dSYishai Hadas 	if (err)
63310ac8903cSJason Gunthorpe 		return err;
6332342ee59dSYishai Hadas 
6333342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
6334342ee59dSYishai Hadas 			     &length, sizeof(length));
6335342ee59dSYishai Hadas 	return err;
6336342ee59dSYishai Hadas }
6337342ee59dSYishai Hadas 
6338342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
6339342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_ALLOC,
6340342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE,
6341342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
6342342ee59dSYishai Hadas 			UVERBS_ACCESS_NEW,
6343342ee59dSYishai Hadas 			UA_MANDATORY),
6344342ee59dSYishai Hadas 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE,
6345342ee59dSYishai Hadas 			     enum mlx5_ib_uapi_uar_alloc_type,
6346342ee59dSYishai Hadas 			     UA_MANDATORY),
6347342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
6348342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
6349342ee59dSYishai Hadas 			   UA_MANDATORY),
6350342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
6351342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
6352342ee59dSYishai Hadas 			   UA_MANDATORY),
6353342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
6354342ee59dSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
6355342ee59dSYishai Hadas 			    UA_MANDATORY));
6356342ee59dSYishai Hadas 
6357342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
6358342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_DESTROY,
6359342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_DESTROY_HANDLE,
6360342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
6361342ee59dSYishai Hadas 			UVERBS_ACCESS_DESTROY,
6362342ee59dSYishai Hadas 			UA_MANDATORY));
6363342ee59dSYishai Hadas 
6364342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_UAR,
6365342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
6366342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_ALLOC),
6367342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_DESTROY));
6368342ee59dSYishai Hadas 
63699a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
63709a119cd5SJason Gunthorpe 	mlx5_ib_dm,
63719a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
637224da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
63739a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
637424da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
637583bb4442SJason Gunthorpe 			    UA_MANDATORY),
63769a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
637724da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
63783b113a1eSAriel Levkovich 			    UA_OPTIONAL),
63793b113a1eSAriel Levkovich 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
63803b113a1eSAriel Levkovich 			     enum mlx5_ib_uapi_dm_type,
63813b113a1eSAriel Levkovich 			     UA_OPTIONAL));
638224da0016SAriel Levkovich 
63839a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
63849a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
63859a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
6386c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
6387bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
6388bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
6389c6475a0bSAviad Yehezkel 
63900fb556b2SYishai Hadas ADD_UVERBS_ATTRIBUTES_SIMPLE(
63910fb556b2SYishai Hadas 	mlx5_ib_query_context,
63920fb556b2SYishai Hadas 	UVERBS_OBJECT_DEVICE,
63930fb556b2SYishai Hadas 	UVERBS_METHOD_QUERY_CONTEXT,
63940fb556b2SYishai Hadas 	UVERBS_ATTR_PTR_OUT(
63950fb556b2SYishai Hadas 		MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
63960fb556b2SYishai Hadas 		UVERBS_ATTR_STRUCT(struct mlx5_ib_alloc_ucontext_resp,
63970fb556b2SYishai Hadas 				   dump_fill_mkey),
63980fb556b2SYishai Hadas 		UA_MANDATORY));
63990fb556b2SYishai Hadas 
64000cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
640136e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
64020cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
640330f2fe40SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_qos_defs),
640405f71ef9SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_std_types_defs),
64058c84660bSMatan Barak 
64060cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
64070cbf432dSJason Gunthorpe 				&mlx5_ib_flow_action),
64080cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
64090fb556b2SYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DEVICE, &mlx5_ib_query_context),
64107be76befSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR,
64117be76befSYishai Hadas 				UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)),
6412342ee59dSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_UAR),
64130cbf432dSJason Gunthorpe 	{}
64140cbf432dSJason Gunthorpe };
64158c84660bSMatan Barak 
64161a1e03dcSRaed Salem static int mlx5_ib_read_counters(struct ib_counters *counters,
64171a1e03dcSRaed Salem 				 struct ib_counters_read_attr *read_attr,
64181a1e03dcSRaed Salem 				 struct uverbs_attr_bundle *attrs)
64191a1e03dcSRaed Salem {
64201a1e03dcSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
64211a1e03dcSRaed Salem 	struct mlx5_read_counters_attr mread_attr = {};
64221a1e03dcSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc;
64231a1e03dcSRaed Salem 	int ret, i;
64241a1e03dcSRaed Salem 
64251a1e03dcSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
64261a1e03dcSRaed Salem 	if (mcounters->cntrs_max_index > read_attr->ncounters) {
64271a1e03dcSRaed Salem 		ret = -EINVAL;
64281a1e03dcSRaed Salem 		goto err_bound;
64291a1e03dcSRaed Salem 	}
64301a1e03dcSRaed Salem 
64311a1e03dcSRaed Salem 	mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
64321a1e03dcSRaed Salem 				 GFP_KERNEL);
64331a1e03dcSRaed Salem 	if (!mread_attr.out) {
64341a1e03dcSRaed Salem 		ret = -ENOMEM;
64351a1e03dcSRaed Salem 		goto err_bound;
64361a1e03dcSRaed Salem 	}
64371a1e03dcSRaed Salem 
64381a1e03dcSRaed Salem 	mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
64391a1e03dcSRaed Salem 	mread_attr.flags = read_attr->flags;
64401a1e03dcSRaed Salem 	ret = mcounters->read_counters(counters->device, &mread_attr);
64411a1e03dcSRaed Salem 	if (ret)
64421a1e03dcSRaed Salem 		goto err_read;
64431a1e03dcSRaed Salem 
64441a1e03dcSRaed Salem 	/* do the pass over the counters data array to assign according to the
64451a1e03dcSRaed Salem 	 * descriptions and indexing pairs
64461a1e03dcSRaed Salem 	 */
64471a1e03dcSRaed Salem 	desc = mcounters->counters_data;
64481a1e03dcSRaed Salem 	for (i = 0; i < mcounters->ncounters; i++)
64491a1e03dcSRaed Salem 		read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
64501a1e03dcSRaed Salem 
64511a1e03dcSRaed Salem err_read:
64521a1e03dcSRaed Salem 	kfree(mread_attr.out);
64531a1e03dcSRaed Salem err_bound:
64541a1e03dcSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
64551a1e03dcSRaed Salem 	return ret;
64561a1e03dcSRaed Salem }
64571a1e03dcSRaed Salem 
6458*3b023e1bSLeon Romanovsky static void mlx5_ib_destroy_counters(struct ib_counters *counters)
6459b29e2a13SRaed Salem {
6460b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
6461b29e2a13SRaed Salem 
64623b3233fbSRaed Salem 	counters_clear_description(counters);
64633b3233fbSRaed Salem 	if (mcounters->hw_cntrs_hndl)
64643b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(counters->device)->mdev,
64653b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
6466b29e2a13SRaed Salem }
6467b29e2a13SRaed Salem 
6468*3b023e1bSLeon Romanovsky static int mlx5_ib_create_counters(struct ib_counters *counters,
6469b29e2a13SRaed Salem 				   struct uverbs_attr_bundle *attrs)
6470b29e2a13SRaed Salem {
6471*3b023e1bSLeon Romanovsky 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
6472b29e2a13SRaed Salem 
64733b3233fbSRaed Salem 	mutex_init(&mcounters->mcntrs_mutex);
6474*3b023e1bSLeon Romanovsky 	return 0;
6475b29e2a13SRaed Salem }
6476b29e2a13SRaed Salem 
6477fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
6478e126ba97SEli Cohen {
647932f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
6480806b101bSJason Gunthorpe 	WARN_ON(!xa_empty(&dev->odp_mkeys));
6481806b101bSJason Gunthorpe 	cleanup_srcu_struct(&dev->odp_srcu);
64824056b12eSAriel Levkovich 
648350211ec9SJason Gunthorpe 	WARN_ON(!xa_empty(&dev->sig_mrs));
64844056b12eSAriel Levkovich 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
648516c1975fSMark Bloch }
648616c1975fSMark Bloch 
6487fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
648816c1975fSMark Bloch {
648916c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
6490e126ba97SEli Cohen 	int err;
649132f69e4bSDaniel Jurgens 	int i;
6492e126ba97SEli Cohen 
649332f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
649432f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
649595579e78SMark Bloch 		rwlock_init(&dev->port[i].roce.netdev_lock);
6496d3b5cc1cSMark Bloch 		dev->port[i].roce.dev = dev;
6497d3b5cc1cSMark Bloch 		dev->port[i].roce.native_port_num = i + 1;
6498d3b5cc1cSMark Bloch 		dev->port[i].roce.last_port_state = IB_PORT_DOWN;
649932f69e4bSDaniel Jurgens 	}
650032f69e4bSDaniel Jurgens 
650100815752SMoni Shoua 	mlx5_ib_internal_fill_odp_caps(dev);
650200815752SMoni Shoua 
650332f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
6504e126ba97SEli Cohen 	if (err)
6505da796ccbSMark Bloch 		return err;
6506e126ba97SEli Cohen 
6507a989ea01SMark Bloch 	err = set_has_smi_cap(dev);
6508a989ea01SMark Bloch 	if (err)
6509a989ea01SMark Bloch 		return err;
6510e126ba97SEli Cohen 
651132f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
651232f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
651332f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
651432f69e4bSDaniel Jurgens 			if (err)
651532f69e4bSDaniel Jurgens 				break;
651632f69e4bSDaniel Jurgens 		}
651732f69e4bSDaniel Jurgens 	} else {
651832f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
651932f69e4bSDaniel Jurgens 	}
652032f69e4bSDaniel Jurgens 	if (err)
652132f69e4bSDaniel Jurgens 		goto err_mp;
652232f69e4bSDaniel Jurgens 
65231b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
6524e126ba97SEli Cohen 		get_ext_port_caps(dev);
6525e126ba97SEli Cohen 
6526e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
6527c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
6528e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
6529f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
6530c42260f1SVu Pham 	dev->ib_dev.dev.parent		= mdev->device;
6531cfc1a89eSMaor Gottlieb 	dev->ib_dev.lag_flags		= RDMA_LAG_FLAGS_HASH_ALL_SLAVES;
6532e126ba97SEli Cohen 
65333cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
65343cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
65353cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
6536806b101bSJason Gunthorpe 	xa_init(&dev->odp_mkeys);
653750211ec9SJason Gunthorpe 	xa_init(&dev->sig_mrs);
6538f743ff3bSSaeed Mahameed 	atomic_set(&dev->mkey_var, 0);
65393cc297dbSMark Bloch 
65403b113a1eSAriel Levkovich 	spin_lock_init(&dev->dm.lock);
65413b113a1eSAriel Levkovich 	dev->dm.dev = mdev;
654224da0016SAriel Levkovich 
6543806b101bSJason Gunthorpe 	err = init_srcu_struct(&dev->odp_srcu);
6544a6bc3875SMoni Shoua 	if (err)
6545c9b9dcb4SAriel Levkovich 		goto err_mp;
65463cc297dbSMark Bloch 
654716c1975fSMark Bloch 	return 0;
654825c13324SAriel Levkovich 
654932f69e4bSDaniel Jurgens err_mp:
655032f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
655116c1975fSMark Bloch 
655216c1975fSMark Bloch 	return -ENOMEM;
655316c1975fSMark Bloch }
655416c1975fSMark Bloch 
65559a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
65569a4ca38dSMark Bloch {
65579a4ca38dSMark Bloch 	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
65589a4ca38dSMark Bloch 
65599a4ca38dSMark Bloch 	if (!dev->flow_db)
65609a4ca38dSMark Bloch 		return -ENOMEM;
65619a4ca38dSMark Bloch 
65629a4ca38dSMark Bloch 	mutex_init(&dev->flow_db->lock);
65639a4ca38dSMark Bloch 
65649a4ca38dSMark Bloch 	return 0;
65659a4ca38dSMark Bloch }
65669a4ca38dSMark Bloch 
65679a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
65689a4ca38dSMark Bloch {
65699a4ca38dSMark Bloch 	kfree(dev->flow_db);
65709a4ca38dSMark Bloch }
65719a4ca38dSMark Bloch 
657296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
65737a154142SJason Gunthorpe 	.owner = THIS_MODULE,
6574b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_MLX5,
657572c6ec18SJason Gunthorpe 	.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION,
6576b9560a41SJason Gunthorpe 
657796458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
657896458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
65796c984472SMax Gurtovoy 	.alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
658096458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
658196458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
658296458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
658396458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
658496458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
658596458233SKamal Heib 	.create_counters = mlx5_ib_create_counters,
658696458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
658796458233SKamal Heib 	.create_flow = mlx5_ib_create_flow,
658896458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
658996458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
659096458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
659196458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
659296458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
659396458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
659496458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
659596458233SKamal Heib 	.destroy_counters = mlx5_ib_destroy_counters,
659696458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
659796458233SKamal Heib 	.destroy_flow = mlx5_ib_destroy_flow,
659896458233SKamal Heib 	.destroy_flow_action = mlx5_ib_destroy_flow_action,
659996458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
660096458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
660196458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
660296458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
660396458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
660496458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
660511f552e2SMichael Guralnik 	.enable_driver = mlx5_ib_enable_driver,
66061ccecc88SMaor Gottlieb 	.fill_res_cq_entry_raw = mlx5_ib_fill_res_cq_entry_raw,
6607f4434529SMaor Gottlieb 	.fill_res_mr_entry = mlx5_ib_fill_res_mr_entry,
660828b5fa68SMaor Gottlieb 	.fill_res_mr_entry_raw = mlx5_ib_fill_res_mr_entry_raw,
66091776dd23SMaor Gottlieb 	.fill_res_qp_entry_raw = mlx5_ib_fill_res_qp_entry_raw,
6610f4434529SMaor Gottlieb 	.fill_stat_mr_entry = mlx5_ib_fill_stat_mr_entry,
661196458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
661296458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
661396458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
661496458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
66156c984472SMax Gurtovoy 	.map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
661696458233SKamal Heib 	.mmap = mlx5_ib_mmap,
6617dc2316ebSYishai Hadas 	.mmap_free = mlx5_ib_mmap_free,
661896458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
661996458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
662096458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
662196458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
662296458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
662396458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
6624029e88fdSLeon Romanovsky 	.post_recv = mlx5_ib_post_recv_nodrain,
6625029e88fdSLeon Romanovsky 	.post_send = mlx5_ib_post_send_nodrain,
662696458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
662796458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
662896458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
662996458233SKamal Heib 	.query_device = mlx5_ib_query_device,
663096458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
663196458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
663296458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
663396458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
66340fb556b2SYishai Hadas 	.query_ucontext = mlx5_ib_query_ucontext,
663596458233SKamal Heib 	.read_counters = mlx5_ib_read_counters,
663696458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
663796458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
663896458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
663996458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
6640d3456914SLeon Romanovsky 
6641d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
6642*3b023e1bSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_counters, mlx5_ib_mcounters, ibcntrs),
6643e39afe3dSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
664421a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
664568e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
6646a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
664796458233SKamal Heib };
664896458233SKamal Heib 
664996458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
665096458233SKamal Heib 	.create_flow_action_esp = mlx5_ib_create_flow_action_esp,
665196458233SKamal Heib 	.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
665296458233SKamal Heib };
665396458233SKamal Heib 
665496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
665596458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
665696458233SKamal Heib };
665796458233SKamal Heib 
665896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
665996458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
66609c0015efSDanit Goldberg 	.get_vf_guid = mlx5_ib_get_vf_guid,
666196458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
666296458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
666396458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
666496458233SKamal Heib };
666596458233SKamal Heib 
666696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
666796458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
666896458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
666996458233SKamal Heib };
667096458233SKamal Heib 
667196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
667296458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
667396458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
667496458233SKamal Heib };
667596458233SKamal Heib 
667696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
667796458233SKamal Heib 	.alloc_dm = mlx5_ib_alloc_dm,
667896458233SKamal Heib 	.dealloc_dm = mlx5_ib_dealloc_dm,
667996458233SKamal Heib 	.reg_dm_mr = mlx5_ib_reg_dm_mr,
668096458233SKamal Heib };
668196458233SKamal Heib 
6682f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev)
6683f164be8cSYishai Hadas {
6684f164be8cSYishai Hadas 	struct mlx5_core_dev *mdev = dev->mdev;
6685f164be8cSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
6686f164be8cSYishai Hadas 	u8 log_doorbell_bar_size;
6687f164be8cSYishai Hadas 	u8 log_doorbell_stride;
6688f164be8cSYishai Hadas 	u64 bar_size;
6689f164be8cSYishai Hadas 
6690f164be8cSYishai Hadas 	log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
6691f164be8cSYishai Hadas 					log_doorbell_bar_size);
6692f164be8cSYishai Hadas 	log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
6693f164be8cSYishai Hadas 					log_doorbell_stride);
6694f164be8cSYishai Hadas 	var_table->hw_start_addr = dev->mdev->bar_addr +
6695f164be8cSYishai Hadas 				MLX5_CAP64_DEV_VDPA_EMULATION(mdev,
6696f164be8cSYishai Hadas 					doorbell_bar_offset);
6697f164be8cSYishai Hadas 	bar_size = (1ULL << log_doorbell_bar_size) * 4096;
6698f164be8cSYishai Hadas 	var_table->stride_size = 1ULL << log_doorbell_stride;
669991b74bf5SAlexander Lobakin 	var_table->num_var_hw_entries = div_u64(bar_size,
670091b74bf5SAlexander Lobakin 						var_table->stride_size);
6701f164be8cSYishai Hadas 	mutex_init(&var_table->bitmap_lock);
6702f164be8cSYishai Hadas 	var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries,
6703f164be8cSYishai Hadas 					  GFP_KERNEL);
6704f164be8cSYishai Hadas 	return (var_table->bitmap) ? 0 : -ENOMEM;
6705f164be8cSYishai Hadas }
6706f164be8cSYishai Hadas 
6707f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev)
6708f164be8cSYishai Hadas {
6709f164be8cSYishai Hadas 	bitmap_free(dev->var_table.bitmap);
6710f164be8cSYishai Hadas }
6711f164be8cSYishai Hadas 
6712fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
671316c1975fSMark Bloch {
671416c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
671516c1975fSMark Bloch 	int err;
671616c1975fSMark Bloch 
6717e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
6718e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
6719e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
6720e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
6721e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
6722e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
672341c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
672441c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
6725e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
672656e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
6727e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
6728e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
6729e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
6730e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
6731e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
6732e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
6733e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
6734e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
6735e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
6736e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
6737e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
6738e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
6739e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
6740e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
6741e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
6742e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
6743e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
67441707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
6745d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
6746d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
67477d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
6748b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
674996458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ)	|
675096458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW)	|
675196458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
6752e126ba97SEli Cohen 
6753f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
6754f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
675596458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
675696458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
67578e959601SNiranjana Vishwanathapura 
675896458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
675996458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
67607c2344c3SMaor Gottlieb 
67616e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
67626e8484c5SMax Gurtovoy 
6763d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
6764d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
6765d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
6766d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
676796458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
6768d2370e0aSMatan Barak 	}
6769d2370e0aSMatan Barak 
6770938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
6771e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
6772e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
6773e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
677496458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
6775e126ba97SEli Cohen 	}
6776e126ba97SEli Cohen 
677725c13324SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic) ||
677825c13324SAriel Levkovich 	    MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
677925c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
678096458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
678124da0016SAriel Levkovich 
6782dfb631a1SJason Gunthorpe 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
678396458233SKamal Heib 	    MLX5_ACCEL_IPSEC_CAP_DEVICE)
678496458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
678596458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
678681e30880SYishai Hadas 
678736e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
678836e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
678981e30880SYishai Hadas 
6790e126ba97SEli Cohen 	err = init_node_data(dev);
6791e126ba97SEli Cohen 	if (err)
679216c1975fSMark Bloch 		return err;
6793e126ba97SEli Cohen 
6794c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
6795e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6796e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
6797a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
6798c8b89924SMark Bloch 
6799f164be8cSYishai Hadas 	if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
6800f164be8cSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) {
6801f164be8cSYishai Hadas 		err = mlx5_ib_init_var_table(dev);
6802f164be8cSYishai Hadas 		if (err)
6803f164be8cSYishai Hadas 			return err;
6804f164be8cSYishai Hadas 	}
6805f164be8cSYishai Hadas 
680696e2fd73SLeon Romanovsky 	dev->ib_dev.use_cq_dim = true;
680796e2fd73SLeon Romanovsky 
680816c1975fSMark Bloch 	return 0;
680916c1975fSMark Bloch }
681016c1975fSMark Bloch 
681196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
681296458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
681396458233SKamal Heib 	.query_port = mlx5_ib_query_port,
681496458233SKamal Heib };
681596458233SKamal Heib 
68168e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
681716c1975fSMark Bloch {
681896458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
68198e6efa3aSMark Bloch 	return 0;
68208e6efa3aSMark Bloch }
68218e6efa3aSMark Bloch 
682296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
682396458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
682496458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
682596458233SKamal Heib };
682696458233SKamal Heib 
6827b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev)
68288e6efa3aSMark Bloch {
682996458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
68308e6efa3aSMark Bloch 	return 0;
68318e6efa3aSMark Bloch }
68328e6efa3aSMark Bloch 
683396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
683496458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
683596458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
683696458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
683796458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
683896458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
683996458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
684096458233SKamal Heib };
684196458233SKamal Heib 
6842e3f1ed1fSLeon Romanovsky static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
68438e6efa3aSMark Bloch {
6844e3f1ed1fSLeon Romanovsky 	u8 port_num;
68457fd8aefbSDaniel Jurgens 
6846c11a226aSMark Bloch 	dev->ib_dev.uverbs_ex_cmd_mask |=
6847c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6848c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6849c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6850c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6851c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
685296458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
68538e6efa3aSMark Bloch 
6854e3f1ed1fSLeon Romanovsky 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6855e3f1ed1fSLeon Romanovsky 
685626628e2dSMark Bloch 	/* Register only for native ports */
68578e6efa3aSMark Bloch 	return mlx5_add_netdev_notifier(dev, port_num);
68588e6efa3aSMark Bloch }
68598e6efa3aSMark Bloch 
68608e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
68618e6efa3aSMark Bloch {
68628e6efa3aSMark Bloch 	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
68638e6efa3aSMark Bloch 
68648e6efa3aSMark Bloch 	mlx5_remove_netdev_notifier(dev, port_num);
68658e6efa3aSMark Bloch }
68668e6efa3aSMark Bloch 
6867b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_roce_init(struct mlx5_ib_dev *dev)
68688e6efa3aSMark Bloch {
68698e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
68708e6efa3aSMark Bloch 	enum rdma_link_layer ll;
68718e6efa3aSMark Bloch 	int port_type_cap;
68728e6efa3aSMark Bloch 	int err = 0;
68738e6efa3aSMark Bloch 
68748e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
68758e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
68768e6efa3aSMark Bloch 
68778e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET)
6878e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
68798e6efa3aSMark Bloch 
688016c1975fSMark Bloch 	return err;
6881fc24fc5eSAchiad Shochat }
6882fc24fc5eSAchiad Shochat 
6883b5a498baSMichael Guralnik static void mlx5_ib_stage_raw_eth_roce_cleanup(struct mlx5_ib_dev *dev)
68848e6efa3aSMark Bloch {
68858e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
68868e6efa3aSMark Bloch }
68878e6efa3aSMark Bloch 
68888e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
68898e6efa3aSMark Bloch {
68908e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
68918e6efa3aSMark Bloch 	enum rdma_link_layer ll;
68928e6efa3aSMark Bloch 	int port_type_cap;
68938e6efa3aSMark Bloch 	int err;
68948e6efa3aSMark Bloch 
68958e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
68968e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
68978e6efa3aSMark Bloch 
68988e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
6899e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
69008e6efa3aSMark Bloch 		if (err)
69018e6efa3aSMark Bloch 			return err;
69028e6efa3aSMark Bloch 
6903e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
69048e6efa3aSMark Bloch 		if (err)
69058e6efa3aSMark Bloch 			goto cleanup;
69068e6efa3aSMark Bloch 	}
69078e6efa3aSMark Bloch 
690816c1975fSMark Bloch 	return 0;
69098e6efa3aSMark Bloch cleanup:
69108e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
69118e6efa3aSMark Bloch 
69128e6efa3aSMark Bloch 	return err;
691345bded2cSKamal Heib }
69146aec21f6SHaggai Eran 
691516c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
691616c1975fSMark Bloch {
691716c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
691816c1975fSMark Bloch 	enum rdma_link_layer ll;
691916c1975fSMark Bloch 	int port_type_cap;
69204a2da0b8SParav Pandit 
692116c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
692216c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
692316c1975fSMark Bloch 
692416c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
692516c1975fSMark Bloch 		mlx5_disable_eth(dev);
69268e6efa3aSMark Bloch 		mlx5_ib_stage_common_roce_cleanup(dev);
692716c1975fSMark Bloch 	}
692816c1975fSMark Bloch }
692916c1975fSMark Bloch 
6930fb652d32SMark Bloch static int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
693116c1975fSMark Bloch {
693216c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
693316c1975fSMark Bloch }
693416c1975fSMark Bloch 
6935fb652d32SMark Bloch static void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
693616c1975fSMark Bloch {
693716c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
693816c1975fSMark Bloch }
693916c1975fSMark Bloch 
694016c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
694116c1975fSMark Bloch {
694216c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
694316c1975fSMark Bloch }
694416c1975fSMark Bloch 
6945f3ffed0cSKamal Heib static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
6946d5d284b8SSaeed Mahameed {
6947d5d284b8SSaeed Mahameed 	mlx5_ib_odp_cleanup_one(dev);
6948d5d284b8SSaeed Mahameed }
6949d5d284b8SSaeed Mahameed 
695096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
695196458233SKamal Heib 	.alloc_hw_stats = mlx5_ib_alloc_hw_stats,
695296458233SKamal Heib 	.get_hw_stats = mlx5_ib_get_hw_stats,
695345842fc6SMark Zhang 	.counter_bind_qp = mlx5_ib_counter_bind_qp,
695445842fc6SMark Zhang 	.counter_unbind_qp = mlx5_ib_counter_unbind_qp,
695545842fc6SMark Zhang 	.counter_dealloc = mlx5_ib_counter_dealloc,
695618d422ceSMark Zhang 	.counter_alloc_stats = mlx5_ib_counter_alloc_stats,
695718d422ceSMark Zhang 	.counter_update_stats = mlx5_ib_counter_update_stats,
695896458233SKamal Heib };
695996458233SKamal Heib 
6960fb652d32SMark Bloch static int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
696116c1975fSMark Bloch {
69625e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
696396458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
69645e1e7612SMark Bloch 
696516c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
69665e1e7612SMark Bloch 	}
696716c1975fSMark Bloch 
696816c1975fSMark Bloch 	return 0;
696916c1975fSMark Bloch }
697016c1975fSMark Bloch 
6971fb652d32SMark Bloch static void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
697216c1975fSMark Bloch {
697316c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
697416c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
697516c1975fSMark Bloch }
697616c1975fSMark Bloch 
697716c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
697816c1975fSMark Bloch {
697973eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
6980a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
698173eb8f03SGreg Kroah-Hartman 	return 0;
698216c1975fSMark Bloch }
698316c1975fSMark Bloch 
698416c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
698516c1975fSMark Bloch {
6986a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
6987a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
698816c1975fSMark Bloch }
698916c1975fSMark Bloch 
699016c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
699116c1975fSMark Bloch {
69925fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
6993444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
699416c1975fSMark Bloch }
699516c1975fSMark Bloch 
699616c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
699716c1975fSMark Bloch {
699816c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
699916c1975fSMark Bloch }
700016c1975fSMark Bloch 
7001fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
700216c1975fSMark Bloch {
700316c1975fSMark Bloch 	int err;
70045fe9dec0SEli Cohen 
70055fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
70065fe9dec0SEli Cohen 	if (err)
700716c1975fSMark Bloch 		return err;
70085fe9dec0SEli Cohen 
70095fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
70105fe9dec0SEli Cohen 	if (err)
701116c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
70125fe9dec0SEli Cohen 
701316c1975fSMark Bloch 	return err;
701416c1975fSMark Bloch }
70150837e86aSMark Bloch 
7016fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
701716c1975fSMark Bloch {
701816c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
701916c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
702016c1975fSMark Bloch }
7021e126ba97SEli Cohen 
7022fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
702316c1975fSMark Bloch {
7024e349f858SJason Gunthorpe 	const char *name;
7025e349f858SJason Gunthorpe 
7026508a523fSParav Pandit 	rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
70277c34ec19SAviv Heller 	if (!mlx5_lag_is_roce(dev->mdev))
7028e349f858SJason Gunthorpe 		name = "mlx5_%d";
7029e349f858SJason Gunthorpe 	else
7030e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
7031ea4baf7fSParav Pandit 	return ib_register_device(&dev->ib_dev, name);
703216c1975fSMark Bloch }
703316c1975fSMark Bloch 
7034fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
703542cea83fSMark Bloch {
703642cea83fSMark Bloch 	destroy_umrc_res(dev);
703742cea83fSMark Bloch }
703842cea83fSMark Bloch 
7039fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
704016c1975fSMark Bloch {
704116c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
704216c1975fSMark Bloch }
704316c1975fSMark Bloch 
7044fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
704516c1975fSMark Bloch {
704616c1975fSMark Bloch 	return create_umr_res(dev);
704716c1975fSMark Bloch }
704816c1975fSMark Bloch 
704916c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
705016c1975fSMark Bloch {
705103404e8aSMaor Gottlieb 	init_delay_drop(dev);
705203404e8aSMaor Gottlieb 
705316c1975fSMark Bloch 	return 0;
705416c1975fSMark Bloch }
705516c1975fSMark Bloch 
705616c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
705716c1975fSMark Bloch {
705816c1975fSMark Bloch 	cancel_delay_drop(dev);
705916c1975fSMark Bloch }
706016c1975fSMark Bloch 
7061df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
7062df097a27SSaeed Mahameed {
7063df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
7064df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
7065df097a27SSaeed Mahameed 	return 0;
7066df097a27SSaeed Mahameed }
7067df097a27SSaeed Mahameed 
7068df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
7069df097a27SSaeed Mahameed {
7070df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
7071df097a27SSaeed Mahameed }
7072df097a27SSaeed Mahameed 
707381773ce5SLeon Romanovsky static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
707481773ce5SLeon Romanovsky {
707581773ce5SLeon Romanovsky 	int uid;
707681773ce5SLeon Romanovsky 
7077fb98153bSYishai Hadas 	uid = mlx5_ib_devx_create(dev, false);
7078e337dd53SYishai Hadas 	if (uid > 0) {
707981773ce5SLeon Romanovsky 		dev->devx_whitelist_uid = uid;
7080e337dd53SYishai Hadas 		mlx5_ib_devx_init_event_table(dev);
7081e337dd53SYishai Hadas 	}
708281773ce5SLeon Romanovsky 
708381773ce5SLeon Romanovsky 	return 0;
708481773ce5SLeon Romanovsky }
708581773ce5SLeon Romanovsky static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
708681773ce5SLeon Romanovsky {
7087e337dd53SYishai Hadas 	if (dev->devx_whitelist_uid) {
7088e337dd53SYishai Hadas 		mlx5_ib_devx_cleanup_event_table(dev);
708981773ce5SLeon Romanovsky 		mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
709081773ce5SLeon Romanovsky 	}
7091e337dd53SYishai Hadas }
709281773ce5SLeon Romanovsky 
709311f552e2SMichael Guralnik int mlx5_ib_enable_driver(struct ib_device *dev)
709411f552e2SMichael Guralnik {
709511f552e2SMichael Guralnik 	struct mlx5_ib_dev *mdev = to_mdev(dev);
709611f552e2SMichael Guralnik 	int ret;
709711f552e2SMichael Guralnik 
709811f552e2SMichael Guralnik 	ret = mlx5_ib_test_wc(mdev);
709911f552e2SMichael Guralnik 	mlx5_ib_dbg(mdev, "Write-Combining %s",
710011f552e2SMichael Guralnik 		    mdev->wc_support ? "supported" : "not supported");
710111f552e2SMichael Guralnik 
710211f552e2SMichael Guralnik 	return ret;
710311f552e2SMichael Guralnik }
710411f552e2SMichael Guralnik 
7105b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
710616c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
710716c1975fSMark Bloch 		      int stage)
710816c1975fSMark Bloch {
71094cca96a8SParav Pandit 	dev->ib_active = false;
71104cca96a8SParav Pandit 
711116c1975fSMark Bloch 	/* Number of stages to cleanup */
711216c1975fSMark Bloch 	while (stage) {
711316c1975fSMark Bloch 		stage--;
711416c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
711516c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
711616c1975fSMark Bloch 	}
71174a6dc855SMark Bloch 
7118da796ccbSMark Bloch 	kfree(dev->port);
71194a6dc855SMark Bloch 	ib_dealloc_device(&dev->ib_dev);
712016c1975fSMark Bloch }
712116c1975fSMark Bloch 
7122b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
712316c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
712416c1975fSMark Bloch {
712516c1975fSMark Bloch 	int err;
712616c1975fSMark Bloch 	int i;
712716c1975fSMark Bloch 
71288d93efb8SMark Bloch 	dev->profile = profile;
71298d93efb8SMark Bloch 
713016c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
713116c1975fSMark Bloch 		if (profile->stage[i].init) {
713216c1975fSMark Bloch 			err = profile->stage[i].init(dev);
713316c1975fSMark Bloch 			if (err)
713416c1975fSMark Bloch 				goto err_out;
713516c1975fSMark Bloch 		}
713616c1975fSMark Bloch 	}
713716c1975fSMark Bloch 
7138e126ba97SEli Cohen 	dev->ib_active = true;
7139e126ba97SEli Cohen 
71409603b61dSJack Morgenstein 	return dev;
7141e126ba97SEli Cohen 
714216c1975fSMark Bloch err_out:
714316c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
7144e126ba97SEli Cohen 
71459603b61dSJack Morgenstein 	return NULL;
7146e126ba97SEli Cohen }
7147e126ba97SEli Cohen 
714816c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
714916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
715016c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
715116c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
71529a4ca38dSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
71539a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_init,
71549a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
715516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
715616c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
7157f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
71588e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
71598e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
71608e6efa3aSMark Bloch 		     NULL),
716116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
716216c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
716316c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
7164333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
7165333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
7166333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
7167f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
7168f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
7169f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
717016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
717116c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
717216c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
7173df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
7174df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
7175df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
717616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
717716c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
7178d5d284b8SSaeed Mahameed 		     mlx5_ib_stage_odp_cleanup),
717916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
718016c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
718116c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
718216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
718316c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
718416c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
718516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
718616c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
718716c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
718816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
718916c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
719016c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
719142cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
719242cea83fSMark Bloch 		     NULL,
719342cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
719481773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
719581773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_init,
719681773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_cleanup),
719716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
719816c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
719916c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
720042cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
720142cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
720242cea83fSMark Bloch 		     NULL),
720316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
720416c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
720516c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
720616c1975fSMark Bloch };
720716c1975fSMark Bloch 
7208b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = {
7209b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
7210b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
7211b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
7212b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
7213b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_init,
7214b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
7215b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
7216b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
7217f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
7218b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
7219b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_non_default_cb,
7220b5ca15adSMark Bloch 		     NULL),
7221b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
7222b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_roce_init,
7223b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_roce_cleanup),
7224333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
7225333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
7226333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
7227f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
7228f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
7229f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
7230b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
7231b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_init,
7232b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
7233df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
7234df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
7235df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
7236b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
7237b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_init,
7238b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
723979db784eSParav Pandit 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
724079db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_init,
724179db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_cleanup),
7242b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
7243b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
7244b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
7245b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
7246b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
7247b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
724803fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
724903fe2debSDavid S. Miller 		     NULL,
725003fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
72517f575103SMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
72527f575103SMark Bloch 		     mlx5_ib_stage_devx_init,
72537f575103SMark Bloch 		     mlx5_ib_stage_devx_cleanup),
7254b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
7255b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
7256b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
725703fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
725803fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
725903fe2debSDavid S. Miller 		     NULL),
7260b5ca15adSMark Bloch };
7261b5ca15adSMark Bloch 
7262e3f1ed1fSLeon Romanovsky static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
726332f69e4bSDaniel Jurgens {
726432f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
726532f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
726632f69e4bSDaniel Jurgens 	bool bound = false;
726732f69e4bSDaniel Jurgens 	int err;
726832f69e4bSDaniel Jurgens 
726932f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
727032f69e4bSDaniel Jurgens 	if (!mpi)
727132f69e4bSDaniel Jurgens 		return NULL;
727232f69e4bSDaniel Jurgens 
727332f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
727432f69e4bSDaniel Jurgens 
727532f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
727632f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
727732f69e4bSDaniel Jurgens 	if (err) {
727832f69e4bSDaniel Jurgens 		kfree(mpi);
727932f69e4bSDaniel Jurgens 		return NULL;
728032f69e4bSDaniel Jurgens 	}
728132f69e4bSDaniel Jurgens 
728232f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
728332f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
728432f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
728532f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
728632f69e4bSDaniel Jurgens 
728732f69e4bSDaniel Jurgens 		if (bound) {
728832f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
728932f69e4bSDaniel Jurgens 			break;
729032f69e4bSDaniel Jurgens 		}
729132f69e4bSDaniel Jurgens 	}
729232f69e4bSDaniel Jurgens 
729332f69e4bSDaniel Jurgens 	if (!bound) {
729432f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
7295c42260f1SVu Pham 		dev_dbg(mdev->device,
7296c42260f1SVu Pham 			"no suitable IB device found to bind to, added to unaffiliated list.\n");
729732f69e4bSDaniel Jurgens 	}
729832f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
729932f69e4bSDaniel Jurgens 
730032f69e4bSDaniel Jurgens 	return mpi;
730132f69e4bSDaniel Jurgens }
730232f69e4bSDaniel Jurgens 
730316c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
730416c1975fSMark Bloch {
730594de879cSMichael Guralnik 	const struct mlx5_ib_profile *profile;
730632f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
7307b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
730832f69e4bSDaniel Jurgens 	int port_type_cap;
7309da796ccbSMark Bloch 	int num_ports;
731032f69e4bSDaniel Jurgens 
7311f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) &&
7312f6455de0SBodong Wang 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == MLX5_ESWITCH_OFFLOADS) {
73135fb58c9eSMark Bloch 		if (!mlx5_core_mp_enabled(mdev))
7314f0666f1fSBodong Wang 			mlx5_ib_register_vport_reps(mdev);
7315f0666f1fSBodong Wang 		return mdev;
7316f0666f1fSBodong Wang 	}
7317f0666f1fSBodong Wang 
731832f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
731932f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
732032f69e4bSDaniel Jurgens 
7321e3f1ed1fSLeon Romanovsky 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
7322e3f1ed1fSLeon Romanovsky 		return mlx5_ib_add_slave_port(mdev);
732332f69e4bSDaniel Jurgens 
7324da796ccbSMark Bloch 	num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
7325da796ccbSMark Bloch 			MLX5_CAP_GEN(mdev, num_vhca_ports));
7326459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
7327b5ca15adSMark Bloch 	if (!dev)
7328b5ca15adSMark Bloch 		return NULL;
7329da796ccbSMark Bloch 	dev->port = kcalloc(num_ports, sizeof(*dev->port),
7330da796ccbSMark Bloch 			     GFP_KERNEL);
7331da796ccbSMark Bloch 	if (!dev->port) {
7332a5c9c299SParav Pandit 		ib_dealloc_device(&dev->ib_dev);
7333da796ccbSMark Bloch 		return NULL;
7334da796ccbSMark Bloch 	}
7335b5ca15adSMark Bloch 
7336b5ca15adSMark Bloch 	dev->mdev = mdev;
7337da796ccbSMark Bloch 	dev->num_ports = num_ports;
7338b5ca15adSMark Bloch 
733994de879cSMichael Guralnik 	if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_is_roce_enabled(mdev))
734094de879cSMichael Guralnik 		profile = &raw_eth_profile;
734194de879cSMichael Guralnik 	else
734294de879cSMichael Guralnik 		profile = &pf_profile;
734394de879cSMichael Guralnik 
734494de879cSMichael Guralnik 	return __mlx5_ib_add(dev, profile);
734516c1975fSMark Bloch }
734616c1975fSMark Bloch 
73479603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
7348e126ba97SEli Cohen {
734932f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
735032f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
73516aec21f6SHaggai Eran 
7352f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
7353f0666f1fSBodong Wang 		mlx5_ib_unregister_vport_reps(mdev);
7354f0666f1fSBodong Wang 		return;
7355f0666f1fSBodong Wang 	}
7356f0666f1fSBodong Wang 
735732f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
735832f69e4bSDaniel Jurgens 		mpi = context;
735932f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
736032f69e4bSDaniel Jurgens 		if (mpi->ibdev)
736132f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
736232f69e4bSDaniel Jurgens 		list_del(&mpi->list);
736332f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
73645d44adebSDanit Goldberg 		kfree(mpi);
736532f69e4bSDaniel Jurgens 		return;
736632f69e4bSDaniel Jurgens 	}
736732f69e4bSDaniel Jurgens 
736832f69e4bSDaniel Jurgens 	dev = context;
736916c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
7370e126ba97SEli Cohen }
7371e126ba97SEli Cohen 
73729603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
73739603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
73749603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
737564613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
7376e126ba97SEli Cohen };
7377e126ba97SEli Cohen 
7378c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
7379c44ef998SIlya Lesokhin {
7380c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
7381c44ef998SIlya Lesokhin 	return xlt_emergency_page;
7382c44ef998SIlya Lesokhin }
7383c44ef998SIlya Lesokhin 
7384c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
7385c44ef998SIlya Lesokhin {
7386c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
7387c44ef998SIlya Lesokhin }
7388c44ef998SIlya Lesokhin 
7389e126ba97SEli Cohen static int __init mlx5_ib_init(void)
7390e126ba97SEli Cohen {
73916aec21f6SHaggai Eran 	int err;
73926aec21f6SHaggai Eran 
7393c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
7394c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
7395d69a24e0SDaniel Jurgens 		return -ENOMEM;
7396d69a24e0SDaniel Jurgens 
7397c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
7398c44ef998SIlya Lesokhin 
7399c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
7400c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
7401c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
7402c44ef998SIlya Lesokhin 		return -ENOMEM;
7403c44ef998SIlya Lesokhin 	}
7404c44ef998SIlya Lesokhin 
740581713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
74069603b61dSJack Morgenstein 
74076aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
74086aec21f6SHaggai Eran 
74096aec21f6SHaggai Eran 	return err;
7410e126ba97SEli Cohen }
7411e126ba97SEli Cohen 
7412e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
7413e126ba97SEli Cohen {
74149603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
7415d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
7416c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
7417c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
7418e126ba97SEli Cohen }
7419e126ba97SEli Cohen 
7420e126ba97SEli Cohen module_init(mlx5_ib_init);
7421e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
7422