xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 45ec21c971eddfb5e8e953e49a9dbe780f4a4997)
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 
1768*45ec21c9SYishai Hadas static int set_ucontext_resp(struct ib_ucontext *uctx,
1769*45ec21c9SYishai Hadas 			     struct mlx5_ib_alloc_ucontext_resp *resp)
1770*45ec21c9SYishai Hadas {
1771*45ec21c9SYishai Hadas 	struct ib_device *ibdev = uctx->device;
1772*45ec21c9SYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1773*45ec21c9SYishai Hadas 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
1774*45ec21c9SYishai Hadas 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
1775*45ec21c9SYishai Hadas 	int err;
1776*45ec21c9SYishai Hadas 
1777*45ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1778*45ec21c9SYishai Hadas 		err = mlx5_cmd_dump_fill_mkey(dev->mdev,
1779*45ec21c9SYishai Hadas 					      &resp->dump_fill_mkey);
1780*45ec21c9SYishai Hadas 		if (err)
1781*45ec21c9SYishai Hadas 			return err;
1782*45ec21c9SYishai Hadas 		resp->comp_mask |=
1783*45ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
1784*45ec21c9SYishai Hadas 	}
1785*45ec21c9SYishai Hadas 
1786*45ec21c9SYishai Hadas 	resp->qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
1787*45ec21c9SYishai Hadas 	if (dev->wc_support)
1788*45ec21c9SYishai Hadas 		resp->bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev,
1789*45ec21c9SYishai Hadas 						      log_bf_reg_size);
1790*45ec21c9SYishai Hadas 	resp->cache_line_size = cache_line_size();
1791*45ec21c9SYishai Hadas 	resp->max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1792*45ec21c9SYishai Hadas 	resp->max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1793*45ec21c9SYishai Hadas 	resp->max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1794*45ec21c9SYishai Hadas 	resp->max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1795*45ec21c9SYishai Hadas 	resp->max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1796*45ec21c9SYishai Hadas 	resp->cqe_version = context->cqe_version;
1797*45ec21c9SYishai Hadas 	resp->log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1798*45ec21c9SYishai Hadas 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1799*45ec21c9SYishai Hadas 	resp->num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1800*45ec21c9SYishai Hadas 					MLX5_CAP_GEN(dev->mdev,
1801*45ec21c9SYishai Hadas 						     num_of_uars_per_page) : 1;
1802*45ec21c9SYishai Hadas 
1803*45ec21c9SYishai Hadas 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
1804*45ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1805*45ec21c9SYishai Hadas 		if (mlx5_get_flow_namespace(dev->mdev,
1806*45ec21c9SYishai Hadas 				MLX5_FLOW_NAMESPACE_EGRESS))
1807*45ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1808*45ec21c9SYishai Hadas 		if (mlx5_accel_ipsec_device_caps(dev->mdev) &
1809*45ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1810*45ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1811*45ec21c9SYishai Hadas 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1812*45ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1813*45ec21c9SYishai Hadas 		if (mlx5_accel_ipsec_device_caps(dev->mdev) &
1814*45ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1815*45ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1816*45ec21c9SYishai Hadas 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1817*45ec21c9SYishai Hadas 	}
1818*45ec21c9SYishai Hadas 
1819*45ec21c9SYishai Hadas 	resp->tot_bfregs = bfregi->lib_uar_dyn ? 0 :
1820*45ec21c9SYishai Hadas 			bfregi->total_num_bfregs - bfregi->num_dyn_bfregs;
1821*45ec21c9SYishai Hadas 	resp->num_ports = dev->num_ports;
1822*45ec21c9SYishai Hadas 	resp->cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1823*45ec21c9SYishai Hadas 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1824*45ec21c9SYishai Hadas 
1825*45ec21c9SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1826*45ec21c9SYishai Hadas 		mlx5_query_min_inline(dev->mdev, &resp->eth_min_inline);
1827*45ec21c9SYishai Hadas 		resp->eth_min_inline++;
1828*45ec21c9SYishai Hadas 	}
1829*45ec21c9SYishai Hadas 
1830*45ec21c9SYishai Hadas 	if (dev->mdev->clock_info)
1831*45ec21c9SYishai Hadas 		resp->clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
1832*45ec21c9SYishai Hadas 
1833*45ec21c9SYishai Hadas 	/*
1834*45ec21c9SYishai Hadas 	 * We don't want to expose information from the PCI bar that is located
1835*45ec21c9SYishai Hadas 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1836*45ec21c9SYishai Hadas 	 * pretend we don't support reading the HCA's core clock. This is also
1837*45ec21c9SYishai Hadas 	 * forced by mmap function.
1838*45ec21c9SYishai Hadas 	 */
1839*45ec21c9SYishai Hadas 	if (PAGE_SIZE <= 4096) {
1840*45ec21c9SYishai Hadas 		resp->comp_mask |=
1841*45ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1842*45ec21c9SYishai Hadas 		resp->hca_core_clock_offset =
1843*45ec21c9SYishai Hadas 			offsetof(struct mlx5_init_seg,
1844*45ec21c9SYishai Hadas 				 internal_timer_h) % PAGE_SIZE;
1845*45ec21c9SYishai Hadas 	}
1846*45ec21c9SYishai Hadas 
1847*45ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, ece_support))
1848*45ec21c9SYishai Hadas 		resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_ECE;
1849*45ec21c9SYishai Hadas 
1850*45ec21c9SYishai Hadas 	resp->num_dyn_bfregs = bfregi->num_dyn_bfregs;
1851*45ec21c9SYishai Hadas 	return 0;
1852*45ec21c9SYishai Hadas }
1853*45ec21c9SYishai 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 
1946*45ec21c9SYishai Hadas 	context->cqe_version = min_t(__u8,
1947*45ec21c9SYishai Hadas 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1948*45ec21c9SYishai Hadas 				 req.max_cqe_version);
1949b368d7cbSMatan Barak 
1950*45ec21c9SYishai Hadas 	err = set_ucontext_resp(uctx, &resp);
1951*45ec21c9SYishai Hadas 	if (err)
1952*45ec21c9SYishai Hadas 		goto out_mdev;
1953b368d7cbSMatan Barak 
1954*45ec21c9SYishai 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 
1993a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1994e126ba97SEli Cohen {
1995e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1996e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1997b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1998e126ba97SEli Cohen 
1999b037c29aSEli Cohen 	bfregi = &context->bfregi;
2000d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2001d2d19121SYishai Hadas 
2002b037c29aSEli Cohen 	if (context->devx_uid)
200376dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
2004e126ba97SEli Cohen 
2005e126ba97SEli Cohen 	deallocate_uars(dev, context);
20062f5ff264SEli Cohen 	kfree(bfregi->sys_pages);
20072f5ff264SEli Cohen 	kfree(bfregi->count);
2008e126ba97SEli Cohen }
2009e126ba97SEli Cohen 
2010e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2011e126ba97SEli Cohen 				 int uar_idx)
2012e126ba97SEli Cohen {
2013b037c29aSEli Cohen 	int fw_uars_per_page;
2014b037c29aSEli Cohen 
2015b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2016b037c29aSEli Cohen 
2017aa8106f1SHuy Nguyen 	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2018e126ba97SEli Cohen }
2019e126ba97SEli Cohen 
2020342ee59dSYishai Hadas static u64 uar_index2paddress(struct mlx5_ib_dev *dev,
2021342ee59dSYishai Hadas 				 int uar_idx)
2022342ee59dSYishai Hadas {
2023342ee59dSYishai Hadas 	unsigned int fw_uars_per_page;
2024342ee59dSYishai Hadas 
2025342ee59dSYishai Hadas 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
2026342ee59dSYishai Hadas 				MLX5_UARS_IN_PAGE : 1;
2027342ee59dSYishai Hadas 
2028342ee59dSYishai Hadas 	return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE);
2029342ee59dSYishai Hadas }
2030342ee59dSYishai Hadas 
2031e126ba97SEli Cohen static int get_command(unsigned long offset)
2032e126ba97SEli Cohen {
2033e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2034e126ba97SEli Cohen }
2035e126ba97SEli Cohen 
2036e126ba97SEli Cohen static int get_arg(unsigned long offset)
2037e126ba97SEli Cohen {
2038e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2039e126ba97SEli Cohen }
2040e126ba97SEli Cohen 
2041e126ba97SEli Cohen static int get_index(unsigned long offset)
2042e126ba97SEli Cohen {
2043e126ba97SEli Cohen 	return get_arg(offset);
2044e126ba97SEli Cohen }
2045e126ba97SEli Cohen 
20464ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
20474ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20484ed131d0SYishai Hadas {
20494ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20504ed131d0SYishai Hadas }
20514ed131d0SYishai Hadas 
20527c2344c3SMaor Gottlieb 
20537c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20547c2344c3SMaor Gottlieb {
20557c2344c3SMaor Gottlieb }
20567c2344c3SMaor Gottlieb 
205737aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2058e126ba97SEli Cohen {
205937aa5c36SGuy Levi 	switch (cmd) {
206037aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
206137aa5c36SGuy Levi 		return "WC";
2062e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
206337aa5c36SGuy Levi 		return "best effort WC";
206437aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
206537aa5c36SGuy Levi 		return "NC";
206624da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
206724da0016SAriel Levkovich 		return "Device Memory";
206837aa5c36SGuy Levi 	default:
206937aa5c36SGuy Levi 		return NULL;
207037aa5c36SGuy Levi 	}
207137aa5c36SGuy Levi }
207237aa5c36SGuy Levi 
20735c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20745c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20755c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20765c99eaecSFeras Daoud {
20774eb6ab13SJason Gunthorpe 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
20784eb6ab13SJason Gunthorpe 	    !(vma->vm_flags & VM_SHARED))
20795c99eaecSFeras Daoud 		return -EINVAL;
20805c99eaecSFeras Daoud 
20815c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20825c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20835c99eaecSFeras Daoud 
20844eb6ab13SJason Gunthorpe 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
20855c99eaecSFeras Daoud 		return -EPERM;
2086c660133cSJason Gunthorpe 	vma->vm_flags &= ~VM_MAYWRITE;
20875c99eaecSFeras Daoud 
2088ddcdc368SJason Gunthorpe 	if (!dev->mdev->clock_info)
20895c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20905c99eaecSFeras Daoud 
20914eb6ab13SJason Gunthorpe 	return vm_insert_page(vma, vma->vm_start,
20924eb6ab13SJason Gunthorpe 			      virt_to_page(dev->mdev->clock_info));
20935c99eaecSFeras Daoud }
20945c99eaecSFeras Daoud 
2095dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
2096dc2316ebSYishai Hadas {
2097dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
2098dc2316ebSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device);
20997be76befSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
2100dc2316ebSYishai Hadas 	struct mlx5_ib_dm *mdm;
2101dc2316ebSYishai Hadas 
2102dc2316ebSYishai Hadas 	switch (mentry->mmap_flag) {
2103dc2316ebSYishai Hadas 	case MLX5_IB_MMAP_TYPE_MEMIC:
2104dc2316ebSYishai Hadas 		mdm = container_of(mentry, struct mlx5_ib_dm, mentry);
2105dc2316ebSYishai Hadas 		mlx5_cmd_dealloc_memic(&dev->dm, mdm->dev_addr,
2106dc2316ebSYishai Hadas 				       mdm->size);
2107dc2316ebSYishai Hadas 		kfree(mdm);
2108dc2316ebSYishai Hadas 		break;
21097be76befSYishai Hadas 	case MLX5_IB_MMAP_TYPE_VAR:
21107be76befSYishai Hadas 		mutex_lock(&var_table->bitmap_lock);
21117be76befSYishai Hadas 		clear_bit(mentry->page_idx, var_table->bitmap);
21127be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
21137be76befSYishai Hadas 		kfree(mentry);
21147be76befSYishai Hadas 		break;
2115342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_WC:
2116342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_NC:
2117342ee59dSYishai Hadas 		mlx5_cmd_free_uar(dev->mdev, mentry->page_idx);
2118342ee59dSYishai Hadas 		kfree(mentry);
2119342ee59dSYishai Hadas 		break;
2120dc2316ebSYishai Hadas 	default:
2121dc2316ebSYishai Hadas 		WARN_ON(true);
2122dc2316ebSYishai Hadas 	}
2123dc2316ebSYishai Hadas }
2124dc2316ebSYishai Hadas 
212537aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
21267c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
21277c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
212837aa5c36SGuy Levi {
21292f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
213037aa5c36SGuy Levi 	int err;
213137aa5c36SGuy Levi 	unsigned long idx;
2132aa09ea6eSKamal Heib 	phys_addr_t pfn;
213337aa5c36SGuy Levi 	pgprot_t prot;
21344ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
21354ed131d0SYishai Hadas 	u32 uar_index;
21364ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
21374ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
21384ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2139b037c29aSEli Cohen 
21400a2fd01cSYishai Hadas 	if (bfregi->lib_uar_dyn)
21410a2fd01cSYishai Hadas 		return -EINVAL;
21420a2fd01cSYishai Hadas 
2143b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2144b037c29aSEli Cohen 		return -EINVAL;
2145b037c29aSEli Cohen 
21464ed131d0SYishai Hadas 	if (dyn_uar)
21474ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
21484ed131d0SYishai Hadas 	else
2149b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
21504ed131d0SYishai Hadas 
21514ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21524ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21534ed131d0SYishai Hadas 			     idx, max_valid_idx);
2154b037c29aSEli Cohen 		return -EINVAL;
2155b037c29aSEli Cohen 	}
215637aa5c36SGuy Levi 
215737aa5c36SGuy Levi 	switch (cmd) {
215837aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21594ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
216037aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
216137aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
216237aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
216337aa5c36SGuy Levi 		break;
216437aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
216537aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
216637aa5c36SGuy Levi 		break;
216737aa5c36SGuy Levi 	default:
216837aa5c36SGuy Levi 		return -EINVAL;
216937aa5c36SGuy Levi 	}
217037aa5c36SGuy Levi 
21714ed131d0SYishai Hadas 	if (dyn_uar) {
21724ed131d0SYishai Hadas 		int uars_per_page;
21734ed131d0SYishai Hadas 
21744ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21754ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21764ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21774ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21784ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21794ed131d0SYishai Hadas 			return -EINVAL;
21804ed131d0SYishai Hadas 		}
21814ed131d0SYishai Hadas 
21824ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21834ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21844ed131d0SYishai Hadas 		 * page holds its count.
21854ed131d0SYishai Hadas 		 */
21864ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
21874ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
21884ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
21894ed131d0SYishai Hadas 			return -EINVAL;
21904ed131d0SYishai Hadas 		}
21914ed131d0SYishai Hadas 
21924ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
21934ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
21944ed131d0SYishai Hadas 
21954ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
21964ed131d0SYishai Hadas 		if (err) {
21974ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
21984ed131d0SYishai Hadas 			goto free_bfreg;
21994ed131d0SYishai Hadas 		}
22004ed131d0SYishai Hadas 	} else {
22014ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
22024ed131d0SYishai Hadas 	}
22034ed131d0SYishai Hadas 
22044ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
220537aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2206e126ba97SEli Cohen 
2207e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2208c043ff2cSMichal Kalderon 				prot, NULL);
220937aa5c36SGuy Levi 	if (err) {
22108f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2211e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
22128f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
22134ed131d0SYishai Hadas 		goto err;
221437aa5c36SGuy Levi 	}
2215e126ba97SEli Cohen 
22164ed131d0SYishai Hadas 	if (dyn_uar)
22174ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
22184ed131d0SYishai Hadas 	return 0;
22194ed131d0SYishai Hadas 
22204ed131d0SYishai Hadas err:
22214ed131d0SYishai Hadas 	if (!dyn_uar)
22224ed131d0SYishai Hadas 		return err;
22234ed131d0SYishai Hadas 
22244ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
22254ed131d0SYishai Hadas 
22264ed131d0SYishai Hadas free_bfreg:
22274ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
22284ed131d0SYishai Hadas 
22294ed131d0SYishai Hadas 	return err;
223037aa5c36SGuy Levi }
223137aa5c36SGuy Levi 
2232dc2316ebSYishai Hadas static int add_dm_mmap_entry(struct ib_ucontext *context,
2233dc2316ebSYishai Hadas 			     struct mlx5_ib_dm *mdm,
2234dc2316ebSYishai Hadas 			     u64 address)
223524da0016SAriel Levkovich {
2236dc2316ebSYishai Hadas 	mdm->mentry.mmap_flag = MLX5_IB_MMAP_TYPE_MEMIC;
2237dc2316ebSYishai Hadas 	mdm->mentry.address = address;
2238dc2316ebSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
2239dc2316ebSYishai Hadas 			context, &mdm->mentry.rdma_entry,
2240dc2316ebSYishai Hadas 			mdm->size,
2241dc2316ebSYishai Hadas 			MLX5_IB_MMAP_DEVICE_MEM << 16,
2242dc2316ebSYishai Hadas 			(MLX5_IB_MMAP_DEVICE_MEM << 16) + (1UL << 16) - 1);
2243dc2316ebSYishai Hadas }
224424da0016SAriel Levkovich 
2245dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma)
2246dc2316ebSYishai Hadas {
2247dc2316ebSYishai Hadas 	unsigned long idx;
2248dc2316ebSYishai Hadas 	u8 command;
2249dc2316ebSYishai Hadas 
2250dc2316ebSYishai Hadas 	command = get_command(vma->vm_pgoff);
2251dc2316ebSYishai Hadas 	idx = get_extended_index(vma->vm_pgoff);
2252dc2316ebSYishai Hadas 
2253dc2316ebSYishai Hadas 	return (command << 16 | idx);
2254dc2316ebSYishai Hadas }
2255dc2316ebSYishai Hadas 
2256dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev,
2257dc2316ebSYishai Hadas 			       struct vm_area_struct *vma,
2258dc2316ebSYishai Hadas 			       struct ib_ucontext *ucontext)
2259dc2316ebSYishai Hadas {
2260dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry;
2261dc2316ebSYishai Hadas 	struct rdma_user_mmap_entry *entry;
2262dc2316ebSYishai Hadas 	unsigned long pgoff;
2263dc2316ebSYishai Hadas 	pgprot_t prot;
2264dc2316ebSYishai Hadas 	phys_addr_t pfn;
2265dc2316ebSYishai Hadas 	int ret;
2266dc2316ebSYishai Hadas 
2267dc2316ebSYishai Hadas 	pgoff = mlx5_vma_to_pgoff(vma);
2268dc2316ebSYishai Hadas 	entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff);
2269dc2316ebSYishai Hadas 	if (!entry)
227024da0016SAriel Levkovich 		return -EINVAL;
227124da0016SAriel Levkovich 
2272dc2316ebSYishai Hadas 	mentry = to_mmmap(entry);
2273dc2316ebSYishai Hadas 	pfn = (mentry->address >> PAGE_SHIFT);
2274342ee59dSYishai Hadas 	if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR ||
2275342ee59dSYishai Hadas 	    mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC)
22763f59b6c3SYishai Hadas 		prot = pgprot_noncached(vma->vm_page_prot);
22773f59b6c3SYishai Hadas 	else
2278dc2316ebSYishai Hadas 		prot = pgprot_writecombine(vma->vm_page_prot);
2279dc2316ebSYishai Hadas 	ret = rdma_user_mmap_io(ucontext, vma, pfn,
2280dc2316ebSYishai Hadas 				entry->npages * PAGE_SIZE,
2281dc2316ebSYishai Hadas 				prot,
2282dc2316ebSYishai Hadas 				entry);
2283dc2316ebSYishai Hadas 	rdma_user_mmap_entry_put(&mentry->rdma_entry);
2284dc2316ebSYishai Hadas 	return ret;
228524da0016SAriel Levkovich }
228624da0016SAriel Levkovich 
22877be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry)
22887be76befSYishai Hadas {
22899b6d3bbcSLeon Romanovsky 	u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF;
22909b6d3bbcSLeon Romanovsky 	u64 index = entry->rdma_entry.start_pgoff & 0xFFFF;
22917be76befSYishai Hadas 
22927be76befSYishai Hadas 	return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) |
22937be76befSYishai Hadas 		(index & 0xFF)) << PAGE_SHIFT;
22947be76befSYishai Hadas }
22957be76befSYishai Hadas 
229637aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
229737aa5c36SGuy Levi {
229837aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
229937aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
230037aa5c36SGuy Levi 	unsigned long command;
230137aa5c36SGuy Levi 	phys_addr_t pfn;
230237aa5c36SGuy Levi 
230337aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
230437aa5c36SGuy Levi 	switch (command) {
230537aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
23061f3db161SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
23071f3db161SYishai Hadas 		if (!dev->wc_support)
23081f3db161SYishai Hadas 			return -EPERM;
23091f3db161SYishai Hadas 		fallthrough;
231037aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
231137aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
23127c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2313e126ba97SEli Cohen 
2314e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2315e126ba97SEli Cohen 		return -ENOSYS;
2316e126ba97SEli Cohen 
2317d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2318d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2319d69e3bcfSMatan Barak 			return -EINVAL;
2320d69e3bcfSMatan Barak 
23216cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2322d69e3bcfSMatan Barak 			return -EPERM;
2323c660133cSJason Gunthorpe 		vma->vm_flags &= ~VM_MAYWRITE;
2324d69e3bcfSMatan Barak 
2325d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2326d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2327d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2328d69e3bcfSMatan Barak 
2329d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2330d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2331d69e3bcfSMatan Barak 			PAGE_SHIFT;
2332d5e560d3SJason Gunthorpe 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2333d5e560d3SJason Gunthorpe 					 PAGE_SIZE,
2334c043ff2cSMichal Kalderon 					 pgprot_noncached(vma->vm_page_prot),
2335c043ff2cSMichal Kalderon 					 NULL);
23365c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
23375c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2338d69e3bcfSMatan Barak 
2339e126ba97SEli Cohen 	default:
2340dc2316ebSYishai Hadas 		return mlx5_ib_mmap_offset(dev, vma, ibcontext);
2341e126ba97SEli Cohen 	}
2342e126ba97SEli Cohen 
2343e126ba97SEli Cohen 	return 0;
2344e126ba97SEli Cohen }
2345e126ba97SEli Cohen 
234625c13324SAriel Levkovich static inline int check_dm_type_support(struct mlx5_ib_dev *dev,
234725c13324SAriel Levkovich 					u32 type)
234825c13324SAriel Levkovich {
234925c13324SAriel Levkovich 	switch (type) {
235025c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
235125c13324SAriel Levkovich 		if (!MLX5_CAP_DEV_MEM(dev->mdev, memic))
235225c13324SAriel Levkovich 			return -EOPNOTSUPP;
235325c13324SAriel Levkovich 		break;
235425c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2355c9b9dcb4SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
235625c13324SAriel Levkovich 		if (!capable(CAP_SYS_RAWIO) ||
235725c13324SAriel Levkovich 		    !capable(CAP_NET_RAW))
235825c13324SAriel Levkovich 			return -EPERM;
235925c13324SAriel Levkovich 
236025c13324SAriel Levkovich 		if (!(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner) ||
236125c13324SAriel Levkovich 		      MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner)))
236225c13324SAriel Levkovich 			return -EOPNOTSUPP;
236325c13324SAriel Levkovich 		break;
236425c13324SAriel Levkovich 	}
236525c13324SAriel Levkovich 
236625c13324SAriel Levkovich 	return 0;
236725c13324SAriel Levkovich }
236825c13324SAriel Levkovich 
23693b113a1eSAriel Levkovich static int handle_alloc_dm_memic(struct ib_ucontext *ctx,
23703b113a1eSAriel Levkovich 				 struct mlx5_ib_dm *dm,
237124da0016SAriel Levkovich 				 struct ib_dm_alloc_attr *attr,
237224da0016SAriel Levkovich 				 struct uverbs_attr_bundle *attrs)
237324da0016SAriel Levkovich {
23743b113a1eSAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
237524da0016SAriel Levkovich 	u64 start_offset;
2376dc2316ebSYishai Hadas 	u16 page_idx;
237724da0016SAriel Levkovich 	int err;
2378dc2316ebSYishai Hadas 	u64 address;
237924da0016SAriel Levkovich 
23803b113a1eSAriel Levkovich 	dm->size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
238124da0016SAriel Levkovich 
23823b113a1eSAriel Levkovich 	err = mlx5_cmd_alloc_memic(dm_db, &dm->dev_addr,
23833b113a1eSAriel Levkovich 				   dm->size, attr->alignment);
238424da0016SAriel Levkovich 	if (err)
23853b113a1eSAriel Levkovich 		return err;
238624da0016SAriel Levkovich 
2387dc2316ebSYishai Hadas 	address = dm->dev_addr & PAGE_MASK;
2388dc2316ebSYishai Hadas 	err = add_dm_mmap_entry(ctx, dm, address);
238924da0016SAriel Levkovich 	if (err)
239024da0016SAriel Levkovich 		goto err_dealloc;
239124da0016SAriel Levkovich 
2392dc2316ebSYishai Hadas 	page_idx = dm->mentry.rdma_entry.start_pgoff & 0xFFFF;
2393dc2316ebSYishai Hadas 	err = uverbs_copy_to(attrs,
2394dc2316ebSYishai Hadas 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2395dc2316ebSYishai Hadas 			     &page_idx,
2396dc2316ebSYishai Hadas 			     sizeof(page_idx));
2397dc2316ebSYishai Hadas 	if (err)
2398dc2316ebSYishai Hadas 		goto err_copy;
2399dc2316ebSYishai Hadas 
24003b113a1eSAriel Levkovich 	start_offset = dm->dev_addr & ~PAGE_MASK;
24013b113a1eSAriel Levkovich 	err = uverbs_copy_to(attrs,
24023b113a1eSAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
24033b113a1eSAriel Levkovich 			     &start_offset, sizeof(start_offset));
24043b113a1eSAriel Levkovich 	if (err)
2405dc2316ebSYishai Hadas 		goto err_copy;
24063b113a1eSAriel Levkovich 
24073b113a1eSAriel Levkovich 	return 0;
24083b113a1eSAriel Levkovich 
2409dc2316ebSYishai Hadas err_copy:
2410dc2316ebSYishai Hadas 	rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
24113b113a1eSAriel Levkovich err_dealloc:
24123b113a1eSAriel Levkovich 	mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
24133b113a1eSAriel Levkovich 
24143b113a1eSAriel Levkovich 	return err;
24153b113a1eSAriel Levkovich }
24163b113a1eSAriel Levkovich 
241725c13324SAriel Levkovich static int handle_alloc_dm_sw_icm(struct ib_ucontext *ctx,
241825c13324SAriel Levkovich 				  struct mlx5_ib_dm *dm,
241925c13324SAriel Levkovich 				  struct ib_dm_alloc_attr *attr,
242025c13324SAriel Levkovich 				  struct uverbs_attr_bundle *attrs,
242125c13324SAriel Levkovich 				  int type)
242225c13324SAriel Levkovich {
2423c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ctx->device)->mdev;
242425c13324SAriel Levkovich 	u64 act_size;
242525c13324SAriel Levkovich 	int err;
242625c13324SAriel Levkovich 
242725c13324SAriel Levkovich 	/* Allocation size must a multiple of the basic block size
242825c13324SAriel Levkovich 	 * and a power of 2.
242925c13324SAriel Levkovich 	 */
2430c9b9dcb4SAriel Levkovich 	act_size = round_up(attr->length, MLX5_SW_ICM_BLOCK_SIZE(dev));
243125c13324SAriel Levkovich 	act_size = roundup_pow_of_two(act_size);
243225c13324SAriel Levkovich 
243325c13324SAriel Levkovich 	dm->size = act_size;
2434dff8e2d1SErez Shitrit 	err = mlx5_dm_sw_icm_alloc(dev, type, act_size, attr->alignment,
243525c13324SAriel Levkovich 				   to_mucontext(ctx)->devx_uid, &dm->dev_addr,
243625c13324SAriel Levkovich 				   &dm->icm_dm.obj_id);
243725c13324SAriel Levkovich 	if (err)
243825c13324SAriel Levkovich 		return err;
243925c13324SAriel Levkovich 
244025c13324SAriel Levkovich 	err = uverbs_copy_to(attrs,
244125c13324SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
244225c13324SAriel Levkovich 			     &dm->dev_addr, sizeof(dm->dev_addr));
244325c13324SAriel Levkovich 	if (err)
2444c9b9dcb4SAriel Levkovich 		mlx5_dm_sw_icm_dealloc(dev, type, dm->size,
2445c9b9dcb4SAriel Levkovich 				       to_mucontext(ctx)->devx_uid, dm->dev_addr,
2446c9b9dcb4SAriel Levkovich 				       dm->icm_dm.obj_id);
244725c13324SAriel Levkovich 
244825c13324SAriel Levkovich 	return err;
244925c13324SAriel Levkovich }
245025c13324SAriel Levkovich 
24513b113a1eSAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
24523b113a1eSAriel Levkovich 			       struct ib_ucontext *context,
24533b113a1eSAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
24543b113a1eSAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
24553b113a1eSAriel Levkovich {
24563b113a1eSAriel Levkovich 	struct mlx5_ib_dm *dm;
24573b113a1eSAriel Levkovich 	enum mlx5_ib_uapi_dm_type type;
24583b113a1eSAriel Levkovich 	int err;
24593b113a1eSAriel Levkovich 
24603b113a1eSAriel Levkovich 	err = uverbs_get_const_default(&type, attrs,
24613b113a1eSAriel Levkovich 				       MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
24623b113a1eSAriel Levkovich 				       MLX5_IB_UAPI_DM_TYPE_MEMIC);
24633b113a1eSAriel Levkovich 	if (err)
24643b113a1eSAriel Levkovich 		return ERR_PTR(err);
24653b113a1eSAriel Levkovich 
24663b113a1eSAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_dm req: dm_type=%d user_length=0x%llx log_alignment=%d\n",
24673b113a1eSAriel Levkovich 		    type, attr->length, attr->alignment);
24683b113a1eSAriel Levkovich 
246925c13324SAriel Levkovich 	err = check_dm_type_support(to_mdev(ibdev), type);
247025c13324SAriel Levkovich 	if (err)
247125c13324SAriel Levkovich 		return ERR_PTR(err);
247225c13324SAriel Levkovich 
24733b113a1eSAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
24743b113a1eSAriel Levkovich 	if (!dm)
24753b113a1eSAriel Levkovich 		return ERR_PTR(-ENOMEM);
24763b113a1eSAriel Levkovich 
24773b113a1eSAriel Levkovich 	dm->type = type;
24783b113a1eSAriel Levkovich 
24793b113a1eSAriel Levkovich 	switch (type) {
24803b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
24813b113a1eSAriel Levkovich 		err = handle_alloc_dm_memic(context, dm,
24823b113a1eSAriel Levkovich 					    attr,
24833b113a1eSAriel Levkovich 					    attrs);
24843b113a1eSAriel Levkovich 		break;
248525c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2486c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2487c9b9dcb4SAriel Levkovich 					     attr, attrs,
2488c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_STEERING);
2489c9b9dcb4SAriel Levkovich 		break;
249025c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2491c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2492c9b9dcb4SAriel Levkovich 					     attr, attrs,
2493c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_HEADER_MODIFY);
249425c13324SAriel Levkovich 		break;
24953b113a1eSAriel Levkovich 	default:
24963b113a1eSAriel Levkovich 		err = -EOPNOTSUPP;
24973b113a1eSAriel Levkovich 	}
24983b113a1eSAriel Levkovich 
24993b113a1eSAriel Levkovich 	if (err)
25003b113a1eSAriel Levkovich 		goto err_free;
250124da0016SAriel Levkovich 
250224da0016SAriel Levkovich 	return &dm->ibdm;
250324da0016SAriel Levkovich 
250424da0016SAriel Levkovich err_free:
250524da0016SAriel Levkovich 	kfree(dm);
250624da0016SAriel Levkovich 	return ERR_PTR(err);
250724da0016SAriel Levkovich }
250824da0016SAriel Levkovich 
2509c4367a26SShamir Rabinovitch int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
251024da0016SAriel Levkovich {
251125c13324SAriel Levkovich 	struct mlx5_ib_ucontext *ctx = rdma_udata_to_drv_context(
251225c13324SAriel Levkovich 		&attrs->driver_udata, struct mlx5_ib_ucontext, ibucontext);
2513c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ibdm->device)->mdev;
251424da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
251524da0016SAriel Levkovich 	int ret;
251624da0016SAriel Levkovich 
25173b113a1eSAriel Levkovich 	switch (dm->type) {
25183b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
2519dc2316ebSYishai Hadas 		rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
2520dc2316ebSYishai Hadas 		return 0;
252125c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2522c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_STEERING,
2523c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
2524c9b9dcb4SAriel Levkovich 					     dm->icm_dm.obj_id);
2525c9b9dcb4SAriel Levkovich 		if (ret)
2526c9b9dcb4SAriel Levkovich 			return ret;
2527c9b9dcb4SAriel Levkovich 		break;
252825c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2529c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_HEADER_MODIFY,
2530c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
253125c13324SAriel Levkovich 					     dm->icm_dm.obj_id);
253225c13324SAriel Levkovich 		if (ret)
253325c13324SAriel Levkovich 			return ret;
25343b113a1eSAriel Levkovich 		break;
25353b113a1eSAriel Levkovich 	default:
25363b113a1eSAriel Levkovich 		return -EOPNOTSUPP;
25373b113a1eSAriel Levkovich 	}
253824da0016SAriel Levkovich 
253924da0016SAriel Levkovich 	kfree(dm);
254024da0016SAriel Levkovich 
254124da0016SAriel Levkovich 	return 0;
254224da0016SAriel Levkovich }
254324da0016SAriel Levkovich 
2544ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2545e126ba97SEli Cohen {
254621a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
254721a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2548e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2549e126ba97SEli Cohen 	int err;
2550a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2551a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2552a1069c1cSYishai Hadas 	u16 uid = 0;
2553ff23dfa1SShamir Rabinovitch 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2554ff23dfa1SShamir Rabinovitch 		udata, struct mlx5_ib_ucontext, ibucontext);
2555e126ba97SEli Cohen 
2556ff23dfa1SShamir Rabinovitch 	uid = context ? context->devx_uid : 0;
2557a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2558a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
255931578defSLeon Romanovsky 	err = mlx5_cmd_exec_inout(to_mdev(ibdev)->mdev, alloc_pd, in, out);
256021a428a0SLeon Romanovsky 	if (err)
256121a428a0SLeon Romanovsky 		return err;
2562e126ba97SEli Cohen 
2563a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2564a1069c1cSYishai Hadas 	pd->uid = uid;
2565ff23dfa1SShamir Rabinovitch 	if (udata) {
2566e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2567e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2568a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
256921a428a0SLeon Romanovsky 			return -EFAULT;
2570e126ba97SEli Cohen 		}
2571e126ba97SEli Cohen 	}
2572e126ba97SEli Cohen 
257321a428a0SLeon Romanovsky 	return 0;
2574e126ba97SEli Cohen }
2575e126ba97SEli Cohen 
2576c4367a26SShamir Rabinovitch static void mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2577e126ba97SEli Cohen {
2578e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2579e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2580e126ba97SEli Cohen 
2581a1069c1cSYishai Hadas 	mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2582e126ba97SEli Cohen }
2583e126ba97SEli Cohen 
2584466fa6d2SMaor Gottlieb enum {
2585466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2586466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
258771c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_INNER_BIT,
258871c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_MISC2_BIT
2589466fa6d2SMaor Gottlieb };
2590038d2ef8SMaor Gottlieb 
2591466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2592466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2593466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2594466fa6d2SMaor Gottlieb 
2595466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2596466fa6d2SMaor Gottlieb {
2597466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2598466fa6d2SMaor Gottlieb 
2599466fa6d2SMaor Gottlieb 	match_criteria_enable =
2600466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2601466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2602466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2603466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2604466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2605466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2606466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2607466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
260871c6e863SAriel Levkovich 	match_criteria_enable |=
260971c6e863SAriel Levkovich 		(!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
261071c6e863SAriel Levkovich 		MATCH_CRITERIA_ENABLE_MISC2_BIT;
2611466fa6d2SMaor Gottlieb 
2612466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2613038d2ef8SMaor Gottlieb }
2614038d2ef8SMaor Gottlieb 
26156113cc44SMaor Gottlieb static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2616ca0d4753SMaor Gottlieb {
26176113cc44SMaor Gottlieb 	u8 entry_mask;
26186113cc44SMaor Gottlieb 	u8 entry_val;
26196113cc44SMaor Gottlieb 	int err = 0;
26206113cc44SMaor Gottlieb 
26216113cc44SMaor Gottlieb 	if (!mask)
26226113cc44SMaor Gottlieb 		goto out;
26236113cc44SMaor Gottlieb 
26246113cc44SMaor Gottlieb 	entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
26256113cc44SMaor Gottlieb 			      ip_protocol);
26266113cc44SMaor Gottlieb 	entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
26276113cc44SMaor Gottlieb 			     ip_protocol);
26286113cc44SMaor Gottlieb 	if (!entry_mask) {
2629ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2630ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
26316113cc44SMaor Gottlieb 		goto out;
26326113cc44SMaor Gottlieb 	}
26336113cc44SMaor Gottlieb 	/* Don't override existing ip protocol */
26346113cc44SMaor Gottlieb 	if (mask != entry_mask || val != entry_val)
26356113cc44SMaor Gottlieb 		err = -EINVAL;
26366113cc44SMaor Gottlieb out:
26376113cc44SMaor Gottlieb 	return err;
2638ca0d4753SMaor Gottlieb }
2639ca0d4753SMaor Gottlieb 
264037da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
26412d1e697eSMoses Reuben 			   bool inner)
26422d1e697eSMoses Reuben {
26432d1e697eSMoses Reuben 	if (inner) {
26442d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26452d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
26462d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26472d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
26482d1e697eSMoses Reuben 	} else {
26492d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26502d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
26512d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
26522d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
26532d1e697eSMoses Reuben 	}
26542d1e697eSMoses Reuben }
26552d1e697eSMoses Reuben 
2656ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2657ca0d4753SMaor Gottlieb {
2658ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2659ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2660ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2661ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2662ca0d4753SMaor Gottlieb }
2663ca0d4753SMaor Gottlieb 
266471c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
266571c6e863SAriel Levkovich {
266671c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
266771c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
266871c6e863SAriel Levkovich 		return -EOPNOTSUPP;
266971c6e863SAriel Levkovich 
267071c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
267171c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
267271c6e863SAriel Levkovich 		return -EOPNOTSUPP;
267371c6e863SAriel Levkovich 
267471c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
267571c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
267671c6e863SAriel Levkovich 		return -EOPNOTSUPP;
267771c6e863SAriel Levkovich 
267871c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
267971c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
268071c6e863SAriel Levkovich 		return -EOPNOTSUPP;
268171c6e863SAriel Levkovich 
268271c6e863SAriel Levkovich 	return 0;
268371c6e863SAriel Levkovich }
268471c6e863SAriel Levkovich 
2685c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2686c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2687ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2688466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2689c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2690ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
26912ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2692a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
26933b3233fbSRaed Salem #define LAST_COUNTERS_FIELD counters
2694c47ac6aeSMaor Gottlieb 
2695c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2696c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2697c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2698c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2699c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2700c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2701c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2702c47ac6aeSMaor Gottlieb 
27032ea26203SMark Bloch int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
27042ea26203SMark Bloch 			   bool is_egress,
2705802c2125SAviad Yehezkel 			   struct mlx5_flow_act *action)
2706802c2125SAviad Yehezkel {
2707802c2125SAviad Yehezkel 
2708802c2125SAviad Yehezkel 	switch (maction->ib_action.type) {
2709802c2125SAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
2710501f14e3SMark Bloch 		if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2711501f14e3SMark Bloch 				      MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2712501f14e3SMark Bloch 			return -EINVAL;
2713802c2125SAviad Yehezkel 		/* Currently only AES_GCM keymat is supported by the driver */
2714802c2125SAviad Yehezkel 		action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
27152ea26203SMark Bloch 		action->action |= is_egress ?
2716802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2717802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2718802c2125SAviad Yehezkel 		return 0;
2719b1085be3SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
2720b1085be3SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2721b1085be3SMark Bloch 		    MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
2722501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2723501f14e3SMark Bloch 				return -EINVAL;
2724b1085be3SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
27252b688ea5SMaor Gottlieb 			action->modify_hdr =
27262b688ea5SMaor Gottlieb 				maction->flow_action_raw.modify_hdr;
2727b1085be3SMark Bloch 			return 0;
2728b1085be3SMark Bloch 		}
272910a30896SMark Bloch 		if (maction->flow_action_raw.sub_type ==
273010a30896SMark Bloch 		    MLX5_IB_FLOW_ACTION_DECAP) {
2731501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2732501f14e3SMark Bloch 				return -EINVAL;
273310a30896SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
273410a30896SMark Bloch 			return 0;
273510a30896SMark Bloch 		}
2736e806f932SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2737e806f932SMark Bloch 		    MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
2738501f14e3SMark Bloch 			if (action->action &
2739501f14e3SMark Bloch 			    MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2740501f14e3SMark Bloch 				return -EINVAL;
2741e806f932SMark Bloch 			action->action |=
2742e806f932SMark Bloch 				MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
27432b688ea5SMaor Gottlieb 			action->pkt_reformat =
27442b688ea5SMaor Gottlieb 				maction->flow_action_raw.pkt_reformat;
2745e806f932SMark Bloch 			return 0;
2746e806f932SMark Bloch 		}
2747b1085be3SMark Bloch 		/* fall through */
2748802c2125SAviad Yehezkel 	default:
2749802c2125SAviad Yehezkel 		return -EOPNOTSUPP;
2750802c2125SAviad Yehezkel 	}
2751802c2125SAviad Yehezkel }
2752802c2125SAviad Yehezkel 
2753bb0ee7dcSJianbo Liu static int parse_flow_attr(struct mlx5_core_dev *mdev,
2754bb0ee7dcSJianbo Liu 			   struct mlx5_flow_spec *spec,
2755bb0ee7dcSJianbo Liu 			   const union ib_flow_spec *ib_spec,
2756802c2125SAviad Yehezkel 			   const struct ib_flow_attr *flow_attr,
275771c6e863SAriel Levkovich 			   struct mlx5_flow_act *action, u32 prev_type)
2758038d2ef8SMaor Gottlieb {
2759bb0ee7dcSJianbo Liu 	struct mlx5_flow_context *flow_context = &spec->flow_context;
2760bb0ee7dcSJianbo Liu 	u32 *match_c = spec->match_criteria;
2761bb0ee7dcSJianbo Liu 	u32 *match_v = spec->match_value;
2762466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2763466fa6d2SMaor Gottlieb 					   misc_parameters);
2764466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2765466fa6d2SMaor Gottlieb 					   misc_parameters);
276671c6e863SAriel Levkovich 	void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
276771c6e863SAriel Levkovich 					    misc_parameters_2);
276871c6e863SAriel Levkovich 	void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
276971c6e863SAriel Levkovich 					    misc_parameters_2);
27702d1e697eSMoses Reuben 	void *headers_c;
27712d1e697eSMoses Reuben 	void *headers_v;
277219cc7524SAriel Levkovich 	int match_ipv;
2773802c2125SAviad Yehezkel 	int ret;
2774466fa6d2SMaor Gottlieb 
27752d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
27762d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
27772d1e697eSMoses Reuben 					 inner_headers);
27782d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
27792d1e697eSMoses Reuben 					 inner_headers);
278019cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
278119cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
27822d1e697eSMoses Reuben 	} else {
27832d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
27842d1e697eSMoses Reuben 					 outer_headers);
27852d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
27862d1e697eSMoses Reuben 					 outer_headers);
278719cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
278819cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
27892d1e697eSMoses Reuben 	}
27902d1e697eSMoses Reuben 
27912d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2792038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2793c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
27941ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2795038d2ef8SMaor Gottlieb 
27962d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2797038d2ef8SMaor Gottlieb 					     dmac_47_16),
2798038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
27992d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2800038d2ef8SMaor Gottlieb 					     dmac_47_16),
2801038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2802038d2ef8SMaor Gottlieb 
28032d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2804ee3da804SMaor Gottlieb 					     smac_47_16),
2805ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
28062d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2807ee3da804SMaor Gottlieb 					     smac_47_16),
2808ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2809ee3da804SMaor Gottlieb 
2810038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
28112d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
281210543365SMohamad Haj Yahia 				 cvlan_tag, 1);
28132d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
281410543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2815038d2ef8SMaor Gottlieb 
28162d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2817038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
28182d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2819038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2820038d2ef8SMaor Gottlieb 
28212d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2822038d2ef8SMaor Gottlieb 				 first_cfi,
2823038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
28242d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2825038d2ef8SMaor Gottlieb 				 first_cfi,
2826038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2827038d2ef8SMaor Gottlieb 
28282d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2829038d2ef8SMaor Gottlieb 				 first_prio,
2830038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
28312d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2832038d2ef8SMaor Gottlieb 				 first_prio,
2833038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2834038d2ef8SMaor Gottlieb 		}
28352d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2836038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
28372d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2838038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2839038d2ef8SMaor Gottlieb 		break;
2840038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2841c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
28421ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2843038d2ef8SMaor Gottlieb 
284419cc7524SAriel Levkovich 		if (match_ipv) {
284519cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
284619cc7524SAriel Levkovich 				 ip_version, 0xf);
284719cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
28483346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV4_VERSION);
284919cc7524SAriel Levkovich 		} else {
28502d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2851038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
28522d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2853038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
285419cc7524SAriel Levkovich 		}
2855038d2ef8SMaor Gottlieb 
28562d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2857038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2858038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2859038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
28602d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2861038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2862038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2863038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
28642d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2865038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2866038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2867038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
28682d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2869038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2870038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2871038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2872ca0d4753SMaor Gottlieb 
28732d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2874ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2875ca0d4753SMaor Gottlieb 
28766113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
28776113cc44SMaor Gottlieb 			      ib_spec->ipv4.mask.proto,
28786113cc44SMaor Gottlieb 			      ib_spec->ipv4.val.proto))
28796113cc44SMaor Gottlieb 			return -EINVAL;
2880038d2ef8SMaor Gottlieb 		break;
2881026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2882c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
28831ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2884026bae0cSMaor Gottlieb 
288519cc7524SAriel Levkovich 		if (match_ipv) {
288619cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
288719cc7524SAriel Levkovich 				 ip_version, 0xf);
288819cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
28893346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV6_VERSION);
289019cc7524SAriel Levkovich 		} else {
28912d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2892026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
28932d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2894026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
289519cc7524SAriel Levkovich 		}
2896026bae0cSMaor Gottlieb 
28972d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2898026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2899026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2900026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
29012d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2902026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2903026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2904026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
29052d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2906026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2907026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2908026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
29092d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2910026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2911026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2912026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2913466fa6d2SMaor Gottlieb 
29142d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2915466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2916466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2917466fa6d2SMaor Gottlieb 
29186113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
2919466fa6d2SMaor Gottlieb 			      ib_spec->ipv6.mask.next_hdr,
29206113cc44SMaor Gottlieb 			      ib_spec->ipv6.val.next_hdr))
29216113cc44SMaor Gottlieb 			return -EINVAL;
2922466fa6d2SMaor Gottlieb 
29232d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
29242d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
29252d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
29262d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
2927802c2125SAviad Yehezkel 		break;
2928802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ESP:
2929802c2125SAviad Yehezkel 		if (ib_spec->esp.mask.seq)
2930802c2125SAviad Yehezkel 			return -EOPNOTSUPP;
29312d1e697eSMoses Reuben 
2932802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2933802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.mask.spi));
2934802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2935802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.val.spi));
2936026bae0cSMaor Gottlieb 		break;
2937038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2938c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2939c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
29401ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2941038d2ef8SMaor Gottlieb 
29426113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
29436113cc44SMaor Gottlieb 			return -EINVAL;
2944038d2ef8SMaor Gottlieb 
29452d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2946038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
29472d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2948038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2949038d2ef8SMaor Gottlieb 
29502d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2951038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
29522d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2953038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2954038d2ef8SMaor Gottlieb 		break;
2955038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2956c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2957c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
29581ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2959038d2ef8SMaor Gottlieb 
29606113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
29616113cc44SMaor Gottlieb 			return -EINVAL;
2962038d2ef8SMaor Gottlieb 
29632d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2964038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
29652d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2966038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2967038d2ef8SMaor Gottlieb 
29682d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2969038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
29702d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2971038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2972038d2ef8SMaor Gottlieb 		break;
2973da2f22aeSAriel Levkovich 	case IB_FLOW_SPEC_GRE:
2974da2f22aeSAriel Levkovich 		if (ib_spec->gre.mask.c_ks_res0_ver)
2975da2f22aeSAriel Levkovich 			return -EOPNOTSUPP;
2976da2f22aeSAriel Levkovich 
29776113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
29786113cc44SMaor Gottlieb 			return -EINVAL;
29796113cc44SMaor Gottlieb 
2980da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2981da2f22aeSAriel Levkovich 			 0xff);
2982da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2983da2f22aeSAriel Levkovich 			 IPPROTO_GRE);
2984da2f22aeSAriel Levkovich 
2985da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
2986a93b632cSMaor Gottlieb 			 ntohs(ib_spec->gre.mask.protocol));
2987da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2988da2f22aeSAriel Levkovich 			 ntohs(ib_spec->gre.val.protocol));
2989da2f22aeSAriel Levkovich 
2990da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
29915886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2992da2f22aeSAriel Levkovich 		       &ib_spec->gre.mask.key,
2993da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.mask.key));
2994da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
29955886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2996da2f22aeSAriel Levkovich 		       &ib_spec->gre.val.key,
2997da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.val.key));
2998da2f22aeSAriel Levkovich 		break;
299971c6e863SAriel Levkovich 	case IB_FLOW_SPEC_MPLS:
300071c6e863SAriel Levkovich 		switch (prev_type) {
300171c6e863SAriel Levkovich 		case IB_FLOW_SPEC_UDP:
300271c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
300371c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_udp),
300471c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
300571c6e863SAriel Levkovich 				return -EOPNOTSUPP;
300671c6e863SAriel Levkovich 
300771c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
300871c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
300971c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
301071c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
301171c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
301271c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
301371c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
301471c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
301571c6e863SAriel Levkovich 			break;
301671c6e863SAriel Levkovich 		case IB_FLOW_SPEC_GRE:
301771c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
301871c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_gre),
301971c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
302071c6e863SAriel Levkovich 				return -EOPNOTSUPP;
302171c6e863SAriel Levkovich 
302271c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
302371c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
302471c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
302571c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
302671c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
302771c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
302871c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
302971c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
303071c6e863SAriel Levkovich 			break;
303171c6e863SAriel Levkovich 		default:
303271c6e863SAriel Levkovich 			if (ib_spec->type & IB_FLOW_SPEC_INNER) {
303371c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
303471c6e863SAriel Levkovich 							   ft_field_support.inner_first_mpls),
303571c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
303671c6e863SAriel Levkovich 					return -EOPNOTSUPP;
303771c6e863SAriel Levkovich 
303871c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
303971c6e863SAriel Levkovich 						    inner_first_mpls),
304071c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
304171c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
304271c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
304371c6e863SAriel Levkovich 						    inner_first_mpls),
304471c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
304571c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
304671c6e863SAriel Levkovich 			} else {
304771c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
304871c6e863SAriel Levkovich 							   ft_field_support.outer_first_mpls),
304971c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
305071c6e863SAriel Levkovich 					return -EOPNOTSUPP;
305171c6e863SAriel Levkovich 
305271c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
305371c6e863SAriel Levkovich 						    outer_first_mpls),
305471c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
305571c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
305671c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
305771c6e863SAriel Levkovich 						    outer_first_mpls),
305871c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
305971c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
306071c6e863SAriel Levkovich 			}
306171c6e863SAriel Levkovich 		}
306271c6e863SAriel Levkovich 		break;
3063ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
3064ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
3065ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
30661ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
3067ffb30d8fSMoses Reuben 
3068ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
3069ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
3070ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
3071ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
3072ffb30d8fSMoses Reuben 		break;
30732ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
30742ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
30752ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
30762ac693f9SMoses Reuben 			return -EOPNOTSUPP;
30772ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
30782ac693f9SMoses Reuben 			return -EINVAL;
30792ac693f9SMoses Reuben 
3080bb0ee7dcSJianbo Liu 		flow_context->flow_tag = ib_spec->flow_tag.tag_id;
3081bb0ee7dcSJianbo Liu 		flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
30822ac693f9SMoses Reuben 		break;
3083a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
3084a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
3085a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
3086a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
3087075572d4SBoris Pismenny 		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
3088a22ed86cSSlava Shwartsman 		break;
3089802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ACTION_HANDLE:
30902ea26203SMark Bloch 		ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
30912ea26203SMark Bloch 			flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
3092802c2125SAviad Yehezkel 		if (ret)
3093802c2125SAviad Yehezkel 			return ret;
3094802c2125SAviad Yehezkel 		break;
30953b3233fbSRaed Salem 	case IB_FLOW_SPEC_ACTION_COUNT:
30963b3233fbSRaed Salem 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
30973b3233fbSRaed Salem 					 LAST_COUNTERS_FIELD))
30983b3233fbSRaed Salem 			return -EOPNOTSUPP;
30993b3233fbSRaed Salem 
31003b3233fbSRaed Salem 		/* for now support only one counters spec per flow */
31013b3233fbSRaed Salem 		if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
31023b3233fbSRaed Salem 			return -EINVAL;
31033b3233fbSRaed Salem 
31043b3233fbSRaed Salem 		action->counters = ib_spec->flow_count.counters;
31053b3233fbSRaed Salem 		action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
31063b3233fbSRaed Salem 		break;
3107038d2ef8SMaor Gottlieb 	default:
3108038d2ef8SMaor Gottlieb 		return -EINVAL;
3109038d2ef8SMaor Gottlieb 	}
3110038d2ef8SMaor Gottlieb 
3111038d2ef8SMaor Gottlieb 	return 0;
3112038d2ef8SMaor Gottlieb }
3113038d2ef8SMaor Gottlieb 
3114038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
3115038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
3116038d2ef8SMaor Gottlieb  * could steal other multicast packets.
3117038d2ef8SMaor Gottlieb  */
3118a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
3119038d2ef8SMaor Gottlieb {
312081e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
3121038d2ef8SMaor Gottlieb 
3122038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
3123038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
3124038d2ef8SMaor Gottlieb 		return false;
3125038d2ef8SMaor Gottlieb 
312681e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
312781e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
312881e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
3129038d2ef8SMaor Gottlieb 
313081e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
313181e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
313281e30880SYishai Hadas 			return true;
313381e30880SYishai Hadas 
313481e30880SYishai Hadas 		return false;
313581e30880SYishai Hadas 	}
313681e30880SYishai Hadas 
313781e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
313881e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
313981e30880SYishai Hadas 
314081e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
3141038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
3142038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
3143038d2ef8SMaor Gottlieb 	}
3144038d2ef8SMaor Gottlieb 
314581e30880SYishai Hadas 	return false;
314681e30880SYishai Hadas }
314781e30880SYishai Hadas 
3148802c2125SAviad Yehezkel enum valid_spec {
3149802c2125SAviad Yehezkel 	VALID_SPEC_INVALID,
3150802c2125SAviad Yehezkel 	VALID_SPEC_VALID,
3151802c2125SAviad Yehezkel 	VALID_SPEC_NA,
3152802c2125SAviad Yehezkel };
3153802c2125SAviad Yehezkel 
3154802c2125SAviad Yehezkel static enum valid_spec
3155802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
3156802c2125SAviad Yehezkel 		     const struct mlx5_flow_spec *spec,
3157802c2125SAviad Yehezkel 		     const struct mlx5_flow_act *flow_act,
3158802c2125SAviad Yehezkel 		     bool egress)
3159802c2125SAviad Yehezkel {
3160802c2125SAviad Yehezkel 	const u32 *match_c = spec->match_criteria;
3161802c2125SAviad Yehezkel 	bool is_crypto =
3162802c2125SAviad Yehezkel 		(flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
3163802c2125SAviad Yehezkel 				     MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
3164802c2125SAviad Yehezkel 	bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
3165802c2125SAviad Yehezkel 	bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
3166802c2125SAviad Yehezkel 
3167802c2125SAviad Yehezkel 	/*
3168802c2125SAviad Yehezkel 	 * Currently only crypto is supported in egress, when regular egress
3169802c2125SAviad Yehezkel 	 * rules would be supported, always return VALID_SPEC_NA.
3170802c2125SAviad Yehezkel 	 */
3171802c2125SAviad Yehezkel 	if (!is_crypto)
317278dd0c43SMark Bloch 		return VALID_SPEC_NA;
3173802c2125SAviad Yehezkel 
3174802c2125SAviad Yehezkel 	return is_crypto && is_ipsec &&
3175bb0ee7dcSJianbo Liu 		(!egress || (!is_drop &&
3176bb0ee7dcSJianbo Liu 			     !(spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG))) ?
3177802c2125SAviad Yehezkel 		VALID_SPEC_VALID : VALID_SPEC_INVALID;
3178802c2125SAviad Yehezkel }
3179802c2125SAviad Yehezkel 
3180802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev,
3181802c2125SAviad Yehezkel 			  const struct mlx5_flow_spec *spec,
3182802c2125SAviad Yehezkel 			  const struct mlx5_flow_act *flow_act,
3183802c2125SAviad Yehezkel 			  bool egress)
3184802c2125SAviad Yehezkel {
3185802c2125SAviad Yehezkel 	/* We curretly only support ipsec egress flow */
3186802c2125SAviad Yehezkel 	return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
3187802c2125SAviad Yehezkel }
3188802c2125SAviad Yehezkel 
318919cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
319019cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
31910f750966SAriel Levkovich 			       bool check_inner)
3192038d2ef8SMaor Gottlieb {
3193038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
319419cc7524SAriel Levkovich 	int match_ipv = check_inner ?
319519cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
319619cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
319719cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
319819cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
31990f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
32000f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
32010f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
32020f750966SAriel Levkovich 	bool has_ethertype = false;
3203038d2ef8SMaor Gottlieb 	unsigned int spec_index;
32040f750966SAriel Levkovich 	bool mask_valid = true;
32050f750966SAriel Levkovich 	u16 eth_type = 0;
32060f750966SAriel Levkovich 	bool type_valid;
3207038d2ef8SMaor Gottlieb 
3208038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
3209038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
32100f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
3211038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
32120f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
32130f750966SAriel Levkovich 				      htons(0xffff));
32140f750966SAriel Levkovich 			has_ethertype = true;
32150f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
32160f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
32170f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
32180f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
3219038d2ef8SMaor Gottlieb 		}
3220038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
3221038d2ef8SMaor Gottlieb 	}
32220f750966SAriel Levkovich 
32230f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
32240f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
32250f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
32260f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
32270f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
32280f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
322919cc7524SAriel Levkovich 
323019cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
323119cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
323219cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
32330f750966SAriel Levkovich 	}
32340f750966SAriel Levkovich 
32350f750966SAriel Levkovich 	return type_valid;
32360f750966SAriel Levkovich }
32370f750966SAriel Levkovich 
323819cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
323919cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
32400f750966SAriel Levkovich {
324119cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
324219cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
3243038d2ef8SMaor Gottlieb }
3244038d2ef8SMaor Gottlieb 
3245038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
3246038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
3247038d2ef8SMaor Gottlieb {
3248038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
3249038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
3250038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
3251038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
3252038d2ef8SMaor Gottlieb 	}
3253038d2ef8SMaor Gottlieb }
3254038d2ef8SMaor Gottlieb 
32553b3233fbSRaed Salem static void counters_clear_description(struct ib_counters *counters)
32563b3233fbSRaed Salem {
32573b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
32583b3233fbSRaed Salem 
32593b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
32603b3233fbSRaed Salem 	kfree(mcounters->counters_data);
32613b3233fbSRaed Salem 	mcounters->counters_data = NULL;
32623b3233fbSRaed Salem 	mcounters->cntrs_max_index = 0;
32633b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
32643b3233fbSRaed Salem }
32653b3233fbSRaed Salem 
3266038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3267038d2ef8SMaor Gottlieb {
3268038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3269038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
3270038d2ef8SMaor Gottlieb 							  ibflow);
3271038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
3272d4be3f44SYishai Hadas 	struct mlx5_ib_dev *dev = handler->dev;
3273038d2ef8SMaor Gottlieb 
32749a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3275038d2ef8SMaor Gottlieb 
3276038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
327774491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
3278cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
3279038d2ef8SMaor Gottlieb 		list_del(&iter->list);
3280038d2ef8SMaor Gottlieb 		kfree(iter);
3281038d2ef8SMaor Gottlieb 	}
3282038d2ef8SMaor Gottlieb 
328374491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
32845497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
32853b3233fbSRaed Salem 	if (handler->ibcounters &&
32863b3233fbSRaed Salem 	    atomic_read(&handler->ibcounters->usecnt) == 1)
32873b3233fbSRaed Salem 		counters_clear_description(handler->ibcounters);
3288038d2ef8SMaor Gottlieb 
32893b3233fbSRaed Salem 	mutex_unlock(&dev->flow_db->lock);
3290d4be3f44SYishai Hadas 	if (handler->flow_matcher)
3291d4be3f44SYishai Hadas 		atomic_dec(&handler->flow_matcher->usecnt);
3292038d2ef8SMaor Gottlieb 	kfree(handler);
3293038d2ef8SMaor Gottlieb 
3294038d2ef8SMaor Gottlieb 	return 0;
3295038d2ef8SMaor Gottlieb }
3296038d2ef8SMaor Gottlieb 
329735d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
329835d19011SMaor Gottlieb {
329935d19011SMaor Gottlieb 	priority *= 2;
330035d19011SMaor Gottlieb 	if (!dont_trap)
330135d19011SMaor Gottlieb 		priority++;
330235d19011SMaor Gottlieb 	return priority;
330335d19011SMaor Gottlieb }
330435d19011SMaor Gottlieb 
3305cc0e5d42SMaor Gottlieb enum flow_table_type {
3306cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
3307cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
3308cc0e5d42SMaor Gottlieb };
3309cc0e5d42SMaor Gottlieb 
331000b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
331100b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
3312d4be3f44SYishai Hadas 
3313d4be3f44SYishai Hadas static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3314d4be3f44SYishai Hadas 					   struct mlx5_ib_flow_prio *prio,
3315d4be3f44SYishai Hadas 					   int priority,
33164adda112SMark Bloch 					   int num_entries, int num_groups,
33174adda112SMark Bloch 					   u32 flags)
3318d4be3f44SYishai Hadas {
331961dc7b01SPaul Blakey 	struct mlx5_flow_table_attr ft_attr = {};
3320d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft;
3321d4be3f44SYishai Hadas 
332261dc7b01SPaul Blakey 	ft_attr.prio = priority;
332361dc7b01SPaul Blakey 	ft_attr.max_fte = num_entries;
332461dc7b01SPaul Blakey 	ft_attr.flags = flags;
332561dc7b01SPaul Blakey 	ft_attr.autogroup.max_num_groups = num_groups;
332661dc7b01SPaul Blakey 	ft = mlx5_create_auto_grouped_flow_table(ns, &ft_attr);
3327d4be3f44SYishai Hadas 	if (IS_ERR(ft))
3328d4be3f44SYishai Hadas 		return ERR_CAST(ft);
3329d4be3f44SYishai Hadas 
3330d4be3f44SYishai Hadas 	prio->flow_table = ft;
3331d4be3f44SYishai Hadas 	prio->refcount = 0;
3332d4be3f44SYishai Hadas 	return prio;
3333d4be3f44SYishai Hadas }
3334d4be3f44SYishai Hadas 
3335038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
3336cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
3337cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
3338038d2ef8SMaor Gottlieb {
333935d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
3340038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
3341038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
3342038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
3343dac388efSMaor Gottlieb 	int max_table_size;
3344038d2ef8SMaor Gottlieb 	int num_entries;
3345038d2ef8SMaor Gottlieb 	int num_groups;
3346cecae747SMaor Gottlieb 	bool esw_encap;
33474adda112SMark Bloch 	u32 flags = 0;
3348038d2ef8SMaor Gottlieb 	int priority;
3349038d2ef8SMaor Gottlieb 
3350dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3351dac388efSMaor Gottlieb 						       log_max_ft_size));
3352cecae747SMaor Gottlieb 	esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
3353cecae747SMaor Gottlieb 		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
3354038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
335578dd0c43SMark Bloch 		enum mlx5_flow_namespace_type fn_type;
335678dd0c43SMark Bloch 
335778dd0c43SMark Bloch 		if (flow_is_multicast_only(flow_attr) &&
335835d19011SMaor Gottlieb 		    !dont_trap)
3359038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
3360038d2ef8SMaor Gottlieb 		else
336135d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
336235d19011SMaor Gottlieb 							dont_trap);
336378dd0c43SMark Bloch 		if (ft_type == MLX5_IB_FT_RX) {
336478dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
336578dd0c43SMark Bloch 			prio = &dev->flow_db->prios[priority];
3366cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
33674adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
33684adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3369cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
33705c2db53fSMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
33715c2db53fSMark Bloch 					reformat_l3_tunnel_to_l2))
33725c2db53fSMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
337378dd0c43SMark Bloch 		} else {
337478dd0c43SMark Bloch 			max_table_size =
337578dd0c43SMark Bloch 				BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
337678dd0c43SMark Bloch 							      log_max_ft_size));
337778dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
337878dd0c43SMark Bloch 			prio = &dev->flow_db->egress_prios[priority];
3379cecae747SMaor Gottlieb 			if (!dev->is_rep && !esw_encap &&
33804adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
33814adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
338278dd0c43SMark Bloch 		}
338378dd0c43SMark Bloch 		ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
3384038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
3385038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
3386038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3387038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3388038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
3389038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
3390038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
3391038d2ef8SMaor Gottlieb 					 &num_entries,
3392038d2ef8SMaor Gottlieb 					 &num_groups);
33939a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
3394cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3395cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3396cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
3397cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
3398cc0e5d42SMaor Gottlieb 
3399cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3400cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3401cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3402cc0e5d42SMaor Gottlieb 
34039a4ca38dSMark Bloch 		prio = &dev->flow_db->sniffer[ft_type];
3404cc0e5d42SMaor Gottlieb 		priority = 0;
3405cc0e5d42SMaor Gottlieb 		num_entries = 1;
3406cc0e5d42SMaor Gottlieb 		num_groups = 1;
3407038d2ef8SMaor Gottlieb 	}
3408038d2ef8SMaor Gottlieb 
3409038d2ef8SMaor Gottlieb 	if (!ns)
3410038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
3411038d2ef8SMaor Gottlieb 
34123b70508aSMark Bloch 	max_table_size = min_t(int, num_entries, max_table_size);
3413dac388efSMaor Gottlieb 
3414038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
3415d4be3f44SYishai Hadas 	if (!ft)
34163b70508aSMark Bloch 		return _get_prio(ns, prio, priority, max_table_size, num_groups,
34174adda112SMark Bloch 				 flags);
3418038d2ef8SMaor Gottlieb 
3419d4be3f44SYishai Hadas 	return prio;
3420038d2ef8SMaor Gottlieb }
3421038d2ef8SMaor Gottlieb 
3422a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
3423a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
3424a550ddfcSYishai Hadas 			    u32 underlay_qpn)
3425a550ddfcSYishai Hadas {
3426a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3427a550ddfcSYishai Hadas 					   spec->match_criteria,
3428a550ddfcSYishai Hadas 					   misc_parameters);
3429a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3430a550ddfcSYishai Hadas 					   misc_parameters);
3431a550ddfcSYishai Hadas 
3432a550ddfcSYishai Hadas 	if (underlay_qpn &&
3433a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3434a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
3435a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3436a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
3437a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3438a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
3439a550ddfcSYishai Hadas 	}
3440a550ddfcSYishai Hadas }
3441a550ddfcSYishai Hadas 
34425e95af5fSRaed Salem static int read_flow_counters(struct ib_device *ibdev,
34435e95af5fSRaed Salem 			      struct mlx5_read_counters_attr *read_attr)
34445e95af5fSRaed Salem {
34455e95af5fSRaed Salem 	struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
34465e95af5fSRaed Salem 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
34475e95af5fSRaed Salem 
34485e95af5fSRaed Salem 	return mlx5_fc_query(dev->mdev, fc,
34495e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_PACKETS],
34505e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_BYTES]);
34515e95af5fSRaed Salem }
34525e95af5fSRaed Salem 
34535e95af5fSRaed Salem /* flow counters currently expose two counters packets and bytes */
34545e95af5fSRaed Salem #define FLOW_COUNTERS_NUM 2
34553b3233fbSRaed Salem static int counters_set_description(struct ib_counters *counters,
34563b3233fbSRaed Salem 				    enum mlx5_ib_counters_type counters_type,
34573b3233fbSRaed Salem 				    struct mlx5_ib_flow_counters_desc *desc_data,
34583b3233fbSRaed Salem 				    u32 ncounters)
34593b3233fbSRaed Salem {
34603b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
34613b3233fbSRaed Salem 	u32 cntrs_max_index = 0;
34623b3233fbSRaed Salem 	int i;
34633b3233fbSRaed Salem 
34643b3233fbSRaed Salem 	if (counters_type != MLX5_IB_COUNTERS_FLOW)
34653b3233fbSRaed Salem 		return -EINVAL;
34663b3233fbSRaed Salem 
34673b3233fbSRaed Salem 	/* init the fields for the object */
34683b3233fbSRaed Salem 	mcounters->type = counters_type;
34695e95af5fSRaed Salem 	mcounters->read_counters = read_flow_counters;
34705e95af5fSRaed Salem 	mcounters->counters_num = FLOW_COUNTERS_NUM;
34713b3233fbSRaed Salem 	mcounters->ncounters = ncounters;
34723b3233fbSRaed Salem 	/* each counter entry have both description and index pair */
34733b3233fbSRaed Salem 	for (i = 0; i < ncounters; i++) {
34743b3233fbSRaed Salem 		if (desc_data[i].description > IB_COUNTER_BYTES)
34753b3233fbSRaed Salem 			return -EINVAL;
34763b3233fbSRaed Salem 
34773b3233fbSRaed Salem 		if (cntrs_max_index <= desc_data[i].index)
34783b3233fbSRaed Salem 			cntrs_max_index = desc_data[i].index + 1;
34793b3233fbSRaed Salem 	}
34803b3233fbSRaed Salem 
34813b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
34823b3233fbSRaed Salem 	mcounters->counters_data = desc_data;
34833b3233fbSRaed Salem 	mcounters->cntrs_max_index = cntrs_max_index;
34843b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
34853b3233fbSRaed Salem 
34863b3233fbSRaed Salem 	return 0;
34873b3233fbSRaed Salem }
34883b3233fbSRaed Salem 
34893b3233fbSRaed Salem #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
34903b3233fbSRaed Salem static int flow_counters_set_data(struct ib_counters *ibcounters,
34913b3233fbSRaed Salem 				  struct mlx5_ib_create_flow *ucmd)
34923b3233fbSRaed Salem {
34933b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
34943b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
34953b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc_data = NULL;
34963b3233fbSRaed Salem 	bool hw_hndl = false;
34973b3233fbSRaed Salem 	int ret = 0;
34983b3233fbSRaed Salem 
34993b3233fbSRaed Salem 	if (ucmd && ucmd->ncounters_data != 0) {
35003b3233fbSRaed Salem 		cntrs_data = ucmd->data;
35013b3233fbSRaed Salem 		if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
35023b3233fbSRaed Salem 			return -EINVAL;
35033b3233fbSRaed Salem 
35043b3233fbSRaed Salem 		desc_data = kcalloc(cntrs_data->ncounters,
35053b3233fbSRaed Salem 				    sizeof(*desc_data),
35063b3233fbSRaed Salem 				    GFP_KERNEL);
35073b3233fbSRaed Salem 		if (!desc_data)
35083b3233fbSRaed Salem 			return  -ENOMEM;
35093b3233fbSRaed Salem 
35103b3233fbSRaed Salem 		if (copy_from_user(desc_data,
35113b3233fbSRaed Salem 				   u64_to_user_ptr(cntrs_data->counters_data),
35123b3233fbSRaed Salem 				   sizeof(*desc_data) * cntrs_data->ncounters)) {
35133b3233fbSRaed Salem 			ret = -EFAULT;
35143b3233fbSRaed Salem 			goto free;
35153b3233fbSRaed Salem 		}
35163b3233fbSRaed Salem 	}
35173b3233fbSRaed Salem 
35183b3233fbSRaed Salem 	if (!mcounters->hw_cntrs_hndl) {
35193b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = mlx5_fc_create(
35203b3233fbSRaed Salem 			to_mdev(ibcounters->device)->mdev, false);
3521e31abf76Sweiyongjun (A) 		if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3522e31abf76Sweiyongjun (A) 			ret = PTR_ERR(mcounters->hw_cntrs_hndl);
35233b3233fbSRaed Salem 			goto free;
35243b3233fbSRaed Salem 		}
35253b3233fbSRaed Salem 		hw_hndl = true;
35263b3233fbSRaed Salem 	}
35273b3233fbSRaed Salem 
35283b3233fbSRaed Salem 	if (desc_data) {
35293b3233fbSRaed Salem 		/* counters already bound to at least one flow */
35303b3233fbSRaed Salem 		if (mcounters->cntrs_max_index) {
35313b3233fbSRaed Salem 			ret = -EINVAL;
35323b3233fbSRaed Salem 			goto free_hndl;
35333b3233fbSRaed Salem 		}
35343b3233fbSRaed Salem 
35353b3233fbSRaed Salem 		ret = counters_set_description(ibcounters,
35363b3233fbSRaed Salem 					       MLX5_IB_COUNTERS_FLOW,
35373b3233fbSRaed Salem 					       desc_data,
35383b3233fbSRaed Salem 					       cntrs_data->ncounters);
35393b3233fbSRaed Salem 		if (ret)
35403b3233fbSRaed Salem 			goto free_hndl;
35413b3233fbSRaed Salem 
35423b3233fbSRaed Salem 	} else if (!mcounters->cntrs_max_index) {
35433b3233fbSRaed Salem 		/* counters not bound yet, must have udata passed */
35443b3233fbSRaed Salem 		ret = -EINVAL;
35453b3233fbSRaed Salem 		goto free_hndl;
35463b3233fbSRaed Salem 	}
35473b3233fbSRaed Salem 
35483b3233fbSRaed Salem 	return 0;
35493b3233fbSRaed Salem 
35503b3233fbSRaed Salem free_hndl:
35513b3233fbSRaed Salem 	if (hw_hndl) {
35523b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
35533b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
35543b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = NULL;
35553b3233fbSRaed Salem 	}
35563b3233fbSRaed Salem free:
35573b3233fbSRaed Salem 	kfree(desc_data);
35583b3233fbSRaed Salem 	return ret;
35593b3233fbSRaed Salem }
35603b3233fbSRaed Salem 
3561669ff1e3SJianbo Liu static void mlx5_ib_set_rule_source_port(struct mlx5_ib_dev *dev,
3562669ff1e3SJianbo Liu 					 struct mlx5_flow_spec *spec,
3563669ff1e3SJianbo Liu 					 struct mlx5_eswitch_rep *rep)
3564669ff1e3SJianbo Liu {
3565669ff1e3SJianbo Liu 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
3566669ff1e3SJianbo Liu 	void *misc;
3567669ff1e3SJianbo Liu 
3568669ff1e3SJianbo Liu 	if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
3569669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3570669ff1e3SJianbo Liu 				    misc_parameters_2);
3571669ff1e3SJianbo Liu 
3572669ff1e3SJianbo Liu 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
3573669ff1e3SJianbo Liu 			 mlx5_eswitch_get_vport_metadata_for_match(esw,
3574669ff1e3SJianbo Liu 								   rep->vport));
3575669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3576669ff1e3SJianbo Liu 				    misc_parameters_2);
3577669ff1e3SJianbo Liu 
35780f0d3827SPaul Blakey 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
35790f0d3827SPaul Blakey 			 mlx5_eswitch_get_vport_metadata_mask());
3580669ff1e3SJianbo Liu 	} else {
3581669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3582669ff1e3SJianbo Liu 				    misc_parameters);
3583669ff1e3SJianbo Liu 
3584669ff1e3SJianbo Liu 		MLX5_SET(fte_match_set_misc, misc, source_port, rep->vport);
3585669ff1e3SJianbo Liu 
3586669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3587669ff1e3SJianbo Liu 				    misc_parameters);
3588669ff1e3SJianbo Liu 
3589669ff1e3SJianbo Liu 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3590669ff1e3SJianbo Liu 	}
3591669ff1e3SJianbo Liu }
3592669ff1e3SJianbo Liu 
3593a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3594038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
3595dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
3596a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
35973b3233fbSRaed Salem 						      u32 underlay_qpn,
35983b3233fbSRaed Salem 						      struct mlx5_ib_create_flow *ucmd)
3599038d2ef8SMaor Gottlieb {
3600038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
3601038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
3602bb0ee7dcSJianbo Liu 	struct mlx5_flow_act flow_act = {};
3603c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
36043b3233fbSRaed Salem 	struct mlx5_flow_destination dest_arr[2] = {};
36053b3233fbSRaed Salem 	struct mlx5_flow_destination *rule_dst = dest_arr;
3606dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3607038d2ef8SMaor Gottlieb 	unsigned int spec_index;
360871c6e863SAriel Levkovich 	u32 prev_type = 0;
3609038d2ef8SMaor Gottlieb 	int err = 0;
36103b3233fbSRaed Salem 	int dest_num = 0;
3611802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3612038d2ef8SMaor Gottlieb 
361319cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
3614038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3615038d2ef8SMaor Gottlieb 
36166a4d00beSMark Bloch 	if (dev->is_rep && is_egress)
361778dd0c43SMark Bloch 		return ERR_PTR(-EINVAL);
361878dd0c43SMark Bloch 
36191b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3620038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3621c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
3622038d2ef8SMaor Gottlieb 		err = -ENOMEM;
3623038d2ef8SMaor Gottlieb 		goto free;
3624038d2ef8SMaor Gottlieb 	}
3625038d2ef8SMaor Gottlieb 
3626038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
3627038d2ef8SMaor Gottlieb 
3628038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
3629bb0ee7dcSJianbo Liu 		err = parse_flow_attr(dev->mdev, spec,
363071c6e863SAriel Levkovich 				      ib_flow, flow_attr, &flow_act,
363171c6e863SAriel Levkovich 				      prev_type);
3632038d2ef8SMaor Gottlieb 		if (err < 0)
3633038d2ef8SMaor Gottlieb 			goto free;
3634038d2ef8SMaor Gottlieb 
363571c6e863SAriel Levkovich 		prev_type = ((union ib_flow_spec *)ib_flow)->type;
3636038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3637038d2ef8SMaor Gottlieb 	}
3638038d2ef8SMaor Gottlieb 
3639ed9085feSMaor Gottlieb 	if (dst && !(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP)) {
3640ed9085feSMaor Gottlieb 		memcpy(&dest_arr[0], dst, sizeof(*dst));
3641ed9085feSMaor Gottlieb 		dest_num++;
3642ed9085feSMaor Gottlieb 	}
3643ed9085feSMaor Gottlieb 
3644a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
3645a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
3646a550ddfcSYishai Hadas 
36476a4d00beSMark Bloch 	if (dev->is_rep) {
3648669ff1e3SJianbo Liu 		struct mlx5_eswitch_rep *rep;
3649018a94eeSMark Bloch 
3650669ff1e3SJianbo Liu 		rep = dev->port[flow_attr->port - 1].rep;
3651669ff1e3SJianbo Liu 		if (!rep) {
36526a4d00beSMark Bloch 			err = -EINVAL;
36536a4d00beSMark Bloch 			goto free;
36546a4d00beSMark Bloch 		}
3655669ff1e3SJianbo Liu 
3656669ff1e3SJianbo Liu 		mlx5_ib_set_rule_source_port(dev, spec, rep);
3657018a94eeSMark Bloch 	}
3658018a94eeSMark Bloch 
3659466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3660802c2125SAviad Yehezkel 
3661802c2125SAviad Yehezkel 	if (is_egress &&
3662802c2125SAviad Yehezkel 	    !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3663802c2125SAviad Yehezkel 		err = -EINVAL;
3664802c2125SAviad Yehezkel 		goto free;
3665802c2125SAviad Yehezkel 	}
3666802c2125SAviad Yehezkel 
36673b3233fbSRaed Salem 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3668171c7625SMark Bloch 		struct mlx5_ib_mcounters *mcounters;
3669171c7625SMark Bloch 
36703b3233fbSRaed Salem 		err = flow_counters_set_data(flow_act.counters, ucmd);
36713b3233fbSRaed Salem 		if (err)
36723b3233fbSRaed Salem 			goto free;
36733b3233fbSRaed Salem 
3674171c7625SMark Bloch 		mcounters = to_mcounters(flow_act.counters);
36753b3233fbSRaed Salem 		handler->ibcounters = flow_act.counters;
36763b3233fbSRaed Salem 		dest_arr[dest_num].type =
36773b3233fbSRaed Salem 			MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3678171c7625SMark Bloch 		dest_arr[dest_num].counter_id =
3679171c7625SMark Bloch 			mlx5_fc_id(mcounters->hw_cntrs_hndl);
36803b3233fbSRaed Salem 		dest_num++;
36813b3233fbSRaed Salem 	}
36823b3233fbSRaed Salem 
3683075572d4SBoris Pismenny 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
3684ed9085feSMaor Gottlieb 		if (!dest_num)
3685a22ed86cSSlava Shwartsman 			rule_dst = NULL;
3686a22ed86cSSlava Shwartsman 	} else {
368714c129e3SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)
368814c129e3SMaor Gottlieb 			flow_act.action |=
368914c129e3SMaor Gottlieb 				MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3690802c2125SAviad Yehezkel 		if (is_egress)
3691802c2125SAviad Yehezkel 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
369214c129e3SMaor Gottlieb 		else if (dest_num)
369314c129e3SMaor Gottlieb 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3694a22ed86cSSlava Shwartsman 	}
36952ac693f9SMoses Reuben 
3696bb0ee7dcSJianbo Liu 	if ((spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG)  &&
36972ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
36982ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
36992ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3700bb0ee7dcSJianbo Liu 			     spec->flow_context.flow_tag, flow_attr->type);
37012ac693f9SMoses Reuben 		err = -EINVAL;
37022ac693f9SMoses Reuben 		goto free;
37032ac693f9SMoses Reuben 	}
370474491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
370566958ed9SHadar Hen Zion 					    &flow_act,
3706a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
3707038d2ef8SMaor Gottlieb 
3708038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
3709038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
3710038d2ef8SMaor Gottlieb 		goto free;
3711038d2ef8SMaor Gottlieb 	}
3712038d2ef8SMaor Gottlieb 
3713d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
37145497adc6SMaor Gottlieb 	handler->prio = ft_prio;
3715d4be3f44SYishai Hadas 	handler->dev = dev;
3716038d2ef8SMaor Gottlieb 
3717038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
3718038d2ef8SMaor Gottlieb free:
37193b3233fbSRaed Salem 	if (err && handler) {
37203b3233fbSRaed Salem 		if (handler->ibcounters &&
37213b3233fbSRaed Salem 		    atomic_read(&handler->ibcounters->usecnt) == 1)
37223b3233fbSRaed Salem 			counters_clear_description(handler->ibcounters);
3723038d2ef8SMaor Gottlieb 		kfree(handler);
37243b3233fbSRaed Salem 	}
3725c5bb1730SMaor Gottlieb 	kvfree(spec);
3726038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
3727038d2ef8SMaor Gottlieb }
3728038d2ef8SMaor Gottlieb 
3729a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3730a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
3731a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
3732a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
3733a550ddfcSYishai Hadas {
37343b3233fbSRaed Salem 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
3735a550ddfcSYishai Hadas }
3736a550ddfcSYishai Hadas 
3737038d2ef8SMaor Gottlieb enum {
3738038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
3739038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
3740038d2ef8SMaor Gottlieb };
3741038d2ef8SMaor Gottlieb 
3742038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3743038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
3744038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
3745038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
3746038d2ef8SMaor Gottlieb {
3747038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
3748038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3749038d2ef8SMaor Gottlieb 
3750038d2ef8SMaor Gottlieb 	static struct {
3751038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
3752038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
3753038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
3754038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
3755038d2ef8SMaor Gottlieb 			.flow_attr = {
3756038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3757038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3758038d2ef8SMaor Gottlieb 			},
3759038d2ef8SMaor Gottlieb 			.eth_flow = {
3760038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3761038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3762038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3763038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
3764038d2ef8SMaor Gottlieb 			}
3765038d2ef8SMaor Gottlieb 		},
3766038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
3767038d2ef8SMaor Gottlieb 			.flow_attr = {
3768038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3769038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3770038d2ef8SMaor Gottlieb 			},
3771038d2ef8SMaor Gottlieb 			.eth_flow = {
3772038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3773038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3774038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3775038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
3776038d2ef8SMaor Gottlieb 			}
3777038d2ef8SMaor Gottlieb 		}
3778038d2ef8SMaor Gottlieb 	};
3779038d2ef8SMaor Gottlieb 
3780038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
3781038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
3782038d2ef8SMaor Gottlieb 				   dst);
3783038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
3784038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3785038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
3786038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3787038d2ef8SMaor Gottlieb 						 dst);
3788038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
378974491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3790d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
3791038d2ef8SMaor Gottlieb 			kfree(handler);
3792038d2ef8SMaor Gottlieb 			handler = handler_ucast;
3793038d2ef8SMaor Gottlieb 		} else {
3794038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
3795038d2ef8SMaor Gottlieb 		}
3796038d2ef8SMaor Gottlieb 	}
3797038d2ef8SMaor Gottlieb 
3798038d2ef8SMaor Gottlieb 	return handler;
3799038d2ef8SMaor Gottlieb }
3800038d2ef8SMaor Gottlieb 
3801cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3802cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
3803cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
3804cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
3805cc0e5d42SMaor Gottlieb {
3806cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
3807cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
3808cc0e5d42SMaor Gottlieb 	int err;
3809cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
3810cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
3811cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
3812cc0e5d42SMaor Gottlieb 	};
3813cc0e5d42SMaor Gottlieb 
3814cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3815cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
3816cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
3817cc0e5d42SMaor Gottlieb 		goto err;
3818cc0e5d42SMaor Gottlieb 	}
3819cc0e5d42SMaor Gottlieb 
3820cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3821cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
3822cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
3823cc0e5d42SMaor Gottlieb 		goto err_tx;
3824cc0e5d42SMaor Gottlieb 	}
3825cc0e5d42SMaor Gottlieb 
3826cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
3827cc0e5d42SMaor Gottlieb 
3828cc0e5d42SMaor Gottlieb 	return handler_rx;
3829cc0e5d42SMaor Gottlieb 
3830cc0e5d42SMaor Gottlieb err_tx:
383174491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
3832cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
3833cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
3834cc0e5d42SMaor Gottlieb err:
3835cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
3836cc0e5d42SMaor Gottlieb }
3837cc0e5d42SMaor Gottlieb 
3838038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3839038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
384059082a32SMatan Barak 					   int domain,
384159082a32SMatan Barak 					   struct ib_udata *udata)
3842038d2ef8SMaor Gottlieb {
3843038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3844d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
3845038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3846038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
3847cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3848038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
3849802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
38503b3233fbSRaed Salem 	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
38513b3233fbSRaed Salem 	size_t min_ucmd_sz, required_ucmd_sz;
3852038d2ef8SMaor Gottlieb 	int err;
3853a550ddfcSYishai Hadas 	int underlay_qpn;
3854038d2ef8SMaor Gottlieb 
38553b3233fbSRaed Salem 	if (udata && udata->inlen) {
38563b3233fbSRaed Salem 		min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
38573b3233fbSRaed Salem 				sizeof(ucmd_hdr.reserved);
38583b3233fbSRaed Salem 		if (udata->inlen < min_ucmd_sz)
385959082a32SMatan Barak 			return ERR_PTR(-EOPNOTSUPP);
386059082a32SMatan Barak 
38613b3233fbSRaed Salem 		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
38623b3233fbSRaed Salem 		if (err)
38633b3233fbSRaed Salem 			return ERR_PTR(err);
38643b3233fbSRaed Salem 
38653b3233fbSRaed Salem 		/* currently supports only one counters data */
38663b3233fbSRaed Salem 		if (ucmd_hdr.ncounters_data > 1)
38673b3233fbSRaed Salem 			return ERR_PTR(-EINVAL);
38683b3233fbSRaed Salem 
38693b3233fbSRaed Salem 		required_ucmd_sz = min_ucmd_sz +
38703b3233fbSRaed Salem 			sizeof(struct mlx5_ib_flow_counters_data) *
38713b3233fbSRaed Salem 			ucmd_hdr.ncounters_data;
38723b3233fbSRaed Salem 		if (udata->inlen > required_ucmd_sz &&
38733b3233fbSRaed Salem 		    !ib_is_udata_cleared(udata, required_ucmd_sz,
38743b3233fbSRaed Salem 					 udata->inlen - required_ucmd_sz))
38753b3233fbSRaed Salem 			return ERR_PTR(-EOPNOTSUPP);
38763b3233fbSRaed Salem 
38773b3233fbSRaed Salem 		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
38783b3233fbSRaed Salem 		if (!ucmd)
38793b3233fbSRaed Salem 			return ERR_PTR(-ENOMEM);
38803b3233fbSRaed Salem 
38813b3233fbSRaed Salem 		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
3882299eafeeSGustavo A. R. Silva 		if (err)
3883299eafeeSGustavo A. R. Silva 			goto free_ucmd;
38843b3233fbSRaed Salem 	}
38853b3233fbSRaed Salem 
3886299eafeeSGustavo A. R. Silva 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3887299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3888299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3889299eafeeSGustavo A. R. Silva 	}
3890038d2ef8SMaor Gottlieb 
3891038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
3892508562d6SDaniel Jurgens 	    flow_attr->port > dev->num_ports ||
3893802c2125SAviad Yehezkel 	    (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3894299eafeeSGustavo A. R. Silva 				  IB_FLOW_ATTR_FLAGS_EGRESS))) {
3895299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3896299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3897299eafeeSGustavo A. R. Silva 	}
3898802c2125SAviad Yehezkel 
3899802c2125SAviad Yehezkel 	if (is_egress &&
3900802c2125SAviad Yehezkel 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3901299eafeeSGustavo A. R. Silva 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3902299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3903299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3904299eafeeSGustavo A. R. Silva 	}
3905038d2ef8SMaor Gottlieb 
3906038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3907299eafeeSGustavo A. R. Silva 	if (!dst) {
3908299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3909299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3910299eafeeSGustavo A. R. Silva 	}
3911038d2ef8SMaor Gottlieb 
39129a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3913038d2ef8SMaor Gottlieb 
3914802c2125SAviad Yehezkel 	ft_prio = get_flow_table(dev, flow_attr,
3915802c2125SAviad Yehezkel 				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3916038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
3917038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
3918038d2ef8SMaor Gottlieb 		goto unlock;
3919038d2ef8SMaor Gottlieb 	}
3920cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3921cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3922cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
3923cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
3924cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
3925cc0e5d42SMaor Gottlieb 			goto destroy_ft;
3926cc0e5d42SMaor Gottlieb 		}
3927cc0e5d42SMaor Gottlieb 	}
3928038d2ef8SMaor Gottlieb 
3929802c2125SAviad Yehezkel 	if (is_egress) {
3930802c2125SAviad Yehezkel 		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3931802c2125SAviad Yehezkel 	} else {
3932038d2ef8SMaor Gottlieb 		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
39332be08c30SLeon Romanovsky 		if (mqp->is_rss)
3934d9f88e5aSYishai Hadas 			dst->tir_num = mqp->rss_qp.tirn;
3935d9f88e5aSYishai Hadas 		else
3936d9f88e5aSYishai Hadas 			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3937802c2125SAviad Yehezkel 	}
3938038d2ef8SMaor Gottlieb 
3939038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
39402be08c30SLeon Romanovsky 		underlay_qpn = (mqp->flags & IB_QP_CREATE_SOURCE_QPN) ?
394114c129e3SMaor Gottlieb 				       mqp->underlay_qpn :
394214c129e3SMaor Gottlieb 				       0;
394314c129e3SMaor Gottlieb 		handler = _create_flow_rule(dev, ft_prio, flow_attr, dst,
394414c129e3SMaor Gottlieb 					    underlay_qpn, ucmd);
3945038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3946038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3947038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3948038d2ef8SMaor Gottlieb 						dst);
3949cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3950cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3951038d2ef8SMaor Gottlieb 	} else {
3952038d2ef8SMaor Gottlieb 		err = -EINVAL;
3953038d2ef8SMaor Gottlieb 		goto destroy_ft;
3954038d2ef8SMaor Gottlieb 	}
3955038d2ef8SMaor Gottlieb 
3956038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
3957038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
3958038d2ef8SMaor Gottlieb 		handler = NULL;
3959038d2ef8SMaor Gottlieb 		goto destroy_ft;
3960038d2ef8SMaor Gottlieb 	}
3961038d2ef8SMaor Gottlieb 
39629a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3963038d2ef8SMaor Gottlieb 	kfree(dst);
39643b3233fbSRaed Salem 	kfree(ucmd);
3965038d2ef8SMaor Gottlieb 
3966038d2ef8SMaor Gottlieb 	return &handler->ibflow;
3967038d2ef8SMaor Gottlieb 
3968038d2ef8SMaor Gottlieb destroy_ft:
3969038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
3970cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
3971cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
3972038d2ef8SMaor Gottlieb unlock:
39739a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3974038d2ef8SMaor Gottlieb 	kfree(dst);
3975299eafeeSGustavo A. R. Silva free_ucmd:
39763b3233fbSRaed Salem 	kfree(ucmd);
3977038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
3978038d2ef8SMaor Gottlieb }
3979038d2ef8SMaor Gottlieb 
3980b47fd4ffSMark Bloch static struct mlx5_ib_flow_prio *
3981b47fd4ffSMark Bloch _get_flow_table(struct mlx5_ib_dev *dev,
3982b47fd4ffSMark Bloch 		struct mlx5_ib_flow_matcher *fs_matcher,
3983b47fd4ffSMark Bloch 		bool mcast)
3984d4be3f44SYishai Hadas {
3985d4be3f44SYishai Hadas 	struct mlx5_flow_namespace *ns = NULL;
398613a43765SMark Bloch 	struct mlx5_ib_flow_prio *prio = NULL;
398713a43765SMark Bloch 	int max_table_size = 0;
3988cecae747SMaor Gottlieb 	bool esw_encap;
3989b47fd4ffSMark Bloch 	u32 flags = 0;
3990b47fd4ffSMark Bloch 	int priority;
3991d4be3f44SYishai Hadas 
399213a43765SMark Bloch 	if (mcast)
399313a43765SMark Bloch 		priority = MLX5_IB_FLOW_MCAST_PRIO;
399413a43765SMark Bloch 	else
399513a43765SMark Bloch 		priority = ib_prio_to_core_prio(fs_matcher->priority, false);
399613a43765SMark Bloch 
3997cecae747SMaor Gottlieb 	esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
3998cecae747SMaor Gottlieb 		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
3999b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
4000d4be3f44SYishai Hadas 		max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
4001d4be3f44SYishai Hadas 					log_max_ft_size));
4002cecae747SMaor Gottlieb 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap) && !esw_encap)
4003b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
4004b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
4005cecae747SMaor Gottlieb 					      reformat_l3_tunnel_to_l2) &&
4006cecae747SMaor Gottlieb 		    !esw_encap)
4007b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
400813a43765SMark Bloch 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS) {
400913a43765SMark Bloch 		max_table_size = BIT(
401013a43765SMark Bloch 			MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, log_max_ft_size));
4011cecae747SMaor Gottlieb 		if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat) && !esw_encap)
4012b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
401313a43765SMark Bloch 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB) {
401413a43765SMark Bloch 		max_table_size = BIT(
401513a43765SMark Bloch 			MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, log_max_ft_size));
401609d985beSMaor Gottlieb 		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, decap) && esw_encap)
401709d985beSMaor Gottlieb 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
401809d985beSMaor Gottlieb 		if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, reformat_l3_tunnel_to_l2) &&
401909d985beSMaor Gottlieb 		    esw_encap)
402009d985beSMaor Gottlieb 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
402113a43765SMark Bloch 		priority = FDB_BYPASS_PATH;
4022d8abe884SMark Zhang 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX) {
4023d8abe884SMark Zhang 		max_table_size =
4024d8abe884SMark Zhang 			BIT(MLX5_CAP_FLOWTABLE_RDMA_RX(dev->mdev,
4025d8abe884SMark Zhang 						       log_max_ft_size));
4026d8abe884SMark Zhang 		priority = fs_matcher->priority;
4027af9c3841SMichael Guralnik 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_TX) {
4028af9c3841SMichael Guralnik 		max_table_size =
4029af9c3841SMichael Guralnik 			BIT(MLX5_CAP_FLOWTABLE_RDMA_TX(dev->mdev,
4030af9c3841SMichael Guralnik 						       log_max_ft_size));
4031af9c3841SMichael Guralnik 		priority = fs_matcher->priority;
4032b47fd4ffSMark Bloch 	}
4033b47fd4ffSMark Bloch 
40343b70508aSMark Bloch 	max_table_size = min_t(int, max_table_size, MLX5_FS_MAX_ENTRIES);
4035d4be3f44SYishai Hadas 
4036b47fd4ffSMark Bloch 	ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
4037d4be3f44SYishai Hadas 	if (!ns)
4038d4be3f44SYishai Hadas 		return ERR_PTR(-ENOTSUPP);
4039d4be3f44SYishai Hadas 
4040b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
4041d4be3f44SYishai Hadas 		prio = &dev->flow_db->prios[priority];
404213a43765SMark Bloch 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS)
4043b47fd4ffSMark Bloch 		prio = &dev->flow_db->egress_prios[priority];
404413a43765SMark Bloch 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB)
404513a43765SMark Bloch 		prio = &dev->flow_db->fdb;
4046d8abe884SMark Zhang 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX)
4047d8abe884SMark Zhang 		prio = &dev->flow_db->rdma_rx[priority];
4048af9c3841SMichael Guralnik 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_TX)
4049af9c3841SMichael Guralnik 		prio = &dev->flow_db->rdma_tx[priority];
405013a43765SMark Bloch 
405113a43765SMark Bloch 	if (!prio)
405213a43765SMark Bloch 		return ERR_PTR(-EINVAL);
4053d4be3f44SYishai Hadas 
4054d4be3f44SYishai Hadas 	if (prio->flow_table)
4055d4be3f44SYishai Hadas 		return prio;
4056d4be3f44SYishai Hadas 
40573b70508aSMark Bloch 	return _get_prio(ns, prio, priority, max_table_size,
4058b47fd4ffSMark Bloch 			 MLX5_FS_MAX_TYPES, flags);
4059d4be3f44SYishai Hadas }
4060d4be3f44SYishai Hadas 
4061d4be3f44SYishai Hadas static struct mlx5_ib_flow_handler *
4062d4be3f44SYishai Hadas _create_raw_flow_rule(struct mlx5_ib_dev *dev,
4063d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_prio *ft_prio,
4064d4be3f44SYishai Hadas 		      struct mlx5_flow_destination *dst,
4065d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_matcher  *fs_matcher,
4066bb0ee7dcSJianbo Liu 		      struct mlx5_flow_context *flow_context,
4067b823dd6dSMark Bloch 		      struct mlx5_flow_act *flow_act,
4068bfc5d839SMark Bloch 		      void *cmd_in, int inlen,
4069bfc5d839SMark Bloch 		      int dst_num)
4070d4be3f44SYishai Hadas {
4071d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
4072d4be3f44SYishai Hadas 	struct mlx5_flow_spec *spec;
4073d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft = ft_prio->flow_table;
4074d4be3f44SYishai Hadas 	int err = 0;
4075d4be3f44SYishai Hadas 
4076d4be3f44SYishai Hadas 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
4077d4be3f44SYishai Hadas 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
4078d4be3f44SYishai Hadas 	if (!handler || !spec) {
4079d4be3f44SYishai Hadas 		err = -ENOMEM;
4080d4be3f44SYishai Hadas 		goto free;
4081d4be3f44SYishai Hadas 	}
4082d4be3f44SYishai Hadas 
4083d4be3f44SYishai Hadas 	INIT_LIST_HEAD(&handler->list);
4084d4be3f44SYishai Hadas 
4085d4be3f44SYishai Hadas 	memcpy(spec->match_value, cmd_in, inlen);
4086d4be3f44SYishai Hadas 	memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
4087d4be3f44SYishai Hadas 	       fs_matcher->mask_len);
4088d4be3f44SYishai Hadas 	spec->match_criteria_enable = fs_matcher->match_criteria_enable;
4089bb0ee7dcSJianbo Liu 	spec->flow_context = *flow_context;
4090d4be3f44SYishai Hadas 
4091d4be3f44SYishai Hadas 	handler->rule = mlx5_add_flow_rules(ft, spec,
4092bfc5d839SMark Bloch 					    flow_act, dst, dst_num);
4093d4be3f44SYishai Hadas 
4094d4be3f44SYishai Hadas 	if (IS_ERR(handler->rule)) {
4095d4be3f44SYishai Hadas 		err = PTR_ERR(handler->rule);
4096d4be3f44SYishai Hadas 		goto free;
4097d4be3f44SYishai Hadas 	}
4098d4be3f44SYishai Hadas 
4099d4be3f44SYishai Hadas 	ft_prio->refcount++;
4100d4be3f44SYishai Hadas 	handler->prio = ft_prio;
4101d4be3f44SYishai Hadas 	handler->dev = dev;
4102d4be3f44SYishai Hadas 	ft_prio->flow_table = ft;
4103d4be3f44SYishai Hadas 
4104d4be3f44SYishai Hadas free:
4105d4be3f44SYishai Hadas 	if (err)
4106d4be3f44SYishai Hadas 		kfree(handler);
4107d4be3f44SYishai Hadas 	kvfree(spec);
4108d4be3f44SYishai Hadas 	return err ? ERR_PTR(err) : handler;
4109d4be3f44SYishai Hadas }
4110d4be3f44SYishai Hadas 
4111d4be3f44SYishai Hadas static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
4112d4be3f44SYishai Hadas 				void *match_v)
4113d4be3f44SYishai Hadas {
4114d4be3f44SYishai Hadas 	void *match_c;
4115d4be3f44SYishai Hadas 	void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
4116d4be3f44SYishai Hadas 	void *dmac, *dmac_mask;
4117d4be3f44SYishai Hadas 	void *ipv4, *ipv4_mask;
4118d4be3f44SYishai Hadas 
4119d4be3f44SYishai Hadas 	if (!(fs_matcher->match_criteria_enable &
4120d4be3f44SYishai Hadas 	      (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
4121d4be3f44SYishai Hadas 		return false;
4122d4be3f44SYishai Hadas 
4123d4be3f44SYishai Hadas 	match_c = fs_matcher->matcher_mask.match_params;
4124d4be3f44SYishai Hadas 	match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
4125d4be3f44SYishai Hadas 					   outer_headers);
4126d4be3f44SYishai Hadas 	match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
4127d4be3f44SYishai Hadas 					   outer_headers);
4128d4be3f44SYishai Hadas 
4129d4be3f44SYishai Hadas 	dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4130d4be3f44SYishai Hadas 			    dmac_47_16);
4131d4be3f44SYishai Hadas 	dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4132d4be3f44SYishai Hadas 				 dmac_47_16);
4133d4be3f44SYishai Hadas 
4134d4be3f44SYishai Hadas 	if (is_multicast_ether_addr(dmac) &&
4135d4be3f44SYishai Hadas 	    is_multicast_ether_addr(dmac_mask))
4136d4be3f44SYishai Hadas 		return true;
4137d4be3f44SYishai Hadas 
4138d4be3f44SYishai Hadas 	ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4139d4be3f44SYishai Hadas 			    dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4140d4be3f44SYishai Hadas 
4141d4be3f44SYishai Hadas 	ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4142d4be3f44SYishai Hadas 				 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4143d4be3f44SYishai Hadas 
4144d4be3f44SYishai Hadas 	if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
4145d4be3f44SYishai Hadas 	    ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
4146d4be3f44SYishai Hadas 		return true;
4147d4be3f44SYishai Hadas 
4148d4be3f44SYishai Hadas 	return false;
4149d4be3f44SYishai Hadas }
4150d4be3f44SYishai Hadas 
415132269441SYishai Hadas struct mlx5_ib_flow_handler *
415232269441SYishai Hadas mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
415332269441SYishai Hadas 			struct mlx5_ib_flow_matcher *fs_matcher,
4154bb0ee7dcSJianbo Liu 			struct mlx5_flow_context *flow_context,
4155b823dd6dSMark Bloch 			struct mlx5_flow_act *flow_act,
4156bfc5d839SMark Bloch 			u32 counter_id,
415732269441SYishai Hadas 			void *cmd_in, int inlen, int dest_id,
415832269441SYishai Hadas 			int dest_type)
415932269441SYishai Hadas {
4160d4be3f44SYishai Hadas 	struct mlx5_flow_destination *dst;
4161d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *ft_prio;
4162d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
4163bfc5d839SMark Bloch 	int dst_num = 0;
4164d4be3f44SYishai Hadas 	bool mcast;
4165d4be3f44SYishai Hadas 	int err;
4166d4be3f44SYishai Hadas 
4167d4be3f44SYishai Hadas 	if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
416832269441SYishai Hadas 		return ERR_PTR(-EOPNOTSUPP);
4169d4be3f44SYishai Hadas 
4170d4be3f44SYishai Hadas 	if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
4171d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
4172d4be3f44SYishai Hadas 
41738e8aa145SGustavo A. R. Silva 	dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
4174d4be3f44SYishai Hadas 	if (!dst)
4175d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
4176d4be3f44SYishai Hadas 
4177d4be3f44SYishai Hadas 	mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
4178d4be3f44SYishai Hadas 	mutex_lock(&dev->flow_db->lock);
4179d4be3f44SYishai Hadas 
4180b47fd4ffSMark Bloch 	ft_prio = _get_flow_table(dev, fs_matcher, mcast);
4181d4be3f44SYishai Hadas 	if (IS_ERR(ft_prio)) {
4182d4be3f44SYishai Hadas 		err = PTR_ERR(ft_prio);
4183d4be3f44SYishai Hadas 		goto unlock;
4184d4be3f44SYishai Hadas 	}
4185d4be3f44SYishai Hadas 
41866346f0bfSYishai Hadas 	if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
4187bfc5d839SMark Bloch 		dst[dst_num].type = dest_type;
41888c112a5fSMaor Gottlieb 		dst[dst_num++].tir_num = dest_id;
4189b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
4190a7ee18bdSMark Bloch 	} else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
4191bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
41928c112a5fSMaor Gottlieb 		dst[dst_num++].ft_num = dest_id;
4193b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
41948c112a5fSMaor Gottlieb 	} else  if (dest_type == MLX5_FLOW_DESTINATION_TYPE_PORT) {
41958c112a5fSMaor Gottlieb 		dst[dst_num++].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
4196a7ee18bdSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
41976346f0bfSYishai Hadas 	}
41986346f0bfSYishai Hadas 
4199bfc5d839SMark Bloch 
4200bfc5d839SMark Bloch 	if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
4201bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
4202bfc5d839SMark Bloch 		dst[dst_num].counter_id = counter_id;
4203bfc5d839SMark Bloch 		dst_num++;
4204bfc5d839SMark Bloch 	}
4205bfc5d839SMark Bloch 
4206bb0ee7dcSJianbo Liu 	handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher,
4207bb0ee7dcSJianbo Liu 					flow_context, flow_act,
4208bfc5d839SMark Bloch 					cmd_in, inlen, dst_num);
4209d4be3f44SYishai Hadas 
4210d4be3f44SYishai Hadas 	if (IS_ERR(handler)) {
4211d4be3f44SYishai Hadas 		err = PTR_ERR(handler);
4212d4be3f44SYishai Hadas 		goto destroy_ft;
4213d4be3f44SYishai Hadas 	}
4214d4be3f44SYishai Hadas 
4215d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
4216d4be3f44SYishai Hadas 	atomic_inc(&fs_matcher->usecnt);
4217d4be3f44SYishai Hadas 	handler->flow_matcher = fs_matcher;
4218d4be3f44SYishai Hadas 
4219d4be3f44SYishai Hadas 	kfree(dst);
4220d4be3f44SYishai Hadas 
4221d4be3f44SYishai Hadas 	return handler;
4222d4be3f44SYishai Hadas 
4223d4be3f44SYishai Hadas destroy_ft:
4224d4be3f44SYishai Hadas 	put_flow_table(dev, ft_prio, false);
4225d4be3f44SYishai Hadas unlock:
4226d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
4227d4be3f44SYishai Hadas 	kfree(dst);
4228d4be3f44SYishai Hadas 
4229d4be3f44SYishai Hadas 	return ERR_PTR(err);
423032269441SYishai Hadas }
423132269441SYishai Hadas 
4232c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
4233c6475a0bSAviad Yehezkel {
4234c6475a0bSAviad Yehezkel 	u32 flags = 0;
4235c6475a0bSAviad Yehezkel 
4236c6475a0bSAviad Yehezkel 	if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
4237c6475a0bSAviad Yehezkel 		flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
4238c6475a0bSAviad Yehezkel 
4239c6475a0bSAviad Yehezkel 	return flags;
4240c6475a0bSAviad Yehezkel }
4241c6475a0bSAviad Yehezkel 
4242c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED	MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
4243c6475a0bSAviad Yehezkel static struct ib_flow_action *
4244c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device,
4245c6475a0bSAviad Yehezkel 			       const struct ib_flow_action_attrs_esp *attr,
4246c6475a0bSAviad Yehezkel 			       struct uverbs_attr_bundle *attrs)
4247c6475a0bSAviad Yehezkel {
4248c6475a0bSAviad Yehezkel 	struct mlx5_ib_dev *mdev = to_mdev(device);
4249c6475a0bSAviad Yehezkel 	struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
4250c6475a0bSAviad Yehezkel 	struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
4251c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *action;
4252c6475a0bSAviad Yehezkel 	u64 action_flags;
4253c6475a0bSAviad Yehezkel 	u64 flags;
4254c6475a0bSAviad Yehezkel 	int err = 0;
4255c6475a0bSAviad Yehezkel 
4256bccd0622SJason Gunthorpe 	err = uverbs_get_flags64(
4257bccd0622SJason Gunthorpe 		&action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4258bccd0622SJason Gunthorpe 		((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
4259bccd0622SJason Gunthorpe 	if (err)
4260bccd0622SJason Gunthorpe 		return ERR_PTR(err);
4261c6475a0bSAviad Yehezkel 
4262c6475a0bSAviad Yehezkel 	flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
4263c6475a0bSAviad Yehezkel 
4264c6475a0bSAviad Yehezkel 	/* We current only support a subset of the standard features. Only a
4265c6475a0bSAviad Yehezkel 	 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
4266c6475a0bSAviad Yehezkel 	 * (with overlap). Full offload mode isn't supported.
4267c6475a0bSAviad Yehezkel 	 */
4268c6475a0bSAviad Yehezkel 	if (!attr->keymat || attr->replay || attr->encap ||
4269c6475a0bSAviad Yehezkel 	    attr->spi || attr->seq || attr->tfc_pad ||
4270c6475a0bSAviad Yehezkel 	    attr->hard_limit_pkts ||
4271c6475a0bSAviad Yehezkel 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4272c6475a0bSAviad Yehezkel 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
4273c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4274c6475a0bSAviad Yehezkel 
4275c6475a0bSAviad Yehezkel 	if (attr->keymat->protocol !=
4276c6475a0bSAviad Yehezkel 	    IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
4277c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4278c6475a0bSAviad Yehezkel 
4279c6475a0bSAviad Yehezkel 	aes_gcm = &attr->keymat->keymat.aes_gcm;
4280c6475a0bSAviad Yehezkel 
4281c6475a0bSAviad Yehezkel 	if (aes_gcm->icv_len != 16 ||
4282c6475a0bSAviad Yehezkel 	    aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
4283c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4284c6475a0bSAviad Yehezkel 
4285c6475a0bSAviad Yehezkel 	action = kmalloc(sizeof(*action), GFP_KERNEL);
4286c6475a0bSAviad Yehezkel 	if (!action)
4287c6475a0bSAviad Yehezkel 		return ERR_PTR(-ENOMEM);
4288c6475a0bSAviad Yehezkel 
4289c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4290c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
4291c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.aes_key));
4292c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
4293c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
4294c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.salt));
4295c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
4296c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
4297c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
4298c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
4299c6475a0bSAviad Yehezkel 	accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
4300c6475a0bSAviad Yehezkel 
4301c6475a0bSAviad Yehezkel 	accel_attrs.esn = attr->esn;
4302c6475a0bSAviad Yehezkel 	if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4303c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4304c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4305c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4306c6475a0bSAviad Yehezkel 
4307c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4308c6475a0bSAviad Yehezkel 		accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4309c6475a0bSAviad Yehezkel 
4310c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ctx =
4311c6475a0bSAviad Yehezkel 		mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4312c6475a0bSAviad Yehezkel 	if (IS_ERR(action->esp_aes_gcm.ctx)) {
4313c6475a0bSAviad Yehezkel 		err = PTR_ERR(action->esp_aes_gcm.ctx);
4314c6475a0bSAviad Yehezkel 		goto err_parse;
4315c6475a0bSAviad Yehezkel 	}
4316c6475a0bSAviad Yehezkel 
4317c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4318c6475a0bSAviad Yehezkel 
4319c6475a0bSAviad Yehezkel 	return &action->ib_action;
4320c6475a0bSAviad Yehezkel 
4321c6475a0bSAviad Yehezkel err_parse:
4322c6475a0bSAviad Yehezkel 	kfree(action);
4323c6475a0bSAviad Yehezkel 	return ERR_PTR(err);
4324c6475a0bSAviad Yehezkel }
4325c6475a0bSAviad Yehezkel 
4326349705c1SMatan Barak static int
4327349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4328349705c1SMatan Barak 			       const struct ib_flow_action_attrs_esp *attr,
4329349705c1SMatan Barak 			       struct uverbs_attr_bundle *attrs)
4330349705c1SMatan Barak {
4331349705c1SMatan Barak 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4332349705c1SMatan Barak 	struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4333349705c1SMatan Barak 	int err = 0;
4334349705c1SMatan Barak 
4335349705c1SMatan Barak 	if (attr->keymat || attr->replay || attr->encap ||
4336349705c1SMatan Barak 	    attr->spi || attr->seq || attr->tfc_pad ||
4337349705c1SMatan Barak 	    attr->hard_limit_pkts ||
4338349705c1SMatan Barak 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4339349705c1SMatan Barak 			     IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4340349705c1SMatan Barak 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4341349705c1SMatan Barak 		return -EOPNOTSUPP;
4342349705c1SMatan Barak 
4343349705c1SMatan Barak 	/* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4344349705c1SMatan Barak 	 * be modified.
4345349705c1SMatan Barak 	 */
4346349705c1SMatan Barak 	if (!(maction->esp_aes_gcm.ib_flags &
4347349705c1SMatan Barak 	      IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4348349705c1SMatan Barak 	    attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4349349705c1SMatan Barak 			   IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4350349705c1SMatan Barak 		return -EINVAL;
4351349705c1SMatan Barak 
4352349705c1SMatan Barak 	memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4353349705c1SMatan Barak 	       sizeof(accel_attrs));
4354349705c1SMatan Barak 
4355349705c1SMatan Barak 	accel_attrs.esn = attr->esn;
4356349705c1SMatan Barak 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4357349705c1SMatan Barak 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4358349705c1SMatan Barak 	else
4359349705c1SMatan Barak 		accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4360349705c1SMatan Barak 
4361349705c1SMatan Barak 	err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4362349705c1SMatan Barak 					 &accel_attrs);
4363349705c1SMatan Barak 	if (err)
4364349705c1SMatan Barak 		return err;
4365349705c1SMatan Barak 
4366349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags &=
4367349705c1SMatan Barak 		~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4368349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags |=
4369349705c1SMatan Barak 		attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4370349705c1SMatan Barak 
4371349705c1SMatan Barak 	return 0;
4372349705c1SMatan Barak }
4373349705c1SMatan Barak 
4374c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4375c6475a0bSAviad Yehezkel {
4376c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4377c6475a0bSAviad Yehezkel 
4378c6475a0bSAviad Yehezkel 	switch (action->type) {
4379c6475a0bSAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
4380c6475a0bSAviad Yehezkel 		/*
4381c6475a0bSAviad Yehezkel 		 * We only support aes_gcm by now, so we implicitly know this is
4382c6475a0bSAviad Yehezkel 		 * the underline crypto.
4383c6475a0bSAviad Yehezkel 		 */
4384c6475a0bSAviad Yehezkel 		mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4385c6475a0bSAviad Yehezkel 		break;
4386b4749bf2SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
4387b4749bf2SMark Bloch 		mlx5_ib_destroy_flow_action_raw(maction);
4388b4749bf2SMark Bloch 		break;
4389c6475a0bSAviad Yehezkel 	default:
4390c6475a0bSAviad Yehezkel 		WARN_ON(true);
4391c6475a0bSAviad Yehezkel 		break;
4392c6475a0bSAviad Yehezkel 	}
4393c6475a0bSAviad Yehezkel 
4394c6475a0bSAviad Yehezkel 	kfree(maction);
4395c6475a0bSAviad Yehezkel 	return 0;
4396c6475a0bSAviad Yehezkel }
4397c6475a0bSAviad Yehezkel 
4398e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4399e126ba97SEli Cohen {
4400e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
440181e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
4402e126ba97SEli Cohen 	int err;
4403539ec982SYishai Hadas 	u16 uid;
4404539ec982SYishai Hadas 
4405539ec982SYishai Hadas 	uid = ibqp->pd ?
4406539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4407e126ba97SEli Cohen 
44082be08c30SLeon Romanovsky 	if (mqp->flags & IB_QP_CREATE_SOURCE_QPN) {
440981e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
441081e30880SYishai Hadas 		return -EOPNOTSUPP;
441181e30880SYishai Hadas 	}
441281e30880SYishai Hadas 
4413539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4414e126ba97SEli Cohen 	if (err)
4415e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4416e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4417e126ba97SEli Cohen 
4418e126ba97SEli Cohen 	return err;
4419e126ba97SEli Cohen }
4420e126ba97SEli Cohen 
4421e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4422e126ba97SEli Cohen {
4423e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4424e126ba97SEli Cohen 	int err;
4425539ec982SYishai Hadas 	u16 uid;
4426e126ba97SEli Cohen 
4427539ec982SYishai Hadas 	uid = ibqp->pd ?
4428539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4429539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4430e126ba97SEli Cohen 	if (err)
4431e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4432e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4433e126ba97SEli Cohen 
4434e126ba97SEli Cohen 	return err;
4435e126ba97SEli Cohen }
4436e126ba97SEli Cohen 
4437e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
4438e126ba97SEli Cohen {
44391b5daf11SMajd Dibbiny 	int err;
4440e126ba97SEli Cohen 
44411b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
4442e126ba97SEli Cohen 	if (err)
4443e126ba97SEli Cohen 		return err;
44441b5daf11SMajd Dibbiny 
44451b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
44461b5daf11SMajd Dibbiny 
44471b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
4448e126ba97SEli Cohen }
4449e126ba97SEli Cohen 
4450508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
4451508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4452e126ba97SEli Cohen {
4453e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
445454747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4455e126ba97SEli Cohen 
44569603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
4457e126ba97SEli Cohen }
4458508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
4459e126ba97SEli Cohen 
4460508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
4461e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
4462e126ba97SEli Cohen {
4463e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
446454747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4465e126ba97SEli Cohen 
44666aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
4467e126ba97SEli Cohen }
4468508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
4469e126ba97SEli Cohen 
4470508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
4471508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4472e126ba97SEli Cohen {
4473e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
447454747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
447554747231SParav Pandit 
44769603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
4477e126ba97SEli Cohen }
4478508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
4479e126ba97SEli Cohen 
4480508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
4481508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
4482e126ba97SEli Cohen {
4483e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
448454747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
448554747231SParav Pandit 
44869603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
4487e126ba97SEli Cohen }
4488508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
4489e126ba97SEli Cohen 
4490508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
4491508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4492e126ba97SEli Cohen {
4493e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
449454747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
449554747231SParav Pandit 
4496e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
44979603b61dSJack Morgenstein 		       dev->mdev->board_id);
4498e126ba97SEli Cohen }
4499508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
4500e126ba97SEli Cohen 
4501508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
4502508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
4503508a523fSParav Pandit 	&dev_attr_hca_type.attr,
4504508a523fSParav Pandit 	&dev_attr_board_id.attr,
4505508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
4506508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
4507508a523fSParav Pandit 	NULL,
4508508a523fSParav Pandit };
4509e126ba97SEli Cohen 
4510508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
4511508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
4512e126ba97SEli Cohen };
4513e126ba97SEli Cohen 
45147722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
45157722f47eSHaggai Eran {
45167722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
45177722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
45187722f47eSHaggai Eran 			     pkey_change_work);
45197722f47eSHaggai Eran 
45207722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
45217722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
45227722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
45237722f47eSHaggai Eran }
45247722f47eSHaggai Eran 
452589ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
452689ea94a7SMaor Gottlieb {
452789ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
452889ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
452989ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
453089ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
453189ea94a7SMaor Gottlieb 	unsigned long flags_qp;
453289ea94a7SMaor Gottlieb 	unsigned long flags_cq;
453389ea94a7SMaor Gottlieb 	unsigned long flags;
453489ea94a7SMaor Gottlieb 
453589ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
453689ea94a7SMaor Gottlieb 
453789ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
453889ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
453989ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
454089ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
454189ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
454289ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
454389ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
454489ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
454589ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
454689ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
454789ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
454889ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
454989ea94a7SMaor Gottlieb 						      &cq_armed_list);
455089ea94a7SMaor Gottlieb 				}
455189ea94a7SMaor Gottlieb 			}
455289ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
455389ea94a7SMaor Gottlieb 		}
455489ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
455589ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
455689ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
455789ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
455889ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
455989ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
456089ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
456189ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
456289ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
456389ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
456489ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
456589ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
456689ea94a7SMaor Gottlieb 							      &cq_armed_list);
456789ea94a7SMaor Gottlieb 					}
456889ea94a7SMaor Gottlieb 				}
456989ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
457089ea94a7SMaor Gottlieb 						       flags_cq);
457189ea94a7SMaor Gottlieb 			}
457289ea94a7SMaor Gottlieb 		}
457389ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
457489ea94a7SMaor Gottlieb 	}
457589ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
457689ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
457789ea94a7SMaor Gottlieb 	 */
457889ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
45794e0e2ea1SYishai Hadas 		mcq->comp(mcq, NULL);
458089ea94a7SMaor Gottlieb 	}
458189ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
458289ea94a7SMaor Gottlieb }
458389ea94a7SMaor Gottlieb 
458403404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
458503404e8aSMaor Gottlieb {
458603404e8aSMaor Gottlieb 	int err;
458703404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
458803404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
458903404e8aSMaor Gottlieb 			     delay_drop_work);
459003404e8aSMaor Gottlieb 
4591fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
4592fe248c3aSMaor Gottlieb 
459303404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
4594333fbaa0SLeon Romanovsky 	err = mlx5_core_set_delay_drop(delay_drop->dev, delay_drop->timeout);
459503404e8aSMaor Gottlieb 	if (err) {
459603404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
459703404e8aSMaor Gottlieb 			     delay_drop->timeout);
459803404e8aSMaor Gottlieb 		delay_drop->activate = false;
459903404e8aSMaor Gottlieb 	}
460003404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
460103404e8aSMaor Gottlieb }
460203404e8aSMaor Gottlieb 
460309e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
460409e574faSSaeed Mahameed 				 struct ib_event *ibev)
460509e574faSSaeed Mahameed {
46066cfdc7e4SAya Levin 	u8 port = (eqe->data.port.port >> 4) & 0xf;
46076cfdc7e4SAya Levin 
460809e574faSSaeed Mahameed 	switch (eqe->sub_type) {
460909e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
46106cfdc7e4SAya Levin 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
46116cfdc7e4SAya Levin 					    IB_LINK_LAYER_ETHERNET)
461209e574faSSaeed Mahameed 			schedule_work(&ibdev->delay_drop.delay_drop_work);
461309e574faSSaeed Mahameed 		break;
461409e574faSSaeed Mahameed 	default: /* do nothing */
461509e574faSSaeed Mahameed 		return;
461609e574faSSaeed Mahameed 	}
461709e574faSSaeed Mahameed }
461809e574faSSaeed Mahameed 
4619134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4620134e9349SSaeed Mahameed 			      struct ib_event *ibev)
4621134e9349SSaeed Mahameed {
4622134e9349SSaeed Mahameed 	u8 port = (eqe->data.port.port >> 4) & 0xf;
4623134e9349SSaeed Mahameed 
4624134e9349SSaeed Mahameed 	ibev->element.port_num = port;
4625134e9349SSaeed Mahameed 
4626134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
4627134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4628134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4629134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4630134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
4631134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
4632134e9349SSaeed Mahameed 		 */
4633134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4634134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
4635134e9349SSaeed Mahameed 			return -EINVAL;
4636134e9349SSaeed Mahameed 
4637134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4638134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4639134e9349SSaeed Mahameed 		break;
4640134e9349SSaeed Mahameed 
4641134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
4642134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
4643134e9349SSaeed Mahameed 		break;
4644134e9349SSaeed Mahameed 
4645134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4646134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
4647134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4648134e9349SSaeed Mahameed 		break;
4649134e9349SSaeed Mahameed 
4650134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4651134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
4652134e9349SSaeed Mahameed 		break;
4653134e9349SSaeed Mahameed 
4654134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4655134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
4656134e9349SSaeed Mahameed 		break;
4657134e9349SSaeed Mahameed 	default:
4658134e9349SSaeed Mahameed 		return -EINVAL;
4659134e9349SSaeed Mahameed 	}
4660134e9349SSaeed Mahameed 
4661134e9349SSaeed Mahameed 	return 0;
4662134e9349SSaeed Mahameed }
4663134e9349SSaeed Mahameed 
4664d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
4665e126ba97SEli Cohen {
4666d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
4667d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
4668d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
4669e126ba97SEli Cohen 	struct ib_event ibev;
4670dbaaff2aSEli Cohen 	bool fatal = false;
4671e126ba97SEli Cohen 
4672df097a27SSaeed Mahameed 	if (work->is_slave) {
4673df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
4674d69a24e0SDaniel Jurgens 		if (!ibdev)
4675d69a24e0SDaniel Jurgens 			goto out;
4676d69a24e0SDaniel Jurgens 	} else {
4677df097a27SSaeed Mahameed 		ibdev = work->dev;
4678d69a24e0SDaniel Jurgens 	}
4679d69a24e0SDaniel Jurgens 
4680d69a24e0SDaniel Jurgens 	switch (work->event) {
4681e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
4682e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
468389ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
4684134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
4685dbaaff2aSEli Cohen 		fatal = true;
4686e126ba97SEli Cohen 		break;
4687134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
4688134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
4689d69a24e0SDaniel Jurgens 			goto out;
4690e126ba97SEli Cohen 		break;
469109e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
469209e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
469309e574faSSaeed Mahameed 		/* fall through */
4694bdc37924SSaeed Mahameed 	default:
469503404e8aSMaor Gottlieb 		goto out;
4696e126ba97SEli Cohen 	}
4697e126ba97SEli Cohen 
4698e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
4699e126ba97SEli Cohen 
4700134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4701134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
470203404e8aSMaor Gottlieb 		goto out;
4703a0c84c32SEli Cohen 	}
4704a0c84c32SEli Cohen 
4705e126ba97SEli Cohen 	if (ibdev->ib_active)
4706e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
4707dbaaff2aSEli Cohen 
4708dbaaff2aSEli Cohen 	if (fatal)
4709dbaaff2aSEli Cohen 		ibdev->ib_active = false;
471003404e8aSMaor Gottlieb out:
4711d69a24e0SDaniel Jurgens 	kfree(work);
4712d69a24e0SDaniel Jurgens }
4713d69a24e0SDaniel Jurgens 
4714df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
4715df097a27SSaeed Mahameed 			 unsigned long event, void *param)
4716d69a24e0SDaniel Jurgens {
4717d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
4718d69a24e0SDaniel Jurgens 
4719d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
472010bea9c8SLeon Romanovsky 	if (!work)
4721df097a27SSaeed Mahameed 		return NOTIFY_DONE;
472210bea9c8SLeon Romanovsky 
4723d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4724df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4725df097a27SSaeed Mahameed 	work->is_slave = false;
4726d69a24e0SDaniel Jurgens 	work->param = param;
4727d69a24e0SDaniel Jurgens 	work->event = event;
4728d69a24e0SDaniel Jurgens 
4729d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
4730df097a27SSaeed Mahameed 
4731df097a27SSaeed Mahameed 	return NOTIFY_OK;
4732df097a27SSaeed Mahameed }
4733df097a27SSaeed Mahameed 
4734df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4735df097a27SSaeed Mahameed 				    unsigned long event, void *param)
4736df097a27SSaeed Mahameed {
4737df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
4738df097a27SSaeed Mahameed 
4739df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
4740df097a27SSaeed Mahameed 	if (!work)
4741df097a27SSaeed Mahameed 		return NOTIFY_DONE;
4742df097a27SSaeed Mahameed 
4743df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4744df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4745df097a27SSaeed Mahameed 	work->is_slave = true;
4746df097a27SSaeed Mahameed 	work->param = param;
4747df097a27SSaeed Mahameed 	work->event = event;
4748df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
4749df097a27SSaeed Mahameed 
4750df097a27SSaeed Mahameed 	return NOTIFY_OK;
4751d69a24e0SDaniel Jurgens }
4752d69a24e0SDaniel Jurgens 
4753c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4754c43f1112SMaor Gottlieb {
4755c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
4756c43f1112SMaor Gottlieb 	int err;
4757c43f1112SMaor Gottlieb 	int port;
4758c43f1112SMaor Gottlieb 
4759a989ea01SMark Bloch 	for (port = 1; port <= ARRAY_SIZE(dev->mdev->port_caps); port++) {
4760c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
4761c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4762c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
4763c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4764c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
4765c43f1112SMaor Gottlieb 								   port, 0,
4766c43f1112SMaor Gottlieb 								   &vport_ctx);
4767c43f1112SMaor Gottlieb 				if (err) {
4768c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4769c43f1112SMaor Gottlieb 						    port, err);
4770c43f1112SMaor Gottlieb 					return err;
4771c43f1112SMaor Gottlieb 				}
4772c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
4773c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
4774c43f1112SMaor Gottlieb 			} else {
4775c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
4776c43f1112SMaor Gottlieb 			}
4777c43f1112SMaor Gottlieb 		}
4778c43f1112SMaor Gottlieb 	}
4779c43f1112SMaor Gottlieb 	return 0;
4780c43f1112SMaor Gottlieb }
4781c43f1112SMaor Gottlieb 
4782e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4783e126ba97SEli Cohen {
4784e126ba97SEli Cohen 	int port;
4785e126ba97SEli Cohen 
4786508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
4787e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
4788e126ba97SEli Cohen }
4789e126ba97SEli Cohen 
479026628e2dSMark Bloch static int __get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4791e126ba97SEli Cohen {
4792e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
4793e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
4794f614fc15SDan Carpenter 	int err = -ENOMEM;
4795e126ba97SEli Cohen 
479650ba3c18SLeon Romanovsky 	pprops = kzalloc(sizeof(*pprops), GFP_KERNEL);
4797e126ba97SEli Cohen 	if (!pprops)
4798e126ba97SEli Cohen 		goto out;
4799e126ba97SEli Cohen 
4800e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4801e126ba97SEli Cohen 	if (!dprops)
4802e126ba97SEli Cohen 		goto out;
4803e126ba97SEli Cohen 
480448357091SLeon Romanovsky 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, NULL);
4805e126ba97SEli Cohen 	if (err) {
4806e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
4807e126ba97SEli Cohen 		goto out;
4808e126ba97SEli Cohen 	}
4809e126ba97SEli Cohen 
4810e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4811e126ba97SEli Cohen 	if (err) {
4812938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
4813938fe83cSSaeed Mahameed 			     port, err);
481432f69e4bSDaniel Jurgens 		goto out;
4815e126ba97SEli Cohen 	}
481632f69e4bSDaniel Jurgens 
4817938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
4818938fe83cSSaeed Mahameed 					dprops->max_pkeys;
4819938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
4820938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
482132f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
482232f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
4823e126ba97SEli Cohen 
4824e126ba97SEli Cohen out:
4825e126ba97SEli Cohen 	kfree(pprops);
4826e126ba97SEli Cohen 	kfree(dprops);
4827e126ba97SEli Cohen 
4828e126ba97SEli Cohen 	return err;
4829e126ba97SEli Cohen }
4830e126ba97SEli Cohen 
483126628e2dSMark Bloch static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
483226628e2dSMark Bloch {
483326628e2dSMark Bloch 	/* For representors use port 1, is this is the only native
483426628e2dSMark Bloch 	 * port
483526628e2dSMark Bloch 	 */
483626628e2dSMark Bloch 	if (dev->is_rep)
483726628e2dSMark Bloch 		return __get_port_caps(dev, 1);
483826628e2dSMark Bloch 	return __get_port_caps(dev, port);
483926628e2dSMark Bloch }
484026628e2dSMark Bloch 
4841e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4842e126ba97SEli Cohen {
4843e126ba97SEli Cohen 	int err;
4844e126ba97SEli Cohen 
4845e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
4846e126ba97SEli Cohen 	if (err)
4847e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4848e126ba97SEli Cohen 
484932927e28SMark Bloch 	if (dev->umrc.qp)
4850c4367a26SShamir Rabinovitch 		mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
485132927e28SMark Bloch 	if (dev->umrc.cq)
4852add08d76SChristoph Hellwig 		ib_free_cq(dev->umrc.cq);
485332927e28SMark Bloch 	if (dev->umrc.pd)
4854e126ba97SEli Cohen 		ib_dealloc_pd(dev->umrc.pd);
4855e126ba97SEli Cohen }
4856e126ba97SEli Cohen 
4857e126ba97SEli Cohen enum {
4858e126ba97SEli Cohen 	MAX_UMR_WR = 128,
4859e126ba97SEli Cohen };
4860e126ba97SEli Cohen 
4861e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
4862e126ba97SEli Cohen {
4863e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
4864e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
4865e126ba97SEli Cohen 	struct ib_pd *pd;
4866e126ba97SEli Cohen 	struct ib_cq *cq;
4867e126ba97SEli Cohen 	struct ib_qp *qp;
4868e126ba97SEli Cohen 	int ret;
4869e126ba97SEli Cohen 
4870e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4871e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4872e126ba97SEli Cohen 	if (!attr || !init_attr) {
4873e126ba97SEli Cohen 		ret = -ENOMEM;
4874e126ba97SEli Cohen 		goto error_0;
4875e126ba97SEli Cohen 	}
4876e126ba97SEli Cohen 
4877ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
4878e126ba97SEli Cohen 	if (IS_ERR(pd)) {
4879e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4880e126ba97SEli Cohen 		ret = PTR_ERR(pd);
4881e126ba97SEli Cohen 		goto error_0;
4882e126ba97SEli Cohen 	}
4883e126ba97SEli Cohen 
4884add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
4885e126ba97SEli Cohen 	if (IS_ERR(cq)) {
4886e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4887e126ba97SEli Cohen 		ret = PTR_ERR(cq);
4888e126ba97SEli Cohen 		goto error_2;
4889e126ba97SEli Cohen 	}
4890e126ba97SEli Cohen 
4891e126ba97SEli Cohen 	init_attr->send_cq = cq;
4892e126ba97SEli Cohen 	init_attr->recv_cq = cq;
4893e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4894e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
4895e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
4896e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4897e126ba97SEli Cohen 	init_attr->port_num = 1;
4898e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4899e126ba97SEli Cohen 	if (IS_ERR(qp)) {
4900e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4901e126ba97SEli Cohen 		ret = PTR_ERR(qp);
4902e126ba97SEli Cohen 		goto error_3;
4903e126ba97SEli Cohen 	}
4904e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
4905e126ba97SEli Cohen 	qp->real_qp    = qp;
4906e126ba97SEli Cohen 	qp->uobject    = NULL;
4907e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
490831fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
490931fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
4910e126ba97SEli Cohen 
4911e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
4912e126ba97SEli Cohen 	attr->port_num = 1;
4913e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4914e126ba97SEli Cohen 				IB_QP_PORT, NULL);
4915e126ba97SEli Cohen 	if (ret) {
4916e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4917e126ba97SEli Cohen 		goto error_4;
4918e126ba97SEli Cohen 	}
4919e126ba97SEli Cohen 
4920e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4921e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
4922e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
4923e126ba97SEli Cohen 
4924e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4925e126ba97SEli Cohen 	if (ret) {
4926e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4927e126ba97SEli Cohen 		goto error_4;
4928e126ba97SEli Cohen 	}
4929e126ba97SEli Cohen 
4930e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4931e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
4932e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4933e126ba97SEli Cohen 	if (ret) {
4934e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4935e126ba97SEli Cohen 		goto error_4;
4936e126ba97SEli Cohen 	}
4937e126ba97SEli Cohen 
4938e126ba97SEli Cohen 	dev->umrc.qp = qp;
4939e126ba97SEli Cohen 	dev->umrc.cq = cq;
4940e126ba97SEli Cohen 	dev->umrc.pd = pd;
4941e126ba97SEli Cohen 
4942e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
4943e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
4944e126ba97SEli Cohen 	if (ret) {
4945e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4946e126ba97SEli Cohen 		goto error_4;
4947e126ba97SEli Cohen 	}
4948e126ba97SEli Cohen 
4949e126ba97SEli Cohen 	kfree(attr);
4950e126ba97SEli Cohen 	kfree(init_attr);
4951e126ba97SEli Cohen 
4952e126ba97SEli Cohen 	return 0;
4953e126ba97SEli Cohen 
4954e126ba97SEli Cohen error_4:
4955c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_qp(qp, NULL);
495632927e28SMark Bloch 	dev->umrc.qp = NULL;
4957e126ba97SEli Cohen 
4958e126ba97SEli Cohen error_3:
4959add08d76SChristoph Hellwig 	ib_free_cq(cq);
496032927e28SMark Bloch 	dev->umrc.cq = NULL;
4961e126ba97SEli Cohen 
4962e126ba97SEli Cohen error_2:
4963e126ba97SEli Cohen 	ib_dealloc_pd(pd);
496432927e28SMark Bloch 	dev->umrc.pd = NULL;
4965e126ba97SEli Cohen 
4966e126ba97SEli Cohen error_0:
4967e126ba97SEli Cohen 	kfree(attr);
4968e126ba97SEli Cohen 	kfree(init_attr);
4969e126ba97SEli Cohen 	return ret;
4970e126ba97SEli Cohen }
4971e126ba97SEli Cohen 
49726e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
49736e8484c5SMax Gurtovoy {
49746e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
49756e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
49766e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
49776e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
49786e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
49796e8484c5SMax Gurtovoy 	default:
49806e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
49816e8484c5SMax Gurtovoy 	}
49826e8484c5SMax Gurtovoy }
49836e8484c5SMax Gurtovoy 
4984e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
4985e126ba97SEli Cohen {
4986e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
4987e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
498821a428a0SLeon Romanovsky 	struct ib_device *ibdev;
4989bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
49907722f47eSHaggai Eran 	int port;
4991e126ba97SEli Cohen 	int ret = 0;
4992e126ba97SEli Cohen 
4993e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
499421a428a0SLeon Romanovsky 	ibdev = &dev->ib_dev;
4995e126ba97SEli Cohen 
4996f4375443SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, xrc))
4997f4375443SLeon Romanovsky 		return -EOPNOTSUPP;
4998f4375443SLeon Romanovsky 
4999d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
5000d16e91daSHaggai Eran 
500121a428a0SLeon Romanovsky 	devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
500221a428a0SLeon Romanovsky 	if (!devr->p0)
500321a428a0SLeon Romanovsky 		return -ENOMEM;
500421a428a0SLeon Romanovsky 
500521a428a0SLeon Romanovsky 	devr->p0->device  = ibdev;
5006e126ba97SEli Cohen 	devr->p0->uobject = NULL;
5007e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
5008e126ba97SEli Cohen 
5009ff23dfa1SShamir Rabinovitch 	ret = mlx5_ib_alloc_pd(devr->p0, NULL);
501021a428a0SLeon Romanovsky 	if (ret)
501121a428a0SLeon Romanovsky 		goto error0;
501221a428a0SLeon Romanovsky 
5013e39afe3dSLeon Romanovsky 	devr->c0 = rdma_zalloc_drv_obj(ibdev, ib_cq);
5014e39afe3dSLeon Romanovsky 	if (!devr->c0) {
5015e39afe3dSLeon Romanovsky 		ret = -ENOMEM;
5016e126ba97SEli Cohen 		goto error1;
5017e126ba97SEli Cohen 	}
5018e39afe3dSLeon Romanovsky 
5019e126ba97SEli Cohen 	devr->c0->device = &dev->ib_dev;
5020e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
5021e126ba97SEli Cohen 
5022e39afe3dSLeon Romanovsky 	ret = mlx5_ib_create_cq(devr->c0, &cq_attr, NULL);
5023e39afe3dSLeon Romanovsky 	if (ret)
5024e39afe3dSLeon Romanovsky 		goto err_create_cq;
5025e39afe3dSLeon Romanovsky 
5026f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn0, 0);
5027f4375443SLeon Romanovsky 	if (ret)
5028e126ba97SEli Cohen 		goto error2;
5029e126ba97SEli Cohen 
5030f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn1, 0);
5031f4375443SLeon Romanovsky 	if (ret)
5032e126ba97SEli Cohen 		goto error3;
5033e126ba97SEli Cohen 
5034e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
5035e126ba97SEli Cohen 	attr.attr.max_sge = 1;
5036e126ba97SEli Cohen 	attr.attr.max_wr = 1;
5037e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
50381a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
5039e126ba97SEli Cohen 
504068e326deSLeon Romanovsky 	devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
504168e326deSLeon Romanovsky 	if (!devr->s0) {
504268e326deSLeon Romanovsky 		ret = -ENOMEM;
5043e126ba97SEli Cohen 		goto error4;
5044e126ba97SEli Cohen 	}
504568e326deSLeon Romanovsky 
5046e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
5047e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
5048e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
50491a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
505068e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
505168e326deSLeon Romanovsky 	if (ret)
505268e326deSLeon Romanovsky 		goto err_create;
505368e326deSLeon Romanovsky 
50541a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
5055e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
5056e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
5057e126ba97SEli Cohen 
50584aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
50594aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
50604aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
50614aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
506268e326deSLeon Romanovsky 	devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
506368e326deSLeon Romanovsky 	if (!devr->s1) {
506468e326deSLeon Romanovsky 		ret = -ENOMEM;
50654aa17b28SHaggai Abramonvsky 		goto error5;
50664aa17b28SHaggai Abramonvsky 	}
506768e326deSLeon Romanovsky 
50684aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
50694aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
50704aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
50711a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
507268e326deSLeon Romanovsky 
507368e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
507468e326deSLeon Romanovsky 	if (ret)
507568e326deSLeon Romanovsky 		goto error6;
507668e326deSLeon Romanovsky 
50774aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
50781a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
50794aa17b28SHaggai Abramonvsky 
50807722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
50817722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
50827722f47eSHaggai Eran 			  pkey_change_handler);
50837722f47eSHaggai Eran 		devr->ports[port].devr = devr;
50847722f47eSHaggai Eran 	}
50857722f47eSHaggai Eran 
5086e126ba97SEli Cohen 	return 0;
5087e126ba97SEli Cohen 
508868e326deSLeon Romanovsky error6:
508968e326deSLeon Romanovsky 	kfree(devr->s1);
50904aa17b28SHaggai Abramonvsky error5:
5091c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
509268e326deSLeon Romanovsky err_create:
509368e326deSLeon Romanovsky 	kfree(devr->s0);
5094e126ba97SEli Cohen error4:
5095f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
5096e126ba97SEli Cohen error3:
5097f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
5098e126ba97SEli Cohen error2:
5099c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
5100e39afe3dSLeon Romanovsky err_create_cq:
5101e39afe3dSLeon Romanovsky 	kfree(devr->c0);
5102e126ba97SEli Cohen error1:
5103c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
5104e126ba97SEli Cohen error0:
510521a428a0SLeon Romanovsky 	kfree(devr->p0);
5106e126ba97SEli Cohen 	return ret;
5107e126ba97SEli Cohen }
5108e126ba97SEli Cohen 
5109e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
5110e126ba97SEli Cohen {
5111f4375443SLeon Romanovsky 	struct mlx5_ib_dev *dev;
51127722f47eSHaggai Eran 	int port;
51137722f47eSHaggai Eran 
5114f4375443SLeon Romanovsky 	dev = container_of(devr, struct mlx5_ib_dev, devr);
5115f4375443SLeon Romanovsky 
5116c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s1, NULL);
511768e326deSLeon Romanovsky 	kfree(devr->s1);
5118c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
511968e326deSLeon Romanovsky 	kfree(devr->s0);
5120f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
5121f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
5122c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
5123e39afe3dSLeon Romanovsky 	kfree(devr->c0);
5124c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
512521a428a0SLeon Romanovsky 	kfree(devr->p0);
51267722f47eSHaggai Eran 
51277722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
51285d8f6a0eSMark Bloch 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
51297722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
5130e126ba97SEli Cohen }
5131e126ba97SEli Cohen 
5132b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
5133b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
5134e53505a8SAchiad Shochat {
5135e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5136e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
5137e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
5138e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
513985c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
5140e53505a8SAchiad Shochat 	u32 ret = 0;
5141e53505a8SAchiad Shochat 
5142b02289b3SArtemy Kovalyov 	if (rep->grh_required)
5143b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
5144b02289b3SArtemy Kovalyov 
5145e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
5146b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
5147e53505a8SAchiad Shochat 
514885c7c014SDaniel Jurgens 	if (raw_support)
5149b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
515072cd5717SOr Gerlitz 
5151e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
515272cd5717SOr Gerlitz 		return ret;
5153e53505a8SAchiad Shochat 
5154e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
515572cd5717SOr Gerlitz 		return ret;
5156e53505a8SAchiad Shochat 
5157e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
5158e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
5159e53505a8SAchiad Shochat 
5160e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
5161e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
5162e53505a8SAchiad Shochat 
5163e53505a8SAchiad Shochat 	return ret;
5164e53505a8SAchiad Shochat }
5165e53505a8SAchiad Shochat 
51667738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
51677738613eSIra Weiny 			       struct ib_port_immutable *immutable)
51687738613eSIra Weiny {
51697738613eSIra Weiny 	struct ib_port_attr attr;
5170ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5171ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
5172b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
51737738613eSIra Weiny 	int err;
51747738613eSIra Weiny 
5175c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
51767738613eSIra Weiny 	if (err)
51777738613eSIra Weiny 		return err;
51787738613eSIra Weiny 
5179b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
5180b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
5181b02289b3SArtemy Kovalyov 						   &rep);
5182b02289b3SArtemy Kovalyov 		if (err)
5183b02289b3SArtemy Kovalyov 			return err;
5184b02289b3SArtemy Kovalyov 	}
5185b02289b3SArtemy Kovalyov 
51867738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
51877738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
5188b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
5189337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
51907738613eSIra Weiny 
51917738613eSIra Weiny 	return 0;
51927738613eSIra Weiny }
51937738613eSIra Weiny 
51948e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
51958e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
51968e6efa3aSMark Bloch {
51978e6efa3aSMark Bloch 	struct ib_port_attr attr;
51988e6efa3aSMark Bloch 	int err;
51998e6efa3aSMark Bloch 
52008e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
52018e6efa3aSMark Bloch 
52028e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
52038e6efa3aSMark Bloch 	if (err)
52048e6efa3aSMark Bloch 		return err;
52058e6efa3aSMark Bloch 
52068e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
52078e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
52088e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
52098e6efa3aSMark Bloch 
52108e6efa3aSMark Bloch 	return 0;
52118e6efa3aSMark Bloch }
52128e6efa3aSMark Bloch 
52139abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
5214c7342823SIra Weiny {
5215c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
5216c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
52179abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
52189abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
52199abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
5220c7342823SIra Weiny }
5221c7342823SIra Weiny 
522245f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
52239ef9c640SAviv Heller {
52249ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
52259ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
52269ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
52279ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
52289ef9c640SAviv Heller 	int err;
52299ef9c640SAviv Heller 
52307c34ec19SAviv Heller 	if (!ns || !mlx5_lag_is_roce(mdev))
52319ef9c640SAviv Heller 		return 0;
52329ef9c640SAviv Heller 
52339ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
52349ef9c640SAviv Heller 	if (err)
52359ef9c640SAviv Heller 		return err;
52369ef9c640SAviv Heller 
52379ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
52389ef9c640SAviv Heller 	if (IS_ERR(ft)) {
52399ef9c640SAviv Heller 		err = PTR_ERR(ft);
52409ef9c640SAviv Heller 		goto err_destroy_vport_lag;
52419ef9c640SAviv Heller 	}
52429ef9c640SAviv Heller 
52439a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
52447c34ec19SAviv Heller 	dev->lag_active = true;
52459ef9c640SAviv Heller 	return 0;
52469ef9c640SAviv Heller 
52479ef9c640SAviv Heller err_destroy_vport_lag:
52489ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
52499ef9c640SAviv Heller 	return err;
52509ef9c640SAviv Heller }
52519ef9c640SAviv Heller 
525245f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
52539ef9c640SAviv Heller {
52549ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
52559ef9c640SAviv Heller 
52567c34ec19SAviv Heller 	if (dev->lag_active) {
52577c34ec19SAviv Heller 		dev->lag_active = false;
52587c34ec19SAviv Heller 
52599a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
52609a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
52619ef9c640SAviv Heller 
52629ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
52639ef9c640SAviv Heller 	}
52649ef9c640SAviv Heller }
52659ef9c640SAviv Heller 
52667fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5267fc24fc5eSAchiad Shochat {
5268e53505a8SAchiad Shochat 	int err;
5269e53505a8SAchiad Shochat 
527095579e78SMark Bloch 	dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
527195579e78SMark Bloch 	err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
52725ec8c83eSAviv Heller 	if (err) {
527395579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
5274e53505a8SAchiad Shochat 		return err;
52755ec8c83eSAviv Heller 	}
5276e53505a8SAchiad Shochat 
5277d012f5d6SOr Gerlitz 	return 0;
5278d012f5d6SOr Gerlitz }
5279d012f5d6SOr Gerlitz 
52807fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5281e126ba97SEli Cohen {
528295579e78SMark Bloch 	if (dev->port[port_num].roce.nb.notifier_call) {
528395579e78SMark Bloch 		unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
528495579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
5285e126ba97SEli Cohen 	}
5286e126ba97SEli Cohen }
5287e126ba97SEli Cohen 
5288e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
5289e126ba97SEli Cohen {
5290e126ba97SEli Cohen 	int err;
5291e126ba97SEli Cohen 
5292e53505a8SAchiad Shochat 	err = mlx5_nic_vport_enable_roce(dev->mdev);
5293e53505a8SAchiad Shochat 	if (err)
52948e6efa3aSMark Bloch 		return err;
5295e53505a8SAchiad Shochat 
529645f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
52979ef9c640SAviv Heller 	if (err)
52989ef9c640SAviv Heller 		goto err_disable_roce;
52999ef9c640SAviv Heller 
5300e53505a8SAchiad Shochat 	return 0;
5301e53505a8SAchiad Shochat 
53029ef9c640SAviv Heller err_disable_roce:
53039ef9c640SAviv Heller 	mlx5_nic_vport_disable_roce(dev->mdev);
53049ef9c640SAviv Heller 
5305e53505a8SAchiad Shochat 	return err;
5306fc24fc5eSAchiad Shochat }
5307fc24fc5eSAchiad Shochat 
530845f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
5309fc24fc5eSAchiad Shochat {
531045f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
5311e53505a8SAchiad Shochat 	mlx5_nic_vport_disable_roce(dev->mdev);
5312fc24fc5eSAchiad Shochat }
5313fc24fc5eSAchiad Shochat 
5314e1f24a79SParav Pandit struct mlx5_ib_counter {
53157c16f477SKamal Heib 	const char *name;
53167c16f477SKamal Heib 	size_t offset;
53177c16f477SKamal Heib };
53187c16f477SKamal Heib 
53197c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
53207c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
53217c16f477SKamal Heib 
5322e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
53237c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
53247c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
53257c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
53267c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
53277c16f477SKamal Heib };
53287c16f477SKamal Heib 
5329e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
53307c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
53317c16f477SKamal Heib };
53327c16f477SKamal Heib 
5333e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
53347c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
53357c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
53367c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
53377c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
53387c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
53397c16f477SKamal Heib };
53407c16f477SKamal Heib 
5341e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
5342e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
5343e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
5344e1f24a79SParav Pandit 
5345e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
5346e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
5347e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
5348e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
5349e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
5350e1f24a79SParav Pandit };
5351e1f24a79SParav Pandit 
535258dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
535358dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
535458dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
535558dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
535658dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
535758dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
535858dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
535958dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
536058dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
536158dcb60aSParav Pandit };
536258dcb60aSParav Pandit 
5363d7fab916SAvihai Horon static const struct mlx5_ib_counter roce_accl_cnts[] = {
5364d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_adp_retrans),
5365d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_adp_retrans_to),
5366d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_slow_restart),
5367d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_slow_restart_cnps),
5368d7fab916SAvihai Horon 	INIT_Q_COUNTER(roce_slow_restart_trans),
5369d7fab916SAvihai Horon };
5370d7fab916SAvihai Horon 
53719f876f3dSTalat Batheesh #define INIT_EXT_PPCNT_COUNTER(_name)		\
53729f876f3dSTalat Batheesh 	{ .name = #_name, .offset =	\
53739f876f3dSTalat Batheesh 	MLX5_BYTE_OFF(ppcnt_reg, \
53749f876f3dSTalat Batheesh 		      counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
53759f876f3dSTalat Batheesh 
53769f876f3dSTalat Batheesh static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
53779f876f3dSTalat Batheesh 	INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
53789f876f3dSTalat Batheesh };
53799f876f3dSTalat Batheesh 
53803e1f000fSParav Pandit static bool is_mdev_switchdev_mode(const struct mlx5_core_dev *mdev)
53813e1f000fSParav Pandit {
53823e1f000fSParav Pandit 	return MLX5_ESWITCH_MANAGER(mdev) &&
53833e1f000fSParav Pandit 	       mlx5_ib_eswitch_mode(mdev->priv.eswitch) ==
53843e1f000fSParav Pandit 		       MLX5_ESWITCH_OFFLOADS;
53853e1f000fSParav Pandit }
53863e1f000fSParav Pandit 
5387e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
53880837e86aSMark Bloch {
538966247fbbSLeon Romanovsky 	u32 in[MLX5_ST_SZ_DW(dealloc_q_counter_in)] = {};
53903e1f000fSParav Pandit 	int num_cnt_ports;
5391aac4492eSDaniel Jurgens 	int i;
53920837e86aSMark Bloch 
53933e1f000fSParav Pandit 	num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports;
53943e1f000fSParav Pandit 
539566247fbbSLeon Romanovsky 	MLX5_SET(dealloc_q_counter_in, in, opcode,
539666247fbbSLeon Romanovsky 		 MLX5_CMD_OP_DEALLOC_Q_COUNTER);
539766247fbbSLeon Romanovsky 
53983e1f000fSParav Pandit 	for (i = 0; i < num_cnt_ports; i++) {
5399bfd745f8SLeon Romanovsky 		if (dev->port[i].cnts.set_id) {
540066247fbbSLeon Romanovsky 			MLX5_SET(dealloc_q_counter_in, in, counter_set_id,
5401e1f24a79SParav Pandit 				 dev->port[i].cnts.set_id);
540266247fbbSLeon Romanovsky 			mlx5_cmd_exec_in(dev->mdev, dealloc_q_counter, in);
540366247fbbSLeon Romanovsky 		}
5404e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
5405e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
54067c16f477SKamal Heib 	}
54077c16f477SKamal Heib }
54087c16f477SKamal Heib 
5409e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5410e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
54117c16f477SKamal Heib {
54127c16f477SKamal Heib 	u32 num_counters;
54137c16f477SKamal Heib 
54147c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
54157c16f477SKamal Heib 
54167c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
54177c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
54187c16f477SKamal Heib 
54197c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
54207c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
542158dcb60aSParav Pandit 
542258dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
542358dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
542458dcb60aSParav Pandit 
5425d7fab916SAvihai Horon 	if (MLX5_CAP_GEN(dev->mdev, roce_accl))
5426d7fab916SAvihai Horon 		num_counters += ARRAY_SIZE(roce_accl_cnts);
5427d7fab916SAvihai Horon 
5428e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
54297c16f477SKamal Heib 
5430e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5431e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5432e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
5433e1f24a79SParav Pandit 	}
54349f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
54359f876f3dSTalat Batheesh 		cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
54369f876f3dSTalat Batheesh 		num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
54379f876f3dSTalat Batheesh 	}
5438e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5439e1f24a79SParav Pandit 	if (!cnts->names)
54407c16f477SKamal Heib 		return -ENOMEM;
54417c16f477SKamal Heib 
5442e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
5443e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
5444e1f24a79SParav Pandit 	if (!cnts->offsets)
54457c16f477SKamal Heib 		goto err_names;
54467c16f477SKamal Heib 
54477c16f477SKamal Heib 	return 0;
54487c16f477SKamal Heib 
54497c16f477SKamal Heib err_names:
5450e1f24a79SParav Pandit 	kfree(cnts->names);
5451aac4492eSDaniel Jurgens 	cnts->names = NULL;
54527c16f477SKamal Heib 	return -ENOMEM;
54537c16f477SKamal Heib }
54547c16f477SKamal Heib 
5455e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
54567c16f477SKamal Heib 				  const char **names,
54577c16f477SKamal Heib 				  size_t *offsets)
54587c16f477SKamal Heib {
54597c16f477SKamal Heib 	int i;
54607c16f477SKamal Heib 	int j = 0;
54617c16f477SKamal Heib 
54627c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
54637c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
54647c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
54657c16f477SKamal Heib 	}
54667c16f477SKamal Heib 
54677c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
54687c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
54697c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
54707c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
54717c16f477SKamal Heib 		}
54727c16f477SKamal Heib 	}
54737c16f477SKamal Heib 
54747c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
54757c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
54767c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
54777c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
54787c16f477SKamal Heib 		}
54797c16f477SKamal Heib 	}
5480e1f24a79SParav Pandit 
548158dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
548258dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
548358dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
548458dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
548558dcb60aSParav Pandit 		}
548658dcb60aSParav Pandit 	}
548758dcb60aSParav Pandit 
5488d7fab916SAvihai Horon 	if (MLX5_CAP_GEN(dev->mdev, roce_accl)) {
5489d7fab916SAvihai Horon 		for (i = 0; i < ARRAY_SIZE(roce_accl_cnts); i++, j++) {
5490d7fab916SAvihai Horon 			names[j] = roce_accl_cnts[i].name;
5491d7fab916SAvihai Horon 			offsets[j] = roce_accl_cnts[i].offset;
5492d7fab916SAvihai Horon 		}
5493d7fab916SAvihai Horon 	}
5494d7fab916SAvihai Horon 
5495e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5496e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5497e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
5498e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
5499e1f24a79SParav Pandit 		}
5500e1f24a79SParav Pandit 	}
55019f876f3dSTalat Batheesh 
55029f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
55039f876f3dSTalat Batheesh 		for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
55049f876f3dSTalat Batheesh 			names[j] = ext_ppcnt_cnts[i].name;
55059f876f3dSTalat Batheesh 			offsets[j] = ext_ppcnt_cnts[i].offset;
55069f876f3dSTalat Batheesh 		}
55079f876f3dSTalat Batheesh 	}
55080837e86aSMark Bloch }
55090837e86aSMark Bloch 
5510e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
55110837e86aSMark Bloch {
5512bfd745f8SLeon Romanovsky 	u32 out[MLX5_ST_SZ_DW(alloc_q_counter_out)] = {};
5513bfd745f8SLeon Romanovsky 	u32 in[MLX5_ST_SZ_DW(alloc_q_counter_in)] = {};
55143e1f000fSParav Pandit 	int num_cnt_ports;
5515aac4492eSDaniel Jurgens 	int err = 0;
55160837e86aSMark Bloch 	int i;
5517aa74be6eSYishai Hadas 	bool is_shared;
5518aa74be6eSYishai Hadas 
5519bfd745f8SLeon Romanovsky 	MLX5_SET(alloc_q_counter_in, in, opcode, MLX5_CMD_OP_ALLOC_Q_COUNTER);
5520aa74be6eSYishai Hadas 	is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
55213e1f000fSParav Pandit 	num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports;
55220837e86aSMark Bloch 
55233e1f000fSParav Pandit 	for (i = 0; i < num_cnt_ports; i++) {
5524aac4492eSDaniel Jurgens 		err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5525aac4492eSDaniel Jurgens 		if (err)
5526aac4492eSDaniel Jurgens 			goto err_alloc;
55277c16f477SKamal Heib 
5528aac4492eSDaniel Jurgens 		mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5529aac4492eSDaniel Jurgens 				      dev->port[i].cnts.offsets);
5530aac4492eSDaniel Jurgens 
5531bfd745f8SLeon Romanovsky 		MLX5_SET(alloc_q_counter_in, in, uid,
5532bfd745f8SLeon Romanovsky 			 is_shared ? MLX5_SHARED_RESOURCE_UID : 0);
5533bfd745f8SLeon Romanovsky 
5534bfd745f8SLeon Romanovsky 		err = mlx5_cmd_exec_inout(dev->mdev, alloc_q_counter, in, out);
5535aac4492eSDaniel Jurgens 		if (err) {
55360837e86aSMark Bloch 			mlx5_ib_warn(dev,
55370837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
5538aac4492eSDaniel Jurgens 				     i + 1, err);
5539aac4492eSDaniel Jurgens 			goto err_alloc;
55400837e86aSMark Bloch 		}
5541bfd745f8SLeon Romanovsky 
5542bfd745f8SLeon Romanovsky 		dev->port[i].cnts.set_id =
5543bfd745f8SLeon Romanovsky 			MLX5_GET(alloc_q_counter_out, out, counter_set_id);
55440837e86aSMark Bloch 	}
55450837e86aSMark Bloch 	return 0;
55460837e86aSMark Bloch 
5547aac4492eSDaniel Jurgens err_alloc:
5548aac4492eSDaniel Jurgens 	mlx5_ib_dealloc_counters(dev);
5549aac4492eSDaniel Jurgens 	return err;
55500837e86aSMark Bloch }
55510837e86aSMark Bloch 
55523e1f000fSParav Pandit static const struct mlx5_ib_counters *get_counters(struct mlx5_ib_dev *dev,
55533e1f000fSParav Pandit 						   u8 port_num)
55543e1f000fSParav Pandit {
55553e1f000fSParav Pandit 	return is_mdev_switchdev_mode(dev->mdev) ? &dev->port[0].cnts :
55563e1f000fSParav Pandit 						   &dev->port[port_num].cnts;
55573e1f000fSParav Pandit }
55583e1f000fSParav Pandit 
55593e1f000fSParav Pandit /**
55603e1f000fSParav Pandit  * mlx5_ib_get_counters_id - Returns counters id to use for device+port
55613e1f000fSParav Pandit  * @dev:	Pointer to mlx5 IB device
55623e1f000fSParav Pandit  * @port_num:	Zero based port number
55633e1f000fSParav Pandit  *
55643e1f000fSParav Pandit  * mlx5_ib_get_counters_id() Returns counters set id to use for given
55653e1f000fSParav Pandit  * device port combination in switchdev and non switchdev mode of the
55663e1f000fSParav Pandit  * parent device.
55673e1f000fSParav Pandit  */
55683e1f000fSParav Pandit u16 mlx5_ib_get_counters_id(struct mlx5_ib_dev *dev, u8 port_num)
55693e1f000fSParav Pandit {
55703e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts = get_counters(dev, port_num);
55713e1f000fSParav Pandit 
55723e1f000fSParav Pandit 	return cnts->set_id;
55733e1f000fSParav Pandit }
55743e1f000fSParav Pandit 
55750ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
55760ad17a8fSMark Bloch 						    u8 port_num)
55770ad17a8fSMark Bloch {
55787c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
55793e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts;
55803e1f000fSParav Pandit 	bool is_switchdev = is_mdev_switchdev_mode(dev->mdev);
55810ad17a8fSMark Bloch 
55823e1f000fSParav Pandit 	if ((is_switchdev && port_num) || (!is_switchdev && !port_num))
55830ad17a8fSMark Bloch 		return NULL;
55840ad17a8fSMark Bloch 
55853e1f000fSParav Pandit 	cnts = get_counters(dev, port_num - 1);
55863e1f000fSParav Pandit 
55875dcecbc9SParav Pandit 	return rdma_alloc_hw_stats_struct(cnts->names,
55885dcecbc9SParav Pandit 					  cnts->num_q_counters +
55895dcecbc9SParav Pandit 					  cnts->num_cong_counters +
55905dcecbc9SParav Pandit 					  cnts->num_ext_ppcnt_counters,
55910ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
55920ad17a8fSMark Bloch }
55930ad17a8fSMark Bloch 
5594aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
55955dcecbc9SParav Pandit 				    const struct mlx5_ib_counters *cnts,
5596318d535cSMark Zhang 				    struct rdma_hw_stats *stats,
5597318d535cSMark Zhang 				    u16 set_id)
5598e1f24a79SParav Pandit {
559966247fbbSLeon Romanovsky 	u32 out[MLX5_ST_SZ_DW(query_q_counter_out)] = {};
560066247fbbSLeon Romanovsky 	u32 in[MLX5_ST_SZ_DW(query_q_counter_in)] = {};
5601e1f24a79SParav Pandit 	__be32 val;
5602e1f24a79SParav Pandit 	int ret, i;
5603e1f24a79SParav Pandit 
560466247fbbSLeon Romanovsky 	MLX5_SET(query_q_counter_in, in, opcode, MLX5_CMD_OP_QUERY_Q_COUNTER);
560566247fbbSLeon Romanovsky 	MLX5_SET(query_q_counter_in, in, counter_set_id, set_id);
560666247fbbSLeon Romanovsky 	ret = mlx5_cmd_exec_inout(mdev, query_q_counter, in, out);
5607e1f24a79SParav Pandit 	if (ret)
560866247fbbSLeon Romanovsky 		return ret;
5609e1f24a79SParav Pandit 
56105dcecbc9SParav Pandit 	for (i = 0; i < cnts->num_q_counters; i++) {
561166247fbbSLeon Romanovsky 		val = *(__be32 *)((void *)out + cnts->offsets[i]);
5612e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
5613e1f24a79SParav Pandit 	}
5614e1f24a79SParav Pandit 
561566247fbbSLeon Romanovsky 	return 0;
5616e1f24a79SParav Pandit }
5617e1f24a79SParav Pandit 
56189f876f3dSTalat Batheesh static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
56195dcecbc9SParav Pandit 					    const struct mlx5_ib_counters *cnts,
56209f876f3dSTalat Batheesh 					    struct rdma_hw_stats *stats)
56219f876f3dSTalat Batheesh {
56225dcecbc9SParav Pandit 	int offset = cnts->num_q_counters + cnts->num_cong_counters;
56239f876f3dSTalat Batheesh 	int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
56249f876f3dSTalat Batheesh 	int ret, i;
56259f876f3dSTalat Batheesh 	void *out;
56269f876f3dSTalat Batheesh 
56279f876f3dSTalat Batheesh 	out = kvzalloc(sz, GFP_KERNEL);
56289f876f3dSTalat Batheesh 	if (!out)
56299f876f3dSTalat Batheesh 		return -ENOMEM;
56309f876f3dSTalat Batheesh 
56319f876f3dSTalat Batheesh 	ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
56329f876f3dSTalat Batheesh 	if (ret)
56339f876f3dSTalat Batheesh 		goto free;
56349f876f3dSTalat Batheesh 
56355dcecbc9SParav Pandit 	for (i = 0; i < cnts->num_ext_ppcnt_counters; i++)
56369f876f3dSTalat Batheesh 		stats->value[i + offset] =
56379f876f3dSTalat Batheesh 			be64_to_cpup((__be64 *)(out +
56385dcecbc9SParav Pandit 				    cnts->offsets[i + offset]));
56399f876f3dSTalat Batheesh free:
56409f876f3dSTalat Batheesh 	kvfree(out);
56419f876f3dSTalat Batheesh 	return ret;
56429f876f3dSTalat Batheesh }
56439f876f3dSTalat Batheesh 
56440ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
56450ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
56467c16f477SKamal Heib 				u8 port_num, int index)
56470ad17a8fSMark Bloch {
56480ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
56493e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts = get_counters(dev, port_num - 1);
5650aac4492eSDaniel Jurgens 	struct mlx5_core_dev *mdev;
5651e1f24a79SParav Pandit 	int ret, num_counters;
5652aac4492eSDaniel Jurgens 	u8 mdev_port_num;
56530ad17a8fSMark Bloch 
56547c16f477SKamal Heib 	if (!stats)
5655e1f24a79SParav Pandit 		return -EINVAL;
56560ad17a8fSMark Bloch 
56575dcecbc9SParav Pandit 	num_counters = cnts->num_q_counters +
56585dcecbc9SParav Pandit 		       cnts->num_cong_counters +
56595dcecbc9SParav Pandit 		       cnts->num_ext_ppcnt_counters;
5660aac4492eSDaniel Jurgens 
5661aac4492eSDaniel Jurgens 	/* q_counters are per IB device, query the master mdev */
56625dcecbc9SParav Pandit 	ret = mlx5_ib_query_q_counters(dev->mdev, cnts, stats, cnts->set_id);
56630ad17a8fSMark Bloch 	if (ret)
5664e1f24a79SParav Pandit 		return ret;
56650ad17a8fSMark Bloch 
56669f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
56675dcecbc9SParav Pandit 		ret =  mlx5_ib_query_ext_ppcnt_counters(dev, cnts, stats);
56689f876f3dSTalat Batheesh 		if (ret)
56699f876f3dSTalat Batheesh 			return ret;
56709f876f3dSTalat Batheesh 	}
56719f876f3dSTalat Batheesh 
5672e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5673aac4492eSDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5674aac4492eSDaniel Jurgens 						    &mdev_port_num);
5675aac4492eSDaniel Jurgens 		if (!mdev) {
5676aac4492eSDaniel Jurgens 			/* If port is not affiliated yet, its in down state
5677aac4492eSDaniel Jurgens 			 * which doesn't have any counters yet, so it would be
5678aac4492eSDaniel Jurgens 			 * zero. So no need to read from the HCA.
5679aac4492eSDaniel Jurgens 			 */
5680aac4492eSDaniel Jurgens 			goto done;
5681aac4492eSDaniel Jurgens 		}
568271a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
568371a0ff65SMajd Dibbiny 						   stats->value +
56845dcecbc9SParav Pandit 						   cnts->num_q_counters,
56855dcecbc9SParav Pandit 						   cnts->num_cong_counters,
56865dcecbc9SParav Pandit 						   cnts->offsets +
56875dcecbc9SParav Pandit 						   cnts->num_q_counters);
5688aac4492eSDaniel Jurgens 
5689aac4492eSDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
5690e1f24a79SParav Pandit 		if (ret)
5691e1f24a79SParav Pandit 			return ret;
56920ad17a8fSMark Bloch 	}
56937c16f477SKamal Heib 
5694aac4492eSDaniel Jurgens done:
5695e1f24a79SParav Pandit 	return num_counters;
56960ad17a8fSMark Bloch }
56970ad17a8fSMark Bloch 
569818d422ceSMark Zhang static struct rdma_hw_stats *
569918d422ceSMark Zhang mlx5_ib_counter_alloc_stats(struct rdma_counter *counter)
570018d422ceSMark Zhang {
570118d422ceSMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
57025dcecbc9SParav Pandit 	const struct mlx5_ib_counters *cnts =
57033e1f000fSParav Pandit 		get_counters(dev, counter->port - 1);
570418d422ceSMark Zhang 
57055dcecbc9SParav Pandit 	return rdma_alloc_hw_stats_struct(cnts->names,
5706ec16b6bbSMark Zhang 					  cnts->num_q_counters +
5707ec16b6bbSMark Zhang 					  cnts->num_cong_counters +
5708ec16b6bbSMark Zhang 					  cnts->num_ext_ppcnt_counters,
570918d422ceSMark Zhang 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
571018d422ceSMark Zhang }
571118d422ceSMark Zhang 
571218d422ceSMark Zhang static int mlx5_ib_counter_update_stats(struct rdma_counter *counter)
571318d422ceSMark Zhang {
571418d422ceSMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
57153e1f000fSParav Pandit 	const struct mlx5_ib_counters *cnts =
57163e1f000fSParav Pandit 		get_counters(dev, counter->port - 1);
571718d422ceSMark Zhang 
57185dcecbc9SParav Pandit 	return mlx5_ib_query_q_counters(dev->mdev, cnts,
571918d422ceSMark Zhang 					counter->stats, counter->id);
572018d422ceSMark Zhang }
572118d422ceSMark Zhang 
572266247fbbSLeon Romanovsky static int mlx5_ib_counter_dealloc(struct rdma_counter *counter)
572366247fbbSLeon Romanovsky {
572466247fbbSLeon Romanovsky 	struct mlx5_ib_dev *dev = to_mdev(counter->device);
572566247fbbSLeon Romanovsky 	u32 in[MLX5_ST_SZ_DW(dealloc_q_counter_in)] = {};
572666247fbbSLeon Romanovsky 
572766247fbbSLeon Romanovsky 	if (!counter->id)
572866247fbbSLeon Romanovsky 		return 0;
572966247fbbSLeon Romanovsky 
573066247fbbSLeon Romanovsky 	MLX5_SET(dealloc_q_counter_in, in, opcode,
573166247fbbSLeon Romanovsky 		 MLX5_CMD_OP_DEALLOC_Q_COUNTER);
573266247fbbSLeon Romanovsky 	MLX5_SET(dealloc_q_counter_in, in, counter_set_id, counter->id);
573366247fbbSLeon Romanovsky 	return mlx5_cmd_exec_in(dev->mdev, dealloc_q_counter, in);
573466247fbbSLeon Romanovsky }
573566247fbbSLeon Romanovsky 
573645842fc6SMark Zhang static int mlx5_ib_counter_bind_qp(struct rdma_counter *counter,
573745842fc6SMark Zhang 				   struct ib_qp *qp)
573845842fc6SMark Zhang {
573945842fc6SMark Zhang 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
574045842fc6SMark Zhang 	int err;
574145842fc6SMark Zhang 
574245842fc6SMark Zhang 	if (!counter->id) {
5743bfd745f8SLeon Romanovsky 		u32 out[MLX5_ST_SZ_DW(alloc_q_counter_out)] = {};
5744bfd745f8SLeon Romanovsky 		u32 in[MLX5_ST_SZ_DW(alloc_q_counter_in)] = {};
5745bfd745f8SLeon Romanovsky 
5746bfd745f8SLeon Romanovsky 		MLX5_SET(alloc_q_counter_in, in, opcode,
5747bfd745f8SLeon Romanovsky 			 MLX5_CMD_OP_ALLOC_Q_COUNTER);
5748bfd745f8SLeon Romanovsky 		MLX5_SET(alloc_q_counter_in, in, uid, MLX5_SHARED_RESOURCE_UID);
5749bfd745f8SLeon Romanovsky 		err = mlx5_cmd_exec_inout(dev->mdev, alloc_q_counter, in, out);
575045842fc6SMark Zhang 		if (err)
575145842fc6SMark Zhang 			return err;
5752bfd745f8SLeon Romanovsky 		counter->id =
5753bfd745f8SLeon Romanovsky 			MLX5_GET(alloc_q_counter_out, out, counter_set_id);
575445842fc6SMark Zhang 	}
575545842fc6SMark Zhang 
575645842fc6SMark Zhang 	err = mlx5_ib_qp_set_counter(qp, counter);
575745842fc6SMark Zhang 	if (err)
575845842fc6SMark Zhang 		goto fail_set_counter;
575945842fc6SMark Zhang 
576045842fc6SMark Zhang 	return 0;
576145842fc6SMark Zhang 
576245842fc6SMark Zhang fail_set_counter:
576366247fbbSLeon Romanovsky 	mlx5_ib_counter_dealloc(counter);
576445842fc6SMark Zhang 	counter->id = 0;
576545842fc6SMark Zhang 
576645842fc6SMark Zhang 	return err;
576745842fc6SMark Zhang }
576845842fc6SMark Zhang 
576945842fc6SMark Zhang static int mlx5_ib_counter_unbind_qp(struct ib_qp *qp)
577045842fc6SMark Zhang {
577145842fc6SMark Zhang 	return mlx5_ib_qp_set_counter(qp, NULL);
577245842fc6SMark Zhang }
577345842fc6SMark Zhang 
5774f6a8a19bSDenis Drozdov static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5775693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
5776f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
5777693dfd5aSErez Shitrit {
5778693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
5779f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
5780693dfd5aSErez Shitrit 
5781f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
5782693dfd5aSErez Shitrit }
5783693dfd5aSErez Shitrit 
5784fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5785fe248c3aSMaor Gottlieb {
578609b0965eSGreg Kroah-Hartman 	if (!dev->delay_drop.dir_debugfs)
5787fe248c3aSMaor Gottlieb 		return;
578809b0965eSGreg Kroah-Hartman 	debugfs_remove_recursive(dev->delay_drop.dir_debugfs);
578909b0965eSGreg Kroah-Hartman 	dev->delay_drop.dir_debugfs = NULL;
5790fe248c3aSMaor Gottlieb }
5791fe248c3aSMaor Gottlieb 
579203404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
579303404e8aSMaor Gottlieb {
579403404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
579503404e8aSMaor Gottlieb 		return;
579603404e8aSMaor Gottlieb 
579703404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
5798fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5799fe248c3aSMaor Gottlieb }
5800fe248c3aSMaor Gottlieb 
5801fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5802fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
5803fe248c3aSMaor Gottlieb {
5804fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5805fe248c3aSMaor Gottlieb 	char lbuf[20];
5806fe248c3aSMaor Gottlieb 	int len;
5807fe248c3aSMaor Gottlieb 
5808fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5809fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
5810fe248c3aSMaor Gottlieb }
5811fe248c3aSMaor Gottlieb 
5812fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5813fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
5814fe248c3aSMaor Gottlieb {
5815fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5816fe248c3aSMaor Gottlieb 	u32 timeout;
5817fe248c3aSMaor Gottlieb 	u32 var;
5818fe248c3aSMaor Gottlieb 
5819fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
5820fe248c3aSMaor Gottlieb 		return -EFAULT;
5821fe248c3aSMaor Gottlieb 
5822fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5823fe248c3aSMaor Gottlieb 			1000);
5824fe248c3aSMaor Gottlieb 	if (timeout != var)
5825fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5826fe248c3aSMaor Gottlieb 			    timeout);
5827fe248c3aSMaor Gottlieb 
5828fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
5829fe248c3aSMaor Gottlieb 
5830fe248c3aSMaor Gottlieb 	return count;
5831fe248c3aSMaor Gottlieb }
5832fe248c3aSMaor Gottlieb 
5833fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
5834fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
5835fe248c3aSMaor Gottlieb 	.open	= simple_open,
5836fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
5837fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
5838fe248c3aSMaor Gottlieb };
5839fe248c3aSMaor Gottlieb 
584009b0965eSGreg Kroah-Hartman static void delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5841fe248c3aSMaor Gottlieb {
584209b0965eSGreg Kroah-Hartman 	struct dentry *root;
5843fe248c3aSMaor Gottlieb 
5844fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
584509b0965eSGreg Kroah-Hartman 		return;
5846fe248c3aSMaor Gottlieb 
584709b0965eSGreg Kroah-Hartman 	root = debugfs_create_dir("delay_drop", dev->mdev->priv.dbg_root);
584809b0965eSGreg Kroah-Hartman 	dev->delay_drop.dir_debugfs = root;
5849fe248c3aSMaor Gottlieb 
585009b0965eSGreg Kroah-Hartman 	debugfs_create_atomic_t("num_timeout_events", 0400, root,
5851fe248c3aSMaor Gottlieb 				&dev->delay_drop.events_cnt);
585209b0965eSGreg Kroah-Hartman 	debugfs_create_atomic_t("num_rqs", 0400, root,
5853fe248c3aSMaor Gottlieb 				&dev->delay_drop.rqs_cnt);
585409b0965eSGreg Kroah-Hartman 	debugfs_create_file("timeout", 0600, root, &dev->delay_drop,
5855fe248c3aSMaor Gottlieb 			    &fops_delay_drop_timeout);
585603404e8aSMaor Gottlieb }
585703404e8aSMaor Gottlieb 
585803404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
585903404e8aSMaor Gottlieb {
586003404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
586103404e8aSMaor Gottlieb 		return;
586203404e8aSMaor Gottlieb 
586303404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
586403404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
586503404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
586603404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
586703404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
5868fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
5869fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
5870fe248c3aSMaor Gottlieb 
587109b0965eSGreg Kroah-Hartman 	delay_drop_debugfs_init(dev);
587203404e8aSMaor Gottlieb }
587303404e8aSMaor Gottlieb 
587432f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
587532f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
587632f69e4bSDaniel Jurgens {
587732f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
587832f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
587932f69e4bSDaniel Jurgens 	int comps;
588032f69e4bSDaniel Jurgens 	int err;
588132f69e4bSDaniel Jurgens 	int i;
588232f69e4bSDaniel Jurgens 
58839dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
58849dc4cfffSLeon Romanovsky 
5885a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5886a9e546e7SParav Pandit 
588732f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
588832f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
588932f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
589032f69e4bSDaniel Jurgens 		return;
589132f69e4bSDaniel Jurgens 	}
5892df097a27SSaeed Mahameed 
589332f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
589432f69e4bSDaniel Jurgens 
589532f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
589623eaf3b5SLeon Romanovsky 	if (mpi->mdev_events.notifier_call)
589723eaf3b5SLeon Romanovsky 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
589823eaf3b5SLeon Romanovsky 	mpi->mdev_events.notifier_call = NULL;
589932f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
590032f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
590132f69e4bSDaniel Jurgens 
590232f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
590332f69e4bSDaniel Jurgens 	if (comps) {
590432f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
590532f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
590632f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
590732f69e4bSDaniel Jurgens 
590832f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
590932f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
591032f69e4bSDaniel Jurgens 
591132f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
591232f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
591332f69e4bSDaniel Jurgens 	}
591432f69e4bSDaniel Jurgens 
591532f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
591632f69e4bSDaniel Jurgens 
591732f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
591832f69e4bSDaniel Jurgens 
591932f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
592032f69e4bSDaniel Jurgens 
592132f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
592232f69e4bSDaniel Jurgens 
592332f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
592432f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
592532f69e4bSDaniel Jurgens 	 * it back to the list.
592632f69e4bSDaniel Jurgens 	 */
592732f69e4bSDaniel Jurgens 	if (err)
592832f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
592932f69e4bSDaniel Jurgens 			    port_num + 1);
593032f69e4bSDaniel Jurgens 
593195579e78SMark Bloch 	ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
593232f69e4bSDaniel Jurgens }
593332f69e4bSDaniel Jurgens 
593432f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
593532f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
593632f69e4bSDaniel Jurgens {
593732f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
593832f69e4bSDaniel Jurgens 	int err;
593932f69e4bSDaniel Jurgens 
59409dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
59419dc4cfffSLeon Romanovsky 
594232f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
594332f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
59442577188eSQing Huang 		mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
594532f69e4bSDaniel Jurgens 			    port_num + 1);
594632f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
594732f69e4bSDaniel Jurgens 		return false;
594832f69e4bSDaniel Jurgens 	}
594932f69e4bSDaniel Jurgens 
595032f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
595132f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
5952df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
595332f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
595432f69e4bSDaniel Jurgens 
595532f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
595632f69e4bSDaniel Jurgens 	if (err)
595732f69e4bSDaniel Jurgens 		goto unbind;
595832f69e4bSDaniel Jurgens 
595932f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
596032f69e4bSDaniel Jurgens 	if (err)
596132f69e4bSDaniel Jurgens 		goto unbind;
596232f69e4bSDaniel Jurgens 
596332f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
596432f69e4bSDaniel Jurgens 	if (err) {
596532f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
596632f69e4bSDaniel Jurgens 			    port_num + 1);
596732f69e4bSDaniel Jurgens 		goto unbind;
596832f69e4bSDaniel Jurgens 	}
596932f69e4bSDaniel Jurgens 
5970df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5971df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5972df097a27SSaeed Mahameed 
597373eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
5974a9e546e7SParav Pandit 
597532f69e4bSDaniel Jurgens 	return true;
597632f69e4bSDaniel Jurgens 
597732f69e4bSDaniel Jurgens unbind:
597832f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
597932f69e4bSDaniel Jurgens 	return false;
598032f69e4bSDaniel Jurgens }
598132f69e4bSDaniel Jurgens 
598232f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
598332f69e4bSDaniel Jurgens {
598432f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
598532f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
598632f69e4bSDaniel Jurgens 							  port_num + 1);
598732f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
598832f69e4bSDaniel Jurgens 	int err;
598932f69e4bSDaniel Jurgens 	int i;
599032f69e4bSDaniel Jurgens 
599132f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
599232f69e4bSDaniel Jurgens 		return 0;
599332f69e4bSDaniel Jurgens 
599432f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
599532f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
599632f69e4bSDaniel Jurgens 	if (err)
599732f69e4bSDaniel Jurgens 		return err;
599832f69e4bSDaniel Jurgens 
599932f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
600032f69e4bSDaniel Jurgens 	if (err)
600132f69e4bSDaniel Jurgens 		return err;
600232f69e4bSDaniel Jurgens 
600332f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
600432f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
600532f69e4bSDaniel Jurgens 		bool bound = false;
600632f69e4bSDaniel Jurgens 
600732f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
600832f69e4bSDaniel Jurgens 		if (i == port_num) {
600932f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
601032f69e4bSDaniel Jurgens 			if (!mpi) {
601132f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
601232f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
601332f69e4bSDaniel Jurgens 				return -ENOMEM;
601432f69e4bSDaniel Jurgens 			}
601532f69e4bSDaniel Jurgens 
601632f69e4bSDaniel Jurgens 			mpi->is_master = true;
601732f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
601832f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
601932f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
602032f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
602132f69e4bSDaniel Jurgens 			mpi = NULL;
602232f69e4bSDaniel Jurgens 			continue;
602332f69e4bSDaniel Jurgens 		}
602432f69e4bSDaniel Jurgens 
602532f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
602632f69e4bSDaniel Jurgens 				    list) {
602732f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
602832f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
602932f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
603032f69e4bSDaniel Jurgens 			}
603132f69e4bSDaniel Jurgens 
603232f69e4bSDaniel Jurgens 			if (bound) {
6033c42260f1SVu Pham 				dev_dbg(mpi->mdev->device,
6034c42260f1SVu Pham 					"removing port from unaffiliated list.\n");
603532f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
603632f69e4bSDaniel Jurgens 				list_del(&mpi->list);
603732f69e4bSDaniel Jurgens 				break;
603832f69e4bSDaniel Jurgens 			}
603932f69e4bSDaniel Jurgens 		}
604032f69e4bSDaniel Jurgens 		if (!bound) {
604132f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
604232f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
604332f69e4bSDaniel Jurgens 				    i + 1);
604432f69e4bSDaniel Jurgens 		}
604532f69e4bSDaniel Jurgens 	}
604632f69e4bSDaniel Jurgens 
604732f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
604832f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
604932f69e4bSDaniel Jurgens 	return err;
605032f69e4bSDaniel Jurgens }
605132f69e4bSDaniel Jurgens 
605232f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
605332f69e4bSDaniel Jurgens {
605432f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
605532f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
605632f69e4bSDaniel Jurgens 							  port_num + 1);
605732f69e4bSDaniel Jurgens 	int i;
605832f69e4bSDaniel Jurgens 
605932f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
606032f69e4bSDaniel Jurgens 		return;
606132f69e4bSDaniel Jurgens 
606232f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
606332f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
606432f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
606532f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
606632f69e4bSDaniel Jurgens 			if (i == port_num) {
606732f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
606832f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
606932f69e4bSDaniel Jurgens 			} else {
607032f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
607132f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
607232f69e4bSDaniel Jurgens 			}
607332f69e4bSDaniel Jurgens 		}
607432f69e4bSDaniel Jurgens 	}
607532f69e4bSDaniel Jurgens 
607632f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
607732f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
607832f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
607932f69e4bSDaniel Jurgens 
608032f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
608132f69e4bSDaniel Jurgens }
608232f69e4bSDaniel Jurgens 
6083342ee59dSYishai Hadas static int mmap_obj_cleanup(struct ib_uobject *uobject,
60847be76befSYishai Hadas 			    enum rdma_remove_reason why,
60857be76befSYishai Hadas 			    struct uverbs_attr_bundle *attrs)
60867be76befSYishai Hadas {
60877be76befSYishai Hadas 	struct mlx5_user_mmap_entry *obj = uobject->object;
60887be76befSYishai Hadas 
60897be76befSYishai Hadas 	rdma_user_mmap_entry_remove(&obj->rdma_entry);
60907be76befSYishai Hadas 	return 0;
60917be76befSYishai Hadas }
60927be76befSYishai Hadas 
6093342ee59dSYishai Hadas static int mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext *c,
6094342ee59dSYishai Hadas 					    struct mlx5_user_mmap_entry *entry,
6095342ee59dSYishai Hadas 					    size_t length)
6096342ee59dSYishai Hadas {
6097342ee59dSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
6098342ee59dSYishai Hadas 		&c->ibucontext, &entry->rdma_entry, length,
6099342ee59dSYishai Hadas 		(MLX5_IB_MMAP_OFFSET_START << 16),
6100342ee59dSYishai Hadas 		((MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1));
6101342ee59dSYishai Hadas }
6102342ee59dSYishai Hadas 
61037be76befSYishai Hadas static struct mlx5_user_mmap_entry *
61047be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c)
61057be76befSYishai Hadas {
61067be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
61077be76befSYishai Hadas 	struct mlx5_var_table *var_table;
61087be76befSYishai Hadas 	u32 page_idx;
61097be76befSYishai Hadas 	int err;
61107be76befSYishai Hadas 
61117be76befSYishai Hadas 	var_table = &to_mdev(c->ibucontext.device)->var_table;
61127be76befSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
61137be76befSYishai Hadas 	if (!entry)
61147be76befSYishai Hadas 		return ERR_PTR(-ENOMEM);
61157be76befSYishai Hadas 
61167be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
61177be76befSYishai Hadas 	page_idx = find_first_zero_bit(var_table->bitmap,
61187be76befSYishai Hadas 				       var_table->num_var_hw_entries);
61197be76befSYishai Hadas 	if (page_idx >= var_table->num_var_hw_entries) {
61207be76befSYishai Hadas 		err = -ENOSPC;
61217be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
61227be76befSYishai Hadas 		goto end;
61237be76befSYishai Hadas 	}
61247be76befSYishai Hadas 
61257be76befSYishai Hadas 	set_bit(page_idx, var_table->bitmap);
61267be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
61277be76befSYishai Hadas 
61287be76befSYishai Hadas 	entry->address = var_table->hw_start_addr +
61297be76befSYishai Hadas 				(page_idx * var_table->stride_size);
61307be76befSYishai Hadas 	entry->page_idx = page_idx;
61317be76befSYishai Hadas 	entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR;
61327be76befSYishai Hadas 
6133342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry,
6134342ee59dSYishai Hadas 					       var_table->stride_size);
61357be76befSYishai Hadas 	if (err)
61367be76befSYishai Hadas 		goto err_insert;
61377be76befSYishai Hadas 
61387be76befSYishai Hadas 	return entry;
61397be76befSYishai Hadas 
61407be76befSYishai Hadas err_insert:
61417be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
61427be76befSYishai Hadas 	clear_bit(page_idx, var_table->bitmap);
61437be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
61447be76befSYishai Hadas end:
61457be76befSYishai Hadas 	kfree(entry);
61467be76befSYishai Hadas 	return ERR_PTR(err);
61477be76befSYishai Hadas }
61487be76befSYishai Hadas 
61497be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)(
61507be76befSYishai Hadas 	struct uverbs_attr_bundle *attrs)
61517be76befSYishai Hadas {
61527be76befSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
61537be76befSYishai Hadas 		attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
61547be76befSYishai Hadas 	struct mlx5_ib_ucontext *c;
61557be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
61567be76befSYishai Hadas 	u64 mmap_offset;
61577be76befSYishai Hadas 	u32 length;
61587be76befSYishai Hadas 	int err;
61597be76befSYishai Hadas 
61607be76befSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
61617be76befSYishai Hadas 	if (IS_ERR(c))
61627be76befSYishai Hadas 		return PTR_ERR(c);
61637be76befSYishai Hadas 
61647be76befSYishai Hadas 	entry = alloc_var_entry(c);
61657be76befSYishai Hadas 	if (IS_ERR(entry))
61667be76befSYishai Hadas 		return PTR_ERR(entry);
61677be76befSYishai Hadas 
61687be76befSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
61697be76befSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
61707be76befSYishai Hadas 	uobj->object = entry;
61710ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
61727be76befSYishai Hadas 
61737be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
61747be76befSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
61757be76befSYishai Hadas 	if (err)
61760ac8903cSJason Gunthorpe 		return err;
61777be76befSYishai Hadas 
61787be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
61797be76befSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
61807be76befSYishai Hadas 	if (err)
61810ac8903cSJason Gunthorpe 		return err;
61827be76befSYishai Hadas 
61837be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
61847be76befSYishai Hadas 			     &length, sizeof(length));
61857be76befSYishai Hadas 	return err;
61867be76befSYishai Hadas }
61877be76befSYishai Hadas 
61887be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
61897be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_ALLOC,
61907be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE,
61917be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
61927be76befSYishai Hadas 			UVERBS_ACCESS_NEW,
61937be76befSYishai Hadas 			UA_MANDATORY),
61947be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
61957be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
61967be76befSYishai Hadas 			   UA_MANDATORY),
61977be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
61987be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
61997be76befSYishai Hadas 			   UA_MANDATORY),
62007be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
62017be76befSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
62027be76befSYishai Hadas 			    UA_MANDATORY));
62037be76befSYishai Hadas 
62047be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
62057be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_DESTROY,
62067be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE,
62077be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
62087be76befSYishai Hadas 			UVERBS_ACCESS_DESTROY,
62097be76befSYishai Hadas 			UA_MANDATORY));
62107be76befSYishai Hadas 
62117be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR,
6212342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
62137be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC),
62147be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY));
62157be76befSYishai Hadas 
62167be76befSYishai Hadas static bool var_is_supported(struct ib_device *device)
62177be76befSYishai Hadas {
62187be76befSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(device);
62197be76befSYishai Hadas 
62207be76befSYishai Hadas 	return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
62217be76befSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q);
62227be76befSYishai Hadas }
62237be76befSYishai Hadas 
6224342ee59dSYishai Hadas static struct mlx5_user_mmap_entry *
6225342ee59dSYishai Hadas alloc_uar_entry(struct mlx5_ib_ucontext *c,
6226342ee59dSYishai Hadas 		enum mlx5_ib_uapi_uar_alloc_type alloc_type)
6227342ee59dSYishai Hadas {
6228342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
6229342ee59dSYishai Hadas 	struct mlx5_ib_dev *dev;
6230342ee59dSYishai Hadas 	u32 uar_index;
6231342ee59dSYishai Hadas 	int err;
6232342ee59dSYishai Hadas 
6233342ee59dSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
6234342ee59dSYishai Hadas 	if (!entry)
6235342ee59dSYishai Hadas 		return ERR_PTR(-ENOMEM);
6236342ee59dSYishai Hadas 
6237342ee59dSYishai Hadas 	dev = to_mdev(c->ibucontext.device);
6238342ee59dSYishai Hadas 	err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
6239342ee59dSYishai Hadas 	if (err)
6240342ee59dSYishai Hadas 		goto end;
6241342ee59dSYishai Hadas 
6242342ee59dSYishai Hadas 	entry->page_idx = uar_index;
6243342ee59dSYishai Hadas 	entry->address = uar_index2paddress(dev, uar_index);
6244342ee59dSYishai Hadas 	if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
6245342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC;
6246342ee59dSYishai Hadas 	else
6247342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_NC;
6248342ee59dSYishai Hadas 
6249342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry, PAGE_SIZE);
6250342ee59dSYishai Hadas 	if (err)
6251342ee59dSYishai Hadas 		goto err_insert;
6252342ee59dSYishai Hadas 
6253342ee59dSYishai Hadas 	return entry;
6254342ee59dSYishai Hadas 
6255342ee59dSYishai Hadas err_insert:
6256342ee59dSYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, uar_index);
6257342ee59dSYishai Hadas end:
6258342ee59dSYishai Hadas 	kfree(entry);
6259342ee59dSYishai Hadas 	return ERR_PTR(err);
6260342ee59dSYishai Hadas }
6261342ee59dSYishai Hadas 
6262342ee59dSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)(
6263342ee59dSYishai Hadas 	struct uverbs_attr_bundle *attrs)
6264342ee59dSYishai Hadas {
6265342ee59dSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
6266342ee59dSYishai Hadas 		attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
6267342ee59dSYishai Hadas 	enum mlx5_ib_uapi_uar_alloc_type alloc_type;
6268342ee59dSYishai Hadas 	struct mlx5_ib_ucontext *c;
6269342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
6270342ee59dSYishai Hadas 	u64 mmap_offset;
6271342ee59dSYishai Hadas 	u32 length;
6272342ee59dSYishai Hadas 	int err;
6273342ee59dSYishai Hadas 
6274342ee59dSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
6275342ee59dSYishai Hadas 	if (IS_ERR(c))
6276342ee59dSYishai Hadas 		return PTR_ERR(c);
6277342ee59dSYishai Hadas 
6278342ee59dSYishai Hadas 	err = uverbs_get_const(&alloc_type, attrs,
6279342ee59dSYishai Hadas 			       MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE);
6280342ee59dSYishai Hadas 	if (err)
6281342ee59dSYishai Hadas 		return err;
6282342ee59dSYishai Hadas 
6283342ee59dSYishai Hadas 	if (alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF &&
6284342ee59dSYishai Hadas 	    alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_NC)
6285342ee59dSYishai Hadas 		return -EOPNOTSUPP;
6286342ee59dSYishai Hadas 
6287342ee59dSYishai Hadas 	if (!to_mdev(c->ibucontext.device)->wc_support &&
6288342ee59dSYishai Hadas 	    alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
6289342ee59dSYishai Hadas 		return -EOPNOTSUPP;
6290342ee59dSYishai Hadas 
6291342ee59dSYishai Hadas 	entry = alloc_uar_entry(c, alloc_type);
6292342ee59dSYishai Hadas 	if (IS_ERR(entry))
6293342ee59dSYishai Hadas 		return PTR_ERR(entry);
6294342ee59dSYishai Hadas 
6295342ee59dSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
6296342ee59dSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
6297342ee59dSYishai Hadas 	uobj->object = entry;
62980ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
6299342ee59dSYishai Hadas 
6300342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
6301342ee59dSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
6302342ee59dSYishai Hadas 	if (err)
63030ac8903cSJason Gunthorpe 		return err;
6304342ee59dSYishai Hadas 
6305342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
6306342ee59dSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
6307342ee59dSYishai Hadas 	if (err)
63080ac8903cSJason Gunthorpe 		return err;
6309342ee59dSYishai Hadas 
6310342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
6311342ee59dSYishai Hadas 			     &length, sizeof(length));
6312342ee59dSYishai Hadas 	return err;
6313342ee59dSYishai Hadas }
6314342ee59dSYishai Hadas 
6315342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
6316342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_ALLOC,
6317342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE,
6318342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
6319342ee59dSYishai Hadas 			UVERBS_ACCESS_NEW,
6320342ee59dSYishai Hadas 			UA_MANDATORY),
6321342ee59dSYishai Hadas 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE,
6322342ee59dSYishai Hadas 			     enum mlx5_ib_uapi_uar_alloc_type,
6323342ee59dSYishai Hadas 			     UA_MANDATORY),
6324342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
6325342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
6326342ee59dSYishai Hadas 			   UA_MANDATORY),
6327342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
6328342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
6329342ee59dSYishai Hadas 			   UA_MANDATORY),
6330342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
6331342ee59dSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
6332342ee59dSYishai Hadas 			    UA_MANDATORY));
6333342ee59dSYishai Hadas 
6334342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
6335342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_DESTROY,
6336342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_DESTROY_HANDLE,
6337342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
6338342ee59dSYishai Hadas 			UVERBS_ACCESS_DESTROY,
6339342ee59dSYishai Hadas 			UA_MANDATORY));
6340342ee59dSYishai Hadas 
6341342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_UAR,
6342342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
6343342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_ALLOC),
6344342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_DESTROY));
6345342ee59dSYishai Hadas 
63469a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
63479a119cd5SJason Gunthorpe 	mlx5_ib_dm,
63489a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
634924da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
63509a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
635124da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
635283bb4442SJason Gunthorpe 			    UA_MANDATORY),
63539a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
635424da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
63553b113a1eSAriel Levkovich 			    UA_OPTIONAL),
63563b113a1eSAriel Levkovich 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
63573b113a1eSAriel Levkovich 			     enum mlx5_ib_uapi_dm_type,
63583b113a1eSAriel Levkovich 			     UA_OPTIONAL));
635924da0016SAriel Levkovich 
63609a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
63619a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
63629a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
6363c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
6364bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
6365bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
6366c6475a0bSAviad Yehezkel 
63670cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
636836e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
63690cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
637030f2fe40SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_qos_defs),
63718c84660bSMatan Barak 
63720cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
63730cbf432dSJason Gunthorpe 				&mlx5_ib_flow_action),
63740cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
63757be76befSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR,
63767be76befSYishai Hadas 				UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)),
6377342ee59dSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_UAR),
63780cbf432dSJason Gunthorpe 	{}
63790cbf432dSJason Gunthorpe };
63808c84660bSMatan Barak 
63811a1e03dcSRaed Salem static int mlx5_ib_read_counters(struct ib_counters *counters,
63821a1e03dcSRaed Salem 				 struct ib_counters_read_attr *read_attr,
63831a1e03dcSRaed Salem 				 struct uverbs_attr_bundle *attrs)
63841a1e03dcSRaed Salem {
63851a1e03dcSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
63861a1e03dcSRaed Salem 	struct mlx5_read_counters_attr mread_attr = {};
63871a1e03dcSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc;
63881a1e03dcSRaed Salem 	int ret, i;
63891a1e03dcSRaed Salem 
63901a1e03dcSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
63911a1e03dcSRaed Salem 	if (mcounters->cntrs_max_index > read_attr->ncounters) {
63921a1e03dcSRaed Salem 		ret = -EINVAL;
63931a1e03dcSRaed Salem 		goto err_bound;
63941a1e03dcSRaed Salem 	}
63951a1e03dcSRaed Salem 
63961a1e03dcSRaed Salem 	mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
63971a1e03dcSRaed Salem 				 GFP_KERNEL);
63981a1e03dcSRaed Salem 	if (!mread_attr.out) {
63991a1e03dcSRaed Salem 		ret = -ENOMEM;
64001a1e03dcSRaed Salem 		goto err_bound;
64011a1e03dcSRaed Salem 	}
64021a1e03dcSRaed Salem 
64031a1e03dcSRaed Salem 	mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
64041a1e03dcSRaed Salem 	mread_attr.flags = read_attr->flags;
64051a1e03dcSRaed Salem 	ret = mcounters->read_counters(counters->device, &mread_attr);
64061a1e03dcSRaed Salem 	if (ret)
64071a1e03dcSRaed Salem 		goto err_read;
64081a1e03dcSRaed Salem 
64091a1e03dcSRaed Salem 	/* do the pass over the counters data array to assign according to the
64101a1e03dcSRaed Salem 	 * descriptions and indexing pairs
64111a1e03dcSRaed Salem 	 */
64121a1e03dcSRaed Salem 	desc = mcounters->counters_data;
64131a1e03dcSRaed Salem 	for (i = 0; i < mcounters->ncounters; i++)
64141a1e03dcSRaed Salem 		read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
64151a1e03dcSRaed Salem 
64161a1e03dcSRaed Salem err_read:
64171a1e03dcSRaed Salem 	kfree(mread_attr.out);
64181a1e03dcSRaed Salem err_bound:
64191a1e03dcSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
64201a1e03dcSRaed Salem 	return ret;
64211a1e03dcSRaed Salem }
64221a1e03dcSRaed Salem 
6423b29e2a13SRaed Salem static int mlx5_ib_destroy_counters(struct ib_counters *counters)
6424b29e2a13SRaed Salem {
6425b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
6426b29e2a13SRaed Salem 
64273b3233fbSRaed Salem 	counters_clear_description(counters);
64283b3233fbSRaed Salem 	if (mcounters->hw_cntrs_hndl)
64293b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(counters->device)->mdev,
64303b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
64313b3233fbSRaed Salem 
6432b29e2a13SRaed Salem 	kfree(mcounters);
6433b29e2a13SRaed Salem 
6434b29e2a13SRaed Salem 	return 0;
6435b29e2a13SRaed Salem }
6436b29e2a13SRaed Salem 
6437b29e2a13SRaed Salem static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
6438b29e2a13SRaed Salem 						   struct uverbs_attr_bundle *attrs)
6439b29e2a13SRaed Salem {
6440b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters;
6441b29e2a13SRaed Salem 
6442b29e2a13SRaed Salem 	mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
6443b29e2a13SRaed Salem 	if (!mcounters)
6444b29e2a13SRaed Salem 		return ERR_PTR(-ENOMEM);
6445b29e2a13SRaed Salem 
64463b3233fbSRaed Salem 	mutex_init(&mcounters->mcntrs_mutex);
64473b3233fbSRaed Salem 
6448b29e2a13SRaed Salem 	return &mcounters->ibcntrs;
6449b29e2a13SRaed Salem }
6450b29e2a13SRaed Salem 
6451fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
6452e126ba97SEli Cohen {
645332f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
6454806b101bSJason Gunthorpe 	WARN_ON(!xa_empty(&dev->odp_mkeys));
6455806b101bSJason Gunthorpe 	cleanup_srcu_struct(&dev->odp_srcu);
64564056b12eSAriel Levkovich 
645750211ec9SJason Gunthorpe 	WARN_ON(!xa_empty(&dev->sig_mrs));
64584056b12eSAriel Levkovich 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
645916c1975fSMark Bloch }
646016c1975fSMark Bloch 
6461fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
646216c1975fSMark Bloch {
646316c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
6464e126ba97SEli Cohen 	int err;
646532f69e4bSDaniel Jurgens 	int i;
6466e126ba97SEli Cohen 
646732f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
646832f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
646995579e78SMark Bloch 		rwlock_init(&dev->port[i].roce.netdev_lock);
6470d3b5cc1cSMark Bloch 		dev->port[i].roce.dev = dev;
6471d3b5cc1cSMark Bloch 		dev->port[i].roce.native_port_num = i + 1;
6472d3b5cc1cSMark Bloch 		dev->port[i].roce.last_port_state = IB_PORT_DOWN;
647332f69e4bSDaniel Jurgens 	}
647432f69e4bSDaniel Jurgens 
647500815752SMoni Shoua 	mlx5_ib_internal_fill_odp_caps(dev);
647600815752SMoni Shoua 
647732f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
6478e126ba97SEli Cohen 	if (err)
6479da796ccbSMark Bloch 		return err;
6480e126ba97SEli Cohen 
6481a989ea01SMark Bloch 	err = set_has_smi_cap(dev);
6482a989ea01SMark Bloch 	if (err)
6483a989ea01SMark Bloch 		return err;
6484e126ba97SEli Cohen 
648532f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
648632f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
648732f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
648832f69e4bSDaniel Jurgens 			if (err)
648932f69e4bSDaniel Jurgens 				break;
649032f69e4bSDaniel Jurgens 		}
649132f69e4bSDaniel Jurgens 	} else {
649232f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
649332f69e4bSDaniel Jurgens 	}
649432f69e4bSDaniel Jurgens 	if (err)
649532f69e4bSDaniel Jurgens 		goto err_mp;
649632f69e4bSDaniel Jurgens 
64971b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
6498e126ba97SEli Cohen 		get_ext_port_caps(dev);
6499e126ba97SEli Cohen 
6500e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
6501c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
6502e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
6503f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
6504c42260f1SVu Pham 	dev->ib_dev.dev.parent		= mdev->device;
6505cfc1a89eSMaor Gottlieb 	dev->ib_dev.lag_flags		= RDMA_LAG_FLAGS_HASH_ALL_SLAVES;
6506e126ba97SEli Cohen 
65073cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
65083cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
65093cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
6510806b101bSJason Gunthorpe 	xa_init(&dev->odp_mkeys);
651150211ec9SJason Gunthorpe 	xa_init(&dev->sig_mrs);
6512f743ff3bSSaeed Mahameed 	atomic_set(&dev->mkey_var, 0);
65133cc297dbSMark Bloch 
65143b113a1eSAriel Levkovich 	spin_lock_init(&dev->dm.lock);
65153b113a1eSAriel Levkovich 	dev->dm.dev = mdev;
651624da0016SAriel Levkovich 
6517806b101bSJason Gunthorpe 	err = init_srcu_struct(&dev->odp_srcu);
6518a6bc3875SMoni Shoua 	if (err)
6519c9b9dcb4SAriel Levkovich 		goto err_mp;
65203cc297dbSMark Bloch 
652116c1975fSMark Bloch 	return 0;
652225c13324SAriel Levkovich 
652332f69e4bSDaniel Jurgens err_mp:
652432f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
652516c1975fSMark Bloch 
652616c1975fSMark Bloch 	return -ENOMEM;
652716c1975fSMark Bloch }
652816c1975fSMark Bloch 
65299a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
65309a4ca38dSMark Bloch {
65319a4ca38dSMark Bloch 	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
65329a4ca38dSMark Bloch 
65339a4ca38dSMark Bloch 	if (!dev->flow_db)
65349a4ca38dSMark Bloch 		return -ENOMEM;
65359a4ca38dSMark Bloch 
65369a4ca38dSMark Bloch 	mutex_init(&dev->flow_db->lock);
65379a4ca38dSMark Bloch 
65389a4ca38dSMark Bloch 	return 0;
65399a4ca38dSMark Bloch }
65409a4ca38dSMark Bloch 
65419a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
65429a4ca38dSMark Bloch {
65439a4ca38dSMark Bloch 	kfree(dev->flow_db);
65449a4ca38dSMark Bloch }
65459a4ca38dSMark Bloch 
654696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
65477a154142SJason Gunthorpe 	.owner = THIS_MODULE,
6548b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_MLX5,
654972c6ec18SJason Gunthorpe 	.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION,
6550b9560a41SJason Gunthorpe 
655196458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
655296458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
65536c984472SMax Gurtovoy 	.alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
655496458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
655596458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
655696458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
655796458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
655896458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
655996458233SKamal Heib 	.create_counters = mlx5_ib_create_counters,
656096458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
656196458233SKamal Heib 	.create_flow = mlx5_ib_create_flow,
656296458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
656396458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
656496458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
656596458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
656696458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
656796458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
656896458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
656996458233SKamal Heib 	.destroy_counters = mlx5_ib_destroy_counters,
657096458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
657196458233SKamal Heib 	.destroy_flow = mlx5_ib_destroy_flow,
657296458233SKamal Heib 	.destroy_flow_action = mlx5_ib_destroy_flow_action,
657396458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
657496458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
657596458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
657696458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
657796458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
657896458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
657911f552e2SMichael Guralnik 	.enable_driver = mlx5_ib_enable_driver,
65801ccecc88SMaor Gottlieb 	.fill_res_cq_entry_raw = mlx5_ib_fill_res_cq_entry_raw,
6581f4434529SMaor Gottlieb 	.fill_res_mr_entry = mlx5_ib_fill_res_mr_entry,
658228b5fa68SMaor Gottlieb 	.fill_res_mr_entry_raw = mlx5_ib_fill_res_mr_entry_raw,
65831776dd23SMaor Gottlieb 	.fill_res_qp_entry_raw = mlx5_ib_fill_res_qp_entry_raw,
6584f4434529SMaor Gottlieb 	.fill_stat_mr_entry = mlx5_ib_fill_stat_mr_entry,
658596458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
658696458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
658796458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
658896458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
65896c984472SMax Gurtovoy 	.map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
659096458233SKamal Heib 	.mmap = mlx5_ib_mmap,
6591dc2316ebSYishai Hadas 	.mmap_free = mlx5_ib_mmap_free,
659296458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
659396458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
659496458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
659596458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
659696458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
659796458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
6598029e88fdSLeon Romanovsky 	.post_recv = mlx5_ib_post_recv_nodrain,
6599029e88fdSLeon Romanovsky 	.post_send = mlx5_ib_post_send_nodrain,
660096458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
660196458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
660296458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
660396458233SKamal Heib 	.query_device = mlx5_ib_query_device,
660496458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
660596458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
660696458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
660796458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
660896458233SKamal Heib 	.read_counters = mlx5_ib_read_counters,
660996458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
661096458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
661196458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
661296458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
6613d3456914SLeon Romanovsky 
6614d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
6615e39afe3dSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
661621a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
661768e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
6618a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
661996458233SKamal Heib };
662096458233SKamal Heib 
662196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
662296458233SKamal Heib 	.create_flow_action_esp = mlx5_ib_create_flow_action_esp,
662396458233SKamal Heib 	.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
662496458233SKamal Heib };
662596458233SKamal Heib 
662696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
662796458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
662896458233SKamal Heib };
662996458233SKamal Heib 
663096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
663196458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
66329c0015efSDanit Goldberg 	.get_vf_guid = mlx5_ib_get_vf_guid,
663396458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
663496458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
663596458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
663696458233SKamal Heib };
663796458233SKamal Heib 
663896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
663996458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
664096458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
664196458233SKamal Heib };
664296458233SKamal Heib 
664396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
664496458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
664596458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
664696458233SKamal Heib };
664796458233SKamal Heib 
664896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
664996458233SKamal Heib 	.alloc_dm = mlx5_ib_alloc_dm,
665096458233SKamal Heib 	.dealloc_dm = mlx5_ib_dealloc_dm,
665196458233SKamal Heib 	.reg_dm_mr = mlx5_ib_reg_dm_mr,
665296458233SKamal Heib };
665396458233SKamal Heib 
6654f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev)
6655f164be8cSYishai Hadas {
6656f164be8cSYishai Hadas 	struct mlx5_core_dev *mdev = dev->mdev;
6657f164be8cSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
6658f164be8cSYishai Hadas 	u8 log_doorbell_bar_size;
6659f164be8cSYishai Hadas 	u8 log_doorbell_stride;
6660f164be8cSYishai Hadas 	u64 bar_size;
6661f164be8cSYishai Hadas 
6662f164be8cSYishai Hadas 	log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
6663f164be8cSYishai Hadas 					log_doorbell_bar_size);
6664f164be8cSYishai Hadas 	log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
6665f164be8cSYishai Hadas 					log_doorbell_stride);
6666f164be8cSYishai Hadas 	var_table->hw_start_addr = dev->mdev->bar_addr +
6667f164be8cSYishai Hadas 				MLX5_CAP64_DEV_VDPA_EMULATION(mdev,
6668f164be8cSYishai Hadas 					doorbell_bar_offset);
6669f164be8cSYishai Hadas 	bar_size = (1ULL << log_doorbell_bar_size) * 4096;
6670f164be8cSYishai Hadas 	var_table->stride_size = 1ULL << log_doorbell_stride;
667191b74bf5SAlexander Lobakin 	var_table->num_var_hw_entries = div_u64(bar_size,
667291b74bf5SAlexander Lobakin 						var_table->stride_size);
6673f164be8cSYishai Hadas 	mutex_init(&var_table->bitmap_lock);
6674f164be8cSYishai Hadas 	var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries,
6675f164be8cSYishai Hadas 					  GFP_KERNEL);
6676f164be8cSYishai Hadas 	return (var_table->bitmap) ? 0 : -ENOMEM;
6677f164be8cSYishai Hadas }
6678f164be8cSYishai Hadas 
6679f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev)
6680f164be8cSYishai Hadas {
6681f164be8cSYishai Hadas 	bitmap_free(dev->var_table.bitmap);
6682f164be8cSYishai Hadas }
6683f164be8cSYishai Hadas 
6684fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
668516c1975fSMark Bloch {
668616c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
668716c1975fSMark Bloch 	int err;
668816c1975fSMark Bloch 
6689e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
6690e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
6691e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
6692e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
6693e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
6694e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
669541c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
669641c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
6697e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
669856e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
6699e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
6700e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
6701e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
6702e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
6703e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
6704e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
6705e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
6706e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
6707e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
6708e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
6709e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
6710e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
6711e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
6712e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
6713e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
6714e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
6715e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
67161707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
6717d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
6718d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
67197d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
6720b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
672196458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ)	|
672296458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW)	|
672396458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
6724e126ba97SEli Cohen 
6725f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
6726f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
672796458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
672896458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
67298e959601SNiranjana Vishwanathapura 
673096458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
673196458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
67327c2344c3SMaor Gottlieb 
67336e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
67346e8484c5SMax Gurtovoy 
6735d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
6736d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
6737d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
6738d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
673996458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
6740d2370e0aSMatan Barak 	}
6741d2370e0aSMatan Barak 
6742938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
6743e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
6744e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
6745e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
674696458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
6747e126ba97SEli Cohen 	}
6748e126ba97SEli Cohen 
674925c13324SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic) ||
675025c13324SAriel Levkovich 	    MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
675125c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
675296458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
675324da0016SAriel Levkovich 
6754dfb631a1SJason Gunthorpe 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
675596458233SKamal Heib 	    MLX5_ACCEL_IPSEC_CAP_DEVICE)
675696458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
675796458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
675881e30880SYishai Hadas 
675936e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
676036e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
676181e30880SYishai Hadas 
6762e126ba97SEli Cohen 	err = init_node_data(dev);
6763e126ba97SEli Cohen 	if (err)
676416c1975fSMark Bloch 		return err;
6765e126ba97SEli Cohen 
6766c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
6767e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6768e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
6769a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
6770c8b89924SMark Bloch 
6771f164be8cSYishai Hadas 	if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
6772f164be8cSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) {
6773f164be8cSYishai Hadas 		err = mlx5_ib_init_var_table(dev);
6774f164be8cSYishai Hadas 		if (err)
6775f164be8cSYishai Hadas 			return err;
6776f164be8cSYishai Hadas 	}
6777f164be8cSYishai Hadas 
677896e2fd73SLeon Romanovsky 	dev->ib_dev.use_cq_dim = true;
677996e2fd73SLeon Romanovsky 
678016c1975fSMark Bloch 	return 0;
678116c1975fSMark Bloch }
678216c1975fSMark Bloch 
678396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
678496458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
678596458233SKamal Heib 	.query_port = mlx5_ib_query_port,
678696458233SKamal Heib };
678796458233SKamal Heib 
67888e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
678916c1975fSMark Bloch {
679096458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
67918e6efa3aSMark Bloch 	return 0;
67928e6efa3aSMark Bloch }
67938e6efa3aSMark Bloch 
679496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
679596458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
679696458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
679796458233SKamal Heib };
679896458233SKamal Heib 
6799b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev)
68008e6efa3aSMark Bloch {
680196458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
68028e6efa3aSMark Bloch 	return 0;
68038e6efa3aSMark Bloch }
68048e6efa3aSMark Bloch 
680596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
680696458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
680796458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
680896458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
680996458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
681096458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
681196458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
681296458233SKamal Heib };
681396458233SKamal Heib 
6814e3f1ed1fSLeon Romanovsky static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
68158e6efa3aSMark Bloch {
6816e3f1ed1fSLeon Romanovsky 	u8 port_num;
68177fd8aefbSDaniel Jurgens 
6818c11a226aSMark Bloch 	dev->ib_dev.uverbs_ex_cmd_mask |=
6819c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6820c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6821c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6822c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6823c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
682496458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
68258e6efa3aSMark Bloch 
6826e3f1ed1fSLeon Romanovsky 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6827e3f1ed1fSLeon Romanovsky 
682826628e2dSMark Bloch 	/* Register only for native ports */
68298e6efa3aSMark Bloch 	return mlx5_add_netdev_notifier(dev, port_num);
68308e6efa3aSMark Bloch }
68318e6efa3aSMark Bloch 
68328e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
68338e6efa3aSMark Bloch {
68348e6efa3aSMark Bloch 	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
68358e6efa3aSMark Bloch 
68368e6efa3aSMark Bloch 	mlx5_remove_netdev_notifier(dev, port_num);
68378e6efa3aSMark Bloch }
68388e6efa3aSMark Bloch 
6839b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_roce_init(struct mlx5_ib_dev *dev)
68408e6efa3aSMark Bloch {
68418e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
68428e6efa3aSMark Bloch 	enum rdma_link_layer ll;
68438e6efa3aSMark Bloch 	int port_type_cap;
68448e6efa3aSMark Bloch 	int err = 0;
68458e6efa3aSMark Bloch 
68468e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
68478e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
68488e6efa3aSMark Bloch 
68498e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET)
6850e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
68518e6efa3aSMark Bloch 
685216c1975fSMark Bloch 	return err;
6853fc24fc5eSAchiad Shochat }
6854fc24fc5eSAchiad Shochat 
6855b5a498baSMichael Guralnik static void mlx5_ib_stage_raw_eth_roce_cleanup(struct mlx5_ib_dev *dev)
68568e6efa3aSMark Bloch {
68578e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
68588e6efa3aSMark Bloch }
68598e6efa3aSMark Bloch 
68608e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
68618e6efa3aSMark Bloch {
68628e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
68638e6efa3aSMark Bloch 	enum rdma_link_layer ll;
68648e6efa3aSMark Bloch 	int port_type_cap;
68658e6efa3aSMark Bloch 	int err;
68668e6efa3aSMark Bloch 
68678e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
68688e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
68698e6efa3aSMark Bloch 
68708e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
6871e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
68728e6efa3aSMark Bloch 		if (err)
68738e6efa3aSMark Bloch 			return err;
68748e6efa3aSMark Bloch 
6875e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
68768e6efa3aSMark Bloch 		if (err)
68778e6efa3aSMark Bloch 			goto cleanup;
68788e6efa3aSMark Bloch 	}
68798e6efa3aSMark Bloch 
688016c1975fSMark Bloch 	return 0;
68818e6efa3aSMark Bloch cleanup:
68828e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
68838e6efa3aSMark Bloch 
68848e6efa3aSMark Bloch 	return err;
688545bded2cSKamal Heib }
68866aec21f6SHaggai Eran 
688716c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
688816c1975fSMark Bloch {
688916c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
689016c1975fSMark Bloch 	enum rdma_link_layer ll;
689116c1975fSMark Bloch 	int port_type_cap;
68924a2da0b8SParav Pandit 
689316c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
689416c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
689516c1975fSMark Bloch 
689616c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
689716c1975fSMark Bloch 		mlx5_disable_eth(dev);
68988e6efa3aSMark Bloch 		mlx5_ib_stage_common_roce_cleanup(dev);
689916c1975fSMark Bloch 	}
690016c1975fSMark Bloch }
690116c1975fSMark Bloch 
6902fb652d32SMark Bloch static int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
690316c1975fSMark Bloch {
690416c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
690516c1975fSMark Bloch }
690616c1975fSMark Bloch 
6907fb652d32SMark Bloch static void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
690816c1975fSMark Bloch {
690916c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
691016c1975fSMark Bloch }
691116c1975fSMark Bloch 
691216c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
691316c1975fSMark Bloch {
691416c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
691516c1975fSMark Bloch }
691616c1975fSMark Bloch 
6917f3ffed0cSKamal Heib static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
6918d5d284b8SSaeed Mahameed {
6919d5d284b8SSaeed Mahameed 	mlx5_ib_odp_cleanup_one(dev);
6920d5d284b8SSaeed Mahameed }
6921d5d284b8SSaeed Mahameed 
692296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
692396458233SKamal Heib 	.alloc_hw_stats = mlx5_ib_alloc_hw_stats,
692496458233SKamal Heib 	.get_hw_stats = mlx5_ib_get_hw_stats,
692545842fc6SMark Zhang 	.counter_bind_qp = mlx5_ib_counter_bind_qp,
692645842fc6SMark Zhang 	.counter_unbind_qp = mlx5_ib_counter_unbind_qp,
692745842fc6SMark Zhang 	.counter_dealloc = mlx5_ib_counter_dealloc,
692818d422ceSMark Zhang 	.counter_alloc_stats = mlx5_ib_counter_alloc_stats,
692918d422ceSMark Zhang 	.counter_update_stats = mlx5_ib_counter_update_stats,
693096458233SKamal Heib };
693196458233SKamal Heib 
6932fb652d32SMark Bloch static int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
693316c1975fSMark Bloch {
69345e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
693596458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
69365e1e7612SMark Bloch 
693716c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
69385e1e7612SMark Bloch 	}
693916c1975fSMark Bloch 
694016c1975fSMark Bloch 	return 0;
694116c1975fSMark Bloch }
694216c1975fSMark Bloch 
6943fb652d32SMark Bloch static void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
694416c1975fSMark Bloch {
694516c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
694616c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
694716c1975fSMark Bloch }
694816c1975fSMark Bloch 
694916c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
695016c1975fSMark Bloch {
695173eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
6952a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
695373eb8f03SGreg Kroah-Hartman 	return 0;
695416c1975fSMark Bloch }
695516c1975fSMark Bloch 
695616c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
695716c1975fSMark Bloch {
6958a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
6959a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
696016c1975fSMark Bloch }
696116c1975fSMark Bloch 
696216c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
696316c1975fSMark Bloch {
69645fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
6965444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
696616c1975fSMark Bloch }
696716c1975fSMark Bloch 
696816c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
696916c1975fSMark Bloch {
697016c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
697116c1975fSMark Bloch }
697216c1975fSMark Bloch 
6973fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
697416c1975fSMark Bloch {
697516c1975fSMark Bloch 	int err;
69765fe9dec0SEli Cohen 
69775fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
69785fe9dec0SEli Cohen 	if (err)
697916c1975fSMark Bloch 		return err;
69805fe9dec0SEli Cohen 
69815fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
69825fe9dec0SEli Cohen 	if (err)
698316c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
69845fe9dec0SEli Cohen 
698516c1975fSMark Bloch 	return err;
698616c1975fSMark Bloch }
69870837e86aSMark Bloch 
6988fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
698916c1975fSMark Bloch {
699016c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
699116c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
699216c1975fSMark Bloch }
6993e126ba97SEli Cohen 
6994fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
699516c1975fSMark Bloch {
6996e349f858SJason Gunthorpe 	const char *name;
6997e349f858SJason Gunthorpe 
6998508a523fSParav Pandit 	rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
69997c34ec19SAviv Heller 	if (!mlx5_lag_is_roce(dev->mdev))
7000e349f858SJason Gunthorpe 		name = "mlx5_%d";
7001e349f858SJason Gunthorpe 	else
7002e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
7003ea4baf7fSParav Pandit 	return ib_register_device(&dev->ib_dev, name);
700416c1975fSMark Bloch }
700516c1975fSMark Bloch 
7006fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
700742cea83fSMark Bloch {
700842cea83fSMark Bloch 	destroy_umrc_res(dev);
700942cea83fSMark Bloch }
701042cea83fSMark Bloch 
7011fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
701216c1975fSMark Bloch {
701316c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
701416c1975fSMark Bloch }
701516c1975fSMark Bloch 
7016fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
701716c1975fSMark Bloch {
701816c1975fSMark Bloch 	return create_umr_res(dev);
701916c1975fSMark Bloch }
702016c1975fSMark Bloch 
702116c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
702216c1975fSMark Bloch {
702303404e8aSMaor Gottlieb 	init_delay_drop(dev);
702403404e8aSMaor Gottlieb 
702516c1975fSMark Bloch 	return 0;
702616c1975fSMark Bloch }
702716c1975fSMark Bloch 
702816c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
702916c1975fSMark Bloch {
703016c1975fSMark Bloch 	cancel_delay_drop(dev);
703116c1975fSMark Bloch }
703216c1975fSMark Bloch 
7033df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
7034df097a27SSaeed Mahameed {
7035df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
7036df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
7037df097a27SSaeed Mahameed 	return 0;
7038df097a27SSaeed Mahameed }
7039df097a27SSaeed Mahameed 
7040df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
7041df097a27SSaeed Mahameed {
7042df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
7043df097a27SSaeed Mahameed }
7044df097a27SSaeed Mahameed 
704581773ce5SLeon Romanovsky static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
704681773ce5SLeon Romanovsky {
704781773ce5SLeon Romanovsky 	int uid;
704881773ce5SLeon Romanovsky 
7049fb98153bSYishai Hadas 	uid = mlx5_ib_devx_create(dev, false);
7050e337dd53SYishai Hadas 	if (uid > 0) {
705181773ce5SLeon Romanovsky 		dev->devx_whitelist_uid = uid;
7052e337dd53SYishai Hadas 		mlx5_ib_devx_init_event_table(dev);
7053e337dd53SYishai Hadas 	}
705481773ce5SLeon Romanovsky 
705581773ce5SLeon Romanovsky 	return 0;
705681773ce5SLeon Romanovsky }
705781773ce5SLeon Romanovsky static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
705881773ce5SLeon Romanovsky {
7059e337dd53SYishai Hadas 	if (dev->devx_whitelist_uid) {
7060e337dd53SYishai Hadas 		mlx5_ib_devx_cleanup_event_table(dev);
706181773ce5SLeon Romanovsky 		mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
706281773ce5SLeon Romanovsky 	}
7063e337dd53SYishai Hadas }
706481773ce5SLeon Romanovsky 
706511f552e2SMichael Guralnik int mlx5_ib_enable_driver(struct ib_device *dev)
706611f552e2SMichael Guralnik {
706711f552e2SMichael Guralnik 	struct mlx5_ib_dev *mdev = to_mdev(dev);
706811f552e2SMichael Guralnik 	int ret;
706911f552e2SMichael Guralnik 
707011f552e2SMichael Guralnik 	ret = mlx5_ib_test_wc(mdev);
707111f552e2SMichael Guralnik 	mlx5_ib_dbg(mdev, "Write-Combining %s",
707211f552e2SMichael Guralnik 		    mdev->wc_support ? "supported" : "not supported");
707311f552e2SMichael Guralnik 
707411f552e2SMichael Guralnik 	return ret;
707511f552e2SMichael Guralnik }
707611f552e2SMichael Guralnik 
7077b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
707816c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
707916c1975fSMark Bloch 		      int stage)
708016c1975fSMark Bloch {
70814cca96a8SParav Pandit 	dev->ib_active = false;
70824cca96a8SParav Pandit 
708316c1975fSMark Bloch 	/* Number of stages to cleanup */
708416c1975fSMark Bloch 	while (stage) {
708516c1975fSMark Bloch 		stage--;
708616c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
708716c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
708816c1975fSMark Bloch 	}
70894a6dc855SMark Bloch 
7090da796ccbSMark Bloch 	kfree(dev->port);
70914a6dc855SMark Bloch 	ib_dealloc_device(&dev->ib_dev);
709216c1975fSMark Bloch }
709316c1975fSMark Bloch 
7094b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
709516c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
709616c1975fSMark Bloch {
709716c1975fSMark Bloch 	int err;
709816c1975fSMark Bloch 	int i;
709916c1975fSMark Bloch 
71008d93efb8SMark Bloch 	dev->profile = profile;
71018d93efb8SMark Bloch 
710216c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
710316c1975fSMark Bloch 		if (profile->stage[i].init) {
710416c1975fSMark Bloch 			err = profile->stage[i].init(dev);
710516c1975fSMark Bloch 			if (err)
710616c1975fSMark Bloch 				goto err_out;
710716c1975fSMark Bloch 		}
710816c1975fSMark Bloch 	}
710916c1975fSMark Bloch 
7110e126ba97SEli Cohen 	dev->ib_active = true;
7111e126ba97SEli Cohen 
71129603b61dSJack Morgenstein 	return dev;
7113e126ba97SEli Cohen 
711416c1975fSMark Bloch err_out:
711516c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
7116e126ba97SEli Cohen 
71179603b61dSJack Morgenstein 	return NULL;
7118e126ba97SEli Cohen }
7119e126ba97SEli Cohen 
712016c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
712116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
712216c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
712316c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
71249a4ca38dSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
71259a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_init,
71269a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
712716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
712816c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
7129f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
71308e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
71318e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
71328e6efa3aSMark Bloch 		     NULL),
713316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
713416c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
713516c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
7136333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
7137333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
7138333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
7139f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
7140f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
7141f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
714216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
714316c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
714416c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
7145df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
7146df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
7147df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
714816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
714916c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
7150d5d284b8SSaeed Mahameed 		     mlx5_ib_stage_odp_cleanup),
715116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
715216c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
715316c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
715416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
715516c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
715616c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
715716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
715816c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
715916c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
716016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
716116c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
716216c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
716342cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
716442cea83fSMark Bloch 		     NULL,
716542cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
716681773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
716781773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_init,
716881773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_cleanup),
716916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
717016c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
717116c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
717242cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
717342cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
717442cea83fSMark Bloch 		     NULL),
717516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
717616c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
717716c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
717816c1975fSMark Bloch };
717916c1975fSMark Bloch 
7180b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = {
7181b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
7182b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
7183b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
7184b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
7185b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_init,
7186b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
7187b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
7188b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
7189f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
7190b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
7191b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_non_default_cb,
7192b5ca15adSMark Bloch 		     NULL),
7193b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
7194b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_roce_init,
7195b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_roce_cleanup),
7196333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
7197333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
7198333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
7199f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
7200f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
7201f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
7202b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
7203b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_init,
7204b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
7205df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
7206df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
7207df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
7208b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
7209b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_init,
7210b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
721179db784eSParav Pandit 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
721279db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_init,
721379db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_cleanup),
7214b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
7215b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
7216b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
7217b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
7218b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
7219b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
722003fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
722103fe2debSDavid S. Miller 		     NULL,
722203fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
72237f575103SMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
72247f575103SMark Bloch 		     mlx5_ib_stage_devx_init,
72257f575103SMark Bloch 		     mlx5_ib_stage_devx_cleanup),
7226b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
7227b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
7228b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
722903fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
723003fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
723103fe2debSDavid S. Miller 		     NULL),
7232b5ca15adSMark Bloch };
7233b5ca15adSMark Bloch 
7234e3f1ed1fSLeon Romanovsky static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
723532f69e4bSDaniel Jurgens {
723632f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
723732f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
723832f69e4bSDaniel Jurgens 	bool bound = false;
723932f69e4bSDaniel Jurgens 	int err;
724032f69e4bSDaniel Jurgens 
724132f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
724232f69e4bSDaniel Jurgens 	if (!mpi)
724332f69e4bSDaniel Jurgens 		return NULL;
724432f69e4bSDaniel Jurgens 
724532f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
724632f69e4bSDaniel Jurgens 
724732f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
724832f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
724932f69e4bSDaniel Jurgens 	if (err) {
725032f69e4bSDaniel Jurgens 		kfree(mpi);
725132f69e4bSDaniel Jurgens 		return NULL;
725232f69e4bSDaniel Jurgens 	}
725332f69e4bSDaniel Jurgens 
725432f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
725532f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
725632f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
725732f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
725832f69e4bSDaniel Jurgens 
725932f69e4bSDaniel Jurgens 		if (bound) {
726032f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
726132f69e4bSDaniel Jurgens 			break;
726232f69e4bSDaniel Jurgens 		}
726332f69e4bSDaniel Jurgens 	}
726432f69e4bSDaniel Jurgens 
726532f69e4bSDaniel Jurgens 	if (!bound) {
726632f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
7267c42260f1SVu Pham 		dev_dbg(mdev->device,
7268c42260f1SVu Pham 			"no suitable IB device found to bind to, added to unaffiliated list.\n");
726932f69e4bSDaniel Jurgens 	}
727032f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
727132f69e4bSDaniel Jurgens 
727232f69e4bSDaniel Jurgens 	return mpi;
727332f69e4bSDaniel Jurgens }
727432f69e4bSDaniel Jurgens 
727516c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
727616c1975fSMark Bloch {
727794de879cSMichael Guralnik 	const struct mlx5_ib_profile *profile;
727832f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
7279b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
728032f69e4bSDaniel Jurgens 	int port_type_cap;
7281da796ccbSMark Bloch 	int num_ports;
728232f69e4bSDaniel Jurgens 
7283f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) &&
7284f6455de0SBodong Wang 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == MLX5_ESWITCH_OFFLOADS) {
72855fb58c9eSMark Bloch 		if (!mlx5_core_mp_enabled(mdev))
7286f0666f1fSBodong Wang 			mlx5_ib_register_vport_reps(mdev);
7287f0666f1fSBodong Wang 		return mdev;
7288f0666f1fSBodong Wang 	}
7289f0666f1fSBodong Wang 
729032f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
729132f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
729232f69e4bSDaniel Jurgens 
7293e3f1ed1fSLeon Romanovsky 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
7294e3f1ed1fSLeon Romanovsky 		return mlx5_ib_add_slave_port(mdev);
729532f69e4bSDaniel Jurgens 
7296da796ccbSMark Bloch 	num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
7297da796ccbSMark Bloch 			MLX5_CAP_GEN(mdev, num_vhca_ports));
7298459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
7299b5ca15adSMark Bloch 	if (!dev)
7300b5ca15adSMark Bloch 		return NULL;
7301da796ccbSMark Bloch 	dev->port = kcalloc(num_ports, sizeof(*dev->port),
7302da796ccbSMark Bloch 			     GFP_KERNEL);
7303da796ccbSMark Bloch 	if (!dev->port) {
7304a5c9c299SParav Pandit 		ib_dealloc_device(&dev->ib_dev);
7305da796ccbSMark Bloch 		return NULL;
7306da796ccbSMark Bloch 	}
7307b5ca15adSMark Bloch 
7308b5ca15adSMark Bloch 	dev->mdev = mdev;
7309da796ccbSMark Bloch 	dev->num_ports = num_ports;
7310b5ca15adSMark Bloch 
731194de879cSMichael Guralnik 	if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_is_roce_enabled(mdev))
731294de879cSMichael Guralnik 		profile = &raw_eth_profile;
731394de879cSMichael Guralnik 	else
731494de879cSMichael Guralnik 		profile = &pf_profile;
731594de879cSMichael Guralnik 
731694de879cSMichael Guralnik 	return __mlx5_ib_add(dev, profile);
731716c1975fSMark Bloch }
731816c1975fSMark Bloch 
73199603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
7320e126ba97SEli Cohen {
732132f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
732232f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
73236aec21f6SHaggai Eran 
7324f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
7325f0666f1fSBodong Wang 		mlx5_ib_unregister_vport_reps(mdev);
7326f0666f1fSBodong Wang 		return;
7327f0666f1fSBodong Wang 	}
7328f0666f1fSBodong Wang 
732932f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
733032f69e4bSDaniel Jurgens 		mpi = context;
733132f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
733232f69e4bSDaniel Jurgens 		if (mpi->ibdev)
733332f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
733432f69e4bSDaniel Jurgens 		list_del(&mpi->list);
733532f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
73365d44adebSDanit Goldberg 		kfree(mpi);
733732f69e4bSDaniel Jurgens 		return;
733832f69e4bSDaniel Jurgens 	}
733932f69e4bSDaniel Jurgens 
734032f69e4bSDaniel Jurgens 	dev = context;
734116c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
7342e126ba97SEli Cohen }
7343e126ba97SEli Cohen 
73449603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
73459603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
73469603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
734764613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
7348e126ba97SEli Cohen };
7349e126ba97SEli Cohen 
7350c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
7351c44ef998SIlya Lesokhin {
7352c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
7353c44ef998SIlya Lesokhin 	return xlt_emergency_page;
7354c44ef998SIlya Lesokhin }
7355c44ef998SIlya Lesokhin 
7356c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
7357c44ef998SIlya Lesokhin {
7358c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
7359c44ef998SIlya Lesokhin }
7360c44ef998SIlya Lesokhin 
7361e126ba97SEli Cohen static int __init mlx5_ib_init(void)
7362e126ba97SEli Cohen {
73636aec21f6SHaggai Eran 	int err;
73646aec21f6SHaggai Eran 
7365c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
7366c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
7367d69a24e0SDaniel Jurgens 		return -ENOMEM;
7368d69a24e0SDaniel Jurgens 
7369c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
7370c44ef998SIlya Lesokhin 
7371c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
7372c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
7373c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
7374c44ef998SIlya Lesokhin 		return -ENOMEM;
7375c44ef998SIlya Lesokhin 	}
7376c44ef998SIlya Lesokhin 
737781713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
73789603b61dSJack Morgenstein 
73796aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
73806aec21f6SHaggai Eran 
73816aec21f6SHaggai Eran 	return err;
7382e126ba97SEli Cohen }
7383e126ba97SEli Cohen 
7384e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
7385e126ba97SEli Cohen {
73869603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
7387d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
7388c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
7389c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
7390e126ba97SEli Cohen }
7391e126ba97SEli Cohen 
7392e126ba97SEli Cohen module_init(mlx5_ib_init);
7393e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
7394