xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 31a78a5a7983141c17852d31eb3a1f70d8161225)
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,
439da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
440da7525d2SEran Ben Elisha {
441da7525d2SEran Ben Elisha 	u8 tmp;
442da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
443da7525d2SEran Ben Elisha 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
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 
4601b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
4611b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
4621b5daf11SMajd Dibbiny {
4631b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4641b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4651b5daf11SMajd Dibbiny 	u64 tmp;
4661b5daf11SMajd Dibbiny 	int err;
4671b5daf11SMajd Dibbiny 
4681b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4691b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4701b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
4711b5daf11SMajd Dibbiny 							    sys_image_guid);
4721b5daf11SMajd Dibbiny 
4731b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4741b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
4753f89a643SAchiad Shochat 		break;
4763f89a643SAchiad Shochat 
4773f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4783f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
4793f89a643SAchiad Shochat 		break;
4801b5daf11SMajd Dibbiny 
4811b5daf11SMajd Dibbiny 	default:
4821b5daf11SMajd Dibbiny 		return -EINVAL;
4831b5daf11SMajd Dibbiny 	}
4843f89a643SAchiad Shochat 
4853f89a643SAchiad Shochat 	if (!err)
4863f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
4873f89a643SAchiad Shochat 
4883f89a643SAchiad Shochat 	return err;
4893f89a643SAchiad Shochat 
4901b5daf11SMajd Dibbiny }
4911b5daf11SMajd Dibbiny 
4921b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
4931b5daf11SMajd Dibbiny 				u16 *max_pkeys)
4941b5daf11SMajd Dibbiny {
4951b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4961b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4971b5daf11SMajd Dibbiny 
4981b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4991b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5001b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
5011b5daf11SMajd Dibbiny 
5021b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5031b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5041b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
5051b5daf11SMajd Dibbiny 						pkey_table_size));
5061b5daf11SMajd Dibbiny 		return 0;
5071b5daf11SMajd Dibbiny 
5081b5daf11SMajd Dibbiny 	default:
5091b5daf11SMajd Dibbiny 		return -EINVAL;
5101b5daf11SMajd Dibbiny 	}
5111b5daf11SMajd Dibbiny }
5121b5daf11SMajd Dibbiny 
5131b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
5141b5daf11SMajd Dibbiny 				u32 *vendor_id)
5151b5daf11SMajd Dibbiny {
5161b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5171b5daf11SMajd Dibbiny 
5181b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
5191b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5201b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
5211b5daf11SMajd Dibbiny 
5221b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5231b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5241b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
5251b5daf11SMajd Dibbiny 
5261b5daf11SMajd Dibbiny 	default:
5271b5daf11SMajd Dibbiny 		return -EINVAL;
5281b5daf11SMajd Dibbiny 	}
5291b5daf11SMajd Dibbiny }
5301b5daf11SMajd Dibbiny 
5311b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
5321b5daf11SMajd Dibbiny 				__be64 *node_guid)
5331b5daf11SMajd Dibbiny {
5341b5daf11SMajd Dibbiny 	u64 tmp;
5351b5daf11SMajd Dibbiny 	int err;
5361b5daf11SMajd Dibbiny 
5371b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
5381b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5391b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
5401b5daf11SMajd Dibbiny 
5411b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5421b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
5433f89a643SAchiad Shochat 		break;
5443f89a643SAchiad Shochat 
5453f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5463f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
5473f89a643SAchiad Shochat 		break;
5481b5daf11SMajd Dibbiny 
5491b5daf11SMajd Dibbiny 	default:
5501b5daf11SMajd Dibbiny 		return -EINVAL;
5511b5daf11SMajd Dibbiny 	}
5523f89a643SAchiad Shochat 
5533f89a643SAchiad Shochat 	if (!err)
5543f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
5553f89a643SAchiad Shochat 
5563f89a643SAchiad Shochat 	return err;
5571b5daf11SMajd Dibbiny }
5581b5daf11SMajd Dibbiny 
5591b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
560bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
5611b5daf11SMajd Dibbiny };
5621b5daf11SMajd Dibbiny 
5631b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
5641b5daf11SMajd Dibbiny {
5651b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
5661b5daf11SMajd Dibbiny 
5671b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
5681b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
5691b5daf11SMajd Dibbiny 
5701b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
5711b5daf11SMajd Dibbiny 
5721b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
5731b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
5741b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
5751b5daf11SMajd Dibbiny }
5761b5daf11SMajd Dibbiny 
577e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
5782528e33eSMatan Barak 				struct ib_device_attr *props,
5792528e33eSMatan Barak 				struct ib_udata *uhw)
580e126ba97SEli Cohen {
581e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
582938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
583e126ba97SEli Cohen 	int err = -ENOMEM;
584288c01b7SEli Cohen 	int max_sq_desc;
585e126ba97SEli Cohen 	int max_rq_sg;
586e126ba97SEli Cohen 	int max_sq_sg;
587e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
588402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
589402ca536SBodong Wang 	size_t resp_len;
590402ca536SBodong Wang 	u64 max_tso;
591e126ba97SEli Cohen 
592402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
593402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
594402ca536SBodong Wang 		return -EINVAL;
595402ca536SBodong Wang 	else
596402ca536SBodong Wang 		resp.response_length = resp_len;
597402ca536SBodong Wang 
598402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
5992528e33eSMatan Barak 		return -EINVAL;
6002528e33eSMatan Barak 
601e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
6021b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
6031b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
6041b5daf11SMajd Dibbiny 	if (err)
6051b5daf11SMajd Dibbiny 		return err;
6061b5daf11SMajd Dibbiny 
6071b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
6081b5daf11SMajd Dibbiny 	if (err)
6091b5daf11SMajd Dibbiny 		return err;
6101b5daf11SMajd Dibbiny 
6111b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
6121b5daf11SMajd Dibbiny 	if (err)
6131b5daf11SMajd Dibbiny 		return err;
614e126ba97SEli Cohen 
6159603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
6169603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
6179603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
618e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
619e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
620e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
6211a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
622938fe83cSSaeed Mahameed 
623938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
624e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
625938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
626e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
627938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
628e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
629938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
630e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
631d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
632d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
633d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
634d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
635b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
636b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
637d2370e0aSMatan Barak 	}
638e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
639938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
6402dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
6412dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
6422dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
6432dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
6442dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
6452dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
6462dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
6472dea9094SSagi Grimberg 	}
648938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
649f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
650e126ba97SEli Cohen 
651402ca536SBodong Wang 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
652e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
653e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
65488115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
655e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
656e8161334SNoa Osherovich 		}
657e8161334SNoa Osherovich 
658e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
659e8161334SNoa Osherovich 			props->raw_packet_caps |=
660e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
66188115fe7SBodong Wang 
662402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
663402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
664402ca536SBodong Wang 			if (max_tso) {
665402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
666402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
667402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
668402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
669402ca536SBodong Wang 			}
670402ca536SBodong Wang 		}
67131f69a82SYishai Hadas 
67231f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
67331f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
67431f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
67531f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
67631f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
67731f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
67831f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
67931f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
68031f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
68131f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
68231f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
6834e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
6844e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
68531f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
68631f69a82SYishai Hadas 		}
68731f69a82SYishai Hadas 	} else {
68831f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
68931f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
69031f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
69131f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
692402ca536SBodong Wang 	}
693402ca536SBodong Wang 
694f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
695f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
696f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
697f0313965SErez Shitrit 	}
698f0313965SErez Shitrit 
69903404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
70003404e8aSMaor Gottlieb 	    MLX5_CAP_GEN(dev->mdev, general_notification_event))
70103404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
70203404e8aSMaor Gottlieb 
7031d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
7041d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
7051d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
7061d54f890SYishai Hadas 
707cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
708e8161334SNoa Osherovich 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
709e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
710cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
711e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
712e8161334SNoa Osherovich 	}
713cff5a0f3SMajd Dibbiny 
714da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
715da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
716da6d6ba3SMaor Gottlieb 
717b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
718b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
719b1383aa6SNoa Osherovich 
7201b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
7211b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
722e126ba97SEli Cohen 
723e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
724e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
725938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
726938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
727938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
728938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
729288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
730288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
731288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
732e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
733e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
734986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
735938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
7369f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
737938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
738938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
739938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
740938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
741938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
742938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
743938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
744e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
745e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
746911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
747911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
748da7525d2SEran Ben Elisha 	get_atomic_caps(dev, props);
74981bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
750938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
751938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
752e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
753e126ba97SEli Cohen 					   props->max_mcast_grp;
754e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
75586695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
7567c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
7577c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
758e126ba97SEli Cohen 
7598cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
760938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
7618cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
7628cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
7638cdd312cSHaggai Eran #endif
7648cdd312cSHaggai Eran 
765051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
766051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
767051f2630SLeon Romanovsky 
768eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
769eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
770eff901d3SEli Cohen 
77131f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
77231f69a82SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
77331f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
77431f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
77531f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
77631f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
77731f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
77831f69a82SYishai Hadas 		props->max_wq_type_rq =
77931f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
78031f69a82SYishai Hadas 	}
78131f69a82SYishai Hadas 
782eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
78378b1beb0SLeon Romanovsky 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
78478b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
785eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
78678b1beb0SLeon Romanovsky 		props->tm_caps.flags = IB_TM_CAP_RC;
78778b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
788eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
78978b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
790eb761894SArtemy Kovalyov 	}
791eb761894SArtemy Kovalyov 
79287ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
79387ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
79487ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
79587ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
79687ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
79787ab3f52SYonatan Cohen 	}
79887ab3f52SYonatan Cohen 
7997e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
8007e43a2a5SBodong Wang 		resp.cqe_comp_caps.max_num =
8017e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
8027e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
8037e43a2a5SBodong Wang 		resp.cqe_comp_caps.supported_format =
8047e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_HASH |
8057e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_CSUM;
8067e43a2a5SBodong Wang 		resp.response_length += sizeof(resp.cqe_comp_caps);
8077e43a2a5SBodong Wang 	}
8087e43a2a5SBodong Wang 
809d949167dSBodong Wang 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
810d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
811d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
812d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
813d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
814d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
815d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
816d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
817d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
818d949167dSBodong Wang 		}
819d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
820d949167dSBodong Wang 	}
821d949167dSBodong Wang 
8229f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
8239f885201SLeon Romanovsky 			uhw->outlen)) {
824795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
8259f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
826795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
827050da902SBodong Wang 
828050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
829050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
830050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
831050da902SBodong Wang 
8329f885201SLeon Romanovsky 		resp.response_length +=
8339f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
8349f885201SLeon Romanovsky 	}
8359f885201SLeon Romanovsky 
836de57f2adSGuy Levi 	if (field_avail(typeof(resp), flags, uhw->outlen)) {
837de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
8387a0c8f42SGuy Levi 
839de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
840de57f2adSGuy Levi 			resp.flags |=
841de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
8427a0c8f42SGuy Levi 
8437a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
8447a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
845de57f2adSGuy Levi 	}
8469f885201SLeon Romanovsky 
84796dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
84896dc3fc5SNoa Osherovich 			uhw->outlen)) {
84996dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
85096dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
85196dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
85296dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
85396dc3fc5SNoa Osherovich 
85496dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
85596dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
85696dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
85796dc3fc5SNoa Osherovich 
85896dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
85996dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
86096dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
86196dc3fc5SNoa Osherovich 
86296dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
86396dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
86496dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
86596dc3fc5SNoa Osherovich 		}
86696dc3fc5SNoa Osherovich 	}
86796dc3fc5SNoa Osherovich 
868b4f34597SNoa Osherovich 	if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen)) {
869b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
870b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
871b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
872b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
873b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
874b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
875b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
876b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
877b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
878b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
879b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
880b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
881b4f34597SNoa Osherovich 		}
882b4f34597SNoa Osherovich 	}
883b4f34597SNoa Osherovich 
884f95ef6cbSMaor Gottlieb 	if (field_avail(typeof(resp), tunnel_offloads_caps,
885f95ef6cbSMaor Gottlieb 			uhw->outlen)) {
886f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
887f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
888f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
889f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
890f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
891f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
892f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
893f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
894f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
895f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
896f95ef6cbSMaor Gottlieb 	}
897f95ef6cbSMaor Gottlieb 
898402ca536SBodong Wang 	if (uhw->outlen) {
899402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
900402ca536SBodong Wang 
901402ca536SBodong Wang 		if (err)
902402ca536SBodong Wang 			return err;
903402ca536SBodong Wang 	}
904402ca536SBodong Wang 
9051b5daf11SMajd Dibbiny 	return 0;
9061b5daf11SMajd Dibbiny }
907e126ba97SEli Cohen 
9081b5daf11SMajd Dibbiny enum mlx5_ib_width {
9091b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
9101b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
9111b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
9121b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
9131b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
9141b5daf11SMajd Dibbiny };
9151b5daf11SMajd Dibbiny 
9161b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
9171b5daf11SMajd Dibbiny 				  u8 *ib_width)
9181b5daf11SMajd Dibbiny {
9191b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
9201b5daf11SMajd Dibbiny 	int err = 0;
9211b5daf11SMajd Dibbiny 
9221b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
9231b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
9241b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
9251b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
9261b5daf11SMajd Dibbiny 			    (int)active_width);
9271b5daf11SMajd Dibbiny 		err = -EINVAL;
9281b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
9291b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
9301b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
9311b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
9321b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
9331b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
9341b5daf11SMajd Dibbiny 	} else {
9351b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
9361b5daf11SMajd Dibbiny 			    (int)active_width);
9371b5daf11SMajd Dibbiny 		err = -EINVAL;
9381b5daf11SMajd Dibbiny 	}
9391b5daf11SMajd Dibbiny 
9401b5daf11SMajd Dibbiny 	return err;
9411b5daf11SMajd Dibbiny }
9421b5daf11SMajd Dibbiny 
9431b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
9441b5daf11SMajd Dibbiny {
9451b5daf11SMajd Dibbiny 	switch (mtu) {
9461b5daf11SMajd Dibbiny 	case 256: return 1;
9471b5daf11SMajd Dibbiny 	case 512: return 2;
9481b5daf11SMajd Dibbiny 	case 1024: return 3;
9491b5daf11SMajd Dibbiny 	case 2048: return 4;
9501b5daf11SMajd Dibbiny 	case 4096: return 5;
9511b5daf11SMajd Dibbiny 	default:
9521b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
9531b5daf11SMajd Dibbiny 		return -1;
9541b5daf11SMajd Dibbiny 	}
9551b5daf11SMajd Dibbiny }
9561b5daf11SMajd Dibbiny 
9571b5daf11SMajd Dibbiny enum ib_max_vl_num {
9581b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
9591b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
9601b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
9611b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
9621b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
9631b5daf11SMajd Dibbiny };
9641b5daf11SMajd Dibbiny 
9651b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
9661b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
9671b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
9681b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
9691b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
9701b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
9711b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
9721b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
9731b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
9741b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
9751b5daf11SMajd Dibbiny };
9761b5daf11SMajd Dibbiny 
9771b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
9781b5daf11SMajd Dibbiny 				u8 *max_vl_num)
9791b5daf11SMajd Dibbiny {
9801b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
9811b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
9821b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
9831b5daf11SMajd Dibbiny 		break;
9841b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
9851b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
9861b5daf11SMajd Dibbiny 		break;
9871b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
9881b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
9891b5daf11SMajd Dibbiny 		break;
9901b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
9911b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
9921b5daf11SMajd Dibbiny 		break;
9931b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
9941b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
9951b5daf11SMajd Dibbiny 		break;
9961b5daf11SMajd Dibbiny 
9971b5daf11SMajd Dibbiny 	default:
9981b5daf11SMajd Dibbiny 		return -EINVAL;
9991b5daf11SMajd Dibbiny 	}
10001b5daf11SMajd Dibbiny 
10011b5daf11SMajd Dibbiny 	return 0;
10021b5daf11SMajd Dibbiny }
10031b5daf11SMajd Dibbiny 
10041b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
10051b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
10061b5daf11SMajd Dibbiny {
10071b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
10081b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
10091b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1010046339eaSSaeed Mahameed 	u16 max_mtu;
1011046339eaSSaeed Mahameed 	u16 oper_mtu;
10121b5daf11SMajd Dibbiny 	int err;
10131b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
10141b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
10151b5daf11SMajd Dibbiny 
10161b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
10171b5daf11SMajd Dibbiny 	if (!rep) {
10181b5daf11SMajd Dibbiny 		err = -ENOMEM;
10191b5daf11SMajd Dibbiny 		goto out;
10201b5daf11SMajd Dibbiny 	}
10211b5daf11SMajd Dibbiny 
1022c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
10231b5daf11SMajd Dibbiny 
10241b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
10251b5daf11SMajd Dibbiny 	if (err)
10261b5daf11SMajd Dibbiny 		goto out;
10271b5daf11SMajd Dibbiny 
10281b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
10291b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
10301b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
10311b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
10321b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
10331b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
10341b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
10351b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
10361b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
10371b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
10381b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
10391b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
10401b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
10411b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
1042eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
10431b5daf11SMajd Dibbiny 
10441b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
10451b5daf11SMajd Dibbiny 	if (err)
10461b5daf11SMajd Dibbiny 		goto out;
10471b5daf11SMajd Dibbiny 
10481b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
10491b5daf11SMajd Dibbiny 				     &props->active_width);
10501b5daf11SMajd Dibbiny 	if (err)
10511b5daf11SMajd Dibbiny 		goto out;
1052d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
10531b5daf11SMajd Dibbiny 	if (err)
10541b5daf11SMajd Dibbiny 		goto out;
10551b5daf11SMajd Dibbiny 
1056facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
10571b5daf11SMajd Dibbiny 
10581b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
10591b5daf11SMajd Dibbiny 
1060facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
10611b5daf11SMajd Dibbiny 
10621b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
10631b5daf11SMajd Dibbiny 
10641b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
10651b5daf11SMajd Dibbiny 	if (err)
10661b5daf11SMajd Dibbiny 		goto out;
10671b5daf11SMajd Dibbiny 
10681b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
10691b5daf11SMajd Dibbiny 				   &props->max_vl_num);
10701b5daf11SMajd Dibbiny out:
10711b5daf11SMajd Dibbiny 	kfree(rep);
1072e126ba97SEli Cohen 	return err;
1073e126ba97SEli Cohen }
1074e126ba97SEli Cohen 
1075e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1076e126ba97SEli Cohen 		       struct ib_port_attr *props)
1077e126ba97SEli Cohen {
1078095b0927SIlan Tayari 	unsigned int count;
1079095b0927SIlan Tayari 	int ret;
1080095b0927SIlan Tayari 
10811b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
10821b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1083095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1084095b0927SIlan Tayari 		break;
1085e126ba97SEli Cohen 
10861b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1087095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1088095b0927SIlan Tayari 		break;
10891b5daf11SMajd Dibbiny 
10903f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1091095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1092095b0927SIlan Tayari 		break;
10933f89a643SAchiad Shochat 
10941b5daf11SMajd Dibbiny 	default:
1095095b0927SIlan Tayari 		ret = -EINVAL;
1096e126ba97SEli Cohen 	}
1097095b0927SIlan Tayari 
1098095b0927SIlan Tayari 	if (!ret && props) {
1099095b0927SIlan Tayari 		count = mlx5_core_reserved_gids_count(to_mdev(ibdev)->mdev);
1100095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1101095b0927SIlan Tayari 	}
1102095b0927SIlan Tayari 	return ret;
1103e126ba97SEli Cohen }
1104e126ba97SEli Cohen 
1105e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1106e126ba97SEli Cohen 			     union ib_gid *gid)
1107e126ba97SEli Cohen {
11081b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11091b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1110e126ba97SEli Cohen 
11111b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
11121b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
11131b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1114e126ba97SEli Cohen 
11151b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
11161b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1117e126ba97SEli Cohen 
11181b5daf11SMajd Dibbiny 	default:
11191b5daf11SMajd Dibbiny 		return -EINVAL;
11201b5daf11SMajd Dibbiny 	}
1121e126ba97SEli Cohen 
1122e126ba97SEli Cohen }
1123e126ba97SEli Cohen 
1124e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1125e126ba97SEli Cohen 			      u16 *pkey)
1126e126ba97SEli Cohen {
11271b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11281b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1129e126ba97SEli Cohen 
11301b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
11311b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
11321b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1133e126ba97SEli Cohen 
11341b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
11351b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
11361b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
11371b5daf11SMajd Dibbiny 						 pkey);
11381b5daf11SMajd Dibbiny 	default:
11391b5daf11SMajd Dibbiny 		return -EINVAL;
1140e126ba97SEli Cohen 	}
11411b5daf11SMajd Dibbiny }
1142e126ba97SEli Cohen 
1143e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1144e126ba97SEli Cohen 				 struct ib_device_modify *props)
1145e126ba97SEli Cohen {
1146e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1147e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1148e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1149e126ba97SEli Cohen 	int err;
1150e126ba97SEli Cohen 
1151e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1152e126ba97SEli Cohen 		return -EOPNOTSUPP;
1153e126ba97SEli Cohen 
1154e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1155e126ba97SEli Cohen 		return 0;
1156e126ba97SEli Cohen 
1157e126ba97SEli Cohen 	/*
1158e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1159e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1160e126ba97SEli Cohen 	 */
1161bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
11629603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1163e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1164e126ba97SEli Cohen 	if (err)
1165e126ba97SEli Cohen 		return err;
1166e126ba97SEli Cohen 
1167bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1168e126ba97SEli Cohen 
1169e126ba97SEli Cohen 	return err;
1170e126ba97SEli Cohen }
1171e126ba97SEli Cohen 
1172cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1173cdbe33d0SEli Cohen 				u32 value)
1174cdbe33d0SEli Cohen {
1175cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1176cdbe33d0SEli Cohen 	int err;
1177cdbe33d0SEli Cohen 
1178cdbe33d0SEli Cohen 	err = mlx5_query_hca_vport_context(dev->mdev, 0,
1179cdbe33d0SEli Cohen 					   port_num, 0, &ctx);
1180cdbe33d0SEli Cohen 	if (err)
1181cdbe33d0SEli Cohen 		return err;
1182cdbe33d0SEli Cohen 
1183cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1184cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1185cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1186cdbe33d0SEli Cohen 		return -EINVAL;
1187cdbe33d0SEli Cohen 	}
1188cdbe33d0SEli Cohen 
1189cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1190cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1191cdbe33d0SEli Cohen 	err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1192cdbe33d0SEli Cohen 						 port_num, 0, &ctx);
1193cdbe33d0SEli Cohen 
1194cdbe33d0SEli Cohen 	return err;
1195cdbe33d0SEli Cohen }
1196cdbe33d0SEli Cohen 
1197e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1198e126ba97SEli Cohen 			       struct ib_port_modify *props)
1199e126ba97SEli Cohen {
1200e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1201e126ba97SEli Cohen 	struct ib_port_attr attr;
1202e126ba97SEli Cohen 	u32 tmp;
1203e126ba97SEli Cohen 	int err;
1204cdbe33d0SEli Cohen 	u32 change_mask;
1205cdbe33d0SEli Cohen 	u32 value;
1206cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1207cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1208cdbe33d0SEli Cohen 
1209ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1210ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1211ec255879SMajd Dibbiny 	 */
1212ec255879SMajd Dibbiny 	if (!is_ib)
1213ec255879SMajd Dibbiny 		return 0;
1214ec255879SMajd Dibbiny 
1215cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1216cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1217cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1218cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1219cdbe33d0SEli Cohen 	}
1220e126ba97SEli Cohen 
1221e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1222e126ba97SEli Cohen 
1223c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1224e126ba97SEli Cohen 	if (err)
1225e126ba97SEli Cohen 		goto out;
1226e126ba97SEli Cohen 
1227e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1228e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1229e126ba97SEli Cohen 
12309603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1231e126ba97SEli Cohen 
1232e126ba97SEli Cohen out:
1233e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1234e126ba97SEli Cohen 	return err;
1235e126ba97SEli Cohen }
1236e126ba97SEli Cohen 
123730aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
123830aa60b3SEli Cohen {
123930aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
124030aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
124130aa60b3SEli Cohen }
124230aa60b3SEli Cohen 
1243*31a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1244*31a78a5aSYishai Hadas {
1245*31a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
1246*31a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
1247*31a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
1248*31a78a5aSYishai Hadas 
1249*31a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
1250*31a78a5aSYishai Hadas }
1251*31a78a5aSYishai Hadas 
1252b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1253b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
1254*31a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1255b037c29aSEli Cohen {
1256b037c29aSEli Cohen 	int uars_per_sys_page;
1257b037c29aSEli Cohen 	int bfregs_per_sys_page;
1258b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1259b037c29aSEli Cohen 
1260b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1261b037c29aSEli Cohen 		return -EINVAL;
1262b037c29aSEli Cohen 
1263b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1264b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1265b037c29aSEli Cohen 
1266b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1267b037c29aSEli Cohen 		return -ENOMEM;
1268b037c29aSEli Cohen 
1269b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1270b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1271*31a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1272b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1273b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1274b037c29aSEli Cohen 		return -EINVAL;
1275b037c29aSEli Cohen 
1276*31a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1277*31a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1278*31a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1279*31a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1280*31a78a5aSYishai Hadas 
1281*31a78a5aSYishai 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",
1282b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1283b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
1284*31a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
1285*31a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1286b037c29aSEli Cohen 
1287b037c29aSEli Cohen 	return 0;
1288b037c29aSEli Cohen }
1289b037c29aSEli Cohen 
1290b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1291b037c29aSEli Cohen {
1292b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1293b037c29aSEli Cohen 	int err;
1294b037c29aSEli Cohen 	int i;
1295b037c29aSEli Cohen 
1296b037c29aSEli Cohen 	bfregi = &context->bfregi;
1297*31a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1298b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1299b037c29aSEli Cohen 		if (err)
1300b037c29aSEli Cohen 			goto error;
1301b037c29aSEli Cohen 
1302b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1303b037c29aSEli Cohen 	}
1304b037c29aSEli Cohen 	return 0;
1305b037c29aSEli Cohen 
1306b037c29aSEli Cohen error:
1307b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1308b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1309b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1310b037c29aSEli Cohen 
1311b037c29aSEli Cohen 	return err;
1312b037c29aSEli Cohen }
1313b037c29aSEli Cohen 
1314b037c29aSEli Cohen static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1315b037c29aSEli Cohen {
1316b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1317b037c29aSEli Cohen 	int err;
1318b037c29aSEli Cohen 	int i;
1319b037c29aSEli Cohen 
1320b037c29aSEli Cohen 	bfregi = &context->bfregi;
1321*31a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1322b037c29aSEli Cohen 		err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1323b037c29aSEli Cohen 		if (err) {
1324b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1325b037c29aSEli Cohen 			return err;
1326b037c29aSEli Cohen 		}
1327b037c29aSEli Cohen 	}
1328b037c29aSEli Cohen 	return 0;
1329b037c29aSEli Cohen }
1330b037c29aSEli Cohen 
1331c85023e1SHuy Nguyen static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1332c85023e1SHuy Nguyen {
1333c85023e1SHuy Nguyen 	int err;
1334c85023e1SHuy Nguyen 
1335c85023e1SHuy Nguyen 	err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1336c85023e1SHuy Nguyen 	if (err)
1337c85023e1SHuy Nguyen 		return err;
1338c85023e1SHuy Nguyen 
1339c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1340c85023e1SHuy Nguyen 	    !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1341c85023e1SHuy Nguyen 		return err;
1342c85023e1SHuy Nguyen 
1343c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1344c85023e1SHuy Nguyen 	dev->user_td++;
1345c85023e1SHuy Nguyen 
1346c85023e1SHuy Nguyen 	if (dev->user_td == 2)
1347c85023e1SHuy Nguyen 		err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1348c85023e1SHuy Nguyen 
1349c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1350c85023e1SHuy Nguyen 	return err;
1351c85023e1SHuy Nguyen }
1352c85023e1SHuy Nguyen 
1353c85023e1SHuy Nguyen static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1354c85023e1SHuy Nguyen {
1355c85023e1SHuy Nguyen 	mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1356c85023e1SHuy Nguyen 
1357c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1358c85023e1SHuy Nguyen 	    !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1359c85023e1SHuy Nguyen 		return;
1360c85023e1SHuy Nguyen 
1361c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1362c85023e1SHuy Nguyen 	dev->user_td--;
1363c85023e1SHuy Nguyen 
1364c85023e1SHuy Nguyen 	if (dev->user_td < 2)
1365c85023e1SHuy Nguyen 		mlx5_nic_vport_update_local_lb(dev->mdev, false);
1366c85023e1SHuy Nguyen 
1367c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1368c85023e1SHuy Nguyen }
1369c85023e1SHuy Nguyen 
1370e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1371e126ba97SEli Cohen 						  struct ib_udata *udata)
1372e126ba97SEli Cohen {
1373e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1374b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1375b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1376e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
13772f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
137878c0f98cSEli Cohen 	int ver;
1379e126ba97SEli Cohen 	int err;
1380a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1381a168a41cSMajd Dibbiny 				     max_cqe_version);
1382b037c29aSEli Cohen 	bool lib_uar_4k;
1383e126ba97SEli Cohen 
1384e126ba97SEli Cohen 	if (!dev->ib_active)
1385e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1386e126ba97SEli Cohen 
1387e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
138878c0f98cSEli Cohen 		ver = 0;
1389e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
139078c0f98cSEli Cohen 		ver = 2;
139178c0f98cSEli Cohen 	else
139278c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
139378c0f98cSEli Cohen 
1394e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1395e126ba97SEli Cohen 	if (err)
1396e126ba97SEli Cohen 		return ERR_PTR(err);
1397e126ba97SEli Cohen 
1398b368d7cbSMatan Barak 	if (req.flags)
139978c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
140078c0f98cSEli Cohen 
1401f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1402b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1403b368d7cbSMatan Barak 
14042f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
14052f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
14062f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1407e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1408e126ba97SEli Cohen 
1409938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
14102cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1411938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1412b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1413938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1414938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1415938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1416938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1417938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1418f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1419f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1420f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
142130aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
142230aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
142330aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
142430aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1425b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1426b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1427e126ba97SEli Cohen 
1428e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1429e126ba97SEli Cohen 	if (!context)
1430e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1431e126ba97SEli Cohen 
143230aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
14332f5ff264SEli Cohen 	bfregi = &context->bfregi;
1434b037c29aSEli Cohen 
1435b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
1436*31a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1437b037c29aSEli Cohen 	if (err)
1438b037c29aSEli Cohen 		goto out_ctx;
1439b037c29aSEli Cohen 
14402f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1441b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
1442*31a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1443b037c29aSEli Cohen 				GFP_KERNEL);
1444b037c29aSEli Cohen 	if (!bfregi->count) {
1445e126ba97SEli Cohen 		err = -ENOMEM;
1446e126ba97SEli Cohen 		goto out_ctx;
1447e126ba97SEli Cohen 	}
1448e126ba97SEli Cohen 
1449b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1450b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1451e126ba97SEli Cohen 				    GFP_KERNEL);
1452b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1453e126ba97SEli Cohen 		err = -ENOMEM;
1454e126ba97SEli Cohen 		goto out_count;
1455e126ba97SEli Cohen 	}
1456e126ba97SEli Cohen 
1457b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1458b037c29aSEli Cohen 	if (err)
1459b037c29aSEli Cohen 		goto out_sys_pages;
1460b037c29aSEli Cohen 
1461b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1462b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1463b4cfe447SHaggai Eran #endif
1464b4cfe447SHaggai Eran 
14657d0cc6edSArtemy Kovalyov 	context->upd_xlt_page = __get_free_page(GFP_KERNEL);
14667d0cc6edSArtemy Kovalyov 	if (!context->upd_xlt_page) {
14677d0cc6edSArtemy Kovalyov 		err = -ENOMEM;
14687d0cc6edSArtemy Kovalyov 		goto out_uars;
14697d0cc6edSArtemy Kovalyov 	}
14707d0cc6edSArtemy Kovalyov 	mutex_init(&context->upd_xlt_page_mutex);
14717d0cc6edSArtemy Kovalyov 
1472146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1473c85023e1SHuy Nguyen 		err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
1474146d2f1aSmajd@mellanox.com 		if (err)
14757d0cc6edSArtemy Kovalyov 			goto out_page;
1476146d2f1aSmajd@mellanox.com 	}
1477146d2f1aSmajd@mellanox.com 
14787c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
1479ad9a3668SMajd Dibbiny 	mutex_init(&context->vma_private_list_mutex);
1480e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1481e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1482e126ba97SEli Cohen 
14832f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1484938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
1485b368d7cbSMatan Barak 
1486f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1487f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1488b368d7cbSMatan Barak 
1489402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
14906ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
14916ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1492402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1493402ca536SBodong Wang 	}
1494402ca536SBodong Wang 
149578984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
149678984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
149778984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
149878984898SOr Gerlitz 			resp.eth_min_inline++;
149978984898SOr Gerlitz 		}
150078984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
150178984898SOr Gerlitz 	}
150278984898SOr Gerlitz 
1503bc5c6eedSNoa Osherovich 	/*
1504bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1505bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1506bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1507bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1508bc5c6eedSNoa Osherovich 	 */
1509de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1510de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1511b368d7cbSMatan Barak 			resp.comp_mask |=
1512b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1513b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1514de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1515de8d6e02SEli Cohen 		}
1516f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.hca_core_clock_offset) +
1517402ca536SBodong Wang 					sizeof(resp.reserved2);
1518b368d7cbSMatan Barak 	}
1519b368d7cbSMatan Barak 
152030aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
152130aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
152230aa60b3SEli Cohen 
152330aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
152430aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
152530aa60b3SEli Cohen 
1526*31a78a5aSYishai Hadas 	if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1527*31a78a5aSYishai Hadas 		resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1528*31a78a5aSYishai Hadas 		resp.response_length += sizeof(resp.num_dyn_bfregs);
1529*31a78a5aSYishai Hadas 	}
1530*31a78a5aSYishai Hadas 
1531b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1532e126ba97SEli Cohen 	if (err)
1533146d2f1aSmajd@mellanox.com 		goto out_td;
1534e126ba97SEli Cohen 
15352f5ff264SEli Cohen 	bfregi->ver = ver;
15362f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1537f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
153830aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
153930aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1540f72300c5SHaggai Abramovsky 
1541e126ba97SEli Cohen 	return &context->ibucontext;
1542e126ba97SEli Cohen 
1543146d2f1aSmajd@mellanox.com out_td:
1544146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1545c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1546146d2f1aSmajd@mellanox.com 
15477d0cc6edSArtemy Kovalyov out_page:
15487d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
15497d0cc6edSArtemy Kovalyov 
1550e126ba97SEli Cohen out_uars:
1551b037c29aSEli Cohen 	deallocate_uars(dev, context);
1552b037c29aSEli Cohen 
1553b037c29aSEli Cohen out_sys_pages:
1554b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1555b037c29aSEli Cohen 
1556e126ba97SEli Cohen out_count:
15572f5ff264SEli Cohen 	kfree(bfregi->count);
1558e126ba97SEli Cohen 
1559e126ba97SEli Cohen out_ctx:
1560e126ba97SEli Cohen 	kfree(context);
1561b037c29aSEli Cohen 
1562e126ba97SEli Cohen 	return ERR_PTR(err);
1563e126ba97SEli Cohen }
1564e126ba97SEli Cohen 
1565e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1566e126ba97SEli Cohen {
1567e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1568e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1569b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1570e126ba97SEli Cohen 
1571b037c29aSEli Cohen 	bfregi = &context->bfregi;
1572146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1573c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1574146d2f1aSmajd@mellanox.com 
15757d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
1576b037c29aSEli Cohen 	deallocate_uars(dev, context);
1577b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
15782f5ff264SEli Cohen 	kfree(bfregi->count);
1579e126ba97SEli Cohen 	kfree(context);
1580e126ba97SEli Cohen 
1581e126ba97SEli Cohen 	return 0;
1582e126ba97SEli Cohen }
1583e126ba97SEli Cohen 
1584b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1585b037c29aSEli Cohen 				 struct mlx5_bfreg_info *bfregi,
1586b037c29aSEli Cohen 				 int idx)
1587e126ba97SEli Cohen {
1588b037c29aSEli Cohen 	int fw_uars_per_page;
1589b037c29aSEli Cohen 
1590b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1591b037c29aSEli Cohen 
1592b037c29aSEli Cohen 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1593b037c29aSEli Cohen 			bfregi->sys_pages[idx] / fw_uars_per_page;
1594e126ba97SEli Cohen }
1595e126ba97SEli Cohen 
1596e126ba97SEli Cohen static int get_command(unsigned long offset)
1597e126ba97SEli Cohen {
1598e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1599e126ba97SEli Cohen }
1600e126ba97SEli Cohen 
1601e126ba97SEli Cohen static int get_arg(unsigned long offset)
1602e126ba97SEli Cohen {
1603e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1604e126ba97SEli Cohen }
1605e126ba97SEli Cohen 
1606e126ba97SEli Cohen static int get_index(unsigned long offset)
1607e126ba97SEli Cohen {
1608e126ba97SEli Cohen 	return get_arg(offset);
1609e126ba97SEli Cohen }
1610e126ba97SEli Cohen 
16117c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
16127c2344c3SMaor Gottlieb {
16137c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
16147c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
16157c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
16167c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
16177c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
16187c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
16197c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
16207c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
16217c2344c3SMaor Gottlieb 	 */
16227c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
16237c2344c3SMaor Gottlieb }
16247c2344c3SMaor Gottlieb 
16257c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
16267c2344c3SMaor Gottlieb {
16277c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
16287c2344c3SMaor Gottlieb 
16297c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
16307c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
16317c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
16327c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
16337c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
16347c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
16357c2344c3SMaor Gottlieb 	 * process is exiting.
16367c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
16377c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
16387c2344c3SMaor Gottlieb 	 */
16397c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
16407c2344c3SMaor Gottlieb 
16417c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
16427c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
16437c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
16447c2344c3SMaor Gottlieb 	 */
16457c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
1646ad9a3668SMajd Dibbiny 	mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
16477c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
1648ad9a3668SMajd Dibbiny 	mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
16497c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
16507c2344c3SMaor Gottlieb }
16517c2344c3SMaor Gottlieb 
16527c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
16537c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
16547c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
16557c2344c3SMaor Gottlieb };
16567c2344c3SMaor Gottlieb 
16577c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
16587c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
16597c2344c3SMaor Gottlieb {
16607c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
16617c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
16627c2344c3SMaor Gottlieb 
16637c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
16647c2344c3SMaor Gottlieb 	if (!vma_prv)
16657c2344c3SMaor Gottlieb 		return -ENOMEM;
16667c2344c3SMaor Gottlieb 
16677c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
1668ad9a3668SMajd Dibbiny 	vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
16697c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
16707c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
16717c2344c3SMaor Gottlieb 
1672ad9a3668SMajd Dibbiny 	mutex_lock(&ctx->vma_private_list_mutex);
16737c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
1674ad9a3668SMajd Dibbiny 	mutex_unlock(&ctx->vma_private_list_mutex);
16757c2344c3SMaor Gottlieb 
16767c2344c3SMaor Gottlieb 	return 0;
16777c2344c3SMaor Gottlieb }
16787c2344c3SMaor Gottlieb 
16797c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
16807c2344c3SMaor Gottlieb {
16817c2344c3SMaor Gottlieb 	int ret;
16827c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
16837c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
16847c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
16857c2344c3SMaor Gottlieb 	struct task_struct *owning_process  = NULL;
16867c2344c3SMaor Gottlieb 	struct mm_struct   *owning_mm       = NULL;
16877c2344c3SMaor Gottlieb 
16887c2344c3SMaor Gottlieb 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
16897c2344c3SMaor Gottlieb 	if (!owning_process)
16907c2344c3SMaor Gottlieb 		return;
16917c2344c3SMaor Gottlieb 
16927c2344c3SMaor Gottlieb 	owning_mm = get_task_mm(owning_process);
16937c2344c3SMaor Gottlieb 	if (!owning_mm) {
16947c2344c3SMaor Gottlieb 		pr_info("no mm, disassociate ucontext is pending task termination\n");
16957c2344c3SMaor Gottlieb 		while (1) {
16967c2344c3SMaor Gottlieb 			put_task_struct(owning_process);
16977c2344c3SMaor Gottlieb 			usleep_range(1000, 2000);
16987c2344c3SMaor Gottlieb 			owning_process = get_pid_task(ibcontext->tgid,
16997c2344c3SMaor Gottlieb 						      PIDTYPE_PID);
17007c2344c3SMaor Gottlieb 			if (!owning_process ||
17017c2344c3SMaor Gottlieb 			    owning_process->state == TASK_DEAD) {
17027c2344c3SMaor Gottlieb 				pr_info("disassociate ucontext done, task was terminated\n");
17037c2344c3SMaor Gottlieb 				/* in case task was dead need to release the
17047c2344c3SMaor Gottlieb 				 * task struct.
17057c2344c3SMaor Gottlieb 				 */
17067c2344c3SMaor Gottlieb 				if (owning_process)
17077c2344c3SMaor Gottlieb 					put_task_struct(owning_process);
17087c2344c3SMaor Gottlieb 				return;
17097c2344c3SMaor Gottlieb 			}
17107c2344c3SMaor Gottlieb 		}
17117c2344c3SMaor Gottlieb 	}
17127c2344c3SMaor Gottlieb 
17137c2344c3SMaor Gottlieb 	/* need to protect from a race on closing the vma as part of
17147c2344c3SMaor Gottlieb 	 * mlx5_ib_vma_close.
17157c2344c3SMaor Gottlieb 	 */
1716ecc7d83bSMaor Gottlieb 	down_write(&owning_mm->mmap_sem);
1717ad9a3668SMajd Dibbiny 	mutex_lock(&context->vma_private_list_mutex);
17187c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
17197c2344c3SMaor Gottlieb 				 list) {
17207c2344c3SMaor Gottlieb 		vma = vma_private->vma;
17217c2344c3SMaor Gottlieb 		ret = zap_vma_ptes(vma, vma->vm_start,
17227c2344c3SMaor Gottlieb 				   PAGE_SIZE);
17237c2344c3SMaor Gottlieb 		WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
17247c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
17257c2344c3SMaor Gottlieb 		 * not access ops any more.
17267c2344c3SMaor Gottlieb 		 */
172713776612SMaor Gottlieb 		vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
17287c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
17297c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
17307c2344c3SMaor Gottlieb 		kfree(vma_private);
17317c2344c3SMaor Gottlieb 	}
1732ad9a3668SMajd Dibbiny 	mutex_unlock(&context->vma_private_list_mutex);
1733ecc7d83bSMaor Gottlieb 	up_write(&owning_mm->mmap_sem);
17347c2344c3SMaor Gottlieb 	mmput(owning_mm);
17357c2344c3SMaor Gottlieb 	put_task_struct(owning_process);
17367c2344c3SMaor Gottlieb }
17377c2344c3SMaor Gottlieb 
173837aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1739e126ba97SEli Cohen {
174037aa5c36SGuy Levi 	switch (cmd) {
174137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
174237aa5c36SGuy Levi 		return "WC";
1743e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
174437aa5c36SGuy Levi 		return "best effort WC";
174537aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
174637aa5c36SGuy Levi 		return "NC";
174737aa5c36SGuy Levi 	default:
174837aa5c36SGuy Levi 		return NULL;
174937aa5c36SGuy Levi 	}
175037aa5c36SGuy Levi }
175137aa5c36SGuy Levi 
175237aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
17537c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
17547c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
175537aa5c36SGuy Levi {
17562f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
175737aa5c36SGuy Levi 	int err;
175837aa5c36SGuy Levi 	unsigned long idx;
175937aa5c36SGuy Levi 	phys_addr_t pfn, pa;
176037aa5c36SGuy Levi 	pgprot_t prot;
1761b037c29aSEli Cohen 	int uars_per_page;
1762b037c29aSEli Cohen 
1763b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1764b037c29aSEli Cohen 		return -EINVAL;
1765b037c29aSEli Cohen 
1766b037c29aSEli Cohen 	uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1767b037c29aSEli Cohen 	idx = get_index(vma->vm_pgoff);
1768b037c29aSEli Cohen 	if (idx % uars_per_page ||
1769b037c29aSEli Cohen 	    idx * uars_per_page >= bfregi->num_sys_pages) {
1770b037c29aSEli Cohen 		mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1771b037c29aSEli Cohen 		return -EINVAL;
1772b037c29aSEli Cohen 	}
177337aa5c36SGuy Levi 
177437aa5c36SGuy Levi 	switch (cmd) {
177537aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
177637aa5c36SGuy Levi /* Some architectures don't support WC memory */
177737aa5c36SGuy Levi #if defined(CONFIG_X86)
177837aa5c36SGuy Levi 		if (!pat_enabled())
177937aa5c36SGuy Levi 			return -EPERM;
178037aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
178137aa5c36SGuy Levi 			return -EPERM;
178237aa5c36SGuy Levi #endif
178337aa5c36SGuy Levi 	/* fall through */
178437aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
178537aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
178637aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
178737aa5c36SGuy Levi 		break;
178837aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
178937aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
179037aa5c36SGuy Levi 		break;
179137aa5c36SGuy Levi 	default:
179237aa5c36SGuy Levi 		return -EINVAL;
179337aa5c36SGuy Levi 	}
179437aa5c36SGuy Levi 
1795b037c29aSEli Cohen 	pfn = uar_index2pfn(dev, bfregi, idx);
179637aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1797e126ba97SEli Cohen 
179837aa5c36SGuy Levi 	vma->vm_page_prot = prot;
179937aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
180037aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
180137aa5c36SGuy Levi 	if (err) {
180237aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
180337aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1804e126ba97SEli Cohen 		return -EAGAIN;
180537aa5c36SGuy Levi 	}
1806e126ba97SEli Cohen 
180737aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
180837aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
180937aa5c36SGuy Levi 		    vma->vm_start, &pa);
181037aa5c36SGuy Levi 
18117c2344c3SMaor Gottlieb 	return mlx5_ib_set_vma_data(vma, context);
181237aa5c36SGuy Levi }
181337aa5c36SGuy Levi 
181437aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
181537aa5c36SGuy Levi {
181637aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
181737aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
181837aa5c36SGuy Levi 	unsigned long command;
181937aa5c36SGuy Levi 	phys_addr_t pfn;
182037aa5c36SGuy Levi 
182137aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
182237aa5c36SGuy Levi 	switch (command) {
182337aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
182437aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
182537aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
18267c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
1827e126ba97SEli Cohen 
1828e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1829e126ba97SEli Cohen 		return -ENOSYS;
1830e126ba97SEli Cohen 
1831d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
1832d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1833d69e3bcfSMatan Barak 			return -EINVAL;
1834d69e3bcfSMatan Barak 
18356cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
1836d69e3bcfSMatan Barak 			return -EPERM;
1837d69e3bcfSMatan Barak 
1838d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
1839d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
1840d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
1841d69e3bcfSMatan Barak 
1842d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1843d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
1844d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1845d69e3bcfSMatan Barak 			PAGE_SHIFT;
1846d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1847d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
1848d69e3bcfSMatan Barak 			return -EAGAIN;
1849d69e3bcfSMatan Barak 
1850d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1851d69e3bcfSMatan Barak 			    vma->vm_start,
1852d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
1853d69e3bcfSMatan Barak 		break;
1854d69e3bcfSMatan Barak 
1855e126ba97SEli Cohen 	default:
1856e126ba97SEli Cohen 		return -EINVAL;
1857e126ba97SEli Cohen 	}
1858e126ba97SEli Cohen 
1859e126ba97SEli Cohen 	return 0;
1860e126ba97SEli Cohen }
1861e126ba97SEli Cohen 
1862e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1863e126ba97SEli Cohen 				      struct ib_ucontext *context,
1864e126ba97SEli Cohen 				      struct ib_udata *udata)
1865e126ba97SEli Cohen {
1866e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
1867e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1868e126ba97SEli Cohen 	int err;
1869e126ba97SEli Cohen 
1870e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1871e126ba97SEli Cohen 	if (!pd)
1872e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1873e126ba97SEli Cohen 
18749603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1875e126ba97SEli Cohen 	if (err) {
1876e126ba97SEli Cohen 		kfree(pd);
1877e126ba97SEli Cohen 		return ERR_PTR(err);
1878e126ba97SEli Cohen 	}
1879e126ba97SEli Cohen 
1880e126ba97SEli Cohen 	if (context) {
1881e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1882e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
18839603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1884e126ba97SEli Cohen 			kfree(pd);
1885e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1886e126ba97SEli Cohen 		}
1887e126ba97SEli Cohen 	}
1888e126ba97SEli Cohen 
1889e126ba97SEli Cohen 	return &pd->ibpd;
1890e126ba97SEli Cohen }
1891e126ba97SEli Cohen 
1892e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1893e126ba97SEli Cohen {
1894e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1895e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1896e126ba97SEli Cohen 
18979603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1898e126ba97SEli Cohen 	kfree(mpd);
1899e126ba97SEli Cohen 
1900e126ba97SEli Cohen 	return 0;
1901e126ba97SEli Cohen }
1902e126ba97SEli Cohen 
1903466fa6d2SMaor Gottlieb enum {
1904466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
1905466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
1906466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_INNER_BIT
1907466fa6d2SMaor Gottlieb };
1908038d2ef8SMaor Gottlieb 
1909466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
1910466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1911466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
1912466fa6d2SMaor Gottlieb 
1913466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
1914466fa6d2SMaor Gottlieb {
1915466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
1916466fa6d2SMaor Gottlieb 
1917466fa6d2SMaor Gottlieb 	match_criteria_enable =
1918466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1919466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
1920466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1921466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1922466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
1923466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1924466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1925466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
1926466fa6d2SMaor Gottlieb 
1927466fa6d2SMaor Gottlieb 	return match_criteria_enable;
1928038d2ef8SMaor Gottlieb }
1929038d2ef8SMaor Gottlieb 
1930ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1931ca0d4753SMaor Gottlieb {
1932ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1933ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1934ca0d4753SMaor Gottlieb }
1935ca0d4753SMaor Gottlieb 
19362d1e697eSMoses Reuben static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
19372d1e697eSMoses Reuben 			   bool inner)
19382d1e697eSMoses Reuben {
19392d1e697eSMoses Reuben 	if (inner) {
19402d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19412d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
19422d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19432d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
19442d1e697eSMoses Reuben 	} else {
19452d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19462d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
19472d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19482d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
19492d1e697eSMoses Reuben 	}
19502d1e697eSMoses Reuben }
19512d1e697eSMoses Reuben 
1952ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1953ca0d4753SMaor Gottlieb {
1954ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1955ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1956ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1957ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1958ca0d4753SMaor Gottlieb }
1959ca0d4753SMaor Gottlieb 
1960c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
1961c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
1962ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
1963466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
1964c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
1965ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
19662ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
1967a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
1968c47ac6aeSMaor Gottlieb 
1969c47ac6aeSMaor Gottlieb /* Field is the last supported field */
1970c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
1971c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
1972c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
1973c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
1974c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
1975c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
1976c47ac6aeSMaor Gottlieb 
197719cc7524SAriel Levkovich #define IPV4_VERSION 4
197819cc7524SAriel Levkovich #define IPV6_VERSION 6
197919cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
198019cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
1981a22ed86cSSlava Shwartsman 			   u32 *tag_id, bool *is_drop)
1982038d2ef8SMaor Gottlieb {
1983466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1984466fa6d2SMaor Gottlieb 					   misc_parameters);
1985466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1986466fa6d2SMaor Gottlieb 					   misc_parameters);
19872d1e697eSMoses Reuben 	void *headers_c;
19882d1e697eSMoses Reuben 	void *headers_v;
198919cc7524SAriel Levkovich 	int match_ipv;
1990466fa6d2SMaor Gottlieb 
19912d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
19922d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
19932d1e697eSMoses Reuben 					 inner_headers);
19942d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
19952d1e697eSMoses Reuben 					 inner_headers);
199619cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
199719cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
19982d1e697eSMoses Reuben 	} else {
19992d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
20002d1e697eSMoses Reuben 					 outer_headers);
20012d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
20022d1e697eSMoses Reuben 					 outer_headers);
200319cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
200419cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
20052d1e697eSMoses Reuben 	}
20062d1e697eSMoses Reuben 
20072d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
2008038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
2009c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
20101ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2011038d2ef8SMaor Gottlieb 
20122d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2013038d2ef8SMaor Gottlieb 					     dmac_47_16),
2014038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
20152d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2016038d2ef8SMaor Gottlieb 					     dmac_47_16),
2017038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2018038d2ef8SMaor Gottlieb 
20192d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2020ee3da804SMaor Gottlieb 					     smac_47_16),
2021ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
20222d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2023ee3da804SMaor Gottlieb 					     smac_47_16),
2024ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2025ee3da804SMaor Gottlieb 
2026038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
20272d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
202810543365SMohamad Haj Yahia 				 cvlan_tag, 1);
20292d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
203010543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2031038d2ef8SMaor Gottlieb 
20322d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2033038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
20342d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2035038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2036038d2ef8SMaor Gottlieb 
20372d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2038038d2ef8SMaor Gottlieb 				 first_cfi,
2039038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
20402d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2041038d2ef8SMaor Gottlieb 				 first_cfi,
2042038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2043038d2ef8SMaor Gottlieb 
20442d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2045038d2ef8SMaor Gottlieb 				 first_prio,
2046038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
20472d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2048038d2ef8SMaor Gottlieb 				 first_prio,
2049038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2050038d2ef8SMaor Gottlieb 		}
20512d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2052038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
20532d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2054038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2055038d2ef8SMaor Gottlieb 		break;
2056038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2057c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
20581ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2059038d2ef8SMaor Gottlieb 
206019cc7524SAriel Levkovich 		if (match_ipv) {
206119cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
206219cc7524SAriel Levkovich 				 ip_version, 0xf);
206319cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
206419cc7524SAriel Levkovich 				 ip_version, IPV4_VERSION);
206519cc7524SAriel Levkovich 		} else {
20662d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2067038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
20682d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2069038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
207019cc7524SAriel Levkovich 		}
2071038d2ef8SMaor Gottlieb 
20722d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2073038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2074038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2075038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
20762d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2077038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2078038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2079038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
20802d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2081038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2082038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2083038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
20842d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2085038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2086038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2087038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2088ca0d4753SMaor Gottlieb 
20892d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2090ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2091ca0d4753SMaor Gottlieb 
20922d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2093ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
2094038d2ef8SMaor Gottlieb 		break;
2095026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2096c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
20971ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2098026bae0cSMaor Gottlieb 
209919cc7524SAriel Levkovich 		if (match_ipv) {
210019cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
210119cc7524SAriel Levkovich 				 ip_version, 0xf);
210219cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
210319cc7524SAriel Levkovich 				 ip_version, IPV6_VERSION);
210419cc7524SAriel Levkovich 		} else {
21052d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2106026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
21072d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2108026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
210919cc7524SAriel Levkovich 		}
2110026bae0cSMaor Gottlieb 
21112d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2112026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2113026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2114026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
21152d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2116026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2117026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2118026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
21192d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2120026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2121026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2122026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
21232d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2124026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2125026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2126026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2127466fa6d2SMaor Gottlieb 
21282d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2129466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2130466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2131466fa6d2SMaor Gottlieb 
21322d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2133466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
2134466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
2135466fa6d2SMaor Gottlieb 
21362d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
21372d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
21382d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
21392d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
21402d1e697eSMoses Reuben 
2141026bae0cSMaor Gottlieb 		break;
2142038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2143c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2144c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
21451ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2146038d2ef8SMaor Gottlieb 
21472d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2148038d2ef8SMaor Gottlieb 			 0xff);
21492d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2150038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
2151038d2ef8SMaor Gottlieb 
21522d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2153038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
21542d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2155038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2156038d2ef8SMaor Gottlieb 
21572d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2158038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
21592d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2160038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2161038d2ef8SMaor Gottlieb 		break;
2162038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2163c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2164c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
21651ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2166038d2ef8SMaor Gottlieb 
21672d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2168038d2ef8SMaor Gottlieb 			 0xff);
21692d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2170038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
2171038d2ef8SMaor Gottlieb 
21722d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2173038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
21742d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2175038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2176038d2ef8SMaor Gottlieb 
21772d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2178038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
21792d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2180038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2181038d2ef8SMaor Gottlieb 		break;
2182ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2183ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2184ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
21851ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2186ffb30d8fSMoses Reuben 
2187ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2188ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2189ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2190ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2191ffb30d8fSMoses Reuben 		break;
21922ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
21932ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
21942ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
21952ac693f9SMoses Reuben 			return -EOPNOTSUPP;
21962ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
21972ac693f9SMoses Reuben 			return -EINVAL;
21982ac693f9SMoses Reuben 
21992ac693f9SMoses Reuben 		*tag_id = ib_spec->flow_tag.tag_id;
22002ac693f9SMoses Reuben 		break;
2201a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2202a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2203a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2204a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2205a22ed86cSSlava Shwartsman 		*is_drop = true;
2206a22ed86cSSlava Shwartsman 		break;
2207038d2ef8SMaor Gottlieb 	default:
2208038d2ef8SMaor Gottlieb 		return -EINVAL;
2209038d2ef8SMaor Gottlieb 	}
2210038d2ef8SMaor Gottlieb 
2211038d2ef8SMaor Gottlieb 	return 0;
2212038d2ef8SMaor Gottlieb }
2213038d2ef8SMaor Gottlieb 
2214038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
2215038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
2216038d2ef8SMaor Gottlieb  * could steal other multicast packets.
2217038d2ef8SMaor Gottlieb  */
2218a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2219038d2ef8SMaor Gottlieb {
222081e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
2221038d2ef8SMaor Gottlieb 
2222038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2223038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
2224038d2ef8SMaor Gottlieb 		return false;
2225038d2ef8SMaor Gottlieb 
222681e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
222781e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
222881e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
2229038d2ef8SMaor Gottlieb 
223081e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
223181e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
223281e30880SYishai Hadas 			return true;
223381e30880SYishai Hadas 
223481e30880SYishai Hadas 		return false;
223581e30880SYishai Hadas 	}
223681e30880SYishai Hadas 
223781e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
223881e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
223981e30880SYishai Hadas 
224081e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2241038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2242038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
2243038d2ef8SMaor Gottlieb 	}
2244038d2ef8SMaor Gottlieb 
224581e30880SYishai Hadas 	return false;
224681e30880SYishai Hadas }
224781e30880SYishai Hadas 
224819cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
224919cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
22500f750966SAriel Levkovich 			       bool check_inner)
2251038d2ef8SMaor Gottlieb {
2252038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
225319cc7524SAriel Levkovich 	int match_ipv = check_inner ?
225419cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
225519cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
225619cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
225719cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
22580f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
22590f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
22600f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
22610f750966SAriel Levkovich 	bool has_ethertype = false;
2262038d2ef8SMaor Gottlieb 	unsigned int spec_index;
22630f750966SAriel Levkovich 	bool mask_valid = true;
22640f750966SAriel Levkovich 	u16 eth_type = 0;
22650f750966SAriel Levkovich 	bool type_valid;
2266038d2ef8SMaor Gottlieb 
2267038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
2268038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
22690f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2270038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
22710f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
22720f750966SAriel Levkovich 				      htons(0xffff));
22730f750966SAriel Levkovich 			has_ethertype = true;
22740f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
22750f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
22760f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
22770f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
2278038d2ef8SMaor Gottlieb 		}
2279038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
2280038d2ef8SMaor Gottlieb 	}
22810f750966SAriel Levkovich 
22820f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
22830f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
22840f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
22850f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
22860f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
22870f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
228819cc7524SAriel Levkovich 
228919cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
229019cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
229119cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
22920f750966SAriel Levkovich 	}
22930f750966SAriel Levkovich 
22940f750966SAriel Levkovich 	return type_valid;
22950f750966SAriel Levkovich }
22960f750966SAriel Levkovich 
229719cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
229819cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
22990f750966SAriel Levkovich {
230019cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
230119cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
2302038d2ef8SMaor Gottlieb }
2303038d2ef8SMaor Gottlieb 
2304038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
2305038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
2306038d2ef8SMaor Gottlieb {
2307038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
2308038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
2309038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
2310038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
2311038d2ef8SMaor Gottlieb 	}
2312038d2ef8SMaor Gottlieb }
2313038d2ef8SMaor Gottlieb 
2314038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2315038d2ef8SMaor Gottlieb {
2316038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2317038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2318038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2319038d2ef8SMaor Gottlieb 							  ibflow);
2320038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2321038d2ef8SMaor Gottlieb 
2322038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2323038d2ef8SMaor Gottlieb 
2324038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
232574491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2326cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
2327038d2ef8SMaor Gottlieb 		list_del(&iter->list);
2328038d2ef8SMaor Gottlieb 		kfree(iter);
2329038d2ef8SMaor Gottlieb 	}
2330038d2ef8SMaor Gottlieb 
233174491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
23325497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
2333038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2334038d2ef8SMaor Gottlieb 
2335038d2ef8SMaor Gottlieb 	kfree(handler);
2336038d2ef8SMaor Gottlieb 
2337038d2ef8SMaor Gottlieb 	return 0;
2338038d2ef8SMaor Gottlieb }
2339038d2ef8SMaor Gottlieb 
234035d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
234135d19011SMaor Gottlieb {
234235d19011SMaor Gottlieb 	priority *= 2;
234335d19011SMaor Gottlieb 	if (!dont_trap)
234435d19011SMaor Gottlieb 		priority++;
234535d19011SMaor Gottlieb 	return priority;
234635d19011SMaor Gottlieb }
234735d19011SMaor Gottlieb 
2348cc0e5d42SMaor Gottlieb enum flow_table_type {
2349cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
2350cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
2351cc0e5d42SMaor Gottlieb };
2352cc0e5d42SMaor Gottlieb 
235300b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
235400b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
2355038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
2356cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
2357cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
2358038d2ef8SMaor Gottlieb {
235935d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
2360038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
2361038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
2362038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
2363dac388efSMaor Gottlieb 	int max_table_size;
2364038d2ef8SMaor Gottlieb 	int num_entries;
2365038d2ef8SMaor Gottlieb 	int num_groups;
2366038d2ef8SMaor Gottlieb 	int priority;
2367038d2ef8SMaor Gottlieb 	int err = 0;
2368038d2ef8SMaor Gottlieb 
2369dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2370dac388efSMaor Gottlieb 						       log_max_ft_size));
2371038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
237235d19011SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr) &&
237335d19011SMaor Gottlieb 		    !dont_trap)
2374038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
2375038d2ef8SMaor Gottlieb 		else
237635d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
237735d19011SMaor Gottlieb 							dont_trap);
2378038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2379038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
2380038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
2381038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
2382038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[priority];
2383038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2384038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2385038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2386038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
2387038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
2388038d2ef8SMaor Gottlieb 					 &num_entries,
2389038d2ef8SMaor Gottlieb 					 &num_groups);
2390038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
2391cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2392cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2393cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
2394cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
2395cc0e5d42SMaor Gottlieb 
2396cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2397cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2398cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2399cc0e5d42SMaor Gottlieb 
2400cc0e5d42SMaor Gottlieb 		prio = &dev->flow_db.sniffer[ft_type];
2401cc0e5d42SMaor Gottlieb 		priority = 0;
2402cc0e5d42SMaor Gottlieb 		num_entries = 1;
2403cc0e5d42SMaor Gottlieb 		num_groups = 1;
2404038d2ef8SMaor Gottlieb 	}
2405038d2ef8SMaor Gottlieb 
2406038d2ef8SMaor Gottlieb 	if (!ns)
2407038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
2408038d2ef8SMaor Gottlieb 
2409dac388efSMaor Gottlieb 	if (num_entries > max_table_size)
2410dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2411dac388efSMaor Gottlieb 
2412038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
2413038d2ef8SMaor Gottlieb 	if (!ft) {
2414038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2415038d2ef8SMaor Gottlieb 							 num_entries,
2416d63cd286SMaor Gottlieb 							 num_groups,
2417c9f1b073SHadar Hen Zion 							 0, 0);
2418038d2ef8SMaor Gottlieb 
2419038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
2420038d2ef8SMaor Gottlieb 			prio->refcount = 0;
2421038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
2422038d2ef8SMaor Gottlieb 		} else {
2423038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
2424038d2ef8SMaor Gottlieb 		}
2425038d2ef8SMaor Gottlieb 	}
2426038d2ef8SMaor Gottlieb 
2427038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
2428038d2ef8SMaor Gottlieb }
2429038d2ef8SMaor Gottlieb 
2430a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
2431a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
2432a550ddfcSYishai Hadas 			    u32 underlay_qpn)
2433a550ddfcSYishai Hadas {
2434a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2435a550ddfcSYishai Hadas 					   spec->match_criteria,
2436a550ddfcSYishai Hadas 					   misc_parameters);
2437a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2438a550ddfcSYishai Hadas 					   misc_parameters);
2439a550ddfcSYishai Hadas 
2440a550ddfcSYishai Hadas 	if (underlay_qpn &&
2441a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2442a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
2443a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
2444a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
2445a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
2446a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
2447a550ddfcSYishai Hadas 	}
2448a550ddfcSYishai Hadas }
2449a550ddfcSYishai Hadas 
2450a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2451038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
2452dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
2453a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
2454a550ddfcSYishai Hadas 						      u32 underlay_qpn)
2455038d2ef8SMaor Gottlieb {
2456038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
2457038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
245866958ed9SHadar Hen Zion 	struct mlx5_flow_act flow_act = {0};
2459c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
2460a22ed86cSSlava Shwartsman 	struct mlx5_flow_destination *rule_dst = dst;
2461dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
2462038d2ef8SMaor Gottlieb 	unsigned int spec_index;
24632ac693f9SMoses Reuben 	u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
2464a22ed86cSSlava Shwartsman 	bool is_drop = false;
2465038d2ef8SMaor Gottlieb 	int err = 0;
2466a22ed86cSSlava Shwartsman 	int dest_num = 1;
2467038d2ef8SMaor Gottlieb 
246819cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
2469038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2470038d2ef8SMaor Gottlieb 
24711b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
2472038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
2473c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
2474038d2ef8SMaor Gottlieb 		err = -ENOMEM;
2475038d2ef8SMaor Gottlieb 		goto free;
2476038d2ef8SMaor Gottlieb 	}
2477038d2ef8SMaor Gottlieb 
2478038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
2479038d2ef8SMaor Gottlieb 
2480038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
248119cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
2482a22ed86cSSlava Shwartsman 				      spec->match_value,
2483a22ed86cSSlava Shwartsman 				      ib_flow, &flow_tag, &is_drop);
2484038d2ef8SMaor Gottlieb 		if (err < 0)
2485038d2ef8SMaor Gottlieb 			goto free;
2486038d2ef8SMaor Gottlieb 
2487038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2488038d2ef8SMaor Gottlieb 	}
2489038d2ef8SMaor Gottlieb 
2490a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
2491a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
2492a550ddfcSYishai Hadas 
2493466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
2494a22ed86cSSlava Shwartsman 	if (is_drop) {
2495a22ed86cSSlava Shwartsman 		flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2496a22ed86cSSlava Shwartsman 		rule_dst = NULL;
2497a22ed86cSSlava Shwartsman 		dest_num = 0;
2498a22ed86cSSlava Shwartsman 	} else {
249966958ed9SHadar Hen Zion 		flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
250035d19011SMaor Gottlieb 		    MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2501a22ed86cSSlava Shwartsman 	}
25022ac693f9SMoses Reuben 
25032ac693f9SMoses Reuben 	if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
25042ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
25052ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
25062ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
25072ac693f9SMoses Reuben 			     flow_tag, flow_attr->type);
25082ac693f9SMoses Reuben 		err = -EINVAL;
25092ac693f9SMoses Reuben 		goto free;
25102ac693f9SMoses Reuben 	}
25112ac693f9SMoses Reuben 	flow_act.flow_tag = flow_tag;
251274491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
251366958ed9SHadar Hen Zion 					    &flow_act,
2514a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
2515038d2ef8SMaor Gottlieb 
2516038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
2517038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
2518038d2ef8SMaor Gottlieb 		goto free;
2519038d2ef8SMaor Gottlieb 	}
2520038d2ef8SMaor Gottlieb 
2521d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
25225497adc6SMaor Gottlieb 	handler->prio = ft_prio;
2523038d2ef8SMaor Gottlieb 
2524038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
2525038d2ef8SMaor Gottlieb free:
2526038d2ef8SMaor Gottlieb 	if (err)
2527038d2ef8SMaor Gottlieb 		kfree(handler);
2528c5bb1730SMaor Gottlieb 	kvfree(spec);
2529038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
2530038d2ef8SMaor Gottlieb }
2531038d2ef8SMaor Gottlieb 
2532a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2533a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
2534a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
2535a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
2536a550ddfcSYishai Hadas {
2537a550ddfcSYishai Hadas 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2538a550ddfcSYishai Hadas }
2539a550ddfcSYishai Hadas 
254035d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
254135d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
254235d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
254335d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
254435d19011SMaor Gottlieb {
254535d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
254635d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
254735d19011SMaor Gottlieb 
254835d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
254935d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
255035d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
255135d19011SMaor Gottlieb 					       flow_attr, dst);
255235d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
255374491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2554d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
255535d19011SMaor Gottlieb 			kfree(handler);
255635d19011SMaor Gottlieb 			handler = handler_dst;
255735d19011SMaor Gottlieb 		} else {
255835d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
255935d19011SMaor Gottlieb 		}
256035d19011SMaor Gottlieb 	}
256135d19011SMaor Gottlieb 
256235d19011SMaor Gottlieb 	return handler;
256335d19011SMaor Gottlieb }
2564038d2ef8SMaor Gottlieb enum {
2565038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
2566038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
2567038d2ef8SMaor Gottlieb };
2568038d2ef8SMaor Gottlieb 
2569038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2570038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
2571038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
2572038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
2573038d2ef8SMaor Gottlieb {
2574038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
2575038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2576038d2ef8SMaor Gottlieb 
2577038d2ef8SMaor Gottlieb 	static struct {
2578038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
2579038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
2580038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
2581038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
2582038d2ef8SMaor Gottlieb 			.flow_attr = {
2583038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2584038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2585038d2ef8SMaor Gottlieb 			},
2586038d2ef8SMaor Gottlieb 			.eth_flow = {
2587038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2588038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2589038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2590038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
2591038d2ef8SMaor Gottlieb 			}
2592038d2ef8SMaor Gottlieb 		},
2593038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
2594038d2ef8SMaor Gottlieb 			.flow_attr = {
2595038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2596038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2597038d2ef8SMaor Gottlieb 			},
2598038d2ef8SMaor Gottlieb 			.eth_flow = {
2599038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2600038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2601038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2602038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
2603038d2ef8SMaor Gottlieb 			}
2604038d2ef8SMaor Gottlieb 		}
2605038d2ef8SMaor Gottlieb 	};
2606038d2ef8SMaor Gottlieb 
2607038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
2608038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
2609038d2ef8SMaor Gottlieb 				   dst);
2610038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
2611038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2612038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
2613038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2614038d2ef8SMaor Gottlieb 						 dst);
2615038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
261674491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2617d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
2618038d2ef8SMaor Gottlieb 			kfree(handler);
2619038d2ef8SMaor Gottlieb 			handler = handler_ucast;
2620038d2ef8SMaor Gottlieb 		} else {
2621038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
2622038d2ef8SMaor Gottlieb 		}
2623038d2ef8SMaor Gottlieb 	}
2624038d2ef8SMaor Gottlieb 
2625038d2ef8SMaor Gottlieb 	return handler;
2626038d2ef8SMaor Gottlieb }
2627038d2ef8SMaor Gottlieb 
2628cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2629cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
2630cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
2631cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
2632cc0e5d42SMaor Gottlieb {
2633cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
2634cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
2635cc0e5d42SMaor Gottlieb 	int err;
2636cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
2637cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
2638cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
2639cc0e5d42SMaor Gottlieb 	};
2640cc0e5d42SMaor Gottlieb 
2641cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2642cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
2643cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
2644cc0e5d42SMaor Gottlieb 		goto err;
2645cc0e5d42SMaor Gottlieb 	}
2646cc0e5d42SMaor Gottlieb 
2647cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2648cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
2649cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
2650cc0e5d42SMaor Gottlieb 		goto err_tx;
2651cc0e5d42SMaor Gottlieb 	}
2652cc0e5d42SMaor Gottlieb 
2653cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
2654cc0e5d42SMaor Gottlieb 
2655cc0e5d42SMaor Gottlieb 	return handler_rx;
2656cc0e5d42SMaor Gottlieb 
2657cc0e5d42SMaor Gottlieb err_tx:
265874491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
2659cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
2660cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
2661cc0e5d42SMaor Gottlieb err:
2662cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
2663cc0e5d42SMaor Gottlieb }
2664cc0e5d42SMaor Gottlieb 
2665038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2666038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
2667038d2ef8SMaor Gottlieb 					   int domain)
2668038d2ef8SMaor Gottlieb {
2669038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
2670d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
2671038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2672038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
2673cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
2674038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
2675038d2ef8SMaor Gottlieb 	int err;
2676a550ddfcSYishai Hadas 	int underlay_qpn;
2677038d2ef8SMaor Gottlieb 
2678038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
2679dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2680038d2ef8SMaor Gottlieb 
2681038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
2682038d2ef8SMaor Gottlieb 	    flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
268335d19011SMaor Gottlieb 	    (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
2684038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2685038d2ef8SMaor Gottlieb 
2686038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2687038d2ef8SMaor Gottlieb 	if (!dst)
2688038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2689038d2ef8SMaor Gottlieb 
2690038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2691038d2ef8SMaor Gottlieb 
2692cc0e5d42SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
2693038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
2694038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
2695038d2ef8SMaor Gottlieb 		goto unlock;
2696038d2ef8SMaor Gottlieb 	}
2697cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2698cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2699cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
2700cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
2701cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
2702cc0e5d42SMaor Gottlieb 			goto destroy_ft;
2703cc0e5d42SMaor Gottlieb 		}
2704cc0e5d42SMaor Gottlieb 	}
2705038d2ef8SMaor Gottlieb 
2706038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
2707d9f88e5aSYishai Hadas 	if (mqp->flags & MLX5_IB_QP_RSS)
2708d9f88e5aSYishai Hadas 		dst->tir_num = mqp->rss_qp.tirn;
2709d9f88e5aSYishai Hadas 	else
2710d9f88e5aSYishai Hadas 		dst->tir_num = mqp->raw_packet_qp.rq.tirn;
2711038d2ef8SMaor Gottlieb 
2712038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
271335d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
271435d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
271535d19011SMaor Gottlieb 							flow_attr, dst);
271635d19011SMaor Gottlieb 		} else {
2717a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
2718a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
2719a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
2720a550ddfcSYishai Hadas 						    dst, underlay_qpn);
272135d19011SMaor Gottlieb 		}
2722038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2723038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2724038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2725038d2ef8SMaor Gottlieb 						dst);
2726cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2727cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
2728038d2ef8SMaor Gottlieb 	} else {
2729038d2ef8SMaor Gottlieb 		err = -EINVAL;
2730038d2ef8SMaor Gottlieb 		goto destroy_ft;
2731038d2ef8SMaor Gottlieb 	}
2732038d2ef8SMaor Gottlieb 
2733038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
2734038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
2735038d2ef8SMaor Gottlieb 		handler = NULL;
2736038d2ef8SMaor Gottlieb 		goto destroy_ft;
2737038d2ef8SMaor Gottlieb 	}
2738038d2ef8SMaor Gottlieb 
2739038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2740038d2ef8SMaor Gottlieb 	kfree(dst);
2741038d2ef8SMaor Gottlieb 
2742038d2ef8SMaor Gottlieb 	return &handler->ibflow;
2743038d2ef8SMaor Gottlieb 
2744038d2ef8SMaor Gottlieb destroy_ft:
2745038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
2746cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
2747cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
2748038d2ef8SMaor Gottlieb unlock:
2749038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2750038d2ef8SMaor Gottlieb 	kfree(dst);
2751038d2ef8SMaor Gottlieb 	kfree(handler);
2752038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
2753038d2ef8SMaor Gottlieb }
2754038d2ef8SMaor Gottlieb 
2755e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2756e126ba97SEli Cohen {
2757e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
275881e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
2759e126ba97SEli Cohen 	int err;
2760e126ba97SEli Cohen 
276181e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
276281e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
276381e30880SYishai Hadas 		return -EOPNOTSUPP;
276481e30880SYishai Hadas 	}
276581e30880SYishai Hadas 
27669603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
2767e126ba97SEli Cohen 	if (err)
2768e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2769e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2770e126ba97SEli Cohen 
2771e126ba97SEli Cohen 	return err;
2772e126ba97SEli Cohen }
2773e126ba97SEli Cohen 
2774e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2775e126ba97SEli Cohen {
2776e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2777e126ba97SEli Cohen 	int err;
2778e126ba97SEli Cohen 
27799603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
2780e126ba97SEli Cohen 	if (err)
2781e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2782e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2783e126ba97SEli Cohen 
2784e126ba97SEli Cohen 	return err;
2785e126ba97SEli Cohen }
2786e126ba97SEli Cohen 
2787e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2788e126ba97SEli Cohen {
27891b5daf11SMajd Dibbiny 	int err;
2790e126ba97SEli Cohen 
27911b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2792e126ba97SEli Cohen 	if (err)
2793e126ba97SEli Cohen 		return err;
27941b5daf11SMajd Dibbiny 
27951b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
27961b5daf11SMajd Dibbiny 
27971b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2798e126ba97SEli Cohen }
2799e126ba97SEli Cohen 
2800e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2801e126ba97SEli Cohen 			     char *buf)
2802e126ba97SEli Cohen {
2803e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2804e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2805e126ba97SEli Cohen 
28069603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
2807e126ba97SEli Cohen }
2808e126ba97SEli Cohen 
2809e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
2810e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2811e126ba97SEli Cohen {
2812e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2813e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2814e126ba97SEli Cohen 
28156aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2816e126ba97SEli Cohen }
2817e126ba97SEli Cohen 
2818e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2819e126ba97SEli Cohen 			char *buf)
2820e126ba97SEli Cohen {
2821e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2822e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
28239603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
2824e126ba97SEli Cohen }
2825e126ba97SEli Cohen 
2826e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2827e126ba97SEli Cohen 			char *buf)
2828e126ba97SEli Cohen {
2829e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2830e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
28319603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
2832e126ba97SEli Cohen }
2833e126ba97SEli Cohen 
2834e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
2835e126ba97SEli Cohen 			  char *buf)
2836e126ba97SEli Cohen {
2837e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2838e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2839e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
28409603b61dSJack Morgenstein 		       dev->mdev->board_id);
2841e126ba97SEli Cohen }
2842e126ba97SEli Cohen 
2843e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
2844e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
2845e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
2846e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2847e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2848e126ba97SEli Cohen 
2849e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
2850e126ba97SEli Cohen 	&dev_attr_hw_rev,
2851e126ba97SEli Cohen 	&dev_attr_hca_type,
2852e126ba97SEli Cohen 	&dev_attr_board_id,
2853e126ba97SEli Cohen 	&dev_attr_fw_pages,
2854e126ba97SEli Cohen 	&dev_attr_reg_pages,
2855e126ba97SEli Cohen };
2856e126ba97SEli Cohen 
28577722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
28587722f47eSHaggai Eran {
28597722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
28607722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
28617722f47eSHaggai Eran 			     pkey_change_work);
28627722f47eSHaggai Eran 
28637722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
28647722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
28657722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
28667722f47eSHaggai Eran }
28677722f47eSHaggai Eran 
286889ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
286989ea94a7SMaor Gottlieb {
287089ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
287189ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
287289ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
287389ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
287489ea94a7SMaor Gottlieb 	unsigned long flags_qp;
287589ea94a7SMaor Gottlieb 	unsigned long flags_cq;
287689ea94a7SMaor Gottlieb 	unsigned long flags;
287789ea94a7SMaor Gottlieb 
287889ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
287989ea94a7SMaor Gottlieb 
288089ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
288189ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
288289ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
288389ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
288489ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
288589ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
288689ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
288789ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
288889ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
288989ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
289089ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
289189ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
289289ea94a7SMaor Gottlieb 						      &cq_armed_list);
289389ea94a7SMaor Gottlieb 				}
289489ea94a7SMaor Gottlieb 			}
289589ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
289689ea94a7SMaor Gottlieb 		}
289789ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
289889ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
289989ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
290089ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
290189ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
290289ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
290389ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
290489ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
290589ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
290689ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
290789ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
290889ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
290989ea94a7SMaor Gottlieb 							      &cq_armed_list);
291089ea94a7SMaor Gottlieb 					}
291189ea94a7SMaor Gottlieb 				}
291289ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
291389ea94a7SMaor Gottlieb 						       flags_cq);
291489ea94a7SMaor Gottlieb 			}
291589ea94a7SMaor Gottlieb 		}
291689ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
291789ea94a7SMaor Gottlieb 	}
291889ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
291989ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
292089ea94a7SMaor Gottlieb 	 */
292189ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
292289ea94a7SMaor Gottlieb 		mcq->comp(mcq);
292389ea94a7SMaor Gottlieb 	}
292489ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
292589ea94a7SMaor Gottlieb }
292689ea94a7SMaor Gottlieb 
292703404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
292803404e8aSMaor Gottlieb {
292903404e8aSMaor Gottlieb 	int err;
293003404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
293103404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
293203404e8aSMaor Gottlieb 			     delay_drop_work);
293303404e8aSMaor Gottlieb 
2934fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
2935fe248c3aSMaor Gottlieb 
293603404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
293703404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
293803404e8aSMaor Gottlieb 				       delay_drop->timeout);
293903404e8aSMaor Gottlieb 	if (err) {
294003404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
294103404e8aSMaor Gottlieb 			     delay_drop->timeout);
294203404e8aSMaor Gottlieb 		delay_drop->activate = false;
294303404e8aSMaor Gottlieb 	}
294403404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
294503404e8aSMaor Gottlieb }
294603404e8aSMaor Gottlieb 
29479603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
29484d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
2949e126ba97SEli Cohen {
29509603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
2951e126ba97SEli Cohen 	struct ib_event ibev;
2952dbaaff2aSEli Cohen 	bool fatal = false;
2953e126ba97SEli Cohen 	u8 port = 0;
2954e126ba97SEli Cohen 
2955e126ba97SEli Cohen 	switch (event) {
2956e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2957e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
295889ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2959dbaaff2aSEli Cohen 		fatal = true;
2960e126ba97SEli Cohen 		break;
2961e126ba97SEli Cohen 
2962e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
2963e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
29642788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
29654d2f9bbbSJack Morgenstein 		port = (u8)param;
29665ec8c83eSAviv Heller 
29675ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
29685ec8c83eSAviv Heller 		 * mlx5_netdev_event().
29695ec8c83eSAviv Heller 		 */
29705ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
29715ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
29725ec8c83eSAviv Heller 			return;
29735ec8c83eSAviv Heller 
29745ec8c83eSAviv Heller 		ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
29755ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2976e126ba97SEli Cohen 		break;
2977e126ba97SEli Cohen 
2978e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
2979e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
29804d2f9bbbSJack Morgenstein 		port = (u8)param;
2981e126ba97SEli Cohen 		break;
2982e126ba97SEli Cohen 
2983e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
2984e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
29854d2f9bbbSJack Morgenstein 		port = (u8)param;
29867722f47eSHaggai Eran 
29877722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2988e126ba97SEli Cohen 		break;
2989e126ba97SEli Cohen 
2990e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
2991e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
29924d2f9bbbSJack Morgenstein 		port = (u8)param;
2993e126ba97SEli Cohen 		break;
2994e126ba97SEli Cohen 
2995e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
2996e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
29974d2f9bbbSJack Morgenstein 		port = (u8)param;
2998e126ba97SEli Cohen 		break;
299903404e8aSMaor Gottlieb 	case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
300003404e8aSMaor Gottlieb 		schedule_work(&ibdev->delay_drop.delay_drop_work);
300103404e8aSMaor Gottlieb 		goto out;
3002bdc37924SSaeed Mahameed 	default:
300303404e8aSMaor Gottlieb 		goto out;
3004e126ba97SEli Cohen 	}
3005e126ba97SEli Cohen 
3006e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
3007e126ba97SEli Cohen 	ibev.element.port_num = port;
3008e126ba97SEli Cohen 
3009a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
3010a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
301103404e8aSMaor Gottlieb 		goto out;
3012a0c84c32SEli Cohen 	}
3013a0c84c32SEli Cohen 
3014e126ba97SEli Cohen 	if (ibdev->ib_active)
3015e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
3016dbaaff2aSEli Cohen 
3017dbaaff2aSEli Cohen 	if (fatal)
3018dbaaff2aSEli Cohen 		ibdev->ib_active = false;
301903404e8aSMaor Gottlieb 
302003404e8aSMaor Gottlieb out:
302103404e8aSMaor Gottlieb 	return;
3022e126ba97SEli Cohen }
3023e126ba97SEli Cohen 
3024c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
3025c43f1112SMaor Gottlieb {
3026c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
3027c43f1112SMaor Gottlieb 	int err;
3028c43f1112SMaor Gottlieb 	int port;
3029c43f1112SMaor Gottlieb 
3030c43f1112SMaor Gottlieb 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
3031c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
3032c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3033c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
3034c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3035c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
3036c43f1112SMaor Gottlieb 								   port, 0,
3037c43f1112SMaor Gottlieb 								   &vport_ctx);
3038c43f1112SMaor Gottlieb 				if (err) {
3039c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3040c43f1112SMaor Gottlieb 						    port, err);
3041c43f1112SMaor Gottlieb 					return err;
3042c43f1112SMaor Gottlieb 				}
3043c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
3044c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
3045c43f1112SMaor Gottlieb 			} else {
3046c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
3047c43f1112SMaor Gottlieb 			}
3048c43f1112SMaor Gottlieb 		}
3049c43f1112SMaor Gottlieb 	}
3050c43f1112SMaor Gottlieb 	return 0;
3051c43f1112SMaor Gottlieb }
3052c43f1112SMaor Gottlieb 
3053e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3054e126ba97SEli Cohen {
3055e126ba97SEli Cohen 	int port;
3056e126ba97SEli Cohen 
3057938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
3058e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
3059e126ba97SEli Cohen }
3060e126ba97SEli Cohen 
3061e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
3062e126ba97SEli Cohen {
3063e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
3064e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
3065f614fc15SDan Carpenter 	int err = -ENOMEM;
3066e126ba97SEli Cohen 	int port;
30672528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
3068e126ba97SEli Cohen 
3069e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3070e126ba97SEli Cohen 	if (!pprops)
3071e126ba97SEli Cohen 		goto out;
3072e126ba97SEli Cohen 
3073e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3074e126ba97SEli Cohen 	if (!dprops)
3075e126ba97SEli Cohen 		goto out;
3076e126ba97SEli Cohen 
3077c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
3078c43f1112SMaor Gottlieb 	if (err)
3079c43f1112SMaor Gottlieb 		goto out;
3080c43f1112SMaor Gottlieb 
30812528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
3082e126ba97SEli Cohen 	if (err) {
3083e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
3084e126ba97SEli Cohen 		goto out;
3085e126ba97SEli Cohen 	}
3086e126ba97SEli Cohen 
3087938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
3088c4550c63SOr Gerlitz 		memset(pprops, 0, sizeof(*pprops));
3089e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3090e126ba97SEli Cohen 		if (err) {
3091938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
3092938fe83cSSaeed Mahameed 				     port, err);
3093e126ba97SEli Cohen 			break;
3094e126ba97SEli Cohen 		}
3095938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
3096938fe83cSSaeed Mahameed 						dprops->max_pkeys;
3097938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
3098938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
3099e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
3100e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
3101e126ba97SEli Cohen 	}
3102e126ba97SEli Cohen 
3103e126ba97SEli Cohen out:
3104e126ba97SEli Cohen 	kfree(pprops);
3105e126ba97SEli Cohen 	kfree(dprops);
3106e126ba97SEli Cohen 
3107e126ba97SEli Cohen 	return err;
3108e126ba97SEli Cohen }
3109e126ba97SEli Cohen 
3110e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3111e126ba97SEli Cohen {
3112e126ba97SEli Cohen 	int err;
3113e126ba97SEli Cohen 
3114e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
3115e126ba97SEli Cohen 	if (err)
3116e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3117e126ba97SEli Cohen 
3118e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
3119add08d76SChristoph Hellwig 	ib_free_cq(dev->umrc.cq);
3120e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
3121e126ba97SEli Cohen }
3122e126ba97SEli Cohen 
3123e126ba97SEli Cohen enum {
3124e126ba97SEli Cohen 	MAX_UMR_WR = 128,
3125e126ba97SEli Cohen };
3126e126ba97SEli Cohen 
3127e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
3128e126ba97SEli Cohen {
3129e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
3130e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
3131e126ba97SEli Cohen 	struct ib_pd *pd;
3132e126ba97SEli Cohen 	struct ib_cq *cq;
3133e126ba97SEli Cohen 	struct ib_qp *qp;
3134e126ba97SEli Cohen 	int ret;
3135e126ba97SEli Cohen 
3136e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3137e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3138e126ba97SEli Cohen 	if (!attr || !init_attr) {
3139e126ba97SEli Cohen 		ret = -ENOMEM;
3140e126ba97SEli Cohen 		goto error_0;
3141e126ba97SEli Cohen 	}
3142e126ba97SEli Cohen 
3143ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
3144e126ba97SEli Cohen 	if (IS_ERR(pd)) {
3145e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3146e126ba97SEli Cohen 		ret = PTR_ERR(pd);
3147e126ba97SEli Cohen 		goto error_0;
3148e126ba97SEli Cohen 	}
3149e126ba97SEli Cohen 
3150add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
3151e126ba97SEli Cohen 	if (IS_ERR(cq)) {
3152e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3153e126ba97SEli Cohen 		ret = PTR_ERR(cq);
3154e126ba97SEli Cohen 		goto error_2;
3155e126ba97SEli Cohen 	}
3156e126ba97SEli Cohen 
3157e126ba97SEli Cohen 	init_attr->send_cq = cq;
3158e126ba97SEli Cohen 	init_attr->recv_cq = cq;
3159e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3160e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
3161e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
3162e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3163e126ba97SEli Cohen 	init_attr->port_num = 1;
3164e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3165e126ba97SEli Cohen 	if (IS_ERR(qp)) {
3166e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3167e126ba97SEli Cohen 		ret = PTR_ERR(qp);
3168e126ba97SEli Cohen 		goto error_3;
3169e126ba97SEli Cohen 	}
3170e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
3171e126ba97SEli Cohen 	qp->real_qp    = qp;
3172e126ba97SEli Cohen 	qp->uobject    = NULL;
3173e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
317431fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
317531fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
3176e126ba97SEli Cohen 
3177e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
3178e126ba97SEli Cohen 	attr->port_num = 1;
3179e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3180e126ba97SEli Cohen 				IB_QP_PORT, NULL);
3181e126ba97SEli Cohen 	if (ret) {
3182e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3183e126ba97SEli Cohen 		goto error_4;
3184e126ba97SEli Cohen 	}
3185e126ba97SEli Cohen 
3186e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3187e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
3188e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
3189e126ba97SEli Cohen 
3190e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3191e126ba97SEli Cohen 	if (ret) {
3192e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3193e126ba97SEli Cohen 		goto error_4;
3194e126ba97SEli Cohen 	}
3195e126ba97SEli Cohen 
3196e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3197e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
3198e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3199e126ba97SEli Cohen 	if (ret) {
3200e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3201e126ba97SEli Cohen 		goto error_4;
3202e126ba97SEli Cohen 	}
3203e126ba97SEli Cohen 
3204e126ba97SEli Cohen 	dev->umrc.qp = qp;
3205e126ba97SEli Cohen 	dev->umrc.cq = cq;
3206e126ba97SEli Cohen 	dev->umrc.pd = pd;
3207e126ba97SEli Cohen 
3208e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
3209e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
3210e126ba97SEli Cohen 	if (ret) {
3211e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3212e126ba97SEli Cohen 		goto error_4;
3213e126ba97SEli Cohen 	}
3214e126ba97SEli Cohen 
3215e126ba97SEli Cohen 	kfree(attr);
3216e126ba97SEli Cohen 	kfree(init_attr);
3217e126ba97SEli Cohen 
3218e126ba97SEli Cohen 	return 0;
3219e126ba97SEli Cohen 
3220e126ba97SEli Cohen error_4:
3221e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
3222e126ba97SEli Cohen 
3223e126ba97SEli Cohen error_3:
3224add08d76SChristoph Hellwig 	ib_free_cq(cq);
3225e126ba97SEli Cohen 
3226e126ba97SEli Cohen error_2:
3227e126ba97SEli Cohen 	ib_dealloc_pd(pd);
3228e126ba97SEli Cohen 
3229e126ba97SEli Cohen error_0:
3230e126ba97SEli Cohen 	kfree(attr);
3231e126ba97SEli Cohen 	kfree(init_attr);
3232e126ba97SEli Cohen 	return ret;
3233e126ba97SEli Cohen }
3234e126ba97SEli Cohen 
32356e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
32366e8484c5SMax Gurtovoy {
32376e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
32386e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
32396e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
32406e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
32416e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
32426e8484c5SMax Gurtovoy 	default:
32436e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
32446e8484c5SMax Gurtovoy 	}
32456e8484c5SMax Gurtovoy }
32466e8484c5SMax Gurtovoy 
3247e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
3248e126ba97SEli Cohen {
3249e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
3250e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3251bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
32527722f47eSHaggai Eran 	int port;
3253e126ba97SEli Cohen 	int ret = 0;
3254e126ba97SEli Cohen 
3255e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
3256e126ba97SEli Cohen 
3257d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
3258d16e91daSHaggai Eran 
3259e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3260e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
3261e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
3262e126ba97SEli Cohen 		goto error0;
3263e126ba97SEli Cohen 	}
3264e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
3265e126ba97SEli Cohen 	devr->p0->uobject = NULL;
3266e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
3267e126ba97SEli Cohen 
3268bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
3269e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
3270e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
3271e126ba97SEli Cohen 		goto error1;
3272e126ba97SEli Cohen 	}
3273e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
3274e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
3275e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
3276e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
3277e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
3278e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
3279e126ba97SEli Cohen 
3280e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3281e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
3282e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
3283e126ba97SEli Cohen 		goto error2;
3284e126ba97SEli Cohen 	}
3285e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
3286e126ba97SEli Cohen 	devr->x0->inode = NULL;
3287e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
3288e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
3289e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3290e126ba97SEli Cohen 
3291e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3292e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
3293e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
3294e126ba97SEli Cohen 		goto error3;
3295e126ba97SEli Cohen 	}
3296e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
3297e126ba97SEli Cohen 	devr->x1->inode = NULL;
3298e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
3299e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
3300e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3301e126ba97SEli Cohen 
3302e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
3303e126ba97SEli Cohen 	attr.attr.max_sge = 1;
3304e126ba97SEli Cohen 	attr.attr.max_wr = 1;
3305e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
33061a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
3307e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
3308e126ba97SEli Cohen 
3309e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3310e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
3311e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
3312e126ba97SEli Cohen 		goto error4;
3313e126ba97SEli Cohen 	}
3314e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
3315e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
3316e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
3317e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
3318e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
3319e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
3320e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
33211a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
3322e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
33231a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
3324e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
3325e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
3326e126ba97SEli Cohen 
33274aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
33284aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
33294aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
33304aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
33314aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
33324aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
33334aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
33344aa17b28SHaggai Abramonvsky 		goto error5;
33354aa17b28SHaggai Abramonvsky 	}
33364aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
33374aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
33384aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
33394aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
33404aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
33414aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
33421a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
33434aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
33441a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
33454aa17b28SHaggai Abramonvsky 
33467722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
33477722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
33487722f47eSHaggai Eran 			  pkey_change_handler);
33497722f47eSHaggai Eran 		devr->ports[port].devr = devr;
33507722f47eSHaggai Eran 	}
33517722f47eSHaggai Eran 
3352e126ba97SEli Cohen 	return 0;
3353e126ba97SEli Cohen 
33544aa17b28SHaggai Abramonvsky error5:
33554aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
3356e126ba97SEli Cohen error4:
3357e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3358e126ba97SEli Cohen error3:
3359e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3360e126ba97SEli Cohen error2:
3361e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3362e126ba97SEli Cohen error1:
3363e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
3364e126ba97SEli Cohen error0:
3365e126ba97SEli Cohen 	return ret;
3366e126ba97SEli Cohen }
3367e126ba97SEli Cohen 
3368e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3369e126ba97SEli Cohen {
33707722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
33717722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
33727722f47eSHaggai Eran 	int port;
33737722f47eSHaggai Eran 
33744aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
3375e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
3376e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3377e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3378e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3379e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
33807722f47eSHaggai Eran 
33817722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
33827722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
33837722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
3384e126ba97SEli Cohen }
3385e126ba97SEli Cohen 
3386e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
3387e53505a8SAchiad Shochat {
3388e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3389e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3390e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3391e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3392e53505a8SAchiad Shochat 	u32 ret = 0;
3393e53505a8SAchiad Shochat 
3394e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
3395e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
3396e53505a8SAchiad Shochat 
339772cd5717SOr Gerlitz 	ret = RDMA_CORE_PORT_RAW_PACKET;
339872cd5717SOr Gerlitz 
3399e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
340072cd5717SOr Gerlitz 		return ret;
3401e53505a8SAchiad Shochat 
3402e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
340372cd5717SOr Gerlitz 		return ret;
3404e53505a8SAchiad Shochat 
3405e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3406e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
3407e53505a8SAchiad Shochat 
3408e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3409e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3410e53505a8SAchiad Shochat 
3411e53505a8SAchiad Shochat 	return ret;
3412e53505a8SAchiad Shochat }
3413e53505a8SAchiad Shochat 
34147738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
34157738613eSIra Weiny 			       struct ib_port_immutable *immutable)
34167738613eSIra Weiny {
34177738613eSIra Weiny 	struct ib_port_attr attr;
3418ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3419ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
34207738613eSIra Weiny 	int err;
34217738613eSIra Weiny 
3422c4550c63SOr Gerlitz 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3423c4550c63SOr Gerlitz 
3424c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
34257738613eSIra Weiny 	if (err)
34267738613eSIra Weiny 		return err;
34277738613eSIra Weiny 
34287738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
34297738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
3430e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3431ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3432337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
34337738613eSIra Weiny 
34347738613eSIra Weiny 	return 0;
34357738613eSIra Weiny }
34367738613eSIra Weiny 
34379abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
3438c7342823SIra Weiny {
3439c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
3440c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
34419abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
34429abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
34439abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
3444c7342823SIra Weiny }
3445c7342823SIra Weiny 
344645f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
34479ef9c640SAviv Heller {
34489ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
34499ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
34509ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
34519ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
34529ef9c640SAviv Heller 	int err;
34539ef9c640SAviv Heller 
34549ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
34559ef9c640SAviv Heller 		return 0;
34569ef9c640SAviv Heller 
34579ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
34589ef9c640SAviv Heller 	if (err)
34599ef9c640SAviv Heller 		return err;
34609ef9c640SAviv Heller 
34619ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
34629ef9c640SAviv Heller 	if (IS_ERR(ft)) {
34639ef9c640SAviv Heller 		err = PTR_ERR(ft);
34649ef9c640SAviv Heller 		goto err_destroy_vport_lag;
34659ef9c640SAviv Heller 	}
34669ef9c640SAviv Heller 
34679ef9c640SAviv Heller 	dev->flow_db.lag_demux_ft = ft;
34689ef9c640SAviv Heller 	return 0;
34699ef9c640SAviv Heller 
34709ef9c640SAviv Heller err_destroy_vport_lag:
34719ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
34729ef9c640SAviv Heller 	return err;
34739ef9c640SAviv Heller }
34749ef9c640SAviv Heller 
347545f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
34769ef9c640SAviv Heller {
34779ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
34789ef9c640SAviv Heller 
34799ef9c640SAviv Heller 	if (dev->flow_db.lag_demux_ft) {
34809ef9c640SAviv Heller 		mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
34819ef9c640SAviv Heller 		dev->flow_db.lag_demux_ft = NULL;
34829ef9c640SAviv Heller 
34839ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
34849ef9c640SAviv Heller 	}
34859ef9c640SAviv Heller }
34869ef9c640SAviv Heller 
3487d012f5d6SOr Gerlitz static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
3488fc24fc5eSAchiad Shochat {
3489e53505a8SAchiad Shochat 	int err;
3490e53505a8SAchiad Shochat 
3491fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
3492e53505a8SAchiad Shochat 	err = register_netdevice_notifier(&dev->roce.nb);
34935ec8c83eSAviv Heller 	if (err) {
34945ec8c83eSAviv Heller 		dev->roce.nb.notifier_call = NULL;
3495e53505a8SAchiad Shochat 		return err;
34965ec8c83eSAviv Heller 	}
3497e53505a8SAchiad Shochat 
3498d012f5d6SOr Gerlitz 	return 0;
3499d012f5d6SOr Gerlitz }
3500d012f5d6SOr Gerlitz 
3501d012f5d6SOr Gerlitz static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
3502e126ba97SEli Cohen {
3503e126ba97SEli Cohen 	if (dev->roce.nb.notifier_call) {
3504e126ba97SEli Cohen 		unregister_netdevice_notifier(&dev->roce.nb);
3505e126ba97SEli Cohen 		dev->roce.nb.notifier_call = NULL;
3506e126ba97SEli Cohen 	}
3507e126ba97SEli Cohen }
3508e126ba97SEli Cohen 
350945f95acdSOr Gerlitz static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3510e126ba97SEli Cohen {
3511e126ba97SEli Cohen 	int err;
3512e126ba97SEli Cohen 
3513d012f5d6SOr Gerlitz 	err = mlx5_add_netdev_notifier(dev);
3514d012f5d6SOr Gerlitz 	if (err)
3515e53505a8SAchiad Shochat 		return err;
3516e53505a8SAchiad Shochat 
3517ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
3518e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3519e53505a8SAchiad Shochat 		if (err)
3520e53505a8SAchiad Shochat 			goto err_unregister_netdevice_notifier;
3521ca5b91d6SOr Gerlitz 	}
3522e53505a8SAchiad Shochat 
352345f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
35249ef9c640SAviv Heller 	if (err)
35259ef9c640SAviv Heller 		goto err_disable_roce;
35269ef9c640SAviv Heller 
3527e53505a8SAchiad Shochat 	return 0;
3528e53505a8SAchiad Shochat 
35299ef9c640SAviv Heller err_disable_roce:
3530ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
35319ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
35329ef9c640SAviv Heller 
3533e53505a8SAchiad Shochat err_unregister_netdevice_notifier:
3534d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
3535e53505a8SAchiad Shochat 	return err;
3536fc24fc5eSAchiad Shochat }
3537fc24fc5eSAchiad Shochat 
353845f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3539fc24fc5eSAchiad Shochat {
354045f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3541ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
3542e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
3543fc24fc5eSAchiad Shochat }
3544fc24fc5eSAchiad Shochat 
3545e1f24a79SParav Pandit struct mlx5_ib_counter {
35467c16f477SKamal Heib 	const char *name;
35477c16f477SKamal Heib 	size_t offset;
35487c16f477SKamal Heib };
35497c16f477SKamal Heib 
35507c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
35517c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
35527c16f477SKamal Heib 
3553e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
35547c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
35557c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
35567c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
35577c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
35587c16f477SKamal Heib };
35597c16f477SKamal Heib 
3560e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
35617c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
35627c16f477SKamal Heib };
35637c16f477SKamal Heib 
3564e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
35657c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
35667c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
35677c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
35687c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
35697c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
35707c16f477SKamal Heib };
35717c16f477SKamal Heib 
3572e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
3573e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
3574e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3575e1f24a79SParav Pandit 
3576e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
3577e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
3578e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
3579e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3580e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
3581e1f24a79SParav Pandit };
3582e1f24a79SParav Pandit 
358358dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
358458dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
358558dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
358658dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
358758dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
358858dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
358958dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
359058dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
359158dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
359258dcb60aSParav Pandit };
359358dcb60aSParav Pandit 
3594e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
35950837e86aSMark Bloch {
35960837e86aSMark Bloch 	unsigned int i;
35970837e86aSMark Bloch 
35987c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
35990837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
3600e1f24a79SParav Pandit 					    dev->port[i].cnts.set_id);
3601e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
3602e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
36037c16f477SKamal Heib 	}
36047c16f477SKamal Heib }
36057c16f477SKamal Heib 
3606e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3607e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
36087c16f477SKamal Heib {
36097c16f477SKamal Heib 	u32 num_counters;
36107c16f477SKamal Heib 
36117c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
36127c16f477SKamal Heib 
36137c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
36147c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
36157c16f477SKamal Heib 
36167c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
36177c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
361858dcb60aSParav Pandit 
361958dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
362058dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
362158dcb60aSParav Pandit 
3622e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
36237c16f477SKamal Heib 
3624e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3625e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3626e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
3627e1f24a79SParav Pandit 	}
3628e1f24a79SParav Pandit 
3629e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3630e1f24a79SParav Pandit 	if (!cnts->names)
36317c16f477SKamal Heib 		return -ENOMEM;
36327c16f477SKamal Heib 
3633e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
3634e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
3635e1f24a79SParav Pandit 	if (!cnts->offsets)
36367c16f477SKamal Heib 		goto err_names;
36377c16f477SKamal Heib 
36387c16f477SKamal Heib 	return 0;
36397c16f477SKamal Heib 
36407c16f477SKamal Heib err_names:
3641e1f24a79SParav Pandit 	kfree(cnts->names);
36427c16f477SKamal Heib 	return -ENOMEM;
36437c16f477SKamal Heib }
36447c16f477SKamal Heib 
3645e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
36467c16f477SKamal Heib 				  const char **names,
36477c16f477SKamal Heib 				  size_t *offsets)
36487c16f477SKamal Heib {
36497c16f477SKamal Heib 	int i;
36507c16f477SKamal Heib 	int j = 0;
36517c16f477SKamal Heib 
36527c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
36537c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
36547c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
36557c16f477SKamal Heib 	}
36567c16f477SKamal Heib 
36577c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
36587c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
36597c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
36607c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
36617c16f477SKamal Heib 		}
36627c16f477SKamal Heib 	}
36637c16f477SKamal Heib 
36647c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
36657c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
36667c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
36677c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
36687c16f477SKamal Heib 		}
36697c16f477SKamal Heib 	}
3670e1f24a79SParav Pandit 
367158dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
367258dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
367358dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
367458dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
367558dcb60aSParav Pandit 		}
367658dcb60aSParav Pandit 	}
367758dcb60aSParav Pandit 
3678e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3679e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3680e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
3681e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
3682e1f24a79SParav Pandit 		}
3683e1f24a79SParav Pandit 	}
36840837e86aSMark Bloch }
36850837e86aSMark Bloch 
3686e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
36870837e86aSMark Bloch {
36880837e86aSMark Bloch 	int i;
36890837e86aSMark Bloch 	int ret;
36900837e86aSMark Bloch 
36910837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
36927c16f477SKamal Heib 		struct mlx5_ib_port *port = &dev->port[i];
36937c16f477SKamal Heib 
36940837e86aSMark Bloch 		ret = mlx5_core_alloc_q_counter(dev->mdev,
3695e1f24a79SParav Pandit 						&port->cnts.set_id);
36960837e86aSMark Bloch 		if (ret) {
36970837e86aSMark Bloch 			mlx5_ib_warn(dev,
36980837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
36990837e86aSMark Bloch 				     i + 1, ret);
37000837e86aSMark Bloch 			goto dealloc_counters;
37010837e86aSMark Bloch 		}
37027c16f477SKamal Heib 
3703e1f24a79SParav Pandit 		ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
37047c16f477SKamal Heib 		if (ret)
37057c16f477SKamal Heib 			goto dealloc_counters;
37067c16f477SKamal Heib 
3707e1f24a79SParav Pandit 		mlx5_ib_fill_counters(dev, port->cnts.names,
3708e1f24a79SParav Pandit 				      port->cnts.offsets);
37090837e86aSMark Bloch 	}
37100837e86aSMark Bloch 
37110837e86aSMark Bloch 	return 0;
37120837e86aSMark Bloch 
37130837e86aSMark Bloch dealloc_counters:
37140837e86aSMark Bloch 	while (--i >= 0)
37150837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
3716e1f24a79SParav Pandit 					    dev->port[i].cnts.set_id);
37170837e86aSMark Bloch 
37180837e86aSMark Bloch 	return ret;
37190837e86aSMark Bloch }
37200837e86aSMark Bloch 
37210ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
37220ad17a8fSMark Bloch 						    u8 port_num)
37230ad17a8fSMark Bloch {
37247c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
37257c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
37260ad17a8fSMark Bloch 
37270ad17a8fSMark Bloch 	/* We support only per port stats */
37280ad17a8fSMark Bloch 	if (port_num == 0)
37290ad17a8fSMark Bloch 		return NULL;
37300ad17a8fSMark Bloch 
3731e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
3732e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
3733e1f24a79SParav Pandit 					  port->cnts.num_cong_counters,
37340ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
37350ad17a8fSMark Bloch }
37360ad17a8fSMark Bloch 
3737e1f24a79SParav Pandit static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3738e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
3739e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
3740e1f24a79SParav Pandit {
3741e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3742e1f24a79SParav Pandit 	void *out;
3743e1f24a79SParav Pandit 	__be32 val;
3744e1f24a79SParav Pandit 	int ret, i;
3745e1f24a79SParav Pandit 
37461b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
3747e1f24a79SParav Pandit 	if (!out)
3748e1f24a79SParav Pandit 		return -ENOMEM;
3749e1f24a79SParav Pandit 
3750e1f24a79SParav Pandit 	ret = mlx5_core_query_q_counter(dev->mdev,
3751e1f24a79SParav Pandit 					port->cnts.set_id, 0,
3752e1f24a79SParav Pandit 					out, outlen);
3753e1f24a79SParav Pandit 	if (ret)
3754e1f24a79SParav Pandit 		goto free;
3755e1f24a79SParav Pandit 
3756e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
3757e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
3758e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
3759e1f24a79SParav Pandit 	}
3760e1f24a79SParav Pandit 
3761e1f24a79SParav Pandit free:
3762e1f24a79SParav Pandit 	kvfree(out);
3763e1f24a79SParav Pandit 	return ret;
3764e1f24a79SParav Pandit }
3765e1f24a79SParav Pandit 
37660ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
37670ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
37687c16f477SKamal Heib 				u8 port_num, int index)
37690ad17a8fSMark Bloch {
37700ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
37717c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
3772e1f24a79SParav Pandit 	int ret, num_counters;
37730ad17a8fSMark Bloch 
37747c16f477SKamal Heib 	if (!stats)
3775e1f24a79SParav Pandit 		return -EINVAL;
37760ad17a8fSMark Bloch 
3777e1f24a79SParav Pandit 	ret = mlx5_ib_query_q_counters(dev, port, stats);
37780ad17a8fSMark Bloch 	if (ret)
3779e1f24a79SParav Pandit 		return ret;
3780e1f24a79SParav Pandit 	num_counters = port->cnts.num_q_counters;
37810ad17a8fSMark Bloch 
3782e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
378371a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
378471a0ff65SMajd Dibbiny 						   stats->value +
378571a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters,
378671a0ff65SMajd Dibbiny 						   port->cnts.num_cong_counters,
378771a0ff65SMajd Dibbiny 						   port->cnts.offsets +
378871a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters);
3789e1f24a79SParav Pandit 		if (ret)
3790e1f24a79SParav Pandit 			return ret;
3791e1f24a79SParav Pandit 		num_counters += port->cnts.num_cong_counters;
37920ad17a8fSMark Bloch 	}
37937c16f477SKamal Heib 
3794e1f24a79SParav Pandit 	return num_counters;
37950ad17a8fSMark Bloch }
37960ad17a8fSMark Bloch 
37978e959601SNiranjana Vishwanathapura static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
37988e959601SNiranjana Vishwanathapura {
37998e959601SNiranjana Vishwanathapura 	return mlx5_rdma_netdev_free(netdev);
38008e959601SNiranjana Vishwanathapura }
38018e959601SNiranjana Vishwanathapura 
3802693dfd5aSErez Shitrit static struct net_device*
3803693dfd5aSErez Shitrit mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3804693dfd5aSErez Shitrit 			  u8 port_num,
3805693dfd5aSErez Shitrit 			  enum rdma_netdev_t type,
3806693dfd5aSErez Shitrit 			  const char *name,
3807693dfd5aSErez Shitrit 			  unsigned char name_assign_type,
3808693dfd5aSErez Shitrit 			  void (*setup)(struct net_device *))
3809693dfd5aSErez Shitrit {
38108e959601SNiranjana Vishwanathapura 	struct net_device *netdev;
38118e959601SNiranjana Vishwanathapura 	struct rdma_netdev *rn;
38128e959601SNiranjana Vishwanathapura 
3813693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
3814693dfd5aSErez Shitrit 		return ERR_PTR(-EOPNOTSUPP);
3815693dfd5aSErez Shitrit 
38168e959601SNiranjana Vishwanathapura 	netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3817693dfd5aSErez Shitrit 					name, setup);
38188e959601SNiranjana Vishwanathapura 	if (likely(!IS_ERR_OR_NULL(netdev))) {
38198e959601SNiranjana Vishwanathapura 		rn = netdev_priv(netdev);
38208e959601SNiranjana Vishwanathapura 		rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
3821693dfd5aSErez Shitrit 	}
38228e959601SNiranjana Vishwanathapura 	return netdev;
3823693dfd5aSErez Shitrit }
3824693dfd5aSErez Shitrit 
3825fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
3826fe248c3aSMaor Gottlieb {
3827fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
3828fe248c3aSMaor Gottlieb 		return;
3829fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
3830fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
3831fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
3832fe248c3aSMaor Gottlieb }
3833fe248c3aSMaor Gottlieb 
383403404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
383503404e8aSMaor Gottlieb {
383603404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
383703404e8aSMaor Gottlieb 		return;
383803404e8aSMaor Gottlieb 
383903404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
3840fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
3841fe248c3aSMaor Gottlieb }
3842fe248c3aSMaor Gottlieb 
3843fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3844fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
3845fe248c3aSMaor Gottlieb {
3846fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3847fe248c3aSMaor Gottlieb 	char lbuf[20];
3848fe248c3aSMaor Gottlieb 	int len;
3849fe248c3aSMaor Gottlieb 
3850fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3851fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
3852fe248c3aSMaor Gottlieb }
3853fe248c3aSMaor Gottlieb 
3854fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3855fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
3856fe248c3aSMaor Gottlieb {
3857fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3858fe248c3aSMaor Gottlieb 	u32 timeout;
3859fe248c3aSMaor Gottlieb 	u32 var;
3860fe248c3aSMaor Gottlieb 
3861fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
3862fe248c3aSMaor Gottlieb 		return -EFAULT;
3863fe248c3aSMaor Gottlieb 
3864fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3865fe248c3aSMaor Gottlieb 			1000);
3866fe248c3aSMaor Gottlieb 	if (timeout != var)
3867fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3868fe248c3aSMaor Gottlieb 			    timeout);
3869fe248c3aSMaor Gottlieb 
3870fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
3871fe248c3aSMaor Gottlieb 
3872fe248c3aSMaor Gottlieb 	return count;
3873fe248c3aSMaor Gottlieb }
3874fe248c3aSMaor Gottlieb 
3875fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
3876fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
3877fe248c3aSMaor Gottlieb 	.open	= simple_open,
3878fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
3879fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
3880fe248c3aSMaor Gottlieb };
3881fe248c3aSMaor Gottlieb 
3882fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
3883fe248c3aSMaor Gottlieb {
3884fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
3885fe248c3aSMaor Gottlieb 
3886fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
3887fe248c3aSMaor Gottlieb 		return 0;
3888fe248c3aSMaor Gottlieb 
3889fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
3890fe248c3aSMaor Gottlieb 	if (!dbg)
3891fe248c3aSMaor Gottlieb 		return -ENOMEM;
3892fe248c3aSMaor Gottlieb 
3893cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
3894cbafad87SSudip Mukherjee 
3895fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
3896fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
3897fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
3898fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
3899cbafad87SSudip Mukherjee 		goto out_debugfs;
3900fe248c3aSMaor Gottlieb 
3901fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
3902fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
3903fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
3904fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
3905fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
3906fe248c3aSMaor Gottlieb 		goto out_debugfs;
3907fe248c3aSMaor Gottlieb 
3908fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
3909fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
3910fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
3911fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
3912fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
3913fe248c3aSMaor Gottlieb 		goto out_debugfs;
3914fe248c3aSMaor Gottlieb 
3915fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
3916fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
3917fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
3918fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
3919fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
3920fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
3921fe248c3aSMaor Gottlieb 		goto out_debugfs;
3922fe248c3aSMaor Gottlieb 
3923fe248c3aSMaor Gottlieb 	return 0;
3924fe248c3aSMaor Gottlieb 
3925fe248c3aSMaor Gottlieb out_debugfs:
3926fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
3927fe248c3aSMaor Gottlieb 	return -ENOMEM;
392803404e8aSMaor Gottlieb }
392903404e8aSMaor Gottlieb 
393003404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
393103404e8aSMaor Gottlieb {
393203404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
393303404e8aSMaor Gottlieb 		return;
393403404e8aSMaor Gottlieb 
393503404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
393603404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
393703404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
393803404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
393903404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
3940fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
3941fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
3942fe248c3aSMaor Gottlieb 
3943fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
3944fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
394503404e8aSMaor Gottlieb }
394603404e8aSMaor Gottlieb 
394784305d71SLeon Romanovsky static const struct cpumask *
394884305d71SLeon Romanovsky mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
394940b24403SSagi Grimberg {
395040b24403SSagi Grimberg 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
395140b24403SSagi Grimberg 
395240b24403SSagi Grimberg 	return mlx5_get_vector_affinity(dev->mdev, comp_vector);
395340b24403SSagi Grimberg }
395440b24403SSagi Grimberg 
39559603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
3956e126ba97SEli Cohen {
3957e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3958ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
3959ebd61f68SAchiad Shochat 	int port_type_cap;
39604babcf97SAviv Heller 	const char *name;
3961e126ba97SEli Cohen 	int err;
3962e126ba97SEli Cohen 	int i;
3963e126ba97SEli Cohen 
3964ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3965ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3966ebd61f68SAchiad Shochat 
3967e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
3968e126ba97SEli Cohen 
3969e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3970e126ba97SEli Cohen 	if (!dev)
39719603b61dSJack Morgenstein 		return NULL;
3972e126ba97SEli Cohen 
39739603b61dSJack Morgenstein 	dev->mdev = mdev;
3974e126ba97SEli Cohen 
39750837e86aSMark Bloch 	dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
39760837e86aSMark Bloch 			    GFP_KERNEL);
39770837e86aSMark Bloch 	if (!dev->port)
39780837e86aSMark Bloch 		goto err_dealloc;
39790837e86aSMark Bloch 
3980fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
3981e126ba97SEli Cohen 	err = get_port_caps(dev);
3982e126ba97SEli Cohen 	if (err)
39830837e86aSMark Bloch 		goto err_free_port;
3984e126ba97SEli Cohen 
39851b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3986e126ba97SEli Cohen 		get_ext_port_caps(dev);
3987e126ba97SEli Cohen 
39884babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
39894babcf97SAviv Heller 		name = "mlx5_%d";
39904babcf97SAviv Heller 	else
39914babcf97SAviv Heller 		name = "mlx5_bond_%d";
39924babcf97SAviv Heller 
39934babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
3994e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
3995e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
3996c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
3997938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
3998e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
3999233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
4000233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
40019b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
4002e126ba97SEli Cohen 
4003e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
4004e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
4005e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
4006e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
4007e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
4008e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
4009e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
401041c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
401141c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
4012e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
401356e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
4014e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
4015e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
4016e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
4017e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
4018e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
4019e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
4020e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
4021e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
4022e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
4023e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
4024e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
4025e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
4026e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
4027e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
4028e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
4029e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
4030e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
40311707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
4032d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
4033d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
40347d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
4035b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
4036b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
4037e126ba97SEli Cohen 
4038e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
4039e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
4040ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
4041fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
4042fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
4043e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
40443cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
40453cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
4046e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
4047e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
4048e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
4049e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
4050e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
4051e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
4052e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
4053e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
4054e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
4055e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
4056e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
4057e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
4058e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
4059e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
4060e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
4061e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
4062e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
4063e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
4064e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
4065e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
4066e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
4067e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
4068e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
4069e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
4070e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
4071e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
4072e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
4073e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
4074e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
4075e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
407656e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
4077e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
4078e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
4079e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
4080e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
40819bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
40828a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
4083d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
40847738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
4085c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
408640b24403SSagi Grimberg 	dev->ib_dev.get_vector_affinity	= mlx5_ib_get_vector_affinity;
40878e959601SNiranjana Vishwanathapura 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
4088693dfd5aSErez Shitrit 		dev->ib_dev.alloc_rdma_netdev	= mlx5_ib_alloc_rdma_netdev;
40898e959601SNiranjana Vishwanathapura 
4090eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
4091eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
4092eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
4093eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
4094eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
4095eff901d3SEli Cohen 	}
4096e126ba97SEli Cohen 
40977c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
40987c2344c3SMaor Gottlieb 
4099938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
41008cdd312cSHaggai Eran 
41016e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
41026e8484c5SMax Gurtovoy 
4103d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
4104d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
4105d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
4106d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
4107d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
4108d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4109d2370e0aSMatan Barak 	}
4110d2370e0aSMatan Barak 
41117c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
41120ad17a8fSMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
41130ad17a8fSMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
41140ad17a8fSMark Bloch 	}
41150ad17a8fSMark Bloch 
4116938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
4117e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4118e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4119e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
4120e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4121e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4122e126ba97SEli Cohen 	}
4123e126ba97SEli Cohen 
4124038d2ef8SMaor Gottlieb 	dev->ib_dev.create_flow	= mlx5_ib_create_flow;
4125038d2ef8SMaor Gottlieb 	dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
412681e30880SYishai Hadas 	dev->ib_dev.uverbs_ex_cmd_mask |=
412781e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
412881e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
412981e30880SYishai Hadas 
413081e30880SYishai Hadas 	if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
413181e30880SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
413279b20a6cSYishai Hadas 		dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
413379b20a6cSYishai Hadas 		dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
413479b20a6cSYishai Hadas 		dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
4135c5f90929SYishai Hadas 		dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4136c5f90929SYishai Hadas 		dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
4137038d2ef8SMaor Gottlieb 		dev->ib_dev.uverbs_ex_cmd_mask |=
413879b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
413979b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
4140c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4141c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4142c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
4143038d2ef8SMaor Gottlieb 	}
4144e126ba97SEli Cohen 	err = init_node_data(dev);
4145e126ba97SEli Cohen 	if (err)
414690be7c8aSMajd Dibbiny 		goto err_free_port;
4147e126ba97SEli Cohen 
4148038d2ef8SMaor Gottlieb 	mutex_init(&dev->flow_db.lock);
4149e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
415089ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&dev->qp_list);
415189ea94a7SMaor Gottlieb 	spin_lock_init(&dev->reset_flow_resource_lock);
4152e126ba97SEli Cohen 
4153fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
415445f95acdSOr Gerlitz 		err = mlx5_enable_eth(dev);
4155e126ba97SEli Cohen 		if (err)
415690be7c8aSMajd Dibbiny 			goto err_free_port;
4157fd65f1b8SMoni Shoua 		dev->roce.last_port_state = IB_PORT_DOWN;
4158fc24fc5eSAchiad Shochat 	}
4159fc24fc5eSAchiad Shochat 
4160fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
4161fc24fc5eSAchiad Shochat 	if (err)
416245f95acdSOr Gerlitz 		goto err_disable_eth;
4163e126ba97SEli Cohen 
41646aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
4165281d1a92SWei Yongjun 	if (err)
4166e126ba97SEli Cohen 		goto err_rsrc;
4167e126ba97SEli Cohen 
416845bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
4169e1f24a79SParav Pandit 		err = mlx5_ib_alloc_counters(dev);
41706aec21f6SHaggai Eran 		if (err)
41716aec21f6SHaggai Eran 			goto err_odp;
417245bded2cSKamal Heib 	}
41736aec21f6SHaggai Eran 
41744a2da0b8SParav Pandit 	err = mlx5_ib_init_cong_debugfs(dev);
41754a2da0b8SParav Pandit 	if (err)
41764a2da0b8SParav Pandit 		goto err_cnt;
41774a2da0b8SParav Pandit 
41785fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
41795fe9dec0SEli Cohen 	if (!dev->mdev->priv.uar)
41804a2da0b8SParav Pandit 		goto err_cong;
41815fe9dec0SEli Cohen 
41825fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
41835fe9dec0SEli Cohen 	if (err)
41845fe9dec0SEli Cohen 		goto err_uar_page;
41855fe9dec0SEli Cohen 
41865fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
41875fe9dec0SEli Cohen 	if (err)
41885fe9dec0SEli Cohen 		goto err_bfreg;
41895fe9dec0SEli Cohen 
41900837e86aSMark Bloch 	err = ib_register_device(&dev->ib_dev, NULL);
41910837e86aSMark Bloch 	if (err)
41925fe9dec0SEli Cohen 		goto err_fp_bfreg;
41930837e86aSMark Bloch 
4194e126ba97SEli Cohen 	err = create_umr_res(dev);
4195e126ba97SEli Cohen 	if (err)
4196e126ba97SEli Cohen 		goto err_dev;
4197e126ba97SEli Cohen 
419803404e8aSMaor Gottlieb 	init_delay_drop(dev);
419903404e8aSMaor Gottlieb 
4200e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
4201281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
4202281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
4203281d1a92SWei Yongjun 		if (err)
420403404e8aSMaor Gottlieb 			goto err_delay_drop;
4205e126ba97SEli Cohen 	}
4206e126ba97SEli Cohen 
4207c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4208c85023e1SHuy Nguyen 	    MLX5_CAP_GEN(mdev, disable_local_lb))
4209c85023e1SHuy Nguyen 		mutex_init(&dev->lb_mutex);
4210c85023e1SHuy Nguyen 
4211e126ba97SEli Cohen 	dev->ib_active = true;
4212e126ba97SEli Cohen 
42139603b61dSJack Morgenstein 	return dev;
4214e126ba97SEli Cohen 
421503404e8aSMaor Gottlieb err_delay_drop:
421603404e8aSMaor Gottlieb 	cancel_delay_drop(dev);
4217e126ba97SEli Cohen 	destroy_umrc_res(dev);
4218e126ba97SEli Cohen 
4219e126ba97SEli Cohen err_dev:
4220e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
4221e126ba97SEli Cohen 
42225fe9dec0SEli Cohen err_fp_bfreg:
42235fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
42245fe9dec0SEli Cohen 
42255fe9dec0SEli Cohen err_bfreg:
42265fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
42275fe9dec0SEli Cohen 
42285fe9dec0SEli Cohen err_uar_page:
42295fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
42305fe9dec0SEli Cohen 
42314a2da0b8SParav Pandit err_cong:
4232e19cd282SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev);
4233e19cd282SParav Pandit err_cnt:
423445bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
4235e1f24a79SParav Pandit 		mlx5_ib_dealloc_counters(dev);
42360837e86aSMark Bloch 
42376aec21f6SHaggai Eran err_odp:
42386aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
42396aec21f6SHaggai Eran 
4240e126ba97SEli Cohen err_rsrc:
4241e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
4242e126ba97SEli Cohen 
424345f95acdSOr Gerlitz err_disable_eth:
42445ec8c83eSAviv Heller 	if (ll == IB_LINK_LAYER_ETHERNET) {
424545f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
4246d012f5d6SOr Gerlitz 		mlx5_remove_netdev_notifier(dev);
42475ec8c83eSAviv Heller 	}
4248fc24fc5eSAchiad Shochat 
42490837e86aSMark Bloch err_free_port:
42500837e86aSMark Bloch 	kfree(dev->port);
42510837e86aSMark Bloch 
42529603b61dSJack Morgenstein err_dealloc:
4253e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
4254e126ba97SEli Cohen 
42559603b61dSJack Morgenstein 	return NULL;
4256e126ba97SEli Cohen }
4257e126ba97SEli Cohen 
42589603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
4259e126ba97SEli Cohen {
42609603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
4261fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
42626aec21f6SHaggai Eran 
426303404e8aSMaor Gottlieb 	cancel_delay_drop(dev);
4264d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
4265e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
42665fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
42675fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
42685fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
42694a2da0b8SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev);
427045bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
4271e1f24a79SParav Pandit 		mlx5_ib_dealloc_counters(dev);
4272eefd56e5SEli Cohen 	destroy_umrc_res(dev);
42736aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
4274e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
4275fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
427645f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
42770837e86aSMark Bloch 	kfree(dev->port);
4278e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
4279e126ba97SEli Cohen }
4280e126ba97SEli Cohen 
42819603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
42829603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
42839603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
42849603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
4285d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4286d9aaed83SArtemy Kovalyov 	.pfault		= mlx5_ib_pfault,
4287d9aaed83SArtemy Kovalyov #endif
428864613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
4289e126ba97SEli Cohen };
4290e126ba97SEli Cohen 
4291e126ba97SEli Cohen static int __init mlx5_ib_init(void)
4292e126ba97SEli Cohen {
42936aec21f6SHaggai Eran 	int err;
42946aec21f6SHaggai Eran 
429581713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
42969603b61dSJack Morgenstein 
42976aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
42986aec21f6SHaggai Eran 
42996aec21f6SHaggai Eran 	return err;
4300e126ba97SEli Cohen }
4301e126ba97SEli Cohen 
4302e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
4303e126ba97SEli Cohen {
43049603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
4305e126ba97SEli Cohen }
4306e126ba97SEli Cohen 
4307e126ba97SEli Cohen module_init(mlx5_ib_init);
4308e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
4309