xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 4e0e2ea1886afe8c001971ff767f6670312a9b04)
1e126ba97SEli Cohen /*
26cf0a15fSSaeed Mahameed  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3e126ba97SEli Cohen  *
4e126ba97SEli Cohen  * This software is available to you under a choice of one of two
5e126ba97SEli Cohen  * licenses.  You may choose to be licensed under the terms of the GNU
6e126ba97SEli Cohen  * General Public License (GPL) Version 2, available from the file
7e126ba97SEli Cohen  * COPYING in the main directory of this source tree, or the
8e126ba97SEli Cohen  * OpenIB.org BSD license below:
9e126ba97SEli Cohen  *
10e126ba97SEli Cohen  *     Redistribution and use in source and binary forms, with or
11e126ba97SEli Cohen  *     without modification, are permitted provided that the following
12e126ba97SEli Cohen  *     conditions are met:
13e126ba97SEli Cohen  *
14e126ba97SEli Cohen  *      - Redistributions of source code must retain the above
15e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
16e126ba97SEli Cohen  *        disclaimer.
17e126ba97SEli Cohen  *
18e126ba97SEli Cohen  *      - Redistributions in binary form must reproduce the above
19e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
20e126ba97SEli Cohen  *        disclaimer in the documentation and/or other materials
21e126ba97SEli Cohen  *        provided with the distribution.
22e126ba97SEli Cohen  *
23e126ba97SEli Cohen  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e126ba97SEli Cohen  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e126ba97SEli Cohen  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e126ba97SEli Cohen  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e126ba97SEli Cohen  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e126ba97SEli Cohen  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e126ba97SEli Cohen  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e126ba97SEli Cohen  * SOFTWARE.
31e126ba97SEli Cohen  */
32e126ba97SEli Cohen 
33fe248c3aSMaor Gottlieb #include <linux/debugfs.h>
34adec640eSChristoph Hellwig #include <linux/highmem.h>
35e126ba97SEli Cohen #include <linux/module.h>
36e126ba97SEli Cohen #include <linux/init.h>
37e126ba97SEli Cohen #include <linux/errno.h>
38e126ba97SEli Cohen #include <linux/pci.h>
39e126ba97SEli Cohen #include <linux/dma-mapping.h>
40e126ba97SEli Cohen #include <linux/slab.h>
4124da0016SAriel Levkovich #include <linux/bitmap.h>
4237aa5c36SGuy Levi #if defined(CONFIG_X86)
4337aa5c36SGuy Levi #include <asm/pat.h>
4437aa5c36SGuy Levi #endif
45e126ba97SEli Cohen #include <linux/sched.h>
466e84f315SIngo Molnar #include <linux/sched/mm.h>
470881e7bdSIngo Molnar #include <linux/sched/task.h>
487c2344c3SMaor Gottlieb #include <linux/delay.h>
49e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
503f89a643SAchiad Shochat #include <rdma/ib_addr.h>
512811ba51SAchiad Shochat #include <rdma/ib_cache.h>
52ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
531b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
5472c7fe90SPravin Shedge #include <linux/mlx5/fs.h>
557c2344c3SMaor Gottlieb #include <linux/list.h>
56e126ba97SEli Cohen #include <rdma/ib_smi.h>
57e126ba97SEli Cohen #include <rdma/ib_umem.h>
58038d2ef8SMaor Gottlieb #include <linux/in.h>
59038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
60e126ba97SEli Cohen #include "mlx5_ib.h"
61fc385b7aSMark Bloch #include "ib_rep.h"
62e1f24a79SParav Pandit #include "cmd.h"
63f3da6577SLeon Romanovsky #include "srq.h"
643346c487SBoris Pismenny #include <linux/mlx5/fs_helpers.h>
65c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h>
668c84660bSMatan Barak #include <rdma/uverbs_std_types.h>
67c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h>
68c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h>
698c84660bSMatan Barak 
708c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
718c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
72e126ba97SEli Cohen 
73e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
74b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0"
75e126ba97SEli Cohen 
76e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
77e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
78e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
79e126ba97SEli Cohen 
80e126ba97SEli Cohen static char mlx5_version[] =
81e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
82b359911dSTariq Toukan 	DRIVER_VERSION "\n";
83e126ba97SEli Cohen 
84d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
85d69a24e0SDaniel Jurgens 	struct work_struct	work;
86df097a27SSaeed Mahameed 	union {
87df097a27SSaeed Mahameed 		struct mlx5_ib_dev	      *dev;
88df097a27SSaeed Mahameed 		struct mlx5_ib_multiport_info *mpi;
89df097a27SSaeed Mahameed 	};
90df097a27SSaeed Mahameed 	bool			is_slave;
91134e9349SSaeed Mahameed 	unsigned int		event;
92df097a27SSaeed Mahameed 	void			*param;
93d69a24e0SDaniel Jurgens };
94d69a24e0SDaniel Jurgens 
95da7525d2SEran Ben Elisha enum {
96da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
97da7525d2SEran Ben Elisha };
981b5daf11SMajd Dibbiny 
99d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
10032f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
10132f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
10232f69e4bSDaniel Jurgens /*
10332f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
10432f69e4bSDaniel Jurgens  */
10532f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
10632f69e4bSDaniel Jurgens 
107c44ef998SIlya Lesokhin /* We can't use an array for xlt_emergency_page because dma_map_single
108c44ef998SIlya Lesokhin  * doesn't work on kernel modules memory
109c44ef998SIlya Lesokhin  */
110c44ef998SIlya Lesokhin static unsigned long xlt_emergency_page;
111c44ef998SIlya Lesokhin static struct mutex xlt_emergency_page_mutex;
112c44ef998SIlya Lesokhin 
11332f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
11432f69e4bSDaniel Jurgens {
11532f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
11632f69e4bSDaniel Jurgens 
11732f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
11832f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
11932f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
12032f69e4bSDaniel Jurgens 	return dev;
12132f69e4bSDaniel Jurgens }
12232f69e4bSDaniel Jurgens 
1231b5daf11SMajd Dibbiny static enum rdma_link_layer
124ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
1251b5daf11SMajd Dibbiny {
126ebd61f68SAchiad Shochat 	switch (port_type_cap) {
1271b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
1281b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
1291b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
1301b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
1311b5daf11SMajd Dibbiny 	default:
1321b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1331b5daf11SMajd Dibbiny 	}
1341b5daf11SMajd Dibbiny }
1351b5daf11SMajd Dibbiny 
136ebd61f68SAchiad Shochat static enum rdma_link_layer
137ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
138ebd61f68SAchiad Shochat {
139ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
140ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
141ebd61f68SAchiad Shochat 
142ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
143ebd61f68SAchiad Shochat }
144ebd61f68SAchiad Shochat 
145fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
146fd65f1b8SMoni Shoua 			  u8 port_num,
147fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
148fd65f1b8SMoni Shoua {
149fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
150fd65f1b8SMoni Shoua 	int ret;
151fd65f1b8SMoni Shoua 
152fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1533023a1e9SKamal Heib 	ret = ibdev->ops.query_port(ibdev, port_num, &attr);
154fd65f1b8SMoni Shoua 	if (!ret)
155fd65f1b8SMoni Shoua 		*state = attr.state;
156fd65f1b8SMoni Shoua 	return ret;
157fd65f1b8SMoni Shoua }
158fd65f1b8SMoni Shoua 
15935b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
16035b0aa67SMark Bloch 					   struct net_device *ndev,
16135b0aa67SMark Bloch 					   u8 *port_num)
16235b0aa67SMark Bloch {
16335b0aa67SMark Bloch 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
16435b0aa67SMark Bloch 	struct net_device *rep_ndev;
16535b0aa67SMark Bloch 	struct mlx5_ib_port *port;
16635b0aa67SMark Bloch 	int i;
16735b0aa67SMark Bloch 
16835b0aa67SMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
16935b0aa67SMark Bloch 		port  = &dev->port[i];
17035b0aa67SMark Bloch 		if (!port->rep)
17135b0aa67SMark Bloch 			continue;
17235b0aa67SMark Bloch 
17335b0aa67SMark Bloch 		read_lock(&port->roce.netdev_lock);
17435b0aa67SMark Bloch 		rep_ndev = mlx5_ib_get_rep_netdev(esw,
17535b0aa67SMark Bloch 						  port->rep->vport);
17635b0aa67SMark Bloch 		if (rep_ndev == ndev) {
17735b0aa67SMark Bloch 			read_unlock(&port->roce.netdev_lock);
17835b0aa67SMark Bloch 			*port_num = i + 1;
17935b0aa67SMark Bloch 			return &port->roce;
18035b0aa67SMark Bloch 		}
18135b0aa67SMark Bloch 		read_unlock(&port->roce.netdev_lock);
18235b0aa67SMark Bloch 	}
18335b0aa67SMark Bloch 
18435b0aa67SMark Bloch 	return NULL;
18535b0aa67SMark Bloch }
18635b0aa67SMark Bloch 
187fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
188fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
189fc24fc5eSAchiad Shochat {
1907fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
191fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1927fd8aefbSDaniel Jurgens 	u8 port_num = roce->native_port_num;
1937fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1947fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1957fd8aefbSDaniel Jurgens 
1967fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
19732f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
19832f69e4bSDaniel Jurgens 	if (!mdev)
19932f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
200fc24fc5eSAchiad Shochat 
2015ec8c83eSAviv Heller 	switch (event) {
2025ec8c83eSAviv Heller 	case NETDEV_REGISTER:
20335b0aa67SMark Bloch 		/* Should already be registered during the load */
20435b0aa67SMark Bloch 		if (ibdev->is_rep)
20535b0aa67SMark Bloch 			break;
2067fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
207dce45af5SLinus Torvalds 		if (ndev->dev.parent == mdev->device)
208842a9c83SOr Gerlitz 			roce->netdev = ndev;
2097fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
2105ec8c83eSAviv Heller 		break;
2115ec8c83eSAviv Heller 
212842a9c83SOr Gerlitz 	case NETDEV_UNREGISTER:
21335b0aa67SMark Bloch 		/* In case of reps, ib device goes away before the netdevs */
214842a9c83SOr Gerlitz 		write_lock(&roce->netdev_lock);
215842a9c83SOr Gerlitz 		if (roce->netdev == ndev)
216842a9c83SOr Gerlitz 			roce->netdev = NULL;
217842a9c83SOr Gerlitz 		write_unlock(&roce->netdev_lock);
218842a9c83SOr Gerlitz 		break;
219842a9c83SOr Gerlitz 
220fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
2215ec8c83eSAviv Heller 	case NETDEV_UP:
22288621dfeSAviv Heller 	case NETDEV_DOWN: {
2237fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
22488621dfeSAviv Heller 		struct net_device *upper = NULL;
22588621dfeSAviv Heller 
22688621dfeSAviv Heller 		if (lag_ndev) {
22788621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
22888621dfeSAviv Heller 			dev_put(lag_ndev);
22988621dfeSAviv Heller 		}
23088621dfeSAviv Heller 
23135b0aa67SMark Bloch 		if (ibdev->is_rep)
23235b0aa67SMark Bloch 			roce = mlx5_get_rep_roce(ibdev, ndev, &port_num);
23335b0aa67SMark Bloch 		if (!roce)
23435b0aa67SMark Bloch 			return NOTIFY_DONE;
2357fd8aefbSDaniel Jurgens 		if ((upper == ndev || (!upper && ndev == roce->netdev))
23688621dfeSAviv Heller 		    && ibdev->ib_active) {
237626bc02dSBart Van Assche 			struct ib_event ibev = { };
238fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2395ec8c83eSAviv Heller 
2407fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2417fd8aefbSDaniel Jurgens 					   &port_state))
2427fd8aefbSDaniel Jurgens 				goto done;
243fd65f1b8SMoni Shoua 
2447fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2457fd8aefbSDaniel Jurgens 				goto done;
246fd65f1b8SMoni Shoua 
2477fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2485ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
249fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
250fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
251fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
252fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
253fd65f1b8SMoni Shoua 			else
2547fd8aefbSDaniel Jurgens 				goto done;
255fd65f1b8SMoni Shoua 
2567fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2575ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2585ec8c83eSAviv Heller 		}
2595ec8c83eSAviv Heller 		break;
26088621dfeSAviv Heller 	}
2615ec8c83eSAviv Heller 
2625ec8c83eSAviv Heller 	default:
2635ec8c83eSAviv Heller 		break;
2645ec8c83eSAviv Heller 	}
2657fd8aefbSDaniel Jurgens done:
26632f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
267fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
268fc24fc5eSAchiad Shochat }
269fc24fc5eSAchiad Shochat 
270fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
271fc24fc5eSAchiad Shochat 					     u8 port_num)
272fc24fc5eSAchiad Shochat {
273fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
274fc24fc5eSAchiad Shochat 	struct net_device *ndev;
27532f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
276fc24fc5eSAchiad Shochat 
27732f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
27832f69e4bSDaniel Jurgens 	if (!mdev)
27932f69e4bSDaniel Jurgens 		return NULL;
28032f69e4bSDaniel Jurgens 
28132f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
28288621dfeSAviv Heller 	if (ndev)
28332f69e4bSDaniel Jurgens 		goto out;
28488621dfeSAviv Heller 
285fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
286fc24fc5eSAchiad Shochat 	 */
28795579e78SMark Bloch 	read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
28895579e78SMark Bloch 	ndev = ibdev->port[port_num - 1].roce.netdev;
289fc24fc5eSAchiad Shochat 	if (ndev)
290fc24fc5eSAchiad Shochat 		dev_hold(ndev);
29195579e78SMark Bloch 	read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
292fc24fc5eSAchiad Shochat 
29332f69e4bSDaniel Jurgens out:
29432f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
295fc24fc5eSAchiad Shochat 	return ndev;
296fc24fc5eSAchiad Shochat }
297fc24fc5eSAchiad Shochat 
29832f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
29932f69e4bSDaniel Jurgens 						   u8 ib_port_num,
30032f69e4bSDaniel Jurgens 						   u8 *native_port_num)
30132f69e4bSDaniel Jurgens {
30232f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
30332f69e4bSDaniel Jurgens 							  ib_port_num);
30432f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
30532f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
30632f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
30732f69e4bSDaniel Jurgens 
308210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
309210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
310210b1f78SMark Bloch 		if (native_port_num)
311210b1f78SMark Bloch 			*native_port_num = ib_port_num;
312210b1f78SMark Bloch 		return ibdev->mdev;
313210b1f78SMark Bloch 	}
314210b1f78SMark Bloch 
31532f69e4bSDaniel Jurgens 	if (native_port_num)
31632f69e4bSDaniel Jurgens 		*native_port_num = 1;
31732f69e4bSDaniel Jurgens 
31832f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
31932f69e4bSDaniel Jurgens 	if (!port)
32032f69e4bSDaniel Jurgens 		return NULL;
32132f69e4bSDaniel Jurgens 
32232f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
32332f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
32432f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
32532f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
32632f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
32732f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
32832f69e4bSDaniel Jurgens 		 */
32932f69e4bSDaniel Jurgens 		if (!mpi->is_master)
33032f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
33132f69e4bSDaniel Jurgens 	}
33232f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
33332f69e4bSDaniel Jurgens 
33432f69e4bSDaniel Jurgens 	return mdev;
33532f69e4bSDaniel Jurgens }
33632f69e4bSDaniel Jurgens 
33732f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
33832f69e4bSDaniel Jurgens {
33932f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
34032f69e4bSDaniel Jurgens 							  port_num);
34132f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
34232f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
34332f69e4bSDaniel Jurgens 
34432f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
34532f69e4bSDaniel Jurgens 		return;
34632f69e4bSDaniel Jurgens 
34732f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
34832f69e4bSDaniel Jurgens 
34932f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
35032f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
35132f69e4bSDaniel Jurgens 	if (mpi->is_master)
35232f69e4bSDaniel Jurgens 		goto out;
35332f69e4bSDaniel Jurgens 
35432f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
35532f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
35632f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
35732f69e4bSDaniel Jurgens out:
35832f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
35932f69e4bSDaniel Jurgens }
36032f69e4bSDaniel Jurgens 
36108e8676fSAya Levin static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, u8 *active_speed,
362f1b65df5SNoa Osherovich 					   u8 *active_width)
363f1b65df5SNoa Osherovich {
364f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
365f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
366f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
367f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
368f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
369f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
370f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
371f1b65df5SNoa Osherovich 		break;
372f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
373f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
374f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
375f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
376f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
377f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
378f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
379f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
380f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
381f1b65df5SNoa Osherovich 		break;
382f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
383f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
384f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
385f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
386f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
387f1b65df5SNoa Osherovich 		break;
388f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
389f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
390f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
391f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
392f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
393f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
394f1b65df5SNoa Osherovich 		break;
395f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
396f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
397f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
398f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
399f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
400f1b65df5SNoa Osherovich 		break;
401f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
402f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
403f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
404f1b65df5SNoa Osherovich 		break;
405f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
406f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
407f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
408f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
409f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
410f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
411f1b65df5SNoa Osherovich 		break;
412f1b65df5SNoa Osherovich 	default:
413f1b65df5SNoa Osherovich 		return -EINVAL;
414f1b65df5SNoa Osherovich 	}
415f1b65df5SNoa Osherovich 
416f1b65df5SNoa Osherovich 	return 0;
417f1b65df5SNoa Osherovich }
418f1b65df5SNoa Osherovich 
41908e8676fSAya Levin static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed,
42008e8676fSAya Levin 					u8 *active_width)
42108e8676fSAya Levin {
42208e8676fSAya Levin 	switch (eth_proto_oper) {
42308e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
42408e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
42508e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
42608e8676fSAya Levin 		*active_speed = IB_SPEED_SDR;
42708e8676fSAya Levin 		break;
42808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
42908e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
43008e8676fSAya Levin 		*active_speed = IB_SPEED_DDR;
43108e8676fSAya Levin 		break;
43208e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
43308e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
43408e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
43508e8676fSAya Levin 		break;
43608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
43708e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
43808e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
43908e8676fSAya Levin 		break;
44008e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
44108e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
44208e8676fSAya Levin 		*active_speed = IB_SPEED_EDR;
44308e8676fSAya Levin 		break;
44408e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
445cd272875SAya Levin 		*active_width = IB_WIDTH_2X;
446cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
447cd272875SAya Levin 		break;
44808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
44908e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
45008e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
45108e8676fSAya Levin 		break;
452cd272875SAya Levin 	case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
453cd272875SAya Levin 		*active_width = IB_WIDTH_4X;
454cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
455cd272875SAya Levin 		break;
45608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
45708e8676fSAya Levin 		*active_width = IB_WIDTH_2X;
45808e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
45908e8676fSAya Levin 		break;
46008e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
46108e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
46208e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
46308e8676fSAya Levin 		break;
46408e8676fSAya Levin 	default:
46508e8676fSAya Levin 		return -EINVAL;
46608e8676fSAya Levin 	}
46708e8676fSAya Levin 
46808e8676fSAya Levin 	return 0;
46908e8676fSAya Levin }
47008e8676fSAya Levin 
47108e8676fSAya Levin static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
47208e8676fSAya Levin 				    u8 *active_width, bool ext)
47308e8676fSAya Levin {
47408e8676fSAya Levin 	return ext ?
47508e8676fSAya Levin 		translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
47608e8676fSAya Levin 					     active_width) :
47708e8676fSAya Levin 		translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
47808e8676fSAya Levin 						active_width);
47908e8676fSAya Levin }
48008e8676fSAya Levin 
481095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
4823f89a643SAchiad Shochat 				struct ib_port_attr *props)
4833f89a643SAchiad Shochat {
4843f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
485bc4e12ffSAya Levin 	u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
486da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
48788621dfeSAviv Heller 	struct net_device *ndev, *upper;
4883f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
489b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
490c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
491f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
492b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
49308e8676fSAya Levin 	bool ext;
494095b0927SIlan Tayari 	int err;
4953f89a643SAchiad Shochat 
496b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
497b3cbd6f0SDaniel Jurgens 	if (!mdev) {
498b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
499b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
500b3cbd6f0SDaniel Jurgens 		 */
501b3cbd6f0SDaniel Jurgens 		put_mdev = false;
502b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
503b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
504b3cbd6f0SDaniel Jurgens 		port_num = 1;
505b3cbd6f0SDaniel Jurgens 	}
506b3cbd6f0SDaniel Jurgens 
507f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
508f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
50926628e2dSMark Bloch 	 * Use native port in case of reps
51050f22fd8SNoa Osherovich 	 */
51126628e2dSMark Bloch 	if (dev->is_rep)
51226628e2dSMark Bloch 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
51326628e2dSMark Bloch 					   1);
51426628e2dSMark Bloch 	else
515bc4e12ffSAya Levin 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
516b3cbd6f0SDaniel Jurgens 					   mdev_port_num);
517095b0927SIlan Tayari 	if (err)
518b3cbd6f0SDaniel Jurgens 		goto out;
51908e8676fSAya Levin 	ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet);
52008e8676fSAya Levin 	eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
521f1b65df5SNoa Osherovich 
5227672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
5237672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
5247672ed33SHonggang Li 
525f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
52608e8676fSAya Levin 				 &props->active_width, ext);
5273f89a643SAchiad Shochat 
5283f89a643SAchiad Shochat 	props->port_cap_flags |= IB_PORT_CM_SUP;
5292f944c0fSJason Gunthorpe 	props->ip_gids = true;
5303f89a643SAchiad Shochat 
5313f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
5323f89a643SAchiad Shochat 						roce_address_table_size);
5333f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
5343f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
5353f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
5363f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
5373f89a643SAchiad Shochat 	props->phys_state       = 3;
5383f89a643SAchiad Shochat 
539b3cbd6f0SDaniel Jurgens 	mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
540c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
5413f89a643SAchiad Shochat 
542b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
543b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
544b3cbd6f0SDaniel Jurgens 		goto out;
545b3cbd6f0SDaniel Jurgens 
5463f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
5473f89a643SAchiad Shochat 	if (!ndev)
548b3cbd6f0SDaniel Jurgens 		goto out;
5493f89a643SAchiad Shochat 
5507c34ec19SAviv Heller 	if (dev->lag_active) {
55188621dfeSAviv Heller 		rcu_read_lock();
55288621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
55388621dfeSAviv Heller 		if (upper) {
55488621dfeSAviv Heller 			dev_put(ndev);
55588621dfeSAviv Heller 			ndev = upper;
55688621dfeSAviv Heller 			dev_hold(ndev);
55788621dfeSAviv Heller 		}
55888621dfeSAviv Heller 		rcu_read_unlock();
55988621dfeSAviv Heller 	}
56088621dfeSAviv Heller 
5613f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
5623f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
5633f89a643SAchiad Shochat 		props->phys_state = 5;
5643f89a643SAchiad Shochat 	}
5653f89a643SAchiad Shochat 
5663f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
5673f89a643SAchiad Shochat 
5683f89a643SAchiad Shochat 	dev_put(ndev);
5693f89a643SAchiad Shochat 
5703f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
571b3cbd6f0SDaniel Jurgens out:
572b3cbd6f0SDaniel Jurgens 	if (put_mdev)
573b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
574b3cbd6f0SDaniel Jurgens 	return err;
5753f89a643SAchiad Shochat }
5763f89a643SAchiad Shochat 
577095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
578095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
579095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
5803cca2606SAchiad Shochat {
581095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
582a70c0739SParav Pandit 	u16 vlan_id = 0xffff;
583095b0927SIlan Tayari 	u8 roce_version = 0;
584095b0927SIlan Tayari 	u8 roce_l3_type = 0;
585095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
586a70c0739SParav Pandit 	int ret;
5873cca2606SAchiad Shochat 
588095b0927SIlan Tayari 	if (gid) {
589095b0927SIlan Tayari 		gid_type = attr->gid_type;
590a70c0739SParav Pandit 		ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
591a70c0739SParav Pandit 		if (ret)
592a70c0739SParav Pandit 			return ret;
5933cca2606SAchiad Shochat 	}
5943cca2606SAchiad Shochat 
595095b0927SIlan Tayari 	switch (gid_type) {
5963cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
597095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
5983cca2606SAchiad Shochat 		break;
5993cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
600095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
601095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
602095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
603095b0927SIlan Tayari 		else
604095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
6053cca2606SAchiad Shochat 		break;
6063cca2606SAchiad Shochat 
6073cca2606SAchiad Shochat 	default:
608095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
6093cca2606SAchiad Shochat 	}
6103cca2606SAchiad Shochat 
611095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
612cf34e1feSParav Pandit 				      roce_l3_type, gid->raw, mac,
613a70c0739SParav Pandit 				      vlan_id < VLAN_CFI_MASK, vlan_id,
614cf34e1feSParav Pandit 				      port_num);
6153cca2606SAchiad Shochat }
6163cca2606SAchiad Shochat 
617f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
6183cca2606SAchiad Shochat 			   __always_unused void **context)
6193cca2606SAchiad Shochat {
620414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
621f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
6223cca2606SAchiad Shochat }
6233cca2606SAchiad Shochat 
624414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
625414448d2SParav Pandit 			   __always_unused void **context)
6263cca2606SAchiad Shochat {
627414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
628414448d2SParav Pandit 			     attr->index, NULL, NULL);
6293cca2606SAchiad Shochat }
6303cca2606SAchiad Shochat 
63147ec3866SParav Pandit __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
63247ec3866SParav Pandit 			       const struct ib_gid_attr *attr)
6332811ba51SAchiad Shochat {
63447ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
6352811ba51SAchiad Shochat 		return 0;
6362811ba51SAchiad Shochat 
6372811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
6382811ba51SAchiad Shochat }
6392811ba51SAchiad Shochat 
6401b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
6411b5daf11SMajd Dibbiny {
6427fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
643d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
6447fae6655SNoa Osherovich 	return 0;
6451b5daf11SMajd Dibbiny }
6461b5daf11SMajd Dibbiny 
6471b5daf11SMajd Dibbiny enum {
6481b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
6491b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
6501b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
6511b5daf11SMajd Dibbiny };
6521b5daf11SMajd Dibbiny 
6531b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
6541b5daf11SMajd Dibbiny {
6551b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
6561b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
6571b5daf11SMajd Dibbiny 
658ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
6591b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
6601b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
6611b5daf11SMajd Dibbiny 
6621b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
6631b5daf11SMajd Dibbiny }
6641b5daf11SMajd Dibbiny 
665da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
666776a3906SMoni Shoua 			    u8 atomic_size_qp,
667da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
668da7525d2SEran Ben Elisha {
669da7525d2SEran Ben Elisha 	u8 tmp;
670da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
671da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
672bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
673da7525d2SEran Ben Elisha 
674da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
675da7525d2SEran Ben Elisha 	 * of host endianness respond
676da7525d2SEran Ben Elisha 	 */
677da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
678da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
679da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
680da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
681da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
682da7525d2SEran Ben Elisha 	} else {
683da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
684da7525d2SEran Ben Elisha 	}
685da7525d2SEran Ben Elisha }
686da7525d2SEran Ben Elisha 
687776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
688776a3906SMoni Shoua 			       struct ib_device_attr *props)
689776a3906SMoni Shoua {
690776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
691776a3906SMoni Shoua 
692776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
693776a3906SMoni Shoua }
694776a3906SMoni Shoua 
695776a3906SMoni Shoua static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
696776a3906SMoni Shoua 			       struct ib_device_attr *props)
697776a3906SMoni Shoua {
698776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
699776a3906SMoni Shoua 
700776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
701776a3906SMoni Shoua }
702776a3906SMoni Shoua 
703776a3906SMoni Shoua bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
704776a3906SMoni Shoua {
705776a3906SMoni Shoua 	struct ib_device_attr props = {};
706776a3906SMoni Shoua 
707776a3906SMoni Shoua 	get_atomic_caps_dc(dev, &props);
708776a3906SMoni Shoua 	return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
709776a3906SMoni Shoua }
7101b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
7111b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
7121b5daf11SMajd Dibbiny {
7131b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7141b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7151b5daf11SMajd Dibbiny 	u64 tmp;
7161b5daf11SMajd Dibbiny 	int err;
7171b5daf11SMajd Dibbiny 
7181b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7191b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7201b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
7211b5daf11SMajd Dibbiny 							    sys_image_guid);
7221b5daf11SMajd Dibbiny 
7231b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7241b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
7253f89a643SAchiad Shochat 		break;
7263f89a643SAchiad Shochat 
7273f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7283f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
7293f89a643SAchiad Shochat 		break;
7301b5daf11SMajd Dibbiny 
7311b5daf11SMajd Dibbiny 	default:
7321b5daf11SMajd Dibbiny 		return -EINVAL;
7331b5daf11SMajd Dibbiny 	}
7343f89a643SAchiad Shochat 
7353f89a643SAchiad Shochat 	if (!err)
7363f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
7373f89a643SAchiad Shochat 
7383f89a643SAchiad Shochat 	return err;
7393f89a643SAchiad Shochat 
7401b5daf11SMajd Dibbiny }
7411b5daf11SMajd Dibbiny 
7421b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
7431b5daf11SMajd Dibbiny 				u16 *max_pkeys)
7441b5daf11SMajd Dibbiny {
7451b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7461b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7471b5daf11SMajd Dibbiny 
7481b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7491b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7501b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
7511b5daf11SMajd Dibbiny 
7521b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7531b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7541b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
7551b5daf11SMajd Dibbiny 						pkey_table_size));
7561b5daf11SMajd Dibbiny 		return 0;
7571b5daf11SMajd Dibbiny 
7581b5daf11SMajd Dibbiny 	default:
7591b5daf11SMajd Dibbiny 		return -EINVAL;
7601b5daf11SMajd Dibbiny 	}
7611b5daf11SMajd Dibbiny }
7621b5daf11SMajd Dibbiny 
7631b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
7641b5daf11SMajd Dibbiny 				u32 *vendor_id)
7651b5daf11SMajd Dibbiny {
7661b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7671b5daf11SMajd Dibbiny 
7681b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7691b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7701b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
7711b5daf11SMajd Dibbiny 
7721b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7731b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7741b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
7751b5daf11SMajd Dibbiny 
7761b5daf11SMajd Dibbiny 	default:
7771b5daf11SMajd Dibbiny 		return -EINVAL;
7781b5daf11SMajd Dibbiny 	}
7791b5daf11SMajd Dibbiny }
7801b5daf11SMajd Dibbiny 
7811b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
7821b5daf11SMajd Dibbiny 				__be64 *node_guid)
7831b5daf11SMajd Dibbiny {
7841b5daf11SMajd Dibbiny 	u64 tmp;
7851b5daf11SMajd Dibbiny 	int err;
7861b5daf11SMajd Dibbiny 
7871b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7881b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7891b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7901b5daf11SMajd Dibbiny 
7911b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7921b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7933f89a643SAchiad Shochat 		break;
7943f89a643SAchiad Shochat 
7953f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7963f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7973f89a643SAchiad Shochat 		break;
7981b5daf11SMajd Dibbiny 
7991b5daf11SMajd Dibbiny 	default:
8001b5daf11SMajd Dibbiny 		return -EINVAL;
8011b5daf11SMajd Dibbiny 	}
8023f89a643SAchiad Shochat 
8033f89a643SAchiad Shochat 	if (!err)
8043f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
8053f89a643SAchiad Shochat 
8063f89a643SAchiad Shochat 	return err;
8071b5daf11SMajd Dibbiny }
8081b5daf11SMajd Dibbiny 
8091b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
810bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
8111b5daf11SMajd Dibbiny };
8121b5daf11SMajd Dibbiny 
8131b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
8141b5daf11SMajd Dibbiny {
8151b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
8161b5daf11SMajd Dibbiny 
8171b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
8181b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
8191b5daf11SMajd Dibbiny 
8201b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
8211b5daf11SMajd Dibbiny 
8221b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
8231b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
8241b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
8251b5daf11SMajd Dibbiny }
8261b5daf11SMajd Dibbiny 
827e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
8282528e33eSMatan Barak 				struct ib_device_attr *props,
8292528e33eSMatan Barak 				struct ib_udata *uhw)
830e126ba97SEli Cohen {
831e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
832938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
833e126ba97SEli Cohen 	int err = -ENOMEM;
834288c01b7SEli Cohen 	int max_sq_desc;
835e126ba97SEli Cohen 	int max_rq_sg;
836e126ba97SEli Cohen 	int max_sq_sg;
837e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
83885c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
839402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
840402ca536SBodong Wang 	size_t resp_len;
841402ca536SBodong Wang 	u64 max_tso;
842e126ba97SEli Cohen 
843402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
844402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
845402ca536SBodong Wang 		return -EINVAL;
846402ca536SBodong Wang 	else
847402ca536SBodong Wang 		resp.response_length = resp_len;
848402ca536SBodong Wang 
849402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
8502528e33eSMatan Barak 		return -EINVAL;
8512528e33eSMatan Barak 
852e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
8531b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
8541b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
8551b5daf11SMajd Dibbiny 	if (err)
8561b5daf11SMajd Dibbiny 		return err;
8571b5daf11SMajd Dibbiny 
8581b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
8591b5daf11SMajd Dibbiny 	if (err)
8601b5daf11SMajd Dibbiny 		return err;
8611b5daf11SMajd Dibbiny 
8621b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
8631b5daf11SMajd Dibbiny 	if (err)
8641b5daf11SMajd Dibbiny 		return err;
865e126ba97SEli Cohen 
8669603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
8679603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
8689603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
869e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
870e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
871e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
8721a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
873938fe83cSSaeed Mahameed 
874938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
875e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
876938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
877e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
878938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
879e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
880938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
881e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
882d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
883d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
884d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
885d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
886b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
887b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
888d2370e0aSMatan Barak 	}
889e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
890938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
8912dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
8922dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8932dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8942dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8952dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8962dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8972dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8982dea9094SSagi Grimberg 	}
899938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
900f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
901e126ba97SEli Cohen 
90285c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
903e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
904e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
90588115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
906e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
907e8161334SNoa Osherovich 		}
908e8161334SNoa Osherovich 
909e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
910e8161334SNoa Osherovich 			props->raw_packet_caps |=
911e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
91288115fe7SBodong Wang 
913402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
914402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
915402ca536SBodong Wang 			if (max_tso) {
916402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
917402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
918402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
919402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
920402ca536SBodong Wang 			}
921402ca536SBodong Wang 		}
92231f69a82SYishai Hadas 
92331f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
92431f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
92531f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
92631f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
92731f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
92831f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
92931f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
93031f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
93131f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
93231f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
93331f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
9344e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
9354e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
9362d93fc85SMatan Barak 			if (mlx5_accel_ipsec_device_caps(dev->mdev) &
9372d93fc85SMatan Barak 			    MLX5_ACCEL_IPSEC_CAP_DEVICE)
9382d93fc85SMatan Barak 				resp.rss_caps.rx_hash_fields_mask |=
9392d93fc85SMatan Barak 					MLX5_RX_HASH_IPSEC_SPI;
94031f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
94131f69a82SYishai Hadas 		}
94231f69a82SYishai Hadas 	} else {
94331f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
94431f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
94531f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
94631f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
947402ca536SBodong Wang 	}
948402ca536SBodong Wang 
949f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
950f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
951f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
952f0313965SErez Shitrit 	}
953f0313965SErez Shitrit 
95403404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
95585c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
95685c7c014SDaniel Jurgens 	    raw_support)
95703404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
95803404e8aSMaor Gottlieb 
9591d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
9601d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
9611d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
9621d54f890SYishai Hadas 
963cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
96485c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
96585c7c014SDaniel Jurgens 	    raw_support) {
966e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
967cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
968e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
969e8161334SNoa Osherovich 	}
970cff5a0f3SMajd Dibbiny 
97124da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
97224da0016SAriel Levkovich 		props->max_dm_size =
97324da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
97424da0016SAriel Levkovich 	}
97524da0016SAriel Levkovich 
976da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
977da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
978da6d6ba3SMaor Gottlieb 
979b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
980b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
981b1383aa6SNoa Osherovich 
9821b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
9831b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
984e126ba97SEli Cohen 
985e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
986e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
987938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
988938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
989938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
990938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
991288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
992288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
993288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
994e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
99533023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
99633023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
997986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
998938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9999f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
1000938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
1001938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
1002938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
1003938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
1004938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
1005938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
1006938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
1007e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
1008e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
1009911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
1010911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
1011776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
101281bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
1013938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1014938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
1015e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1016e126ba97SEli Cohen 					   props->max_mcast_grp;
1017e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
101886695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
10197c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
10207c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
1021e126ba97SEli Cohen 
1022e502b8b0SLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
1023938fe83cSSaeed Mahameed 		if (MLX5_CAP_GEN(mdev, pg))
10248cdd312cSHaggai Eran 			props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
10258cdd312cSHaggai Eran 		props->odp_caps = dev->odp_caps;
1026e502b8b0SLeon Romanovsky 	}
10278cdd312cSHaggai Eran 
1028051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
1029051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1030051f2630SLeon Romanovsky 
1031eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
1032eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1033eff901d3SEli Cohen 
103431f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
103585c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
103631f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
103731f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
103831f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
103931f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
104031f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
104131f69a82SYishai Hadas 		props->max_wq_type_rq =
104231f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
104331f69a82SYishai Hadas 	}
104431f69a82SYishai Hadas 
1045eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
104678b1beb0SLeon Romanovsky 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
104778b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
1048eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
104978b1beb0SLeon Romanovsky 		props->tm_caps.flags = IB_TM_CAP_RC;
105078b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
1051eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
105278b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1053eb761894SArtemy Kovalyov 	}
1054eb761894SArtemy Kovalyov 
105587ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
105687ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
105787ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
105887ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
105987ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
106087ab3f52SYonatan Cohen 	}
106187ab3f52SYonatan Cohen 
10627e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
1063572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
1064572f46bfSYonatan Cohen 
1065572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
10667e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
1067572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
1068572f46bfSYonatan Cohen 					     cqe_compression_max_num);
1069572f46bfSYonatan Cohen 
10707e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
10717e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
10727e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
10736f1006a4SYonatan Cohen 
10746f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
10756f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
10766f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1077572f46bfSYonatan Cohen 		}
10787e43a2a5SBodong Wang 	}
10797e43a2a5SBodong Wang 
108085c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
108185c7c014SDaniel Jurgens 	    raw_support) {
1082d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1083d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
1084d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
1085d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1086d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
1087d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1088d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
1089d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
109061147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
109161147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
109261147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
109361147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
1094d949167dSBodong Wang 		}
1095d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1096d949167dSBodong Wang 	}
1097d949167dSBodong Wang 
10989f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
10999f885201SLeon Romanovsky 			uhw->outlen)) {
1100795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
11019f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1102795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1103050da902SBodong Wang 
1104050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1105050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1106050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1107050da902SBodong Wang 
11089f885201SLeon Romanovsky 		resp.response_length +=
11099f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
11109f885201SLeon Romanovsky 	}
11119f885201SLeon Romanovsky 
1112de57f2adSGuy Levi 	if (field_avail(typeof(resp), flags, uhw->outlen)) {
1113de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
11147a0c8f42SGuy Levi 
1115de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1116de57f2adSGuy Levi 			resp.flags |=
1117de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
11187a0c8f42SGuy Levi 
11197a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
11207a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
11217e11b911SDanit Goldberg 		if (MLX5_CAP_GEN(mdev, qp_packet_based))
11227e11b911SDanit Goldberg 			resp.flags |=
11237e11b911SDanit Goldberg 				MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
11247249c8eaSGuy Levi 
11257249c8eaSGuy Levi 		resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1126de57f2adSGuy Levi 	}
11279f885201SLeon Romanovsky 
112896dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
112996dc3fc5SNoa Osherovich 			uhw->outlen)) {
113096dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
113196dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
113296dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
113396dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
113496dc3fc5SNoa Osherovich 
113596dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
113696dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
113796dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
113896dc3fc5SNoa Osherovich 
113996dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
114096dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
114196dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
114296dc3fc5SNoa Osherovich 
114396dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
114496dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
114596dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
114696dc3fc5SNoa Osherovich 		}
114796dc3fc5SNoa Osherovich 	}
114896dc3fc5SNoa Osherovich 
114985c7c014SDaniel Jurgens 	if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
115085c7c014SDaniel Jurgens 	    raw_support) {
1151b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1152b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1153b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1154b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1155b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1156b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1157b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1158b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1159b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1160b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1161b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1162b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1163b4f34597SNoa Osherovich 		}
1164b4f34597SNoa Osherovich 	}
1165b4f34597SNoa Osherovich 
1166f95ef6cbSMaor Gottlieb 	if (field_avail(typeof(resp), tunnel_offloads_caps,
1167f95ef6cbSMaor Gottlieb 			uhw->outlen)) {
1168f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1169f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1170f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1171f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1172f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1173f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1174f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1175f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1176f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1177f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
1178e818e255SAriel Levkovich 		if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1179e818e255SAriel Levkovich 		    MLX5_FLEX_PROTO_CW_MPLS_GRE)
1180e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1181e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1182e818e255SAriel Levkovich 		if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1183e818e255SAriel Levkovich 		    MLX5_FLEX_PROTO_CW_MPLS_UDP)
1184e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1185e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1186f95ef6cbSMaor Gottlieb 	}
1187f95ef6cbSMaor Gottlieb 
1188402ca536SBodong Wang 	if (uhw->outlen) {
1189402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1190402ca536SBodong Wang 
1191402ca536SBodong Wang 		if (err)
1192402ca536SBodong Wang 			return err;
1193402ca536SBodong Wang 	}
1194402ca536SBodong Wang 
11951b5daf11SMajd Dibbiny 	return 0;
11961b5daf11SMajd Dibbiny }
1197e126ba97SEli Cohen 
11981b5daf11SMajd Dibbiny enum mlx5_ib_width {
11991b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
12001b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
12011b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
12021b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
12031b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
12041b5daf11SMajd Dibbiny };
12051b5daf11SMajd Dibbiny 
1206db7a691aSMichael Guralnik static void translate_active_width(struct ib_device *ibdev, u8 active_width,
12071b5daf11SMajd Dibbiny 				  u8 *ib_width)
12081b5daf11SMajd Dibbiny {
12091b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12101b5daf11SMajd Dibbiny 
1211db7a691aSMichael Guralnik 	if (active_width & MLX5_IB_WIDTH_1X)
12121b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1213d764970bSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_2X)
1214d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1215db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_4X)
12161b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1217db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_8X)
12181b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1219db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_12X)
12201b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1221db7a691aSMichael Guralnik 	else {
1222db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
12231b5daf11SMajd Dibbiny 			    (int)active_width);
1224db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
12251b5daf11SMajd Dibbiny 	}
12261b5daf11SMajd Dibbiny 
1227db7a691aSMichael Guralnik 	return;
12281b5daf11SMajd Dibbiny }
12291b5daf11SMajd Dibbiny 
12301b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
12311b5daf11SMajd Dibbiny {
12321b5daf11SMajd Dibbiny 	switch (mtu) {
12331b5daf11SMajd Dibbiny 	case 256: return 1;
12341b5daf11SMajd Dibbiny 	case 512: return 2;
12351b5daf11SMajd Dibbiny 	case 1024: return 3;
12361b5daf11SMajd Dibbiny 	case 2048: return 4;
12371b5daf11SMajd Dibbiny 	case 4096: return 5;
12381b5daf11SMajd Dibbiny 	default:
12391b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
12401b5daf11SMajd Dibbiny 		return -1;
12411b5daf11SMajd Dibbiny 	}
12421b5daf11SMajd Dibbiny }
12431b5daf11SMajd Dibbiny 
12441b5daf11SMajd Dibbiny enum ib_max_vl_num {
12451b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
12461b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
12471b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
12481b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
12491b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
12501b5daf11SMajd Dibbiny };
12511b5daf11SMajd Dibbiny 
12521b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
12531b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
12541b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
12551b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
12561b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
12571b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
12581b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
12591b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
12601b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
12611b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
12621b5daf11SMajd Dibbiny };
12631b5daf11SMajd Dibbiny 
12641b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
12651b5daf11SMajd Dibbiny 				u8 *max_vl_num)
12661b5daf11SMajd Dibbiny {
12671b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
12681b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
12691b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
12701b5daf11SMajd Dibbiny 		break;
12711b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
12721b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
12731b5daf11SMajd Dibbiny 		break;
12741b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
12751b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
12761b5daf11SMajd Dibbiny 		break;
12771b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
12781b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
12791b5daf11SMajd Dibbiny 		break;
12801b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
12811b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
12821b5daf11SMajd Dibbiny 		break;
12831b5daf11SMajd Dibbiny 
12841b5daf11SMajd Dibbiny 	default:
12851b5daf11SMajd Dibbiny 		return -EINVAL;
12861b5daf11SMajd Dibbiny 	}
12871b5daf11SMajd Dibbiny 
12881b5daf11SMajd Dibbiny 	return 0;
12891b5daf11SMajd Dibbiny }
12901b5daf11SMajd Dibbiny 
12911b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
12921b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
12931b5daf11SMajd Dibbiny {
12941b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12951b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
12961b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1297046339eaSSaeed Mahameed 	u16 max_mtu;
1298046339eaSSaeed Mahameed 	u16 oper_mtu;
12991b5daf11SMajd Dibbiny 	int err;
13001b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
13011b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
13021b5daf11SMajd Dibbiny 
13031b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
13041b5daf11SMajd Dibbiny 	if (!rep) {
13051b5daf11SMajd Dibbiny 		err = -ENOMEM;
13061b5daf11SMajd Dibbiny 		goto out;
13071b5daf11SMajd Dibbiny 	}
13081b5daf11SMajd Dibbiny 
1309c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
13101b5daf11SMajd Dibbiny 
13111b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
13121b5daf11SMajd Dibbiny 	if (err)
13131b5daf11SMajd Dibbiny 		goto out;
13141b5daf11SMajd Dibbiny 
13151b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
13161b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
13171b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
13181b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
13191b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
13201b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
13211b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
13221b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
13231b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
13241b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
13251b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
13261b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
13271b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
13281b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
13291b5daf11SMajd Dibbiny 
13304106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
13314106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
13324106a758SMichael Guralnik 
13331b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
13341b5daf11SMajd Dibbiny 	if (err)
13351b5daf11SMajd Dibbiny 		goto out;
13361b5daf11SMajd Dibbiny 
1337db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1338db7a691aSMichael Guralnik 
1339d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
13401b5daf11SMajd Dibbiny 	if (err)
13411b5daf11SMajd Dibbiny 		goto out;
13421b5daf11SMajd Dibbiny 
1343facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
13441b5daf11SMajd Dibbiny 
13451b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
13461b5daf11SMajd Dibbiny 
1347facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
13481b5daf11SMajd Dibbiny 
13491b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
13501b5daf11SMajd Dibbiny 
13511b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
13521b5daf11SMajd Dibbiny 	if (err)
13531b5daf11SMajd Dibbiny 		goto out;
13541b5daf11SMajd Dibbiny 
13551b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
13561b5daf11SMajd Dibbiny 				   &props->max_vl_num);
13571b5daf11SMajd Dibbiny out:
13581b5daf11SMajd Dibbiny 	kfree(rep);
1359e126ba97SEli Cohen 	return err;
1360e126ba97SEli Cohen }
1361e126ba97SEli Cohen 
1362e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1363e126ba97SEli Cohen 		       struct ib_port_attr *props)
1364e126ba97SEli Cohen {
1365095b0927SIlan Tayari 	unsigned int count;
1366095b0927SIlan Tayari 	int ret;
1367095b0927SIlan Tayari 
13681b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13691b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1370095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1371095b0927SIlan Tayari 		break;
1372e126ba97SEli Cohen 
13731b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1374095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1375095b0927SIlan Tayari 		break;
13761b5daf11SMajd Dibbiny 
13773f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1378095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1379095b0927SIlan Tayari 		break;
13803f89a643SAchiad Shochat 
13811b5daf11SMajd Dibbiny 	default:
1382095b0927SIlan Tayari 		ret = -EINVAL;
1383e126ba97SEli Cohen 	}
1384095b0927SIlan Tayari 
1385095b0927SIlan Tayari 	if (!ret && props) {
1386b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1387b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1388b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1389b3cbd6f0SDaniel Jurgens 
1390b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1391b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1392b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1393b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1394b3cbd6f0SDaniel Jurgens 			 */
1395b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1396b3cbd6f0SDaniel Jurgens 			port = 1;
1397b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1398b3cbd6f0SDaniel Jurgens 		}
1399b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1400b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1401b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1402095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1403095b0927SIlan Tayari 	}
1404095b0927SIlan Tayari 	return ret;
1405e126ba97SEli Cohen }
1406e126ba97SEli Cohen 
14078e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
14088e6efa3aSMark Bloch 				  struct ib_port_attr *props)
14098e6efa3aSMark Bloch {
14108e6efa3aSMark Bloch 	int ret;
14118e6efa3aSMark Bloch 
141226628e2dSMark Bloch 	/* Only link layer == ethernet is valid for representors
141326628e2dSMark Bloch 	 * and we always use port 1
141426628e2dSMark Bloch 	 */
14158e6efa3aSMark Bloch 	ret = mlx5_query_port_roce(ibdev, port, props);
14168e6efa3aSMark Bloch 	if (ret || !props)
14178e6efa3aSMark Bloch 		return ret;
14188e6efa3aSMark Bloch 
14198e6efa3aSMark Bloch 	/* We don't support GIDS */
14208e6efa3aSMark Bloch 	props->gid_tbl_len = 0;
14218e6efa3aSMark Bloch 
14228e6efa3aSMark Bloch 	return ret;
14238e6efa3aSMark Bloch }
14248e6efa3aSMark Bloch 
1425e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1426e126ba97SEli Cohen 			     union ib_gid *gid)
1427e126ba97SEli Cohen {
14281b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
14291b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1430e126ba97SEli Cohen 
14311b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14321b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14331b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1434e126ba97SEli Cohen 
14351b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14361b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1437e126ba97SEli Cohen 
14381b5daf11SMajd Dibbiny 	default:
14391b5daf11SMajd Dibbiny 		return -EINVAL;
14401b5daf11SMajd Dibbiny 	}
1441e126ba97SEli Cohen 
1442e126ba97SEli Cohen }
1443e126ba97SEli Cohen 
1444b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1445b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1446b3cbd6f0SDaniel Jurgens {
1447b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1448b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1449b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1450b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1451b3cbd6f0SDaniel Jurgens 	int err;
1452b3cbd6f0SDaniel Jurgens 
1453b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1454b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1455b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1456b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1457b3cbd6f0SDaniel Jurgens 		 */
1458b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1459b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1460b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1461b3cbd6f0SDaniel Jurgens 	}
1462b3cbd6f0SDaniel Jurgens 
1463b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1464b3cbd6f0SDaniel Jurgens 					index, pkey);
1465b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1466b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1467b3cbd6f0SDaniel Jurgens 
1468b3cbd6f0SDaniel Jurgens 	return err;
1469b3cbd6f0SDaniel Jurgens }
1470b3cbd6f0SDaniel Jurgens 
1471e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1472e126ba97SEli Cohen 			      u16 *pkey)
1473e126ba97SEli Cohen {
14741b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14751b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14761b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1477e126ba97SEli Cohen 
14781b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14791b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1480b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
14811b5daf11SMajd Dibbiny 	default:
14821b5daf11SMajd Dibbiny 		return -EINVAL;
1483e126ba97SEli Cohen 	}
14841b5daf11SMajd Dibbiny }
1485e126ba97SEli Cohen 
1486e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1487e126ba97SEli Cohen 				 struct ib_device_modify *props)
1488e126ba97SEli Cohen {
1489e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1490e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1491e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1492e126ba97SEli Cohen 	int err;
1493e126ba97SEli Cohen 
1494e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1495e126ba97SEli Cohen 		return -EOPNOTSUPP;
1496e126ba97SEli Cohen 
1497e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1498e126ba97SEli Cohen 		return 0;
1499e126ba97SEli Cohen 
1500e126ba97SEli Cohen 	/*
1501e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1502e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1503e126ba97SEli Cohen 	 */
1504bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
15059603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1506e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1507e126ba97SEli Cohen 	if (err)
1508e126ba97SEli Cohen 		return err;
1509e126ba97SEli Cohen 
1510bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1511e126ba97SEli Cohen 
1512e126ba97SEli Cohen 	return err;
1513e126ba97SEli Cohen }
1514e126ba97SEli Cohen 
1515cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1516cdbe33d0SEli Cohen 				u32 value)
1517cdbe33d0SEli Cohen {
1518cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1519b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1520b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1521cdbe33d0SEli Cohen 	int err;
1522cdbe33d0SEli Cohen 
1523b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1524b3cbd6f0SDaniel Jurgens 	if (!mdev)
1525b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1526b3cbd6f0SDaniel Jurgens 
1527b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1528cdbe33d0SEli Cohen 	if (err)
1529b3cbd6f0SDaniel Jurgens 		goto out;
1530cdbe33d0SEli Cohen 
1531cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1532cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1533cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1534b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1535b3cbd6f0SDaniel Jurgens 		goto out;
1536cdbe33d0SEli Cohen 	}
1537cdbe33d0SEli Cohen 
1538cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1539cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1540b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1541b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1542b3cbd6f0SDaniel Jurgens 
1543b3cbd6f0SDaniel Jurgens out:
1544b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1545cdbe33d0SEli Cohen 
1546cdbe33d0SEli Cohen 	return err;
1547cdbe33d0SEli Cohen }
1548cdbe33d0SEli Cohen 
1549e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1550e126ba97SEli Cohen 			       struct ib_port_modify *props)
1551e126ba97SEli Cohen {
1552e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1553e126ba97SEli Cohen 	struct ib_port_attr attr;
1554e126ba97SEli Cohen 	u32 tmp;
1555e126ba97SEli Cohen 	int err;
1556cdbe33d0SEli Cohen 	u32 change_mask;
1557cdbe33d0SEli Cohen 	u32 value;
1558cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1559cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1560cdbe33d0SEli Cohen 
1561ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1562ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1563ec255879SMajd Dibbiny 	 */
1564ec255879SMajd Dibbiny 	if (!is_ib)
1565ec255879SMajd Dibbiny 		return 0;
1566ec255879SMajd Dibbiny 
1567cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1568cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1569cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1570cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1571cdbe33d0SEli Cohen 	}
1572e126ba97SEli Cohen 
1573e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1574e126ba97SEli Cohen 
1575c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1576e126ba97SEli Cohen 	if (err)
1577e126ba97SEli Cohen 		goto out;
1578e126ba97SEli Cohen 
1579e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1580e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1581e126ba97SEli Cohen 
15829603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1583e126ba97SEli Cohen 
1584e126ba97SEli Cohen out:
1585e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1586e126ba97SEli Cohen 	return err;
1587e126ba97SEli Cohen }
1588e126ba97SEli Cohen 
158930aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
159030aa60b3SEli Cohen {
159130aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
159230aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
159330aa60b3SEli Cohen }
159430aa60b3SEli Cohen 
159531a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
159631a78a5aSYishai Hadas {
159731a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
159831a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
159931a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
160031a78a5aSYishai Hadas 
160131a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
160231a78a5aSYishai Hadas }
160331a78a5aSYishai Hadas 
1604b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1605b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
160631a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1607b037c29aSEli Cohen {
1608b037c29aSEli Cohen 	int uars_per_sys_page;
1609b037c29aSEli Cohen 	int bfregs_per_sys_page;
1610b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1611b037c29aSEli Cohen 
1612b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1613b037c29aSEli Cohen 		return -EINVAL;
1614b037c29aSEli Cohen 
1615b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1616b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1617b037c29aSEli Cohen 
1618b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1619b037c29aSEli Cohen 		return -ENOMEM;
1620b037c29aSEli Cohen 
1621b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1622b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
162331a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1624b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1625b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1626b037c29aSEli Cohen 		return -EINVAL;
1627b037c29aSEli Cohen 
162831a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
162931a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
163031a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
163131a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
163231a78a5aSYishai Hadas 
163331a78a5aSYishai Hadas 	mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, allocated %d, total bfregs %d, using %d sys pages\n",
1634b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1635b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
163631a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
163731a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1638b037c29aSEli Cohen 
1639b037c29aSEli Cohen 	return 0;
1640b037c29aSEli Cohen }
1641b037c29aSEli Cohen 
1642b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1643b037c29aSEli Cohen {
1644b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1645b037c29aSEli Cohen 	int err;
1646b037c29aSEli Cohen 	int i;
1647b037c29aSEli Cohen 
1648b037c29aSEli Cohen 	bfregi = &context->bfregi;
164931a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1650b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1651b037c29aSEli Cohen 		if (err)
1652b037c29aSEli Cohen 			goto error;
1653b037c29aSEli Cohen 
1654b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1655b037c29aSEli Cohen 	}
16564ed131d0SYishai Hadas 
16574ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
16584ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
16594ed131d0SYishai Hadas 
1660b037c29aSEli Cohen 	return 0;
1661b037c29aSEli Cohen 
1662b037c29aSEli Cohen error:
1663b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1664b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1665b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1666b037c29aSEli Cohen 
1667b037c29aSEli Cohen 	return err;
1668b037c29aSEli Cohen }
1669b037c29aSEli Cohen 
167015177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
167115177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1672b037c29aSEli Cohen {
1673b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1674b037c29aSEli Cohen 	int i;
1675b037c29aSEli Cohen 
1676b037c29aSEli Cohen 	bfregi = &context->bfregi;
167715177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
16784ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
167915177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
168015177999SLeon Romanovsky 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1681b037c29aSEli Cohen }
1682b037c29aSEli Cohen 
16830042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1684a560f1d9SMark Bloch {
1685a560f1d9SMark Bloch 	int err = 0;
1686a560f1d9SMark Bloch 
1687a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16880042f9e4SMark Bloch 	if (td)
1689a560f1d9SMark Bloch 		dev->lb.user_td++;
16900042f9e4SMark Bloch 	if (qp)
16910042f9e4SMark Bloch 		dev->lb.qps++;
1692a560f1d9SMark Bloch 
16930042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
16940042f9e4SMark Bloch 	    dev->lb.qps == 1) {
16950042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1696a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
16970042f9e4SMark Bloch 			dev->lb.enabled = true;
16980042f9e4SMark Bloch 		}
16990042f9e4SMark Bloch 	}
1700a560f1d9SMark Bloch 
1701a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1702a560f1d9SMark Bloch 
1703a560f1d9SMark Bloch 	return err;
1704a560f1d9SMark Bloch }
1705a560f1d9SMark Bloch 
17060042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1707a560f1d9SMark Bloch {
1708a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
17090042f9e4SMark Bloch 	if (td)
1710a560f1d9SMark Bloch 		dev->lb.user_td--;
17110042f9e4SMark Bloch 	if (qp)
17120042f9e4SMark Bloch 		dev->lb.qps--;
1713a560f1d9SMark Bloch 
17140042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
17150042f9e4SMark Bloch 	    dev->lb.qps == 0) {
17160042f9e4SMark Bloch 		if (dev->lb.enabled) {
1717a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
17180042f9e4SMark Bloch 			dev->lb.enabled = false;
17190042f9e4SMark Bloch 		}
17200042f9e4SMark Bloch 	}
1721a560f1d9SMark Bloch 
1722a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1723a560f1d9SMark Bloch }
1724a560f1d9SMark Bloch 
1725d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1726d2d19121SYishai Hadas 					  u16 uid)
1727c85023e1SHuy Nguyen {
1728c85023e1SHuy Nguyen 	int err;
1729c85023e1SHuy Nguyen 
1730cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1731cfdeb893SLeon Romanovsky 		return 0;
1732cfdeb893SLeon Romanovsky 
1733d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1734c85023e1SHuy Nguyen 	if (err)
1735c85023e1SHuy Nguyen 		return err;
1736c85023e1SHuy Nguyen 
1737c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17388978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17398978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1740c85023e1SHuy Nguyen 		return err;
1741c85023e1SHuy Nguyen 
17420042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1743c85023e1SHuy Nguyen }
1744c85023e1SHuy Nguyen 
1745d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1746d2d19121SYishai Hadas 					     u16 uid)
1747c85023e1SHuy Nguyen {
1748cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1749cfdeb893SLeon Romanovsky 		return;
1750cfdeb893SLeon Romanovsky 
1751d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1752c85023e1SHuy Nguyen 
1753c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17548978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17558978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1756c85023e1SHuy Nguyen 		return;
1757c85023e1SHuy Nguyen 
17580042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1759c85023e1SHuy Nguyen }
1760c85023e1SHuy Nguyen 
1761a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1762e126ba97SEli Cohen 				  struct ib_udata *udata)
1763e126ba97SEli Cohen {
1764a2a074efSLeon Romanovsky 	struct ib_device *ibdev = uctx->device;
1765e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1766b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1767b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
17685c99eaecSFeras Daoud 	struct mlx5_core_dev *mdev = dev->mdev;
1769a2a074efSLeon Romanovsky 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
17702f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
177178c0f98cSEli Cohen 	int ver;
1772e126ba97SEli Cohen 	int err;
1773a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1774a168a41cSMajd Dibbiny 				     max_cqe_version);
177525bb36e7SYonatan Cohen 	u32 dump_fill_mkey;
1776b037c29aSEli Cohen 	bool lib_uar_4k;
1777e126ba97SEli Cohen 
1778e126ba97SEli Cohen 	if (!dev->ib_active)
1779a2a074efSLeon Romanovsky 		return -EAGAIN;
1780e126ba97SEli Cohen 
1781e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
178278c0f98cSEli Cohen 		ver = 0;
1783e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
178478c0f98cSEli Cohen 		ver = 2;
178578c0f98cSEli Cohen 	else
1786a2a074efSLeon Romanovsky 		return -EINVAL;
178778c0f98cSEli Cohen 
1788e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1789e126ba97SEli Cohen 	if (err)
1790a2a074efSLeon Romanovsky 		return err;
1791e126ba97SEli Cohen 
1792a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1793a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
179478c0f98cSEli Cohen 
1795f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1796a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
1797b368d7cbSMatan Barak 
17982f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
17992f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
18002f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1801a2a074efSLeon Romanovsky 		return -EINVAL;
1802e126ba97SEli Cohen 
1803938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
18042cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1805938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1806b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1807938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1808938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1809938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1810938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1811938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1812f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1813f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1814f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
181530aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
181630aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
181730aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
181830aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1819b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1820b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1821e126ba97SEli Cohen 
1822c03faa56SMatan Barak 	if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1823c03faa56SMatan Barak 		if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1824c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1825c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1826c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1827c03faa56SMatan Barak 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1828c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1829c03faa56SMatan Barak 		if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1830c03faa56SMatan Barak 			resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1831c03faa56SMatan Barak 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1832c03faa56SMatan Barak 	}
1833c03faa56SMatan Barak 
183430aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
18352f5ff264SEli Cohen 	bfregi = &context->bfregi;
1836b037c29aSEli Cohen 
1837b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
183831a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1839b037c29aSEli Cohen 	if (err)
1840b037c29aSEli Cohen 		goto out_ctx;
1841b037c29aSEli Cohen 
18422f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1843b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
184431a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1845b037c29aSEli Cohen 				GFP_KERNEL);
1846b037c29aSEli Cohen 	if (!bfregi->count) {
1847e126ba97SEli Cohen 		err = -ENOMEM;
1848e126ba97SEli Cohen 		goto out_ctx;
1849e126ba97SEli Cohen 	}
1850e126ba97SEli Cohen 
1851b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1852b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1853e126ba97SEli Cohen 				    GFP_KERNEL);
1854b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1855e126ba97SEli Cohen 		err = -ENOMEM;
1856e126ba97SEli Cohen 		goto out_count;
1857e126ba97SEli Cohen 	}
1858e126ba97SEli Cohen 
1859b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1860b037c29aSEli Cohen 	if (err)
1861b037c29aSEli Cohen 		goto out_sys_pages;
1862b037c29aSEli Cohen 
186313859d5dSLeon Romanovsky 	if (ibdev->attrs.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING)
186413859d5dSLeon Romanovsky 		context->ibucontext.invalidate_range =
186513859d5dSLeon Romanovsky 			&mlx5_ib_invalidate_range;
1866b4cfe447SHaggai Eran 
1867a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1868fb98153bSYishai Hadas 		err = mlx5_ib_devx_create(dev, true);
186976dc5a84SYishai Hadas 		if (err < 0)
1870d2d19121SYishai Hadas 			goto out_uars;
187176dc5a84SYishai Hadas 		context->devx_uid = err;
1872a8b92ca1SYishai Hadas 	}
1873a8b92ca1SYishai Hadas 
1874d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1875d2d19121SYishai Hadas 					     context->devx_uid);
1876a8b92ca1SYishai Hadas 	if (err)
1877d2d19121SYishai Hadas 		goto out_devx;
1878a8b92ca1SYishai Hadas 
187925bb36e7SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
188025bb36e7SYonatan Cohen 		err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
188125bb36e7SYonatan Cohen 		if (err)
18828193abb6SJason Gunthorpe 			goto out_mdev;
188325bb36e7SYonatan Cohen 	}
188425bb36e7SYonatan Cohen 
1885e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1886e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1887e126ba97SEli Cohen 
18882f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1889508562d6SDaniel Jurgens 	resp.num_ports = dev->num_ports;
1890b368d7cbSMatan Barak 
1891f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1892f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1893b368d7cbSMatan Barak 
1894402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
18956ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
18966ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1897402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1898402ca536SBodong Wang 	}
1899402ca536SBodong Wang 
190078984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
190178984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
190278984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
190378984898SOr Gerlitz 			resp.eth_min_inline++;
190478984898SOr Gerlitz 		}
190578984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
190678984898SOr Gerlitz 	}
190778984898SOr Gerlitz 
19085c99eaecSFeras Daoud 	if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
19095c99eaecSFeras Daoud 		if (mdev->clock_info)
19105c99eaecSFeras Daoud 			resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
19115c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.clock_info_versions);
19125c99eaecSFeras Daoud 	}
19135c99eaecSFeras Daoud 
1914bc5c6eedSNoa Osherovich 	/*
1915bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1916bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1917bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1918bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1919bc5c6eedSNoa Osherovich 	 */
1920de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1921de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1922b368d7cbSMatan Barak 			resp.comp_mask |=
1923b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1924b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1925de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1926de8d6e02SEli Cohen 		}
19275c99eaecSFeras Daoud 		resp.response_length += sizeof(resp.hca_core_clock_offset);
1928b368d7cbSMatan Barak 	}
1929b368d7cbSMatan Barak 
193030aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
193130aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
193230aa60b3SEli Cohen 
193330aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
193430aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
193530aa60b3SEli Cohen 
193631a78a5aSYishai Hadas 	if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
193731a78a5aSYishai Hadas 		resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
193831a78a5aSYishai Hadas 		resp.response_length += sizeof(resp.num_dyn_bfregs);
193931a78a5aSYishai Hadas 	}
194031a78a5aSYishai Hadas 
194125bb36e7SYonatan Cohen 	if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
194225bb36e7SYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
194325bb36e7SYonatan Cohen 			resp.dump_fill_mkey = dump_fill_mkey;
194425bb36e7SYonatan Cohen 			resp.comp_mask |=
194525bb36e7SYonatan Cohen 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
194625bb36e7SYonatan Cohen 		}
194725bb36e7SYonatan Cohen 		resp.response_length += sizeof(resp.dump_fill_mkey);
194825bb36e7SYonatan Cohen 	}
194925bb36e7SYonatan Cohen 
1950b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1951e126ba97SEli Cohen 	if (err)
1952a8b92ca1SYishai Hadas 		goto out_mdev;
1953e126ba97SEli Cohen 
19542f5ff264SEli Cohen 	bfregi->ver = ver;
19552f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1956f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
195730aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
195830aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1959f72300c5SHaggai Abramovsky 
19607c34ec19SAviv Heller 	if (dev->lag_active) {
196195579e78SMark Bloch 		u8 port = mlx5_core_native_port_num(dev->mdev) - 1;
1962c6a21c38SMajd Dibbiny 
1963c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1964c6a21c38SMajd Dibbiny 			   atomic_add_return(
196595579e78SMark Bloch 				   1, &dev->port[port].roce.tx_port_affinity));
1966c6a21c38SMajd Dibbiny 	}
1967c6a21c38SMajd Dibbiny 
1968a2a074efSLeon Romanovsky 	return 0;
1969e126ba97SEli Cohen 
1970a8b92ca1SYishai Hadas out_mdev:
1971d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1972d2d19121SYishai Hadas out_devx:
1973a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
197476dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1975146d2f1aSmajd@mellanox.com 
1976e126ba97SEli Cohen out_uars:
1977b037c29aSEli Cohen 	deallocate_uars(dev, context);
1978b037c29aSEli Cohen 
1979b037c29aSEli Cohen out_sys_pages:
1980b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1981b037c29aSEli Cohen 
1982e126ba97SEli Cohen out_count:
19832f5ff264SEli Cohen 	kfree(bfregi->count);
1984e126ba97SEli Cohen 
1985e126ba97SEli Cohen out_ctx:
1986a2a074efSLeon Romanovsky 	return err;
1987e126ba97SEli Cohen }
1988e126ba97SEli Cohen 
1989a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1990e126ba97SEli Cohen {
1991e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1992e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1993b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1994e126ba97SEli Cohen 
1995f27a0d50SJason Gunthorpe 	/* All umem's must be destroyed before destroying the ucontext. */
1996f27a0d50SJason Gunthorpe 	mutex_lock(&ibcontext->per_mm_list_lock);
1997f27a0d50SJason Gunthorpe 	WARN_ON(!list_empty(&ibcontext->per_mm_list));
1998f27a0d50SJason Gunthorpe 	mutex_unlock(&ibcontext->per_mm_list_lock);
1999a8b92ca1SYishai Hadas 
2000b037c29aSEli Cohen 	bfregi = &context->bfregi;
2001d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2002d2d19121SYishai Hadas 
2003b037c29aSEli Cohen 	if (context->devx_uid)
200476dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
2005e126ba97SEli Cohen 
2006e126ba97SEli Cohen 	deallocate_uars(dev, context);
20072f5ff264SEli Cohen 	kfree(bfregi->sys_pages);
20082f5ff264SEli Cohen 	kfree(bfregi->count);
2009e126ba97SEli Cohen }
2010e126ba97SEli Cohen 
2011e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2012e126ba97SEli Cohen 				 int uar_idx)
2013e126ba97SEli Cohen {
2014b037c29aSEli Cohen 	int fw_uars_per_page;
2015b037c29aSEli Cohen 
2016b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2017b037c29aSEli Cohen 
2018aa8106f1SHuy Nguyen 	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2019e126ba97SEli Cohen }
2020e126ba97SEli Cohen 
2021e126ba97SEli Cohen static int get_command(unsigned long offset)
2022e126ba97SEli Cohen {
2023e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2024e126ba97SEli Cohen }
2025e126ba97SEli Cohen 
2026e126ba97SEli Cohen static int get_arg(unsigned long offset)
2027e126ba97SEli Cohen {
2028e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2029e126ba97SEli Cohen }
2030e126ba97SEli Cohen 
2031e126ba97SEli Cohen static int get_index(unsigned long offset)
2032e126ba97SEli Cohen {
2033e126ba97SEli Cohen 	return get_arg(offset);
2034e126ba97SEli Cohen }
2035e126ba97SEli Cohen 
20364ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
20374ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20384ed131d0SYishai Hadas {
20394ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20404ed131d0SYishai Hadas }
20414ed131d0SYishai Hadas 
20427c2344c3SMaor Gottlieb 
20437c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20447c2344c3SMaor Gottlieb {
20457c2344c3SMaor Gottlieb }
20467c2344c3SMaor Gottlieb 
204737aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2048e126ba97SEli Cohen {
204937aa5c36SGuy Levi 	switch (cmd) {
205037aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
205137aa5c36SGuy Levi 		return "WC";
2052e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
205337aa5c36SGuy Levi 		return "best effort WC";
205437aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
205537aa5c36SGuy Levi 		return "NC";
205624da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
205724da0016SAriel Levkovich 		return "Device Memory";
205837aa5c36SGuy Levi 	default:
205937aa5c36SGuy Levi 		return NULL;
206037aa5c36SGuy Levi 	}
206137aa5c36SGuy Levi }
206237aa5c36SGuy Levi 
20635c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20645c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20655c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20665c99eaecSFeras Daoud {
20674eb6ab13SJason Gunthorpe 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
20684eb6ab13SJason Gunthorpe 	    !(vma->vm_flags & VM_SHARED))
20695c99eaecSFeras Daoud 		return -EINVAL;
20705c99eaecSFeras Daoud 
20715c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20725c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20735c99eaecSFeras Daoud 
20744eb6ab13SJason Gunthorpe 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
20755c99eaecSFeras Daoud 		return -EPERM;
2076c660133cSJason Gunthorpe 	vma->vm_flags &= ~VM_MAYWRITE;
20775c99eaecSFeras Daoud 
2078ddcdc368SJason Gunthorpe 	if (!dev->mdev->clock_info)
20795c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20805c99eaecSFeras Daoud 
20814eb6ab13SJason Gunthorpe 	return vm_insert_page(vma, vma->vm_start,
20824eb6ab13SJason Gunthorpe 			      virt_to_page(dev->mdev->clock_info));
20835c99eaecSFeras Daoud }
20845c99eaecSFeras Daoud 
208537aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
20867c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
20877c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
208837aa5c36SGuy Levi {
20892f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
209037aa5c36SGuy Levi 	int err;
209137aa5c36SGuy Levi 	unsigned long idx;
2092aa09ea6eSKamal Heib 	phys_addr_t pfn;
209337aa5c36SGuy Levi 	pgprot_t prot;
20944ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
20954ed131d0SYishai Hadas 	u32 uar_index;
20964ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
20974ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
20984ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2099b037c29aSEli Cohen 
2100b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2101b037c29aSEli Cohen 		return -EINVAL;
2102b037c29aSEli Cohen 
21034ed131d0SYishai Hadas 	if (dyn_uar)
21044ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
21054ed131d0SYishai Hadas 	else
2106b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
21074ed131d0SYishai Hadas 
21084ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21094ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21104ed131d0SYishai Hadas 			     idx, max_valid_idx);
2111b037c29aSEli Cohen 		return -EINVAL;
2112b037c29aSEli Cohen 	}
211337aa5c36SGuy Levi 
211437aa5c36SGuy Levi 	switch (cmd) {
211537aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21164ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
211737aa5c36SGuy Levi /* Some architectures don't support WC memory */
211837aa5c36SGuy Levi #if defined(CONFIG_X86)
211937aa5c36SGuy Levi 		if (!pat_enabled())
212037aa5c36SGuy Levi 			return -EPERM;
212137aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
212237aa5c36SGuy Levi 			return -EPERM;
212337aa5c36SGuy Levi #endif
212437aa5c36SGuy Levi 	/* fall through */
212537aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
212637aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
212737aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
212837aa5c36SGuy Levi 		break;
212937aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
213037aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
213137aa5c36SGuy Levi 		break;
213237aa5c36SGuy Levi 	default:
213337aa5c36SGuy Levi 		return -EINVAL;
213437aa5c36SGuy Levi 	}
213537aa5c36SGuy Levi 
21364ed131d0SYishai Hadas 	if (dyn_uar) {
21374ed131d0SYishai Hadas 		int uars_per_page;
21384ed131d0SYishai Hadas 
21394ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21404ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21414ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21424ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21434ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21444ed131d0SYishai Hadas 			return -EINVAL;
21454ed131d0SYishai Hadas 		}
21464ed131d0SYishai Hadas 
21474ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21484ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21494ed131d0SYishai Hadas 		 * page holds its count.
21504ed131d0SYishai Hadas 		 */
21514ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
21524ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
21534ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
21544ed131d0SYishai Hadas 			return -EINVAL;
21554ed131d0SYishai Hadas 		}
21564ed131d0SYishai Hadas 
21574ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
21584ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
21594ed131d0SYishai Hadas 
21604ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
21614ed131d0SYishai Hadas 		if (err) {
21624ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
21634ed131d0SYishai Hadas 			goto free_bfreg;
21644ed131d0SYishai Hadas 		}
21654ed131d0SYishai Hadas 	} else {
21664ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
21674ed131d0SYishai Hadas 	}
21684ed131d0SYishai Hadas 
21694ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
217037aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2171e126ba97SEli Cohen 
2172e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2173e2cd1d1aSJason Gunthorpe 				prot);
217437aa5c36SGuy Levi 	if (err) {
21758f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2176e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
21778f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
21784ed131d0SYishai Hadas 		goto err;
217937aa5c36SGuy Levi 	}
2180e126ba97SEli Cohen 
21814ed131d0SYishai Hadas 	if (dyn_uar)
21824ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
21834ed131d0SYishai Hadas 	return 0;
21844ed131d0SYishai Hadas 
21854ed131d0SYishai Hadas err:
21864ed131d0SYishai Hadas 	if (!dyn_uar)
21874ed131d0SYishai Hadas 		return err;
21884ed131d0SYishai Hadas 
21894ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
21904ed131d0SYishai Hadas 
21914ed131d0SYishai Hadas free_bfreg:
21924ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
21934ed131d0SYishai Hadas 
21944ed131d0SYishai Hadas 	return err;
219537aa5c36SGuy Levi }
219637aa5c36SGuy Levi 
219724da0016SAriel Levkovich static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
219824da0016SAriel Levkovich {
219924da0016SAriel Levkovich 	struct mlx5_ib_ucontext *mctx = to_mucontext(context);
220024da0016SAriel Levkovich 	struct mlx5_ib_dev *dev = to_mdev(context->device);
220124da0016SAriel Levkovich 	u16 page_idx = get_extended_index(vma->vm_pgoff);
220224da0016SAriel Levkovich 	size_t map_size = vma->vm_end - vma->vm_start;
220324da0016SAriel Levkovich 	u32 npages = map_size >> PAGE_SHIFT;
220424da0016SAriel Levkovich 	phys_addr_t pfn;
220524da0016SAriel Levkovich 
220624da0016SAriel Levkovich 	if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
220724da0016SAriel Levkovich 	    page_idx + npages)
220824da0016SAriel Levkovich 		return -EINVAL;
220924da0016SAriel Levkovich 
2210aa8106f1SHuy Nguyen 	pfn = ((dev->mdev->bar_addr +
221124da0016SAriel Levkovich 	      MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
221224da0016SAriel Levkovich 	      PAGE_SHIFT) +
221324da0016SAriel Levkovich 	      page_idx;
2214e2cd1d1aSJason Gunthorpe 	return rdma_user_mmap_io(context, vma, pfn, map_size,
2215e2cd1d1aSJason Gunthorpe 				 pgprot_writecombine(vma->vm_page_prot));
221624da0016SAriel Levkovich }
221724da0016SAriel Levkovich 
221837aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
221937aa5c36SGuy Levi {
222037aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
222137aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
222237aa5c36SGuy Levi 	unsigned long command;
222337aa5c36SGuy Levi 	phys_addr_t pfn;
222437aa5c36SGuy Levi 
222537aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
222637aa5c36SGuy Levi 	switch (command) {
222737aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
222837aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
222937aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
22304ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
22317c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2232e126ba97SEli Cohen 
2233e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2234e126ba97SEli Cohen 		return -ENOSYS;
2235e126ba97SEli Cohen 
2236d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2237d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2238d69e3bcfSMatan Barak 			return -EINVAL;
2239d69e3bcfSMatan Barak 
22406cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2241d69e3bcfSMatan Barak 			return -EPERM;
2242c660133cSJason Gunthorpe 		vma->vm_flags &= ~VM_MAYWRITE;
2243d69e3bcfSMatan Barak 
2244d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2245d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2246d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2247d69e3bcfSMatan Barak 
2248d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2249d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2250d69e3bcfSMatan Barak 			PAGE_SHIFT;
2251d5e560d3SJason Gunthorpe 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2252d5e560d3SJason Gunthorpe 					 PAGE_SIZE,
2253d5e560d3SJason Gunthorpe 					 pgprot_noncached(vma->vm_page_prot));
22545c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
22555c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2256d69e3bcfSMatan Barak 
225724da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
225824da0016SAriel Levkovich 		return dm_mmap(ibcontext, vma);
225924da0016SAriel Levkovich 
2260e126ba97SEli Cohen 	default:
2261e126ba97SEli Cohen 		return -EINVAL;
2262e126ba97SEli Cohen 	}
2263e126ba97SEli Cohen 
2264e126ba97SEli Cohen 	return 0;
2265e126ba97SEli Cohen }
2266e126ba97SEli Cohen 
226725c13324SAriel Levkovich static inline int check_dm_type_support(struct mlx5_ib_dev *dev,
226825c13324SAriel Levkovich 					u32 type)
226925c13324SAriel Levkovich {
227025c13324SAriel Levkovich 	switch (type) {
227125c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
227225c13324SAriel Levkovich 		if (!MLX5_CAP_DEV_MEM(dev->mdev, memic))
227325c13324SAriel Levkovich 			return -EOPNOTSUPP;
227425c13324SAriel Levkovich 		break;
227525c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
227625c13324SAriel Levkovich 		if (!capable(CAP_SYS_RAWIO) ||
227725c13324SAriel Levkovich 		    !capable(CAP_NET_RAW))
227825c13324SAriel Levkovich 			return -EPERM;
227925c13324SAriel Levkovich 
228025c13324SAriel Levkovich 		if (!(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner) ||
228125c13324SAriel Levkovich 		      MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner)))
228225c13324SAriel Levkovich 			return -EOPNOTSUPP;
228325c13324SAriel Levkovich 		break;
228425c13324SAriel Levkovich 	}
228525c13324SAriel Levkovich 
228625c13324SAriel Levkovich 	return 0;
228725c13324SAriel Levkovich }
228825c13324SAriel Levkovich 
22893b113a1eSAriel Levkovich static int handle_alloc_dm_memic(struct ib_ucontext *ctx,
22903b113a1eSAriel Levkovich 				 struct mlx5_ib_dm *dm,
229124da0016SAriel Levkovich 				 struct ib_dm_alloc_attr *attr,
229224da0016SAriel Levkovich 				 struct uverbs_attr_bundle *attrs)
229324da0016SAriel Levkovich {
22943b113a1eSAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
229524da0016SAriel Levkovich 	u64 start_offset;
229624da0016SAriel Levkovich 	u32 page_idx;
229724da0016SAriel Levkovich 	int err;
229824da0016SAriel Levkovich 
22993b113a1eSAriel Levkovich 	dm->size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
230024da0016SAriel Levkovich 
23013b113a1eSAriel Levkovich 	err = mlx5_cmd_alloc_memic(dm_db, &dm->dev_addr,
23023b113a1eSAriel Levkovich 				   dm->size, attr->alignment);
230324da0016SAriel Levkovich 	if (err)
23043b113a1eSAriel Levkovich 		return err;
230524da0016SAriel Levkovich 
23063b113a1eSAriel Levkovich 	page_idx = (dm->dev_addr - pci_resource_start(dm_db->dev->pdev, 0) -
23073b113a1eSAriel Levkovich 		    MLX5_CAP64_DEV_MEM(dm_db->dev, memic_bar_start_addr)) >>
230824da0016SAriel Levkovich 		    PAGE_SHIFT;
230924da0016SAriel Levkovich 
231024da0016SAriel Levkovich 	err = uverbs_copy_to(attrs,
231124da0016SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
231224da0016SAriel Levkovich 			     &page_idx, sizeof(page_idx));
231324da0016SAriel Levkovich 	if (err)
231424da0016SAriel Levkovich 		goto err_dealloc;
231524da0016SAriel Levkovich 
23163b113a1eSAriel Levkovich 	start_offset = dm->dev_addr & ~PAGE_MASK;
23173b113a1eSAriel Levkovich 	err = uverbs_copy_to(attrs,
23183b113a1eSAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
23193b113a1eSAriel Levkovich 			     &start_offset, sizeof(start_offset));
23203b113a1eSAriel Levkovich 	if (err)
23213b113a1eSAriel Levkovich 		goto err_dealloc;
232224da0016SAriel Levkovich 
23233b113a1eSAriel Levkovich 	bitmap_set(to_mucontext(ctx)->dm_pages, page_idx,
23243b113a1eSAriel Levkovich 		   DIV_ROUND_UP(dm->size, PAGE_SIZE));
23253b113a1eSAriel Levkovich 
23263b113a1eSAriel Levkovich 	return 0;
23273b113a1eSAriel Levkovich 
23283b113a1eSAriel Levkovich err_dealloc:
23293b113a1eSAriel Levkovich 	mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
23303b113a1eSAriel Levkovich 
23313b113a1eSAriel Levkovich 	return err;
23323b113a1eSAriel Levkovich }
23333b113a1eSAriel Levkovich 
233425c13324SAriel Levkovich static int handle_alloc_dm_sw_icm(struct ib_ucontext *ctx,
233525c13324SAriel Levkovich 				  struct mlx5_ib_dm *dm,
233625c13324SAriel Levkovich 				  struct ib_dm_alloc_attr *attr,
233725c13324SAriel Levkovich 				  struct uverbs_attr_bundle *attrs,
233825c13324SAriel Levkovich 				  int type)
233925c13324SAriel Levkovich {
234025c13324SAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
234125c13324SAriel Levkovich 	u64 act_size;
234225c13324SAriel Levkovich 	int err;
234325c13324SAriel Levkovich 
234425c13324SAriel Levkovich 	/* Allocation size must a multiple of the basic block size
234525c13324SAriel Levkovich 	 * and a power of 2.
234625c13324SAriel Levkovich 	 */
234725c13324SAriel Levkovich 	act_size = roundup(attr->length, MLX5_SW_ICM_BLOCK_SIZE(dm_db->dev));
234825c13324SAriel Levkovich 	act_size = roundup_pow_of_two(act_size);
234925c13324SAriel Levkovich 
235025c13324SAriel Levkovich 	dm->size = act_size;
235125c13324SAriel Levkovich 	err = mlx5_cmd_alloc_sw_icm(dm_db, type, act_size,
235225c13324SAriel Levkovich 				    to_mucontext(ctx)->devx_uid, &dm->dev_addr,
235325c13324SAriel Levkovich 				    &dm->icm_dm.obj_id);
235425c13324SAriel Levkovich 	if (err)
235525c13324SAriel Levkovich 		return err;
235625c13324SAriel Levkovich 
235725c13324SAriel Levkovich 	err = uverbs_copy_to(attrs,
235825c13324SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
235925c13324SAriel Levkovich 			     &dm->dev_addr, sizeof(dm->dev_addr));
236025c13324SAriel Levkovich 	if (err)
236125c13324SAriel Levkovich 		mlx5_cmd_dealloc_sw_icm(dm_db, type, dm->size,
236225c13324SAriel Levkovich 					to_mucontext(ctx)->devx_uid,
236325c13324SAriel Levkovich 					dm->dev_addr, dm->icm_dm.obj_id);
236425c13324SAriel Levkovich 
236525c13324SAriel Levkovich 	return err;
236625c13324SAriel Levkovich }
236725c13324SAriel Levkovich 
23683b113a1eSAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
23693b113a1eSAriel Levkovich 			       struct ib_ucontext *context,
23703b113a1eSAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
23713b113a1eSAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
23723b113a1eSAriel Levkovich {
23733b113a1eSAriel Levkovich 	struct mlx5_ib_dm *dm;
23743b113a1eSAriel Levkovich 	enum mlx5_ib_uapi_dm_type type;
23753b113a1eSAriel Levkovich 	int err;
23763b113a1eSAriel Levkovich 
23773b113a1eSAriel Levkovich 	err = uverbs_get_const_default(&type, attrs,
23783b113a1eSAriel Levkovich 				       MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
23793b113a1eSAriel Levkovich 				       MLX5_IB_UAPI_DM_TYPE_MEMIC);
23803b113a1eSAriel Levkovich 	if (err)
23813b113a1eSAriel Levkovich 		return ERR_PTR(err);
23823b113a1eSAriel Levkovich 
23833b113a1eSAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_dm req: dm_type=%d user_length=0x%llx log_alignment=%d\n",
23843b113a1eSAriel Levkovich 		    type, attr->length, attr->alignment);
23853b113a1eSAriel Levkovich 
238625c13324SAriel Levkovich 	err = check_dm_type_support(to_mdev(ibdev), type);
238725c13324SAriel Levkovich 	if (err)
238825c13324SAriel Levkovich 		return ERR_PTR(err);
238925c13324SAriel Levkovich 
23903b113a1eSAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
23913b113a1eSAriel Levkovich 	if (!dm)
23923b113a1eSAriel Levkovich 		return ERR_PTR(-ENOMEM);
23933b113a1eSAriel Levkovich 
23943b113a1eSAriel Levkovich 	dm->type = type;
23953b113a1eSAriel Levkovich 
23963b113a1eSAriel Levkovich 	switch (type) {
23973b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
23983b113a1eSAriel Levkovich 		err = handle_alloc_dm_memic(context, dm,
23993b113a1eSAriel Levkovich 					    attr,
24003b113a1eSAriel Levkovich 					    attrs);
24013b113a1eSAriel Levkovich 		break;
240225c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
240325c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
240425c13324SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm, attr, attrs, type);
240525c13324SAriel Levkovich 		break;
24063b113a1eSAriel Levkovich 	default:
24073b113a1eSAriel Levkovich 		err = -EOPNOTSUPP;
24083b113a1eSAriel Levkovich 	}
24093b113a1eSAriel Levkovich 
24103b113a1eSAriel Levkovich 	if (err)
24113b113a1eSAriel Levkovich 		goto err_free;
241224da0016SAriel Levkovich 
241324da0016SAriel Levkovich 	return &dm->ibdm;
241424da0016SAriel Levkovich 
241524da0016SAriel Levkovich err_free:
241624da0016SAriel Levkovich 	kfree(dm);
241724da0016SAriel Levkovich 	return ERR_PTR(err);
241824da0016SAriel Levkovich }
241924da0016SAriel Levkovich 
2420c4367a26SShamir Rabinovitch int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
242124da0016SAriel Levkovich {
242225c13324SAriel Levkovich 	struct mlx5_ib_ucontext *ctx = rdma_udata_to_drv_context(
242325c13324SAriel Levkovich 		&attrs->driver_udata, struct mlx5_ib_ucontext, ibucontext);
24243b113a1eSAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ibdm->device)->dm;
242524da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
242624da0016SAriel Levkovich 	u32 page_idx;
242724da0016SAriel Levkovich 	int ret;
242824da0016SAriel Levkovich 
24293b113a1eSAriel Levkovich 	switch (dm->type) {
24303b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
24313b113a1eSAriel Levkovich 		ret = mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
243224da0016SAriel Levkovich 		if (ret)
243324da0016SAriel Levkovich 			return ret;
243424da0016SAriel Levkovich 
24353b113a1eSAriel Levkovich 		page_idx = (dm->dev_addr -
24363b113a1eSAriel Levkovich 			    pci_resource_start(dm_db->dev->pdev, 0) -
24373b113a1eSAriel Levkovich 			    MLX5_CAP64_DEV_MEM(dm_db->dev,
24383b113a1eSAriel Levkovich 					       memic_bar_start_addr)) >>
243924da0016SAriel Levkovich 			   PAGE_SHIFT;
244025c13324SAriel Levkovich 		bitmap_clear(ctx->dm_pages, page_idx,
244125c13324SAriel Levkovich 			     DIV_ROUND_UP(dm->size, PAGE_SIZE));
244225c13324SAriel Levkovich 		break;
244325c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
244425c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
244525c13324SAriel Levkovich 		ret = mlx5_cmd_dealloc_sw_icm(dm_db, dm->type, dm->size,
244625c13324SAriel Levkovich 					      ctx->devx_uid, dm->dev_addr,
244725c13324SAriel Levkovich 					      dm->icm_dm.obj_id);
244825c13324SAriel Levkovich 		if (ret)
244925c13324SAriel Levkovich 			return ret;
24503b113a1eSAriel Levkovich 		break;
24513b113a1eSAriel Levkovich 	default:
24523b113a1eSAriel Levkovich 		return -EOPNOTSUPP;
24533b113a1eSAriel Levkovich 	}
245424da0016SAriel Levkovich 
245524da0016SAriel Levkovich 	kfree(dm);
245624da0016SAriel Levkovich 
245724da0016SAriel Levkovich 	return 0;
245824da0016SAriel Levkovich }
245924da0016SAriel Levkovich 
2460ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2461e126ba97SEli Cohen {
246221a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
246321a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2464e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2465e126ba97SEli Cohen 	int err;
2466a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2467a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)]   = {};
2468a1069c1cSYishai Hadas 	u16 uid = 0;
2469ff23dfa1SShamir Rabinovitch 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2470ff23dfa1SShamir Rabinovitch 		udata, struct mlx5_ib_ucontext, ibucontext);
2471e126ba97SEli Cohen 
2472ff23dfa1SShamir Rabinovitch 	uid = context ? context->devx_uid : 0;
2473a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2474a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
2475a1069c1cSYishai Hadas 	err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2476a1069c1cSYishai Hadas 			    out, sizeof(out));
247721a428a0SLeon Romanovsky 	if (err)
247821a428a0SLeon Romanovsky 		return err;
2479e126ba97SEli Cohen 
2480a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2481a1069c1cSYishai Hadas 	pd->uid = uid;
2482ff23dfa1SShamir Rabinovitch 	if (udata) {
2483e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2484e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2485a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
248621a428a0SLeon Romanovsky 			return -EFAULT;
2487e126ba97SEli Cohen 		}
2488e126ba97SEli Cohen 	}
2489e126ba97SEli Cohen 
249021a428a0SLeon Romanovsky 	return 0;
2491e126ba97SEli Cohen }
2492e126ba97SEli Cohen 
2493c4367a26SShamir Rabinovitch static void mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2494e126ba97SEli Cohen {
2495e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2496e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2497e126ba97SEli Cohen 
2498a1069c1cSYishai Hadas 	mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2499e126ba97SEli Cohen }
2500e126ba97SEli Cohen 
2501466fa6d2SMaor Gottlieb enum {
2502466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2503466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
250471c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_INNER_BIT,
250571c6e863SAriel Levkovich 	MATCH_CRITERIA_ENABLE_MISC2_BIT
2506466fa6d2SMaor Gottlieb };
2507038d2ef8SMaor Gottlieb 
2508466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2509466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2510466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2511466fa6d2SMaor Gottlieb 
2512466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2513466fa6d2SMaor Gottlieb {
2514466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2515466fa6d2SMaor Gottlieb 
2516466fa6d2SMaor Gottlieb 	match_criteria_enable =
2517466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2518466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2519466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2520466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2521466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2522466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2523466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2524466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
252571c6e863SAriel Levkovich 	match_criteria_enable |=
252671c6e863SAriel Levkovich 		(!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
252771c6e863SAriel Levkovich 		MATCH_CRITERIA_ENABLE_MISC2_BIT;
2528466fa6d2SMaor Gottlieb 
2529466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2530038d2ef8SMaor Gottlieb }
2531038d2ef8SMaor Gottlieb 
25326113cc44SMaor Gottlieb static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2533ca0d4753SMaor Gottlieb {
25346113cc44SMaor Gottlieb 	u8 entry_mask;
25356113cc44SMaor Gottlieb 	u8 entry_val;
25366113cc44SMaor Gottlieb 	int err = 0;
25376113cc44SMaor Gottlieb 
25386113cc44SMaor Gottlieb 	if (!mask)
25396113cc44SMaor Gottlieb 		goto out;
25406113cc44SMaor Gottlieb 
25416113cc44SMaor Gottlieb 	entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
25426113cc44SMaor Gottlieb 			      ip_protocol);
25436113cc44SMaor Gottlieb 	entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
25446113cc44SMaor Gottlieb 			     ip_protocol);
25456113cc44SMaor Gottlieb 	if (!entry_mask) {
2546ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2547ca0d4753SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
25486113cc44SMaor Gottlieb 		goto out;
25496113cc44SMaor Gottlieb 	}
25506113cc44SMaor Gottlieb 	/* Don't override existing ip protocol */
25516113cc44SMaor Gottlieb 	if (mask != entry_mask || val != entry_val)
25526113cc44SMaor Gottlieb 		err = -EINVAL;
25536113cc44SMaor Gottlieb out:
25546113cc44SMaor Gottlieb 	return err;
2555ca0d4753SMaor Gottlieb }
2556ca0d4753SMaor Gottlieb 
255737da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
25582d1e697eSMoses Reuben 			   bool inner)
25592d1e697eSMoses Reuben {
25602d1e697eSMoses Reuben 	if (inner) {
25612d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
25622d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
25632d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
25642d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
25652d1e697eSMoses Reuben 	} else {
25662d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
25672d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
25682d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
25692d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
25702d1e697eSMoses Reuben 	}
25712d1e697eSMoses Reuben }
25722d1e697eSMoses Reuben 
2573ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2574ca0d4753SMaor Gottlieb {
2575ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2576ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2577ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2578ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2579ca0d4753SMaor Gottlieb }
2580ca0d4753SMaor Gottlieb 
258171c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
258271c6e863SAriel Levkovich {
258371c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
258471c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
258571c6e863SAriel Levkovich 		return -EOPNOTSUPP;
258671c6e863SAriel Levkovich 
258771c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
258871c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
258971c6e863SAriel Levkovich 		return -EOPNOTSUPP;
259071c6e863SAriel Levkovich 
259171c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
259271c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
259371c6e863SAriel Levkovich 		return -EOPNOTSUPP;
259471c6e863SAriel Levkovich 
259571c6e863SAriel Levkovich 	if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
259671c6e863SAriel Levkovich 	    !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
259771c6e863SAriel Levkovich 		return -EOPNOTSUPP;
259871c6e863SAriel Levkovich 
259971c6e863SAriel Levkovich 	return 0;
260071c6e863SAriel Levkovich }
260171c6e863SAriel Levkovich 
2602c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2603c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2604ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2605466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2606c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2607ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
26082ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2609a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
26103b3233fbSRaed Salem #define LAST_COUNTERS_FIELD counters
2611c47ac6aeSMaor Gottlieb 
2612c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2613c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2614c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2615c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2616c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2617c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2618c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2619c47ac6aeSMaor Gottlieb 
26202ea26203SMark Bloch int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
26212ea26203SMark Bloch 			   bool is_egress,
2622802c2125SAviad Yehezkel 			   struct mlx5_flow_act *action)
2623802c2125SAviad Yehezkel {
2624802c2125SAviad Yehezkel 
2625802c2125SAviad Yehezkel 	switch (maction->ib_action.type) {
2626802c2125SAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
2627501f14e3SMark Bloch 		if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2628501f14e3SMark Bloch 				      MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2629501f14e3SMark Bloch 			return -EINVAL;
2630802c2125SAviad Yehezkel 		/* Currently only AES_GCM keymat is supported by the driver */
2631802c2125SAviad Yehezkel 		action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
26322ea26203SMark Bloch 		action->action |= is_egress ?
2633802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2634802c2125SAviad Yehezkel 			MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2635802c2125SAviad Yehezkel 		return 0;
2636b1085be3SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
2637b1085be3SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2638b1085be3SMark Bloch 		    MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
2639501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2640501f14e3SMark Bloch 				return -EINVAL;
2641b1085be3SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2642b1085be3SMark Bloch 			action->modify_id = maction->flow_action_raw.action_id;
2643b1085be3SMark Bloch 			return 0;
2644b1085be3SMark Bloch 		}
264510a30896SMark Bloch 		if (maction->flow_action_raw.sub_type ==
264610a30896SMark Bloch 		    MLX5_IB_FLOW_ACTION_DECAP) {
2647501f14e3SMark Bloch 			if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2648501f14e3SMark Bloch 				return -EINVAL;
264910a30896SMark Bloch 			action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
265010a30896SMark Bloch 			return 0;
265110a30896SMark Bloch 		}
2652e806f932SMark Bloch 		if (maction->flow_action_raw.sub_type ==
2653e806f932SMark Bloch 		    MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
2654501f14e3SMark Bloch 			if (action->action &
2655501f14e3SMark Bloch 			    MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2656501f14e3SMark Bloch 				return -EINVAL;
2657e806f932SMark Bloch 			action->action |=
2658e806f932SMark Bloch 				MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2659e806f932SMark Bloch 			action->reformat_id =
2660e806f932SMark Bloch 				maction->flow_action_raw.action_id;
2661e806f932SMark Bloch 			return 0;
2662e806f932SMark Bloch 		}
2663b1085be3SMark Bloch 		/* fall through */
2664802c2125SAviad Yehezkel 	default:
2665802c2125SAviad Yehezkel 		return -EOPNOTSUPP;
2666802c2125SAviad Yehezkel 	}
2667802c2125SAviad Yehezkel }
2668802c2125SAviad Yehezkel 
2669bb0ee7dcSJianbo Liu static int parse_flow_attr(struct mlx5_core_dev *mdev,
2670bb0ee7dcSJianbo Liu 			   struct mlx5_flow_spec *spec,
2671bb0ee7dcSJianbo Liu 			   const union ib_flow_spec *ib_spec,
2672802c2125SAviad Yehezkel 			   const struct ib_flow_attr *flow_attr,
267371c6e863SAriel Levkovich 			   struct mlx5_flow_act *action, u32 prev_type)
2674038d2ef8SMaor Gottlieb {
2675bb0ee7dcSJianbo Liu 	struct mlx5_flow_context *flow_context = &spec->flow_context;
2676bb0ee7dcSJianbo Liu 	u32 *match_c = spec->match_criteria;
2677bb0ee7dcSJianbo Liu 	u32 *match_v = spec->match_value;
2678466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2679466fa6d2SMaor Gottlieb 					   misc_parameters);
2680466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2681466fa6d2SMaor Gottlieb 					   misc_parameters);
268271c6e863SAriel Levkovich 	void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
268371c6e863SAriel Levkovich 					    misc_parameters_2);
268471c6e863SAriel Levkovich 	void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
268571c6e863SAriel Levkovich 					    misc_parameters_2);
26862d1e697eSMoses Reuben 	void *headers_c;
26872d1e697eSMoses Reuben 	void *headers_v;
268819cc7524SAriel Levkovich 	int match_ipv;
2689802c2125SAviad Yehezkel 	int ret;
2690466fa6d2SMaor Gottlieb 
26912d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
26922d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
26932d1e697eSMoses Reuben 					 inner_headers);
26942d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
26952d1e697eSMoses Reuben 					 inner_headers);
269619cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
269719cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
26982d1e697eSMoses Reuben 	} else {
26992d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
27002d1e697eSMoses Reuben 					 outer_headers);
27012d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
27022d1e697eSMoses Reuben 					 outer_headers);
270319cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
270419cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
27052d1e697eSMoses Reuben 	}
27062d1e697eSMoses Reuben 
27072d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2708038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2709c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
27101ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2711038d2ef8SMaor Gottlieb 
27122d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2713038d2ef8SMaor Gottlieb 					     dmac_47_16),
2714038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
27152d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2716038d2ef8SMaor Gottlieb 					     dmac_47_16),
2717038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2718038d2ef8SMaor Gottlieb 
27192d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2720ee3da804SMaor Gottlieb 					     smac_47_16),
2721ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
27222d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2723ee3da804SMaor Gottlieb 					     smac_47_16),
2724ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2725ee3da804SMaor Gottlieb 
2726038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
27272d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
272810543365SMohamad Haj Yahia 				 cvlan_tag, 1);
27292d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
273010543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2731038d2ef8SMaor Gottlieb 
27322d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2733038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
27342d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2735038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2736038d2ef8SMaor Gottlieb 
27372d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2738038d2ef8SMaor Gottlieb 				 first_cfi,
2739038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
27402d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2741038d2ef8SMaor Gottlieb 				 first_cfi,
2742038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2743038d2ef8SMaor Gottlieb 
27442d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2745038d2ef8SMaor Gottlieb 				 first_prio,
2746038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
27472d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2748038d2ef8SMaor Gottlieb 				 first_prio,
2749038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2750038d2ef8SMaor Gottlieb 		}
27512d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2752038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
27532d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2754038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2755038d2ef8SMaor Gottlieb 		break;
2756038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2757c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
27581ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2759038d2ef8SMaor Gottlieb 
276019cc7524SAriel Levkovich 		if (match_ipv) {
276119cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
276219cc7524SAriel Levkovich 				 ip_version, 0xf);
276319cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
27643346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV4_VERSION);
276519cc7524SAriel Levkovich 		} else {
27662d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2767038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
27682d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2769038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
277019cc7524SAriel Levkovich 		}
2771038d2ef8SMaor Gottlieb 
27722d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2773038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2774038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2775038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
27762d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2777038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2778038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2779038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
27802d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2781038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2782038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2783038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
27842d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2785038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2786038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2787038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2788ca0d4753SMaor Gottlieb 
27892d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2790ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2791ca0d4753SMaor Gottlieb 
27926113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
27936113cc44SMaor Gottlieb 			      ib_spec->ipv4.mask.proto,
27946113cc44SMaor Gottlieb 			      ib_spec->ipv4.val.proto))
27956113cc44SMaor Gottlieb 			return -EINVAL;
2796038d2ef8SMaor Gottlieb 		break;
2797026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2798c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
27991ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2800026bae0cSMaor Gottlieb 
280119cc7524SAriel Levkovich 		if (match_ipv) {
280219cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
280319cc7524SAriel Levkovich 				 ip_version, 0xf);
280419cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
28053346c487SBoris Pismenny 				 ip_version, MLX5_FS_IPV6_VERSION);
280619cc7524SAriel Levkovich 		} else {
28072d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2808026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
28092d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2810026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
281119cc7524SAriel Levkovich 		}
2812026bae0cSMaor Gottlieb 
28132d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2814026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2815026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2816026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
28172d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2818026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2819026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2820026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
28212d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2822026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2823026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2824026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
28252d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2826026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2827026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2828026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2829466fa6d2SMaor Gottlieb 
28302d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2831466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2832466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2833466fa6d2SMaor Gottlieb 
28346113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v,
2835466fa6d2SMaor Gottlieb 			      ib_spec->ipv6.mask.next_hdr,
28366113cc44SMaor Gottlieb 			      ib_spec->ipv6.val.next_hdr))
28376113cc44SMaor Gottlieb 			return -EINVAL;
2838466fa6d2SMaor Gottlieb 
28392d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
28402d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
28412d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
28422d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
2843802c2125SAviad Yehezkel 		break;
2844802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ESP:
2845802c2125SAviad Yehezkel 		if (ib_spec->esp.mask.seq)
2846802c2125SAviad Yehezkel 			return -EOPNOTSUPP;
28472d1e697eSMoses Reuben 
2848802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2849802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.mask.spi));
2850802c2125SAviad Yehezkel 		MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2851802c2125SAviad Yehezkel 			 ntohl(ib_spec->esp.val.spi));
2852026bae0cSMaor Gottlieb 		break;
2853038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2854c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2855c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
28561ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2857038d2ef8SMaor Gottlieb 
28586113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
28596113cc44SMaor Gottlieb 			return -EINVAL;
2860038d2ef8SMaor Gottlieb 
28612d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2862038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
28632d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2864038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2865038d2ef8SMaor Gottlieb 
28662d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2867038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
28682d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2869038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2870038d2ef8SMaor Gottlieb 		break;
2871038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2872c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2873c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
28741ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2875038d2ef8SMaor Gottlieb 
28766113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
28776113cc44SMaor Gottlieb 			return -EINVAL;
2878038d2ef8SMaor Gottlieb 
28792d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2880038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
28812d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2882038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2883038d2ef8SMaor Gottlieb 
28842d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2885038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
28862d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2887038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2888038d2ef8SMaor Gottlieb 		break;
2889da2f22aeSAriel Levkovich 	case IB_FLOW_SPEC_GRE:
2890da2f22aeSAriel Levkovich 		if (ib_spec->gre.mask.c_ks_res0_ver)
2891da2f22aeSAriel Levkovich 			return -EOPNOTSUPP;
2892da2f22aeSAriel Levkovich 
28936113cc44SMaor Gottlieb 		if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
28946113cc44SMaor Gottlieb 			return -EINVAL;
28956113cc44SMaor Gottlieb 
2896da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2897da2f22aeSAriel Levkovich 			 0xff);
2898da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2899da2f22aeSAriel Levkovich 			 IPPROTO_GRE);
2900da2f22aeSAriel Levkovich 
2901da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
2902a93b632cSMaor Gottlieb 			 ntohs(ib_spec->gre.mask.protocol));
2903da2f22aeSAriel Levkovich 		MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2904da2f22aeSAriel Levkovich 			 ntohs(ib_spec->gre.val.protocol));
2905da2f22aeSAriel Levkovich 
2906da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
29075886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2908da2f22aeSAriel Levkovich 		       &ib_spec->gre.mask.key,
2909da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.mask.key));
2910da2f22aeSAriel Levkovich 		memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
29115886a96aSOz Shlomo 				    gre_key.nvgre.hi),
2912da2f22aeSAriel Levkovich 		       &ib_spec->gre.val.key,
2913da2f22aeSAriel Levkovich 		       sizeof(ib_spec->gre.val.key));
2914da2f22aeSAriel Levkovich 		break;
291571c6e863SAriel Levkovich 	case IB_FLOW_SPEC_MPLS:
291671c6e863SAriel Levkovich 		switch (prev_type) {
291771c6e863SAriel Levkovich 		case IB_FLOW_SPEC_UDP:
291871c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
291971c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_udp),
292071c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
292171c6e863SAriel Levkovich 				return -EOPNOTSUPP;
292271c6e863SAriel Levkovich 
292371c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
292471c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
292571c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
292671c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
292771c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
292871c6e863SAriel Levkovich 					    outer_first_mpls_over_udp),
292971c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
293071c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
293171c6e863SAriel Levkovich 			break;
293271c6e863SAriel Levkovich 		case IB_FLOW_SPEC_GRE:
293371c6e863SAriel Levkovich 			if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
293471c6e863SAriel Levkovich 						   ft_field_support.outer_first_mpls_over_gre),
293571c6e863SAriel Levkovich 						   &ib_spec->mpls.mask.tag))
293671c6e863SAriel Levkovich 				return -EOPNOTSUPP;
293771c6e863SAriel Levkovich 
293871c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
293971c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
294071c6e863SAriel Levkovich 			       &ib_spec->mpls.val.tag,
294171c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.val.tag));
294271c6e863SAriel Levkovich 			memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
294371c6e863SAriel Levkovich 					    outer_first_mpls_over_gre),
294471c6e863SAriel Levkovich 			       &ib_spec->mpls.mask.tag,
294571c6e863SAriel Levkovich 			       sizeof(ib_spec->mpls.mask.tag));
294671c6e863SAriel Levkovich 			break;
294771c6e863SAriel Levkovich 		default:
294871c6e863SAriel Levkovich 			if (ib_spec->type & IB_FLOW_SPEC_INNER) {
294971c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
295071c6e863SAriel Levkovich 							   ft_field_support.inner_first_mpls),
295171c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
295271c6e863SAriel Levkovich 					return -EOPNOTSUPP;
295371c6e863SAriel Levkovich 
295471c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
295571c6e863SAriel Levkovich 						    inner_first_mpls),
295671c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
295771c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
295871c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
295971c6e863SAriel Levkovich 						    inner_first_mpls),
296071c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
296171c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
296271c6e863SAriel Levkovich 			} else {
296371c6e863SAriel Levkovich 				if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
296471c6e863SAriel Levkovich 							   ft_field_support.outer_first_mpls),
296571c6e863SAriel Levkovich 							   &ib_spec->mpls.mask.tag))
296671c6e863SAriel Levkovich 					return -EOPNOTSUPP;
296771c6e863SAriel Levkovich 
296871c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
296971c6e863SAriel Levkovich 						    outer_first_mpls),
297071c6e863SAriel Levkovich 				       &ib_spec->mpls.val.tag,
297171c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.val.tag));
297271c6e863SAriel Levkovich 				memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
297371c6e863SAriel Levkovich 						    outer_first_mpls),
297471c6e863SAriel Levkovich 				       &ib_spec->mpls.mask.tag,
297571c6e863SAriel Levkovich 				       sizeof(ib_spec->mpls.mask.tag));
297671c6e863SAriel Levkovich 			}
297771c6e863SAriel Levkovich 		}
297871c6e863SAriel Levkovich 		break;
2979ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2980ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2981ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
29821ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2983ffb30d8fSMoses Reuben 
2984ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2985ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2986ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2987ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2988ffb30d8fSMoses Reuben 		break;
29892ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
29902ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
29912ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
29922ac693f9SMoses Reuben 			return -EOPNOTSUPP;
29932ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
29942ac693f9SMoses Reuben 			return -EINVAL;
29952ac693f9SMoses Reuben 
2996bb0ee7dcSJianbo Liu 		flow_context->flow_tag = ib_spec->flow_tag.tag_id;
2997bb0ee7dcSJianbo Liu 		flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
29982ac693f9SMoses Reuben 		break;
2999a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
3000a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
3001a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
3002a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
3003075572d4SBoris Pismenny 		action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
3004a22ed86cSSlava Shwartsman 		break;
3005802c2125SAviad Yehezkel 	case IB_FLOW_SPEC_ACTION_HANDLE:
30062ea26203SMark Bloch 		ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
30072ea26203SMark Bloch 			flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
3008802c2125SAviad Yehezkel 		if (ret)
3009802c2125SAviad Yehezkel 			return ret;
3010802c2125SAviad Yehezkel 		break;
30113b3233fbSRaed Salem 	case IB_FLOW_SPEC_ACTION_COUNT:
30123b3233fbSRaed Salem 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
30133b3233fbSRaed Salem 					 LAST_COUNTERS_FIELD))
30143b3233fbSRaed Salem 			return -EOPNOTSUPP;
30153b3233fbSRaed Salem 
30163b3233fbSRaed Salem 		/* for now support only one counters spec per flow */
30173b3233fbSRaed Salem 		if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
30183b3233fbSRaed Salem 			return -EINVAL;
30193b3233fbSRaed Salem 
30203b3233fbSRaed Salem 		action->counters = ib_spec->flow_count.counters;
30213b3233fbSRaed Salem 		action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
30223b3233fbSRaed Salem 		break;
3023038d2ef8SMaor Gottlieb 	default:
3024038d2ef8SMaor Gottlieb 		return -EINVAL;
3025038d2ef8SMaor Gottlieb 	}
3026038d2ef8SMaor Gottlieb 
3027038d2ef8SMaor Gottlieb 	return 0;
3028038d2ef8SMaor Gottlieb }
3029038d2ef8SMaor Gottlieb 
3030038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
3031038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
3032038d2ef8SMaor Gottlieb  * could steal other multicast packets.
3033038d2ef8SMaor Gottlieb  */
3034a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
3035038d2ef8SMaor Gottlieb {
303681e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
3037038d2ef8SMaor Gottlieb 
3038038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
3039038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
3040038d2ef8SMaor Gottlieb 		return false;
3041038d2ef8SMaor Gottlieb 
304281e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
304381e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
304481e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
3045038d2ef8SMaor Gottlieb 
304681e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
304781e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
304881e30880SYishai Hadas 			return true;
304981e30880SYishai Hadas 
305081e30880SYishai Hadas 		return false;
305181e30880SYishai Hadas 	}
305281e30880SYishai Hadas 
305381e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
305481e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
305581e30880SYishai Hadas 
305681e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
3057038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
3058038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
3059038d2ef8SMaor Gottlieb 	}
3060038d2ef8SMaor Gottlieb 
306181e30880SYishai Hadas 	return false;
306281e30880SYishai Hadas }
306381e30880SYishai Hadas 
3064802c2125SAviad Yehezkel enum valid_spec {
3065802c2125SAviad Yehezkel 	VALID_SPEC_INVALID,
3066802c2125SAviad Yehezkel 	VALID_SPEC_VALID,
3067802c2125SAviad Yehezkel 	VALID_SPEC_NA,
3068802c2125SAviad Yehezkel };
3069802c2125SAviad Yehezkel 
3070802c2125SAviad Yehezkel static enum valid_spec
3071802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
3072802c2125SAviad Yehezkel 		     const struct mlx5_flow_spec *spec,
3073802c2125SAviad Yehezkel 		     const struct mlx5_flow_act *flow_act,
3074802c2125SAviad Yehezkel 		     bool egress)
3075802c2125SAviad Yehezkel {
3076802c2125SAviad Yehezkel 	const u32 *match_c = spec->match_criteria;
3077802c2125SAviad Yehezkel 	bool is_crypto =
3078802c2125SAviad Yehezkel 		(flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
3079802c2125SAviad Yehezkel 				     MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
3080802c2125SAviad Yehezkel 	bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
3081802c2125SAviad Yehezkel 	bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
3082802c2125SAviad Yehezkel 
3083802c2125SAviad Yehezkel 	/*
3084802c2125SAviad Yehezkel 	 * Currently only crypto is supported in egress, when regular egress
3085802c2125SAviad Yehezkel 	 * rules would be supported, always return VALID_SPEC_NA.
3086802c2125SAviad Yehezkel 	 */
3087802c2125SAviad Yehezkel 	if (!is_crypto)
308878dd0c43SMark Bloch 		return VALID_SPEC_NA;
3089802c2125SAviad Yehezkel 
3090802c2125SAviad Yehezkel 	return is_crypto && is_ipsec &&
3091bb0ee7dcSJianbo Liu 		(!egress || (!is_drop &&
3092bb0ee7dcSJianbo Liu 			     !(spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG))) ?
3093802c2125SAviad Yehezkel 		VALID_SPEC_VALID : VALID_SPEC_INVALID;
3094802c2125SAviad Yehezkel }
3095802c2125SAviad Yehezkel 
3096802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev,
3097802c2125SAviad Yehezkel 			  const struct mlx5_flow_spec *spec,
3098802c2125SAviad Yehezkel 			  const struct mlx5_flow_act *flow_act,
3099802c2125SAviad Yehezkel 			  bool egress)
3100802c2125SAviad Yehezkel {
3101802c2125SAviad Yehezkel 	/* We curretly only support ipsec egress flow */
3102802c2125SAviad Yehezkel 	return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
3103802c2125SAviad Yehezkel }
3104802c2125SAviad Yehezkel 
310519cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
310619cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
31070f750966SAriel Levkovich 			       bool check_inner)
3108038d2ef8SMaor Gottlieb {
3109038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
311019cc7524SAriel Levkovich 	int match_ipv = check_inner ?
311119cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
311219cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
311319cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
311419cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
31150f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
31160f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
31170f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
31180f750966SAriel Levkovich 	bool has_ethertype = false;
3119038d2ef8SMaor Gottlieb 	unsigned int spec_index;
31200f750966SAriel Levkovich 	bool mask_valid = true;
31210f750966SAriel Levkovich 	u16 eth_type = 0;
31220f750966SAriel Levkovich 	bool type_valid;
3123038d2ef8SMaor Gottlieb 
3124038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
3125038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
31260f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
3127038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
31280f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
31290f750966SAriel Levkovich 				      htons(0xffff));
31300f750966SAriel Levkovich 			has_ethertype = true;
31310f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
31320f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
31330f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
31340f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
3135038d2ef8SMaor Gottlieb 		}
3136038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
3137038d2ef8SMaor Gottlieb 	}
31380f750966SAriel Levkovich 
31390f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
31400f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
31410f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
31420f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
31430f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
31440f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
314519cc7524SAriel Levkovich 
314619cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
314719cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
314819cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
31490f750966SAriel Levkovich 	}
31500f750966SAriel Levkovich 
31510f750966SAriel Levkovich 	return type_valid;
31520f750966SAriel Levkovich }
31530f750966SAriel Levkovich 
315419cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
315519cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
31560f750966SAriel Levkovich {
315719cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
315819cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
3159038d2ef8SMaor Gottlieb }
3160038d2ef8SMaor Gottlieb 
3161038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
3162038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
3163038d2ef8SMaor Gottlieb {
3164038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
3165038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
3166038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
3167038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
3168038d2ef8SMaor Gottlieb 	}
3169038d2ef8SMaor Gottlieb }
3170038d2ef8SMaor Gottlieb 
31713b3233fbSRaed Salem static void counters_clear_description(struct ib_counters *counters)
31723b3233fbSRaed Salem {
31733b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
31743b3233fbSRaed Salem 
31753b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
31763b3233fbSRaed Salem 	kfree(mcounters->counters_data);
31773b3233fbSRaed Salem 	mcounters->counters_data = NULL;
31783b3233fbSRaed Salem 	mcounters->cntrs_max_index = 0;
31793b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
31803b3233fbSRaed Salem }
31813b3233fbSRaed Salem 
3182038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3183038d2ef8SMaor Gottlieb {
3184038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3185038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
3186038d2ef8SMaor Gottlieb 							  ibflow);
3187038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
3188d4be3f44SYishai Hadas 	struct mlx5_ib_dev *dev = handler->dev;
3189038d2ef8SMaor Gottlieb 
31909a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3191038d2ef8SMaor Gottlieb 
3192038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
319374491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
3194cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
3195038d2ef8SMaor Gottlieb 		list_del(&iter->list);
3196038d2ef8SMaor Gottlieb 		kfree(iter);
3197038d2ef8SMaor Gottlieb 	}
3198038d2ef8SMaor Gottlieb 
319974491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
32005497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
32013b3233fbSRaed Salem 	if (handler->ibcounters &&
32023b3233fbSRaed Salem 	    atomic_read(&handler->ibcounters->usecnt) == 1)
32033b3233fbSRaed Salem 		counters_clear_description(handler->ibcounters);
3204038d2ef8SMaor Gottlieb 
32053b3233fbSRaed Salem 	mutex_unlock(&dev->flow_db->lock);
3206d4be3f44SYishai Hadas 	if (handler->flow_matcher)
3207d4be3f44SYishai Hadas 		atomic_dec(&handler->flow_matcher->usecnt);
3208038d2ef8SMaor Gottlieb 	kfree(handler);
3209038d2ef8SMaor Gottlieb 
3210038d2ef8SMaor Gottlieb 	return 0;
3211038d2ef8SMaor Gottlieb }
3212038d2ef8SMaor Gottlieb 
321335d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
321435d19011SMaor Gottlieb {
321535d19011SMaor Gottlieb 	priority *= 2;
321635d19011SMaor Gottlieb 	if (!dont_trap)
321735d19011SMaor Gottlieb 		priority++;
321835d19011SMaor Gottlieb 	return priority;
321935d19011SMaor Gottlieb }
322035d19011SMaor Gottlieb 
3221cc0e5d42SMaor Gottlieb enum flow_table_type {
3222cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
3223cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
3224cc0e5d42SMaor Gottlieb };
3225cc0e5d42SMaor Gottlieb 
322600b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
322700b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
3228d4be3f44SYishai Hadas 
3229d4be3f44SYishai Hadas static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3230d4be3f44SYishai Hadas 					   struct mlx5_ib_flow_prio *prio,
3231d4be3f44SYishai Hadas 					   int priority,
32324adda112SMark Bloch 					   int num_entries, int num_groups,
32334adda112SMark Bloch 					   u32 flags)
3234d4be3f44SYishai Hadas {
3235d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft;
3236d4be3f44SYishai Hadas 
3237d4be3f44SYishai Hadas 	ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3238d4be3f44SYishai Hadas 						 num_entries,
3239d4be3f44SYishai Hadas 						 num_groups,
32404adda112SMark Bloch 						 0, flags);
3241d4be3f44SYishai Hadas 	if (IS_ERR(ft))
3242d4be3f44SYishai Hadas 		return ERR_CAST(ft);
3243d4be3f44SYishai Hadas 
3244d4be3f44SYishai Hadas 	prio->flow_table = ft;
3245d4be3f44SYishai Hadas 	prio->refcount = 0;
3246d4be3f44SYishai Hadas 	return prio;
3247d4be3f44SYishai Hadas }
3248d4be3f44SYishai Hadas 
3249038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
3250cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
3251cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
3252038d2ef8SMaor Gottlieb {
325335d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
3254038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
3255038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
3256038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
3257dac388efSMaor Gottlieb 	int max_table_size;
3258038d2ef8SMaor Gottlieb 	int num_entries;
3259038d2ef8SMaor Gottlieb 	int num_groups;
32604adda112SMark Bloch 	u32 flags = 0;
3261038d2ef8SMaor Gottlieb 	int priority;
3262038d2ef8SMaor Gottlieb 
3263dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3264dac388efSMaor Gottlieb 						       log_max_ft_size));
3265038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
326678dd0c43SMark Bloch 		enum mlx5_flow_namespace_type fn_type;
326778dd0c43SMark Bloch 
326878dd0c43SMark Bloch 		if (flow_is_multicast_only(flow_attr) &&
326935d19011SMaor Gottlieb 		    !dont_trap)
3270038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
3271038d2ef8SMaor Gottlieb 		else
327235d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
327335d19011SMaor Gottlieb 							dont_trap);
327478dd0c43SMark Bloch 		if (ft_type == MLX5_IB_FT_RX) {
327578dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
327678dd0c43SMark Bloch 			prio = &dev->flow_db->prios[priority];
32776a4d00beSMark Bloch 			if (!dev->is_rep &&
32784adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
32794adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
32806a4d00beSMark Bloch 			if (!dev->is_rep &&
32815c2db53fSMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
32825c2db53fSMark Bloch 					reformat_l3_tunnel_to_l2))
32835c2db53fSMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
328478dd0c43SMark Bloch 		} else {
328578dd0c43SMark Bloch 			max_table_size =
328678dd0c43SMark Bloch 				BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
328778dd0c43SMark Bloch 							      log_max_ft_size));
328878dd0c43SMark Bloch 			fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
328978dd0c43SMark Bloch 			prio = &dev->flow_db->egress_prios[priority];
32906a4d00beSMark Bloch 			if (!dev->is_rep &&
32914adda112SMark Bloch 			    MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
32924adda112SMark Bloch 				flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
329378dd0c43SMark Bloch 		}
329478dd0c43SMark Bloch 		ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
3295038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
3296038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
3297038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3298038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3299038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
3300038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
3301038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
3302038d2ef8SMaor Gottlieb 					 &num_entries,
3303038d2ef8SMaor Gottlieb 					 &num_groups);
33049a4ca38dSMark Bloch 		prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
3305cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3306cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3307cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
3308cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
3309cc0e5d42SMaor Gottlieb 
3310cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3311cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3312cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3313cc0e5d42SMaor Gottlieb 
33149a4ca38dSMark Bloch 		prio = &dev->flow_db->sniffer[ft_type];
3315cc0e5d42SMaor Gottlieb 		priority = 0;
3316cc0e5d42SMaor Gottlieb 		num_entries = 1;
3317cc0e5d42SMaor Gottlieb 		num_groups = 1;
3318038d2ef8SMaor Gottlieb 	}
3319038d2ef8SMaor Gottlieb 
3320038d2ef8SMaor Gottlieb 	if (!ns)
3321038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
3322038d2ef8SMaor Gottlieb 
33233b70508aSMark Bloch 	max_table_size = min_t(int, num_entries, max_table_size);
3324dac388efSMaor Gottlieb 
3325038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
3326d4be3f44SYishai Hadas 	if (!ft)
33273b70508aSMark Bloch 		return _get_prio(ns, prio, priority, max_table_size, num_groups,
33284adda112SMark Bloch 				 flags);
3329038d2ef8SMaor Gottlieb 
3330d4be3f44SYishai Hadas 	return prio;
3331038d2ef8SMaor Gottlieb }
3332038d2ef8SMaor Gottlieb 
3333a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
3334a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
3335a550ddfcSYishai Hadas 			    u32 underlay_qpn)
3336a550ddfcSYishai Hadas {
3337a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3338a550ddfcSYishai Hadas 					   spec->match_criteria,
3339a550ddfcSYishai Hadas 					   misc_parameters);
3340a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3341a550ddfcSYishai Hadas 					   misc_parameters);
3342a550ddfcSYishai Hadas 
3343a550ddfcSYishai Hadas 	if (underlay_qpn &&
3344a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3345a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
3346a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3347a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
3348a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
3349a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
3350a550ddfcSYishai Hadas 	}
3351a550ddfcSYishai Hadas }
3352a550ddfcSYishai Hadas 
33535e95af5fSRaed Salem static int read_flow_counters(struct ib_device *ibdev,
33545e95af5fSRaed Salem 			      struct mlx5_read_counters_attr *read_attr)
33555e95af5fSRaed Salem {
33565e95af5fSRaed Salem 	struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
33575e95af5fSRaed Salem 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
33585e95af5fSRaed Salem 
33595e95af5fSRaed Salem 	return mlx5_fc_query(dev->mdev, fc,
33605e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_PACKETS],
33615e95af5fSRaed Salem 			     &read_attr->out[IB_COUNTER_BYTES]);
33625e95af5fSRaed Salem }
33635e95af5fSRaed Salem 
33645e95af5fSRaed Salem /* flow counters currently expose two counters packets and bytes */
33655e95af5fSRaed Salem #define FLOW_COUNTERS_NUM 2
33663b3233fbSRaed Salem static int counters_set_description(struct ib_counters *counters,
33673b3233fbSRaed Salem 				    enum mlx5_ib_counters_type counters_type,
33683b3233fbSRaed Salem 				    struct mlx5_ib_flow_counters_desc *desc_data,
33693b3233fbSRaed Salem 				    u32 ncounters)
33703b3233fbSRaed Salem {
33713b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
33723b3233fbSRaed Salem 	u32 cntrs_max_index = 0;
33733b3233fbSRaed Salem 	int i;
33743b3233fbSRaed Salem 
33753b3233fbSRaed Salem 	if (counters_type != MLX5_IB_COUNTERS_FLOW)
33763b3233fbSRaed Salem 		return -EINVAL;
33773b3233fbSRaed Salem 
33783b3233fbSRaed Salem 	/* init the fields for the object */
33793b3233fbSRaed Salem 	mcounters->type = counters_type;
33805e95af5fSRaed Salem 	mcounters->read_counters = read_flow_counters;
33815e95af5fSRaed Salem 	mcounters->counters_num = FLOW_COUNTERS_NUM;
33823b3233fbSRaed Salem 	mcounters->ncounters = ncounters;
33833b3233fbSRaed Salem 	/* each counter entry have both description and index pair */
33843b3233fbSRaed Salem 	for (i = 0; i < ncounters; i++) {
33853b3233fbSRaed Salem 		if (desc_data[i].description > IB_COUNTER_BYTES)
33863b3233fbSRaed Salem 			return -EINVAL;
33873b3233fbSRaed Salem 
33883b3233fbSRaed Salem 		if (cntrs_max_index <= desc_data[i].index)
33893b3233fbSRaed Salem 			cntrs_max_index = desc_data[i].index + 1;
33903b3233fbSRaed Salem 	}
33913b3233fbSRaed Salem 
33923b3233fbSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
33933b3233fbSRaed Salem 	mcounters->counters_data = desc_data;
33943b3233fbSRaed Salem 	mcounters->cntrs_max_index = cntrs_max_index;
33953b3233fbSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
33963b3233fbSRaed Salem 
33973b3233fbSRaed Salem 	return 0;
33983b3233fbSRaed Salem }
33993b3233fbSRaed Salem 
34003b3233fbSRaed Salem #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
34013b3233fbSRaed Salem static int flow_counters_set_data(struct ib_counters *ibcounters,
34023b3233fbSRaed Salem 				  struct mlx5_ib_create_flow *ucmd)
34033b3233fbSRaed Salem {
34043b3233fbSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
34053b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
34063b3233fbSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc_data = NULL;
34073b3233fbSRaed Salem 	bool hw_hndl = false;
34083b3233fbSRaed Salem 	int ret = 0;
34093b3233fbSRaed Salem 
34103b3233fbSRaed Salem 	if (ucmd && ucmd->ncounters_data != 0) {
34113b3233fbSRaed Salem 		cntrs_data = ucmd->data;
34123b3233fbSRaed Salem 		if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
34133b3233fbSRaed Salem 			return -EINVAL;
34143b3233fbSRaed Salem 
34153b3233fbSRaed Salem 		desc_data = kcalloc(cntrs_data->ncounters,
34163b3233fbSRaed Salem 				    sizeof(*desc_data),
34173b3233fbSRaed Salem 				    GFP_KERNEL);
34183b3233fbSRaed Salem 		if (!desc_data)
34193b3233fbSRaed Salem 			return  -ENOMEM;
34203b3233fbSRaed Salem 
34213b3233fbSRaed Salem 		if (copy_from_user(desc_data,
34223b3233fbSRaed Salem 				   u64_to_user_ptr(cntrs_data->counters_data),
34233b3233fbSRaed Salem 				   sizeof(*desc_data) * cntrs_data->ncounters)) {
34243b3233fbSRaed Salem 			ret = -EFAULT;
34253b3233fbSRaed Salem 			goto free;
34263b3233fbSRaed Salem 		}
34273b3233fbSRaed Salem 	}
34283b3233fbSRaed Salem 
34293b3233fbSRaed Salem 	if (!mcounters->hw_cntrs_hndl) {
34303b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = mlx5_fc_create(
34313b3233fbSRaed Salem 			to_mdev(ibcounters->device)->mdev, false);
3432e31abf76Sweiyongjun (A) 		if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3433e31abf76Sweiyongjun (A) 			ret = PTR_ERR(mcounters->hw_cntrs_hndl);
34343b3233fbSRaed Salem 			goto free;
34353b3233fbSRaed Salem 		}
34363b3233fbSRaed Salem 		hw_hndl = true;
34373b3233fbSRaed Salem 	}
34383b3233fbSRaed Salem 
34393b3233fbSRaed Salem 	if (desc_data) {
34403b3233fbSRaed Salem 		/* counters already bound to at least one flow */
34413b3233fbSRaed Salem 		if (mcounters->cntrs_max_index) {
34423b3233fbSRaed Salem 			ret = -EINVAL;
34433b3233fbSRaed Salem 			goto free_hndl;
34443b3233fbSRaed Salem 		}
34453b3233fbSRaed Salem 
34463b3233fbSRaed Salem 		ret = counters_set_description(ibcounters,
34473b3233fbSRaed Salem 					       MLX5_IB_COUNTERS_FLOW,
34483b3233fbSRaed Salem 					       desc_data,
34493b3233fbSRaed Salem 					       cntrs_data->ncounters);
34503b3233fbSRaed Salem 		if (ret)
34513b3233fbSRaed Salem 			goto free_hndl;
34523b3233fbSRaed Salem 
34533b3233fbSRaed Salem 	} else if (!mcounters->cntrs_max_index) {
34543b3233fbSRaed Salem 		/* counters not bound yet, must have udata passed */
34553b3233fbSRaed Salem 		ret = -EINVAL;
34563b3233fbSRaed Salem 		goto free_hndl;
34573b3233fbSRaed Salem 	}
34583b3233fbSRaed Salem 
34593b3233fbSRaed Salem 	return 0;
34603b3233fbSRaed Salem 
34613b3233fbSRaed Salem free_hndl:
34623b3233fbSRaed Salem 	if (hw_hndl) {
34633b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
34643b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
34653b3233fbSRaed Salem 		mcounters->hw_cntrs_hndl = NULL;
34663b3233fbSRaed Salem 	}
34673b3233fbSRaed Salem free:
34683b3233fbSRaed Salem 	kfree(desc_data);
34693b3233fbSRaed Salem 	return ret;
34703b3233fbSRaed Salem }
34713b3233fbSRaed Salem 
3472669ff1e3SJianbo Liu static void mlx5_ib_set_rule_source_port(struct mlx5_ib_dev *dev,
3473669ff1e3SJianbo Liu 					 struct mlx5_flow_spec *spec,
3474669ff1e3SJianbo Liu 					 struct mlx5_eswitch_rep *rep)
3475669ff1e3SJianbo Liu {
3476669ff1e3SJianbo Liu 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
3477669ff1e3SJianbo Liu 	void *misc;
3478669ff1e3SJianbo Liu 
3479669ff1e3SJianbo Liu 	if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
3480669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3481669ff1e3SJianbo Liu 				    misc_parameters_2);
3482669ff1e3SJianbo Liu 
3483669ff1e3SJianbo Liu 		MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
3484669ff1e3SJianbo Liu 			 mlx5_eswitch_get_vport_metadata_for_match(esw,
3485669ff1e3SJianbo Liu 								   rep->vport));
3486669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3487669ff1e3SJianbo Liu 				    misc_parameters_2);
3488669ff1e3SJianbo Liu 
3489669ff1e3SJianbo Liu 		MLX5_SET_TO_ONES(fte_match_set_misc2, misc, metadata_reg_c_0);
3490669ff1e3SJianbo Liu 	} else {
3491669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3492669ff1e3SJianbo Liu 				    misc_parameters);
3493669ff1e3SJianbo Liu 
3494669ff1e3SJianbo Liu 		MLX5_SET(fte_match_set_misc, misc, source_port, rep->vport);
3495669ff1e3SJianbo Liu 
3496669ff1e3SJianbo Liu 		misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3497669ff1e3SJianbo Liu 				    misc_parameters);
3498669ff1e3SJianbo Liu 
3499669ff1e3SJianbo Liu 		MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3500669ff1e3SJianbo Liu 	}
3501669ff1e3SJianbo Liu }
3502669ff1e3SJianbo Liu 
3503a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3504038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
3505dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
3506a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
35073b3233fbSRaed Salem 						      u32 underlay_qpn,
35083b3233fbSRaed Salem 						      struct mlx5_ib_create_flow *ucmd)
3509038d2ef8SMaor Gottlieb {
3510038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
3511038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
3512bb0ee7dcSJianbo Liu 	struct mlx5_flow_act flow_act = {};
3513c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
35143b3233fbSRaed Salem 	struct mlx5_flow_destination dest_arr[2] = {};
35153b3233fbSRaed Salem 	struct mlx5_flow_destination *rule_dst = dest_arr;
3516dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
3517038d2ef8SMaor Gottlieb 	unsigned int spec_index;
351871c6e863SAriel Levkovich 	u32 prev_type = 0;
3519038d2ef8SMaor Gottlieb 	int err = 0;
35203b3233fbSRaed Salem 	int dest_num = 0;
3521802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
3522038d2ef8SMaor Gottlieb 
352319cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
3524038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
3525038d2ef8SMaor Gottlieb 
35266a4d00beSMark Bloch 	if (dev->is_rep && is_egress)
352778dd0c43SMark Bloch 		return ERR_PTR(-EINVAL);
352878dd0c43SMark Bloch 
35291b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3530038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3531c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
3532038d2ef8SMaor Gottlieb 		err = -ENOMEM;
3533038d2ef8SMaor Gottlieb 		goto free;
3534038d2ef8SMaor Gottlieb 	}
3535038d2ef8SMaor Gottlieb 
3536038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
35373b3233fbSRaed Salem 	if (dst) {
35383b3233fbSRaed Salem 		memcpy(&dest_arr[0], dst, sizeof(*dst));
35393b3233fbSRaed Salem 		dest_num++;
35403b3233fbSRaed Salem 	}
3541038d2ef8SMaor Gottlieb 
3542038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
3543bb0ee7dcSJianbo Liu 		err = parse_flow_attr(dev->mdev, spec,
354471c6e863SAriel Levkovich 				      ib_flow, flow_attr, &flow_act,
354571c6e863SAriel Levkovich 				      prev_type);
3546038d2ef8SMaor Gottlieb 		if (err < 0)
3547038d2ef8SMaor Gottlieb 			goto free;
3548038d2ef8SMaor Gottlieb 
354971c6e863SAriel Levkovich 		prev_type = ((union ib_flow_spec *)ib_flow)->type;
3550038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3551038d2ef8SMaor Gottlieb 	}
3552038d2ef8SMaor Gottlieb 
3553a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
3554a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
3555a550ddfcSYishai Hadas 
35566a4d00beSMark Bloch 	if (dev->is_rep) {
3557669ff1e3SJianbo Liu 		struct mlx5_eswitch_rep *rep;
3558018a94eeSMark Bloch 
3559669ff1e3SJianbo Liu 		rep = dev->port[flow_attr->port - 1].rep;
3560669ff1e3SJianbo Liu 		if (!rep) {
35616a4d00beSMark Bloch 			err = -EINVAL;
35626a4d00beSMark Bloch 			goto free;
35636a4d00beSMark Bloch 		}
3564669ff1e3SJianbo Liu 
3565669ff1e3SJianbo Liu 		mlx5_ib_set_rule_source_port(dev, spec, rep);
3566018a94eeSMark Bloch 	}
3567018a94eeSMark Bloch 
3568466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
3569802c2125SAviad Yehezkel 
3570802c2125SAviad Yehezkel 	if (is_egress &&
3571802c2125SAviad Yehezkel 	    !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3572802c2125SAviad Yehezkel 		err = -EINVAL;
3573802c2125SAviad Yehezkel 		goto free;
3574802c2125SAviad Yehezkel 	}
3575802c2125SAviad Yehezkel 
35763b3233fbSRaed Salem 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3577171c7625SMark Bloch 		struct mlx5_ib_mcounters *mcounters;
3578171c7625SMark Bloch 
35793b3233fbSRaed Salem 		err = flow_counters_set_data(flow_act.counters, ucmd);
35803b3233fbSRaed Salem 		if (err)
35813b3233fbSRaed Salem 			goto free;
35823b3233fbSRaed Salem 
3583171c7625SMark Bloch 		mcounters = to_mcounters(flow_act.counters);
35843b3233fbSRaed Salem 		handler->ibcounters = flow_act.counters;
35853b3233fbSRaed Salem 		dest_arr[dest_num].type =
35863b3233fbSRaed Salem 			MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3587171c7625SMark Bloch 		dest_arr[dest_num].counter_id =
3588171c7625SMark Bloch 			mlx5_fc_id(mcounters->hw_cntrs_hndl);
35893b3233fbSRaed Salem 		dest_num++;
35903b3233fbSRaed Salem 	}
35913b3233fbSRaed Salem 
3592075572d4SBoris Pismenny 	if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
35933b3233fbSRaed Salem 		if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3594a22ed86cSSlava Shwartsman 			rule_dst = NULL;
3595a22ed86cSSlava Shwartsman 			dest_num = 0;
35963b3233fbSRaed Salem 		}
3597a22ed86cSSlava Shwartsman 	} else {
3598802c2125SAviad Yehezkel 		if (is_egress)
3599802c2125SAviad Yehezkel 			flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3600802c2125SAviad Yehezkel 		else
3601802c2125SAviad Yehezkel 			flow_act.action |=
36023b3233fbSRaed Salem 				dest_num ?  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
360335d19011SMaor Gottlieb 					MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
3604a22ed86cSSlava Shwartsman 	}
36052ac693f9SMoses Reuben 
3606bb0ee7dcSJianbo Liu 	if ((spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG)  &&
36072ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
36082ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
36092ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
3610bb0ee7dcSJianbo Liu 			     spec->flow_context.flow_tag, flow_attr->type);
36112ac693f9SMoses Reuben 		err = -EINVAL;
36122ac693f9SMoses Reuben 		goto free;
36132ac693f9SMoses Reuben 	}
361474491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
361566958ed9SHadar Hen Zion 					    &flow_act,
3616a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
3617038d2ef8SMaor Gottlieb 
3618038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
3619038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
3620038d2ef8SMaor Gottlieb 		goto free;
3621038d2ef8SMaor Gottlieb 	}
3622038d2ef8SMaor Gottlieb 
3623d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
36245497adc6SMaor Gottlieb 	handler->prio = ft_prio;
3625d4be3f44SYishai Hadas 	handler->dev = dev;
3626038d2ef8SMaor Gottlieb 
3627038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
3628038d2ef8SMaor Gottlieb free:
36293b3233fbSRaed Salem 	if (err && handler) {
36303b3233fbSRaed Salem 		if (handler->ibcounters &&
36313b3233fbSRaed Salem 		    atomic_read(&handler->ibcounters->usecnt) == 1)
36323b3233fbSRaed Salem 			counters_clear_description(handler->ibcounters);
3633038d2ef8SMaor Gottlieb 		kfree(handler);
36343b3233fbSRaed Salem 	}
3635c5bb1730SMaor Gottlieb 	kvfree(spec);
3636038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
3637038d2ef8SMaor Gottlieb }
3638038d2ef8SMaor Gottlieb 
3639a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3640a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
3641a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
3642a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
3643a550ddfcSYishai Hadas {
36443b3233fbSRaed Salem 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
3645a550ddfcSYishai Hadas }
3646a550ddfcSYishai Hadas 
364735d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
364835d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
364935d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
365035d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
365135d19011SMaor Gottlieb {
365235d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
365335d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
365435d19011SMaor Gottlieb 
365535d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
365635d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
365735d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
365835d19011SMaor Gottlieb 					       flow_attr, dst);
365935d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
366074491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3661d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
366235d19011SMaor Gottlieb 			kfree(handler);
366335d19011SMaor Gottlieb 			handler = handler_dst;
366435d19011SMaor Gottlieb 		} else {
366535d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
366635d19011SMaor Gottlieb 		}
366735d19011SMaor Gottlieb 	}
366835d19011SMaor Gottlieb 
366935d19011SMaor Gottlieb 	return handler;
367035d19011SMaor Gottlieb }
3671038d2ef8SMaor Gottlieb enum {
3672038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
3673038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
3674038d2ef8SMaor Gottlieb };
3675038d2ef8SMaor Gottlieb 
3676038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3677038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
3678038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
3679038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
3680038d2ef8SMaor Gottlieb {
3681038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
3682038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3683038d2ef8SMaor Gottlieb 
3684038d2ef8SMaor Gottlieb 	static struct {
3685038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
3686038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
3687038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
3688038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
3689038d2ef8SMaor Gottlieb 			.flow_attr = {
3690038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3691038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3692038d2ef8SMaor Gottlieb 			},
3693038d2ef8SMaor Gottlieb 			.eth_flow = {
3694038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3695038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3696038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3697038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
3698038d2ef8SMaor Gottlieb 			}
3699038d2ef8SMaor Gottlieb 		},
3700038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
3701038d2ef8SMaor Gottlieb 			.flow_attr = {
3702038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
3703038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
3704038d2ef8SMaor Gottlieb 			},
3705038d2ef8SMaor Gottlieb 			.eth_flow = {
3706038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
3707038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
3708038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
3709038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
3710038d2ef8SMaor Gottlieb 			}
3711038d2ef8SMaor Gottlieb 		}
3712038d2ef8SMaor Gottlieb 	};
3713038d2ef8SMaor Gottlieb 
3714038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
3715038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
3716038d2ef8SMaor Gottlieb 				   dst);
3717038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
3718038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3719038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
3720038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3721038d2ef8SMaor Gottlieb 						 dst);
3722038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
372374491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
3724d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
3725038d2ef8SMaor Gottlieb 			kfree(handler);
3726038d2ef8SMaor Gottlieb 			handler = handler_ucast;
3727038d2ef8SMaor Gottlieb 		} else {
3728038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
3729038d2ef8SMaor Gottlieb 		}
3730038d2ef8SMaor Gottlieb 	}
3731038d2ef8SMaor Gottlieb 
3732038d2ef8SMaor Gottlieb 	return handler;
3733038d2ef8SMaor Gottlieb }
3734038d2ef8SMaor Gottlieb 
3735cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3736cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
3737cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
3738cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
3739cc0e5d42SMaor Gottlieb {
3740cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
3741cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
3742cc0e5d42SMaor Gottlieb 	int err;
3743cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
3744cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
3745cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
3746cc0e5d42SMaor Gottlieb 	};
3747cc0e5d42SMaor Gottlieb 
3748cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3749cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
3750cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
3751cc0e5d42SMaor Gottlieb 		goto err;
3752cc0e5d42SMaor Gottlieb 	}
3753cc0e5d42SMaor Gottlieb 
3754cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3755cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
3756cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
3757cc0e5d42SMaor Gottlieb 		goto err_tx;
3758cc0e5d42SMaor Gottlieb 	}
3759cc0e5d42SMaor Gottlieb 
3760cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
3761cc0e5d42SMaor Gottlieb 
3762cc0e5d42SMaor Gottlieb 	return handler_rx;
3763cc0e5d42SMaor Gottlieb 
3764cc0e5d42SMaor Gottlieb err_tx:
376574491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
3766cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
3767cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
3768cc0e5d42SMaor Gottlieb err:
3769cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
3770cc0e5d42SMaor Gottlieb }
3771cc0e5d42SMaor Gottlieb 
3772038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3773038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
377459082a32SMatan Barak 					   int domain,
377559082a32SMatan Barak 					   struct ib_udata *udata)
3776038d2ef8SMaor Gottlieb {
3777038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3778d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
3779038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
3780038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
3781cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
3782038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
3783802c2125SAviad Yehezkel 	bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
37843b3233fbSRaed Salem 	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
37853b3233fbSRaed Salem 	size_t min_ucmd_sz, required_ucmd_sz;
3786038d2ef8SMaor Gottlieb 	int err;
3787a550ddfcSYishai Hadas 	int underlay_qpn;
3788038d2ef8SMaor Gottlieb 
37893b3233fbSRaed Salem 	if (udata && udata->inlen) {
37903b3233fbSRaed Salem 		min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
37913b3233fbSRaed Salem 				sizeof(ucmd_hdr.reserved);
37923b3233fbSRaed Salem 		if (udata->inlen < min_ucmd_sz)
379359082a32SMatan Barak 			return ERR_PTR(-EOPNOTSUPP);
379459082a32SMatan Barak 
37953b3233fbSRaed Salem 		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
37963b3233fbSRaed Salem 		if (err)
37973b3233fbSRaed Salem 			return ERR_PTR(err);
37983b3233fbSRaed Salem 
37993b3233fbSRaed Salem 		/* currently supports only one counters data */
38003b3233fbSRaed Salem 		if (ucmd_hdr.ncounters_data > 1)
38013b3233fbSRaed Salem 			return ERR_PTR(-EINVAL);
38023b3233fbSRaed Salem 
38033b3233fbSRaed Salem 		required_ucmd_sz = min_ucmd_sz +
38043b3233fbSRaed Salem 			sizeof(struct mlx5_ib_flow_counters_data) *
38053b3233fbSRaed Salem 			ucmd_hdr.ncounters_data;
38063b3233fbSRaed Salem 		if (udata->inlen > required_ucmd_sz &&
38073b3233fbSRaed Salem 		    !ib_is_udata_cleared(udata, required_ucmd_sz,
38083b3233fbSRaed Salem 					 udata->inlen - required_ucmd_sz))
38093b3233fbSRaed Salem 			return ERR_PTR(-EOPNOTSUPP);
38103b3233fbSRaed Salem 
38113b3233fbSRaed Salem 		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
38123b3233fbSRaed Salem 		if (!ucmd)
38133b3233fbSRaed Salem 			return ERR_PTR(-ENOMEM);
38143b3233fbSRaed Salem 
38153b3233fbSRaed Salem 		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
3816299eafeeSGustavo A. R. Silva 		if (err)
3817299eafeeSGustavo A. R. Silva 			goto free_ucmd;
38183b3233fbSRaed Salem 	}
38193b3233fbSRaed Salem 
3820299eafeeSGustavo A. R. Silva 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3821299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3822299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3823299eafeeSGustavo A. R. Silva 	}
3824038d2ef8SMaor Gottlieb 
3825038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
3826508562d6SDaniel Jurgens 	    flow_attr->port > dev->num_ports ||
3827802c2125SAviad Yehezkel 	    (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3828299eafeeSGustavo A. R. Silva 				  IB_FLOW_ATTR_FLAGS_EGRESS))) {
3829299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3830299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3831299eafeeSGustavo A. R. Silva 	}
3832802c2125SAviad Yehezkel 
3833802c2125SAviad Yehezkel 	if (is_egress &&
3834802c2125SAviad Yehezkel 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3835299eafeeSGustavo A. R. Silva 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3836299eafeeSGustavo A. R. Silva 		err = -EINVAL;
3837299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3838299eafeeSGustavo A. R. Silva 	}
3839038d2ef8SMaor Gottlieb 
3840038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3841299eafeeSGustavo A. R. Silva 	if (!dst) {
3842299eafeeSGustavo A. R. Silva 		err = -ENOMEM;
3843299eafeeSGustavo A. R. Silva 		goto free_ucmd;
3844299eafeeSGustavo A. R. Silva 	}
3845038d2ef8SMaor Gottlieb 
38469a4ca38dSMark Bloch 	mutex_lock(&dev->flow_db->lock);
3847038d2ef8SMaor Gottlieb 
3848802c2125SAviad Yehezkel 	ft_prio = get_flow_table(dev, flow_attr,
3849802c2125SAviad Yehezkel 				 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
3850038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
3851038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
3852038d2ef8SMaor Gottlieb 		goto unlock;
3853038d2ef8SMaor Gottlieb 	}
3854cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3855cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3856cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
3857cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
3858cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
3859cc0e5d42SMaor Gottlieb 			goto destroy_ft;
3860cc0e5d42SMaor Gottlieb 		}
3861cc0e5d42SMaor Gottlieb 	}
3862038d2ef8SMaor Gottlieb 
3863802c2125SAviad Yehezkel 	if (is_egress) {
3864802c2125SAviad Yehezkel 		dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3865802c2125SAviad Yehezkel 	} else {
3866038d2ef8SMaor Gottlieb 		dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3867d9f88e5aSYishai Hadas 		if (mqp->flags & MLX5_IB_QP_RSS)
3868d9f88e5aSYishai Hadas 			dst->tir_num = mqp->rss_qp.tirn;
3869d9f88e5aSYishai Hadas 		else
3870d9f88e5aSYishai Hadas 			dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3871802c2125SAviad Yehezkel 	}
3872038d2ef8SMaor Gottlieb 
3873038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
387435d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
387535d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
387635d19011SMaor Gottlieb 							flow_attr, dst);
387735d19011SMaor Gottlieb 		} else {
3878a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3879a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
3880a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
38813b3233fbSRaed Salem 						    dst, underlay_qpn, ucmd);
388235d19011SMaor Gottlieb 		}
3883038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3884038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3885038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3886038d2ef8SMaor Gottlieb 						dst);
3887cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3888cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
3889038d2ef8SMaor Gottlieb 	} else {
3890038d2ef8SMaor Gottlieb 		err = -EINVAL;
3891038d2ef8SMaor Gottlieb 		goto destroy_ft;
3892038d2ef8SMaor Gottlieb 	}
3893038d2ef8SMaor Gottlieb 
3894038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
3895038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
3896038d2ef8SMaor Gottlieb 		handler = NULL;
3897038d2ef8SMaor Gottlieb 		goto destroy_ft;
3898038d2ef8SMaor Gottlieb 	}
3899038d2ef8SMaor Gottlieb 
39009a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3901038d2ef8SMaor Gottlieb 	kfree(dst);
39023b3233fbSRaed Salem 	kfree(ucmd);
3903038d2ef8SMaor Gottlieb 
3904038d2ef8SMaor Gottlieb 	return &handler->ibflow;
3905038d2ef8SMaor Gottlieb 
3906038d2ef8SMaor Gottlieb destroy_ft:
3907038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
3908cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
3909cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
3910038d2ef8SMaor Gottlieb unlock:
39119a4ca38dSMark Bloch 	mutex_unlock(&dev->flow_db->lock);
3912038d2ef8SMaor Gottlieb 	kfree(dst);
3913299eafeeSGustavo A. R. Silva free_ucmd:
39143b3233fbSRaed Salem 	kfree(ucmd);
3915038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
3916038d2ef8SMaor Gottlieb }
3917038d2ef8SMaor Gottlieb 
3918b47fd4ffSMark Bloch static struct mlx5_ib_flow_prio *
3919b47fd4ffSMark Bloch _get_flow_table(struct mlx5_ib_dev *dev,
3920b47fd4ffSMark Bloch 		struct mlx5_ib_flow_matcher *fs_matcher,
3921b47fd4ffSMark Bloch 		bool mcast)
3922d4be3f44SYishai Hadas {
3923d4be3f44SYishai Hadas 	struct mlx5_flow_namespace *ns = NULL;
392413a43765SMark Bloch 	struct mlx5_ib_flow_prio *prio = NULL;
392513a43765SMark Bloch 	int max_table_size = 0;
3926b47fd4ffSMark Bloch 	u32 flags = 0;
3927b47fd4ffSMark Bloch 	int priority;
3928d4be3f44SYishai Hadas 
392913a43765SMark Bloch 	if (mcast)
393013a43765SMark Bloch 		priority = MLX5_IB_FLOW_MCAST_PRIO;
393113a43765SMark Bloch 	else
393213a43765SMark Bloch 		priority = ib_prio_to_core_prio(fs_matcher->priority, false);
393313a43765SMark Bloch 
3934b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
3935d4be3f44SYishai Hadas 		max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3936d4be3f44SYishai Hadas 					log_max_ft_size));
3937b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3938b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3939b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3940b47fd4ffSMark Bloch 					      reformat_l3_tunnel_to_l2))
3941b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
394213a43765SMark Bloch 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS) {
394313a43765SMark Bloch 		max_table_size = BIT(
394413a43765SMark Bloch 			MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, log_max_ft_size));
3945b47fd4ffSMark Bloch 		if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3946b47fd4ffSMark Bloch 			flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
394713a43765SMark Bloch 	} else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB) {
394813a43765SMark Bloch 		max_table_size = BIT(
394913a43765SMark Bloch 			MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, log_max_ft_size));
395013a43765SMark Bloch 		priority = FDB_BYPASS_PATH;
3951b47fd4ffSMark Bloch 	}
3952b47fd4ffSMark Bloch 
39533b70508aSMark Bloch 	max_table_size = min_t(int, max_table_size, MLX5_FS_MAX_ENTRIES);
3954d4be3f44SYishai Hadas 
3955b47fd4ffSMark Bloch 	ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
3956d4be3f44SYishai Hadas 	if (!ns)
3957d4be3f44SYishai Hadas 		return ERR_PTR(-ENOTSUPP);
3958d4be3f44SYishai Hadas 
3959b47fd4ffSMark Bloch 	if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
3960d4be3f44SYishai Hadas 		prio = &dev->flow_db->prios[priority];
396113a43765SMark Bloch 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS)
3962b47fd4ffSMark Bloch 		prio = &dev->flow_db->egress_prios[priority];
396313a43765SMark Bloch 	else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB)
396413a43765SMark Bloch 		prio = &dev->flow_db->fdb;
396513a43765SMark Bloch 
396613a43765SMark Bloch 	if (!prio)
396713a43765SMark Bloch 		return ERR_PTR(-EINVAL);
3968d4be3f44SYishai Hadas 
3969d4be3f44SYishai Hadas 	if (prio->flow_table)
3970d4be3f44SYishai Hadas 		return prio;
3971d4be3f44SYishai Hadas 
39723b70508aSMark Bloch 	return _get_prio(ns, prio, priority, max_table_size,
3973b47fd4ffSMark Bloch 			 MLX5_FS_MAX_TYPES, flags);
3974d4be3f44SYishai Hadas }
3975d4be3f44SYishai Hadas 
3976d4be3f44SYishai Hadas static struct mlx5_ib_flow_handler *
3977d4be3f44SYishai Hadas _create_raw_flow_rule(struct mlx5_ib_dev *dev,
3978d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_prio *ft_prio,
3979d4be3f44SYishai Hadas 		      struct mlx5_flow_destination *dst,
3980d4be3f44SYishai Hadas 		      struct mlx5_ib_flow_matcher  *fs_matcher,
3981bb0ee7dcSJianbo Liu 		      struct mlx5_flow_context *flow_context,
3982b823dd6dSMark Bloch 		      struct mlx5_flow_act *flow_act,
3983bfc5d839SMark Bloch 		      void *cmd_in, int inlen,
3984bfc5d839SMark Bloch 		      int dst_num)
3985d4be3f44SYishai Hadas {
3986d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
3987d4be3f44SYishai Hadas 	struct mlx5_flow_spec *spec;
3988d4be3f44SYishai Hadas 	struct mlx5_flow_table *ft = ft_prio->flow_table;
3989d4be3f44SYishai Hadas 	int err = 0;
3990d4be3f44SYishai Hadas 
3991d4be3f44SYishai Hadas 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3992d4be3f44SYishai Hadas 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3993d4be3f44SYishai Hadas 	if (!handler || !spec) {
3994d4be3f44SYishai Hadas 		err = -ENOMEM;
3995d4be3f44SYishai Hadas 		goto free;
3996d4be3f44SYishai Hadas 	}
3997d4be3f44SYishai Hadas 
3998d4be3f44SYishai Hadas 	INIT_LIST_HEAD(&handler->list);
3999d4be3f44SYishai Hadas 
4000d4be3f44SYishai Hadas 	memcpy(spec->match_value, cmd_in, inlen);
4001d4be3f44SYishai Hadas 	memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
4002d4be3f44SYishai Hadas 	       fs_matcher->mask_len);
4003d4be3f44SYishai Hadas 	spec->match_criteria_enable = fs_matcher->match_criteria_enable;
4004bb0ee7dcSJianbo Liu 	spec->flow_context = *flow_context;
4005d4be3f44SYishai Hadas 
4006d4be3f44SYishai Hadas 	handler->rule = mlx5_add_flow_rules(ft, spec,
4007bfc5d839SMark Bloch 					    flow_act, dst, dst_num);
4008d4be3f44SYishai Hadas 
4009d4be3f44SYishai Hadas 	if (IS_ERR(handler->rule)) {
4010d4be3f44SYishai Hadas 		err = PTR_ERR(handler->rule);
4011d4be3f44SYishai Hadas 		goto free;
4012d4be3f44SYishai Hadas 	}
4013d4be3f44SYishai Hadas 
4014d4be3f44SYishai Hadas 	ft_prio->refcount++;
4015d4be3f44SYishai Hadas 	handler->prio = ft_prio;
4016d4be3f44SYishai Hadas 	handler->dev = dev;
4017d4be3f44SYishai Hadas 	ft_prio->flow_table = ft;
4018d4be3f44SYishai Hadas 
4019d4be3f44SYishai Hadas free:
4020d4be3f44SYishai Hadas 	if (err)
4021d4be3f44SYishai Hadas 		kfree(handler);
4022d4be3f44SYishai Hadas 	kvfree(spec);
4023d4be3f44SYishai Hadas 	return err ? ERR_PTR(err) : handler;
4024d4be3f44SYishai Hadas }
4025d4be3f44SYishai Hadas 
4026d4be3f44SYishai Hadas static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
4027d4be3f44SYishai Hadas 				void *match_v)
4028d4be3f44SYishai Hadas {
4029d4be3f44SYishai Hadas 	void *match_c;
4030d4be3f44SYishai Hadas 	void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
4031d4be3f44SYishai Hadas 	void *dmac, *dmac_mask;
4032d4be3f44SYishai Hadas 	void *ipv4, *ipv4_mask;
4033d4be3f44SYishai Hadas 
4034d4be3f44SYishai Hadas 	if (!(fs_matcher->match_criteria_enable &
4035d4be3f44SYishai Hadas 	      (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
4036d4be3f44SYishai Hadas 		return false;
4037d4be3f44SYishai Hadas 
4038d4be3f44SYishai Hadas 	match_c = fs_matcher->matcher_mask.match_params;
4039d4be3f44SYishai Hadas 	match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
4040d4be3f44SYishai Hadas 					   outer_headers);
4041d4be3f44SYishai Hadas 	match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
4042d4be3f44SYishai Hadas 					   outer_headers);
4043d4be3f44SYishai Hadas 
4044d4be3f44SYishai Hadas 	dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4045d4be3f44SYishai Hadas 			    dmac_47_16);
4046d4be3f44SYishai Hadas 	dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4047d4be3f44SYishai Hadas 				 dmac_47_16);
4048d4be3f44SYishai Hadas 
4049d4be3f44SYishai Hadas 	if (is_multicast_ether_addr(dmac) &&
4050d4be3f44SYishai Hadas 	    is_multicast_ether_addr(dmac_mask))
4051d4be3f44SYishai Hadas 		return true;
4052d4be3f44SYishai Hadas 
4053d4be3f44SYishai Hadas 	ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4054d4be3f44SYishai Hadas 			    dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4055d4be3f44SYishai Hadas 
4056d4be3f44SYishai Hadas 	ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4057d4be3f44SYishai Hadas 				 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4058d4be3f44SYishai Hadas 
4059d4be3f44SYishai Hadas 	if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
4060d4be3f44SYishai Hadas 	    ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
4061d4be3f44SYishai Hadas 		return true;
4062d4be3f44SYishai Hadas 
4063d4be3f44SYishai Hadas 	return false;
4064d4be3f44SYishai Hadas }
4065d4be3f44SYishai Hadas 
406632269441SYishai Hadas struct mlx5_ib_flow_handler *
406732269441SYishai Hadas mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
406832269441SYishai Hadas 			struct mlx5_ib_flow_matcher *fs_matcher,
4069bb0ee7dcSJianbo Liu 			struct mlx5_flow_context *flow_context,
4070b823dd6dSMark Bloch 			struct mlx5_flow_act *flow_act,
4071bfc5d839SMark Bloch 			u32 counter_id,
407232269441SYishai Hadas 			void *cmd_in, int inlen, int dest_id,
407332269441SYishai Hadas 			int dest_type)
407432269441SYishai Hadas {
4075d4be3f44SYishai Hadas 	struct mlx5_flow_destination *dst;
4076d4be3f44SYishai Hadas 	struct mlx5_ib_flow_prio *ft_prio;
4077d4be3f44SYishai Hadas 	struct mlx5_ib_flow_handler *handler;
4078bfc5d839SMark Bloch 	int dst_num = 0;
4079d4be3f44SYishai Hadas 	bool mcast;
4080d4be3f44SYishai Hadas 	int err;
4081d4be3f44SYishai Hadas 
4082d4be3f44SYishai Hadas 	if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
408332269441SYishai Hadas 		return ERR_PTR(-EOPNOTSUPP);
4084d4be3f44SYishai Hadas 
4085d4be3f44SYishai Hadas 	if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
4086d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
4087d4be3f44SYishai Hadas 
40888e8aa145SGustavo A. R. Silva 	dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
4089d4be3f44SYishai Hadas 	if (!dst)
4090d4be3f44SYishai Hadas 		return ERR_PTR(-ENOMEM);
4091d4be3f44SYishai Hadas 
4092d4be3f44SYishai Hadas 	mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
4093d4be3f44SYishai Hadas 	mutex_lock(&dev->flow_db->lock);
4094d4be3f44SYishai Hadas 
4095b47fd4ffSMark Bloch 	ft_prio = _get_flow_table(dev, fs_matcher, mcast);
4096d4be3f44SYishai Hadas 	if (IS_ERR(ft_prio)) {
4097d4be3f44SYishai Hadas 		err = PTR_ERR(ft_prio);
4098d4be3f44SYishai Hadas 		goto unlock;
4099d4be3f44SYishai Hadas 	}
4100d4be3f44SYishai Hadas 
41016346f0bfSYishai Hadas 	if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
4102bfc5d839SMark Bloch 		dst[dst_num].type = dest_type;
4103bfc5d839SMark Bloch 		dst[dst_num].tir_num = dest_id;
4104b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
4105a7ee18bdSMark Bloch 	} else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
4106bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
4107bfc5d839SMark Bloch 		dst[dst_num].ft_num = dest_id;
4108b823dd6dSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
4109a7ee18bdSMark Bloch 	} else {
4110bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
4111a7ee18bdSMark Bloch 		flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
41126346f0bfSYishai Hadas 	}
41136346f0bfSYishai Hadas 
4114bfc5d839SMark Bloch 	dst_num++;
4115bfc5d839SMark Bloch 
4116bfc5d839SMark Bloch 	if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
4117bfc5d839SMark Bloch 		dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
4118bfc5d839SMark Bloch 		dst[dst_num].counter_id = counter_id;
4119bfc5d839SMark Bloch 		dst_num++;
4120bfc5d839SMark Bloch 	}
4121bfc5d839SMark Bloch 
4122bb0ee7dcSJianbo Liu 	handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher,
4123bb0ee7dcSJianbo Liu 					flow_context, flow_act,
4124bfc5d839SMark Bloch 					cmd_in, inlen, dst_num);
4125d4be3f44SYishai Hadas 
4126d4be3f44SYishai Hadas 	if (IS_ERR(handler)) {
4127d4be3f44SYishai Hadas 		err = PTR_ERR(handler);
4128d4be3f44SYishai Hadas 		goto destroy_ft;
4129d4be3f44SYishai Hadas 	}
4130d4be3f44SYishai Hadas 
4131d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
4132d4be3f44SYishai Hadas 	atomic_inc(&fs_matcher->usecnt);
4133d4be3f44SYishai Hadas 	handler->flow_matcher = fs_matcher;
4134d4be3f44SYishai Hadas 
4135d4be3f44SYishai Hadas 	kfree(dst);
4136d4be3f44SYishai Hadas 
4137d4be3f44SYishai Hadas 	return handler;
4138d4be3f44SYishai Hadas 
4139d4be3f44SYishai Hadas destroy_ft:
4140d4be3f44SYishai Hadas 	put_flow_table(dev, ft_prio, false);
4141d4be3f44SYishai Hadas unlock:
4142d4be3f44SYishai Hadas 	mutex_unlock(&dev->flow_db->lock);
4143d4be3f44SYishai Hadas 	kfree(dst);
4144d4be3f44SYishai Hadas 
4145d4be3f44SYishai Hadas 	return ERR_PTR(err);
414632269441SYishai Hadas }
414732269441SYishai Hadas 
4148c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
4149c6475a0bSAviad Yehezkel {
4150c6475a0bSAviad Yehezkel 	u32 flags = 0;
4151c6475a0bSAviad Yehezkel 
4152c6475a0bSAviad Yehezkel 	if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
4153c6475a0bSAviad Yehezkel 		flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
4154c6475a0bSAviad Yehezkel 
4155c6475a0bSAviad Yehezkel 	return flags;
4156c6475a0bSAviad Yehezkel }
4157c6475a0bSAviad Yehezkel 
4158c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED	MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
4159c6475a0bSAviad Yehezkel static struct ib_flow_action *
4160c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device,
4161c6475a0bSAviad Yehezkel 			       const struct ib_flow_action_attrs_esp *attr,
4162c6475a0bSAviad Yehezkel 			       struct uverbs_attr_bundle *attrs)
4163c6475a0bSAviad Yehezkel {
4164c6475a0bSAviad Yehezkel 	struct mlx5_ib_dev *mdev = to_mdev(device);
4165c6475a0bSAviad Yehezkel 	struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
4166c6475a0bSAviad Yehezkel 	struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
4167c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *action;
4168c6475a0bSAviad Yehezkel 	u64 action_flags;
4169c6475a0bSAviad Yehezkel 	u64 flags;
4170c6475a0bSAviad Yehezkel 	int err = 0;
4171c6475a0bSAviad Yehezkel 
4172bccd0622SJason Gunthorpe 	err = uverbs_get_flags64(
4173bccd0622SJason Gunthorpe 		&action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4174bccd0622SJason Gunthorpe 		((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
4175bccd0622SJason Gunthorpe 	if (err)
4176bccd0622SJason Gunthorpe 		return ERR_PTR(err);
4177c6475a0bSAviad Yehezkel 
4178c6475a0bSAviad Yehezkel 	flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
4179c6475a0bSAviad Yehezkel 
4180c6475a0bSAviad Yehezkel 	/* We current only support a subset of the standard features. Only a
4181c6475a0bSAviad Yehezkel 	 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
4182c6475a0bSAviad Yehezkel 	 * (with overlap). Full offload mode isn't supported.
4183c6475a0bSAviad Yehezkel 	 */
4184c6475a0bSAviad Yehezkel 	if (!attr->keymat || attr->replay || attr->encap ||
4185c6475a0bSAviad Yehezkel 	    attr->spi || attr->seq || attr->tfc_pad ||
4186c6475a0bSAviad Yehezkel 	    attr->hard_limit_pkts ||
4187c6475a0bSAviad Yehezkel 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4188c6475a0bSAviad Yehezkel 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
4189c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4190c6475a0bSAviad Yehezkel 
4191c6475a0bSAviad Yehezkel 	if (attr->keymat->protocol !=
4192c6475a0bSAviad Yehezkel 	    IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
4193c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4194c6475a0bSAviad Yehezkel 
4195c6475a0bSAviad Yehezkel 	aes_gcm = &attr->keymat->keymat.aes_gcm;
4196c6475a0bSAviad Yehezkel 
4197c6475a0bSAviad Yehezkel 	if (aes_gcm->icv_len != 16 ||
4198c6475a0bSAviad Yehezkel 	    aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
4199c6475a0bSAviad Yehezkel 		return ERR_PTR(-EOPNOTSUPP);
4200c6475a0bSAviad Yehezkel 
4201c6475a0bSAviad Yehezkel 	action = kmalloc(sizeof(*action), GFP_KERNEL);
4202c6475a0bSAviad Yehezkel 	if (!action)
4203c6475a0bSAviad Yehezkel 		return ERR_PTR(-ENOMEM);
4204c6475a0bSAviad Yehezkel 
4205c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4206c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
4207c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.aes_key));
4208c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
4209c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
4210c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.salt));
4211c6475a0bSAviad Yehezkel 	memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
4212c6475a0bSAviad Yehezkel 	       sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
4213c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
4214c6475a0bSAviad Yehezkel 	accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
4215c6475a0bSAviad Yehezkel 	accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
4216c6475a0bSAviad Yehezkel 
4217c6475a0bSAviad Yehezkel 	accel_attrs.esn = attr->esn;
4218c6475a0bSAviad Yehezkel 	if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4219c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4220c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4221c6475a0bSAviad Yehezkel 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4222c6475a0bSAviad Yehezkel 
4223c6475a0bSAviad Yehezkel 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4224c6475a0bSAviad Yehezkel 		accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4225c6475a0bSAviad Yehezkel 
4226c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ctx =
4227c6475a0bSAviad Yehezkel 		mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4228c6475a0bSAviad Yehezkel 	if (IS_ERR(action->esp_aes_gcm.ctx)) {
4229c6475a0bSAviad Yehezkel 		err = PTR_ERR(action->esp_aes_gcm.ctx);
4230c6475a0bSAviad Yehezkel 		goto err_parse;
4231c6475a0bSAviad Yehezkel 	}
4232c6475a0bSAviad Yehezkel 
4233c6475a0bSAviad Yehezkel 	action->esp_aes_gcm.ib_flags = attr->flags;
4234c6475a0bSAviad Yehezkel 
4235c6475a0bSAviad Yehezkel 	return &action->ib_action;
4236c6475a0bSAviad Yehezkel 
4237c6475a0bSAviad Yehezkel err_parse:
4238c6475a0bSAviad Yehezkel 	kfree(action);
4239c6475a0bSAviad Yehezkel 	return ERR_PTR(err);
4240c6475a0bSAviad Yehezkel }
4241c6475a0bSAviad Yehezkel 
4242349705c1SMatan Barak static int
4243349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4244349705c1SMatan Barak 			       const struct ib_flow_action_attrs_esp *attr,
4245349705c1SMatan Barak 			       struct uverbs_attr_bundle *attrs)
4246349705c1SMatan Barak {
4247349705c1SMatan Barak 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4248349705c1SMatan Barak 	struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4249349705c1SMatan Barak 	int err = 0;
4250349705c1SMatan Barak 
4251349705c1SMatan Barak 	if (attr->keymat || attr->replay || attr->encap ||
4252349705c1SMatan Barak 	    attr->spi || attr->seq || attr->tfc_pad ||
4253349705c1SMatan Barak 	    attr->hard_limit_pkts ||
4254349705c1SMatan Barak 	    (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4255349705c1SMatan Barak 			     IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4256349705c1SMatan Barak 			     IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4257349705c1SMatan Barak 		return -EOPNOTSUPP;
4258349705c1SMatan Barak 
4259349705c1SMatan Barak 	/* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4260349705c1SMatan Barak 	 * be modified.
4261349705c1SMatan Barak 	 */
4262349705c1SMatan Barak 	if (!(maction->esp_aes_gcm.ib_flags &
4263349705c1SMatan Barak 	      IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4264349705c1SMatan Barak 	    attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4265349705c1SMatan Barak 			   IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4266349705c1SMatan Barak 		return -EINVAL;
4267349705c1SMatan Barak 
4268349705c1SMatan Barak 	memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4269349705c1SMatan Barak 	       sizeof(accel_attrs));
4270349705c1SMatan Barak 
4271349705c1SMatan Barak 	accel_attrs.esn = attr->esn;
4272349705c1SMatan Barak 	if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4273349705c1SMatan Barak 		accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4274349705c1SMatan Barak 	else
4275349705c1SMatan Barak 		accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4276349705c1SMatan Barak 
4277349705c1SMatan Barak 	err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4278349705c1SMatan Barak 					 &accel_attrs);
4279349705c1SMatan Barak 	if (err)
4280349705c1SMatan Barak 		return err;
4281349705c1SMatan Barak 
4282349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags &=
4283349705c1SMatan Barak 		~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4284349705c1SMatan Barak 	maction->esp_aes_gcm.ib_flags |=
4285349705c1SMatan Barak 		attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4286349705c1SMatan Barak 
4287349705c1SMatan Barak 	return 0;
4288349705c1SMatan Barak }
4289349705c1SMatan Barak 
4290c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4291c6475a0bSAviad Yehezkel {
4292c6475a0bSAviad Yehezkel 	struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4293c6475a0bSAviad Yehezkel 
4294c6475a0bSAviad Yehezkel 	switch (action->type) {
4295c6475a0bSAviad Yehezkel 	case IB_FLOW_ACTION_ESP:
4296c6475a0bSAviad Yehezkel 		/*
4297c6475a0bSAviad Yehezkel 		 * We only support aes_gcm by now, so we implicitly know this is
4298c6475a0bSAviad Yehezkel 		 * the underline crypto.
4299c6475a0bSAviad Yehezkel 		 */
4300c6475a0bSAviad Yehezkel 		mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4301c6475a0bSAviad Yehezkel 		break;
4302b4749bf2SMark Bloch 	case IB_FLOW_ACTION_UNSPECIFIED:
4303b4749bf2SMark Bloch 		mlx5_ib_destroy_flow_action_raw(maction);
4304b4749bf2SMark Bloch 		break;
4305c6475a0bSAviad Yehezkel 	default:
4306c6475a0bSAviad Yehezkel 		WARN_ON(true);
4307c6475a0bSAviad Yehezkel 		break;
4308c6475a0bSAviad Yehezkel 	}
4309c6475a0bSAviad Yehezkel 
4310c6475a0bSAviad Yehezkel 	kfree(maction);
4311c6475a0bSAviad Yehezkel 	return 0;
4312c6475a0bSAviad Yehezkel }
4313c6475a0bSAviad Yehezkel 
4314e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4315e126ba97SEli Cohen {
4316e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
431781e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
4318e126ba97SEli Cohen 	int err;
4319539ec982SYishai Hadas 	u16 uid;
4320539ec982SYishai Hadas 
4321539ec982SYishai Hadas 	uid = ibqp->pd ?
4322539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4323e126ba97SEli Cohen 
432481e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
432581e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
432681e30880SYishai Hadas 		return -EOPNOTSUPP;
432781e30880SYishai Hadas 	}
432881e30880SYishai Hadas 
4329539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4330e126ba97SEli Cohen 	if (err)
4331e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4332e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4333e126ba97SEli Cohen 
4334e126ba97SEli Cohen 	return err;
4335e126ba97SEli Cohen }
4336e126ba97SEli Cohen 
4337e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4338e126ba97SEli Cohen {
4339e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4340e126ba97SEli Cohen 	int err;
4341539ec982SYishai Hadas 	u16 uid;
4342e126ba97SEli Cohen 
4343539ec982SYishai Hadas 	uid = ibqp->pd ?
4344539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
4345539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
4346e126ba97SEli Cohen 	if (err)
4347e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4348e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
4349e126ba97SEli Cohen 
4350e126ba97SEli Cohen 	return err;
4351e126ba97SEli Cohen }
4352e126ba97SEli Cohen 
4353e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
4354e126ba97SEli Cohen {
43551b5daf11SMajd Dibbiny 	int err;
4356e126ba97SEli Cohen 
43571b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
4358e126ba97SEli Cohen 	if (err)
4359e126ba97SEli Cohen 		return err;
43601b5daf11SMajd Dibbiny 
43611b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
43621b5daf11SMajd Dibbiny 
43631b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
4364e126ba97SEli Cohen }
4365e126ba97SEli Cohen 
4366508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
4367508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4368e126ba97SEli Cohen {
4369e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
437054747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4371e126ba97SEli Cohen 
43729603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
4373e126ba97SEli Cohen }
4374508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
4375e126ba97SEli Cohen 
4376508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
4377e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
4378e126ba97SEli Cohen {
4379e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
438054747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4381e126ba97SEli Cohen 
43826aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
4383e126ba97SEli Cohen }
4384508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
4385e126ba97SEli Cohen 
4386508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
4387508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4388e126ba97SEli Cohen {
4389e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
439054747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
439154747231SParav Pandit 
43929603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
4393e126ba97SEli Cohen }
4394508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
4395e126ba97SEli Cohen 
4396508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
4397508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
4398e126ba97SEli Cohen {
4399e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
440054747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
440154747231SParav Pandit 
44029603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
4403e126ba97SEli Cohen }
4404508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
4405e126ba97SEli Cohen 
4406508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
4407508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
4408e126ba97SEli Cohen {
4409e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
441054747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
441154747231SParav Pandit 
4412e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
44139603b61dSJack Morgenstein 		       dev->mdev->board_id);
4414e126ba97SEli Cohen }
4415508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
4416e126ba97SEli Cohen 
4417508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
4418508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
4419508a523fSParav Pandit 	&dev_attr_hca_type.attr,
4420508a523fSParav Pandit 	&dev_attr_board_id.attr,
4421508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
4422508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
4423508a523fSParav Pandit 	NULL,
4424508a523fSParav Pandit };
4425e126ba97SEli Cohen 
4426508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
4427508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
4428e126ba97SEli Cohen };
4429e126ba97SEli Cohen 
44307722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
44317722f47eSHaggai Eran {
44327722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
44337722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
44347722f47eSHaggai Eran 			     pkey_change_work);
44357722f47eSHaggai Eran 
44367722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
44377722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
44387722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
44397722f47eSHaggai Eran }
44407722f47eSHaggai Eran 
444189ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
444289ea94a7SMaor Gottlieb {
444389ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
444489ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
444589ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
444689ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
444789ea94a7SMaor Gottlieb 	unsigned long flags_qp;
444889ea94a7SMaor Gottlieb 	unsigned long flags_cq;
444989ea94a7SMaor Gottlieb 	unsigned long flags;
445089ea94a7SMaor Gottlieb 
445189ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
445289ea94a7SMaor Gottlieb 
445389ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
445489ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
445589ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
445689ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
445789ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
445889ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
445989ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
446089ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
446189ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
446289ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
446389ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
446489ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
446589ea94a7SMaor Gottlieb 						      &cq_armed_list);
446689ea94a7SMaor Gottlieb 				}
446789ea94a7SMaor Gottlieb 			}
446889ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
446989ea94a7SMaor Gottlieb 		}
447089ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
447189ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
447289ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
447389ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
447489ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
447589ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
447689ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
447789ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
447889ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
447989ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
448089ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
448189ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
448289ea94a7SMaor Gottlieb 							      &cq_armed_list);
448389ea94a7SMaor Gottlieb 					}
448489ea94a7SMaor Gottlieb 				}
448589ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
448689ea94a7SMaor Gottlieb 						       flags_cq);
448789ea94a7SMaor Gottlieb 			}
448889ea94a7SMaor Gottlieb 		}
448989ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
449089ea94a7SMaor Gottlieb 	}
449189ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
449289ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
449389ea94a7SMaor Gottlieb 	 */
449489ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
4495*4e0e2ea1SYishai Hadas 		mcq->comp(mcq, NULL);
449689ea94a7SMaor Gottlieb 	}
449789ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
449889ea94a7SMaor Gottlieb }
449989ea94a7SMaor Gottlieb 
450003404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
450103404e8aSMaor Gottlieb {
450203404e8aSMaor Gottlieb 	int err;
450303404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
450403404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
450503404e8aSMaor Gottlieb 			     delay_drop_work);
450603404e8aSMaor Gottlieb 
4507fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
4508fe248c3aSMaor Gottlieb 
450903404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
451003404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
451103404e8aSMaor Gottlieb 				       delay_drop->timeout);
451203404e8aSMaor Gottlieb 	if (err) {
451303404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
451403404e8aSMaor Gottlieb 			     delay_drop->timeout);
451503404e8aSMaor Gottlieb 		delay_drop->activate = false;
451603404e8aSMaor Gottlieb 	}
451703404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
451803404e8aSMaor Gottlieb }
451903404e8aSMaor Gottlieb 
452009e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
452109e574faSSaeed Mahameed 				 struct ib_event *ibev)
452209e574faSSaeed Mahameed {
45236cfdc7e4SAya Levin 	u8 port = (eqe->data.port.port >> 4) & 0xf;
45246cfdc7e4SAya Levin 
452509e574faSSaeed Mahameed 	switch (eqe->sub_type) {
452609e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
45276cfdc7e4SAya Levin 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
45286cfdc7e4SAya Levin 					    IB_LINK_LAYER_ETHERNET)
452909e574faSSaeed Mahameed 			schedule_work(&ibdev->delay_drop.delay_drop_work);
453009e574faSSaeed Mahameed 		break;
453109e574faSSaeed Mahameed 	default: /* do nothing */
453209e574faSSaeed Mahameed 		return;
453309e574faSSaeed Mahameed 	}
453409e574faSSaeed Mahameed }
453509e574faSSaeed Mahameed 
4536134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4537134e9349SSaeed Mahameed 			      struct ib_event *ibev)
4538134e9349SSaeed Mahameed {
4539134e9349SSaeed Mahameed 	u8 port = (eqe->data.port.port >> 4) & 0xf;
4540134e9349SSaeed Mahameed 
4541134e9349SSaeed Mahameed 	ibev->element.port_num = port;
4542134e9349SSaeed Mahameed 
4543134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
4544134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4545134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4546134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4547134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
4548134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
4549134e9349SSaeed Mahameed 		 */
4550134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4551134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
4552134e9349SSaeed Mahameed 			return -EINVAL;
4553134e9349SSaeed Mahameed 
4554134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4555134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4556134e9349SSaeed Mahameed 		break;
4557134e9349SSaeed Mahameed 
4558134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
4559134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
4560134e9349SSaeed Mahameed 		break;
4561134e9349SSaeed Mahameed 
4562134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4563134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
4564134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4565134e9349SSaeed Mahameed 		break;
4566134e9349SSaeed Mahameed 
4567134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4568134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
4569134e9349SSaeed Mahameed 		break;
4570134e9349SSaeed Mahameed 
4571134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4572134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
4573134e9349SSaeed Mahameed 		break;
4574134e9349SSaeed Mahameed 	default:
4575134e9349SSaeed Mahameed 		return -EINVAL;
4576134e9349SSaeed Mahameed 	}
4577134e9349SSaeed Mahameed 
4578134e9349SSaeed Mahameed 	return 0;
4579134e9349SSaeed Mahameed }
4580134e9349SSaeed Mahameed 
4581d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
4582e126ba97SEli Cohen {
4583d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
4584d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
4585d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
4586e126ba97SEli Cohen 	struct ib_event ibev;
4587dbaaff2aSEli Cohen 	bool fatal = false;
4588e126ba97SEli Cohen 
4589df097a27SSaeed Mahameed 	if (work->is_slave) {
4590df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
4591d69a24e0SDaniel Jurgens 		if (!ibdev)
4592d69a24e0SDaniel Jurgens 			goto out;
4593d69a24e0SDaniel Jurgens 	} else {
4594df097a27SSaeed Mahameed 		ibdev = work->dev;
4595d69a24e0SDaniel Jurgens 	}
4596d69a24e0SDaniel Jurgens 
4597d69a24e0SDaniel Jurgens 	switch (work->event) {
4598e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
4599e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
460089ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
4601134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
4602dbaaff2aSEli Cohen 		fatal = true;
4603e126ba97SEli Cohen 		break;
4604134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
4605134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
4606d69a24e0SDaniel Jurgens 			goto out;
4607e126ba97SEli Cohen 		break;
460809e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
460909e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
461009e574faSSaeed Mahameed 		/* fall through */
4611bdc37924SSaeed Mahameed 	default:
461203404e8aSMaor Gottlieb 		goto out;
4613e126ba97SEli Cohen 	}
4614e126ba97SEli Cohen 
4615e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
4616e126ba97SEli Cohen 
4617134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4618134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
461903404e8aSMaor Gottlieb 		goto out;
4620a0c84c32SEli Cohen 	}
4621a0c84c32SEli Cohen 
4622e126ba97SEli Cohen 	if (ibdev->ib_active)
4623e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
4624dbaaff2aSEli Cohen 
4625dbaaff2aSEli Cohen 	if (fatal)
4626dbaaff2aSEli Cohen 		ibdev->ib_active = false;
462703404e8aSMaor Gottlieb out:
4628d69a24e0SDaniel Jurgens 	kfree(work);
4629d69a24e0SDaniel Jurgens }
4630d69a24e0SDaniel Jurgens 
4631df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
4632df097a27SSaeed Mahameed 			 unsigned long event, void *param)
4633d69a24e0SDaniel Jurgens {
4634d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
4635d69a24e0SDaniel Jurgens 
4636d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
463710bea9c8SLeon Romanovsky 	if (!work)
4638df097a27SSaeed Mahameed 		return NOTIFY_DONE;
463910bea9c8SLeon Romanovsky 
4640d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4641df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4642df097a27SSaeed Mahameed 	work->is_slave = false;
4643d69a24e0SDaniel Jurgens 	work->param = param;
4644d69a24e0SDaniel Jurgens 	work->event = event;
4645d69a24e0SDaniel Jurgens 
4646d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
4647df097a27SSaeed Mahameed 
4648df097a27SSaeed Mahameed 	return NOTIFY_OK;
4649df097a27SSaeed Mahameed }
4650df097a27SSaeed Mahameed 
4651df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4652df097a27SSaeed Mahameed 				    unsigned long event, void *param)
4653df097a27SSaeed Mahameed {
4654df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
4655df097a27SSaeed Mahameed 
4656df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
4657df097a27SSaeed Mahameed 	if (!work)
4658df097a27SSaeed Mahameed 		return NOTIFY_DONE;
4659df097a27SSaeed Mahameed 
4660df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
4661df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4662df097a27SSaeed Mahameed 	work->is_slave = true;
4663df097a27SSaeed Mahameed 	work->param = param;
4664df097a27SSaeed Mahameed 	work->event = event;
4665df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
4666df097a27SSaeed Mahameed 
4667df097a27SSaeed Mahameed 	return NOTIFY_OK;
4668d69a24e0SDaniel Jurgens }
4669d69a24e0SDaniel Jurgens 
4670c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4671c43f1112SMaor Gottlieb {
4672c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
4673c43f1112SMaor Gottlieb 	int err;
4674c43f1112SMaor Gottlieb 	int port;
4675c43f1112SMaor Gottlieb 
4676a989ea01SMark Bloch 	for (port = 1; port <= ARRAY_SIZE(dev->mdev->port_caps); port++) {
4677c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
4678c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4679c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
4680c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4681c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
4682c43f1112SMaor Gottlieb 								   port, 0,
4683c43f1112SMaor Gottlieb 								   &vport_ctx);
4684c43f1112SMaor Gottlieb 				if (err) {
4685c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4686c43f1112SMaor Gottlieb 						    port, err);
4687c43f1112SMaor Gottlieb 					return err;
4688c43f1112SMaor Gottlieb 				}
4689c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
4690c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
4691c43f1112SMaor Gottlieb 			} else {
4692c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
4693c43f1112SMaor Gottlieb 			}
4694c43f1112SMaor Gottlieb 		}
4695c43f1112SMaor Gottlieb 	}
4696c43f1112SMaor Gottlieb 	return 0;
4697c43f1112SMaor Gottlieb }
4698c43f1112SMaor Gottlieb 
4699e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4700e126ba97SEli Cohen {
4701e126ba97SEli Cohen 	int port;
4702e126ba97SEli Cohen 
4703508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
4704e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
4705e126ba97SEli Cohen }
4706e126ba97SEli Cohen 
470726628e2dSMark Bloch static int __get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4708e126ba97SEli Cohen {
4709e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
4710e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
4711f614fc15SDan Carpenter 	int err = -ENOMEM;
47122528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
4713e126ba97SEli Cohen 
4714e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
4715e126ba97SEli Cohen 	if (!pprops)
4716e126ba97SEli Cohen 		goto out;
4717e126ba97SEli Cohen 
4718e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4719e126ba97SEli Cohen 	if (!dprops)
4720e126ba97SEli Cohen 		goto out;
4721e126ba97SEli Cohen 
47222528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
4723e126ba97SEli Cohen 	if (err) {
4724e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
4725e126ba97SEli Cohen 		goto out;
4726e126ba97SEli Cohen 	}
4727e126ba97SEli Cohen 
4728c4550c63SOr Gerlitz 	memset(pprops, 0, sizeof(*pprops));
4729e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4730e126ba97SEli Cohen 	if (err) {
4731938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
4732938fe83cSSaeed Mahameed 			     port, err);
473332f69e4bSDaniel Jurgens 		goto out;
4734e126ba97SEli Cohen 	}
473532f69e4bSDaniel Jurgens 
4736938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
4737938fe83cSSaeed Mahameed 					dprops->max_pkeys;
4738938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
4739938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
474032f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
474132f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
4742e126ba97SEli Cohen 
4743e126ba97SEli Cohen out:
4744e126ba97SEli Cohen 	kfree(pprops);
4745e126ba97SEli Cohen 	kfree(dprops);
4746e126ba97SEli Cohen 
4747e126ba97SEli Cohen 	return err;
4748e126ba97SEli Cohen }
4749e126ba97SEli Cohen 
475026628e2dSMark Bloch static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
475126628e2dSMark Bloch {
475226628e2dSMark Bloch 	/* For representors use port 1, is this is the only native
475326628e2dSMark Bloch 	 * port
475426628e2dSMark Bloch 	 */
475526628e2dSMark Bloch 	if (dev->is_rep)
475626628e2dSMark Bloch 		return __get_port_caps(dev, 1);
475726628e2dSMark Bloch 	return __get_port_caps(dev, port);
475826628e2dSMark Bloch }
475926628e2dSMark Bloch 
4760e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4761e126ba97SEli Cohen {
4762e126ba97SEli Cohen 	int err;
4763e126ba97SEli Cohen 
4764e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
4765e126ba97SEli Cohen 	if (err)
4766e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4767e126ba97SEli Cohen 
476832927e28SMark Bloch 	if (dev->umrc.qp)
4769c4367a26SShamir Rabinovitch 		mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
477032927e28SMark Bloch 	if (dev->umrc.cq)
4771add08d76SChristoph Hellwig 		ib_free_cq(dev->umrc.cq);
477232927e28SMark Bloch 	if (dev->umrc.pd)
4773e126ba97SEli Cohen 		ib_dealloc_pd(dev->umrc.pd);
4774e126ba97SEli Cohen }
4775e126ba97SEli Cohen 
4776e126ba97SEli Cohen enum {
4777e126ba97SEli Cohen 	MAX_UMR_WR = 128,
4778e126ba97SEli Cohen };
4779e126ba97SEli Cohen 
4780e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
4781e126ba97SEli Cohen {
4782e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
4783e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
4784e126ba97SEli Cohen 	struct ib_pd *pd;
4785e126ba97SEli Cohen 	struct ib_cq *cq;
4786e126ba97SEli Cohen 	struct ib_qp *qp;
4787e126ba97SEli Cohen 	int ret;
4788e126ba97SEli Cohen 
4789e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4790e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4791e126ba97SEli Cohen 	if (!attr || !init_attr) {
4792e126ba97SEli Cohen 		ret = -ENOMEM;
4793e126ba97SEli Cohen 		goto error_0;
4794e126ba97SEli Cohen 	}
4795e126ba97SEli Cohen 
4796ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
4797e126ba97SEli Cohen 	if (IS_ERR(pd)) {
4798e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4799e126ba97SEli Cohen 		ret = PTR_ERR(pd);
4800e126ba97SEli Cohen 		goto error_0;
4801e126ba97SEli Cohen 	}
4802e126ba97SEli Cohen 
4803add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
4804e126ba97SEli Cohen 	if (IS_ERR(cq)) {
4805e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4806e126ba97SEli Cohen 		ret = PTR_ERR(cq);
4807e126ba97SEli Cohen 		goto error_2;
4808e126ba97SEli Cohen 	}
4809e126ba97SEli Cohen 
4810e126ba97SEli Cohen 	init_attr->send_cq = cq;
4811e126ba97SEli Cohen 	init_attr->recv_cq = cq;
4812e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4813e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
4814e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
4815e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4816e126ba97SEli Cohen 	init_attr->port_num = 1;
4817e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4818e126ba97SEli Cohen 	if (IS_ERR(qp)) {
4819e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4820e126ba97SEli Cohen 		ret = PTR_ERR(qp);
4821e126ba97SEli Cohen 		goto error_3;
4822e126ba97SEli Cohen 	}
4823e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
4824e126ba97SEli Cohen 	qp->real_qp    = qp;
4825e126ba97SEli Cohen 	qp->uobject    = NULL;
4826e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
482731fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
482831fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
4829e126ba97SEli Cohen 
4830e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
4831e126ba97SEli Cohen 	attr->port_num = 1;
4832e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4833e126ba97SEli Cohen 				IB_QP_PORT, NULL);
4834e126ba97SEli Cohen 	if (ret) {
4835e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4836e126ba97SEli Cohen 		goto error_4;
4837e126ba97SEli Cohen 	}
4838e126ba97SEli Cohen 
4839e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4840e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
4841e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
4842e126ba97SEli Cohen 
4843e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4844e126ba97SEli Cohen 	if (ret) {
4845e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4846e126ba97SEli Cohen 		goto error_4;
4847e126ba97SEli Cohen 	}
4848e126ba97SEli Cohen 
4849e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
4850e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
4851e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4852e126ba97SEli Cohen 	if (ret) {
4853e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4854e126ba97SEli Cohen 		goto error_4;
4855e126ba97SEli Cohen 	}
4856e126ba97SEli Cohen 
4857e126ba97SEli Cohen 	dev->umrc.qp = qp;
4858e126ba97SEli Cohen 	dev->umrc.cq = cq;
4859e126ba97SEli Cohen 	dev->umrc.pd = pd;
4860e126ba97SEli Cohen 
4861e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
4862e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
4863e126ba97SEli Cohen 	if (ret) {
4864e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4865e126ba97SEli Cohen 		goto error_4;
4866e126ba97SEli Cohen 	}
4867e126ba97SEli Cohen 
4868e126ba97SEli Cohen 	kfree(attr);
4869e126ba97SEli Cohen 	kfree(init_attr);
4870e126ba97SEli Cohen 
4871e126ba97SEli Cohen 	return 0;
4872e126ba97SEli Cohen 
4873e126ba97SEli Cohen error_4:
4874c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_qp(qp, NULL);
487532927e28SMark Bloch 	dev->umrc.qp = NULL;
4876e126ba97SEli Cohen 
4877e126ba97SEli Cohen error_3:
4878add08d76SChristoph Hellwig 	ib_free_cq(cq);
487932927e28SMark Bloch 	dev->umrc.cq = NULL;
4880e126ba97SEli Cohen 
4881e126ba97SEli Cohen error_2:
4882e126ba97SEli Cohen 	ib_dealloc_pd(pd);
488332927e28SMark Bloch 	dev->umrc.pd = NULL;
4884e126ba97SEli Cohen 
4885e126ba97SEli Cohen error_0:
4886e126ba97SEli Cohen 	kfree(attr);
4887e126ba97SEli Cohen 	kfree(init_attr);
4888e126ba97SEli Cohen 	return ret;
4889e126ba97SEli Cohen }
4890e126ba97SEli Cohen 
48916e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
48926e8484c5SMax Gurtovoy {
48936e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
48946e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
48956e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
48966e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
48976e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
48986e8484c5SMax Gurtovoy 	default:
48996e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
49006e8484c5SMax Gurtovoy 	}
49016e8484c5SMax Gurtovoy }
49026e8484c5SMax Gurtovoy 
4903e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
4904e126ba97SEli Cohen {
4905e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
4906e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
490721a428a0SLeon Romanovsky 	struct ib_device *ibdev;
4908bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
49097722f47eSHaggai Eran 	int port;
4910e126ba97SEli Cohen 	int ret = 0;
4911e126ba97SEli Cohen 
4912e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
491321a428a0SLeon Romanovsky 	ibdev = &dev->ib_dev;
4914e126ba97SEli Cohen 
4915d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
4916d16e91daSHaggai Eran 
491721a428a0SLeon Romanovsky 	devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
491821a428a0SLeon Romanovsky 	if (!devr->p0)
491921a428a0SLeon Romanovsky 		return -ENOMEM;
492021a428a0SLeon Romanovsky 
492121a428a0SLeon Romanovsky 	devr->p0->device  = ibdev;
4922e126ba97SEli Cohen 	devr->p0->uobject = NULL;
4923e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
4924e126ba97SEli Cohen 
4925ff23dfa1SShamir Rabinovitch 	ret = mlx5_ib_alloc_pd(devr->p0, NULL);
492621a428a0SLeon Romanovsky 	if (ret)
492721a428a0SLeon Romanovsky 		goto error0;
492821a428a0SLeon Romanovsky 
4929ff23dfa1SShamir Rabinovitch 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL);
4930e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
4931e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
4932e126ba97SEli Cohen 		goto error1;
4933e126ba97SEli Cohen 	}
4934e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
4935e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
4936e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
4937e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
4938e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
4939e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
4940e126ba97SEli Cohen 
4941ff23dfa1SShamir Rabinovitch 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
4942e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
4943e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
4944e126ba97SEli Cohen 		goto error2;
4945e126ba97SEli Cohen 	}
4946e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
4947e126ba97SEli Cohen 	devr->x0->inode = NULL;
4948e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
4949e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
4950e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4951e126ba97SEli Cohen 
4952ff23dfa1SShamir Rabinovitch 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
4953e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
4954e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
4955e126ba97SEli Cohen 		goto error3;
4956e126ba97SEli Cohen 	}
4957e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
4958e126ba97SEli Cohen 	devr->x1->inode = NULL;
4959e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
4960e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
4961e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4962e126ba97SEli Cohen 
4963e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
4964e126ba97SEli Cohen 	attr.attr.max_sge = 1;
4965e126ba97SEli Cohen 	attr.attr.max_wr = 1;
4966e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
49671a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
4968e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
4969e126ba97SEli Cohen 
497068e326deSLeon Romanovsky 	devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
497168e326deSLeon Romanovsky 	if (!devr->s0) {
497268e326deSLeon Romanovsky 		ret = -ENOMEM;
4973e126ba97SEli Cohen 		goto error4;
4974e126ba97SEli Cohen 	}
497568e326deSLeon Romanovsky 
4976e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
4977e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
4978e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
4979e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
49801a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
498168e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
498268e326deSLeon Romanovsky 	if (ret)
498368e326deSLeon Romanovsky 		goto err_create;
498468e326deSLeon Romanovsky 
4985e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
49861a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
4987e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
4988e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
4989e126ba97SEli Cohen 
49904aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
49914aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
49924aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
49934aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
499468e326deSLeon Romanovsky 	devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
499568e326deSLeon Romanovsky 	if (!devr->s1) {
499668e326deSLeon Romanovsky 		ret = -ENOMEM;
49974aa17b28SHaggai Abramonvsky 		goto error5;
49984aa17b28SHaggai Abramonvsky 	}
499968e326deSLeon Romanovsky 
50004aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
50014aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
50024aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
50031a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
500468e326deSLeon Romanovsky 
500568e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
500668e326deSLeon Romanovsky 	if (ret)
500768e326deSLeon Romanovsky 		goto error6;
500868e326deSLeon Romanovsky 
50094aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
50101a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
50114aa17b28SHaggai Abramonvsky 
50127722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
50137722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
50147722f47eSHaggai Eran 			  pkey_change_handler);
50157722f47eSHaggai Eran 		devr->ports[port].devr = devr;
50167722f47eSHaggai Eran 	}
50177722f47eSHaggai Eran 
5018e126ba97SEli Cohen 	return 0;
5019e126ba97SEli Cohen 
502068e326deSLeon Romanovsky error6:
502168e326deSLeon Romanovsky 	kfree(devr->s1);
50224aa17b28SHaggai Abramonvsky error5:
5023c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
502468e326deSLeon Romanovsky err_create:
502568e326deSLeon Romanovsky 	kfree(devr->s0);
5026e126ba97SEli Cohen error4:
5027c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x1, NULL);
5028e126ba97SEli Cohen error3:
5029c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x0, NULL);
5030e126ba97SEli Cohen error2:
5031c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
5032e126ba97SEli Cohen error1:
5033c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
5034e126ba97SEli Cohen error0:
503521a428a0SLeon Romanovsky 	kfree(devr->p0);
5036e126ba97SEli Cohen 	return ret;
5037e126ba97SEli Cohen }
5038e126ba97SEli Cohen 
5039e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
5040e126ba97SEli Cohen {
50417722f47eSHaggai Eran 	int port;
50427722f47eSHaggai Eran 
5043c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s1, NULL);
504468e326deSLeon Romanovsky 	kfree(devr->s1);
5045c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
504668e326deSLeon Romanovsky 	kfree(devr->s0);
5047c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x0, NULL);
5048c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_xrcd(devr->x1, NULL);
5049c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
5050c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
505121a428a0SLeon Romanovsky 	kfree(devr->p0);
50527722f47eSHaggai Eran 
50537722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
50545d8f6a0eSMark Bloch 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
50557722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
5056e126ba97SEli Cohen }
5057e126ba97SEli Cohen 
5058b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
5059b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
5060e53505a8SAchiad Shochat {
5061e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5062e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
5063e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
5064e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
506585c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
5066e53505a8SAchiad Shochat 	u32 ret = 0;
5067e53505a8SAchiad Shochat 
5068b02289b3SArtemy Kovalyov 	if (rep->grh_required)
5069b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
5070b02289b3SArtemy Kovalyov 
5071e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
5072b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
5073e53505a8SAchiad Shochat 
507485c7c014SDaniel Jurgens 	if (raw_support)
5075b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
507672cd5717SOr Gerlitz 
5077e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
507872cd5717SOr Gerlitz 		return ret;
5079e53505a8SAchiad Shochat 
5080e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
508172cd5717SOr Gerlitz 		return ret;
5082e53505a8SAchiad Shochat 
5083e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
5084e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
5085e53505a8SAchiad Shochat 
5086e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
5087e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
5088e53505a8SAchiad Shochat 
5089e53505a8SAchiad Shochat 	return ret;
5090e53505a8SAchiad Shochat }
5091e53505a8SAchiad Shochat 
50927738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
50937738613eSIra Weiny 			       struct ib_port_immutable *immutable)
50947738613eSIra Weiny {
50957738613eSIra Weiny 	struct ib_port_attr attr;
5096ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5097ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
5098b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
50997738613eSIra Weiny 	int err;
51007738613eSIra Weiny 
5101c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
51027738613eSIra Weiny 	if (err)
51037738613eSIra Weiny 		return err;
51047738613eSIra Weiny 
5105b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
5106b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
5107b02289b3SArtemy Kovalyov 						   &rep);
5108b02289b3SArtemy Kovalyov 		if (err)
5109b02289b3SArtemy Kovalyov 			return err;
5110b02289b3SArtemy Kovalyov 	}
5111b02289b3SArtemy Kovalyov 
51127738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
51137738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
5114b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
5115ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
5116337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
51177738613eSIra Weiny 
51187738613eSIra Weiny 	return 0;
51197738613eSIra Weiny }
51207738613eSIra Weiny 
51218e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
51228e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
51238e6efa3aSMark Bloch {
51248e6efa3aSMark Bloch 	struct ib_port_attr attr;
51258e6efa3aSMark Bloch 	int err;
51268e6efa3aSMark Bloch 
51278e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
51288e6efa3aSMark Bloch 
51298e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
51308e6efa3aSMark Bloch 	if (err)
51318e6efa3aSMark Bloch 		return err;
51328e6efa3aSMark Bloch 
51338e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
51348e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
51358e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
51368e6efa3aSMark Bloch 
51378e6efa3aSMark Bloch 	return 0;
51388e6efa3aSMark Bloch }
51398e6efa3aSMark Bloch 
51409abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
5141c7342823SIra Weiny {
5142c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
5143c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
51449abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
51459abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
51469abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
5147c7342823SIra Weiny }
5148c7342823SIra Weiny 
514945f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
51509ef9c640SAviv Heller {
51519ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
51529ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
51539ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
51549ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
51559ef9c640SAviv Heller 	int err;
51569ef9c640SAviv Heller 
51577c34ec19SAviv Heller 	if (!ns || !mlx5_lag_is_roce(mdev))
51589ef9c640SAviv Heller 		return 0;
51599ef9c640SAviv Heller 
51609ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
51619ef9c640SAviv Heller 	if (err)
51629ef9c640SAviv Heller 		return err;
51639ef9c640SAviv Heller 
51649ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
51659ef9c640SAviv Heller 	if (IS_ERR(ft)) {
51669ef9c640SAviv Heller 		err = PTR_ERR(ft);
51679ef9c640SAviv Heller 		goto err_destroy_vport_lag;
51689ef9c640SAviv Heller 	}
51699ef9c640SAviv Heller 
51709a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
51717c34ec19SAviv Heller 	dev->lag_active = true;
51729ef9c640SAviv Heller 	return 0;
51739ef9c640SAviv Heller 
51749ef9c640SAviv Heller err_destroy_vport_lag:
51759ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
51769ef9c640SAviv Heller 	return err;
51779ef9c640SAviv Heller }
51789ef9c640SAviv Heller 
517945f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
51809ef9c640SAviv Heller {
51819ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
51829ef9c640SAviv Heller 
51837c34ec19SAviv Heller 	if (dev->lag_active) {
51847c34ec19SAviv Heller 		dev->lag_active = false;
51857c34ec19SAviv Heller 
51869a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
51879a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
51889ef9c640SAviv Heller 
51899ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
51909ef9c640SAviv Heller 	}
51919ef9c640SAviv Heller }
51929ef9c640SAviv Heller 
51937fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5194fc24fc5eSAchiad Shochat {
5195e53505a8SAchiad Shochat 	int err;
5196e53505a8SAchiad Shochat 
519795579e78SMark Bloch 	dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
519895579e78SMark Bloch 	err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
51995ec8c83eSAviv Heller 	if (err) {
520095579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
5201e53505a8SAchiad Shochat 		return err;
52025ec8c83eSAviv Heller 	}
5203e53505a8SAchiad Shochat 
5204d012f5d6SOr Gerlitz 	return 0;
5205d012f5d6SOr Gerlitz }
5206d012f5d6SOr Gerlitz 
52077fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
5208e126ba97SEli Cohen {
520995579e78SMark Bloch 	if (dev->port[port_num].roce.nb.notifier_call) {
521095579e78SMark Bloch 		unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
521195579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
5212e126ba97SEli Cohen 	}
5213e126ba97SEli Cohen }
5214e126ba97SEli Cohen 
5215e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
5216e126ba97SEli Cohen {
5217e126ba97SEli Cohen 	int err;
5218e126ba97SEli Cohen 
5219ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
5220e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
5221e53505a8SAchiad Shochat 		if (err)
52228e6efa3aSMark Bloch 			return err;
5223ca5b91d6SOr Gerlitz 	}
5224e53505a8SAchiad Shochat 
522545f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
52269ef9c640SAviv Heller 	if (err)
52279ef9c640SAviv Heller 		goto err_disable_roce;
52289ef9c640SAviv Heller 
5229e53505a8SAchiad Shochat 	return 0;
5230e53505a8SAchiad Shochat 
52319ef9c640SAviv Heller err_disable_roce:
5232ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
52339ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
52349ef9c640SAviv Heller 
5235e53505a8SAchiad Shochat 	return err;
5236fc24fc5eSAchiad Shochat }
5237fc24fc5eSAchiad Shochat 
523845f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
5239fc24fc5eSAchiad Shochat {
524045f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
5241ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
5242e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
5243fc24fc5eSAchiad Shochat }
5244fc24fc5eSAchiad Shochat 
5245e1f24a79SParav Pandit struct mlx5_ib_counter {
52467c16f477SKamal Heib 	const char *name;
52477c16f477SKamal Heib 	size_t offset;
52487c16f477SKamal Heib };
52497c16f477SKamal Heib 
52507c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
52517c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
52527c16f477SKamal Heib 
5253e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
52547c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
52557c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
52567c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
52577c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
52587c16f477SKamal Heib };
52597c16f477SKamal Heib 
5260e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
52617c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
52627c16f477SKamal Heib };
52637c16f477SKamal Heib 
5264e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
52657c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
52667c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
52677c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
52687c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
52697c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
52707c16f477SKamal Heib };
52717c16f477SKamal Heib 
5272e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
5273e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
5274e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
5275e1f24a79SParav Pandit 
5276e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
5277e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
5278e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
5279e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
5280e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
5281e1f24a79SParav Pandit };
5282e1f24a79SParav Pandit 
528358dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
528458dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
528558dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
528658dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
528758dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
528858dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
528958dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
529058dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
529158dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
529258dcb60aSParav Pandit };
529358dcb60aSParav Pandit 
52949f876f3dSTalat Batheesh #define INIT_EXT_PPCNT_COUNTER(_name)		\
52959f876f3dSTalat Batheesh 	{ .name = #_name, .offset =	\
52969f876f3dSTalat Batheesh 	MLX5_BYTE_OFF(ppcnt_reg, \
52979f876f3dSTalat Batheesh 		      counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
52989f876f3dSTalat Batheesh 
52999f876f3dSTalat Batheesh static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
53009f876f3dSTalat Batheesh 	INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
53019f876f3dSTalat Batheesh };
53029f876f3dSTalat Batheesh 
5303e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
53040837e86aSMark Bloch {
5305aac4492eSDaniel Jurgens 	int i;
53060837e86aSMark Bloch 
53077c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
5308921c0f5bSParav Pandit 		if (dev->port[i].cnts.set_id_valid)
53090837e86aSMark Bloch 			mlx5_core_dealloc_q_counter(dev->mdev,
5310e1f24a79SParav Pandit 						    dev->port[i].cnts.set_id);
5311e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
5312e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
53137c16f477SKamal Heib 	}
53147c16f477SKamal Heib }
53157c16f477SKamal Heib 
5316e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5317e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
53187c16f477SKamal Heib {
53197c16f477SKamal Heib 	u32 num_counters;
53207c16f477SKamal Heib 
53217c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
53227c16f477SKamal Heib 
53237c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
53247c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
53257c16f477SKamal Heib 
53267c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
53277c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
532858dcb60aSParav Pandit 
532958dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
533058dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
533158dcb60aSParav Pandit 
5332e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
53337c16f477SKamal Heib 
5334e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5335e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5336e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
5337e1f24a79SParav Pandit 	}
53389f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
53399f876f3dSTalat Batheesh 		cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
53409f876f3dSTalat Batheesh 		num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
53419f876f3dSTalat Batheesh 	}
5342e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5343e1f24a79SParav Pandit 	if (!cnts->names)
53447c16f477SKamal Heib 		return -ENOMEM;
53457c16f477SKamal Heib 
5346e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
5347e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
5348e1f24a79SParav Pandit 	if (!cnts->offsets)
53497c16f477SKamal Heib 		goto err_names;
53507c16f477SKamal Heib 
53517c16f477SKamal Heib 	return 0;
53527c16f477SKamal Heib 
53537c16f477SKamal Heib err_names:
5354e1f24a79SParav Pandit 	kfree(cnts->names);
5355aac4492eSDaniel Jurgens 	cnts->names = NULL;
53567c16f477SKamal Heib 	return -ENOMEM;
53577c16f477SKamal Heib }
53587c16f477SKamal Heib 
5359e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
53607c16f477SKamal Heib 				  const char **names,
53617c16f477SKamal Heib 				  size_t *offsets)
53627c16f477SKamal Heib {
53637c16f477SKamal Heib 	int i;
53647c16f477SKamal Heib 	int j = 0;
53657c16f477SKamal Heib 
53667c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
53677c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
53687c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
53697c16f477SKamal Heib 	}
53707c16f477SKamal Heib 
53717c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
53727c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
53737c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
53747c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
53757c16f477SKamal Heib 		}
53767c16f477SKamal Heib 	}
53777c16f477SKamal Heib 
53787c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
53797c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
53807c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
53817c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
53827c16f477SKamal Heib 		}
53837c16f477SKamal Heib 	}
5384e1f24a79SParav Pandit 
538558dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
538658dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
538758dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
538858dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
538958dcb60aSParav Pandit 		}
539058dcb60aSParav Pandit 	}
539158dcb60aSParav Pandit 
5392e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5393e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5394e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
5395e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
5396e1f24a79SParav Pandit 		}
5397e1f24a79SParav Pandit 	}
53989f876f3dSTalat Batheesh 
53999f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
54009f876f3dSTalat Batheesh 		for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
54019f876f3dSTalat Batheesh 			names[j] = ext_ppcnt_cnts[i].name;
54029f876f3dSTalat Batheesh 			offsets[j] = ext_ppcnt_cnts[i].offset;
54039f876f3dSTalat Batheesh 		}
54049f876f3dSTalat Batheesh 	}
54050837e86aSMark Bloch }
54060837e86aSMark Bloch 
5407e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
54080837e86aSMark Bloch {
5409aac4492eSDaniel Jurgens 	int err = 0;
54100837e86aSMark Bloch 	int i;
5411aa74be6eSYishai Hadas 	bool is_shared;
5412aa74be6eSYishai Hadas 
5413aa74be6eSYishai Hadas 	is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
54140837e86aSMark Bloch 
54150837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
5416aac4492eSDaniel Jurgens 		err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5417aac4492eSDaniel Jurgens 		if (err)
5418aac4492eSDaniel Jurgens 			goto err_alloc;
54197c16f477SKamal Heib 
5420aac4492eSDaniel Jurgens 		mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5421aac4492eSDaniel Jurgens 				      dev->port[i].cnts.offsets);
5422aac4492eSDaniel Jurgens 
5423aa74be6eSYishai Hadas 		err = mlx5_cmd_alloc_q_counter(dev->mdev,
5424aa74be6eSYishai Hadas 					       &dev->port[i].cnts.set_id,
5425aa74be6eSYishai Hadas 					       is_shared ?
5426aa74be6eSYishai Hadas 					       MLX5_SHARED_RESOURCE_UID : 0);
5427aac4492eSDaniel Jurgens 		if (err) {
54280837e86aSMark Bloch 			mlx5_ib_warn(dev,
54290837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
5430aac4492eSDaniel Jurgens 				     i + 1, err);
5431aac4492eSDaniel Jurgens 			goto err_alloc;
54320837e86aSMark Bloch 		}
5433aac4492eSDaniel Jurgens 		dev->port[i].cnts.set_id_valid = true;
54340837e86aSMark Bloch 	}
54350837e86aSMark Bloch 
54360837e86aSMark Bloch 	return 0;
54370837e86aSMark Bloch 
5438aac4492eSDaniel Jurgens err_alloc:
5439aac4492eSDaniel Jurgens 	mlx5_ib_dealloc_counters(dev);
5440aac4492eSDaniel Jurgens 	return err;
54410837e86aSMark Bloch }
54420837e86aSMark Bloch 
54430ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
54440ad17a8fSMark Bloch 						    u8 port_num)
54450ad17a8fSMark Bloch {
54467c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
54477c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
54480ad17a8fSMark Bloch 
54490ad17a8fSMark Bloch 	/* We support only per port stats */
54500ad17a8fSMark Bloch 	if (port_num == 0)
54510ad17a8fSMark Bloch 		return NULL;
54520ad17a8fSMark Bloch 
5453e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
5454e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
54559f876f3dSTalat Batheesh 					  port->cnts.num_cong_counters +
54569f876f3dSTalat Batheesh 					  port->cnts.num_ext_ppcnt_counters,
54570ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
54580ad17a8fSMark Bloch }
54590ad17a8fSMark Bloch 
5460aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
5461e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
5462e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
5463e1f24a79SParav Pandit {
5464e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5465e1f24a79SParav Pandit 	void *out;
5466e1f24a79SParav Pandit 	__be32 val;
5467e1f24a79SParav Pandit 	int ret, i;
5468e1f24a79SParav Pandit 
54691b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
5470e1f24a79SParav Pandit 	if (!out)
5471e1f24a79SParav Pandit 		return -ENOMEM;
5472e1f24a79SParav Pandit 
5473aac4492eSDaniel Jurgens 	ret = mlx5_core_query_q_counter(mdev,
5474e1f24a79SParav Pandit 					port->cnts.set_id, 0,
5475e1f24a79SParav Pandit 					out, outlen);
5476e1f24a79SParav Pandit 	if (ret)
5477e1f24a79SParav Pandit 		goto free;
5478e1f24a79SParav Pandit 
5479e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
5480e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
5481e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
5482e1f24a79SParav Pandit 	}
5483e1f24a79SParav Pandit 
5484e1f24a79SParav Pandit free:
5485e1f24a79SParav Pandit 	kvfree(out);
5486e1f24a79SParav Pandit 	return ret;
5487e1f24a79SParav Pandit }
5488e1f24a79SParav Pandit 
54899f876f3dSTalat Batheesh static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
54909f876f3dSTalat Batheesh 					  struct mlx5_ib_port *port,
54919f876f3dSTalat Batheesh 					  struct rdma_hw_stats *stats)
54929f876f3dSTalat Batheesh {
54939f876f3dSTalat Batheesh 	int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
54949f876f3dSTalat Batheesh 	int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
54959f876f3dSTalat Batheesh 	int ret, i;
54969f876f3dSTalat Batheesh 	void *out;
54979f876f3dSTalat Batheesh 
54989f876f3dSTalat Batheesh 	out = kvzalloc(sz, GFP_KERNEL);
54999f876f3dSTalat Batheesh 	if (!out)
55009f876f3dSTalat Batheesh 		return -ENOMEM;
55019f876f3dSTalat Batheesh 
55029f876f3dSTalat Batheesh 	ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
55039f876f3dSTalat Batheesh 	if (ret)
55049f876f3dSTalat Batheesh 		goto free;
55059f876f3dSTalat Batheesh 
55069f876f3dSTalat Batheesh 	for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
55079f876f3dSTalat Batheesh 		stats->value[i + offset] =
55089f876f3dSTalat Batheesh 			be64_to_cpup((__be64 *)(out +
55099f876f3dSTalat Batheesh 				    port->cnts.offsets[i + offset]));
55109f876f3dSTalat Batheesh 	}
55119f876f3dSTalat Batheesh 
55129f876f3dSTalat Batheesh free:
55139f876f3dSTalat Batheesh 	kvfree(out);
55149f876f3dSTalat Batheesh 	return ret;
55159f876f3dSTalat Batheesh }
55169f876f3dSTalat Batheesh 
55170ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
55180ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
55197c16f477SKamal Heib 				u8 port_num, int index)
55200ad17a8fSMark Bloch {
55210ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
55227c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
5523aac4492eSDaniel Jurgens 	struct mlx5_core_dev *mdev;
5524e1f24a79SParav Pandit 	int ret, num_counters;
5525aac4492eSDaniel Jurgens 	u8 mdev_port_num;
55260ad17a8fSMark Bloch 
55277c16f477SKamal Heib 	if (!stats)
5528e1f24a79SParav Pandit 		return -EINVAL;
55290ad17a8fSMark Bloch 
55309f876f3dSTalat Batheesh 	num_counters = port->cnts.num_q_counters +
55319f876f3dSTalat Batheesh 		       port->cnts.num_cong_counters +
55329f876f3dSTalat Batheesh 		       port->cnts.num_ext_ppcnt_counters;
5533aac4492eSDaniel Jurgens 
5534aac4492eSDaniel Jurgens 	/* q_counters are per IB device, query the master mdev */
5535aac4492eSDaniel Jurgens 	ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
55360ad17a8fSMark Bloch 	if (ret)
5537e1f24a79SParav Pandit 		return ret;
55380ad17a8fSMark Bloch 
55399f876f3dSTalat Batheesh 	if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
55409f876f3dSTalat Batheesh 		ret =  mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
55419f876f3dSTalat Batheesh 		if (ret)
55429f876f3dSTalat Batheesh 			return ret;
55439f876f3dSTalat Batheesh 	}
55449f876f3dSTalat Batheesh 
5545e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5546aac4492eSDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5547aac4492eSDaniel Jurgens 						    &mdev_port_num);
5548aac4492eSDaniel Jurgens 		if (!mdev) {
5549aac4492eSDaniel Jurgens 			/* If port is not affiliated yet, its in down state
5550aac4492eSDaniel Jurgens 			 * which doesn't have any counters yet, so it would be
5551aac4492eSDaniel Jurgens 			 * zero. So no need to read from the HCA.
5552aac4492eSDaniel Jurgens 			 */
5553aac4492eSDaniel Jurgens 			goto done;
5554aac4492eSDaniel Jurgens 		}
555571a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
555671a0ff65SMajd Dibbiny 						   stats->value +
555771a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters,
555871a0ff65SMajd Dibbiny 						   port->cnts.num_cong_counters,
555971a0ff65SMajd Dibbiny 						   port->cnts.offsets +
556071a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters);
5561aac4492eSDaniel Jurgens 
5562aac4492eSDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
5563e1f24a79SParav Pandit 		if (ret)
5564e1f24a79SParav Pandit 			return ret;
55650ad17a8fSMark Bloch 	}
55667c16f477SKamal Heib 
5567aac4492eSDaniel Jurgens done:
5568e1f24a79SParav Pandit 	return num_counters;
55690ad17a8fSMark Bloch }
55700ad17a8fSMark Bloch 
5571f6a8a19bSDenis Drozdov static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5572693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
5573f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
5574693dfd5aSErez Shitrit {
5575693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
5576f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
5577693dfd5aSErez Shitrit 
5578f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
5579693dfd5aSErez Shitrit }
5580693dfd5aSErez Shitrit 
5581fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5582fe248c3aSMaor Gottlieb {
5583fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
5584fe248c3aSMaor Gottlieb 		return;
5585fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5586fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
5587fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
5588fe248c3aSMaor Gottlieb }
5589fe248c3aSMaor Gottlieb 
559003404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
559103404e8aSMaor Gottlieb {
559203404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
559303404e8aSMaor Gottlieb 		return;
559403404e8aSMaor Gottlieb 
559503404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
5596fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5597fe248c3aSMaor Gottlieb }
5598fe248c3aSMaor Gottlieb 
5599fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5600fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
5601fe248c3aSMaor Gottlieb {
5602fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5603fe248c3aSMaor Gottlieb 	char lbuf[20];
5604fe248c3aSMaor Gottlieb 	int len;
5605fe248c3aSMaor Gottlieb 
5606fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5607fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
5608fe248c3aSMaor Gottlieb }
5609fe248c3aSMaor Gottlieb 
5610fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5611fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
5612fe248c3aSMaor Gottlieb {
5613fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5614fe248c3aSMaor Gottlieb 	u32 timeout;
5615fe248c3aSMaor Gottlieb 	u32 var;
5616fe248c3aSMaor Gottlieb 
5617fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
5618fe248c3aSMaor Gottlieb 		return -EFAULT;
5619fe248c3aSMaor Gottlieb 
5620fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5621fe248c3aSMaor Gottlieb 			1000);
5622fe248c3aSMaor Gottlieb 	if (timeout != var)
5623fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5624fe248c3aSMaor Gottlieb 			    timeout);
5625fe248c3aSMaor Gottlieb 
5626fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
5627fe248c3aSMaor Gottlieb 
5628fe248c3aSMaor Gottlieb 	return count;
5629fe248c3aSMaor Gottlieb }
5630fe248c3aSMaor Gottlieb 
5631fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
5632fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
5633fe248c3aSMaor Gottlieb 	.open	= simple_open,
5634fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
5635fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
5636fe248c3aSMaor Gottlieb };
5637fe248c3aSMaor Gottlieb 
5638fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5639fe248c3aSMaor Gottlieb {
5640fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
5641fe248c3aSMaor Gottlieb 
5642fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
5643fe248c3aSMaor Gottlieb 		return 0;
5644fe248c3aSMaor Gottlieb 
5645fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5646fe248c3aSMaor Gottlieb 	if (!dbg)
5647fe248c3aSMaor Gottlieb 		return -ENOMEM;
5648fe248c3aSMaor Gottlieb 
5649cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
5650cbafad87SSudip Mukherjee 
5651fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
5652fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
5653fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
5654fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
5655cbafad87SSudip Mukherjee 		goto out_debugfs;
5656fe248c3aSMaor Gottlieb 
5657fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
5658fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
5659fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5660fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
5661fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
5662fe248c3aSMaor Gottlieb 		goto out_debugfs;
5663fe248c3aSMaor Gottlieb 
5664fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
5665fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
5666fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
5667fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
5668fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
5669fe248c3aSMaor Gottlieb 		goto out_debugfs;
5670fe248c3aSMaor Gottlieb 
5671fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
5672fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
5673fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
5674fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
5675fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
5676fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
5677fe248c3aSMaor Gottlieb 		goto out_debugfs;
5678fe248c3aSMaor Gottlieb 
5679fe248c3aSMaor Gottlieb 	return 0;
5680fe248c3aSMaor Gottlieb 
5681fe248c3aSMaor Gottlieb out_debugfs:
5682fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
5683fe248c3aSMaor Gottlieb 	return -ENOMEM;
568403404e8aSMaor Gottlieb }
568503404e8aSMaor Gottlieb 
568603404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
568703404e8aSMaor Gottlieb {
568803404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
568903404e8aSMaor Gottlieb 		return;
569003404e8aSMaor Gottlieb 
569103404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
569203404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
569303404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
569403404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
569503404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
5696fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
5697fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
5698fe248c3aSMaor Gottlieb 
5699fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
5700fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
570103404e8aSMaor Gottlieb }
570203404e8aSMaor Gottlieb 
570332f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
570432f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
570532f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
570632f69e4bSDaniel Jurgens {
570732f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
570832f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
570932f69e4bSDaniel Jurgens 	int comps;
571032f69e4bSDaniel Jurgens 	int err;
571132f69e4bSDaniel Jurgens 	int i;
571232f69e4bSDaniel Jurgens 
5713a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5714a9e546e7SParav Pandit 
571532f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
571632f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
571732f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
571832f69e4bSDaniel Jurgens 		return;
571932f69e4bSDaniel Jurgens 	}
5720df097a27SSaeed Mahameed 
5721df097a27SSaeed Mahameed 	if (mpi->mdev_events.notifier_call)
5722df097a27SSaeed Mahameed 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
5723df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
5724df097a27SSaeed Mahameed 
572532f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
572632f69e4bSDaniel Jurgens 
572732f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
572832f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
572932f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
573032f69e4bSDaniel Jurgens 
573132f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
573232f69e4bSDaniel Jurgens 	if (comps) {
573332f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
573432f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
573532f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
573632f69e4bSDaniel Jurgens 
573732f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
573832f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
573932f69e4bSDaniel Jurgens 
574032f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
574132f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
574232f69e4bSDaniel Jurgens 	}
574332f69e4bSDaniel Jurgens 
574432f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
574532f69e4bSDaniel Jurgens 
574632f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
574732f69e4bSDaniel Jurgens 
574832f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
574932f69e4bSDaniel Jurgens 
575032f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
575132f69e4bSDaniel Jurgens 
575232f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
575332f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
575432f69e4bSDaniel Jurgens 	 * it back to the list.
575532f69e4bSDaniel Jurgens 	 */
575632f69e4bSDaniel Jurgens 	if (err)
575732f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
575832f69e4bSDaniel Jurgens 			    port_num + 1);
575932f69e4bSDaniel Jurgens 
576095579e78SMark Bloch 	ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
576132f69e4bSDaniel Jurgens }
576232f69e4bSDaniel Jurgens 
576332f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */
576432f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
576532f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
576632f69e4bSDaniel Jurgens {
576732f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
576832f69e4bSDaniel Jurgens 	int err;
576932f69e4bSDaniel Jurgens 
577032f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
577132f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
57722577188eSQing Huang 		mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
577332f69e4bSDaniel Jurgens 			    port_num + 1);
577432f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
577532f69e4bSDaniel Jurgens 		return false;
577632f69e4bSDaniel Jurgens 	}
577732f69e4bSDaniel Jurgens 
577832f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
577932f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
5780df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
578132f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
578232f69e4bSDaniel Jurgens 
578332f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
578432f69e4bSDaniel Jurgens 	if (err)
578532f69e4bSDaniel Jurgens 		goto unbind;
578632f69e4bSDaniel Jurgens 
578732f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
578832f69e4bSDaniel Jurgens 	if (err)
578932f69e4bSDaniel Jurgens 		goto unbind;
579032f69e4bSDaniel Jurgens 
579132f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
579232f69e4bSDaniel Jurgens 	if (err) {
579332f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
579432f69e4bSDaniel Jurgens 			    port_num + 1);
579532f69e4bSDaniel Jurgens 		goto unbind;
579632f69e4bSDaniel Jurgens 	}
579732f69e4bSDaniel Jurgens 
5798df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5799df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5800df097a27SSaeed Mahameed 
580173eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
5802a9e546e7SParav Pandit 
580332f69e4bSDaniel Jurgens 	return true;
580432f69e4bSDaniel Jurgens 
580532f69e4bSDaniel Jurgens unbind:
580632f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
580732f69e4bSDaniel Jurgens 	return false;
580832f69e4bSDaniel Jurgens }
580932f69e4bSDaniel Jurgens 
581032f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
581132f69e4bSDaniel Jurgens {
581232f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
581332f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
581432f69e4bSDaniel Jurgens 							  port_num + 1);
581532f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
581632f69e4bSDaniel Jurgens 	int err;
581732f69e4bSDaniel Jurgens 	int i;
581832f69e4bSDaniel Jurgens 
581932f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
582032f69e4bSDaniel Jurgens 		return 0;
582132f69e4bSDaniel Jurgens 
582232f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
582332f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
582432f69e4bSDaniel Jurgens 	if (err)
582532f69e4bSDaniel Jurgens 		return err;
582632f69e4bSDaniel Jurgens 
582732f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
582832f69e4bSDaniel Jurgens 	if (err)
582932f69e4bSDaniel Jurgens 		return err;
583032f69e4bSDaniel Jurgens 
583132f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
583232f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
583332f69e4bSDaniel Jurgens 		bool bound = false;
583432f69e4bSDaniel Jurgens 
583532f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
583632f69e4bSDaniel Jurgens 		if (i == port_num) {
583732f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
583832f69e4bSDaniel Jurgens 			if (!mpi) {
583932f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
584032f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
584132f69e4bSDaniel Jurgens 				return -ENOMEM;
584232f69e4bSDaniel Jurgens 			}
584332f69e4bSDaniel Jurgens 
584432f69e4bSDaniel Jurgens 			mpi->is_master = true;
584532f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
584632f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
584732f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
584832f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
584932f69e4bSDaniel Jurgens 			mpi = NULL;
585032f69e4bSDaniel Jurgens 			continue;
585132f69e4bSDaniel Jurgens 		}
585232f69e4bSDaniel Jurgens 
585332f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
585432f69e4bSDaniel Jurgens 				    list) {
585532f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
585632f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
585732f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
585832f69e4bSDaniel Jurgens 			}
585932f69e4bSDaniel Jurgens 
586032f69e4bSDaniel Jurgens 			if (bound) {
5861c42260f1SVu Pham 				dev_dbg(mpi->mdev->device,
5862c42260f1SVu Pham 					"removing port from unaffiliated list.\n");
586332f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
586432f69e4bSDaniel Jurgens 				list_del(&mpi->list);
586532f69e4bSDaniel Jurgens 				break;
586632f69e4bSDaniel Jurgens 			}
586732f69e4bSDaniel Jurgens 		}
586832f69e4bSDaniel Jurgens 		if (!bound) {
586932f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
587032f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
587132f69e4bSDaniel Jurgens 				    i + 1);
587232f69e4bSDaniel Jurgens 		}
587332f69e4bSDaniel Jurgens 	}
587432f69e4bSDaniel Jurgens 
587532f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
587632f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
587732f69e4bSDaniel Jurgens 	return err;
587832f69e4bSDaniel Jurgens }
587932f69e4bSDaniel Jurgens 
588032f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
588132f69e4bSDaniel Jurgens {
588232f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
588332f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
588432f69e4bSDaniel Jurgens 							  port_num + 1);
588532f69e4bSDaniel Jurgens 	int i;
588632f69e4bSDaniel Jurgens 
588732f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
588832f69e4bSDaniel Jurgens 		return;
588932f69e4bSDaniel Jurgens 
589032f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
589132f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
589232f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
589332f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
589432f69e4bSDaniel Jurgens 			if (i == port_num) {
589532f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
589632f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
589732f69e4bSDaniel Jurgens 			} else {
589832f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
589932f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
590032f69e4bSDaniel Jurgens 			}
590132f69e4bSDaniel Jurgens 		}
590232f69e4bSDaniel Jurgens 	}
590332f69e4bSDaniel Jurgens 
590432f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
590532f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
590632f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
590732f69e4bSDaniel Jurgens 
590832f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
590932f69e4bSDaniel Jurgens }
591032f69e4bSDaniel Jurgens 
59119a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
59129a119cd5SJason Gunthorpe 	mlx5_ib_dm,
59139a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
591424da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
59159a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
591624da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
591783bb4442SJason Gunthorpe 			    UA_MANDATORY),
59189a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
591924da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
59203b113a1eSAriel Levkovich 			    UA_OPTIONAL),
59213b113a1eSAriel Levkovich 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
59223b113a1eSAriel Levkovich 			     enum mlx5_ib_uapi_dm_type,
59233b113a1eSAriel Levkovich 			     UA_OPTIONAL));
592424da0016SAriel Levkovich 
59259a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
59269a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
59279a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
5928c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
5929bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
5930bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
5931c6475a0bSAviad Yehezkel 
59320cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
59330cbf432dSJason Gunthorpe #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
593436e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
59350cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
59360cbf432dSJason Gunthorpe #endif
59378c84660bSMatan Barak 
59380cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
59390cbf432dSJason Gunthorpe 				&mlx5_ib_flow_action),
59400cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
59410cbf432dSJason Gunthorpe 	{}
59420cbf432dSJason Gunthorpe };
59438c84660bSMatan Barak 
59441a1e03dcSRaed Salem static int mlx5_ib_read_counters(struct ib_counters *counters,
59451a1e03dcSRaed Salem 				 struct ib_counters_read_attr *read_attr,
59461a1e03dcSRaed Salem 				 struct uverbs_attr_bundle *attrs)
59471a1e03dcSRaed Salem {
59481a1e03dcSRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
59491a1e03dcSRaed Salem 	struct mlx5_read_counters_attr mread_attr = {};
59501a1e03dcSRaed Salem 	struct mlx5_ib_flow_counters_desc *desc;
59511a1e03dcSRaed Salem 	int ret, i;
59521a1e03dcSRaed Salem 
59531a1e03dcSRaed Salem 	mutex_lock(&mcounters->mcntrs_mutex);
59541a1e03dcSRaed Salem 	if (mcounters->cntrs_max_index > read_attr->ncounters) {
59551a1e03dcSRaed Salem 		ret = -EINVAL;
59561a1e03dcSRaed Salem 		goto err_bound;
59571a1e03dcSRaed Salem 	}
59581a1e03dcSRaed Salem 
59591a1e03dcSRaed Salem 	mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
59601a1e03dcSRaed Salem 				 GFP_KERNEL);
59611a1e03dcSRaed Salem 	if (!mread_attr.out) {
59621a1e03dcSRaed Salem 		ret = -ENOMEM;
59631a1e03dcSRaed Salem 		goto err_bound;
59641a1e03dcSRaed Salem 	}
59651a1e03dcSRaed Salem 
59661a1e03dcSRaed Salem 	mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
59671a1e03dcSRaed Salem 	mread_attr.flags = read_attr->flags;
59681a1e03dcSRaed Salem 	ret = mcounters->read_counters(counters->device, &mread_attr);
59691a1e03dcSRaed Salem 	if (ret)
59701a1e03dcSRaed Salem 		goto err_read;
59711a1e03dcSRaed Salem 
59721a1e03dcSRaed Salem 	/* do the pass over the counters data array to assign according to the
59731a1e03dcSRaed Salem 	 * descriptions and indexing pairs
59741a1e03dcSRaed Salem 	 */
59751a1e03dcSRaed Salem 	desc = mcounters->counters_data;
59761a1e03dcSRaed Salem 	for (i = 0; i < mcounters->ncounters; i++)
59771a1e03dcSRaed Salem 		read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
59781a1e03dcSRaed Salem 
59791a1e03dcSRaed Salem err_read:
59801a1e03dcSRaed Salem 	kfree(mread_attr.out);
59811a1e03dcSRaed Salem err_bound:
59821a1e03dcSRaed Salem 	mutex_unlock(&mcounters->mcntrs_mutex);
59831a1e03dcSRaed Salem 	return ret;
59841a1e03dcSRaed Salem }
59851a1e03dcSRaed Salem 
5986b29e2a13SRaed Salem static int mlx5_ib_destroy_counters(struct ib_counters *counters)
5987b29e2a13SRaed Salem {
5988b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5989b29e2a13SRaed Salem 
59903b3233fbSRaed Salem 	counters_clear_description(counters);
59913b3233fbSRaed Salem 	if (mcounters->hw_cntrs_hndl)
59923b3233fbSRaed Salem 		mlx5_fc_destroy(to_mdev(counters->device)->mdev,
59933b3233fbSRaed Salem 				mcounters->hw_cntrs_hndl);
59943b3233fbSRaed Salem 
5995b29e2a13SRaed Salem 	kfree(mcounters);
5996b29e2a13SRaed Salem 
5997b29e2a13SRaed Salem 	return 0;
5998b29e2a13SRaed Salem }
5999b29e2a13SRaed Salem 
6000b29e2a13SRaed Salem static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
6001b29e2a13SRaed Salem 						   struct uverbs_attr_bundle *attrs)
6002b29e2a13SRaed Salem {
6003b29e2a13SRaed Salem 	struct mlx5_ib_mcounters *mcounters;
6004b29e2a13SRaed Salem 
6005b29e2a13SRaed Salem 	mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
6006b29e2a13SRaed Salem 	if (!mcounters)
6007b29e2a13SRaed Salem 		return ERR_PTR(-ENOMEM);
6008b29e2a13SRaed Salem 
60093b3233fbSRaed Salem 	mutex_init(&mcounters->mcntrs_mutex);
60103b3233fbSRaed Salem 
6011b29e2a13SRaed Salem 	return &mcounters->ibcntrs;
6012b29e2a13SRaed Salem }
6013b29e2a13SRaed Salem 
6014fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
6015e126ba97SEli Cohen {
601625c13324SAriel Levkovich 	struct mlx5_core_dev *mdev = dev->mdev;
601725c13324SAriel Levkovich 
601832f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
601913859d5dSLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
6020534fd7aaSYishai Hadas 		srcu_barrier(&dev->mr_srcu);
60213cc297dbSMark Bloch 		cleanup_srcu_struct(&dev->mr_srcu);
602213859d5dSLeon Romanovsky 	}
60234056b12eSAriel Levkovich 
60244056b12eSAriel Levkovich 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
602525c13324SAriel Levkovich 
602625c13324SAriel Levkovich 	WARN_ON(dev->dm.steering_sw_icm_alloc_blocks &&
602725c13324SAriel Levkovich 		!bitmap_empty(
602825c13324SAriel Levkovich 			dev->dm.steering_sw_icm_alloc_blocks,
602925c13324SAriel Levkovich 			BIT(MLX5_CAP_DEV_MEM(mdev, log_steering_sw_icm_size) -
603025c13324SAriel Levkovich 			    MLX5_LOG_SW_ICM_BLOCK_SIZE(mdev))));
603125c13324SAriel Levkovich 
603225c13324SAriel Levkovich 	kfree(dev->dm.steering_sw_icm_alloc_blocks);
603325c13324SAriel Levkovich 
603425c13324SAriel Levkovich 	WARN_ON(dev->dm.header_modify_sw_icm_alloc_blocks &&
603525c13324SAriel Levkovich 		!bitmap_empty(dev->dm.header_modify_sw_icm_alloc_blocks,
603625c13324SAriel Levkovich 			      BIT(MLX5_CAP_DEV_MEM(
603725c13324SAriel Levkovich 					  mdev, log_header_modify_sw_icm_size) -
603825c13324SAriel Levkovich 				  MLX5_LOG_SW_ICM_BLOCK_SIZE(mdev))));
603925c13324SAriel Levkovich 
604025c13324SAriel Levkovich 	kfree(dev->dm.header_modify_sw_icm_alloc_blocks);
604116c1975fSMark Bloch }
604216c1975fSMark Bloch 
6043fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
604416c1975fSMark Bloch {
604516c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
604625c13324SAriel Levkovich 	u64 header_modify_icm_blocks = 0;
604725c13324SAriel Levkovich 	u64 steering_icm_blocks = 0;
6048e126ba97SEli Cohen 	int err;
604932f69e4bSDaniel Jurgens 	int i;
6050e126ba97SEli Cohen 
605132f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
605232f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
605395579e78SMark Bloch 		rwlock_init(&dev->port[i].roce.netdev_lock);
6054d3b5cc1cSMark Bloch 		dev->port[i].roce.dev = dev;
6055d3b5cc1cSMark Bloch 		dev->port[i].roce.native_port_num = i + 1;
6056d3b5cc1cSMark Bloch 		dev->port[i].roce.last_port_state = IB_PORT_DOWN;
605732f69e4bSDaniel Jurgens 	}
605832f69e4bSDaniel Jurgens 
605932f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
6060e126ba97SEli Cohen 	if (err)
6061da796ccbSMark Bloch 		return err;
6062e126ba97SEli Cohen 
6063a989ea01SMark Bloch 	err = set_has_smi_cap(dev);
6064a989ea01SMark Bloch 	if (err)
6065a989ea01SMark Bloch 		return err;
6066e126ba97SEli Cohen 
606732f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
606832f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
606932f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
607032f69e4bSDaniel Jurgens 			if (err)
607132f69e4bSDaniel Jurgens 				break;
607232f69e4bSDaniel Jurgens 		}
607332f69e4bSDaniel Jurgens 	} else {
607432f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
607532f69e4bSDaniel Jurgens 	}
607632f69e4bSDaniel Jurgens 	if (err)
607732f69e4bSDaniel Jurgens 		goto err_mp;
607832f69e4bSDaniel Jurgens 
60791b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
6080e126ba97SEli Cohen 		get_ext_port_caps(dev);
6081e126ba97SEli Cohen 
6082e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
6083e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
6084c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
6085e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
6086f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
6087c42260f1SVu Pham 	dev->ib_dev.dev.parent		= mdev->device;
6088e126ba97SEli Cohen 
60893cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
60903cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
60913cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
60923cc297dbSMark Bloch 
609325c13324SAriel Levkovich 	if (MLX5_CAP_GEN_64(mdev, general_obj_types) &
609425c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM) {
609525c13324SAriel Levkovich 		if (MLX5_CAP64_DEV_MEM(mdev, steering_sw_icm_start_address)) {
609625c13324SAriel Levkovich 			steering_icm_blocks =
609725c13324SAriel Levkovich 				BIT(MLX5_CAP_DEV_MEM(mdev,
609825c13324SAriel Levkovich 						     log_steering_sw_icm_size) -
609925c13324SAriel Levkovich 				    MLX5_LOG_SW_ICM_BLOCK_SIZE(mdev));
610025c13324SAriel Levkovich 
610125c13324SAriel Levkovich 			dev->dm.steering_sw_icm_alloc_blocks =
610225c13324SAriel Levkovich 				kcalloc(BITS_TO_LONGS(steering_icm_blocks),
610325c13324SAriel Levkovich 					sizeof(unsigned long), GFP_KERNEL);
610425c13324SAriel Levkovich 			if (!dev->dm.steering_sw_icm_alloc_blocks)
610525c13324SAriel Levkovich 				goto err_mp;
610625c13324SAriel Levkovich 		}
610725c13324SAriel Levkovich 
610825c13324SAriel Levkovich 		if (MLX5_CAP64_DEV_MEM(mdev,
610925c13324SAriel Levkovich 				       header_modify_sw_icm_start_address)) {
611025c13324SAriel Levkovich 			header_modify_icm_blocks = BIT(
611125c13324SAriel Levkovich 				MLX5_CAP_DEV_MEM(
611225c13324SAriel Levkovich 					mdev, log_header_modify_sw_icm_size) -
611325c13324SAriel Levkovich 				MLX5_LOG_SW_ICM_BLOCK_SIZE(mdev));
611425c13324SAriel Levkovich 
611525c13324SAriel Levkovich 			dev->dm.header_modify_sw_icm_alloc_blocks =
611625c13324SAriel Levkovich 				kcalloc(BITS_TO_LONGS(header_modify_icm_blocks),
611725c13324SAriel Levkovich 					sizeof(unsigned long), GFP_KERNEL);
611825c13324SAriel Levkovich 			if (!dev->dm.header_modify_sw_icm_alloc_blocks)
611925c13324SAriel Levkovich 				goto err_dm;
612025c13324SAriel Levkovich 		}
612125c13324SAriel Levkovich 	}
612225c13324SAriel Levkovich 
61233b113a1eSAriel Levkovich 	spin_lock_init(&dev->dm.lock);
61243b113a1eSAriel Levkovich 	dev->dm.dev = mdev;
612524da0016SAriel Levkovich 
612613859d5dSLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
61273cc297dbSMark Bloch 		err = init_srcu_struct(&dev->mr_srcu);
6128a6bc3875SMoni Shoua 		if (err)
612925c13324SAriel Levkovich 			goto err_dm;
6130623d1543SJason Gunthorpe 	}
61313cc297dbSMark Bloch 
613216c1975fSMark Bloch 	return 0;
613325c13324SAriel Levkovich 
613425c13324SAriel Levkovich err_dm:
613525c13324SAriel Levkovich 	kfree(dev->dm.steering_sw_icm_alloc_blocks);
613625c13324SAriel Levkovich 	kfree(dev->dm.header_modify_sw_icm_alloc_blocks);
613725c13324SAriel Levkovich 
613832f69e4bSDaniel Jurgens err_mp:
613932f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
614016c1975fSMark Bloch 
614116c1975fSMark Bloch 	return -ENOMEM;
614216c1975fSMark Bloch }
614316c1975fSMark Bloch 
61449a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
61459a4ca38dSMark Bloch {
61469a4ca38dSMark Bloch 	dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
61479a4ca38dSMark Bloch 
61489a4ca38dSMark Bloch 	if (!dev->flow_db)
61499a4ca38dSMark Bloch 		return -ENOMEM;
61509a4ca38dSMark Bloch 
61519a4ca38dSMark Bloch 	mutex_init(&dev->flow_db->lock);
61529a4ca38dSMark Bloch 
61539a4ca38dSMark Bloch 	return 0;
61549a4ca38dSMark Bloch }
61559a4ca38dSMark Bloch 
61569a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
61579a4ca38dSMark Bloch {
61589a4ca38dSMark Bloch 	kfree(dev->flow_db);
61599a4ca38dSMark Bloch }
61609a4ca38dSMark Bloch 
616196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
616296458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
616396458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
616496458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
616596458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
616696458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
616796458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
616896458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
616996458233SKamal Heib 	.create_counters = mlx5_ib_create_counters,
617096458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
617196458233SKamal Heib 	.create_flow = mlx5_ib_create_flow,
617296458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
617396458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
617496458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
617596458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
617696458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
617796458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
617896458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
617996458233SKamal Heib 	.destroy_counters = mlx5_ib_destroy_counters,
618096458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
618196458233SKamal Heib 	.destroy_flow = mlx5_ib_destroy_flow,
618296458233SKamal Heib 	.destroy_flow_action = mlx5_ib_destroy_flow_action,
618396458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
618496458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
618596458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
618696458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
618796458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
618896458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
618996458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
619096458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
619196458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
619296458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
619396458233SKamal Heib 	.mmap = mlx5_ib_mmap,
619496458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
619596458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
619696458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
619796458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
619896458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
619996458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
620096458233SKamal Heib 	.post_recv = mlx5_ib_post_recv,
620196458233SKamal Heib 	.post_send = mlx5_ib_post_send,
620296458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
620396458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
620496458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
620596458233SKamal Heib 	.query_device = mlx5_ib_query_device,
620696458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
620796458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
620896458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
620996458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
621096458233SKamal Heib 	.read_counters = mlx5_ib_read_counters,
621196458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
621296458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
621396458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
621496458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
6215d3456914SLeon Romanovsky 
6216d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
621721a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
621868e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
6219a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
622096458233SKamal Heib };
622196458233SKamal Heib 
622296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
622396458233SKamal Heib 	.create_flow_action_esp = mlx5_ib_create_flow_action_esp,
622496458233SKamal Heib 	.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
622596458233SKamal Heib };
622696458233SKamal Heib 
622796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
622896458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
622996458233SKamal Heib };
623096458233SKamal Heib 
623196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
623296458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
623396458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
623496458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
623596458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
623696458233SKamal Heib };
623796458233SKamal Heib 
623896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
623996458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
624096458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
624196458233SKamal Heib };
624296458233SKamal Heib 
624396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
624496458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
624596458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
624696458233SKamal Heib };
624796458233SKamal Heib 
624896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
624996458233SKamal Heib 	.alloc_dm = mlx5_ib_alloc_dm,
625096458233SKamal Heib 	.dealloc_dm = mlx5_ib_dealloc_dm,
625196458233SKamal Heib 	.reg_dm_mr = mlx5_ib_reg_dm_mr,
625296458233SKamal Heib };
625396458233SKamal Heib 
6254fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
625516c1975fSMark Bloch {
625616c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
625716c1975fSMark Bloch 	int err;
625816c1975fSMark Bloch 
6259e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
6260e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
6261e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
6262e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
6263e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
6264e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
6265e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
626641c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
626741c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
6268e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
626956e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
6270e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
6271e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
6272e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
6273e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
6274e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
6275e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
6276e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
6277e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
6278e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
6279e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
6280e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
6281e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
6282e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
6283e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
6284e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
6285e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
6286e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
62871707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
6288d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
6289d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
62907d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
6291b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
629296458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ)	|
629396458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW)	|
629496458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
6295e126ba97SEli Cohen 
6296f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
6297f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
629896458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
629996458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
63008e959601SNiranjana Vishwanathapura 
630196458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
630296458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
63037c2344c3SMaor Gottlieb 
63046e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
63056e8484c5SMax Gurtovoy 
6306d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
6307d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
6308d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
6309d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
631096458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
6311d2370e0aSMatan Barak 	}
6312d2370e0aSMatan Barak 
6313938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
6314e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
6315e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
6316e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
631796458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
6318e126ba97SEli Cohen 	}
6319e126ba97SEli Cohen 
632025c13324SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic) ||
632125c13324SAriel Levkovich 	    MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
632225c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
632396458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
632424da0016SAriel Levkovich 
6325dfb631a1SJason Gunthorpe 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
632696458233SKamal Heib 	    MLX5_ACCEL_IPSEC_CAP_DEVICE)
632796458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
63280ede73bcSMatan Barak 	dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
632996458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
633081e30880SYishai Hadas 
633136e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
633236e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
633381e30880SYishai Hadas 
6334e126ba97SEli Cohen 	err = init_node_data(dev);
6335e126ba97SEli Cohen 	if (err)
633616c1975fSMark Bloch 		return err;
6337e126ba97SEli Cohen 
6338c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
6339e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6340e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
6341a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
6342c8b89924SMark Bloch 
634316c1975fSMark Bloch 	return 0;
634416c1975fSMark Bloch }
634516c1975fSMark Bloch 
634696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
634796458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
634896458233SKamal Heib 	.query_port = mlx5_ib_query_port,
634996458233SKamal Heib };
635096458233SKamal Heib 
63518e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
635216c1975fSMark Bloch {
635396458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
63548e6efa3aSMark Bloch 	return 0;
63558e6efa3aSMark Bloch }
63568e6efa3aSMark Bloch 
635796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
635896458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
635996458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
636096458233SKamal Heib };
636196458233SKamal Heib 
6362fb652d32SMark Bloch static int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
63638e6efa3aSMark Bloch {
636496458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
63658e6efa3aSMark Bloch 	return 0;
63668e6efa3aSMark Bloch }
63678e6efa3aSMark Bloch 
636896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
636996458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
637096458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
637196458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
637296458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
637396458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
637496458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
637596458233SKamal Heib };
637696458233SKamal Heib 
6377e3f1ed1fSLeon Romanovsky static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
63788e6efa3aSMark Bloch {
6379e3f1ed1fSLeon Romanovsky 	u8 port_num;
63807fd8aefbSDaniel Jurgens 
6381c11a226aSMark Bloch 	dev->ib_dev.uverbs_ex_cmd_mask |=
6382c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6383c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6384c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6385c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6386c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
638796458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
63888e6efa3aSMark Bloch 
6389e3f1ed1fSLeon Romanovsky 	port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6390e3f1ed1fSLeon Romanovsky 
639126628e2dSMark Bloch 	/* Register only for native ports */
63928e6efa3aSMark Bloch 	return mlx5_add_netdev_notifier(dev, port_num);
63938e6efa3aSMark Bloch }
63948e6efa3aSMark Bloch 
63958e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
63968e6efa3aSMark Bloch {
63978e6efa3aSMark Bloch 	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
63988e6efa3aSMark Bloch 
63998e6efa3aSMark Bloch 	mlx5_remove_netdev_notifier(dev, port_num);
64008e6efa3aSMark Bloch }
64018e6efa3aSMark Bloch 
6402fb652d32SMark Bloch static int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
64038e6efa3aSMark Bloch {
64048e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
64058e6efa3aSMark Bloch 	enum rdma_link_layer ll;
64068e6efa3aSMark Bloch 	int port_type_cap;
64078e6efa3aSMark Bloch 	int err = 0;
64088e6efa3aSMark Bloch 
64098e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
64108e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
64118e6efa3aSMark Bloch 
64128e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET)
6413e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
64148e6efa3aSMark Bloch 
641516c1975fSMark Bloch 	return err;
6416fc24fc5eSAchiad Shochat }
6417fc24fc5eSAchiad Shochat 
6418fb652d32SMark Bloch static void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
64198e6efa3aSMark Bloch {
64208e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
64218e6efa3aSMark Bloch }
64228e6efa3aSMark Bloch 
64238e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
64248e6efa3aSMark Bloch {
64258e6efa3aSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
64268e6efa3aSMark Bloch 	enum rdma_link_layer ll;
64278e6efa3aSMark Bloch 	int port_type_cap;
64288e6efa3aSMark Bloch 	int err;
64298e6efa3aSMark Bloch 
64308e6efa3aSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
64318e6efa3aSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
64328e6efa3aSMark Bloch 
64338e6efa3aSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
6434e3f1ed1fSLeon Romanovsky 		err = mlx5_ib_stage_common_roce_init(dev);
64358e6efa3aSMark Bloch 		if (err)
64368e6efa3aSMark Bloch 			return err;
64378e6efa3aSMark Bloch 
6438e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
64398e6efa3aSMark Bloch 		if (err)
64408e6efa3aSMark Bloch 			goto cleanup;
64418e6efa3aSMark Bloch 	}
64428e6efa3aSMark Bloch 
644316c1975fSMark Bloch 	return 0;
64448e6efa3aSMark Bloch cleanup:
64458e6efa3aSMark Bloch 	mlx5_ib_stage_common_roce_cleanup(dev);
64468e6efa3aSMark Bloch 
64478e6efa3aSMark Bloch 	return err;
644845bded2cSKamal Heib }
64496aec21f6SHaggai Eran 
645016c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
645116c1975fSMark Bloch {
645216c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
645316c1975fSMark Bloch 	enum rdma_link_layer ll;
645416c1975fSMark Bloch 	int port_type_cap;
64554a2da0b8SParav Pandit 
645616c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
645716c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
645816c1975fSMark Bloch 
645916c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
646016c1975fSMark Bloch 		mlx5_disable_eth(dev);
64618e6efa3aSMark Bloch 		mlx5_ib_stage_common_roce_cleanup(dev);
646216c1975fSMark Bloch 	}
646316c1975fSMark Bloch }
646416c1975fSMark Bloch 
6465fb652d32SMark Bloch static int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
646616c1975fSMark Bloch {
646716c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
646816c1975fSMark Bloch }
646916c1975fSMark Bloch 
6470fb652d32SMark Bloch static void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
647116c1975fSMark Bloch {
647216c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
647316c1975fSMark Bloch }
647416c1975fSMark Bloch 
647516c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
647616c1975fSMark Bloch {
647707321b3cSMark Bloch 	mlx5_ib_internal_fill_odp_caps(dev);
647807321b3cSMark Bloch 
647916c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
648016c1975fSMark Bloch }
648116c1975fSMark Bloch 
6482f3ffed0cSKamal Heib static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
6483d5d284b8SSaeed Mahameed {
6484d5d284b8SSaeed Mahameed 	mlx5_ib_odp_cleanup_one(dev);
6485d5d284b8SSaeed Mahameed }
6486d5d284b8SSaeed Mahameed 
648796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
648896458233SKamal Heib 	.alloc_hw_stats = mlx5_ib_alloc_hw_stats,
648996458233SKamal Heib 	.get_hw_stats = mlx5_ib_get_hw_stats,
649096458233SKamal Heib };
649196458233SKamal Heib 
6492fb652d32SMark Bloch static int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
649316c1975fSMark Bloch {
64945e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
649596458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
64965e1e7612SMark Bloch 
649716c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
64985e1e7612SMark Bloch 	}
649916c1975fSMark Bloch 
650016c1975fSMark Bloch 	return 0;
650116c1975fSMark Bloch }
650216c1975fSMark Bloch 
6503fb652d32SMark Bloch static void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
650416c1975fSMark Bloch {
650516c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
650616c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
650716c1975fSMark Bloch }
650816c1975fSMark Bloch 
650916c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
651016c1975fSMark Bloch {
651173eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
6512a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
651373eb8f03SGreg Kroah-Hartman 	return 0;
651416c1975fSMark Bloch }
651516c1975fSMark Bloch 
651616c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
651716c1975fSMark Bloch {
6518a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
6519a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
652016c1975fSMark Bloch }
652116c1975fSMark Bloch 
652216c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
652316c1975fSMark Bloch {
65245fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
6525444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
652616c1975fSMark Bloch }
652716c1975fSMark Bloch 
652816c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
652916c1975fSMark Bloch {
653016c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
653116c1975fSMark Bloch }
653216c1975fSMark Bloch 
6533fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
653416c1975fSMark Bloch {
653516c1975fSMark Bloch 	int err;
65365fe9dec0SEli Cohen 
65375fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
65385fe9dec0SEli Cohen 	if (err)
653916c1975fSMark Bloch 		return err;
65405fe9dec0SEli Cohen 
65415fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
65425fe9dec0SEli Cohen 	if (err)
654316c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
65445fe9dec0SEli Cohen 
654516c1975fSMark Bloch 	return err;
654616c1975fSMark Bloch }
65470837e86aSMark Bloch 
6548fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
654916c1975fSMark Bloch {
655016c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
655116c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
655216c1975fSMark Bloch }
6553e126ba97SEli Cohen 
6554fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
655516c1975fSMark Bloch {
6556e349f858SJason Gunthorpe 	const char *name;
6557e349f858SJason Gunthorpe 
6558508a523fSParav Pandit 	rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
65597c34ec19SAviv Heller 	if (!mlx5_lag_is_roce(dev->mdev))
6560e349f858SJason Gunthorpe 		name = "mlx5_%d";
6561e349f858SJason Gunthorpe 	else
6562e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
6563ea4baf7fSParav Pandit 	return ib_register_device(&dev->ib_dev, name);
656416c1975fSMark Bloch }
656516c1975fSMark Bloch 
6566fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
656742cea83fSMark Bloch {
656842cea83fSMark Bloch 	destroy_umrc_res(dev);
656942cea83fSMark Bloch }
657042cea83fSMark Bloch 
6571fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
657216c1975fSMark Bloch {
657316c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
657416c1975fSMark Bloch }
657516c1975fSMark Bloch 
6576fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
657716c1975fSMark Bloch {
657816c1975fSMark Bloch 	return create_umr_res(dev);
657916c1975fSMark Bloch }
658016c1975fSMark Bloch 
658116c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
658216c1975fSMark Bloch {
658303404e8aSMaor Gottlieb 	init_delay_drop(dev);
658403404e8aSMaor Gottlieb 
658516c1975fSMark Bloch 	return 0;
658616c1975fSMark Bloch }
658716c1975fSMark Bloch 
658816c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
658916c1975fSMark Bloch {
659016c1975fSMark Bloch 	cancel_delay_drop(dev);
659116c1975fSMark Bloch }
659216c1975fSMark Bloch 
6593df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
6594df097a27SSaeed Mahameed {
6595df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
6596df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
6597df097a27SSaeed Mahameed 	return 0;
6598df097a27SSaeed Mahameed }
6599df097a27SSaeed Mahameed 
6600df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
6601df097a27SSaeed Mahameed {
6602df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
6603df097a27SSaeed Mahameed }
6604df097a27SSaeed Mahameed 
660581773ce5SLeon Romanovsky static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
660681773ce5SLeon Romanovsky {
660781773ce5SLeon Romanovsky 	int uid;
660881773ce5SLeon Romanovsky 
6609fb98153bSYishai Hadas 	uid = mlx5_ib_devx_create(dev, false);
661081773ce5SLeon Romanovsky 	if (uid > 0)
661181773ce5SLeon Romanovsky 		dev->devx_whitelist_uid = uid;
661281773ce5SLeon Romanovsky 
661381773ce5SLeon Romanovsky 	return 0;
661481773ce5SLeon Romanovsky }
661581773ce5SLeon Romanovsky static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
661681773ce5SLeon Romanovsky {
661781773ce5SLeon Romanovsky 	if (dev->devx_whitelist_uid)
661881773ce5SLeon Romanovsky 		mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
661981773ce5SLeon Romanovsky }
662081773ce5SLeon Romanovsky 
6621b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
662216c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
662316c1975fSMark Bloch 		      int stage)
662416c1975fSMark Bloch {
662516c1975fSMark Bloch 	/* Number of stages to cleanup */
662616c1975fSMark Bloch 	while (stage) {
662716c1975fSMark Bloch 		stage--;
662816c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
662916c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
663016c1975fSMark Bloch 	}
66314a6dc855SMark Bloch 
6632da796ccbSMark Bloch 	kfree(dev->port);
66334a6dc855SMark Bloch 	ib_dealloc_device(&dev->ib_dev);
663416c1975fSMark Bloch }
663516c1975fSMark Bloch 
6636b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
663716c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
663816c1975fSMark Bloch {
663916c1975fSMark Bloch 	int err;
664016c1975fSMark Bloch 	int i;
664116c1975fSMark Bloch 
664216c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
664316c1975fSMark Bloch 		if (profile->stage[i].init) {
664416c1975fSMark Bloch 			err = profile->stage[i].init(dev);
664516c1975fSMark Bloch 			if (err)
664616c1975fSMark Bloch 				goto err_out;
664716c1975fSMark Bloch 		}
664816c1975fSMark Bloch 	}
664916c1975fSMark Bloch 
665016c1975fSMark Bloch 	dev->profile = profile;
6651e126ba97SEli Cohen 	dev->ib_active = true;
6652e126ba97SEli Cohen 
66539603b61dSJack Morgenstein 	return dev;
6654e126ba97SEli Cohen 
665516c1975fSMark Bloch err_out:
665616c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
6657e126ba97SEli Cohen 
66589603b61dSJack Morgenstein 	return NULL;
6659e126ba97SEli Cohen }
6660e126ba97SEli Cohen 
666116c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
666216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
666316c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
666416c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
66659a4ca38dSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
66669a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_init,
66679a4ca38dSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
666816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
666916c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
667016c1975fSMark Bloch 		     NULL),
66718e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
66728e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
66738e6efa3aSMark Bloch 		     NULL),
667416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
667516c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
667616c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
6677f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6678f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
6679f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
668016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
668116c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
668216c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
6683df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6684df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
6685df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
668616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
668716c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
6688d5d284b8SSaeed Mahameed 		     mlx5_ib_stage_odp_cleanup),
668916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
669016c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
669116c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
669216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
669316c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
669416c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
669516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
669616c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
669716c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
669816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
669916c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
670016c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
670142cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
670242cea83fSMark Bloch 		     NULL,
670342cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
670481773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
670581773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_init,
670681773ce5SLeon Romanovsky 		     mlx5_ib_stage_devx_cleanup),
670716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
670816c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
670916c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
671042cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
671142cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
671242cea83fSMark Bloch 		     NULL),
671316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
671416c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
671516c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
671616c1975fSMark Bloch };
671716c1975fSMark Bloch 
6718f0666f1fSBodong Wang const struct mlx5_ib_profile uplink_rep_profile = {
6719b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
6720b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
6721b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
6722b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6723b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_init,
6724b5ca15adSMark Bloch 		     mlx5_ib_stage_flow_db_cleanup),
6725b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6726b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
6727b5ca15adSMark Bloch 		     NULL),
6728b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6729b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_non_default_cb,
6730b5ca15adSMark Bloch 		     NULL),
6731b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6732b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_init,
6733b5ca15adSMark Bloch 		     mlx5_ib_stage_rep_roce_cleanup),
6734f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6735f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
6736f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
6737b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6738b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_init,
6739b5ca15adSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
6740df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6741df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
6742df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
6743b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6744b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_init,
6745b5ca15adSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
6746b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
6747b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
6748b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
6749b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6750b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
6751b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
675203fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
675303fe2debSDavid S. Miller 		     NULL,
675403fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
67557f575103SMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
67567f575103SMark Bloch 		     mlx5_ib_stage_devx_init,
67577f575103SMark Bloch 		     mlx5_ib_stage_devx_cleanup),
6758b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6759b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
6760b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
676103fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
676203fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
676303fe2debSDavid S. Miller 		     NULL),
6764b5ca15adSMark Bloch };
6765b5ca15adSMark Bloch 
6766e3f1ed1fSLeon Romanovsky static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
676732f69e4bSDaniel Jurgens {
676832f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
676932f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
677032f69e4bSDaniel Jurgens 	bool bound = false;
677132f69e4bSDaniel Jurgens 	int err;
677232f69e4bSDaniel Jurgens 
677332f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
677432f69e4bSDaniel Jurgens 	if (!mpi)
677532f69e4bSDaniel Jurgens 		return NULL;
677632f69e4bSDaniel Jurgens 
677732f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
677832f69e4bSDaniel Jurgens 
677932f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
678032f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
678132f69e4bSDaniel Jurgens 	if (err) {
678232f69e4bSDaniel Jurgens 		kfree(mpi);
678332f69e4bSDaniel Jurgens 		return NULL;
678432f69e4bSDaniel Jurgens 	}
678532f69e4bSDaniel Jurgens 
678632f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
678732f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
678832f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
678932f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
679032f69e4bSDaniel Jurgens 
679132f69e4bSDaniel Jurgens 		if (bound) {
679232f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
679332f69e4bSDaniel Jurgens 			break;
679432f69e4bSDaniel Jurgens 		}
679532f69e4bSDaniel Jurgens 	}
679632f69e4bSDaniel Jurgens 
679732f69e4bSDaniel Jurgens 	if (!bound) {
679832f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
6799c42260f1SVu Pham 		dev_dbg(mdev->device,
6800c42260f1SVu Pham 			"no suitable IB device found to bind to, added to unaffiliated list.\n");
680132f69e4bSDaniel Jurgens 	}
680232f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
680332f69e4bSDaniel Jurgens 
680432f69e4bSDaniel Jurgens 	return mpi;
680532f69e4bSDaniel Jurgens }
680632f69e4bSDaniel Jurgens 
680716c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
680816c1975fSMark Bloch {
680932f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
6810b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
681132f69e4bSDaniel Jurgens 	int port_type_cap;
6812da796ccbSMark Bloch 	int num_ports;
681332f69e4bSDaniel Jurgens 
6814b5ca15adSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
6815b5ca15adSMark Bloch 
6816f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) &&
6817f6455de0SBodong Wang 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == MLX5_ESWITCH_OFFLOADS) {
68185fb58c9eSMark Bloch 		if (!mlx5_core_mp_enabled(mdev))
6819f0666f1fSBodong Wang 			mlx5_ib_register_vport_reps(mdev);
6820f0666f1fSBodong Wang 		return mdev;
6821f0666f1fSBodong Wang 	}
6822f0666f1fSBodong Wang 
682332f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
682432f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
682532f69e4bSDaniel Jurgens 
6826e3f1ed1fSLeon Romanovsky 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6827e3f1ed1fSLeon Romanovsky 		return mlx5_ib_add_slave_port(mdev);
682832f69e4bSDaniel Jurgens 
6829da796ccbSMark Bloch 	num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6830da796ccbSMark Bloch 			MLX5_CAP_GEN(mdev, num_vhca_ports));
6831459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
6832b5ca15adSMark Bloch 	if (!dev)
6833b5ca15adSMark Bloch 		return NULL;
6834da796ccbSMark Bloch 	dev->port = kcalloc(num_ports, sizeof(*dev->port),
6835da796ccbSMark Bloch 			     GFP_KERNEL);
6836da796ccbSMark Bloch 	if (!dev->port) {
6837da796ccbSMark Bloch 		ib_dealloc_device((struct ib_device *)dev);
6838da796ccbSMark Bloch 		return NULL;
6839da796ccbSMark Bloch 	}
6840b5ca15adSMark Bloch 
6841b5ca15adSMark Bloch 	dev->mdev = mdev;
6842da796ccbSMark Bloch 	dev->num_ports = num_ports;
6843b5ca15adSMark Bloch 
6844b5ca15adSMark Bloch 	return __mlx5_ib_add(dev, &pf_profile);
684516c1975fSMark Bloch }
684616c1975fSMark Bloch 
68479603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
6848e126ba97SEli Cohen {
684932f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
685032f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
68516aec21f6SHaggai Eran 
6852f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
6853f0666f1fSBodong Wang 		mlx5_ib_unregister_vport_reps(mdev);
6854f0666f1fSBodong Wang 		return;
6855f0666f1fSBodong Wang 	}
6856f0666f1fSBodong Wang 
685732f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
685832f69e4bSDaniel Jurgens 		mpi = context;
685932f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
686032f69e4bSDaniel Jurgens 		if (mpi->ibdev)
686132f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
686232f69e4bSDaniel Jurgens 		list_del(&mpi->list);
686332f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
686432f69e4bSDaniel Jurgens 		return;
686532f69e4bSDaniel Jurgens 	}
686632f69e4bSDaniel Jurgens 
686732f69e4bSDaniel Jurgens 	dev = context;
686816c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
6869e126ba97SEli Cohen }
6870e126ba97SEli Cohen 
68719603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
68729603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
68739603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
687464613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
6875e126ba97SEli Cohen };
6876e126ba97SEli Cohen 
6877c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
6878c44ef998SIlya Lesokhin {
6879c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
6880c44ef998SIlya Lesokhin 	return xlt_emergency_page;
6881c44ef998SIlya Lesokhin }
6882c44ef998SIlya Lesokhin 
6883c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
6884c44ef998SIlya Lesokhin {
6885c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
6886c44ef998SIlya Lesokhin }
6887c44ef998SIlya Lesokhin 
6888e126ba97SEli Cohen static int __init mlx5_ib_init(void)
6889e126ba97SEli Cohen {
68906aec21f6SHaggai Eran 	int err;
68916aec21f6SHaggai Eran 
6892c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
6893c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
6894d69a24e0SDaniel Jurgens 		return -ENOMEM;
6895d69a24e0SDaniel Jurgens 
6896c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
6897c44ef998SIlya Lesokhin 
6898c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6899c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
6900c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
6901c44ef998SIlya Lesokhin 		return -ENOMEM;
6902c44ef998SIlya Lesokhin 	}
6903c44ef998SIlya Lesokhin 
690481713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
69059603b61dSJack Morgenstein 
69066aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
69076aec21f6SHaggai Eran 
69086aec21f6SHaggai Eran 	return err;
6909e126ba97SEli Cohen }
6910e126ba97SEli Cohen 
6911e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
6912e126ba97SEli Cohen {
69139603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
6914d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
6915c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
6916c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
6917e126ba97SEli Cohen }
6918e126ba97SEli Cohen 
6919e126ba97SEli Cohen module_init(mlx5_ib_init);
6920e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
6921