xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 776a3906b692963586ee9952e64ed87fb4b401c6)
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>
4137aa5c36SGuy Levi #if defined(CONFIG_X86)
4237aa5c36SGuy Levi #include <asm/pat.h>
4337aa5c36SGuy Levi #endif
44e126ba97SEli Cohen #include <linux/sched.h>
456e84f315SIngo Molnar #include <linux/sched/mm.h>
460881e7bdSIngo Molnar #include <linux/sched/task.h>
477c2344c3SMaor Gottlieb #include <linux/delay.h>
48e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
493f89a643SAchiad Shochat #include <rdma/ib_addr.h>
502811ba51SAchiad Shochat #include <rdma/ib_cache.h>
51ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
521b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
5372c7fe90SPravin Shedge #include <linux/mlx5/fs.h>
547c2344c3SMaor Gottlieb #include <linux/list.h>
55e126ba97SEli Cohen #include <rdma/ib_smi.h>
56e126ba97SEli Cohen #include <rdma/ib_umem.h>
57038d2ef8SMaor Gottlieb #include <linux/in.h>
58038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
59e126ba97SEli Cohen #include "mlx5_ib.h"
60e1f24a79SParav Pandit #include "cmd.h"
61e126ba97SEli Cohen 
62e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
63b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0"
64e126ba97SEli Cohen 
65e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
66e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
67e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
68e126ba97SEli Cohen 
69e126ba97SEli Cohen static char mlx5_version[] =
70e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
71b359911dSTariq Toukan 	DRIVER_VERSION "\n";
72e126ba97SEli Cohen 
73da7525d2SEran Ben Elisha enum {
74da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
75da7525d2SEran Ben Elisha };
761b5daf11SMajd Dibbiny 
771b5daf11SMajd Dibbiny static enum rdma_link_layer
78ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
791b5daf11SMajd Dibbiny {
80ebd61f68SAchiad Shochat 	switch (port_type_cap) {
811b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
821b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
831b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
841b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
851b5daf11SMajd Dibbiny 	default:
861b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
871b5daf11SMajd Dibbiny 	}
881b5daf11SMajd Dibbiny }
891b5daf11SMajd Dibbiny 
90ebd61f68SAchiad Shochat static enum rdma_link_layer
91ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
92ebd61f68SAchiad Shochat {
93ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
94ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
95ebd61f68SAchiad Shochat 
96ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
97ebd61f68SAchiad Shochat }
98ebd61f68SAchiad Shochat 
99fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
100fd65f1b8SMoni Shoua 			  u8 port_num,
101fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
102fd65f1b8SMoni Shoua {
103fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
104fd65f1b8SMoni Shoua 	int ret;
105fd65f1b8SMoni Shoua 
106fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
107fd65f1b8SMoni Shoua 	ret = mlx5_ib_query_port(ibdev, port_num, &attr);
108fd65f1b8SMoni Shoua 	if (!ret)
109fd65f1b8SMoni Shoua 		*state = attr.state;
110fd65f1b8SMoni Shoua 	return ret;
111fd65f1b8SMoni Shoua }
112fd65f1b8SMoni Shoua 
113fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
114fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
115fc24fc5eSAchiad Shochat {
116fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
117fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
118fc24fc5eSAchiad Shochat 						 roce.nb);
119fc24fc5eSAchiad Shochat 
1205ec8c83eSAviv Heller 	switch (event) {
1215ec8c83eSAviv Heller 	case NETDEV_REGISTER:
1225ec8c83eSAviv Heller 	case NETDEV_UNREGISTER:
123fc24fc5eSAchiad Shochat 		write_lock(&ibdev->roce.netdev_lock);
124fc24fc5eSAchiad Shochat 		if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
1255ec8c83eSAviv Heller 			ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
1265ec8c83eSAviv Heller 					     NULL : ndev;
127fc24fc5eSAchiad Shochat 		write_unlock(&ibdev->roce.netdev_lock);
1285ec8c83eSAviv Heller 		break;
1295ec8c83eSAviv Heller 
130fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
1315ec8c83eSAviv Heller 	case NETDEV_UP:
13288621dfeSAviv Heller 	case NETDEV_DOWN: {
13388621dfeSAviv Heller 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
13488621dfeSAviv Heller 		struct net_device *upper = NULL;
13588621dfeSAviv Heller 
13688621dfeSAviv Heller 		if (lag_ndev) {
13788621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
13888621dfeSAviv Heller 			dev_put(lag_ndev);
13988621dfeSAviv Heller 		}
14088621dfeSAviv Heller 
14188621dfeSAviv Heller 		if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
14288621dfeSAviv Heller 		    && ibdev->ib_active) {
143626bc02dSBart Van Assche 			struct ib_event ibev = { };
144fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
1455ec8c83eSAviv Heller 
146fd65f1b8SMoni Shoua 			if (get_port_state(&ibdev->ib_dev, 1, &port_state))
147fd65f1b8SMoni Shoua 				return NOTIFY_DONE;
148fd65f1b8SMoni Shoua 
149fd65f1b8SMoni Shoua 			if (ibdev->roce.last_port_state == port_state)
150fd65f1b8SMoni Shoua 				return NOTIFY_DONE;
151fd65f1b8SMoni Shoua 
152fd65f1b8SMoni Shoua 			ibdev->roce.last_port_state = port_state;
1535ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
154fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
155fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
156fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
157fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
158fd65f1b8SMoni Shoua 			else
159fd65f1b8SMoni Shoua 				return NOTIFY_DONE;
160fd65f1b8SMoni Shoua 
1615ec8c83eSAviv Heller 			ibev.element.port_num = 1;
1625ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
1635ec8c83eSAviv Heller 		}
1645ec8c83eSAviv Heller 		break;
16588621dfeSAviv Heller 	}
1665ec8c83eSAviv Heller 
1675ec8c83eSAviv Heller 	default:
1685ec8c83eSAviv Heller 		break;
1695ec8c83eSAviv Heller 	}
170fc24fc5eSAchiad Shochat 
171fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
172fc24fc5eSAchiad Shochat }
173fc24fc5eSAchiad Shochat 
174fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
175fc24fc5eSAchiad Shochat 					     u8 port_num)
176fc24fc5eSAchiad Shochat {
177fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
178fc24fc5eSAchiad Shochat 	struct net_device *ndev;
179fc24fc5eSAchiad Shochat 
18088621dfeSAviv Heller 	ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
18188621dfeSAviv Heller 	if (ndev)
18288621dfeSAviv Heller 		return ndev;
18388621dfeSAviv Heller 
184fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
185fc24fc5eSAchiad Shochat 	 */
186fc24fc5eSAchiad Shochat 	read_lock(&ibdev->roce.netdev_lock);
187fc24fc5eSAchiad Shochat 	ndev = ibdev->roce.netdev;
188fc24fc5eSAchiad Shochat 	if (ndev)
189fc24fc5eSAchiad Shochat 		dev_hold(ndev);
190fc24fc5eSAchiad Shochat 	read_unlock(&ibdev->roce.netdev_lock);
191fc24fc5eSAchiad Shochat 
192fc24fc5eSAchiad Shochat 	return ndev;
193fc24fc5eSAchiad Shochat }
194fc24fc5eSAchiad Shochat 
195f1b65df5SNoa Osherovich static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
196f1b65df5SNoa Osherovich 				    u8 *active_width)
197f1b65df5SNoa Osherovich {
198f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
199f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
200f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
201f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
202f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
203f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
204f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
205f1b65df5SNoa Osherovich 		break;
206f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
207f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
208f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
209f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
210f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
211f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
212f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
213f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
214f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
215f1b65df5SNoa Osherovich 		break;
216f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
217f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
218f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
219f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
220f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
221f1b65df5SNoa Osherovich 		break;
222f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
223f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
224f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
225f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
226f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
227f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
228f1b65df5SNoa Osherovich 		break;
229f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
230f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
231f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
232f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
233f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
234f1b65df5SNoa Osherovich 		break;
235f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
236f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
237f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
238f1b65df5SNoa Osherovich 		break;
239f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
240f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
241f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
242f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
243f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
244f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
245f1b65df5SNoa Osherovich 		break;
246f1b65df5SNoa Osherovich 	default:
247f1b65df5SNoa Osherovich 		return -EINVAL;
248f1b65df5SNoa Osherovich 	}
249f1b65df5SNoa Osherovich 
250f1b65df5SNoa Osherovich 	return 0;
251f1b65df5SNoa Osherovich }
252f1b65df5SNoa Osherovich 
253095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
2543f89a643SAchiad Shochat 				struct ib_port_attr *props)
2553f89a643SAchiad Shochat {
2563f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
257f1b65df5SNoa Osherovich 	struct mlx5_core_dev *mdev = dev->mdev;
25888621dfeSAviv Heller 	struct net_device *ndev, *upper;
2593f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
260c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
261f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
262095b0927SIlan Tayari 	int err;
2633f89a643SAchiad Shochat 
264f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
265f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
26650f22fd8SNoa Osherovich 	 */
267095b0927SIlan Tayari 	err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper, port_num);
268095b0927SIlan Tayari 	if (err)
269095b0927SIlan Tayari 		return err;
270f1b65df5SNoa Osherovich 
271f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
272f1b65df5SNoa Osherovich 				 &props->active_width);
2733f89a643SAchiad Shochat 
2743f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
2753f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
2763f89a643SAchiad Shochat 
2773f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
2783f89a643SAchiad Shochat 						roce_address_table_size);
2793f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
2803f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
2813f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
2823f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
2833f89a643SAchiad Shochat 	props->phys_state       = 3;
2843f89a643SAchiad Shochat 
285c876a1b7SLeon Romanovsky 	mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
286c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
2873f89a643SAchiad Shochat 
2883f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
2893f89a643SAchiad Shochat 	if (!ndev)
290095b0927SIlan Tayari 		return 0;
2913f89a643SAchiad Shochat 
29288621dfeSAviv Heller 	if (mlx5_lag_is_active(dev->mdev)) {
29388621dfeSAviv Heller 		rcu_read_lock();
29488621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
29588621dfeSAviv Heller 		if (upper) {
29688621dfeSAviv Heller 			dev_put(ndev);
29788621dfeSAviv Heller 			ndev = upper;
29888621dfeSAviv Heller 			dev_hold(ndev);
29988621dfeSAviv Heller 		}
30088621dfeSAviv Heller 		rcu_read_unlock();
30188621dfeSAviv Heller 	}
30288621dfeSAviv Heller 
3033f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
3043f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
3053f89a643SAchiad Shochat 		props->phys_state = 5;
3063f89a643SAchiad Shochat 	}
3073f89a643SAchiad Shochat 
3083f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
3093f89a643SAchiad Shochat 
3103f89a643SAchiad Shochat 	dev_put(ndev);
3113f89a643SAchiad Shochat 
3123f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
313095b0927SIlan Tayari 	return 0;
3143f89a643SAchiad Shochat }
3153f89a643SAchiad Shochat 
316095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
317095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
318095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
3193cca2606SAchiad Shochat {
320095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
321095b0927SIlan Tayari 	u8 roce_version = 0;
322095b0927SIlan Tayari 	u8 roce_l3_type = 0;
323095b0927SIlan Tayari 	bool vlan = false;
324095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
325095b0927SIlan Tayari 	u16 vlan_id = 0;
3263cca2606SAchiad Shochat 
327095b0927SIlan Tayari 	if (gid) {
328095b0927SIlan Tayari 		gid_type = attr->gid_type;
329095b0927SIlan Tayari 		ether_addr_copy(mac, attr->ndev->dev_addr);
3303cca2606SAchiad Shochat 
3313cca2606SAchiad Shochat 		if (is_vlan_dev(attr->ndev)) {
332095b0927SIlan Tayari 			vlan = true;
333095b0927SIlan Tayari 			vlan_id = vlan_dev_vlan_id(attr->ndev);
334095b0927SIlan Tayari 		}
3353cca2606SAchiad Shochat 	}
3363cca2606SAchiad Shochat 
337095b0927SIlan Tayari 	switch (gid_type) {
3383cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
339095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
3403cca2606SAchiad Shochat 		break;
3413cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
342095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
343095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
344095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
345095b0927SIlan Tayari 		else
346095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
3473cca2606SAchiad Shochat 		break;
3483cca2606SAchiad Shochat 
3493cca2606SAchiad Shochat 	default:
350095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
3513cca2606SAchiad Shochat 	}
3523cca2606SAchiad Shochat 
353095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
354095b0927SIlan Tayari 				      roce_l3_type, gid->raw, mac, vlan,
355095b0927SIlan Tayari 				      vlan_id);
3563cca2606SAchiad Shochat }
3573cca2606SAchiad Shochat 
3583cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
3593cca2606SAchiad Shochat 			   unsigned int index, const union ib_gid *gid,
3603cca2606SAchiad Shochat 			   const struct ib_gid_attr *attr,
3613cca2606SAchiad Shochat 			   __always_unused void **context)
3623cca2606SAchiad Shochat {
363095b0927SIlan Tayari 	return set_roce_addr(to_mdev(device), port_num, index, gid, attr);
3643cca2606SAchiad Shochat }
3653cca2606SAchiad Shochat 
3663cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
3673cca2606SAchiad Shochat 			   unsigned int index, __always_unused void **context)
3683cca2606SAchiad Shochat {
369095b0927SIlan Tayari 	return set_roce_addr(to_mdev(device), port_num, index, NULL, NULL);
3703cca2606SAchiad Shochat }
3713cca2606SAchiad Shochat 
3722811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
3732811ba51SAchiad Shochat 			       int index)
3742811ba51SAchiad Shochat {
3752811ba51SAchiad Shochat 	struct ib_gid_attr attr;
3762811ba51SAchiad Shochat 	union ib_gid gid;
3772811ba51SAchiad Shochat 
3782811ba51SAchiad Shochat 	if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
3792811ba51SAchiad Shochat 		return 0;
3802811ba51SAchiad Shochat 
3812811ba51SAchiad Shochat 	if (!attr.ndev)
3822811ba51SAchiad Shochat 		return 0;
3832811ba51SAchiad Shochat 
3842811ba51SAchiad Shochat 	dev_put(attr.ndev);
3852811ba51SAchiad Shochat 
3862811ba51SAchiad Shochat 	if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
3872811ba51SAchiad Shochat 		return 0;
3882811ba51SAchiad Shochat 
3892811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
3902811ba51SAchiad Shochat }
3912811ba51SAchiad Shochat 
392ed88451eSMajd Dibbiny int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
393ed88451eSMajd Dibbiny 			   int index, enum ib_gid_type *gid_type)
394ed88451eSMajd Dibbiny {
395ed88451eSMajd Dibbiny 	struct ib_gid_attr attr;
396ed88451eSMajd Dibbiny 	union ib_gid gid;
397ed88451eSMajd Dibbiny 	int ret;
398ed88451eSMajd Dibbiny 
399ed88451eSMajd Dibbiny 	ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
400ed88451eSMajd Dibbiny 	if (ret)
401ed88451eSMajd Dibbiny 		return ret;
402ed88451eSMajd Dibbiny 
403ed88451eSMajd Dibbiny 	if (!attr.ndev)
404ed88451eSMajd Dibbiny 		return -ENODEV;
405ed88451eSMajd Dibbiny 
406ed88451eSMajd Dibbiny 	dev_put(attr.ndev);
407ed88451eSMajd Dibbiny 
408ed88451eSMajd Dibbiny 	*gid_type = attr.gid_type;
409ed88451eSMajd Dibbiny 
410ed88451eSMajd Dibbiny 	return 0;
411ed88451eSMajd Dibbiny }
412ed88451eSMajd Dibbiny 
4131b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
4141b5daf11SMajd Dibbiny {
4157fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
416d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
4177fae6655SNoa Osherovich 	return 0;
4181b5daf11SMajd Dibbiny }
4191b5daf11SMajd Dibbiny 
4201b5daf11SMajd Dibbiny enum {
4211b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
4221b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
4231b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
4241b5daf11SMajd Dibbiny };
4251b5daf11SMajd Dibbiny 
4261b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
4271b5daf11SMajd Dibbiny {
4281b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
4291b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
4301b5daf11SMajd Dibbiny 
431ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
4321b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
4331b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
4341b5daf11SMajd Dibbiny 
4351b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
4361b5daf11SMajd Dibbiny }
4371b5daf11SMajd Dibbiny 
438da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
439*776a3906SMoni Shoua 			    u8 atomic_size_qp,
440da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
441da7525d2SEran Ben Elisha {
442da7525d2SEran Ben Elisha 	u8 tmp;
443da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
444da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
445bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
446da7525d2SEran Ben Elisha 
447da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
448da7525d2SEran Ben Elisha 	 * of host endianness respond
449da7525d2SEran Ben Elisha 	 */
450da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
451da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
452da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
453da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
454da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
455da7525d2SEran Ben Elisha 	} else {
456da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
457da7525d2SEran Ben Elisha 	}
458da7525d2SEran Ben Elisha }
459da7525d2SEran Ben Elisha 
460*776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
461*776a3906SMoni Shoua 			       struct ib_device_attr *props)
462*776a3906SMoni Shoua {
463*776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
464*776a3906SMoni Shoua 
465*776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
466*776a3906SMoni Shoua }
467*776a3906SMoni Shoua 
468*776a3906SMoni Shoua static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
469*776a3906SMoni Shoua 			       struct ib_device_attr *props)
470*776a3906SMoni Shoua {
471*776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
472*776a3906SMoni Shoua 
473*776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
474*776a3906SMoni Shoua }
475*776a3906SMoni Shoua 
476*776a3906SMoni Shoua bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
477*776a3906SMoni Shoua {
478*776a3906SMoni Shoua 	struct ib_device_attr props = {};
479*776a3906SMoni Shoua 
480*776a3906SMoni Shoua 	get_atomic_caps_dc(dev, &props);
481*776a3906SMoni Shoua 	return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
482*776a3906SMoni Shoua }
4831b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
4841b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
4851b5daf11SMajd Dibbiny {
4861b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4871b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4881b5daf11SMajd Dibbiny 	u64 tmp;
4891b5daf11SMajd Dibbiny 	int err;
4901b5daf11SMajd Dibbiny 
4911b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4921b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4931b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
4941b5daf11SMajd Dibbiny 							    sys_image_guid);
4951b5daf11SMajd Dibbiny 
4961b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4971b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
4983f89a643SAchiad Shochat 		break;
4993f89a643SAchiad Shochat 
5003f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5013f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
5023f89a643SAchiad Shochat 		break;
5031b5daf11SMajd Dibbiny 
5041b5daf11SMajd Dibbiny 	default:
5051b5daf11SMajd Dibbiny 		return -EINVAL;
5061b5daf11SMajd Dibbiny 	}
5073f89a643SAchiad Shochat 
5083f89a643SAchiad Shochat 	if (!err)
5093f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
5103f89a643SAchiad Shochat 
5113f89a643SAchiad Shochat 	return err;
5123f89a643SAchiad Shochat 
5131b5daf11SMajd Dibbiny }
5141b5daf11SMajd Dibbiny 
5151b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
5161b5daf11SMajd Dibbiny 				u16 *max_pkeys)
5171b5daf11SMajd Dibbiny {
5181b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5191b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
5201b5daf11SMajd Dibbiny 
5211b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
5221b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5231b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
5241b5daf11SMajd Dibbiny 
5251b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5261b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5271b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
5281b5daf11SMajd Dibbiny 						pkey_table_size));
5291b5daf11SMajd Dibbiny 		return 0;
5301b5daf11SMajd Dibbiny 
5311b5daf11SMajd Dibbiny 	default:
5321b5daf11SMajd Dibbiny 		return -EINVAL;
5331b5daf11SMajd Dibbiny 	}
5341b5daf11SMajd Dibbiny }
5351b5daf11SMajd Dibbiny 
5361b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
5371b5daf11SMajd Dibbiny 				u32 *vendor_id)
5381b5daf11SMajd Dibbiny {
5391b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5401b5daf11SMajd Dibbiny 
5411b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
5421b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5431b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
5441b5daf11SMajd Dibbiny 
5451b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5461b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5471b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
5481b5daf11SMajd Dibbiny 
5491b5daf11SMajd Dibbiny 	default:
5501b5daf11SMajd Dibbiny 		return -EINVAL;
5511b5daf11SMajd Dibbiny 	}
5521b5daf11SMajd Dibbiny }
5531b5daf11SMajd Dibbiny 
5541b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
5551b5daf11SMajd Dibbiny 				__be64 *node_guid)
5561b5daf11SMajd Dibbiny {
5571b5daf11SMajd Dibbiny 	u64 tmp;
5581b5daf11SMajd Dibbiny 	int err;
5591b5daf11SMajd Dibbiny 
5601b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
5611b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5621b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
5631b5daf11SMajd Dibbiny 
5641b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5651b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
5663f89a643SAchiad Shochat 		break;
5673f89a643SAchiad Shochat 
5683f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5693f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
5703f89a643SAchiad Shochat 		break;
5711b5daf11SMajd Dibbiny 
5721b5daf11SMajd Dibbiny 	default:
5731b5daf11SMajd Dibbiny 		return -EINVAL;
5741b5daf11SMajd Dibbiny 	}
5753f89a643SAchiad Shochat 
5763f89a643SAchiad Shochat 	if (!err)
5773f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
5783f89a643SAchiad Shochat 
5793f89a643SAchiad Shochat 	return err;
5801b5daf11SMajd Dibbiny }
5811b5daf11SMajd Dibbiny 
5821b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
583bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
5841b5daf11SMajd Dibbiny };
5851b5daf11SMajd Dibbiny 
5861b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
5871b5daf11SMajd Dibbiny {
5881b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
5891b5daf11SMajd Dibbiny 
5901b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
5911b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
5921b5daf11SMajd Dibbiny 
5931b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
5941b5daf11SMajd Dibbiny 
5951b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
5961b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
5971b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
5981b5daf11SMajd Dibbiny }
5991b5daf11SMajd Dibbiny 
600e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
6012528e33eSMatan Barak 				struct ib_device_attr *props,
6022528e33eSMatan Barak 				struct ib_udata *uhw)
603e126ba97SEli Cohen {
604e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
605938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
606e126ba97SEli Cohen 	int err = -ENOMEM;
607288c01b7SEli Cohen 	int max_sq_desc;
608e126ba97SEli Cohen 	int max_rq_sg;
609e126ba97SEli Cohen 	int max_sq_sg;
610e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
611402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
612402ca536SBodong Wang 	size_t resp_len;
613402ca536SBodong Wang 	u64 max_tso;
614e126ba97SEli Cohen 
615402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
616402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
617402ca536SBodong Wang 		return -EINVAL;
618402ca536SBodong Wang 	else
619402ca536SBodong Wang 		resp.response_length = resp_len;
620402ca536SBodong Wang 
621402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
6222528e33eSMatan Barak 		return -EINVAL;
6232528e33eSMatan Barak 
624e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
6251b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
6261b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
6271b5daf11SMajd Dibbiny 	if (err)
6281b5daf11SMajd Dibbiny 		return err;
6291b5daf11SMajd Dibbiny 
6301b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
6311b5daf11SMajd Dibbiny 	if (err)
6321b5daf11SMajd Dibbiny 		return err;
6331b5daf11SMajd Dibbiny 
6341b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
6351b5daf11SMajd Dibbiny 	if (err)
6361b5daf11SMajd Dibbiny 		return err;
637e126ba97SEli Cohen 
6389603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
6399603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
6409603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
641e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
642e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
643e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
6441a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
645938fe83cSSaeed Mahameed 
646938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
647e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
648938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
649e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
650938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
651e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
652938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
653e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
654d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
655d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
656d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
657d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
658b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
659b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
660d2370e0aSMatan Barak 	}
661e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
662938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
6632dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
6642dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
6652dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
6662dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
6672dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
6682dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
6692dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
6702dea9094SSagi Grimberg 	}
671938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
672f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
673e126ba97SEli Cohen 
674402ca536SBodong Wang 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
675e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
676e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
67788115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
678e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
679e8161334SNoa Osherovich 		}
680e8161334SNoa Osherovich 
681e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
682e8161334SNoa Osherovich 			props->raw_packet_caps |=
683e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
68488115fe7SBodong Wang 
685402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
686402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
687402ca536SBodong Wang 			if (max_tso) {
688402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
689402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
690402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
691402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
692402ca536SBodong Wang 			}
693402ca536SBodong Wang 		}
69431f69a82SYishai Hadas 
69531f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
69631f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
69731f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
69831f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
69931f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
70031f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
70131f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
70231f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
70331f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
70431f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
70531f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
7064e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
7074e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
70831f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
70931f69a82SYishai Hadas 		}
71031f69a82SYishai Hadas 	} else {
71131f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
71231f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
71331f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
71431f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
715402ca536SBodong Wang 	}
716402ca536SBodong Wang 
717f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
718f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
719f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
720f0313965SErez Shitrit 	}
721f0313965SErez Shitrit 
72203404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
72303404e8aSMaor Gottlieb 	    MLX5_CAP_GEN(dev->mdev, general_notification_event))
72403404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
72503404e8aSMaor Gottlieb 
7261d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
7271d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
7281d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
7291d54f890SYishai Hadas 
730cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
731e8161334SNoa Osherovich 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
732e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
733cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
734e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
735e8161334SNoa Osherovich 	}
736cff5a0f3SMajd Dibbiny 
737da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
738da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
739da6d6ba3SMaor Gottlieb 
740b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
741b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
742b1383aa6SNoa Osherovich 
7431b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
7441b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
745e126ba97SEli Cohen 
746e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
747e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
748938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
749938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
750938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
751938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
752288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
753288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
754288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
755e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
756e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
757986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
758938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
7599f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
760938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
761938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
762938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
763938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
764938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
765938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
766938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
767e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
768e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
769911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
770911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
771*776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
77281bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
773938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
774938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
775e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
776e126ba97SEli Cohen 					   props->max_mcast_grp;
777e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
77886695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
7797c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
7807c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
781e126ba97SEli Cohen 
7828cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
783938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
7848cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
7858cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
7868cdd312cSHaggai Eran #endif
7878cdd312cSHaggai Eran 
788051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
789051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
790051f2630SLeon Romanovsky 
791eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
792eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
793eff901d3SEli Cohen 
79431f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
79531f69a82SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
79631f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
79731f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
79831f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
79931f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
80031f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
80131f69a82SYishai Hadas 		props->max_wq_type_rq =
80231f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
80331f69a82SYishai Hadas 	}
80431f69a82SYishai Hadas 
805eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
80678b1beb0SLeon Romanovsky 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
80778b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
808eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
80978b1beb0SLeon Romanovsky 		props->tm_caps.flags = IB_TM_CAP_RC;
81078b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
811eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
81278b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
813eb761894SArtemy Kovalyov 	}
814eb761894SArtemy Kovalyov 
81587ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
81687ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
81787ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
81887ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
81987ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
82087ab3f52SYonatan Cohen 	}
82187ab3f52SYonatan Cohen 
8227e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
8237e43a2a5SBodong Wang 		resp.cqe_comp_caps.max_num =
8247e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
8257e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
8267e43a2a5SBodong Wang 		resp.cqe_comp_caps.supported_format =
8277e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_HASH |
8287e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_CSUM;
8297e43a2a5SBodong Wang 		resp.response_length += sizeof(resp.cqe_comp_caps);
8307e43a2a5SBodong Wang 	}
8317e43a2a5SBodong Wang 
832d949167dSBodong Wang 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
833d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
834d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
835d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
836d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
837d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
838d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
839d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
840d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
841d949167dSBodong Wang 		}
842d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
843d949167dSBodong Wang 	}
844d949167dSBodong Wang 
8459f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
8469f885201SLeon Romanovsky 			uhw->outlen)) {
847795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
8489f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
849795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
850050da902SBodong Wang 
851050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
852050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
853050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
854050da902SBodong Wang 
8559f885201SLeon Romanovsky 		resp.response_length +=
8569f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
8579f885201SLeon Romanovsky 	}
8589f885201SLeon Romanovsky 
859de57f2adSGuy Levi 	if (field_avail(typeof(resp), flags, uhw->outlen)) {
860de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
8617a0c8f42SGuy Levi 
862de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
863de57f2adSGuy Levi 			resp.flags |=
864de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
8657a0c8f42SGuy Levi 
8667a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
8677a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
868de57f2adSGuy Levi 	}
8699f885201SLeon Romanovsky 
87096dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
87196dc3fc5SNoa Osherovich 			uhw->outlen)) {
87296dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
87396dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
87496dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
87596dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
87696dc3fc5SNoa Osherovich 
87796dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
87896dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
87996dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
88096dc3fc5SNoa Osherovich 
88196dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
88296dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
88396dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
88496dc3fc5SNoa Osherovich 
88596dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
88696dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
88796dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
88896dc3fc5SNoa Osherovich 		}
88996dc3fc5SNoa Osherovich 	}
89096dc3fc5SNoa Osherovich 
891b4f34597SNoa Osherovich 	if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen)) {
892b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
893b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
894b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
895b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
896b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
897b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
898b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
899b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
900b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
901b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
902b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
903b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
904b4f34597SNoa Osherovich 		}
905b4f34597SNoa Osherovich 	}
906b4f34597SNoa Osherovich 
907f95ef6cbSMaor Gottlieb 	if (field_avail(typeof(resp), tunnel_offloads_caps,
908f95ef6cbSMaor Gottlieb 			uhw->outlen)) {
909f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
910f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
911f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
912f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
913f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
914f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
915f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
916f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
917f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
918f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
919f95ef6cbSMaor Gottlieb 	}
920f95ef6cbSMaor Gottlieb 
921402ca536SBodong Wang 	if (uhw->outlen) {
922402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
923402ca536SBodong Wang 
924402ca536SBodong Wang 		if (err)
925402ca536SBodong Wang 			return err;
926402ca536SBodong Wang 	}
927402ca536SBodong Wang 
9281b5daf11SMajd Dibbiny 	return 0;
9291b5daf11SMajd Dibbiny }
930e126ba97SEli Cohen 
9311b5daf11SMajd Dibbiny enum mlx5_ib_width {
9321b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
9331b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
9341b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
9351b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
9361b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
9371b5daf11SMajd Dibbiny };
9381b5daf11SMajd Dibbiny 
9391b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
9401b5daf11SMajd Dibbiny 				  u8 *ib_width)
9411b5daf11SMajd Dibbiny {
9421b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
9431b5daf11SMajd Dibbiny 	int err = 0;
9441b5daf11SMajd Dibbiny 
9451b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
9461b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
9471b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
9481b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
9491b5daf11SMajd Dibbiny 			    (int)active_width);
9501b5daf11SMajd Dibbiny 		err = -EINVAL;
9511b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
9521b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
9531b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
9541b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
9551b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
9561b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
9571b5daf11SMajd Dibbiny 	} else {
9581b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
9591b5daf11SMajd Dibbiny 			    (int)active_width);
9601b5daf11SMajd Dibbiny 		err = -EINVAL;
9611b5daf11SMajd Dibbiny 	}
9621b5daf11SMajd Dibbiny 
9631b5daf11SMajd Dibbiny 	return err;
9641b5daf11SMajd Dibbiny }
9651b5daf11SMajd Dibbiny 
9661b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
9671b5daf11SMajd Dibbiny {
9681b5daf11SMajd Dibbiny 	switch (mtu) {
9691b5daf11SMajd Dibbiny 	case 256: return 1;
9701b5daf11SMajd Dibbiny 	case 512: return 2;
9711b5daf11SMajd Dibbiny 	case 1024: return 3;
9721b5daf11SMajd Dibbiny 	case 2048: return 4;
9731b5daf11SMajd Dibbiny 	case 4096: return 5;
9741b5daf11SMajd Dibbiny 	default:
9751b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
9761b5daf11SMajd Dibbiny 		return -1;
9771b5daf11SMajd Dibbiny 	}
9781b5daf11SMajd Dibbiny }
9791b5daf11SMajd Dibbiny 
9801b5daf11SMajd Dibbiny enum ib_max_vl_num {
9811b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
9821b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
9831b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
9841b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
9851b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
9861b5daf11SMajd Dibbiny };
9871b5daf11SMajd Dibbiny 
9881b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
9891b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
9901b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
9911b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
9921b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
9931b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
9941b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
9951b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
9961b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
9971b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
9981b5daf11SMajd Dibbiny };
9991b5daf11SMajd Dibbiny 
10001b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
10011b5daf11SMajd Dibbiny 				u8 *max_vl_num)
10021b5daf11SMajd Dibbiny {
10031b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
10041b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
10051b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
10061b5daf11SMajd Dibbiny 		break;
10071b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
10081b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
10091b5daf11SMajd Dibbiny 		break;
10101b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
10111b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
10121b5daf11SMajd Dibbiny 		break;
10131b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
10141b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
10151b5daf11SMajd Dibbiny 		break;
10161b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
10171b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
10181b5daf11SMajd Dibbiny 		break;
10191b5daf11SMajd Dibbiny 
10201b5daf11SMajd Dibbiny 	default:
10211b5daf11SMajd Dibbiny 		return -EINVAL;
10221b5daf11SMajd Dibbiny 	}
10231b5daf11SMajd Dibbiny 
10241b5daf11SMajd Dibbiny 	return 0;
10251b5daf11SMajd Dibbiny }
10261b5daf11SMajd Dibbiny 
10271b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
10281b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
10291b5daf11SMajd Dibbiny {
10301b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
10311b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
10321b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1033046339eaSSaeed Mahameed 	u16 max_mtu;
1034046339eaSSaeed Mahameed 	u16 oper_mtu;
10351b5daf11SMajd Dibbiny 	int err;
10361b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
10371b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
10381b5daf11SMajd Dibbiny 
10391b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
10401b5daf11SMajd Dibbiny 	if (!rep) {
10411b5daf11SMajd Dibbiny 		err = -ENOMEM;
10421b5daf11SMajd Dibbiny 		goto out;
10431b5daf11SMajd Dibbiny 	}
10441b5daf11SMajd Dibbiny 
1045c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
10461b5daf11SMajd Dibbiny 
10471b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
10481b5daf11SMajd Dibbiny 	if (err)
10491b5daf11SMajd Dibbiny 		goto out;
10501b5daf11SMajd Dibbiny 
10511b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
10521b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
10531b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
10541b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
10551b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
10561b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
10571b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
10581b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
10591b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
10601b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
10611b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
10621b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
10631b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
10641b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
1065eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
10661b5daf11SMajd Dibbiny 
10671b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
10681b5daf11SMajd Dibbiny 	if (err)
10691b5daf11SMajd Dibbiny 		goto out;
10701b5daf11SMajd Dibbiny 
10711b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
10721b5daf11SMajd Dibbiny 				     &props->active_width);
10731b5daf11SMajd Dibbiny 	if (err)
10741b5daf11SMajd Dibbiny 		goto out;
1075d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
10761b5daf11SMajd Dibbiny 	if (err)
10771b5daf11SMajd Dibbiny 		goto out;
10781b5daf11SMajd Dibbiny 
1079facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
10801b5daf11SMajd Dibbiny 
10811b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
10821b5daf11SMajd Dibbiny 
1083facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
10841b5daf11SMajd Dibbiny 
10851b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
10861b5daf11SMajd Dibbiny 
10871b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
10881b5daf11SMajd Dibbiny 	if (err)
10891b5daf11SMajd Dibbiny 		goto out;
10901b5daf11SMajd Dibbiny 
10911b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
10921b5daf11SMajd Dibbiny 				   &props->max_vl_num);
10931b5daf11SMajd Dibbiny out:
10941b5daf11SMajd Dibbiny 	kfree(rep);
1095e126ba97SEli Cohen 	return err;
1096e126ba97SEli Cohen }
1097e126ba97SEli Cohen 
1098e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1099e126ba97SEli Cohen 		       struct ib_port_attr *props)
1100e126ba97SEli Cohen {
1101095b0927SIlan Tayari 	unsigned int count;
1102095b0927SIlan Tayari 	int ret;
1103095b0927SIlan Tayari 
11041b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
11051b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1106095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1107095b0927SIlan Tayari 		break;
1108e126ba97SEli Cohen 
11091b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1110095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1111095b0927SIlan Tayari 		break;
11121b5daf11SMajd Dibbiny 
11133f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1114095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1115095b0927SIlan Tayari 		break;
11163f89a643SAchiad Shochat 
11171b5daf11SMajd Dibbiny 	default:
1118095b0927SIlan Tayari 		ret = -EINVAL;
1119e126ba97SEli Cohen 	}
1120095b0927SIlan Tayari 
1121095b0927SIlan Tayari 	if (!ret && props) {
1122095b0927SIlan Tayari 		count = mlx5_core_reserved_gids_count(to_mdev(ibdev)->mdev);
1123095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1124095b0927SIlan Tayari 	}
1125095b0927SIlan Tayari 	return ret;
1126e126ba97SEli Cohen }
1127e126ba97SEli Cohen 
1128e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1129e126ba97SEli Cohen 			     union ib_gid *gid)
1130e126ba97SEli Cohen {
11311b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11321b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1133e126ba97SEli Cohen 
11341b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
11351b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
11361b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1137e126ba97SEli Cohen 
11381b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
11391b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1140e126ba97SEli Cohen 
11411b5daf11SMajd Dibbiny 	default:
11421b5daf11SMajd Dibbiny 		return -EINVAL;
11431b5daf11SMajd Dibbiny 	}
1144e126ba97SEli Cohen 
1145e126ba97SEli Cohen }
1146e126ba97SEli Cohen 
1147e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1148e126ba97SEli Cohen 			      u16 *pkey)
1149e126ba97SEli Cohen {
11501b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11511b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1152e126ba97SEli Cohen 
11531b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
11541b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
11551b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1156e126ba97SEli Cohen 
11571b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
11581b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
11591b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
11601b5daf11SMajd Dibbiny 						 pkey);
11611b5daf11SMajd Dibbiny 	default:
11621b5daf11SMajd Dibbiny 		return -EINVAL;
1163e126ba97SEli Cohen 	}
11641b5daf11SMajd Dibbiny }
1165e126ba97SEli Cohen 
1166e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1167e126ba97SEli Cohen 				 struct ib_device_modify *props)
1168e126ba97SEli Cohen {
1169e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1170e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1171e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1172e126ba97SEli Cohen 	int err;
1173e126ba97SEli Cohen 
1174e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1175e126ba97SEli Cohen 		return -EOPNOTSUPP;
1176e126ba97SEli Cohen 
1177e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1178e126ba97SEli Cohen 		return 0;
1179e126ba97SEli Cohen 
1180e126ba97SEli Cohen 	/*
1181e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1182e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1183e126ba97SEli Cohen 	 */
1184bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
11859603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1186e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1187e126ba97SEli Cohen 	if (err)
1188e126ba97SEli Cohen 		return err;
1189e126ba97SEli Cohen 
1190bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1191e126ba97SEli Cohen 
1192e126ba97SEli Cohen 	return err;
1193e126ba97SEli Cohen }
1194e126ba97SEli Cohen 
1195cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1196cdbe33d0SEli Cohen 				u32 value)
1197cdbe33d0SEli Cohen {
1198cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1199cdbe33d0SEli Cohen 	int err;
1200cdbe33d0SEli Cohen 
1201cdbe33d0SEli Cohen 	err = mlx5_query_hca_vport_context(dev->mdev, 0,
1202cdbe33d0SEli Cohen 					   port_num, 0, &ctx);
1203cdbe33d0SEli Cohen 	if (err)
1204cdbe33d0SEli Cohen 		return err;
1205cdbe33d0SEli Cohen 
1206cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1207cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1208cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1209cdbe33d0SEli Cohen 		return -EINVAL;
1210cdbe33d0SEli Cohen 	}
1211cdbe33d0SEli Cohen 
1212cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1213cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1214cdbe33d0SEli Cohen 	err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1215cdbe33d0SEli Cohen 						 port_num, 0, &ctx);
1216cdbe33d0SEli Cohen 
1217cdbe33d0SEli Cohen 	return err;
1218cdbe33d0SEli Cohen }
1219cdbe33d0SEli Cohen 
1220e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1221e126ba97SEli Cohen 			       struct ib_port_modify *props)
1222e126ba97SEli Cohen {
1223e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1224e126ba97SEli Cohen 	struct ib_port_attr attr;
1225e126ba97SEli Cohen 	u32 tmp;
1226e126ba97SEli Cohen 	int err;
1227cdbe33d0SEli Cohen 	u32 change_mask;
1228cdbe33d0SEli Cohen 	u32 value;
1229cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1230cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1231cdbe33d0SEli Cohen 
1232ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1233ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1234ec255879SMajd Dibbiny 	 */
1235ec255879SMajd Dibbiny 	if (!is_ib)
1236ec255879SMajd Dibbiny 		return 0;
1237ec255879SMajd Dibbiny 
1238cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1239cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1240cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1241cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1242cdbe33d0SEli Cohen 	}
1243e126ba97SEli Cohen 
1244e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1245e126ba97SEli Cohen 
1246c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1247e126ba97SEli Cohen 	if (err)
1248e126ba97SEli Cohen 		goto out;
1249e126ba97SEli Cohen 
1250e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1251e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1252e126ba97SEli Cohen 
12539603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1254e126ba97SEli Cohen 
1255e126ba97SEli Cohen out:
1256e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1257e126ba97SEli Cohen 	return err;
1258e126ba97SEli Cohen }
1259e126ba97SEli Cohen 
126030aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
126130aa60b3SEli Cohen {
126230aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
126330aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
126430aa60b3SEli Cohen }
126530aa60b3SEli Cohen 
126631a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
126731a78a5aSYishai Hadas {
126831a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
126931a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
127031a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
127131a78a5aSYishai Hadas 
127231a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
127331a78a5aSYishai Hadas }
127431a78a5aSYishai Hadas 
1275b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1276b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
127731a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1278b037c29aSEli Cohen {
1279b037c29aSEli Cohen 	int uars_per_sys_page;
1280b037c29aSEli Cohen 	int bfregs_per_sys_page;
1281b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1282b037c29aSEli Cohen 
1283b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1284b037c29aSEli Cohen 		return -EINVAL;
1285b037c29aSEli Cohen 
1286b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1287b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1288b037c29aSEli Cohen 
1289b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1290b037c29aSEli Cohen 		return -ENOMEM;
1291b037c29aSEli Cohen 
1292b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1293b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
129431a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1295b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1296b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1297b037c29aSEli Cohen 		return -EINVAL;
1298b037c29aSEli Cohen 
129931a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
130031a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
130131a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
130231a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
130331a78a5aSYishai Hadas 
130431a78a5aSYishai 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",
1305b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1306b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
130731a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
130831a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1309b037c29aSEli Cohen 
1310b037c29aSEli Cohen 	return 0;
1311b037c29aSEli Cohen }
1312b037c29aSEli Cohen 
1313b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1314b037c29aSEli Cohen {
1315b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1316b037c29aSEli Cohen 	int err;
1317b037c29aSEli Cohen 	int i;
1318b037c29aSEli Cohen 
1319b037c29aSEli Cohen 	bfregi = &context->bfregi;
132031a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1321b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1322b037c29aSEli Cohen 		if (err)
1323b037c29aSEli Cohen 			goto error;
1324b037c29aSEli Cohen 
1325b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1326b037c29aSEli Cohen 	}
13274ed131d0SYishai Hadas 
13284ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
13294ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
13304ed131d0SYishai Hadas 
1331b037c29aSEli Cohen 	return 0;
1332b037c29aSEli Cohen 
1333b037c29aSEli Cohen error:
1334b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1335b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1336b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1337b037c29aSEli Cohen 
1338b037c29aSEli Cohen 	return err;
1339b037c29aSEli Cohen }
1340b037c29aSEli Cohen 
1341b037c29aSEli Cohen static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1342b037c29aSEli Cohen {
1343b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1344b037c29aSEli Cohen 	int err;
1345b037c29aSEli Cohen 	int i;
1346b037c29aSEli Cohen 
1347b037c29aSEli Cohen 	bfregi = &context->bfregi;
13484ed131d0SYishai Hadas 	for (i = 0; i < bfregi->num_sys_pages; i++) {
13494ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
13504ed131d0SYishai Hadas 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX) {
1351b037c29aSEli Cohen 			err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1352b037c29aSEli Cohen 			if (err) {
13534ed131d0SYishai Hadas 				mlx5_ib_warn(dev, "failed to free uar %d, err=%d\n", i, err);
1354b037c29aSEli Cohen 				return err;
1355b037c29aSEli Cohen 			}
1356b037c29aSEli Cohen 		}
13574ed131d0SYishai Hadas 	}
13584ed131d0SYishai Hadas 
1359b037c29aSEli Cohen 	return 0;
1360b037c29aSEli Cohen }
1361b037c29aSEli Cohen 
1362c85023e1SHuy Nguyen static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1363c85023e1SHuy Nguyen {
1364c85023e1SHuy Nguyen 	int err;
1365c85023e1SHuy Nguyen 
1366c85023e1SHuy Nguyen 	err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1367c85023e1SHuy Nguyen 	if (err)
1368c85023e1SHuy Nguyen 		return err;
1369c85023e1SHuy Nguyen 
1370c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1371c85023e1SHuy Nguyen 	    !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1372c85023e1SHuy Nguyen 		return err;
1373c85023e1SHuy Nguyen 
1374c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1375c85023e1SHuy Nguyen 	dev->user_td++;
1376c85023e1SHuy Nguyen 
1377c85023e1SHuy Nguyen 	if (dev->user_td == 2)
1378c85023e1SHuy Nguyen 		err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1379c85023e1SHuy Nguyen 
1380c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1381c85023e1SHuy Nguyen 	return err;
1382c85023e1SHuy Nguyen }
1383c85023e1SHuy Nguyen 
1384c85023e1SHuy Nguyen static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1385c85023e1SHuy Nguyen {
1386c85023e1SHuy Nguyen 	mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1387c85023e1SHuy Nguyen 
1388c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1389c85023e1SHuy Nguyen 	    !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1390c85023e1SHuy Nguyen 		return;
1391c85023e1SHuy Nguyen 
1392c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1393c85023e1SHuy Nguyen 	dev->user_td--;
1394c85023e1SHuy Nguyen 
1395c85023e1SHuy Nguyen 	if (dev->user_td < 2)
1396c85023e1SHuy Nguyen 		mlx5_nic_vport_update_local_lb(dev->mdev, false);
1397c85023e1SHuy Nguyen 
1398c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1399c85023e1SHuy Nguyen }
1400c85023e1SHuy Nguyen 
1401e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1402e126ba97SEli Cohen 						  struct ib_udata *udata)
1403e126ba97SEli Cohen {
1404e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1405b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1406b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1407e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
14082f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
140978c0f98cSEli Cohen 	int ver;
1410e126ba97SEli Cohen 	int err;
1411a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1412a168a41cSMajd Dibbiny 				     max_cqe_version);
1413b037c29aSEli Cohen 	bool lib_uar_4k;
1414e126ba97SEli Cohen 
1415e126ba97SEli Cohen 	if (!dev->ib_active)
1416e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1417e126ba97SEli Cohen 
1418e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
141978c0f98cSEli Cohen 		ver = 0;
1420e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
142178c0f98cSEli Cohen 		ver = 2;
142278c0f98cSEli Cohen 	else
142378c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
142478c0f98cSEli Cohen 
1425e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1426e126ba97SEli Cohen 	if (err)
1427e126ba97SEli Cohen 		return ERR_PTR(err);
1428e126ba97SEli Cohen 
1429b368d7cbSMatan Barak 	if (req.flags)
143078c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
143178c0f98cSEli Cohen 
1432f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1433b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1434b368d7cbSMatan Barak 
14352f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
14362f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
14372f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1438e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1439e126ba97SEli Cohen 
1440938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
14412cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1442938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1443b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1444938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1445938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1446938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1447938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1448938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1449f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1450f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1451f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
145230aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
145330aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
145430aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
145530aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1456b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1457b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1458e126ba97SEli Cohen 
1459e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1460e126ba97SEli Cohen 	if (!context)
1461e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1462e126ba97SEli Cohen 
146330aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
14642f5ff264SEli Cohen 	bfregi = &context->bfregi;
1465b037c29aSEli Cohen 
1466b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
146731a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1468b037c29aSEli Cohen 	if (err)
1469b037c29aSEli Cohen 		goto out_ctx;
1470b037c29aSEli Cohen 
14712f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1472b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
147331a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1474b037c29aSEli Cohen 				GFP_KERNEL);
1475b037c29aSEli Cohen 	if (!bfregi->count) {
1476e126ba97SEli Cohen 		err = -ENOMEM;
1477e126ba97SEli Cohen 		goto out_ctx;
1478e126ba97SEli Cohen 	}
1479e126ba97SEli Cohen 
1480b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1481b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1482e126ba97SEli Cohen 				    GFP_KERNEL);
1483b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1484e126ba97SEli Cohen 		err = -ENOMEM;
1485e126ba97SEli Cohen 		goto out_count;
1486e126ba97SEli Cohen 	}
1487e126ba97SEli Cohen 
1488b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1489b037c29aSEli Cohen 	if (err)
1490b037c29aSEli Cohen 		goto out_sys_pages;
1491b037c29aSEli Cohen 
1492b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1493b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1494b4cfe447SHaggai Eran #endif
1495b4cfe447SHaggai Eran 
14967d0cc6edSArtemy Kovalyov 	context->upd_xlt_page = __get_free_page(GFP_KERNEL);
14977d0cc6edSArtemy Kovalyov 	if (!context->upd_xlt_page) {
14987d0cc6edSArtemy Kovalyov 		err = -ENOMEM;
14997d0cc6edSArtemy Kovalyov 		goto out_uars;
15007d0cc6edSArtemy Kovalyov 	}
15017d0cc6edSArtemy Kovalyov 	mutex_init(&context->upd_xlt_page_mutex);
15027d0cc6edSArtemy Kovalyov 
1503146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1504c85023e1SHuy Nguyen 		err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
1505146d2f1aSmajd@mellanox.com 		if (err)
15067d0cc6edSArtemy Kovalyov 			goto out_page;
1507146d2f1aSmajd@mellanox.com 	}
1508146d2f1aSmajd@mellanox.com 
15097c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
1510ad9a3668SMajd Dibbiny 	mutex_init(&context->vma_private_list_mutex);
1511e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1512e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1513e126ba97SEli Cohen 
15142f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1515938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
1516b368d7cbSMatan Barak 
1517f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1518f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1519b368d7cbSMatan Barak 
1520402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
15216ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
15226ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1523402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1524402ca536SBodong Wang 	}
1525402ca536SBodong Wang 
152678984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
152778984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
152878984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
152978984898SOr Gerlitz 			resp.eth_min_inline++;
153078984898SOr Gerlitz 		}
153178984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
153278984898SOr Gerlitz 	}
153378984898SOr Gerlitz 
1534bc5c6eedSNoa Osherovich 	/*
1535bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1536bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1537bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1538bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1539bc5c6eedSNoa Osherovich 	 */
1540de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1541de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1542b368d7cbSMatan Barak 			resp.comp_mask |=
1543b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1544b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1545de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1546de8d6e02SEli Cohen 		}
1547f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.hca_core_clock_offset) +
1548402ca536SBodong Wang 					sizeof(resp.reserved2);
1549b368d7cbSMatan Barak 	}
1550b368d7cbSMatan Barak 
155130aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
155230aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
155330aa60b3SEli Cohen 
155430aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
155530aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
155630aa60b3SEli Cohen 
155731a78a5aSYishai Hadas 	if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
155831a78a5aSYishai Hadas 		resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
155931a78a5aSYishai Hadas 		resp.response_length += sizeof(resp.num_dyn_bfregs);
156031a78a5aSYishai Hadas 	}
156131a78a5aSYishai Hadas 
1562b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1563e126ba97SEli Cohen 	if (err)
1564146d2f1aSmajd@mellanox.com 		goto out_td;
1565e126ba97SEli Cohen 
15662f5ff264SEli Cohen 	bfregi->ver = ver;
15672f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1568f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
156930aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
157030aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1571f72300c5SHaggai Abramovsky 
1572e126ba97SEli Cohen 	return &context->ibucontext;
1573e126ba97SEli Cohen 
1574146d2f1aSmajd@mellanox.com out_td:
1575146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1576c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1577146d2f1aSmajd@mellanox.com 
15787d0cc6edSArtemy Kovalyov out_page:
15797d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
15807d0cc6edSArtemy Kovalyov 
1581e126ba97SEli Cohen out_uars:
1582b037c29aSEli Cohen 	deallocate_uars(dev, context);
1583b037c29aSEli Cohen 
1584b037c29aSEli Cohen out_sys_pages:
1585b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1586b037c29aSEli Cohen 
1587e126ba97SEli Cohen out_count:
15882f5ff264SEli Cohen 	kfree(bfregi->count);
1589e126ba97SEli Cohen 
1590e126ba97SEli Cohen out_ctx:
1591e126ba97SEli Cohen 	kfree(context);
1592b037c29aSEli Cohen 
1593e126ba97SEli Cohen 	return ERR_PTR(err);
1594e126ba97SEli Cohen }
1595e126ba97SEli Cohen 
1596e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1597e126ba97SEli Cohen {
1598e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1599e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1600b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1601e126ba97SEli Cohen 
1602b037c29aSEli Cohen 	bfregi = &context->bfregi;
1603146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1604c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1605146d2f1aSmajd@mellanox.com 
16067d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
1607b037c29aSEli Cohen 	deallocate_uars(dev, context);
1608b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
16092f5ff264SEli Cohen 	kfree(bfregi->count);
1610e126ba97SEli Cohen 	kfree(context);
1611e126ba97SEli Cohen 
1612e126ba97SEli Cohen 	return 0;
1613e126ba97SEli Cohen }
1614e126ba97SEli Cohen 
1615b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
16164ed131d0SYishai Hadas 				 int uar_idx)
1617e126ba97SEli Cohen {
1618b037c29aSEli Cohen 	int fw_uars_per_page;
1619b037c29aSEli Cohen 
1620b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1621b037c29aSEli Cohen 
16224ed131d0SYishai Hadas 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
1623e126ba97SEli Cohen }
1624e126ba97SEli Cohen 
1625e126ba97SEli Cohen static int get_command(unsigned long offset)
1626e126ba97SEli Cohen {
1627e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1628e126ba97SEli Cohen }
1629e126ba97SEli Cohen 
1630e126ba97SEli Cohen static int get_arg(unsigned long offset)
1631e126ba97SEli Cohen {
1632e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1633e126ba97SEli Cohen }
1634e126ba97SEli Cohen 
1635e126ba97SEli Cohen static int get_index(unsigned long offset)
1636e126ba97SEli Cohen {
1637e126ba97SEli Cohen 	return get_arg(offset);
1638e126ba97SEli Cohen }
1639e126ba97SEli Cohen 
16404ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
16414ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
16424ed131d0SYishai Hadas {
16434ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
16444ed131d0SYishai Hadas }
16454ed131d0SYishai Hadas 
16467c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
16477c2344c3SMaor Gottlieb {
16487c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
16497c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
16507c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
16517c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
16527c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
16537c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
16547c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
16557c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
16567c2344c3SMaor Gottlieb 	 */
16577c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
16587c2344c3SMaor Gottlieb }
16597c2344c3SMaor Gottlieb 
16607c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
16617c2344c3SMaor Gottlieb {
16627c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
16637c2344c3SMaor Gottlieb 
16647c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
16657c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
16667c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
16677c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
16687c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
16697c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
16707c2344c3SMaor Gottlieb 	 * process is exiting.
16717c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
16727c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
16737c2344c3SMaor Gottlieb 	 */
16747c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
16757c2344c3SMaor Gottlieb 
16767c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
16777c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
16787c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
16797c2344c3SMaor Gottlieb 	 */
16807c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
1681ad9a3668SMajd Dibbiny 	mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
16827c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
1683ad9a3668SMajd Dibbiny 	mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
16847c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
16857c2344c3SMaor Gottlieb }
16867c2344c3SMaor Gottlieb 
16877c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
16887c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
16897c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
16907c2344c3SMaor Gottlieb };
16917c2344c3SMaor Gottlieb 
16927c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
16937c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
16947c2344c3SMaor Gottlieb {
16957c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
16967c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
16977c2344c3SMaor Gottlieb 
16987c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
16997c2344c3SMaor Gottlieb 	if (!vma_prv)
17007c2344c3SMaor Gottlieb 		return -ENOMEM;
17017c2344c3SMaor Gottlieb 
17027c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
1703ad9a3668SMajd Dibbiny 	vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
17047c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
17057c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
17067c2344c3SMaor Gottlieb 
1707ad9a3668SMajd Dibbiny 	mutex_lock(&ctx->vma_private_list_mutex);
17087c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
1709ad9a3668SMajd Dibbiny 	mutex_unlock(&ctx->vma_private_list_mutex);
17107c2344c3SMaor Gottlieb 
17117c2344c3SMaor Gottlieb 	return 0;
17127c2344c3SMaor Gottlieb }
17137c2344c3SMaor Gottlieb 
17147c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
17157c2344c3SMaor Gottlieb {
17167c2344c3SMaor Gottlieb 	int ret;
17177c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
17187c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
17197c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
17207c2344c3SMaor Gottlieb 	struct task_struct *owning_process  = NULL;
17217c2344c3SMaor Gottlieb 	struct mm_struct   *owning_mm       = NULL;
17227c2344c3SMaor Gottlieb 
17237c2344c3SMaor Gottlieb 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
17247c2344c3SMaor Gottlieb 	if (!owning_process)
17257c2344c3SMaor Gottlieb 		return;
17267c2344c3SMaor Gottlieb 
17277c2344c3SMaor Gottlieb 	owning_mm = get_task_mm(owning_process);
17287c2344c3SMaor Gottlieb 	if (!owning_mm) {
17297c2344c3SMaor Gottlieb 		pr_info("no mm, disassociate ucontext is pending task termination\n");
17307c2344c3SMaor Gottlieb 		while (1) {
17317c2344c3SMaor Gottlieb 			put_task_struct(owning_process);
17327c2344c3SMaor Gottlieb 			usleep_range(1000, 2000);
17337c2344c3SMaor Gottlieb 			owning_process = get_pid_task(ibcontext->tgid,
17347c2344c3SMaor Gottlieb 						      PIDTYPE_PID);
17357c2344c3SMaor Gottlieb 			if (!owning_process ||
17367c2344c3SMaor Gottlieb 			    owning_process->state == TASK_DEAD) {
17377c2344c3SMaor Gottlieb 				pr_info("disassociate ucontext done, task was terminated\n");
17387c2344c3SMaor Gottlieb 				/* in case task was dead need to release the
17397c2344c3SMaor Gottlieb 				 * task struct.
17407c2344c3SMaor Gottlieb 				 */
17417c2344c3SMaor Gottlieb 				if (owning_process)
17427c2344c3SMaor Gottlieb 					put_task_struct(owning_process);
17437c2344c3SMaor Gottlieb 				return;
17447c2344c3SMaor Gottlieb 			}
17457c2344c3SMaor Gottlieb 		}
17467c2344c3SMaor Gottlieb 	}
17477c2344c3SMaor Gottlieb 
17487c2344c3SMaor Gottlieb 	/* need to protect from a race on closing the vma as part of
17497c2344c3SMaor Gottlieb 	 * mlx5_ib_vma_close.
17507c2344c3SMaor Gottlieb 	 */
1751ecc7d83bSMaor Gottlieb 	down_write(&owning_mm->mmap_sem);
1752ad9a3668SMajd Dibbiny 	mutex_lock(&context->vma_private_list_mutex);
17537c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
17547c2344c3SMaor Gottlieb 				 list) {
17557c2344c3SMaor Gottlieb 		vma = vma_private->vma;
17567c2344c3SMaor Gottlieb 		ret = zap_vma_ptes(vma, vma->vm_start,
17577c2344c3SMaor Gottlieb 				   PAGE_SIZE);
17587c2344c3SMaor Gottlieb 		WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
17597c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
17607c2344c3SMaor Gottlieb 		 * not access ops any more.
17617c2344c3SMaor Gottlieb 		 */
176213776612SMaor Gottlieb 		vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
17637c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
17647c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
17657c2344c3SMaor Gottlieb 		kfree(vma_private);
17667c2344c3SMaor Gottlieb 	}
1767ad9a3668SMajd Dibbiny 	mutex_unlock(&context->vma_private_list_mutex);
1768ecc7d83bSMaor Gottlieb 	up_write(&owning_mm->mmap_sem);
17697c2344c3SMaor Gottlieb 	mmput(owning_mm);
17707c2344c3SMaor Gottlieb 	put_task_struct(owning_process);
17717c2344c3SMaor Gottlieb }
17727c2344c3SMaor Gottlieb 
177337aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1774e126ba97SEli Cohen {
177537aa5c36SGuy Levi 	switch (cmd) {
177637aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
177737aa5c36SGuy Levi 		return "WC";
1778e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
177937aa5c36SGuy Levi 		return "best effort WC";
178037aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
178137aa5c36SGuy Levi 		return "NC";
178237aa5c36SGuy Levi 	default:
178337aa5c36SGuy Levi 		return NULL;
178437aa5c36SGuy Levi 	}
178537aa5c36SGuy Levi }
178637aa5c36SGuy Levi 
178737aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
17887c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
17897c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
179037aa5c36SGuy Levi {
17912f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
179237aa5c36SGuy Levi 	int err;
179337aa5c36SGuy Levi 	unsigned long idx;
179437aa5c36SGuy Levi 	phys_addr_t pfn, pa;
179537aa5c36SGuy Levi 	pgprot_t prot;
17964ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
17974ed131d0SYishai Hadas 	u32 uar_index;
17984ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
17994ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
18004ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
1801b037c29aSEli Cohen 
1802b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1803b037c29aSEli Cohen 		return -EINVAL;
1804b037c29aSEli Cohen 
18054ed131d0SYishai Hadas 	if (dyn_uar)
18064ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
18074ed131d0SYishai Hadas 	else
1808b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
18094ed131d0SYishai Hadas 
18104ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
18114ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
18124ed131d0SYishai Hadas 			     idx, max_valid_idx);
1813b037c29aSEli Cohen 		return -EINVAL;
1814b037c29aSEli Cohen 	}
181537aa5c36SGuy Levi 
181637aa5c36SGuy Levi 	switch (cmd) {
181737aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
18184ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
181937aa5c36SGuy Levi /* Some architectures don't support WC memory */
182037aa5c36SGuy Levi #if defined(CONFIG_X86)
182137aa5c36SGuy Levi 		if (!pat_enabled())
182237aa5c36SGuy Levi 			return -EPERM;
182337aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
182437aa5c36SGuy Levi 			return -EPERM;
182537aa5c36SGuy Levi #endif
182637aa5c36SGuy Levi 	/* fall through */
182737aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
182837aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
182937aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
183037aa5c36SGuy Levi 		break;
183137aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
183237aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
183337aa5c36SGuy Levi 		break;
183437aa5c36SGuy Levi 	default:
183537aa5c36SGuy Levi 		return -EINVAL;
183637aa5c36SGuy Levi 	}
183737aa5c36SGuy Levi 
18384ed131d0SYishai Hadas 	if (dyn_uar) {
18394ed131d0SYishai Hadas 		int uars_per_page;
18404ed131d0SYishai Hadas 
18414ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
18424ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
18434ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
18444ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
18454ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
18464ed131d0SYishai Hadas 			return -EINVAL;
18474ed131d0SYishai Hadas 		}
18484ed131d0SYishai Hadas 
18494ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
18504ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
18514ed131d0SYishai Hadas 		 * page holds its count.
18524ed131d0SYishai Hadas 		 */
18534ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
18544ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
18554ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
18564ed131d0SYishai Hadas 			return -EINVAL;
18574ed131d0SYishai Hadas 		}
18584ed131d0SYishai Hadas 
18594ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
18604ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
18614ed131d0SYishai Hadas 
18624ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
18634ed131d0SYishai Hadas 		if (err) {
18644ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
18654ed131d0SYishai Hadas 			goto free_bfreg;
18664ed131d0SYishai Hadas 		}
18674ed131d0SYishai Hadas 	} else {
18684ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
18694ed131d0SYishai Hadas 	}
18704ed131d0SYishai Hadas 
18714ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
187237aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1873e126ba97SEli Cohen 
187437aa5c36SGuy Levi 	vma->vm_page_prot = prot;
187537aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
187637aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
187737aa5c36SGuy Levi 	if (err) {
187837aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
187937aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
18804ed131d0SYishai Hadas 		err = -EAGAIN;
18814ed131d0SYishai Hadas 		goto err;
188237aa5c36SGuy Levi 	}
1883e126ba97SEli Cohen 
188437aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
188537aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
188637aa5c36SGuy Levi 		    vma->vm_start, &pa);
188737aa5c36SGuy Levi 
18884ed131d0SYishai Hadas 	err = mlx5_ib_set_vma_data(vma, context);
18894ed131d0SYishai Hadas 	if (err)
18904ed131d0SYishai Hadas 		goto err;
18914ed131d0SYishai Hadas 
18924ed131d0SYishai Hadas 	if (dyn_uar)
18934ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
18944ed131d0SYishai Hadas 	return 0;
18954ed131d0SYishai Hadas 
18964ed131d0SYishai Hadas err:
18974ed131d0SYishai Hadas 	if (!dyn_uar)
18984ed131d0SYishai Hadas 		return err;
18994ed131d0SYishai Hadas 
19004ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
19014ed131d0SYishai Hadas 
19024ed131d0SYishai Hadas free_bfreg:
19034ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
19044ed131d0SYishai Hadas 
19054ed131d0SYishai Hadas 	return err;
190637aa5c36SGuy Levi }
190737aa5c36SGuy Levi 
190837aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
190937aa5c36SGuy Levi {
191037aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
191137aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
191237aa5c36SGuy Levi 	unsigned long command;
191337aa5c36SGuy Levi 	phys_addr_t pfn;
191437aa5c36SGuy Levi 
191537aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
191637aa5c36SGuy Levi 	switch (command) {
191737aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
191837aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
191937aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
19204ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
19217c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
1922e126ba97SEli Cohen 
1923e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1924e126ba97SEli Cohen 		return -ENOSYS;
1925e126ba97SEli Cohen 
1926d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
1927d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1928d69e3bcfSMatan Barak 			return -EINVAL;
1929d69e3bcfSMatan Barak 
19306cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
1931d69e3bcfSMatan Barak 			return -EPERM;
1932d69e3bcfSMatan Barak 
1933d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
1934d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
1935d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
1936d69e3bcfSMatan Barak 
1937d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1938d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
1939d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1940d69e3bcfSMatan Barak 			PAGE_SHIFT;
1941d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1942d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
1943d69e3bcfSMatan Barak 			return -EAGAIN;
1944d69e3bcfSMatan Barak 
1945d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1946d69e3bcfSMatan Barak 			    vma->vm_start,
1947d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
1948d69e3bcfSMatan Barak 		break;
1949d69e3bcfSMatan Barak 
1950e126ba97SEli Cohen 	default:
1951e126ba97SEli Cohen 		return -EINVAL;
1952e126ba97SEli Cohen 	}
1953e126ba97SEli Cohen 
1954e126ba97SEli Cohen 	return 0;
1955e126ba97SEli Cohen }
1956e126ba97SEli Cohen 
1957e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1958e126ba97SEli Cohen 				      struct ib_ucontext *context,
1959e126ba97SEli Cohen 				      struct ib_udata *udata)
1960e126ba97SEli Cohen {
1961e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
1962e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1963e126ba97SEli Cohen 	int err;
1964e126ba97SEli Cohen 
1965e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1966e126ba97SEli Cohen 	if (!pd)
1967e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1968e126ba97SEli Cohen 
19699603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1970e126ba97SEli Cohen 	if (err) {
1971e126ba97SEli Cohen 		kfree(pd);
1972e126ba97SEli Cohen 		return ERR_PTR(err);
1973e126ba97SEli Cohen 	}
1974e126ba97SEli Cohen 
1975e126ba97SEli Cohen 	if (context) {
1976e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1977e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
19789603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1979e126ba97SEli Cohen 			kfree(pd);
1980e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1981e126ba97SEli Cohen 		}
1982e126ba97SEli Cohen 	}
1983e126ba97SEli Cohen 
1984e126ba97SEli Cohen 	return &pd->ibpd;
1985e126ba97SEli Cohen }
1986e126ba97SEli Cohen 
1987e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1988e126ba97SEli Cohen {
1989e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1990e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1991e126ba97SEli Cohen 
19929603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1993e126ba97SEli Cohen 	kfree(mpd);
1994e126ba97SEli Cohen 
1995e126ba97SEli Cohen 	return 0;
1996e126ba97SEli Cohen }
1997e126ba97SEli Cohen 
1998466fa6d2SMaor Gottlieb enum {
1999466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
2000466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
2001466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_INNER_BIT
2002466fa6d2SMaor Gottlieb };
2003038d2ef8SMaor Gottlieb 
2004466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
2005466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2006466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
2007466fa6d2SMaor Gottlieb 
2008466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
2009466fa6d2SMaor Gottlieb {
2010466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
2011466fa6d2SMaor Gottlieb 
2012466fa6d2SMaor Gottlieb 	match_criteria_enable =
2013466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2014466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
2015466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2016466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2017466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
2018466fa6d2SMaor Gottlieb 	match_criteria_enable |=
2019466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2020466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
2021466fa6d2SMaor Gottlieb 
2022466fa6d2SMaor Gottlieb 	return match_criteria_enable;
2023038d2ef8SMaor Gottlieb }
2024038d2ef8SMaor Gottlieb 
2025ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2026ca0d4753SMaor Gottlieb {
2027ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2028ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2029ca0d4753SMaor Gottlieb }
2030ca0d4753SMaor Gottlieb 
20312d1e697eSMoses Reuben static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
20322d1e697eSMoses Reuben 			   bool inner)
20332d1e697eSMoses Reuben {
20342d1e697eSMoses Reuben 	if (inner) {
20352d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
20362d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
20372d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
20382d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
20392d1e697eSMoses Reuben 	} else {
20402d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
20412d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
20422d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
20432d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
20442d1e697eSMoses Reuben 	}
20452d1e697eSMoses Reuben }
20462d1e697eSMoses Reuben 
2047ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2048ca0d4753SMaor Gottlieb {
2049ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2050ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2051ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2052ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2053ca0d4753SMaor Gottlieb }
2054ca0d4753SMaor Gottlieb 
2055c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
2056c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
2057ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
2058466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
2059c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
2060ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
20612ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
2062a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
2063c47ac6aeSMaor Gottlieb 
2064c47ac6aeSMaor Gottlieb /* Field is the last supported field */
2065c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
2066c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
2067c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
2068c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
2069c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
2070c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
2071c47ac6aeSMaor Gottlieb 
207219cc7524SAriel Levkovich #define IPV4_VERSION 4
207319cc7524SAriel Levkovich #define IPV6_VERSION 6
207419cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
207519cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
2076a22ed86cSSlava Shwartsman 			   u32 *tag_id, bool *is_drop)
2077038d2ef8SMaor Gottlieb {
2078466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2079466fa6d2SMaor Gottlieb 					   misc_parameters);
2080466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2081466fa6d2SMaor Gottlieb 					   misc_parameters);
20822d1e697eSMoses Reuben 	void *headers_c;
20832d1e697eSMoses Reuben 	void *headers_v;
208419cc7524SAriel Levkovich 	int match_ipv;
2085466fa6d2SMaor Gottlieb 
20862d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
20872d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
20882d1e697eSMoses Reuben 					 inner_headers);
20892d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
20902d1e697eSMoses Reuben 					 inner_headers);
209119cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
209219cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
20932d1e697eSMoses Reuben 	} else {
20942d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
20952d1e697eSMoses Reuben 					 outer_headers);
20962d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
20972d1e697eSMoses Reuben 					 outer_headers);
209819cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
209919cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
21002d1e697eSMoses Reuben 	}
21012d1e697eSMoses Reuben 
21022d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2103038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2104c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
21051ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2106038d2ef8SMaor Gottlieb 
21072d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2108038d2ef8SMaor Gottlieb 					     dmac_47_16),
2109038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
21102d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2111038d2ef8SMaor Gottlieb 					     dmac_47_16),
2112038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2113038d2ef8SMaor Gottlieb 
21142d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2115ee3da804SMaor Gottlieb 					     smac_47_16),
2116ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
21172d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2118ee3da804SMaor Gottlieb 					     smac_47_16),
2119ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2120ee3da804SMaor Gottlieb 
2121038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
21222d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
212310543365SMohamad Haj Yahia 				 cvlan_tag, 1);
21242d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
212510543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2126038d2ef8SMaor Gottlieb 
21272d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2128038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
21292d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2130038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2131038d2ef8SMaor Gottlieb 
21322d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2133038d2ef8SMaor Gottlieb 				 first_cfi,
2134038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
21352d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2136038d2ef8SMaor Gottlieb 				 first_cfi,
2137038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2138038d2ef8SMaor Gottlieb 
21392d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2140038d2ef8SMaor Gottlieb 				 first_prio,
2141038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
21422d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2143038d2ef8SMaor Gottlieb 				 first_prio,
2144038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2145038d2ef8SMaor Gottlieb 		}
21462d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2147038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
21482d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2149038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2150038d2ef8SMaor Gottlieb 		break;
2151038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2152c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
21531ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2154038d2ef8SMaor Gottlieb 
215519cc7524SAriel Levkovich 		if (match_ipv) {
215619cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
215719cc7524SAriel Levkovich 				 ip_version, 0xf);
215819cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
215919cc7524SAriel Levkovich 				 ip_version, IPV4_VERSION);
216019cc7524SAriel Levkovich 		} else {
21612d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2162038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
21632d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2164038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
216519cc7524SAriel Levkovich 		}
2166038d2ef8SMaor Gottlieb 
21672d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2168038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2169038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2170038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
21712d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2172038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2173038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2174038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
21752d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2176038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2177038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2178038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
21792d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2180038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2181038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2182038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2183ca0d4753SMaor Gottlieb 
21842d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2185ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2186ca0d4753SMaor Gottlieb 
21872d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2188ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
2189038d2ef8SMaor Gottlieb 		break;
2190026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2191c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
21921ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2193026bae0cSMaor Gottlieb 
219419cc7524SAriel Levkovich 		if (match_ipv) {
219519cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
219619cc7524SAriel Levkovich 				 ip_version, 0xf);
219719cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
219819cc7524SAriel Levkovich 				 ip_version, IPV6_VERSION);
219919cc7524SAriel Levkovich 		} else {
22002d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2201026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
22022d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2203026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
220419cc7524SAriel Levkovich 		}
2205026bae0cSMaor Gottlieb 
22062d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2207026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2208026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2209026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
22102d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2211026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2212026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2213026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
22142d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2215026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2216026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2217026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
22182d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2219026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2220026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2221026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2222466fa6d2SMaor Gottlieb 
22232d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2224466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2225466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2226466fa6d2SMaor Gottlieb 
22272d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2228466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
2229466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
2230466fa6d2SMaor Gottlieb 
22312d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
22322d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
22332d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
22342d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
22352d1e697eSMoses Reuben 
2236026bae0cSMaor Gottlieb 		break;
2237038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2238c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2239c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
22401ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2241038d2ef8SMaor Gottlieb 
22422d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2243038d2ef8SMaor Gottlieb 			 0xff);
22442d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2245038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
2246038d2ef8SMaor Gottlieb 
22472d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2248038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
22492d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2250038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2251038d2ef8SMaor Gottlieb 
22522d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2253038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
22542d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2255038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2256038d2ef8SMaor Gottlieb 		break;
2257038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2258c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2259c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
22601ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2261038d2ef8SMaor Gottlieb 
22622d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2263038d2ef8SMaor Gottlieb 			 0xff);
22642d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2265038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
2266038d2ef8SMaor Gottlieb 
22672d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2268038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
22692d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2270038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2271038d2ef8SMaor Gottlieb 
22722d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2273038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
22742d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2275038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2276038d2ef8SMaor Gottlieb 		break;
2277ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2278ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2279ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
22801ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2281ffb30d8fSMoses Reuben 
2282ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2283ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2284ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2285ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2286ffb30d8fSMoses Reuben 		break;
22872ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
22882ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
22892ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
22902ac693f9SMoses Reuben 			return -EOPNOTSUPP;
22912ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
22922ac693f9SMoses Reuben 			return -EINVAL;
22932ac693f9SMoses Reuben 
22942ac693f9SMoses Reuben 		*tag_id = ib_spec->flow_tag.tag_id;
22952ac693f9SMoses Reuben 		break;
2296a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2297a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2298a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2299a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2300a22ed86cSSlava Shwartsman 		*is_drop = true;
2301a22ed86cSSlava Shwartsman 		break;
2302038d2ef8SMaor Gottlieb 	default:
2303038d2ef8SMaor Gottlieb 		return -EINVAL;
2304038d2ef8SMaor Gottlieb 	}
2305038d2ef8SMaor Gottlieb 
2306038d2ef8SMaor Gottlieb 	return 0;
2307038d2ef8SMaor Gottlieb }
2308038d2ef8SMaor Gottlieb 
2309038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
2310038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
2311038d2ef8SMaor Gottlieb  * could steal other multicast packets.
2312038d2ef8SMaor Gottlieb  */
2313a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2314038d2ef8SMaor Gottlieb {
231581e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
2316038d2ef8SMaor Gottlieb 
2317038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2318038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
2319038d2ef8SMaor Gottlieb 		return false;
2320038d2ef8SMaor Gottlieb 
232181e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
232281e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
232381e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
2324038d2ef8SMaor Gottlieb 
232581e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
232681e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
232781e30880SYishai Hadas 			return true;
232881e30880SYishai Hadas 
232981e30880SYishai Hadas 		return false;
233081e30880SYishai Hadas 	}
233181e30880SYishai Hadas 
233281e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
233381e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
233481e30880SYishai Hadas 
233581e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2336038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2337038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
2338038d2ef8SMaor Gottlieb 	}
2339038d2ef8SMaor Gottlieb 
234081e30880SYishai Hadas 	return false;
234181e30880SYishai Hadas }
234281e30880SYishai Hadas 
234319cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
234419cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
23450f750966SAriel Levkovich 			       bool check_inner)
2346038d2ef8SMaor Gottlieb {
2347038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
234819cc7524SAriel Levkovich 	int match_ipv = check_inner ?
234919cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
235019cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
235119cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
235219cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
23530f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
23540f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
23550f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
23560f750966SAriel Levkovich 	bool has_ethertype = false;
2357038d2ef8SMaor Gottlieb 	unsigned int spec_index;
23580f750966SAriel Levkovich 	bool mask_valid = true;
23590f750966SAriel Levkovich 	u16 eth_type = 0;
23600f750966SAriel Levkovich 	bool type_valid;
2361038d2ef8SMaor Gottlieb 
2362038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
2363038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
23640f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2365038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
23660f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
23670f750966SAriel Levkovich 				      htons(0xffff));
23680f750966SAriel Levkovich 			has_ethertype = true;
23690f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
23700f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
23710f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
23720f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
2373038d2ef8SMaor Gottlieb 		}
2374038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
2375038d2ef8SMaor Gottlieb 	}
23760f750966SAriel Levkovich 
23770f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
23780f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
23790f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
23800f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
23810f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
23820f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
238319cc7524SAriel Levkovich 
238419cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
238519cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
238619cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
23870f750966SAriel Levkovich 	}
23880f750966SAriel Levkovich 
23890f750966SAriel Levkovich 	return type_valid;
23900f750966SAriel Levkovich }
23910f750966SAriel Levkovich 
239219cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
239319cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
23940f750966SAriel Levkovich {
239519cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
239619cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
2397038d2ef8SMaor Gottlieb }
2398038d2ef8SMaor Gottlieb 
2399038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
2400038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
2401038d2ef8SMaor Gottlieb {
2402038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
2403038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
2404038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
2405038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
2406038d2ef8SMaor Gottlieb 	}
2407038d2ef8SMaor Gottlieb }
2408038d2ef8SMaor Gottlieb 
2409038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2410038d2ef8SMaor Gottlieb {
2411038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2412038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2413038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2414038d2ef8SMaor Gottlieb 							  ibflow);
2415038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2416038d2ef8SMaor Gottlieb 
2417038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2418038d2ef8SMaor Gottlieb 
2419038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
242074491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2421cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
2422038d2ef8SMaor Gottlieb 		list_del(&iter->list);
2423038d2ef8SMaor Gottlieb 		kfree(iter);
2424038d2ef8SMaor Gottlieb 	}
2425038d2ef8SMaor Gottlieb 
242674491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
24275497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
2428038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2429038d2ef8SMaor Gottlieb 
2430038d2ef8SMaor Gottlieb 	kfree(handler);
2431038d2ef8SMaor Gottlieb 
2432038d2ef8SMaor Gottlieb 	return 0;
2433038d2ef8SMaor Gottlieb }
2434038d2ef8SMaor Gottlieb 
243535d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
243635d19011SMaor Gottlieb {
243735d19011SMaor Gottlieb 	priority *= 2;
243835d19011SMaor Gottlieb 	if (!dont_trap)
243935d19011SMaor Gottlieb 		priority++;
244035d19011SMaor Gottlieb 	return priority;
244135d19011SMaor Gottlieb }
244235d19011SMaor Gottlieb 
2443cc0e5d42SMaor Gottlieb enum flow_table_type {
2444cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
2445cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
2446cc0e5d42SMaor Gottlieb };
2447cc0e5d42SMaor Gottlieb 
244800b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
244900b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
2450038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
2451cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
2452cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
2453038d2ef8SMaor Gottlieb {
245435d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
2455038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
2456038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
2457038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
2458dac388efSMaor Gottlieb 	int max_table_size;
2459038d2ef8SMaor Gottlieb 	int num_entries;
2460038d2ef8SMaor Gottlieb 	int num_groups;
2461038d2ef8SMaor Gottlieb 	int priority;
2462038d2ef8SMaor Gottlieb 	int err = 0;
2463038d2ef8SMaor Gottlieb 
2464dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2465dac388efSMaor Gottlieb 						       log_max_ft_size));
2466038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
246735d19011SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr) &&
246835d19011SMaor Gottlieb 		    !dont_trap)
2469038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
2470038d2ef8SMaor Gottlieb 		else
247135d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
247235d19011SMaor Gottlieb 							dont_trap);
2473038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2474038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
2475038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
2476038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
2477038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[priority];
2478038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2479038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2480038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2481038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
2482038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
2483038d2ef8SMaor Gottlieb 					 &num_entries,
2484038d2ef8SMaor Gottlieb 					 &num_groups);
2485038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
2486cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2487cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2488cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
2489cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
2490cc0e5d42SMaor Gottlieb 
2491cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2492cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2493cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2494cc0e5d42SMaor Gottlieb 
2495cc0e5d42SMaor Gottlieb 		prio = &dev->flow_db.sniffer[ft_type];
2496cc0e5d42SMaor Gottlieb 		priority = 0;
2497cc0e5d42SMaor Gottlieb 		num_entries = 1;
2498cc0e5d42SMaor Gottlieb 		num_groups = 1;
2499038d2ef8SMaor Gottlieb 	}
2500038d2ef8SMaor Gottlieb 
2501038d2ef8SMaor Gottlieb 	if (!ns)
2502038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
2503038d2ef8SMaor Gottlieb 
2504dac388efSMaor Gottlieb 	if (num_entries > max_table_size)
2505dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2506dac388efSMaor Gottlieb 
2507038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
2508038d2ef8SMaor Gottlieb 	if (!ft) {
2509038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2510038d2ef8SMaor Gottlieb 							 num_entries,
2511d63cd286SMaor Gottlieb 							 num_groups,
2512c9f1b073SHadar Hen Zion 							 0, 0);
2513038d2ef8SMaor Gottlieb 
2514038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
2515038d2ef8SMaor Gottlieb 			prio->refcount = 0;
2516038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
2517038d2ef8SMaor Gottlieb 		} else {
2518038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
2519038d2ef8SMaor Gottlieb 		}
2520038d2ef8SMaor Gottlieb 	}
2521038d2ef8SMaor Gottlieb 
2522038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
2523038d2ef8SMaor Gottlieb }
2524038d2ef8SMaor Gottlieb 
2525a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
2526a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
2527a550ddfcSYishai Hadas 			    u32 underlay_qpn)
2528a550ddfcSYishai Hadas {
2529a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2530a550ddfcSYishai Hadas 					   spec->match_criteria,
2531a550ddfcSYishai Hadas 					   misc_parameters);
2532a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2533a550ddfcSYishai Hadas 					   misc_parameters);
2534a550ddfcSYishai Hadas 
2535a550ddfcSYishai Hadas 	if (underlay_qpn &&
2536a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2537a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
2538a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
2539a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
2540a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
2541a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
2542a550ddfcSYishai Hadas 	}
2543a550ddfcSYishai Hadas }
2544a550ddfcSYishai Hadas 
2545a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2546038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
2547dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
2548a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
2549a550ddfcSYishai Hadas 						      u32 underlay_qpn)
2550038d2ef8SMaor Gottlieb {
2551038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
2552038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
255366958ed9SHadar Hen Zion 	struct mlx5_flow_act flow_act = {0};
2554c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
2555a22ed86cSSlava Shwartsman 	struct mlx5_flow_destination *rule_dst = dst;
2556dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
2557038d2ef8SMaor Gottlieb 	unsigned int spec_index;
25582ac693f9SMoses Reuben 	u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
2559a22ed86cSSlava Shwartsman 	bool is_drop = false;
2560038d2ef8SMaor Gottlieb 	int err = 0;
2561a22ed86cSSlava Shwartsman 	int dest_num = 1;
2562038d2ef8SMaor Gottlieb 
256319cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
2564038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2565038d2ef8SMaor Gottlieb 
25661b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
2567038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
2568c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
2569038d2ef8SMaor Gottlieb 		err = -ENOMEM;
2570038d2ef8SMaor Gottlieb 		goto free;
2571038d2ef8SMaor Gottlieb 	}
2572038d2ef8SMaor Gottlieb 
2573038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
2574038d2ef8SMaor Gottlieb 
2575038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
257619cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
2577a22ed86cSSlava Shwartsman 				      spec->match_value,
2578a22ed86cSSlava Shwartsman 				      ib_flow, &flow_tag, &is_drop);
2579038d2ef8SMaor Gottlieb 		if (err < 0)
2580038d2ef8SMaor Gottlieb 			goto free;
2581038d2ef8SMaor Gottlieb 
2582038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2583038d2ef8SMaor Gottlieb 	}
2584038d2ef8SMaor Gottlieb 
2585a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
2586a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
2587a550ddfcSYishai Hadas 
2588466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
2589a22ed86cSSlava Shwartsman 	if (is_drop) {
2590a22ed86cSSlava Shwartsman 		flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2591a22ed86cSSlava Shwartsman 		rule_dst = NULL;
2592a22ed86cSSlava Shwartsman 		dest_num = 0;
2593a22ed86cSSlava Shwartsman 	} else {
259466958ed9SHadar Hen Zion 		flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
259535d19011SMaor Gottlieb 		    MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2596a22ed86cSSlava Shwartsman 	}
25972ac693f9SMoses Reuben 
25982ac693f9SMoses Reuben 	if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
25992ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
26002ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
26012ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
26022ac693f9SMoses Reuben 			     flow_tag, flow_attr->type);
26032ac693f9SMoses Reuben 		err = -EINVAL;
26042ac693f9SMoses Reuben 		goto free;
26052ac693f9SMoses Reuben 	}
26062ac693f9SMoses Reuben 	flow_act.flow_tag = flow_tag;
260774491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
260866958ed9SHadar Hen Zion 					    &flow_act,
2609a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
2610038d2ef8SMaor Gottlieb 
2611038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
2612038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
2613038d2ef8SMaor Gottlieb 		goto free;
2614038d2ef8SMaor Gottlieb 	}
2615038d2ef8SMaor Gottlieb 
2616d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
26175497adc6SMaor Gottlieb 	handler->prio = ft_prio;
2618038d2ef8SMaor Gottlieb 
2619038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
2620038d2ef8SMaor Gottlieb free:
2621038d2ef8SMaor Gottlieb 	if (err)
2622038d2ef8SMaor Gottlieb 		kfree(handler);
2623c5bb1730SMaor Gottlieb 	kvfree(spec);
2624038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
2625038d2ef8SMaor Gottlieb }
2626038d2ef8SMaor Gottlieb 
2627a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2628a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
2629a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
2630a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
2631a550ddfcSYishai Hadas {
2632a550ddfcSYishai Hadas 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2633a550ddfcSYishai Hadas }
2634a550ddfcSYishai Hadas 
263535d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
263635d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
263735d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
263835d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
263935d19011SMaor Gottlieb {
264035d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
264135d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
264235d19011SMaor Gottlieb 
264335d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
264435d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
264535d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
264635d19011SMaor Gottlieb 					       flow_attr, dst);
264735d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
264874491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2649d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
265035d19011SMaor Gottlieb 			kfree(handler);
265135d19011SMaor Gottlieb 			handler = handler_dst;
265235d19011SMaor Gottlieb 		} else {
265335d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
265435d19011SMaor Gottlieb 		}
265535d19011SMaor Gottlieb 	}
265635d19011SMaor Gottlieb 
265735d19011SMaor Gottlieb 	return handler;
265835d19011SMaor Gottlieb }
2659038d2ef8SMaor Gottlieb enum {
2660038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
2661038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
2662038d2ef8SMaor Gottlieb };
2663038d2ef8SMaor Gottlieb 
2664038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2665038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
2666038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
2667038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
2668038d2ef8SMaor Gottlieb {
2669038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
2670038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2671038d2ef8SMaor Gottlieb 
2672038d2ef8SMaor Gottlieb 	static struct {
2673038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
2674038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
2675038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
2676038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
2677038d2ef8SMaor Gottlieb 			.flow_attr = {
2678038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2679038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2680038d2ef8SMaor Gottlieb 			},
2681038d2ef8SMaor Gottlieb 			.eth_flow = {
2682038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2683038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2684038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2685038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
2686038d2ef8SMaor Gottlieb 			}
2687038d2ef8SMaor Gottlieb 		},
2688038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
2689038d2ef8SMaor Gottlieb 			.flow_attr = {
2690038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2691038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2692038d2ef8SMaor Gottlieb 			},
2693038d2ef8SMaor Gottlieb 			.eth_flow = {
2694038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2695038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2696038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2697038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
2698038d2ef8SMaor Gottlieb 			}
2699038d2ef8SMaor Gottlieb 		}
2700038d2ef8SMaor Gottlieb 	};
2701038d2ef8SMaor Gottlieb 
2702038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
2703038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
2704038d2ef8SMaor Gottlieb 				   dst);
2705038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
2706038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2707038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
2708038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2709038d2ef8SMaor Gottlieb 						 dst);
2710038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
271174491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2712d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
2713038d2ef8SMaor Gottlieb 			kfree(handler);
2714038d2ef8SMaor Gottlieb 			handler = handler_ucast;
2715038d2ef8SMaor Gottlieb 		} else {
2716038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
2717038d2ef8SMaor Gottlieb 		}
2718038d2ef8SMaor Gottlieb 	}
2719038d2ef8SMaor Gottlieb 
2720038d2ef8SMaor Gottlieb 	return handler;
2721038d2ef8SMaor Gottlieb }
2722038d2ef8SMaor Gottlieb 
2723cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2724cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
2725cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
2726cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
2727cc0e5d42SMaor Gottlieb {
2728cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
2729cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
2730cc0e5d42SMaor Gottlieb 	int err;
2731cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
2732cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
2733cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
2734cc0e5d42SMaor Gottlieb 	};
2735cc0e5d42SMaor Gottlieb 
2736cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2737cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
2738cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
2739cc0e5d42SMaor Gottlieb 		goto err;
2740cc0e5d42SMaor Gottlieb 	}
2741cc0e5d42SMaor Gottlieb 
2742cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2743cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
2744cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
2745cc0e5d42SMaor Gottlieb 		goto err_tx;
2746cc0e5d42SMaor Gottlieb 	}
2747cc0e5d42SMaor Gottlieb 
2748cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
2749cc0e5d42SMaor Gottlieb 
2750cc0e5d42SMaor Gottlieb 	return handler_rx;
2751cc0e5d42SMaor Gottlieb 
2752cc0e5d42SMaor Gottlieb err_tx:
275374491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
2754cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
2755cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
2756cc0e5d42SMaor Gottlieb err:
2757cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
2758cc0e5d42SMaor Gottlieb }
2759cc0e5d42SMaor Gottlieb 
2760038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2761038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
2762038d2ef8SMaor Gottlieb 					   int domain)
2763038d2ef8SMaor Gottlieb {
2764038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
2765d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
2766038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2767038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
2768cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
2769038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
2770038d2ef8SMaor Gottlieb 	int err;
2771a550ddfcSYishai Hadas 	int underlay_qpn;
2772038d2ef8SMaor Gottlieb 
2773038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
2774dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2775038d2ef8SMaor Gottlieb 
2776038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
2777038d2ef8SMaor Gottlieb 	    flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
277835d19011SMaor Gottlieb 	    (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
2779038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2780038d2ef8SMaor Gottlieb 
2781038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2782038d2ef8SMaor Gottlieb 	if (!dst)
2783038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2784038d2ef8SMaor Gottlieb 
2785038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2786038d2ef8SMaor Gottlieb 
2787cc0e5d42SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
2788038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
2789038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
2790038d2ef8SMaor Gottlieb 		goto unlock;
2791038d2ef8SMaor Gottlieb 	}
2792cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2793cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2794cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
2795cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
2796cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
2797cc0e5d42SMaor Gottlieb 			goto destroy_ft;
2798cc0e5d42SMaor Gottlieb 		}
2799cc0e5d42SMaor Gottlieb 	}
2800038d2ef8SMaor Gottlieb 
2801038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
2802d9f88e5aSYishai Hadas 	if (mqp->flags & MLX5_IB_QP_RSS)
2803d9f88e5aSYishai Hadas 		dst->tir_num = mqp->rss_qp.tirn;
2804d9f88e5aSYishai Hadas 	else
2805d9f88e5aSYishai Hadas 		dst->tir_num = mqp->raw_packet_qp.rq.tirn;
2806038d2ef8SMaor Gottlieb 
2807038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
280835d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
280935d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
281035d19011SMaor Gottlieb 							flow_attr, dst);
281135d19011SMaor Gottlieb 		} else {
2812a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
2813a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
2814a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
2815a550ddfcSYishai Hadas 						    dst, underlay_qpn);
281635d19011SMaor Gottlieb 		}
2817038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2818038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2819038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2820038d2ef8SMaor Gottlieb 						dst);
2821cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2822cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
2823038d2ef8SMaor Gottlieb 	} else {
2824038d2ef8SMaor Gottlieb 		err = -EINVAL;
2825038d2ef8SMaor Gottlieb 		goto destroy_ft;
2826038d2ef8SMaor Gottlieb 	}
2827038d2ef8SMaor Gottlieb 
2828038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
2829038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
2830038d2ef8SMaor Gottlieb 		handler = NULL;
2831038d2ef8SMaor Gottlieb 		goto destroy_ft;
2832038d2ef8SMaor Gottlieb 	}
2833038d2ef8SMaor Gottlieb 
2834038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2835038d2ef8SMaor Gottlieb 	kfree(dst);
2836038d2ef8SMaor Gottlieb 
2837038d2ef8SMaor Gottlieb 	return &handler->ibflow;
2838038d2ef8SMaor Gottlieb 
2839038d2ef8SMaor Gottlieb destroy_ft:
2840038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
2841cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
2842cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
2843038d2ef8SMaor Gottlieb unlock:
2844038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2845038d2ef8SMaor Gottlieb 	kfree(dst);
2846038d2ef8SMaor Gottlieb 	kfree(handler);
2847038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
2848038d2ef8SMaor Gottlieb }
2849038d2ef8SMaor Gottlieb 
2850e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2851e126ba97SEli Cohen {
2852e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
285381e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
2854e126ba97SEli Cohen 	int err;
2855e126ba97SEli Cohen 
285681e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
285781e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
285881e30880SYishai Hadas 		return -EOPNOTSUPP;
285981e30880SYishai Hadas 	}
286081e30880SYishai Hadas 
28619603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
2862e126ba97SEli Cohen 	if (err)
2863e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2864e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2865e126ba97SEli Cohen 
2866e126ba97SEli Cohen 	return err;
2867e126ba97SEli Cohen }
2868e126ba97SEli Cohen 
2869e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2870e126ba97SEli Cohen {
2871e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2872e126ba97SEli Cohen 	int err;
2873e126ba97SEli Cohen 
28749603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
2875e126ba97SEli Cohen 	if (err)
2876e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2877e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2878e126ba97SEli Cohen 
2879e126ba97SEli Cohen 	return err;
2880e126ba97SEli Cohen }
2881e126ba97SEli Cohen 
2882e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2883e126ba97SEli Cohen {
28841b5daf11SMajd Dibbiny 	int err;
2885e126ba97SEli Cohen 
28861b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2887e126ba97SEli Cohen 	if (err)
2888e126ba97SEli Cohen 		return err;
28891b5daf11SMajd Dibbiny 
28901b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
28911b5daf11SMajd Dibbiny 
28921b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2893e126ba97SEli Cohen }
2894e126ba97SEli Cohen 
2895e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2896e126ba97SEli Cohen 			     char *buf)
2897e126ba97SEli Cohen {
2898e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2899e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2900e126ba97SEli Cohen 
29019603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
2902e126ba97SEli Cohen }
2903e126ba97SEli Cohen 
2904e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
2905e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2906e126ba97SEli Cohen {
2907e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2908e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2909e126ba97SEli Cohen 
29106aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2911e126ba97SEli Cohen }
2912e126ba97SEli Cohen 
2913e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2914e126ba97SEli Cohen 			char *buf)
2915e126ba97SEli Cohen {
2916e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2917e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
29189603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
2919e126ba97SEli Cohen }
2920e126ba97SEli Cohen 
2921e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2922e126ba97SEli Cohen 			char *buf)
2923e126ba97SEli Cohen {
2924e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2925e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
29269603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
2927e126ba97SEli Cohen }
2928e126ba97SEli Cohen 
2929e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
2930e126ba97SEli Cohen 			  char *buf)
2931e126ba97SEli Cohen {
2932e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2933e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2934e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
29359603b61dSJack Morgenstein 		       dev->mdev->board_id);
2936e126ba97SEli Cohen }
2937e126ba97SEli Cohen 
2938e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
2939e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
2940e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
2941e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2942e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2943e126ba97SEli Cohen 
2944e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
2945e126ba97SEli Cohen 	&dev_attr_hw_rev,
2946e126ba97SEli Cohen 	&dev_attr_hca_type,
2947e126ba97SEli Cohen 	&dev_attr_board_id,
2948e126ba97SEli Cohen 	&dev_attr_fw_pages,
2949e126ba97SEli Cohen 	&dev_attr_reg_pages,
2950e126ba97SEli Cohen };
2951e126ba97SEli Cohen 
29527722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
29537722f47eSHaggai Eran {
29547722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
29557722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
29567722f47eSHaggai Eran 			     pkey_change_work);
29577722f47eSHaggai Eran 
29587722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
29597722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
29607722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
29617722f47eSHaggai Eran }
29627722f47eSHaggai Eran 
296389ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
296489ea94a7SMaor Gottlieb {
296589ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
296689ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
296789ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
296889ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
296989ea94a7SMaor Gottlieb 	unsigned long flags_qp;
297089ea94a7SMaor Gottlieb 	unsigned long flags_cq;
297189ea94a7SMaor Gottlieb 	unsigned long flags;
297289ea94a7SMaor Gottlieb 
297389ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
297489ea94a7SMaor Gottlieb 
297589ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
297689ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
297789ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
297889ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
297989ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
298089ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
298189ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
298289ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
298389ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
298489ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
298589ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
298689ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
298789ea94a7SMaor Gottlieb 						      &cq_armed_list);
298889ea94a7SMaor Gottlieb 				}
298989ea94a7SMaor Gottlieb 			}
299089ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
299189ea94a7SMaor Gottlieb 		}
299289ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
299389ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
299489ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
299589ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
299689ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
299789ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
299889ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
299989ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
300089ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
300189ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
300289ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
300389ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
300489ea94a7SMaor Gottlieb 							      &cq_armed_list);
300589ea94a7SMaor Gottlieb 					}
300689ea94a7SMaor Gottlieb 				}
300789ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
300889ea94a7SMaor Gottlieb 						       flags_cq);
300989ea94a7SMaor Gottlieb 			}
301089ea94a7SMaor Gottlieb 		}
301189ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
301289ea94a7SMaor Gottlieb 	}
301389ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
301489ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
301589ea94a7SMaor Gottlieb 	 */
301689ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
301789ea94a7SMaor Gottlieb 		mcq->comp(mcq);
301889ea94a7SMaor Gottlieb 	}
301989ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
302089ea94a7SMaor Gottlieb }
302189ea94a7SMaor Gottlieb 
302203404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
302303404e8aSMaor Gottlieb {
302403404e8aSMaor Gottlieb 	int err;
302503404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
302603404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
302703404e8aSMaor Gottlieb 			     delay_drop_work);
302803404e8aSMaor Gottlieb 
3029fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
3030fe248c3aSMaor Gottlieb 
303103404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
303203404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
303303404e8aSMaor Gottlieb 				       delay_drop->timeout);
303403404e8aSMaor Gottlieb 	if (err) {
303503404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
303603404e8aSMaor Gottlieb 			     delay_drop->timeout);
303703404e8aSMaor Gottlieb 		delay_drop->activate = false;
303803404e8aSMaor Gottlieb 	}
303903404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
304003404e8aSMaor Gottlieb }
304103404e8aSMaor Gottlieb 
30429603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
30434d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
3044e126ba97SEli Cohen {
30459603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
3046e126ba97SEli Cohen 	struct ib_event ibev;
3047dbaaff2aSEli Cohen 	bool fatal = false;
3048e126ba97SEli Cohen 	u8 port = 0;
3049e126ba97SEli Cohen 
3050e126ba97SEli Cohen 	switch (event) {
3051e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
3052e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
305389ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
3054dbaaff2aSEli Cohen 		fatal = true;
3055e126ba97SEli Cohen 		break;
3056e126ba97SEli Cohen 
3057e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
3058e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
30592788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
30604d2f9bbbSJack Morgenstein 		port = (u8)param;
30615ec8c83eSAviv Heller 
30625ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
30635ec8c83eSAviv Heller 		 * mlx5_netdev_event().
30645ec8c83eSAviv Heller 		 */
30655ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
30665ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
30675ec8c83eSAviv Heller 			return;
30685ec8c83eSAviv Heller 
30695ec8c83eSAviv Heller 		ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
30705ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
3071e126ba97SEli Cohen 		break;
3072e126ba97SEli Cohen 
3073e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
3074e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
30754d2f9bbbSJack Morgenstein 		port = (u8)param;
3076e126ba97SEli Cohen 		break;
3077e126ba97SEli Cohen 
3078e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
3079e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
30804d2f9bbbSJack Morgenstein 		port = (u8)param;
30817722f47eSHaggai Eran 
30827722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
3083e126ba97SEli Cohen 		break;
3084e126ba97SEli Cohen 
3085e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
3086e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
30874d2f9bbbSJack Morgenstein 		port = (u8)param;
3088e126ba97SEli Cohen 		break;
3089e126ba97SEli Cohen 
3090e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
3091e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
30924d2f9bbbSJack Morgenstein 		port = (u8)param;
3093e126ba97SEli Cohen 		break;
309403404e8aSMaor Gottlieb 	case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
309503404e8aSMaor Gottlieb 		schedule_work(&ibdev->delay_drop.delay_drop_work);
309603404e8aSMaor Gottlieb 		goto out;
3097bdc37924SSaeed Mahameed 	default:
309803404e8aSMaor Gottlieb 		goto out;
3099e126ba97SEli Cohen 	}
3100e126ba97SEli Cohen 
3101e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
3102e126ba97SEli Cohen 	ibev.element.port_num = port;
3103e126ba97SEli Cohen 
3104a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
3105a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
310603404e8aSMaor Gottlieb 		goto out;
3107a0c84c32SEli Cohen 	}
3108a0c84c32SEli Cohen 
3109e126ba97SEli Cohen 	if (ibdev->ib_active)
3110e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
3111dbaaff2aSEli Cohen 
3112dbaaff2aSEli Cohen 	if (fatal)
3113dbaaff2aSEli Cohen 		ibdev->ib_active = false;
311403404e8aSMaor Gottlieb 
311503404e8aSMaor Gottlieb out:
311603404e8aSMaor Gottlieb 	return;
3117e126ba97SEli Cohen }
3118e126ba97SEli Cohen 
3119c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
3120c43f1112SMaor Gottlieb {
3121c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
3122c43f1112SMaor Gottlieb 	int err;
3123c43f1112SMaor Gottlieb 	int port;
3124c43f1112SMaor Gottlieb 
3125c43f1112SMaor Gottlieb 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
3126c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
3127c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3128c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
3129c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3130c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
3131c43f1112SMaor Gottlieb 								   port, 0,
3132c43f1112SMaor Gottlieb 								   &vport_ctx);
3133c43f1112SMaor Gottlieb 				if (err) {
3134c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3135c43f1112SMaor Gottlieb 						    port, err);
3136c43f1112SMaor Gottlieb 					return err;
3137c43f1112SMaor Gottlieb 				}
3138c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
3139c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
3140c43f1112SMaor Gottlieb 			} else {
3141c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
3142c43f1112SMaor Gottlieb 			}
3143c43f1112SMaor Gottlieb 		}
3144c43f1112SMaor Gottlieb 	}
3145c43f1112SMaor Gottlieb 	return 0;
3146c43f1112SMaor Gottlieb }
3147c43f1112SMaor Gottlieb 
3148e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3149e126ba97SEli Cohen {
3150e126ba97SEli Cohen 	int port;
3151e126ba97SEli Cohen 
3152938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
3153e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
3154e126ba97SEli Cohen }
3155e126ba97SEli Cohen 
3156e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
3157e126ba97SEli Cohen {
3158e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
3159e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
3160f614fc15SDan Carpenter 	int err = -ENOMEM;
3161e126ba97SEli Cohen 	int port;
31622528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
3163e126ba97SEli Cohen 
3164e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3165e126ba97SEli Cohen 	if (!pprops)
3166e126ba97SEli Cohen 		goto out;
3167e126ba97SEli Cohen 
3168e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3169e126ba97SEli Cohen 	if (!dprops)
3170e126ba97SEli Cohen 		goto out;
3171e126ba97SEli Cohen 
3172c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
3173c43f1112SMaor Gottlieb 	if (err)
3174c43f1112SMaor Gottlieb 		goto out;
3175c43f1112SMaor Gottlieb 
31762528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
3177e126ba97SEli Cohen 	if (err) {
3178e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
3179e126ba97SEli Cohen 		goto out;
3180e126ba97SEli Cohen 	}
3181e126ba97SEli Cohen 
3182938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
3183c4550c63SOr Gerlitz 		memset(pprops, 0, sizeof(*pprops));
3184e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3185e126ba97SEli Cohen 		if (err) {
3186938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
3187938fe83cSSaeed Mahameed 				     port, err);
3188e126ba97SEli Cohen 			break;
3189e126ba97SEli Cohen 		}
3190938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
3191938fe83cSSaeed Mahameed 						dprops->max_pkeys;
3192938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
3193938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
3194e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
3195e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
3196e126ba97SEli Cohen 	}
3197e126ba97SEli Cohen 
3198e126ba97SEli Cohen out:
3199e126ba97SEli Cohen 	kfree(pprops);
3200e126ba97SEli Cohen 	kfree(dprops);
3201e126ba97SEli Cohen 
3202e126ba97SEli Cohen 	return err;
3203e126ba97SEli Cohen }
3204e126ba97SEli Cohen 
3205e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3206e126ba97SEli Cohen {
3207e126ba97SEli Cohen 	int err;
3208e126ba97SEli Cohen 
3209e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
3210e126ba97SEli Cohen 	if (err)
3211e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3212e126ba97SEli Cohen 
3213e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
3214add08d76SChristoph Hellwig 	ib_free_cq(dev->umrc.cq);
3215e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
3216e126ba97SEli Cohen }
3217e126ba97SEli Cohen 
3218e126ba97SEli Cohen enum {
3219e126ba97SEli Cohen 	MAX_UMR_WR = 128,
3220e126ba97SEli Cohen };
3221e126ba97SEli Cohen 
3222e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
3223e126ba97SEli Cohen {
3224e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
3225e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
3226e126ba97SEli Cohen 	struct ib_pd *pd;
3227e126ba97SEli Cohen 	struct ib_cq *cq;
3228e126ba97SEli Cohen 	struct ib_qp *qp;
3229e126ba97SEli Cohen 	int ret;
3230e126ba97SEli Cohen 
3231e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3232e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3233e126ba97SEli Cohen 	if (!attr || !init_attr) {
3234e126ba97SEli Cohen 		ret = -ENOMEM;
3235e126ba97SEli Cohen 		goto error_0;
3236e126ba97SEli Cohen 	}
3237e126ba97SEli Cohen 
3238ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
3239e126ba97SEli Cohen 	if (IS_ERR(pd)) {
3240e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3241e126ba97SEli Cohen 		ret = PTR_ERR(pd);
3242e126ba97SEli Cohen 		goto error_0;
3243e126ba97SEli Cohen 	}
3244e126ba97SEli Cohen 
3245add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
3246e126ba97SEli Cohen 	if (IS_ERR(cq)) {
3247e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3248e126ba97SEli Cohen 		ret = PTR_ERR(cq);
3249e126ba97SEli Cohen 		goto error_2;
3250e126ba97SEli Cohen 	}
3251e126ba97SEli Cohen 
3252e126ba97SEli Cohen 	init_attr->send_cq = cq;
3253e126ba97SEli Cohen 	init_attr->recv_cq = cq;
3254e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3255e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
3256e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
3257e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3258e126ba97SEli Cohen 	init_attr->port_num = 1;
3259e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3260e126ba97SEli Cohen 	if (IS_ERR(qp)) {
3261e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3262e126ba97SEli Cohen 		ret = PTR_ERR(qp);
3263e126ba97SEli Cohen 		goto error_3;
3264e126ba97SEli Cohen 	}
3265e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
3266e126ba97SEli Cohen 	qp->real_qp    = qp;
3267e126ba97SEli Cohen 	qp->uobject    = NULL;
3268e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
326931fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
327031fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
3271e126ba97SEli Cohen 
3272e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
3273e126ba97SEli Cohen 	attr->port_num = 1;
3274e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3275e126ba97SEli Cohen 				IB_QP_PORT, NULL);
3276e126ba97SEli Cohen 	if (ret) {
3277e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3278e126ba97SEli Cohen 		goto error_4;
3279e126ba97SEli Cohen 	}
3280e126ba97SEli Cohen 
3281e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3282e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
3283e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
3284e126ba97SEli Cohen 
3285e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3286e126ba97SEli Cohen 	if (ret) {
3287e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3288e126ba97SEli Cohen 		goto error_4;
3289e126ba97SEli Cohen 	}
3290e126ba97SEli Cohen 
3291e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3292e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
3293e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3294e126ba97SEli Cohen 	if (ret) {
3295e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3296e126ba97SEli Cohen 		goto error_4;
3297e126ba97SEli Cohen 	}
3298e126ba97SEli Cohen 
3299e126ba97SEli Cohen 	dev->umrc.qp = qp;
3300e126ba97SEli Cohen 	dev->umrc.cq = cq;
3301e126ba97SEli Cohen 	dev->umrc.pd = pd;
3302e126ba97SEli Cohen 
3303e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
3304e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
3305e126ba97SEli Cohen 	if (ret) {
3306e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3307e126ba97SEli Cohen 		goto error_4;
3308e126ba97SEli Cohen 	}
3309e126ba97SEli Cohen 
3310e126ba97SEli Cohen 	kfree(attr);
3311e126ba97SEli Cohen 	kfree(init_attr);
3312e126ba97SEli Cohen 
3313e126ba97SEli Cohen 	return 0;
3314e126ba97SEli Cohen 
3315e126ba97SEli Cohen error_4:
3316e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
3317e126ba97SEli Cohen 
3318e126ba97SEli Cohen error_3:
3319add08d76SChristoph Hellwig 	ib_free_cq(cq);
3320e126ba97SEli Cohen 
3321e126ba97SEli Cohen error_2:
3322e126ba97SEli Cohen 	ib_dealloc_pd(pd);
3323e126ba97SEli Cohen 
3324e126ba97SEli Cohen error_0:
3325e126ba97SEli Cohen 	kfree(attr);
3326e126ba97SEli Cohen 	kfree(init_attr);
3327e126ba97SEli Cohen 	return ret;
3328e126ba97SEli Cohen }
3329e126ba97SEli Cohen 
33306e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
33316e8484c5SMax Gurtovoy {
33326e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
33336e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
33346e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
33356e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
33366e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
33376e8484c5SMax Gurtovoy 	default:
33386e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
33396e8484c5SMax Gurtovoy 	}
33406e8484c5SMax Gurtovoy }
33416e8484c5SMax Gurtovoy 
3342e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
3343e126ba97SEli Cohen {
3344e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
3345e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3346bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
33477722f47eSHaggai Eran 	int port;
3348e126ba97SEli Cohen 	int ret = 0;
3349e126ba97SEli Cohen 
3350e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
3351e126ba97SEli Cohen 
3352d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
3353d16e91daSHaggai Eran 
3354e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3355e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
3356e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
3357e126ba97SEli Cohen 		goto error0;
3358e126ba97SEli Cohen 	}
3359e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
3360e126ba97SEli Cohen 	devr->p0->uobject = NULL;
3361e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
3362e126ba97SEli Cohen 
3363bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
3364e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
3365e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
3366e126ba97SEli Cohen 		goto error1;
3367e126ba97SEli Cohen 	}
3368e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
3369e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
3370e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
3371e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
3372e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
3373e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
3374e126ba97SEli Cohen 
3375e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3376e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
3377e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
3378e126ba97SEli Cohen 		goto error2;
3379e126ba97SEli Cohen 	}
3380e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
3381e126ba97SEli Cohen 	devr->x0->inode = NULL;
3382e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
3383e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
3384e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3385e126ba97SEli Cohen 
3386e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3387e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
3388e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
3389e126ba97SEli Cohen 		goto error3;
3390e126ba97SEli Cohen 	}
3391e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
3392e126ba97SEli Cohen 	devr->x1->inode = NULL;
3393e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
3394e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
3395e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3396e126ba97SEli Cohen 
3397e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
3398e126ba97SEli Cohen 	attr.attr.max_sge = 1;
3399e126ba97SEli Cohen 	attr.attr.max_wr = 1;
3400e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
34011a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
3402e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
3403e126ba97SEli Cohen 
3404e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3405e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
3406e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
3407e126ba97SEli Cohen 		goto error4;
3408e126ba97SEli Cohen 	}
3409e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
3410e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
3411e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
3412e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
3413e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
3414e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
3415e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
34161a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
3417e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
34181a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
3419e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
3420e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
3421e126ba97SEli Cohen 
34224aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
34234aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
34244aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
34254aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
34264aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
34274aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
34284aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
34294aa17b28SHaggai Abramonvsky 		goto error5;
34304aa17b28SHaggai Abramonvsky 	}
34314aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
34324aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
34334aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
34344aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
34354aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
34364aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
34371a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
34384aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
34391a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
34404aa17b28SHaggai Abramonvsky 
34417722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
34427722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
34437722f47eSHaggai Eran 			  pkey_change_handler);
34447722f47eSHaggai Eran 		devr->ports[port].devr = devr;
34457722f47eSHaggai Eran 	}
34467722f47eSHaggai Eran 
3447e126ba97SEli Cohen 	return 0;
3448e126ba97SEli Cohen 
34494aa17b28SHaggai Abramonvsky error5:
34504aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
3451e126ba97SEli Cohen error4:
3452e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3453e126ba97SEli Cohen error3:
3454e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3455e126ba97SEli Cohen error2:
3456e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3457e126ba97SEli Cohen error1:
3458e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
3459e126ba97SEli Cohen error0:
3460e126ba97SEli Cohen 	return ret;
3461e126ba97SEli Cohen }
3462e126ba97SEli Cohen 
3463e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3464e126ba97SEli Cohen {
34657722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
34667722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
34677722f47eSHaggai Eran 	int port;
34687722f47eSHaggai Eran 
34694aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
3470e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
3471e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3472e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3473e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3474e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
34757722f47eSHaggai Eran 
34767722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
34777722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
34787722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
3479e126ba97SEli Cohen }
3480e126ba97SEli Cohen 
3481e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
3482e53505a8SAchiad Shochat {
3483e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3484e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3485e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3486e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3487e53505a8SAchiad Shochat 	u32 ret = 0;
3488e53505a8SAchiad Shochat 
3489e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
3490e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
3491e53505a8SAchiad Shochat 
349272cd5717SOr Gerlitz 	ret = RDMA_CORE_PORT_RAW_PACKET;
349372cd5717SOr Gerlitz 
3494e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
349572cd5717SOr Gerlitz 		return ret;
3496e53505a8SAchiad Shochat 
3497e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
349872cd5717SOr Gerlitz 		return ret;
3499e53505a8SAchiad Shochat 
3500e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3501e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
3502e53505a8SAchiad Shochat 
3503e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3504e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3505e53505a8SAchiad Shochat 
3506e53505a8SAchiad Shochat 	return ret;
3507e53505a8SAchiad Shochat }
3508e53505a8SAchiad Shochat 
35097738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
35107738613eSIra Weiny 			       struct ib_port_immutable *immutable)
35117738613eSIra Weiny {
35127738613eSIra Weiny 	struct ib_port_attr attr;
3513ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3514ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
35157738613eSIra Weiny 	int err;
35167738613eSIra Weiny 
3517c4550c63SOr Gerlitz 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3518c4550c63SOr Gerlitz 
3519c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
35207738613eSIra Weiny 	if (err)
35217738613eSIra Weiny 		return err;
35227738613eSIra Weiny 
35237738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
35247738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
3525e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3526ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3527337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
35287738613eSIra Weiny 
35297738613eSIra Weiny 	return 0;
35307738613eSIra Weiny }
35317738613eSIra Weiny 
35329abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
3533c7342823SIra Weiny {
3534c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
3535c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
35369abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
35379abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
35389abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
3539c7342823SIra Weiny }
3540c7342823SIra Weiny 
354145f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
35429ef9c640SAviv Heller {
35439ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
35449ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
35459ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
35469ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
35479ef9c640SAviv Heller 	int err;
35489ef9c640SAviv Heller 
35499ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
35509ef9c640SAviv Heller 		return 0;
35519ef9c640SAviv Heller 
35529ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
35539ef9c640SAviv Heller 	if (err)
35549ef9c640SAviv Heller 		return err;
35559ef9c640SAviv Heller 
35569ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
35579ef9c640SAviv Heller 	if (IS_ERR(ft)) {
35589ef9c640SAviv Heller 		err = PTR_ERR(ft);
35599ef9c640SAviv Heller 		goto err_destroy_vport_lag;
35609ef9c640SAviv Heller 	}
35619ef9c640SAviv Heller 
35629ef9c640SAviv Heller 	dev->flow_db.lag_demux_ft = ft;
35639ef9c640SAviv Heller 	return 0;
35649ef9c640SAviv Heller 
35659ef9c640SAviv Heller err_destroy_vport_lag:
35669ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
35679ef9c640SAviv Heller 	return err;
35689ef9c640SAviv Heller }
35699ef9c640SAviv Heller 
357045f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
35719ef9c640SAviv Heller {
35729ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
35739ef9c640SAviv Heller 
35749ef9c640SAviv Heller 	if (dev->flow_db.lag_demux_ft) {
35759ef9c640SAviv Heller 		mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
35769ef9c640SAviv Heller 		dev->flow_db.lag_demux_ft = NULL;
35779ef9c640SAviv Heller 
35789ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
35799ef9c640SAviv Heller 	}
35809ef9c640SAviv Heller }
35819ef9c640SAviv Heller 
3582d012f5d6SOr Gerlitz static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
3583fc24fc5eSAchiad Shochat {
3584e53505a8SAchiad Shochat 	int err;
3585e53505a8SAchiad Shochat 
3586fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
3587e53505a8SAchiad Shochat 	err = register_netdevice_notifier(&dev->roce.nb);
35885ec8c83eSAviv Heller 	if (err) {
35895ec8c83eSAviv Heller 		dev->roce.nb.notifier_call = NULL;
3590e53505a8SAchiad Shochat 		return err;
35915ec8c83eSAviv Heller 	}
3592e53505a8SAchiad Shochat 
3593d012f5d6SOr Gerlitz 	return 0;
3594d012f5d6SOr Gerlitz }
3595d012f5d6SOr Gerlitz 
3596d012f5d6SOr Gerlitz static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
3597e126ba97SEli Cohen {
3598e126ba97SEli Cohen 	if (dev->roce.nb.notifier_call) {
3599e126ba97SEli Cohen 		unregister_netdevice_notifier(&dev->roce.nb);
3600e126ba97SEli Cohen 		dev->roce.nb.notifier_call = NULL;
3601e126ba97SEli Cohen 	}
3602e126ba97SEli Cohen }
3603e126ba97SEli Cohen 
360445f95acdSOr Gerlitz static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3605e126ba97SEli Cohen {
3606e126ba97SEli Cohen 	int err;
3607e126ba97SEli Cohen 
3608d012f5d6SOr Gerlitz 	err = mlx5_add_netdev_notifier(dev);
3609d012f5d6SOr Gerlitz 	if (err)
3610e53505a8SAchiad Shochat 		return err;
3611e53505a8SAchiad Shochat 
3612ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
3613e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3614e53505a8SAchiad Shochat 		if (err)
3615e53505a8SAchiad Shochat 			goto err_unregister_netdevice_notifier;
3616ca5b91d6SOr Gerlitz 	}
3617e53505a8SAchiad Shochat 
361845f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
36199ef9c640SAviv Heller 	if (err)
36209ef9c640SAviv Heller 		goto err_disable_roce;
36219ef9c640SAviv Heller 
3622e53505a8SAchiad Shochat 	return 0;
3623e53505a8SAchiad Shochat 
36249ef9c640SAviv Heller err_disable_roce:
3625ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
36269ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
36279ef9c640SAviv Heller 
3628e53505a8SAchiad Shochat err_unregister_netdevice_notifier:
3629d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
3630e53505a8SAchiad Shochat 	return err;
3631fc24fc5eSAchiad Shochat }
3632fc24fc5eSAchiad Shochat 
363345f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3634fc24fc5eSAchiad Shochat {
363545f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3636ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
3637e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
3638fc24fc5eSAchiad Shochat }
3639fc24fc5eSAchiad Shochat 
3640e1f24a79SParav Pandit struct mlx5_ib_counter {
36417c16f477SKamal Heib 	const char *name;
36427c16f477SKamal Heib 	size_t offset;
36437c16f477SKamal Heib };
36447c16f477SKamal Heib 
36457c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
36467c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
36477c16f477SKamal Heib 
3648e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
36497c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
36507c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
36517c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
36527c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
36537c16f477SKamal Heib };
36547c16f477SKamal Heib 
3655e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
36567c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
36577c16f477SKamal Heib };
36587c16f477SKamal Heib 
3659e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
36607c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
36617c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
36627c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
36637c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
36647c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
36657c16f477SKamal Heib };
36667c16f477SKamal Heib 
3667e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
3668e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
3669e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3670e1f24a79SParav Pandit 
3671e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
3672e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
3673e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
3674e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3675e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
3676e1f24a79SParav Pandit };
3677e1f24a79SParav Pandit 
367858dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
367958dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
368058dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
368158dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
368258dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
368358dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
368458dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
368558dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
368658dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
368758dcb60aSParav Pandit };
368858dcb60aSParav Pandit 
3689e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
36900837e86aSMark Bloch {
36910837e86aSMark Bloch 	unsigned int i;
36920837e86aSMark Bloch 
36937c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
36940837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
3695e1f24a79SParav Pandit 					    dev->port[i].cnts.set_id);
3696e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
3697e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
36987c16f477SKamal Heib 	}
36997c16f477SKamal Heib }
37007c16f477SKamal Heib 
3701e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3702e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
37037c16f477SKamal Heib {
37047c16f477SKamal Heib 	u32 num_counters;
37057c16f477SKamal Heib 
37067c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
37077c16f477SKamal Heib 
37087c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
37097c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
37107c16f477SKamal Heib 
37117c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
37127c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
371358dcb60aSParav Pandit 
371458dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
371558dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
371658dcb60aSParav Pandit 
3717e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
37187c16f477SKamal Heib 
3719e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3720e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3721e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
3722e1f24a79SParav Pandit 	}
3723e1f24a79SParav Pandit 
3724e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3725e1f24a79SParav Pandit 	if (!cnts->names)
37267c16f477SKamal Heib 		return -ENOMEM;
37277c16f477SKamal Heib 
3728e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
3729e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
3730e1f24a79SParav Pandit 	if (!cnts->offsets)
37317c16f477SKamal Heib 		goto err_names;
37327c16f477SKamal Heib 
37337c16f477SKamal Heib 	return 0;
37347c16f477SKamal Heib 
37357c16f477SKamal Heib err_names:
3736e1f24a79SParav Pandit 	kfree(cnts->names);
37377c16f477SKamal Heib 	return -ENOMEM;
37387c16f477SKamal Heib }
37397c16f477SKamal Heib 
3740e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
37417c16f477SKamal Heib 				  const char **names,
37427c16f477SKamal Heib 				  size_t *offsets)
37437c16f477SKamal Heib {
37447c16f477SKamal Heib 	int i;
37457c16f477SKamal Heib 	int j = 0;
37467c16f477SKamal Heib 
37477c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
37487c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
37497c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
37507c16f477SKamal Heib 	}
37517c16f477SKamal Heib 
37527c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
37537c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
37547c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
37557c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
37567c16f477SKamal Heib 		}
37577c16f477SKamal Heib 	}
37587c16f477SKamal Heib 
37597c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
37607c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
37617c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
37627c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
37637c16f477SKamal Heib 		}
37647c16f477SKamal Heib 	}
3765e1f24a79SParav Pandit 
376658dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
376758dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
376858dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
376958dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
377058dcb60aSParav Pandit 		}
377158dcb60aSParav Pandit 	}
377258dcb60aSParav Pandit 
3773e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3774e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3775e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
3776e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
3777e1f24a79SParav Pandit 		}
3778e1f24a79SParav Pandit 	}
37790837e86aSMark Bloch }
37800837e86aSMark Bloch 
3781e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
37820837e86aSMark Bloch {
37830837e86aSMark Bloch 	int i;
37840837e86aSMark Bloch 	int ret;
37850837e86aSMark Bloch 
37860837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
37877c16f477SKamal Heib 		struct mlx5_ib_port *port = &dev->port[i];
37887c16f477SKamal Heib 
37890837e86aSMark Bloch 		ret = mlx5_core_alloc_q_counter(dev->mdev,
3790e1f24a79SParav Pandit 						&port->cnts.set_id);
37910837e86aSMark Bloch 		if (ret) {
37920837e86aSMark Bloch 			mlx5_ib_warn(dev,
37930837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
37940837e86aSMark Bloch 				     i + 1, ret);
37950837e86aSMark Bloch 			goto dealloc_counters;
37960837e86aSMark Bloch 		}
37977c16f477SKamal Heib 
3798e1f24a79SParav Pandit 		ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
37997c16f477SKamal Heib 		if (ret)
38007c16f477SKamal Heib 			goto dealloc_counters;
38017c16f477SKamal Heib 
3802e1f24a79SParav Pandit 		mlx5_ib_fill_counters(dev, port->cnts.names,
3803e1f24a79SParav Pandit 				      port->cnts.offsets);
38040837e86aSMark Bloch 	}
38050837e86aSMark Bloch 
38060837e86aSMark Bloch 	return 0;
38070837e86aSMark Bloch 
38080837e86aSMark Bloch dealloc_counters:
38090837e86aSMark Bloch 	while (--i >= 0)
38100837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
3811e1f24a79SParav Pandit 					    dev->port[i].cnts.set_id);
38120837e86aSMark Bloch 
38130837e86aSMark Bloch 	return ret;
38140837e86aSMark Bloch }
38150837e86aSMark Bloch 
38160ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
38170ad17a8fSMark Bloch 						    u8 port_num)
38180ad17a8fSMark Bloch {
38197c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
38207c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
38210ad17a8fSMark Bloch 
38220ad17a8fSMark Bloch 	/* We support only per port stats */
38230ad17a8fSMark Bloch 	if (port_num == 0)
38240ad17a8fSMark Bloch 		return NULL;
38250ad17a8fSMark Bloch 
3826e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
3827e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
3828e1f24a79SParav Pandit 					  port->cnts.num_cong_counters,
38290ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
38300ad17a8fSMark Bloch }
38310ad17a8fSMark Bloch 
3832e1f24a79SParav Pandit static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3833e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
3834e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
3835e1f24a79SParav Pandit {
3836e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3837e1f24a79SParav Pandit 	void *out;
3838e1f24a79SParav Pandit 	__be32 val;
3839e1f24a79SParav Pandit 	int ret, i;
3840e1f24a79SParav Pandit 
38411b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
3842e1f24a79SParav Pandit 	if (!out)
3843e1f24a79SParav Pandit 		return -ENOMEM;
3844e1f24a79SParav Pandit 
3845e1f24a79SParav Pandit 	ret = mlx5_core_query_q_counter(dev->mdev,
3846e1f24a79SParav Pandit 					port->cnts.set_id, 0,
3847e1f24a79SParav Pandit 					out, outlen);
3848e1f24a79SParav Pandit 	if (ret)
3849e1f24a79SParav Pandit 		goto free;
3850e1f24a79SParav Pandit 
3851e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
3852e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
3853e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
3854e1f24a79SParav Pandit 	}
3855e1f24a79SParav Pandit 
3856e1f24a79SParav Pandit free:
3857e1f24a79SParav Pandit 	kvfree(out);
3858e1f24a79SParav Pandit 	return ret;
3859e1f24a79SParav Pandit }
3860e1f24a79SParav Pandit 
38610ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
38620ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
38637c16f477SKamal Heib 				u8 port_num, int index)
38640ad17a8fSMark Bloch {
38650ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
38667c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
3867e1f24a79SParav Pandit 	int ret, num_counters;
38680ad17a8fSMark Bloch 
38697c16f477SKamal Heib 	if (!stats)
3870e1f24a79SParav Pandit 		return -EINVAL;
38710ad17a8fSMark Bloch 
3872e1f24a79SParav Pandit 	ret = mlx5_ib_query_q_counters(dev, port, stats);
38730ad17a8fSMark Bloch 	if (ret)
3874e1f24a79SParav Pandit 		return ret;
3875e1f24a79SParav Pandit 	num_counters = port->cnts.num_q_counters;
38760ad17a8fSMark Bloch 
3877e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
387871a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
387971a0ff65SMajd Dibbiny 						   stats->value +
388071a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters,
388171a0ff65SMajd Dibbiny 						   port->cnts.num_cong_counters,
388271a0ff65SMajd Dibbiny 						   port->cnts.offsets +
388371a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters);
3884e1f24a79SParav Pandit 		if (ret)
3885e1f24a79SParav Pandit 			return ret;
3886e1f24a79SParav Pandit 		num_counters += port->cnts.num_cong_counters;
38870ad17a8fSMark Bloch 	}
38887c16f477SKamal Heib 
3889e1f24a79SParav Pandit 	return num_counters;
38900ad17a8fSMark Bloch }
38910ad17a8fSMark Bloch 
38928e959601SNiranjana Vishwanathapura static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
38938e959601SNiranjana Vishwanathapura {
38948e959601SNiranjana Vishwanathapura 	return mlx5_rdma_netdev_free(netdev);
38958e959601SNiranjana Vishwanathapura }
38968e959601SNiranjana Vishwanathapura 
3897693dfd5aSErez Shitrit static struct net_device*
3898693dfd5aSErez Shitrit mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3899693dfd5aSErez Shitrit 			  u8 port_num,
3900693dfd5aSErez Shitrit 			  enum rdma_netdev_t type,
3901693dfd5aSErez Shitrit 			  const char *name,
3902693dfd5aSErez Shitrit 			  unsigned char name_assign_type,
3903693dfd5aSErez Shitrit 			  void (*setup)(struct net_device *))
3904693dfd5aSErez Shitrit {
39058e959601SNiranjana Vishwanathapura 	struct net_device *netdev;
39068e959601SNiranjana Vishwanathapura 	struct rdma_netdev *rn;
39078e959601SNiranjana Vishwanathapura 
3908693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
3909693dfd5aSErez Shitrit 		return ERR_PTR(-EOPNOTSUPP);
3910693dfd5aSErez Shitrit 
39118e959601SNiranjana Vishwanathapura 	netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3912693dfd5aSErez Shitrit 					name, setup);
39138e959601SNiranjana Vishwanathapura 	if (likely(!IS_ERR_OR_NULL(netdev))) {
39148e959601SNiranjana Vishwanathapura 		rn = netdev_priv(netdev);
39158e959601SNiranjana Vishwanathapura 		rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
3916693dfd5aSErez Shitrit 	}
39178e959601SNiranjana Vishwanathapura 	return netdev;
3918693dfd5aSErez Shitrit }
3919693dfd5aSErez Shitrit 
3920fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
3921fe248c3aSMaor Gottlieb {
3922fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
3923fe248c3aSMaor Gottlieb 		return;
3924fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
3925fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
3926fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
3927fe248c3aSMaor Gottlieb }
3928fe248c3aSMaor Gottlieb 
392903404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
393003404e8aSMaor Gottlieb {
393103404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
393203404e8aSMaor Gottlieb 		return;
393303404e8aSMaor Gottlieb 
393403404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
3935fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
3936fe248c3aSMaor Gottlieb }
3937fe248c3aSMaor Gottlieb 
3938fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3939fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
3940fe248c3aSMaor Gottlieb {
3941fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3942fe248c3aSMaor Gottlieb 	char lbuf[20];
3943fe248c3aSMaor Gottlieb 	int len;
3944fe248c3aSMaor Gottlieb 
3945fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3946fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
3947fe248c3aSMaor Gottlieb }
3948fe248c3aSMaor Gottlieb 
3949fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3950fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
3951fe248c3aSMaor Gottlieb {
3952fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3953fe248c3aSMaor Gottlieb 	u32 timeout;
3954fe248c3aSMaor Gottlieb 	u32 var;
3955fe248c3aSMaor Gottlieb 
3956fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
3957fe248c3aSMaor Gottlieb 		return -EFAULT;
3958fe248c3aSMaor Gottlieb 
3959fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3960fe248c3aSMaor Gottlieb 			1000);
3961fe248c3aSMaor Gottlieb 	if (timeout != var)
3962fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3963fe248c3aSMaor Gottlieb 			    timeout);
3964fe248c3aSMaor Gottlieb 
3965fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
3966fe248c3aSMaor Gottlieb 
3967fe248c3aSMaor Gottlieb 	return count;
3968fe248c3aSMaor Gottlieb }
3969fe248c3aSMaor Gottlieb 
3970fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
3971fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
3972fe248c3aSMaor Gottlieb 	.open	= simple_open,
3973fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
3974fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
3975fe248c3aSMaor Gottlieb };
3976fe248c3aSMaor Gottlieb 
3977fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
3978fe248c3aSMaor Gottlieb {
3979fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
3980fe248c3aSMaor Gottlieb 
3981fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
3982fe248c3aSMaor Gottlieb 		return 0;
3983fe248c3aSMaor Gottlieb 
3984fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
3985fe248c3aSMaor Gottlieb 	if (!dbg)
3986fe248c3aSMaor Gottlieb 		return -ENOMEM;
3987fe248c3aSMaor Gottlieb 
3988cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
3989cbafad87SSudip Mukherjee 
3990fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
3991fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
3992fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
3993fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
3994cbafad87SSudip Mukherjee 		goto out_debugfs;
3995fe248c3aSMaor Gottlieb 
3996fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
3997fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
3998fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
3999fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
4000fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
4001fe248c3aSMaor Gottlieb 		goto out_debugfs;
4002fe248c3aSMaor Gottlieb 
4003fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
4004fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
4005fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
4006fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
4007fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
4008fe248c3aSMaor Gottlieb 		goto out_debugfs;
4009fe248c3aSMaor Gottlieb 
4010fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
4011fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
4012fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
4013fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
4014fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
4015fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
4016fe248c3aSMaor Gottlieb 		goto out_debugfs;
4017fe248c3aSMaor Gottlieb 
4018fe248c3aSMaor Gottlieb 	return 0;
4019fe248c3aSMaor Gottlieb 
4020fe248c3aSMaor Gottlieb out_debugfs:
4021fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
4022fe248c3aSMaor Gottlieb 	return -ENOMEM;
402303404e8aSMaor Gottlieb }
402403404e8aSMaor Gottlieb 
402503404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
402603404e8aSMaor Gottlieb {
402703404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
402803404e8aSMaor Gottlieb 		return;
402903404e8aSMaor Gottlieb 
403003404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
403103404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
403203404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
403303404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
403403404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
4035fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
4036fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
4037fe248c3aSMaor Gottlieb 
4038fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
4039fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
404003404e8aSMaor Gottlieb }
404103404e8aSMaor Gottlieb 
404284305d71SLeon Romanovsky static const struct cpumask *
404384305d71SLeon Romanovsky mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
404440b24403SSagi Grimberg {
404540b24403SSagi Grimberg 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
404640b24403SSagi Grimberg 
404740b24403SSagi Grimberg 	return mlx5_get_vector_affinity(dev->mdev, comp_vector);
404840b24403SSagi Grimberg }
404940b24403SSagi Grimberg 
405016c1975fSMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
4051e126ba97SEli Cohen {
40523cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
40533cc297dbSMark Bloch 	cleanup_srcu_struct(&dev->mr_srcu);
40543cc297dbSMark Bloch #endif
405516c1975fSMark Bloch 	kfree(dev->port);
405616c1975fSMark Bloch }
405716c1975fSMark Bloch 
405816c1975fSMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
405916c1975fSMark Bloch {
406016c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
40614babcf97SAviv Heller 	const char *name;
4062e126ba97SEli Cohen 	int err;
4063e126ba97SEli Cohen 
40640837e86aSMark Bloch 	dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
40650837e86aSMark Bloch 			    GFP_KERNEL);
40660837e86aSMark Bloch 	if (!dev->port)
406716c1975fSMark Bloch 		return -ENOMEM;
40680837e86aSMark Bloch 
4069fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
4070e126ba97SEli Cohen 	err = get_port_caps(dev);
4071e126ba97SEli Cohen 	if (err)
40720837e86aSMark Bloch 		goto err_free_port;
4073e126ba97SEli Cohen 
40741b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
4075e126ba97SEli Cohen 		get_ext_port_caps(dev);
4076e126ba97SEli Cohen 
40774babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
40784babcf97SAviv Heller 		name = "mlx5_%d";
40794babcf97SAviv Heller 	else
40804babcf97SAviv Heller 		name = "mlx5_bond_%d";
40814babcf97SAviv Heller 
40824babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
4083e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
4084e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
4085c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
4086938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
4087e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
4088233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
4089233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
40909b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
4091e126ba97SEli Cohen 
40923cc297dbSMark Bloch 	mutex_init(&dev->flow_db.lock);
40933cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
40943cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
40953cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
40963cc297dbSMark Bloch 
40973cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
40983cc297dbSMark Bloch 	err = init_srcu_struct(&dev->mr_srcu);
40993cc297dbSMark Bloch 	if (err)
41003cc297dbSMark Bloch 		goto err_free_port;
41013cc297dbSMark Bloch #endif
41023cc297dbSMark Bloch 
410316c1975fSMark Bloch 	return 0;
410416c1975fSMark Bloch 
410516c1975fSMark Bloch err_free_port:
410616c1975fSMark Bloch 	kfree(dev->port);
410716c1975fSMark Bloch 
410816c1975fSMark Bloch 	return -ENOMEM;
410916c1975fSMark Bloch }
411016c1975fSMark Bloch 
411116c1975fSMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
411216c1975fSMark Bloch {
411316c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
411416c1975fSMark Bloch 	int err;
411516c1975fSMark Bloch 
4116e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
4117e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
4118e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
4119e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
4120e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
4121e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
4122e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
412341c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
412441c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
4125e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
412656e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
4127e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
4128e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
4129e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
4130e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
4131e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
4132e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
4133e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
4134e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
4135e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
4136e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
4137e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
4138e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
4139e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
4140e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
4141e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
4142e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
4143e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
41441707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
4145d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
4146d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
41477d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
4148b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
4149b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
4150e126ba97SEli Cohen 
4151e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
4152e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
4153ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
4154e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
41553cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
41563cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
4157e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
4158e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
4159e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
4160e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
4161e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
4162e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
4163e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
4164e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
4165e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
4166e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
4167e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
4168e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
4169e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
4170e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
4171e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
4172e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
4173e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
4174e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
4175e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
4176e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
4177e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
4178e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
4179e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
4180e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
4181e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
4182e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
4183e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
4184e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
4185e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
4186e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
418756e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
4188e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
4189e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
4190e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
4191e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
41929bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
41938a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
4194d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
41957738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
4196c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
419740b24403SSagi Grimberg 	dev->ib_dev.get_vector_affinity	= mlx5_ib_get_vector_affinity;
41988e959601SNiranjana Vishwanathapura 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
4199693dfd5aSErez Shitrit 		dev->ib_dev.alloc_rdma_netdev	= mlx5_ib_alloc_rdma_netdev;
42008e959601SNiranjana Vishwanathapura 
4201eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
4202eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
4203eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
4204eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
4205eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
4206eff901d3SEli Cohen 	}
4207e126ba97SEli Cohen 
42087c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
42097c2344c3SMaor Gottlieb 
42106e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
42116e8484c5SMax Gurtovoy 
4212d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
4213d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
4214d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
4215d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
4216d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
4217d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4218d2370e0aSMatan Barak 	}
4219d2370e0aSMatan Barak 
4220938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
4221e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4222e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4223e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
4224e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4225e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4226e126ba97SEli Cohen 	}
4227e126ba97SEli Cohen 
4228038d2ef8SMaor Gottlieb 	dev->ib_dev.create_flow	= mlx5_ib_create_flow;
4229038d2ef8SMaor Gottlieb 	dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
423081e30880SYishai Hadas 	dev->ib_dev.uverbs_ex_cmd_mask |=
423181e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
423281e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
423381e30880SYishai Hadas 
4234e126ba97SEli Cohen 	err = init_node_data(dev);
4235e126ba97SEli Cohen 	if (err)
423616c1975fSMark Bloch 		return err;
4237e126ba97SEli Cohen 
4238c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4239c8b89924SMark Bloch 	    MLX5_CAP_GEN(dev->mdev, disable_local_lb))
4240c8b89924SMark Bloch 		mutex_init(&dev->lb_mutex);
4241c8b89924SMark Bloch 
424216c1975fSMark Bloch 	return 0;
424316c1975fSMark Bloch }
424416c1975fSMark Bloch 
424516c1975fSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
424616c1975fSMark Bloch {
424716c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
424816c1975fSMark Bloch 	enum rdma_link_layer ll;
424916c1975fSMark Bloch 	int port_type_cap;
425016c1975fSMark Bloch 	int err;
425116c1975fSMark Bloch 
425216c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
425316c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
425416c1975fSMark Bloch 
4255fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
4256c11a226aSMark Bloch 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
4257c11a226aSMark Bloch 		dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
4258c11a226aSMark Bloch 		dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
4259c11a226aSMark Bloch 		dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
4260c11a226aSMark Bloch 		dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4261c11a226aSMark Bloch 		dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
4262c11a226aSMark Bloch 		dev->ib_dev.uverbs_ex_cmd_mask |=
4263c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
4264c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
4265c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4266c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4267c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
426845f95acdSOr Gerlitz 		err = mlx5_enable_eth(dev);
4269e126ba97SEli Cohen 		if (err)
427016c1975fSMark Bloch 			return err;
4271fd65f1b8SMoni Shoua 		dev->roce.last_port_state = IB_PORT_DOWN;
4272fc24fc5eSAchiad Shochat 	}
4273fc24fc5eSAchiad Shochat 
427416c1975fSMark Bloch 	return 0;
427545bded2cSKamal Heib }
42766aec21f6SHaggai Eran 
427716c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
427816c1975fSMark Bloch {
427916c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
428016c1975fSMark Bloch 	enum rdma_link_layer ll;
428116c1975fSMark Bloch 	int port_type_cap;
42824a2da0b8SParav Pandit 
428316c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
428416c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
428516c1975fSMark Bloch 
428616c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
428716c1975fSMark Bloch 		mlx5_disable_eth(dev);
428816c1975fSMark Bloch 		mlx5_remove_netdev_notifier(dev);
428916c1975fSMark Bloch 	}
429016c1975fSMark Bloch }
429116c1975fSMark Bloch 
429216c1975fSMark Bloch static int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
429316c1975fSMark Bloch {
429416c1975fSMark Bloch 	return create_dev_resources(&dev->devr);
429516c1975fSMark Bloch }
429616c1975fSMark Bloch 
429716c1975fSMark Bloch static void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
429816c1975fSMark Bloch {
429916c1975fSMark Bloch 	destroy_dev_resources(&dev->devr);
430016c1975fSMark Bloch }
430116c1975fSMark Bloch 
430216c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
430316c1975fSMark Bloch {
430407321b3cSMark Bloch 	mlx5_ib_internal_fill_odp_caps(dev);
430507321b3cSMark Bloch 
430616c1975fSMark Bloch 	return mlx5_ib_odp_init_one(dev);
430716c1975fSMark Bloch }
430816c1975fSMark Bloch 
430916c1975fSMark Bloch static int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
431016c1975fSMark Bloch {
43115e1e7612SMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
43125e1e7612SMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
43135e1e7612SMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
43145e1e7612SMark Bloch 
431516c1975fSMark Bloch 		return mlx5_ib_alloc_counters(dev);
43165e1e7612SMark Bloch 	}
431716c1975fSMark Bloch 
431816c1975fSMark Bloch 	return 0;
431916c1975fSMark Bloch }
432016c1975fSMark Bloch 
432116c1975fSMark Bloch static void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
432216c1975fSMark Bloch {
432316c1975fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
432416c1975fSMark Bloch 		mlx5_ib_dealloc_counters(dev);
432516c1975fSMark Bloch }
432616c1975fSMark Bloch 
432716c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
432816c1975fSMark Bloch {
432916c1975fSMark Bloch 	return mlx5_ib_init_cong_debugfs(dev);
433016c1975fSMark Bloch }
433116c1975fSMark Bloch 
433216c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
433316c1975fSMark Bloch {
433416c1975fSMark Bloch 	mlx5_ib_cleanup_cong_debugfs(dev);
433516c1975fSMark Bloch }
433616c1975fSMark Bloch 
433716c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
433816c1975fSMark Bloch {
43395fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
43405fe9dec0SEli Cohen 	if (!dev->mdev->priv.uar)
434116c1975fSMark Bloch 		return -ENOMEM;
434216c1975fSMark Bloch 	return 0;
434316c1975fSMark Bloch }
434416c1975fSMark Bloch 
434516c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
434616c1975fSMark Bloch {
434716c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
434816c1975fSMark Bloch }
434916c1975fSMark Bloch 
435016c1975fSMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
435116c1975fSMark Bloch {
435216c1975fSMark Bloch 	int err;
43535fe9dec0SEli Cohen 
43545fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
43555fe9dec0SEli Cohen 	if (err)
435616c1975fSMark Bloch 		return err;
43575fe9dec0SEli Cohen 
43585fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
43595fe9dec0SEli Cohen 	if (err)
436016c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
43615fe9dec0SEli Cohen 
436216c1975fSMark Bloch 	return err;
436316c1975fSMark Bloch }
43640837e86aSMark Bloch 
436516c1975fSMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
436616c1975fSMark Bloch {
436716c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
436816c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
436916c1975fSMark Bloch }
4370e126ba97SEli Cohen 
437116c1975fSMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
437216c1975fSMark Bloch {
437316c1975fSMark Bloch 	return ib_register_device(&dev->ib_dev, NULL);
437416c1975fSMark Bloch }
437516c1975fSMark Bloch 
437616c1975fSMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
437716c1975fSMark Bloch {
437816c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
437916c1975fSMark Bloch }
438016c1975fSMark Bloch 
438116c1975fSMark Bloch static int mlx5_ib_stage_umr_res_init(struct mlx5_ib_dev *dev)
438216c1975fSMark Bloch {
438316c1975fSMark Bloch 	return create_umr_res(dev);
438416c1975fSMark Bloch }
438516c1975fSMark Bloch 
438616c1975fSMark Bloch static void mlx5_ib_stage_umr_res_cleanup(struct mlx5_ib_dev *dev)
438716c1975fSMark Bloch {
438816c1975fSMark Bloch 	destroy_umrc_res(dev);
438916c1975fSMark Bloch }
439016c1975fSMark Bloch 
439116c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
439216c1975fSMark Bloch {
439303404e8aSMaor Gottlieb 	init_delay_drop(dev);
439403404e8aSMaor Gottlieb 
439516c1975fSMark Bloch 	return 0;
439616c1975fSMark Bloch }
439716c1975fSMark Bloch 
439816c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
439916c1975fSMark Bloch {
440016c1975fSMark Bloch 	cancel_delay_drop(dev);
440116c1975fSMark Bloch }
440216c1975fSMark Bloch 
440316c1975fSMark Bloch static int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev)
440416c1975fSMark Bloch {
440516c1975fSMark Bloch 	int err;
440616c1975fSMark Bloch 	int i;
440716c1975fSMark Bloch 
4408e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
4409281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
4410281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
4411281d1a92SWei Yongjun 		if (err)
441216c1975fSMark Bloch 			return err;
4413e126ba97SEli Cohen 	}
4414e126ba97SEli Cohen 
441516c1975fSMark Bloch 	return 0;
441616c1975fSMark Bloch }
441716c1975fSMark Bloch 
441816c1975fSMark Bloch static void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
441916c1975fSMark Bloch 			     const struct mlx5_ib_profile *profile,
442016c1975fSMark Bloch 			     int stage)
442116c1975fSMark Bloch {
442216c1975fSMark Bloch 	/* Number of stages to cleanup */
442316c1975fSMark Bloch 	while (stage) {
442416c1975fSMark Bloch 		stage--;
442516c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
442616c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
442716c1975fSMark Bloch 	}
442816c1975fSMark Bloch 
442916c1975fSMark Bloch 	ib_dealloc_device((struct ib_device *)dev);
443016c1975fSMark Bloch }
443116c1975fSMark Bloch 
443216c1975fSMark Bloch static void *__mlx5_ib_add(struct mlx5_core_dev *mdev,
443316c1975fSMark Bloch 			   const struct mlx5_ib_profile *profile)
443416c1975fSMark Bloch {
443516c1975fSMark Bloch 	struct mlx5_ib_dev *dev;
443616c1975fSMark Bloch 	int err;
443716c1975fSMark Bloch 	int i;
443816c1975fSMark Bloch 
443916c1975fSMark Bloch 	printk_once(KERN_INFO "%s", mlx5_version);
444016c1975fSMark Bloch 
444116c1975fSMark Bloch 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
444216c1975fSMark Bloch 	if (!dev)
444316c1975fSMark Bloch 		return NULL;
444416c1975fSMark Bloch 
444516c1975fSMark Bloch 	dev->mdev = mdev;
444616c1975fSMark Bloch 
444716c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
444816c1975fSMark Bloch 		if (profile->stage[i].init) {
444916c1975fSMark Bloch 			err = profile->stage[i].init(dev);
445016c1975fSMark Bloch 			if (err)
445116c1975fSMark Bloch 				goto err_out;
445216c1975fSMark Bloch 		}
445316c1975fSMark Bloch 	}
445416c1975fSMark Bloch 
445516c1975fSMark Bloch 	dev->profile = profile;
4456e126ba97SEli Cohen 	dev->ib_active = true;
4457e126ba97SEli Cohen 
44589603b61dSJack Morgenstein 	return dev;
4459e126ba97SEli Cohen 
446016c1975fSMark Bloch err_out:
446116c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
4462e126ba97SEli Cohen 
44639603b61dSJack Morgenstein 	return NULL;
4464e126ba97SEli Cohen }
4465e126ba97SEli Cohen 
446616c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
446716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
446816c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
446916c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
447016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
447116c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
447216c1975fSMark Bloch 		     NULL),
447316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
447416c1975fSMark Bloch 		     mlx5_ib_stage_roce_init,
447516c1975fSMark Bloch 		     mlx5_ib_stage_roce_cleanup),
447616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
447716c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_init,
447816c1975fSMark Bloch 		     mlx5_ib_stage_dev_res_cleanup),
447916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
448016c1975fSMark Bloch 		     mlx5_ib_stage_odp_init,
44813cc297dbSMark Bloch 		     NULL),
448216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
448316c1975fSMark Bloch 		     mlx5_ib_stage_counters_init,
448416c1975fSMark Bloch 		     mlx5_ib_stage_counters_cleanup),
448516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
448616c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
448716c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
448816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
448916c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
449016c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
449116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
449216c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
449316c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
449416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
449516c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
449616c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
449716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UMR_RESOURCES,
449816c1975fSMark Bloch 		     mlx5_ib_stage_umr_res_init,
449916c1975fSMark Bloch 		     mlx5_ib_stage_umr_res_cleanup),
450016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
450116c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
450216c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
450316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
450416c1975fSMark Bloch 		     mlx5_ib_stage_class_attr_init,
450516c1975fSMark Bloch 		     NULL),
450616c1975fSMark Bloch };
450716c1975fSMark Bloch 
450816c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
450916c1975fSMark Bloch {
451016c1975fSMark Bloch 	return __mlx5_ib_add(mdev, &pf_profile);
451116c1975fSMark Bloch }
451216c1975fSMark Bloch 
45139603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
4514e126ba97SEli Cohen {
45159603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
45166aec21f6SHaggai Eran 
451716c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
4518e126ba97SEli Cohen }
4519e126ba97SEli Cohen 
45209603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
45219603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
45229603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
45239603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
4524d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4525d9aaed83SArtemy Kovalyov 	.pfault		= mlx5_ib_pfault,
4526d9aaed83SArtemy Kovalyov #endif
452764613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
4528e126ba97SEli Cohen };
4529e126ba97SEli Cohen 
4530e126ba97SEli Cohen static int __init mlx5_ib_init(void)
4531e126ba97SEli Cohen {
45326aec21f6SHaggai Eran 	int err;
45336aec21f6SHaggai Eran 
453481713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
45359603b61dSJack Morgenstein 
45366aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
45376aec21f6SHaggai Eran 
45386aec21f6SHaggai Eran 	return err;
4539e126ba97SEli Cohen }
4540e126ba97SEli Cohen 
4541e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
4542e126ba97SEli Cohen {
45439603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
4544e126ba97SEli Cohen }
4545e126ba97SEli Cohen 
4546e126ba97SEli Cohen module_init(mlx5_ib_init);
4547e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
4548